diagnostic_emitter.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/diagnostic_loc_converter.h"
  12. #include "toolchain/sem_ir/ids.h"
  13. #include "toolchain/sem_ir/stringify.h"
  14. namespace Carbon::Check {
  15. auto DiagnosticEmitter::ConvertLoc(LocIdForDiagnostics loc_id,
  16. ContextFnT context_fn) const
  17. -> Diagnostics::ConvertedLoc {
  18. auto [imports, converted] = loc_converter_.ConvertWithImports(
  19. loc_id.loc_id(), loc_id.is_token_only());
  20. for (const auto& import : imports) {
  21. CARBON_DIAGNOSTIC(InImport, LocationInfo, "in import");
  22. CARBON_DIAGNOSTIC(InCppInclude, LocationInfo, "in file included here");
  23. CARBON_DIAGNOSTIC(InCppModule, LocationInfo, "in module imported here");
  24. CARBON_DIAGNOSTIC(InCppMacroExpansion, LocationInfo,
  25. "in expansion of macro defined here");
  26. switch (import.kind) {
  27. case Carbon::SemIR::DiagnosticLocConverter::ImportLoc::Import:
  28. // TODO: Include the library name in the note.
  29. context_fn(import.loc, InImport);
  30. break;
  31. case Carbon::SemIR::DiagnosticLocConverter::ImportLoc::CppInclude:
  32. // TODO: Include the file name in the note.
  33. context_fn(import.loc, InCppInclude);
  34. break;
  35. case Carbon::SemIR::DiagnosticLocConverter::ImportLoc::CppModuleImport:
  36. // TODO: Include the module name in the note.
  37. context_fn(import.loc, InCppModule);
  38. break;
  39. case Carbon::SemIR::DiagnosticLocConverter::ImportLoc::CppMacroExpansion:
  40. // TODO: Include the macro name in the note.
  41. context_fn(import.loc, InCppMacroExpansion);
  42. break;
  43. }
  44. }
  45. // Use the token when possible, but -1 is the default value.
  46. auto last_offset = -1;
  47. if (last_token_.has_value()) {
  48. last_offset = sem_ir_->parse_tree().tokens().GetByteOffset(last_token_);
  49. }
  50. // When the diagnostic is in the same file, we use the last possible offset;
  51. // otherwise, we ignore the offset because it's probably in that file.
  52. if (converted.loc.filename == sem_ir_->filename()) {
  53. converted.last_byte_offset =
  54. std::max(converted.last_byte_offset, last_offset);
  55. } else {
  56. converted.last_byte_offset = last_offset;
  57. }
  58. return converted;
  59. }
  60. auto DiagnosticEmitter::ConvertArg(llvm::Any arg) const -> llvm::Any {
  61. if (auto* library_name_id = llvm::any_cast<SemIR::LibraryNameId>(&arg)) {
  62. std::string library_name;
  63. if (*library_name_id == SemIR::LibraryNameId::Default) {
  64. library_name = "default library";
  65. } else if (!library_name_id->has_value()) {
  66. library_name = "library <none>";
  67. } else {
  68. RawStringOstream stream;
  69. stream << "library \""
  70. << sem_ir_->string_literal_values().Get(
  71. library_name_id->AsStringLiteralValueId())
  72. << "\"";
  73. library_name = stream.TakeStr();
  74. }
  75. return library_name;
  76. }
  77. if (auto* name_id = llvm::any_cast<SemIR::NameId>(&arg)) {
  78. return sem_ir_->names().GetFormatted(*name_id).str();
  79. }
  80. if (auto* type_of_expr = llvm::any_cast<TypeOfInstId>(&arg)) {
  81. if (!type_of_expr->inst_id.has_value()) {
  82. return "<none>";
  83. }
  84. // TODO: Where possible, produce a better description of the type based on
  85. // the expression.
  86. return "`" +
  87. StringifyConstantInst(
  88. *sem_ir_,
  89. sem_ir_->types().GetInstId(
  90. sem_ir_->insts().Get(type_of_expr->inst_id).type_id())) +
  91. "`";
  92. }
  93. if (auto* expr = llvm::any_cast<InstIdAsConstant>(&arg)) {
  94. return "`" + StringifyConstantInst(*sem_ir_, expr->inst_id) + "`";
  95. }
  96. if (auto* type_expr = llvm::any_cast<InstIdAsRawType>(&arg)) {
  97. return StringifyConstantInst(*sem_ir_, type_expr->inst_id);
  98. }
  99. if (auto* type = llvm::any_cast<TypeIdAsRawType>(&arg)) {
  100. return StringifyConstantInst(*sem_ir_,
  101. sem_ir_->types().GetInstId(type->type_id));
  102. }
  103. if (auto* type_id = llvm::any_cast<SemIR::TypeId>(&arg)) {
  104. return "`" +
  105. StringifyConstantInst(*sem_ir_,
  106. sem_ir_->types().GetInstId(*type_id)) +
  107. "`";
  108. }
  109. if (auto* specific_id = llvm::any_cast<SemIR::SpecificId>(&arg)) {
  110. return "`" + StringifySpecific(*sem_ir_, *specific_id) + "`";
  111. }
  112. if (auto* typed_int = llvm::any_cast<TypedInt>(&arg)) {
  113. return llvm::APSInt(typed_int->value,
  114. !sem_ir_->types().IsSignedInt(typed_int->type));
  115. }
  116. if (auto* real_id = llvm::any_cast<RealId>(&arg)) {
  117. RawStringOstream out;
  118. sem_ir_->reals().Get(*real_id).Print(out);
  119. return out.TakeStr();
  120. }
  121. if (auto* specific_interface_id =
  122. llvm::any_cast<SemIR::SpecificInterfaceId>(&arg)) {
  123. auto specific_interface =
  124. sem_ir_->specific_interfaces().Get(*specific_interface_id);
  125. return "`" + StringifySpecificInterface(*sem_ir_, specific_interface) + "`";
  126. }
  127. if (auto* specific_interface_raw =
  128. llvm::any_cast<SpecificInterfaceIdAsRawType>(&arg)) {
  129. auto specific_interface = sem_ir_->specific_interfaces().Get(
  130. specific_interface_raw->specific_interface_id);
  131. return StringifySpecificInterface(*sem_ir_, specific_interface);
  132. }
  133. return DiagnosticEmitterBase::ConvertArg(arg);
  134. }
  135. } // namespace Carbon::Check