generate_ast.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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/cpp/generate_ast.h"
  5. #include <memory>
  6. #include <string>
  7. #include "clang/AST/ASTContext.h"
  8. #include "clang/Basic/FileManager.h"
  9. #include "clang/CodeGen/ModuleBuilder.h"
  10. #include "clang/Frontend/CompilerInstance.h"
  11. #include "clang/Frontend/CompilerInvocation.h"
  12. #include "clang/Frontend/FrontendAction.h"
  13. #include "clang/Frontend/TextDiagnostic.h"
  14. #include "clang/Lex/PreprocessorOptions.h"
  15. #include "clang/Parse/Parser.h"
  16. #include "clang/Sema/ExternalSemaSource.h"
  17. #include "clang/Sema/MultiplexExternalSemaSource.h"
  18. #include "clang/Sema/Sema.h"
  19. #include "common/check.h"
  20. #include "common/map.h"
  21. #include "common/raw_string_ostream.h"
  22. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include "toolchain/check/context.h"
  26. #include "toolchain/check/cpp/import.h"
  27. #include "toolchain/check/name_lookup.h"
  28. #include "toolchain/diagnostics/diagnostic.h"
  29. #include "toolchain/diagnostics/emitter.h"
  30. #include "toolchain/diagnostics/format_providers.h"
  31. #include "toolchain/parse/node_ids.h"
  32. #include "toolchain/sem_ir/cpp_file.h"
  33. namespace Carbon::Check {
  34. // Add a line marker directive pointing at the location of the `import Cpp`
  35. // declaration in the Carbon source file. This will cause Clang's diagnostics
  36. // machinery to track and report the location in Carbon code where the import
  37. // was written.
  38. static auto GenerateLineMarker(Context& context, llvm::raw_ostream& out,
  39. int line) {
  40. out << "# " << line << " \""
  41. << FormatEscaped(context.tokens().source().filename()) << "\"\n";
  42. }
  43. // Generates C++ file contents to #include all requested imports.
  44. static auto GenerateCppIncludesHeaderCode(
  45. Context& context, llvm::ArrayRef<Parse::Tree::PackagingNames> imports)
  46. -> std::string {
  47. std::string code;
  48. llvm::raw_string_ostream code_stream(code);
  49. for (const Parse::Tree::PackagingNames& import : imports) {
  50. if (import.inline_body_id.has_value()) {
  51. // Expand `import Cpp inline "code";` directly into the specified code.
  52. auto code_token = context.parse_tree().node_token(import.inline_body_id);
  53. // Compute the line number on which the C++ code starts. Usually the code
  54. // is specified as a block string literal and starts on the line after the
  55. // start of the string token.
  56. // TODO: Determine if this is a block string literal without calling
  57. // `GetTokenText`, which re-lexes the string.
  58. int line = context.tokens().GetLineNumber(code_token);
  59. if (context.tokens().GetTokenText(code_token).contains('\n')) {
  60. ++line;
  61. }
  62. GenerateLineMarker(context, code_stream, line);
  63. code_stream << context.string_literal_values().Get(
  64. context.tokens().GetStringLiteralValue(code_token))
  65. << "\n";
  66. // TODO: Inject a clang pragma here to produce an error if there are
  67. // unclosed scopes at the end of this inline C++ fragment.
  68. } else if (import.library_id.has_value()) {
  69. // Translate `import Cpp library "foo.h";` into `#include "foo.h"`.
  70. GenerateLineMarker(context, code_stream,
  71. context.tokens().GetLineNumber(
  72. context.parse_tree().node_token(import.node_id)));
  73. auto name = context.string_literal_values().Get(import.library_id);
  74. if (name.starts_with('<') && name.ends_with('>')) {
  75. code_stream << "#include <"
  76. << FormatEscaped(name.drop_front().drop_back()) << ">\n";
  77. } else {
  78. code_stream << "#include \"" << FormatEscaped(name) << "\"\n";
  79. }
  80. }
  81. }
  82. return code;
  83. }
  84. // Adds the given source location and an `ImportIRInst` referring to it in
  85. // `ImportIRId::Cpp`.
  86. static auto AddImportIRInst(SemIR::File& file,
  87. clang::SourceLocation clang_source_loc)
  88. -> SemIR::ImportIRInstId {
  89. SemIR::ClangSourceLocId clang_source_loc_id =
  90. file.clang_source_locs().Add(clang_source_loc);
  91. return file.import_ir_insts().Add(SemIR::ImportIRInst(clang_source_loc_id));
  92. }
  93. namespace {
  94. // Used to convert Clang diagnostics to Carbon diagnostics.
  95. //
  96. // Handling of Clang notes is a little subtle: as far as Clang is concerned,
  97. // notes are separate diagnostics, not connected to the error or warning that
  98. // precedes them. But in Carbon's diagnostics system, notes are part of the
  99. // enclosing diagnostic. To handle this, we buffer Clang diagnostics until we
  100. // reach a point where we know we're not in the middle of a diagnostic, and then
  101. // emit a diagnostic along with all of its notes. This is triggered when adding
  102. // or removing a Carbon context note, which could otherwise get attached to the
  103. // wrong C++ diagnostics, and at the end of the Carbon program.
  104. class CarbonClangDiagnosticConsumer : public clang::DiagnosticConsumer {
  105. public:
  106. // Creates an instance with the location that triggers calling Clang. The
  107. // `context` is not stored here, and the diagnostics consumer is expected to
  108. // outlive it.
  109. explicit CarbonClangDiagnosticConsumer(
  110. Context& context, std::shared_ptr<clang::CompilerInvocation> invocation)
  111. : sem_ir_(&context.sem_ir()),
  112. emitter_(&context.emitter()),
  113. invocation_(std::move(invocation)) {
  114. emitter_->AddFlushFn([this] { EmitDiagnostics(); });
  115. }
  116. ~CarbonClangDiagnosticConsumer() override {
  117. // Do not inspect `emitter_` here; it's typically destroyed before the
  118. // consumer is.
  119. // TODO: If Clang produces diagnostics after check finishes, they'll get
  120. // added to the list of pending diagnostics and never emitted.
  121. CARBON_CHECK(diagnostic_infos_.empty(),
  122. "Missing flush before destroying diagnostic consumer");
  123. }
  124. // Generates a Carbon warning for each Clang warning and a Carbon error for
  125. // each Clang error or fatal.
  126. auto HandleDiagnostic(clang::DiagnosticsEngine::Level diag_level,
  127. const clang::Diagnostic& info) -> void override {
  128. DiagnosticConsumer::HandleDiagnostic(diag_level, info);
  129. SemIR::ImportIRInstId clang_import_ir_inst_id =
  130. AddImportIRInst(*sem_ir_, info.getLocation());
  131. llvm::SmallString<256> message;
  132. info.FormatDiagnostic(message);
  133. // Render a code snippet including any highlighted ranges and fixit hints.
  134. // TODO: Also include the #include stack and macro expansion stack in the
  135. // diagnostic output in some way.
  136. RawStringOstream snippet_stream;
  137. if (!info.hasSourceManager()) {
  138. // If we don't have a source manager, this is an error from early in the
  139. // frontend. Don't produce a snippet.
  140. CARBON_CHECK(info.getLocation().isInvalid());
  141. } else {
  142. CodeContextRenderer(snippet_stream, invocation_->getLangOpts(),
  143. invocation_->getDiagnosticOpts())
  144. .emitDiagnostic(
  145. clang::FullSourceLoc(info.getLocation(), info.getSourceManager()),
  146. diag_level, message, info.getRanges(), info.getFixItHints());
  147. }
  148. diagnostic_infos_.push_back({.level = diag_level,
  149. .import_ir_inst_id = clang_import_ir_inst_id,
  150. .message = message.str().str(),
  151. .snippet = snippet_stream.TakeStr()});
  152. }
  153. // Returns the diagnostic to use for a given Clang diagnostic level.
  154. static auto GetDiagnostic(clang::DiagnosticsEngine::Level level)
  155. -> const Diagnostics::DiagnosticBase<std::string>& {
  156. switch (level) {
  157. case clang::DiagnosticsEngine::Ignored: {
  158. CARBON_FATAL("Emitting an ignored diagnostic");
  159. break;
  160. }
  161. case clang::DiagnosticsEngine::Note: {
  162. CARBON_DIAGNOSTIC(CppInteropParseNote, Note, "{0}", std::string);
  163. return CppInteropParseNote;
  164. }
  165. case clang::DiagnosticsEngine::Remark:
  166. case clang::DiagnosticsEngine::Warning: {
  167. // TODO: Add a distinct Remark level to Carbon diagnostics, and stop
  168. // mapping remarks to warnings.
  169. CARBON_DIAGNOSTIC(CppInteropParseWarning, Warning, "{0}", std::string);
  170. return CppInteropParseWarning;
  171. }
  172. case clang::DiagnosticsEngine::Error:
  173. case clang::DiagnosticsEngine::Fatal: {
  174. CARBON_DIAGNOSTIC(CppInteropParseError, Error, "{0}", std::string);
  175. return CppInteropParseError;
  176. }
  177. }
  178. }
  179. // Outputs Carbon diagnostics based on the collected Clang diagnostics. Must
  180. // be called after the AST is set in the context.
  181. auto EmitDiagnostics() -> void {
  182. CARBON_CHECK(
  183. sem_ir_->cpp_file(),
  184. "Attempted to emit C++ diagnostics before the C++ file is set");
  185. for (size_t i = 0; i != diagnostic_infos_.size(); ++i) {
  186. const ClangDiagnosticInfo& info = diagnostic_infos_[i];
  187. auto builder = emitter_->Build(SemIR::LocId(info.import_ir_inst_id),
  188. GetDiagnostic(info.level), info.message);
  189. builder.OverrideSnippet(info.snippet);
  190. for (; i + 1 < diagnostic_infos_.size() &&
  191. diagnostic_infos_[i + 1].level == clang::DiagnosticsEngine::Note;
  192. ++i) {
  193. const ClangDiagnosticInfo& note_info = diagnostic_infos_[i + 1];
  194. builder
  195. .Note(SemIR::LocId(note_info.import_ir_inst_id),
  196. GetDiagnostic(note_info.level), note_info.message)
  197. .OverrideSnippet(note_info.snippet);
  198. }
  199. // TODO: This will apply all current Carbon annotation functions. We
  200. // should instead track how Clang's context notes and Carbon's annotation
  201. // functions are interleaved, and interleave the notes in the same order.
  202. builder.Emit();
  203. }
  204. diagnostic_infos_.clear();
  205. }
  206. private:
  207. // A diagnostics renderer based on clang's TextDiagnostic that captures just
  208. // the code context (the snippet).
  209. class CodeContextRenderer : public clang::TextDiagnostic {
  210. protected:
  211. using TextDiagnostic::TextDiagnostic;
  212. void emitDiagnosticMessage(
  213. clang::FullSourceLoc /*loc*/, clang::PresumedLoc /*ploc*/,
  214. clang::DiagnosticsEngine::Level /*level*/, llvm::StringRef /*message*/,
  215. llvm::ArrayRef<clang::CharSourceRange> /*ranges*/,
  216. clang::DiagOrStoredDiag /*info*/) override {}
  217. void emitDiagnosticLoc(
  218. clang::FullSourceLoc /*loc*/, clang::PresumedLoc /*ploc*/,
  219. clang::DiagnosticsEngine::Level /*level*/,
  220. llvm::ArrayRef<clang::CharSourceRange> /*ranges*/) override {}
  221. // emitCodeContext is inherited from clang::TextDiagnostic.
  222. void emitIncludeLocation(clang::FullSourceLoc /*loc*/,
  223. clang::PresumedLoc /*ploc*/) override {}
  224. void emitImportLocation(clang::FullSourceLoc /*loc*/,
  225. clang::PresumedLoc /*ploc*/,
  226. llvm::StringRef /*module_name*/) override {}
  227. void emitBuildingModuleLocation(clang::FullSourceLoc /*loc*/,
  228. clang::PresumedLoc /*ploc*/,
  229. llvm::StringRef /*module_name*/) override {}
  230. // beginDiagnostic and endDiagnostic are inherited from
  231. // clang::TextDiagnostic in case it wants to do any setup / teardown work.
  232. };
  233. // Information on a Clang diagnostic that can be converted to a Carbon
  234. // diagnostic.
  235. struct ClangDiagnosticInfo {
  236. // The Clang diagnostic level.
  237. clang::DiagnosticsEngine::Level level;
  238. // The ID of the ImportIR instruction referring to the Clang source
  239. // location.
  240. SemIR::ImportIRInstId import_ir_inst_id;
  241. // The Clang diagnostic textual message.
  242. std::string message;
  243. // The code snippet produced by clang.
  244. std::string snippet;
  245. };
  246. // The Carbon file that this C++ compilation is attached to.
  247. SemIR::File* sem_ir_;
  248. // The diagnostic emitter that we're emitting diagnostics into.
  249. DiagnosticEmitterBase* emitter_;
  250. // The compiler invocation that is producing the diagnostics.
  251. std::shared_ptr<clang::CompilerInvocation> invocation_;
  252. // Collects the information for all Clang diagnostics to be converted to
  253. // Carbon diagnostics after the context has been initialized with the Clang
  254. // AST.
  255. llvm::SmallVector<ClangDiagnosticInfo> diagnostic_infos_;
  256. };
  257. // A wrapper around a clang::CompilerInvocation that allows us to make a shallow
  258. // copy of most of the invocation and only make a deep copy of the parts that we
  259. // want to change.
  260. //
  261. // clang::CowCompilerInvocation almost allows this, but doesn't derive from
  262. // CompilerInvocation or support shallow copies from a CompilerInvocation, so is
  263. // not useful to us as we can't build an ASTUnit from it.
  264. class ShallowCopyCompilerInvocation : public clang::CompilerInvocation {
  265. public:
  266. explicit ShallowCopyCompilerInvocation(
  267. const clang::CompilerInvocation& invocation) {
  268. shallow_copy_assign(invocation);
  269. // Make a deep copy of options that we modify.
  270. FrontendOpts = std::make_shared<clang::FrontendOptions>(*FrontendOpts);
  271. PPOpts = std::make_shared<clang::PreprocessorOptions>(*PPOpts);
  272. }
  273. };
  274. // Provides clang AST nodes representing Carbon SemIR entities.
  275. class CarbonExternalASTSource : public clang::ExternalASTSource {
  276. public:
  277. explicit CarbonExternalASTSource(Context* context,
  278. clang::ASTContext* ast_context)
  279. : context_(context), ast_context_(ast_context) {}
  280. // Look up decls for `decl_name` inside `decl_context`, adding the decls to
  281. // `decl_context`. Returns true if any decls were added.
  282. auto FindExternalVisibleDeclsByName(
  283. const clang::DeclContext* decl_context, clang::DeclarationName decl_name,
  284. const clang::DeclContext* original_decl_context) -> bool override;
  285. // See clang::ExternalASTSource.
  286. auto StartTranslationUnit(clang::ASTConsumer* consumer) -> void override;
  287. private:
  288. // Map a Carbon entity to a Clang NamedDecl. Returns null if the entity cannot
  289. // currently be represented in C++.
  290. auto MapInstIdToClangDecl(clang::DeclContext& decl_context,
  291. LookupResult lookup) -> clang::NamedDecl*;
  292. Check::Context* context_;
  293. clang::ASTContext* ast_context_;
  294. // The association between clang DeclContexts and the corresponding
  295. // SemIR::Namespaces in Carbon.
  296. // TODO: reuse the SemIR::File::ClangDeclStore to avoid duplicates, and to
  297. // enable roundtripping through forward and reverse interop (once we have
  298. // syntax/support for that).
  299. Map<clang::DeclContext*, SemIR::InstId> scope_map_;
  300. // Has the "Carbon" C++ namespace been created yet
  301. // (this could be replaced with `!scope_map_.empty()` if Carbon::Map supported
  302. // `empty()`)
  303. bool root_scope_initialized_ = false;
  304. };
  305. void CarbonExternalASTSource::StartTranslationUnit(
  306. clang::ASTConsumer* /*Consumer*/) {
  307. auto& translation_unit = *ast_context_->getTranslationUnitDecl();
  308. // Mark the translation unit as having external storage so we get a query for
  309. // the `Carbon` namespace in the top level/translation unit scope.
  310. translation_unit.setHasExternalVisibleStorage();
  311. }
  312. auto CarbonExternalASTSource::MapInstIdToClangDecl(
  313. clang::DeclContext& decl_context, LookupResult lookup)
  314. -> clang::NamedDecl* {
  315. auto target_inst_id = lookup.scope_result.target_inst_id();
  316. if (auto target_inst =
  317. context_->insts().TryGetAs<SemIR::Namespace>(target_inst_id)) {
  318. auto& name_scope = context_->name_scopes().Get(target_inst->name_scope_id);
  319. auto* identifier_info =
  320. GetClangIdentifierInfo(*context_, name_scope.name_id());
  321. // TODO: Don't immediately use the decl_context - build any intermediate
  322. // namespaces iteratively.
  323. // Eventually add a mapping and use that/populate it/keep it up to date.
  324. // decl_context could be prepopulated in that mapping and not passed
  325. // explicitly to MapInstIdToClangDecl.
  326. auto* namespace_decl = clang::NamespaceDecl::Create(
  327. *ast_context_, &decl_context, false, clang::SourceLocation(),
  328. clang::SourceLocation(), identifier_info, nullptr, false);
  329. auto result =
  330. scope_map_.Insert(namespace_decl->getPrimaryContext(), target_inst_id);
  331. CARBON_CHECK(result.is_inserted(), "Inserting over an existing entry.");
  332. namespace_decl->setHasExternalVisibleStorage();
  333. return namespace_decl;
  334. }
  335. return nullptr;
  336. }
  337. auto CarbonExternalASTSource::FindExternalVisibleDeclsByName(
  338. const clang::DeclContext* decl_context, clang::DeclarationName decl_name,
  339. const clang::DeclContext* /*OriginalDC*/) -> bool {
  340. if (decl_context->getDeclKind() == clang::Decl::Kind::TranslationUnit) {
  341. // If the context doesn't already have a mapping between C++ and Carbon,
  342. // check if this is the root mapping (for the "Carbon" namespace in the
  343. // translation unit scope) and if so, create that mapping.
  344. if (root_scope_initialized_) {
  345. return false;
  346. }
  347. static const llvm::StringLiteral carbon_namespace_name = "Carbon";
  348. if (auto* identifier = decl_name.getAsIdentifierInfo();
  349. !identifier || !identifier->isStr(carbon_namespace_name)) {
  350. return false;
  351. }
  352. // Build the top level 'Carbon' namespace
  353. auto& ast_context = decl_context->getParentASTContext();
  354. auto& mutable_tu_decl_context = *ast_context.getTranslationUnitDecl();
  355. auto* carbon_cpp_namespace = clang::NamespaceDecl::Create(
  356. ast_context, &mutable_tu_decl_context, false, clang::SourceLocation(),
  357. clang::SourceLocation(), &ast_context.Idents.get(carbon_namespace_name),
  358. nullptr, false);
  359. carbon_cpp_namespace->setHasExternalVisibleStorage();
  360. auto result = scope_map_.Insert(carbon_cpp_namespace->getPrimaryContext(),
  361. SemIR::Namespace::PackageInstId);
  362. CARBON_CHECK(result.is_inserted(), "Inserting over an existing entry.");
  363. SetExternalVisibleDeclsForName(decl_context, decl_name,
  364. {carbon_cpp_namespace});
  365. root_scope_initialized_ = true;
  366. return true;
  367. }
  368. auto decl_context_inst_id =
  369. scope_map_.Lookup(decl_context->getPrimaryContext());
  370. CARBON_CHECK(
  371. decl_context_inst_id,
  372. "The DeclContext should already be associated with a Carbon InstId.");
  373. llvm::SmallVector<Check::LookupScope> lookup_scopes;
  374. // LocId::None seems fine here because we shouldn't produce any diagnostics
  375. // here - completeness should've been checked by clang before this point.
  376. if (!AppendLookupScopesForConstant(
  377. *context_, SemIR::LocId::None,
  378. context_->constant_values().Get(decl_context_inst_id.value()),
  379. SemIR::ConstantId::None, &lookup_scopes)) {
  380. return false;
  381. }
  382. auto* identifier = decl_name.getAsIdentifierInfo();
  383. if (!identifier) {
  384. // Only supporting identifiers for now.
  385. return false;
  386. }
  387. auto name_id = AddIdentifierName(*context_, identifier->getName());
  388. // `required=false` so Carbon doesn't diagnose a failure, let Clang diagnose
  389. // it or even SFINAE.
  390. LookupResult result =
  391. LookupQualifiedName(*context_, SemIR::LocId::None, name_id, lookup_scopes,
  392. /*required=*/false);
  393. if (!result.scope_result.is_found()) {
  394. return false;
  395. }
  396. // Map the found Carbon entity to a Clang NamedDecl.
  397. // Use the key to reach the owned, mutable copy of decl_context.
  398. auto* clang_decl = MapInstIdToClangDecl(*decl_context_inst_id.key(), result);
  399. if (!clang_decl) {
  400. return false;
  401. }
  402. SetExternalVisibleDeclsForName(decl_context, decl_name, {clang_decl});
  403. return true;
  404. }
  405. // An action and a set of registered Clang callbacks used to generate an AST
  406. // from a set of Cpp imports.
  407. class GenerateASTAction : public clang::ASTFrontendAction {
  408. public:
  409. explicit GenerateASTAction(Context& context) : context_(&context) {}
  410. protected:
  411. auto CreateASTConsumer(clang::CompilerInstance& clang_instance,
  412. llvm::StringRef /*file*/)
  413. -> std::unique_ptr<clang::ASTConsumer> override {
  414. auto& cpp_file = *context_->sem_ir().cpp_file();
  415. if (!cpp_file.llvm_context()) {
  416. return std::make_unique<clang::ASTConsumer>();
  417. }
  418. auto code_generator =
  419. std::unique_ptr<clang::CodeGenerator>(clang::CreateLLVMCodeGen(
  420. cpp_file.diagnostics(), context_->sem_ir().filename(),
  421. clang_instance.getVirtualFileSystemPtr(),
  422. clang_instance.getHeaderSearchOpts(),
  423. clang_instance.getPreprocessorOpts(),
  424. clang_instance.getCodeGenOpts(), *cpp_file.llvm_context()));
  425. cpp_file.SetCodeGenerator(code_generator.get());
  426. return code_generator;
  427. }
  428. auto BeginSourceFileAction(clang::CompilerInstance& /*clang_instance*/)
  429. -> bool override {
  430. // TODO: `clang.getPreprocessor().enableIncrementalProcessing();` to avoid
  431. // the TU scope getting torn down before we're done parsing macros.
  432. return true;
  433. }
  434. // Parse the imports and inline C++ fragments. This is notionally very similar
  435. // to `clang::ParseAST`, which `ASTFrontendAction::ExecuteAction` calls, but
  436. // this version doesn't parse C++20 modules and stops just before reaching the
  437. // end of the translation unit.
  438. auto ExecuteAction() -> void override {
  439. clang::CompilerInstance& clang_instance = getCompilerInstance();
  440. clang_instance.createSema(getTranslationUnitKind(),
  441. /*CompletionConsumer=*/nullptr);
  442. auto parser_ptr = std::make_unique<clang::Parser>(
  443. clang_instance.getPreprocessor(), clang_instance.getSema(),
  444. /*SkipFunctionBodies=*/false);
  445. auto& parser = *parser_ptr;
  446. clang_instance.getPreprocessor().EnterMainSourceFile();
  447. if (auto* source = clang_instance.getASTContext().getExternalSource()) {
  448. source->StartTranslationUnit(&clang_instance.getASTConsumer());
  449. }
  450. parser.Initialize();
  451. clang_instance.getSema().ActOnStartOfTranslationUnit();
  452. context_->set_cpp_context(
  453. std::make_unique<CppContext>(clang_instance, std::move(parser_ptr)));
  454. // Don't allow C++20 module declarations in inline Cpp code fragments.
  455. auto module_import_state = clang::Sema::ModuleImportState::NotACXX20Module;
  456. // Parse top-level declarations until we see EOF. Do not parse EOF, as that
  457. // will cause the parser to end the translation unit prematurely.
  458. while (parser.getCurToken().isNot(clang::tok::eof)) {
  459. clang::Parser::DeclGroupPtrTy decl_group;
  460. bool eof = parser.ParseTopLevelDecl(decl_group, module_import_state);
  461. CARBON_CHECK(!eof);
  462. if (decl_group && !clang_instance.getASTConsumer().HandleTopLevelDecl(
  463. decl_group.get())) {
  464. break;
  465. }
  466. }
  467. }
  468. private:
  469. Context* context_;
  470. };
  471. } // namespace
  472. auto GenerateAst(Context& context,
  473. llvm::ArrayRef<Parse::Tree::PackagingNames> imports,
  474. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  475. llvm::LLVMContext* llvm_context,
  476. std::shared_ptr<clang::CompilerInvocation> base_invocation)
  477. -> bool {
  478. CARBON_CHECK(!context.cpp_context());
  479. CARBON_CHECK(!context.sem_ir().cpp_file());
  480. auto invocation =
  481. std::make_shared<ShallowCopyCompilerInvocation>(*base_invocation);
  482. // Ask Clang to not leak memory.
  483. invocation->getFrontendOpts().DisableFree = false;
  484. // Build a diagnostics engine.
  485. llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diags(
  486. clang::CompilerInstance::createDiagnostics(
  487. *fs, invocation->getDiagnosticOpts(),
  488. new CarbonClangDiagnosticConsumer(context, invocation),
  489. /*ShouldOwnClient=*/true));
  490. // Extract the input from the frontend invocation and make sure it makes
  491. // sense.
  492. const auto& inputs = invocation->getFrontendOpts().Inputs;
  493. CARBON_CHECK(inputs.size() == 1 &&
  494. inputs[0].getKind().getLanguage() == clang::Language::CXX &&
  495. inputs[0].getKind().getFormat() == clang::InputKind::Source);
  496. llvm::StringRef file_name = inputs[0].getFile();
  497. // Remap the imports file name to the corresponding `#include`s.
  498. // TODO: Modify the frontend options to specify this memory buffer as input
  499. // instead of remapping the file.
  500. std::string includes = GenerateCppIncludesHeaderCode(context, imports);
  501. auto includes_buffer =
  502. llvm::MemoryBuffer::getMemBufferCopy(includes, file_name);
  503. invocation->getPreprocessorOpts().addRemappedFile(file_name,
  504. includes_buffer.release());
  505. auto clang_instance_ptr =
  506. std::make_unique<clang::CompilerInstance>(invocation);
  507. auto& clang_instance = *clang_instance_ptr;
  508. context.sem_ir().set_cpp_file(std::make_unique<SemIR::CppFile>(
  509. std::move(clang_instance_ptr), llvm_context));
  510. clang_instance.setDiagnostics(diags);
  511. clang_instance.setVirtualFileSystem(fs);
  512. clang_instance.createFileManager();
  513. clang_instance.createSourceManager();
  514. if (!clang_instance.createTarget()) {
  515. return false;
  516. }
  517. GenerateASTAction action(context);
  518. if (!action.BeginSourceFile(clang_instance, inputs[0])) {
  519. return false;
  520. }
  521. auto& ast = clang_instance.getASTContext();
  522. // TODO: Clang's modules support is implemented as an ExternalASTSource
  523. // (ASTReader) and there's no multiplexing support for ExternalASTSources at
  524. // the moment - so registering CarbonExternalASTSource breaks Clang modules
  525. // support. Implement multiplexing support (possibly in Clang) to restore
  526. // modules functionality.
  527. ast.setExternalSource(
  528. llvm::makeIntrusiveRefCnt<CarbonExternalASTSource>(&context, &ast));
  529. if (llvm::Error error = action.Execute()) {
  530. // `Execute` currently never fails, but its contract allows it to.
  531. context.TODO(SemIR::LocId::None, "failed to execute clang action: " +
  532. llvm::toString(std::move(error)));
  533. return false;
  534. }
  535. // Flush any diagnostics. We know we're not part-way through emitting a
  536. // diagnostic now.
  537. context.emitter().Flush();
  538. return true;
  539. }
  540. auto FinishAst(Context& context) -> void {
  541. if (!context.cpp_context()) {
  542. return;
  543. }
  544. context.cpp_context()->sema().ActOnEndOfTranslationUnit();
  545. // We don't call FrontendAction::EndSourceFile, because that destroys the AST.
  546. context.set_cpp_context(nullptr);
  547. context.emitter().Flush();
  548. }
  549. } // namespace Carbon::Check