check.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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/check.h"
  5. #include <string>
  6. #include <utility>
  7. #include "common/check.h"
  8. #include "common/map.h"
  9. #include "toolchain/check/check_unit.h"
  10. #include "toolchain/check/context.h"
  11. #include "toolchain/check/cpp/import.h"
  12. #include "toolchain/check/diagnostic_emitter.h"
  13. #include "toolchain/check/diagnostic_helpers.h"
  14. #include "toolchain/diagnostics/diagnostic.h"
  15. #include "toolchain/diagnostics/diagnostic_consumer.h"
  16. #include "toolchain/diagnostics/format_providers.h"
  17. #include "toolchain/lex/token_kind.h"
  18. #include "toolchain/parse/node_ids.h"
  19. #include "toolchain/parse/tree.h"
  20. #include "toolchain/sem_ir/file.h"
  21. #include "toolchain/sem_ir/formatter.h"
  22. #include "toolchain/sem_ir/typed_insts.h"
  23. namespace Carbon::Check {
  24. // The package and library names, used as map keys.
  25. using ImportKey = std::pair<llvm::StringRef, llvm::StringRef>;
  26. // Returns a key form of the package object. file_package_id is only used for
  27. // imports, not the main package declaration; as a consequence, it will be
  28. // `None` for the main package declaration.
  29. static auto GetImportKey(UnitAndImports& unit_info,
  30. PackageNameId file_package_id,
  31. Parse::Tree::PackagingNames names) -> ImportKey {
  32. auto* stores = unit_info.unit->value_stores;
  33. PackageNameId package_id =
  34. names.package_id.has_value() ? names.package_id : file_package_id;
  35. llvm::StringRef package_name;
  36. if (package_id.has_value()) {
  37. auto package_ident_id = package_id.AsIdentifierId();
  38. package_name = package_ident_id.has_value()
  39. ? stores->identifiers().Get(package_ident_id)
  40. : package_id.AsSpecialName();
  41. }
  42. llvm::StringRef library_name =
  43. names.library_id.has_value()
  44. ? stores->string_literal_values().Get(names.library_id)
  45. : "";
  46. return {package_name, library_name};
  47. }
  48. static constexpr llvm::StringLiteral CppPackageName = "Cpp";
  49. static constexpr llvm::StringLiteral MainPackageName = "Main";
  50. static auto RenderImportKey(ImportKey import_key) -> std::string {
  51. if (import_key.first.empty()) {
  52. import_key.first = MainPackageName;
  53. }
  54. if (import_key.second.empty()) {
  55. return import_key.first.str();
  56. }
  57. return llvm::formatv("{0}//{1}", import_key.first, import_key.second).str();
  58. }
  59. // Marks an import as required on both the source and target file.
  60. //
  61. // The ID comparisons between the import and unit are okay because they both
  62. // come from the same file.
  63. static auto TrackImport(Map<ImportKey, UnitAndImports*>& api_map,
  64. Map<ImportKey, Parse::NodeId>* explicit_import_map,
  65. UnitAndImports& unit_info,
  66. Parse::Tree::PackagingNames import, bool fuzzing)
  67. -> void {
  68. const auto& packaging = unit_info.parse_tree().packaging_decl();
  69. PackageNameId file_package_id =
  70. packaging ? packaging->names.package_id : PackageNameId::None;
  71. const auto import_key = GetImportKey(unit_info, file_package_id, import);
  72. const auto& [import_package_name, import_library_name] = import_key;
  73. if (import_package_name == CppPackageName) {
  74. if (!explicit_import_map) {
  75. // Don't diagnose the implicit import in `impl package Cpp`, because we'll
  76. // have diagnosed the use of `Cpp` in the declaration.
  77. return;
  78. }
  79. if (!import.library_id.has_value() && !import.inline_body_id.has_value()) {
  80. CARBON_DIAGNOSTIC(CppInteropMissingLibrary, Error,
  81. "`Cpp` import without `library` or `inline`");
  82. unit_info.emitter.Emit(import.node_id, CppInteropMissingLibrary);
  83. return;
  84. }
  85. if (fuzzing) {
  86. // Clang is not crash-resilient.
  87. CARBON_DIAGNOSTIC(CppInteropFuzzing, Error,
  88. "`Cpp` import found during fuzzing");
  89. unit_info.emitter.Emit(import.node_id, CppInteropFuzzing);
  90. return;
  91. }
  92. unit_info.cpp_imports.push_back(import);
  93. return;
  94. } else if (import.inline_body_id.has_value()) {
  95. CARBON_DIAGNOSTIC(InlineImportNotCpp, Error,
  96. "`inline` import not in package `Cpp`");
  97. unit_info.emitter.Emit(import.node_id, InlineImportNotCpp);
  98. return;
  99. }
  100. // True if the import has `Main` as the package name, even if it comes from
  101. // the file's packaging (diagnostics may differentiate).
  102. bool is_explicit_main = import_package_name == MainPackageName;
  103. // Explicit imports need more validation than implicit ones. We try to do
  104. // these in an order of imports that should be removed, followed by imports
  105. // that might be valid with syntax fixes.
  106. if (explicit_import_map) {
  107. // Diagnose redundant imports.
  108. if (auto insert_result =
  109. explicit_import_map->Insert(import_key, import.node_id);
  110. !insert_result.is_inserted()) {
  111. CARBON_DIAGNOSTIC(RepeatedImport, Error,
  112. "library imported more than once");
  113. CARBON_DIAGNOSTIC(FirstImported, Note, "first import here");
  114. unit_info.emitter.Build(import.node_id, RepeatedImport)
  115. .Note(insert_result.value(), FirstImported)
  116. .Emit();
  117. return;
  118. }
  119. // True if the file's package is implicitly `Main` (by omitting an explicit
  120. // package name).
  121. bool is_file_implicit_main =
  122. !packaging || !packaging->names.package_id.has_value();
  123. // True if the import is using implicit "current package" syntax (by
  124. // omitting an explicit package name).
  125. bool is_import_implicit_current_package = !import.package_id.has_value();
  126. // True if the import is using `default` library syntax.
  127. bool is_import_default_library = !import.library_id.has_value();
  128. // True if the import and file point at the same package, even by
  129. // incorrectly specifying the current package name to `import`.
  130. bool is_same_package = is_import_implicit_current_package ||
  131. import.package_id == file_package_id;
  132. // True if the import points at the same library as the file's library.
  133. bool is_same_library =
  134. is_same_package &&
  135. (packaging ? import.library_id == packaging->names.library_id
  136. : is_import_default_library);
  137. // Diagnose explicit imports of the same library, whether from `api` or
  138. // `impl`.
  139. if (is_same_library) {
  140. CARBON_DIAGNOSTIC(ExplicitImportApi, Error,
  141. "explicit import of `api` from `impl` file is "
  142. "redundant with implicit import");
  143. CARBON_DIAGNOSTIC(ImportSelf, Error, "file cannot import itself");
  144. bool is_impl = !packaging || packaging->is_impl;
  145. unit_info.emitter.Emit(import.node_id,
  146. is_impl ? ExplicitImportApi : ImportSelf);
  147. return;
  148. }
  149. // Diagnose explicit imports of `Main//default`. There is no `api` for it.
  150. // This lets other diagnostics handle explicit `Main` package naming.
  151. if (is_file_implicit_main && is_import_implicit_current_package &&
  152. is_import_default_library) {
  153. CARBON_DIAGNOSTIC(ImportMainDefaultLibrary, Error,
  154. "cannot import `Main//default`");
  155. unit_info.emitter.Emit(import.node_id, ImportMainDefaultLibrary);
  156. return;
  157. }
  158. if (!is_import_implicit_current_package) {
  159. // Diagnose explicit imports of the same package that use the package
  160. // name.
  161. if (is_same_package || (is_file_implicit_main && is_explicit_main)) {
  162. CARBON_DIAGNOSTIC(
  163. ImportCurrentPackageByName, Error,
  164. "imports from the current package must omit the package name");
  165. unit_info.emitter.Emit(import.node_id, ImportCurrentPackageByName);
  166. return;
  167. }
  168. // Diagnose explicit imports from `Main`.
  169. if (is_explicit_main) {
  170. CARBON_DIAGNOSTIC(ImportMainPackage, Error,
  171. "cannot import `Main` from other packages");
  172. unit_info.emitter.Emit(import.node_id, ImportMainPackage);
  173. return;
  174. }
  175. }
  176. } else if (is_explicit_main) {
  177. // An implicit import with an explicit `Main` occurs when a `package` rule
  178. // has bad syntax, which will have been diagnosed when building the API map.
  179. // As a consequence, we return silently.
  180. return;
  181. }
  182. // Get the package imports, or create them if this is the first.
  183. auto create_imports = [&]() -> int32_t {
  184. int32_t index = unit_info.package_imports.size();
  185. unit_info.package_imports.push_back(
  186. PackageImports(import.package_id, import.node_id));
  187. return index;
  188. };
  189. auto insert_result =
  190. unit_info.package_imports_map.Insert(import.package_id, create_imports);
  191. PackageImports& package_imports =
  192. unit_info.package_imports[insert_result.value()];
  193. if (auto api_lookup = api_map.Lookup(import_key)) {
  194. // Add references between the file and imported api.
  195. UnitAndImports* api = api_lookup.value();
  196. package_imports.imports.push_back({import, api});
  197. ++unit_info.imports_remaining;
  198. api->incoming_imports.push_back(&unit_info);
  199. // If this is the implicit import, note we have it.
  200. if (!explicit_import_map) {
  201. CARBON_CHECK(!unit_info.api_for_impl);
  202. unit_info.api_for_impl = api;
  203. }
  204. } else {
  205. // The imported api is missing.
  206. package_imports.has_load_error = true;
  207. CARBON_DIAGNOSTIC(LibraryApiNotFound, Error,
  208. "corresponding API for '{0}' not found", std::string);
  209. CARBON_DIAGNOSTIC(ImportNotFound, Error, "imported API '{0}' not found",
  210. std::string);
  211. unit_info.emitter.Emit(
  212. import.node_id,
  213. explicit_import_map ? ImportNotFound : LibraryApiNotFound,
  214. RenderImportKey(import_key));
  215. }
  216. }
  217. // Builds a map of `api` files which might be imported. Also diagnoses issues
  218. // related to the packaging because the strings are loaded as part of getting
  219. // the ImportKey (which we then do for `impl` files too).
  220. static auto BuildApiMapAndDiagnosePackaging(
  221. llvm::MutableArrayRef<UnitAndImports> unit_infos)
  222. -> Map<ImportKey, UnitAndImports*> {
  223. Map<ImportKey, UnitAndImports*> api_map;
  224. for (auto& unit_info : unit_infos) {
  225. const auto& packaging = unit_info.parse_tree().packaging_decl();
  226. // An import key formed from the `package` or `library` declaration. Or, for
  227. // Main//default, a placeholder key.
  228. auto import_key = packaging ? GetImportKey(unit_info, PackageNameId::None,
  229. packaging->names)
  230. // Construct a boring key for Main//default.
  231. : ImportKey{"", ""};
  232. // Diagnose restricted package names before they become marked as possible
  233. // APIs.
  234. if (import_key.first == MainPackageName) {
  235. CARBON_DIAGNOSTIC(ExplicitMainPackage, Error,
  236. "`Main//default` must omit `package` declaration");
  237. CARBON_DIAGNOSTIC(
  238. ExplicitMainLibrary, Error,
  239. "use `library` declaration in `Main` package libraries");
  240. unit_info.emitter.Emit(packaging->names.node_id,
  241. import_key.second.empty() ? ExplicitMainPackage
  242. : ExplicitMainLibrary);
  243. continue;
  244. } else if (import_key.first == CppPackageName) {
  245. CARBON_DIAGNOSTIC(CppPackageDeclaration, Error,
  246. "`Cpp` cannot be used by a `package` declaration");
  247. unit_info.emitter.Emit(packaging->names.node_id, CppPackageDeclaration);
  248. continue;
  249. }
  250. bool is_impl = packaging && packaging->is_impl;
  251. // Add to the `api` map and diagnose duplicates. This occurs before the
  252. // file extension check because we might emit both diagnostics in situations
  253. // where the user forgets (or has syntax errors with) a package line
  254. // multiple times.
  255. if (!is_impl) {
  256. auto insert_result = api_map.Insert(import_key, &unit_info);
  257. if (!insert_result.is_inserted()) {
  258. llvm::StringRef prev_filename =
  259. insert_result.value()->source().filename();
  260. if (packaging) {
  261. CARBON_DIAGNOSTIC(DuplicateLibraryApi, Error,
  262. "library's API previously provided by `{0}`",
  263. std::string);
  264. unit_info.emitter.Emit(packaging->names.node_id, DuplicateLibraryApi,
  265. prev_filename.str());
  266. } else {
  267. CARBON_DIAGNOSTIC(DuplicateMainApi, Error,
  268. "`Main//default` previously provided by `{0}`",
  269. std::string);
  270. // Use `NodeId::None` because there's no node to associate with.
  271. unit_info.emitter.Emit(Parse::NodeId::None, DuplicateMainApi,
  272. prev_filename.str());
  273. }
  274. }
  275. }
  276. // Validate file extensions. Note imports rely the packaging declaration,
  277. // not the extension. If the input is not a regular file, for example
  278. // because it is stdin, no filename checking is performed.
  279. if (unit_info.source().is_regular_file()) {
  280. auto filename = unit_info.source().filename();
  281. static constexpr llvm::StringLiteral ApiExt = ".carbon";
  282. static constexpr llvm::StringLiteral ImplExt = ".impl.carbon";
  283. bool is_api_with_impl_ext = !is_impl && filename.ends_with(ImplExt);
  284. auto want_ext = is_impl ? ImplExt : ApiExt;
  285. if (is_api_with_impl_ext || !filename.ends_with(want_ext)) {
  286. CARBON_DIAGNOSTIC(
  287. IncorrectExtension, Error,
  288. "file extension of `{0:.impl|}.carbon` required for {0:`impl`|api}",
  289. Diagnostics::BoolAsSelect);
  290. auto diag = unit_info.emitter.Build(
  291. packaging ? packaging->names.node_id : Parse::NodeId::None,
  292. IncorrectExtension, is_impl);
  293. if (is_api_with_impl_ext) {
  294. CARBON_DIAGNOSTIC(
  295. IncorrectExtensionImplNote, Note,
  296. "file extension of `.impl.carbon` only allowed for `impl`");
  297. diag.Note(Parse::NodeId::None, IncorrectExtensionImplNote);
  298. }
  299. diag.Emit();
  300. }
  301. }
  302. }
  303. return api_map;
  304. }
  305. // Handles printing of formatted SemIR.
  306. static auto MaybeDumpFormattedSemIR(
  307. const SemIR::File& sem_ir, int total_ir_count,
  308. Parse::GetTreeAndSubtreesFn tree_and_subtrees_getter, bool include_in_dumps,
  309. const CheckParseTreesOptions& options) -> void {
  310. bool dump = options.dump_stream && include_in_dumps;
  311. if (!options.vlog_stream && !dump) {
  312. return;
  313. }
  314. const auto& tokens = sem_ir.parse_tree().tokens();
  315. if (options.dump_sem_ir_ranges ==
  316. CheckParseTreesOptions::DumpSemIRRanges::Only &&
  317. !tokens.has_dump_sem_ir_ranges() && !tokens.has_include_in_dumps()) {
  318. return;
  319. }
  320. bool use_dump_sem_ir_ranges =
  321. options.dump_sem_ir_ranges !=
  322. CheckParseTreesOptions::DumpSemIRRanges::Ignore &&
  323. tokens.has_dump_sem_ir_ranges();
  324. SemIR::Formatter formatter(&sem_ir, total_ir_count, tree_and_subtrees_getter,
  325. options.include_in_dumps, use_dump_sem_ir_ranges);
  326. formatter.Format();
  327. if (options.vlog_stream) {
  328. CARBON_VLOG_TO(options.vlog_stream, "*** SemIR::File ***\n");
  329. formatter.Write(*options.vlog_stream);
  330. }
  331. if (dump) {
  332. formatter.Write(*options.dump_stream);
  333. }
  334. }
  335. // Handles options for dumping SemIR, including verbose output.
  336. static auto MaybeDumpSemIR(
  337. llvm::ArrayRef<Unit> units,
  338. const Parse::GetTreeAndSubtreesStore& tree_and_subtrees_getters,
  339. const CheckParseTreesOptions& options) -> void {
  340. if (!options.vlog_stream && !options.dump_stream &&
  341. !options.raw_dump_stream) {
  342. return;
  343. }
  344. // Flush diagnostics before printing.
  345. for (const auto& unit : units) {
  346. unit.consumer->Flush();
  347. }
  348. for (const auto& unit : units) {
  349. bool include_in_dumps =
  350. options.include_in_dumps->Get(unit.sem_ir->check_ir_id());
  351. if (include_in_dumps && options.raw_dump_stream) {
  352. unit.sem_ir->Print(*options.raw_dump_stream,
  353. options.dump_raw_sem_ir_builtins);
  354. }
  355. MaybeDumpFormattedSemIR(
  356. *unit.sem_ir, units.size(),
  357. tree_and_subtrees_getters.Get(unit.sem_ir->check_ir_id()),
  358. include_in_dumps, options);
  359. }
  360. }
  361. // Handles options for dumping C++ AST.
  362. static auto MaybeDumpCppAST(llvm::ArrayRef<Unit> units,
  363. const CheckParseTreesOptions& options) -> void {
  364. if (!options.dump_cpp_ast_stream) {
  365. return;
  366. }
  367. for (const Unit& unit : units) {
  368. if (!unit.clang_ast_unit || !*unit.clang_ast_unit) {
  369. continue;
  370. }
  371. clang::ASTContext& ast_context = (*unit.clang_ast_unit)->getASTContext();
  372. ast_context.getTranslationUnitDecl()->dump(*options.dump_cpp_ast_stream);
  373. }
  374. }
  375. auto CheckParseTrees(
  376. llvm::MutableArrayRef<Unit> units,
  377. const Parse::GetTreeAndSubtreesStore& tree_and_subtrees_getters,
  378. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  379. const CheckParseTreesOptions& options,
  380. std::shared_ptr<clang::CompilerInvocation> clang_invocation) -> void {
  381. // UnitAndImports is big due to its SmallVectors, so we default to 0 on the
  382. // stack.
  383. llvm::SmallVector<UnitAndImports, 0> unit_infos(
  384. llvm::map_range(units, [&](Unit& unit) {
  385. return UnitAndImports(
  386. &unit, tree_and_subtrees_getters.Get(unit.sem_ir->check_ir_id()));
  387. }));
  388. Map<ImportKey, UnitAndImports*> api_map =
  389. BuildApiMapAndDiagnosePackaging(unit_infos);
  390. // Mark down imports for all files.
  391. llvm::SmallVector<UnitAndImports*> ready_to_check;
  392. ready_to_check.reserve(units.size());
  393. for (auto& unit_info : unit_infos) {
  394. const auto& packaging = unit_info.parse_tree().packaging_decl();
  395. if (packaging && packaging->is_impl) {
  396. // An `impl` has an implicit import of its `api`.
  397. auto implicit_names = packaging->names;
  398. implicit_names.package_id = PackageNameId::None;
  399. TrackImport(api_map, nullptr, unit_info, implicit_names, options.fuzzing);
  400. }
  401. Map<ImportKey, Parse::NodeId> explicit_import_map;
  402. // Add the prelude import. It's added to explicit_import_map so that it can
  403. // conflict with an explicit import of the prelude.
  404. if (options.prelude_import &&
  405. !(packaging && packaging->names.package_id == PackageNameId::Core)) {
  406. auto prelude_id =
  407. unit_info.unit->value_stores->string_literal_values().Add("prelude");
  408. TrackImport(api_map, &explicit_import_map, unit_info,
  409. {.node_id = Parse::NoneNodeId(),
  410. .package_id = PackageNameId::Core,
  411. .library_id = prelude_id},
  412. options.fuzzing);
  413. }
  414. for (const auto& import : unit_info.parse_tree().imports()) {
  415. TrackImport(api_map, &explicit_import_map, unit_info, import,
  416. options.fuzzing);
  417. }
  418. // If there were no imports, mark the file as ready to check for below.
  419. if (unit_info.imports_remaining == 0) {
  420. ready_to_check.push_back(&unit_info);
  421. }
  422. }
  423. // Check everything with no dependencies. Earlier entries with dependencies
  424. // will be checked as soon as all their dependencies have been checked.
  425. for (int check_index = 0;
  426. check_index < static_cast<int>(ready_to_check.size()); ++check_index) {
  427. auto* unit_info = ready_to_check[check_index];
  428. CheckUnit(unit_info, &tree_and_subtrees_getters, fs, clang_invocation,
  429. options.vlog_stream)
  430. .Run();
  431. for (auto* incoming_import : unit_info->incoming_imports) {
  432. --incoming_import->imports_remaining;
  433. if (incoming_import->imports_remaining == 0) {
  434. ready_to_check.push_back(incoming_import);
  435. }
  436. }
  437. }
  438. // If there are still units with remaining imports, it means there's a
  439. // dependency loop.
  440. if (ready_to_check.size() < unit_infos.size()) {
  441. // Go through units and mask out unevaluated imports. This breaks everything
  442. // associated with a loop equivalently, whether it's part of it or depending
  443. // on a part of it.
  444. // TODO: Better identify cycles, maybe try to untangle them.
  445. for (auto& unit_info : unit_infos) {
  446. if (unit_info.imports_remaining > 0) {
  447. for (auto& package_imports : unit_info.package_imports) {
  448. for (auto* import_it = package_imports.imports.begin();
  449. import_it != package_imports.imports.end();) {
  450. if (import_it->unit_info->is_checked) {
  451. // The import is checked, so continue.
  452. ++import_it;
  453. } else {
  454. // The import hasn't been checked, indicating a cycle.
  455. CARBON_DIAGNOSTIC(ImportCycleDetected, Error,
  456. "import cannot be used due to a cycle; cycle "
  457. "must be fixed to import");
  458. unit_info.emitter.Emit(import_it->names.node_id,
  459. ImportCycleDetected);
  460. // Make this look the same as an import which wasn't found.
  461. package_imports.has_load_error = true;
  462. if (unit_info.api_for_impl == import_it->unit_info) {
  463. unit_info.api_for_impl = nullptr;
  464. }
  465. import_it = package_imports.imports.erase(import_it);
  466. }
  467. }
  468. }
  469. }
  470. }
  471. // Check the remaining file contents, which are probably broken due to
  472. // incomplete imports.
  473. for (auto& unit_info : unit_infos) {
  474. if (unit_info.imports_remaining > 0) {
  475. CheckUnit(&unit_info, &tree_and_subtrees_getters, fs, clang_invocation,
  476. options.vlog_stream)
  477. .Run();
  478. }
  479. }
  480. }
  481. MaybeDumpSemIR(units, tree_and_subtrees_getters, options);
  482. MaybeDumpCppAST(units, options);
  483. }
  484. } // namespace Carbon::Check