diagnostic_emitter.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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(loc_id.loc_id(), loc_id.is_token_only(), 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, bool is_token_only,
  34. ContextFnT context_fn) const
  35. -> Diagnostics::ConvertedLoc {
  36. llvm::SmallVector<SemIR::AbsoluteNodeId> absolute_node_ids =
  37. SemIR::GetAbsoluteNodeId(sem_ir_, loc_id);
  38. auto final_node_id = absolute_node_ids.pop_back_val();
  39. for (const auto& absolute_node_id : absolute_node_ids) {
  40. if (!absolute_node_id.node_id().has_value()) {
  41. // TODO: Add an "In implicit import of prelude." note for the case where
  42. // we don't have a location.
  43. continue;
  44. }
  45. // TODO: Include the name of the imported library in the diagnostic.
  46. auto diag_loc =
  47. ConvertLocInFile(absolute_node_id, is_token_only, context_fn);
  48. CARBON_DIAGNOSTIC(InImport, LocationInfo, "in import");
  49. context_fn(diag_loc.loc, InImport);
  50. }
  51. return ConvertLocInFile(final_node_id, is_token_only, context_fn);
  52. }
  53. auto DiagnosticEmitter::ConvertLocInFile(SemIR::AbsoluteNodeId absolute_node_id,
  54. bool token_only,
  55. ContextFnT /*context_fn*/) const
  56. -> Diagnostics::ConvertedLoc {
  57. if (absolute_node_id.check_ir_id() == SemIR::CheckIRId::Cpp) {
  58. // Special handling of Clang source locations.
  59. // TODO: Refactor to add an `InImport` pointing at the `Cpp` import, and
  60. // eliminate `InCppImport`.
  61. clang::SourceLocation clang_loc = sem_ir_->clang_source_locs().Get(
  62. absolute_node_id.clang_source_loc_id());
  63. CARBON_CHECK(sem_ir_->cpp_ast());
  64. clang::PresumedLoc presumed_loc =
  65. sem_ir_->cpp_ast()->getSourceManager().getPresumedLoc(clang_loc);
  66. return Diagnostics::ConvertedLoc{
  67. .loc = {.filename = presumed_loc.getFilename(),
  68. .line_number = static_cast<int32_t>(presumed_loc.getLine())},
  69. // TODO: Set `last_byte_offset` based on the `import Cpp` location.
  70. .last_byte_offset = 0};
  71. }
  72. const auto& tree_and_subtrees =
  73. tree_and_subtrees_getters_[absolute_node_id.check_ir_id().index]();
  74. return tree_and_subtrees.NodeToDiagnosticLoc(absolute_node_id.node_id(),
  75. token_only);
  76. }
  77. auto DiagnosticEmitter::ConvertArg(llvm::Any arg) const -> llvm::Any {
  78. if (auto* library_name_id = llvm::any_cast<SemIR::LibraryNameId>(&arg)) {
  79. std::string library_name;
  80. if (*library_name_id == SemIR::LibraryNameId::Default) {
  81. library_name = "default library";
  82. } else if (!library_name_id->has_value()) {
  83. library_name = "library <none>";
  84. } else {
  85. RawStringOstream stream;
  86. stream << "library \""
  87. << sem_ir_->string_literal_values().Get(
  88. library_name_id->AsStringLiteralValueId())
  89. << "\"";
  90. library_name = stream.TakeStr();
  91. }
  92. return library_name;
  93. }
  94. if (auto* name_id = llvm::any_cast<SemIR::NameId>(&arg)) {
  95. return sem_ir_->names().GetFormatted(*name_id).str();
  96. }
  97. if (auto* type_of_expr = llvm::any_cast<TypeOfInstId>(&arg)) {
  98. if (!type_of_expr->inst_id.has_value()) {
  99. return "<none>";
  100. }
  101. // TODO: Where possible, produce a better description of the type based on
  102. // the expression.
  103. return "`" +
  104. StringifyConstantInst(
  105. *sem_ir_,
  106. sem_ir_->types().GetInstId(
  107. sem_ir_->insts().Get(type_of_expr->inst_id).type_id())) +
  108. "`";
  109. }
  110. if (auto* expr = llvm::any_cast<InstIdAsConstant>(&arg)) {
  111. return "`" + StringifyConstantInst(*sem_ir_, expr->inst_id) + "`";
  112. }
  113. if (auto* type_expr = llvm::any_cast<InstIdAsRawType>(&arg)) {
  114. return StringifyConstantInst(*sem_ir_, type_expr->inst_id);
  115. }
  116. if (auto* type = llvm::any_cast<TypeIdAsRawType>(&arg)) {
  117. return StringifyConstantInst(*sem_ir_,
  118. sem_ir_->types().GetInstId(type->type_id));
  119. }
  120. if (auto* type_id = llvm::any_cast<SemIR::TypeId>(&arg)) {
  121. return "`" +
  122. StringifyConstantInst(*sem_ir_,
  123. sem_ir_->types().GetInstId(*type_id)) +
  124. "`";
  125. }
  126. if (auto* specific_id = llvm::any_cast<SemIR::SpecificId>(&arg)) {
  127. return "`" + StringifySpecific(*sem_ir_, *specific_id) + "`";
  128. }
  129. if (auto* typed_int = llvm::any_cast<TypedInt>(&arg)) {
  130. return llvm::APSInt(typed_int->value,
  131. !sem_ir_->types().IsSignedInt(typed_int->type));
  132. }
  133. if (auto* specific_interface_id =
  134. llvm::any_cast<SemIR::SpecificInterfaceId>(&arg)) {
  135. auto specific_interface =
  136. sem_ir_->specific_interfaces().Get(*specific_interface_id);
  137. return "`" + StringifySpecificInterface(*sem_ir_, specific_interface) + "`";
  138. }
  139. if (auto* specific_interface_raw =
  140. llvm::any_cast<SpecificInterfaceIdAsRawType>(&arg)) {
  141. auto specific_interface = sem_ir_->specific_interfaces().Get(
  142. specific_interface_raw->specific_interface_id);
  143. return StringifySpecificInterface(*sem_ir_, specific_interface);
  144. }
  145. return DiagnosticEmitterBase::ConvertArg(arg);
  146. }
  147. } // namespace Carbon::Check