diagnostic_emitter.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/diagnostic_emitter.h"
  5. #include <algorithm>
  6. #include <optional>
  7. #include <string>
  8. #include "common/raw_string_ostream.h"
  9. #include "toolchain/check/diagnostic_helpers.h"
  10. #include "toolchain/sem_ir/absolute_node_id.h"
  11. #include "toolchain/sem_ir/stringify.h"
  12. namespace Carbon::Check {
  13. auto DiagnosticEmitter::ConvertLoc(LocIdForDiagnostics loc_id,
  14. ContextFnT context_fn) const
  15. -> Diagnostics::ConvertedLoc {
  16. auto converted =
  17. ConvertLocImpl(static_cast<SemIR::LocId>(loc_id), context_fn);
  18. // Use the token when possible, but -1 is the default value.
  19. auto last_offset = -1;
  20. if (last_token_.has_value()) {
  21. last_offset = sem_ir_->parse_tree().tokens().GetByteOffset(last_token_);
  22. }
  23. // When the diagnostic is in the same file, we use the last possible offset;
  24. // otherwise, we ignore the offset because it's probably in that file.
  25. if (converted.loc.filename == sem_ir_->filename()) {
  26. converted.last_byte_offset =
  27. std::max(converted.last_byte_offset, last_offset);
  28. } else {
  29. converted.last_byte_offset = last_offset;
  30. }
  31. return converted;
  32. }
  33. auto DiagnosticEmitter::ConvertLocImpl(SemIR::LocId loc_id,
  34. ContextFnT context_fn) const
  35. -> Diagnostics::ConvertedLoc {
  36. llvm::SmallVector<SemIR::AbsoluteNodeId> absolute_node_ids =
  37. SemIR::GetAbsoluteNodeId(sem_ir_, loc_id);
  38. bool token_only =
  39. loc_id.kind() != SemIR::LocId::Kind::InstId && loc_id.is_token_only();
  40. auto final_node_id = absolute_node_ids.pop_back_val();
  41. for (const auto& absolute_node_id : absolute_node_ids) {
  42. if (!absolute_node_id.node_id().has_value()) {
  43. // TODO: Add an "In implicit import of prelude." note for the case where
  44. // we don't have a location.
  45. continue;
  46. }
  47. // TODO: Include the name of the imported library in the diagnostic.
  48. auto diag_loc = ConvertLocInFile(absolute_node_id, token_only, context_fn);
  49. CARBON_DIAGNOSTIC(InImport, LocationInfo, "in import");
  50. context_fn(diag_loc.loc, InImport);
  51. }
  52. return ConvertLocInFile(final_node_id, token_only, context_fn);
  53. }
  54. auto DiagnosticEmitter::ConvertLocInFile(SemIR::AbsoluteNodeId absolute_node_id,
  55. bool token_only,
  56. ContextFnT /*context_fn*/) const
  57. -> Diagnostics::ConvertedLoc {
  58. if (absolute_node_id.check_ir_id() == SemIR::CheckIRId::Cpp) {
  59. // Special handling of Clang source locations.
  60. // TODO: Refactor to add an `InImport` pointing at the `Cpp` import, and
  61. // eliminate `InCppImport`.
  62. clang::SourceLocation clang_loc = sem_ir_->clang_source_locs().Get(
  63. absolute_node_id.clang_source_loc_id());
  64. CARBON_CHECK(sem_ir_->cpp_ast());
  65. clang::PresumedLoc presumed_loc =
  66. sem_ir_->cpp_ast()->getSourceManager().getPresumedLoc(clang_loc);
  67. return Diagnostics::ConvertedLoc{
  68. .loc = {.filename = presumed_loc.getFilename(),
  69. .line_number = static_cast<int32_t>(presumed_loc.getLine())},
  70. // TODO: Set `last_byte_offset` based on the `import Cpp` location.
  71. .last_byte_offset = 0};
  72. }
  73. const auto& tree_and_subtrees =
  74. tree_and_subtrees_getters_[absolute_node_id.check_ir_id().index]();
  75. return tree_and_subtrees.NodeToDiagnosticLoc(absolute_node_id.node_id(),
  76. token_only);
  77. }
  78. auto DiagnosticEmitter::ConvertArg(llvm::Any arg) const -> llvm::Any {
  79. if (auto* library_name_id = llvm::any_cast<SemIR::LibraryNameId>(&arg)) {
  80. std::string library_name;
  81. if (*library_name_id == SemIR::LibraryNameId::Default) {
  82. library_name = "default library";
  83. } else if (!library_name_id->has_value()) {
  84. library_name = "library <none>";
  85. } else {
  86. RawStringOstream stream;
  87. stream << "library \""
  88. << sem_ir_->string_literal_values().Get(
  89. library_name_id->AsStringLiteralValueId())
  90. << "\"";
  91. library_name = stream.TakeStr();
  92. }
  93. return library_name;
  94. }
  95. if (auto* name_id = llvm::any_cast<SemIR::NameId>(&arg)) {
  96. return sem_ir_->names().GetFormatted(*name_id).str();
  97. }
  98. if (auto* type_of_expr = llvm::any_cast<TypeOfInstId>(&arg)) {
  99. if (!type_of_expr->inst_id.has_value()) {
  100. return "<none>";
  101. }
  102. // TODO: Where possible, produce a better description of the type based on
  103. // the expression.
  104. return "`" +
  105. StringifyConstantInst(
  106. *sem_ir_,
  107. sem_ir_->types().GetInstId(
  108. sem_ir_->insts().Get(type_of_expr->inst_id).type_id())) +
  109. "`";
  110. }
  111. if (auto* expr = llvm::any_cast<InstIdAsConstant>(&arg)) {
  112. return "`" + StringifyConstantInst(*sem_ir_, expr->inst_id) + "`";
  113. }
  114. if (auto* type_expr = llvm::any_cast<InstIdAsRawType>(&arg)) {
  115. return StringifyConstantInst(*sem_ir_, type_expr->inst_id);
  116. }
  117. if (auto* type = llvm::any_cast<TypeIdAsRawType>(&arg)) {
  118. return StringifyConstantInst(*sem_ir_,
  119. sem_ir_->types().GetInstId(type->type_id));
  120. }
  121. if (auto* type_id = llvm::any_cast<SemIR::TypeId>(&arg)) {
  122. return "`" +
  123. StringifyConstantInst(*sem_ir_,
  124. sem_ir_->types().GetInstId(*type_id)) +
  125. "`";
  126. }
  127. if (auto* specific_id = llvm::any_cast<SemIR::SpecificId>(&arg)) {
  128. return "`" + StringifySpecific(*sem_ir_, *specific_id) + "`";
  129. }
  130. if (auto* typed_int = llvm::any_cast<TypedInt>(&arg)) {
  131. return llvm::APSInt(typed_int->value,
  132. !sem_ir_->types().IsSignedInt(typed_int->type));
  133. }
  134. if (auto* specific_interface_id =
  135. llvm::any_cast<SemIR::SpecificInterfaceId>(&arg)) {
  136. auto specific_interface =
  137. sem_ir_->specific_interfaces().Get(*specific_interface_id);
  138. return "`" + StringifySpecificInterface(*sem_ir_, specific_interface) + "`";
  139. }
  140. if (auto* specific_interface_raw =
  141. llvm::any_cast<SpecificInterfaceIdAsRawType>(&arg)) {
  142. auto specific_interface = sem_ir_->specific_interfaces().Get(
  143. specific_interface_raw->specific_interface_id);
  144. return StringifySpecificInterface(*sem_ir_, specific_interface);
  145. }
  146. return DiagnosticEmitterBase::ConvertArg(arg);
  147. }
  148. } // namespace Carbon::Check