FrontendAction.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. //===--- FrontendAction.cpp -----------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/Frontend/FrontendAction.h"
  10. #include "clang/AST/ASTConsumer.h"
  11. #include "clang/AST/ASTContext.h"
  12. #include "clang/AST/DeclGroup.h"
  13. #include "clang/Frontend/ASTUnit.h"
  14. #include "clang/Frontend/CompilerInstance.h"
  15. #include "clang/Frontend/FrontendDiagnostic.h"
  16. #include "clang/Frontend/FrontendPluginRegistry.h"
  17. #include "clang/Frontend/LayoutOverrideSource.h"
  18. #include "clang/Frontend/MultiplexConsumer.h"
  19. #include "clang/Frontend/Utils.h"
  20. #include "clang/Lex/HeaderSearch.h"
  21. #include "clang/Lex/Preprocessor.h"
  22. #include "clang/Lex/PreprocessorOptions.h"
  23. #include "clang/Parse/ParseAST.h"
  24. #include "clang/Serialization/ASTDeserializationListener.h"
  25. #include "clang/Serialization/ASTReader.h"
  26. #include "clang/Serialization/GlobalModuleIndex.h"
  27. #include "llvm/Support/ErrorHandling.h"
  28. #include "llvm/Support/FileSystem.h"
  29. #include "llvm/Support/Path.h"
  30. #include "llvm/Support/Timer.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. #include <system_error>
  33. using namespace clang;
  34. LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
  35. namespace {
  36. class DelegatingDeserializationListener : public ASTDeserializationListener {
  37. ASTDeserializationListener *Previous;
  38. bool DeletePrevious;
  39. public:
  40. explicit DelegatingDeserializationListener(
  41. ASTDeserializationListener *Previous, bool DeletePrevious)
  42. : Previous(Previous), DeletePrevious(DeletePrevious) {}
  43. ~DelegatingDeserializationListener() override {
  44. if (DeletePrevious)
  45. delete Previous;
  46. }
  47. void ReaderInitialized(ASTReader *Reader) override {
  48. if (Previous)
  49. Previous->ReaderInitialized(Reader);
  50. }
  51. void IdentifierRead(serialization::IdentID ID,
  52. IdentifierInfo *II) override {
  53. if (Previous)
  54. Previous->IdentifierRead(ID, II);
  55. }
  56. void TypeRead(serialization::TypeIdx Idx, QualType T) override {
  57. if (Previous)
  58. Previous->TypeRead(Idx, T);
  59. }
  60. void DeclRead(serialization::DeclID ID, const Decl *D) override {
  61. if (Previous)
  62. Previous->DeclRead(ID, D);
  63. }
  64. void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
  65. if (Previous)
  66. Previous->SelectorRead(ID, Sel);
  67. }
  68. void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
  69. MacroDefinitionRecord *MD) override {
  70. if (Previous)
  71. Previous->MacroDefinitionRead(PPID, MD);
  72. }
  73. };
  74. /// \brief Dumps deserialized declarations.
  75. class DeserializedDeclsDumper : public DelegatingDeserializationListener {
  76. public:
  77. explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
  78. bool DeletePrevious)
  79. : DelegatingDeserializationListener(Previous, DeletePrevious) {}
  80. void DeclRead(serialization::DeclID ID, const Decl *D) override {
  81. llvm::outs() << "PCH DECL: " << D->getDeclKindName();
  82. if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
  83. llvm::outs() << " - " << *ND;
  84. llvm::outs() << "\n";
  85. DelegatingDeserializationListener::DeclRead(ID, D);
  86. }
  87. };
  88. /// \brief Checks deserialized declarations and emits error if a name
  89. /// matches one given in command-line using -error-on-deserialized-decl.
  90. class DeserializedDeclsChecker : public DelegatingDeserializationListener {
  91. ASTContext &Ctx;
  92. std::set<std::string> NamesToCheck;
  93. public:
  94. DeserializedDeclsChecker(ASTContext &Ctx,
  95. const std::set<std::string> &NamesToCheck,
  96. ASTDeserializationListener *Previous,
  97. bool DeletePrevious)
  98. : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
  99. NamesToCheck(NamesToCheck) {}
  100. void DeclRead(serialization::DeclID ID, const Decl *D) override {
  101. if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
  102. if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
  103. unsigned DiagID
  104. = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
  105. "%0 was deserialized");
  106. Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
  107. << ND->getNameAsString();
  108. }
  109. DelegatingDeserializationListener::DeclRead(ID, D);
  110. }
  111. };
  112. } // end anonymous namespace
  113. FrontendAction::FrontendAction() : Instance(nullptr) {}
  114. FrontendAction::~FrontendAction() {}
  115. void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
  116. std::unique_ptr<ASTUnit> AST) {
  117. this->CurrentInput = CurrentInput;
  118. CurrentASTUnit = std::move(AST);
  119. }
  120. std::unique_ptr<ASTConsumer>
  121. FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
  122. StringRef InFile) {
  123. std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
  124. if (!Consumer)
  125. return nullptr;
  126. // If there are no registered plugins we don't need to wrap the consumer
  127. if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end())
  128. return Consumer;
  129. // Collect the list of plugins that go before the main action (in Consumers)
  130. // or after it (in AfterConsumers)
  131. std::vector<std::unique_ptr<ASTConsumer>> Consumers;
  132. std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
  133. for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
  134. ie = FrontendPluginRegistry::end();
  135. it != ie; ++it) {
  136. std::unique_ptr<PluginASTAction> P = it->instantiate();
  137. PluginASTAction::ActionType ActionType = P->getActionType();
  138. if (ActionType == PluginASTAction::Cmdline) {
  139. // This is O(|plugins| * |add_plugins|), but since both numbers are
  140. // way below 50 in practice, that's ok.
  141. for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
  142. i != e; ++i) {
  143. if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
  144. ActionType = PluginASTAction::AddAfterMainAction;
  145. break;
  146. }
  147. }
  148. }
  149. if ((ActionType == PluginASTAction::AddBeforeMainAction ||
  150. ActionType == PluginASTAction::AddAfterMainAction) &&
  151. P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()])) {
  152. std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
  153. if (ActionType == PluginASTAction::AddBeforeMainAction) {
  154. Consumers.push_back(std::move(PluginConsumer));
  155. } else {
  156. AfterConsumers.push_back(std::move(PluginConsumer));
  157. }
  158. }
  159. }
  160. // Add to Consumers the main consumer, then all the plugins that go after it
  161. Consumers.push_back(std::move(Consumer));
  162. for (auto &C : AfterConsumers) {
  163. Consumers.push_back(std::move(C));
  164. }
  165. return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
  166. }
  167. bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
  168. const FrontendInputFile &Input) {
  169. assert(!Instance && "Already processing a source file!");
  170. assert(!Input.isEmpty() && "Unexpected empty filename!");
  171. setCurrentInput(Input);
  172. setCompilerInstance(&CI);
  173. StringRef InputFile = Input.getFile();
  174. bool HasBegunSourceFile = false;
  175. if (!BeginInvocation(CI))
  176. goto failure;
  177. // AST files follow a very different path, since they share objects via the
  178. // AST unit.
  179. if (Input.getKind() == IK_AST) {
  180. assert(!usesPreprocessorOnly() &&
  181. "Attempt to pass AST file to preprocessor only action!");
  182. assert(hasASTFileSupport() &&
  183. "This action does not have AST file support!");
  184. IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
  185. std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
  186. InputFile, CI.getPCHContainerReader(), Diags, CI.getFileSystemOpts(),
  187. CI.getCodeGenOpts().DebugTypeExtRefs);
  188. if (!AST)
  189. goto failure;
  190. // Inform the diagnostic client we are processing a source file.
  191. CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
  192. HasBegunSourceFile = true;
  193. // Set the shared objects, these are reset when we finish processing the
  194. // file, otherwise the CompilerInstance will happily destroy them.
  195. CI.setFileManager(&AST->getFileManager());
  196. CI.setSourceManager(&AST->getSourceManager());
  197. CI.setPreprocessor(&AST->getPreprocessor());
  198. CI.setASTContext(&AST->getASTContext());
  199. setCurrentInput(Input, std::move(AST));
  200. // Initialize the action.
  201. if (!BeginSourceFileAction(CI, InputFile))
  202. goto failure;
  203. // Create the AST consumer.
  204. CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
  205. if (!CI.hasASTConsumer())
  206. goto failure;
  207. return true;
  208. }
  209. if (!CI.hasVirtualFileSystem()) {
  210. if (IntrusiveRefCntPtr<vfs::FileSystem> VFS =
  211. createVFSFromCompilerInvocation(CI.getInvocation(),
  212. CI.getDiagnostics()))
  213. CI.setVirtualFileSystem(VFS);
  214. else
  215. goto failure;
  216. }
  217. // Set up the file and source managers, if needed.
  218. if (!CI.hasFileManager())
  219. CI.createFileManager();
  220. if (!CI.hasSourceManager())
  221. CI.createSourceManager(CI.getFileManager());
  222. // IR files bypass the rest of initialization.
  223. if (Input.getKind() == IK_LLVM_IR) {
  224. assert(hasIRSupport() &&
  225. "This action does not have IR file support!");
  226. // Inform the diagnostic client we are processing a source file.
  227. CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
  228. HasBegunSourceFile = true;
  229. // Initialize the action.
  230. if (!BeginSourceFileAction(CI, InputFile))
  231. goto failure;
  232. // Initialize the main file entry.
  233. if (!CI.InitializeSourceManager(CurrentInput))
  234. goto failure;
  235. return true;
  236. }
  237. // If the implicit PCH include is actually a directory, rather than
  238. // a single file, search for a suitable PCH file in that directory.
  239. if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
  240. FileManager &FileMgr = CI.getFileManager();
  241. PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
  242. StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
  243. std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath();
  244. if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
  245. std::error_code EC;
  246. SmallString<128> DirNative;
  247. llvm::sys::path::native(PCHDir->getName(), DirNative);
  248. bool Found = false;
  249. for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
  250. Dir != DirEnd && !EC; Dir.increment(EC)) {
  251. // Check whether this is an acceptable AST file.
  252. if (ASTReader::isAcceptableASTFile(
  253. Dir->path(), FileMgr, CI.getPCHContainerReader(),
  254. CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(),
  255. SpecificModuleCachePath)) {
  256. PPOpts.ImplicitPCHInclude = Dir->path();
  257. Found = true;
  258. break;
  259. }
  260. }
  261. if (!Found) {
  262. CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
  263. goto failure;
  264. }
  265. }
  266. }
  267. // Set up the preprocessor if needed. When parsing model files the
  268. // preprocessor of the original source is reused.
  269. if (!isModelParsingAction())
  270. CI.createPreprocessor(getTranslationUnitKind());
  271. // Inform the diagnostic client we are processing a source file.
  272. CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
  273. &CI.getPreprocessor());
  274. HasBegunSourceFile = true;
  275. // Initialize the action.
  276. if (!BeginSourceFileAction(CI, InputFile))
  277. goto failure;
  278. // Initialize the main file entry. It is important that this occurs after
  279. // BeginSourceFileAction, which may change CurrentInput during module builds.
  280. if (!CI.InitializeSourceManager(CurrentInput))
  281. goto failure;
  282. // Create the AST context and consumer unless this is a preprocessor only
  283. // action.
  284. if (!usesPreprocessorOnly()) {
  285. // Parsing a model file should reuse the existing ASTContext.
  286. if (!isModelParsingAction())
  287. CI.createASTContext();
  288. std::unique_ptr<ASTConsumer> Consumer =
  289. CreateWrappedASTConsumer(CI, InputFile);
  290. if (!Consumer)
  291. goto failure;
  292. // FIXME: should not overwrite ASTMutationListener when parsing model files?
  293. if (!isModelParsingAction())
  294. CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
  295. if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
  296. // Convert headers to PCH and chain them.
  297. IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
  298. source = createChainedIncludesSource(CI, FinalReader);
  299. if (!source)
  300. goto failure;
  301. CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get()));
  302. CI.getASTContext().setExternalSource(source);
  303. } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
  304. // Use PCH.
  305. assert(hasPCHSupport() && "This action does not have PCH support!");
  306. ASTDeserializationListener *DeserialListener =
  307. Consumer->GetASTDeserializationListener();
  308. bool DeleteDeserialListener = false;
  309. if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
  310. DeserialListener = new DeserializedDeclsDumper(DeserialListener,
  311. DeleteDeserialListener);
  312. DeleteDeserialListener = true;
  313. }
  314. if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
  315. DeserialListener = new DeserializedDeclsChecker(
  316. CI.getASTContext(),
  317. CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
  318. DeserialListener, DeleteDeserialListener);
  319. DeleteDeserialListener = true;
  320. }
  321. CI.createPCHExternalASTSource(
  322. CI.getPreprocessorOpts().ImplicitPCHInclude,
  323. CI.getPreprocessorOpts().DisablePCHValidation,
  324. CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener,
  325. DeleteDeserialListener);
  326. if (!CI.getASTContext().getExternalSource())
  327. goto failure;
  328. }
  329. CI.setASTConsumer(std::move(Consumer));
  330. if (!CI.hasASTConsumer())
  331. goto failure;
  332. }
  333. // Initialize built-in info as long as we aren't using an external AST
  334. // source.
  335. if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
  336. Preprocessor &PP = CI.getPreprocessor();
  337. // If modules are enabled, create the module manager before creating
  338. // any builtins, so that all declarations know that they might be
  339. // extended by an external source.
  340. if (CI.getLangOpts().Modules)
  341. CI.createModuleManager();
  342. PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
  343. PP.getLangOpts());
  344. } else {
  345. // FIXME: If this is a problem, recover from it by creating a multiplex
  346. // source.
  347. assert((!CI.getLangOpts().Modules || CI.getModuleManager()) &&
  348. "modules enabled but created an external source that "
  349. "doesn't support modules");
  350. }
  351. // If we were asked to load any module map files, do so now.
  352. for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
  353. if (auto *File = CI.getFileManager().getFile(Filename))
  354. CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
  355. File, /*IsSystem*/false);
  356. else
  357. CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
  358. }
  359. // If we were asked to load any module files, do so now.
  360. for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
  361. if (!CI.loadModuleFile(ModuleFile))
  362. goto failure;
  363. // If there is a layout overrides file, attach an external AST source that
  364. // provides the layouts from that file.
  365. if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
  366. CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
  367. IntrusiveRefCntPtr<ExternalASTSource>
  368. Override(new LayoutOverrideSource(
  369. CI.getFrontendOpts().OverrideRecordLayoutsFile));
  370. CI.getASTContext().setExternalSource(Override);
  371. }
  372. return true;
  373. // If we failed, reset state since the client will not end up calling the
  374. // matching EndSourceFile().
  375. failure:
  376. if (isCurrentFileAST()) {
  377. CI.setASTContext(nullptr);
  378. CI.setPreprocessor(nullptr);
  379. CI.setSourceManager(nullptr);
  380. CI.setFileManager(nullptr);
  381. }
  382. if (HasBegunSourceFile)
  383. CI.getDiagnosticClient().EndSourceFile();
  384. CI.clearOutputFiles(/*EraseFiles=*/true);
  385. setCurrentInput(FrontendInputFile());
  386. setCompilerInstance(nullptr);
  387. return false;
  388. }
  389. bool FrontendAction::Execute() {
  390. CompilerInstance &CI = getCompilerInstance();
  391. if (CI.hasFrontendTimer()) {
  392. llvm::TimeRegion Timer(CI.getFrontendTimer());
  393. ExecuteAction();
  394. }
  395. else ExecuteAction();
  396. // If we are supposed to rebuild the global module index, do so now unless
  397. // there were any module-build failures.
  398. if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
  399. CI.hasPreprocessor()) {
  400. StringRef Cache =
  401. CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
  402. if (!Cache.empty())
  403. GlobalModuleIndex::writeIndex(CI.getFileManager(),
  404. CI.getPCHContainerReader(), Cache);
  405. }
  406. return true;
  407. }
  408. void FrontendAction::EndSourceFile() {
  409. CompilerInstance &CI = getCompilerInstance();
  410. // Inform the diagnostic client we are done with this source file.
  411. CI.getDiagnosticClient().EndSourceFile();
  412. // Inform the preprocessor we are done.
  413. if (CI.hasPreprocessor())
  414. CI.getPreprocessor().EndSourceFile();
  415. // Finalize the action.
  416. EndSourceFileAction();
  417. // Sema references the ast consumer, so reset sema first.
  418. //
  419. // FIXME: There is more per-file stuff we could just drop here?
  420. bool DisableFree = CI.getFrontendOpts().DisableFree;
  421. if (DisableFree) {
  422. CI.resetAndLeakSema();
  423. CI.resetAndLeakASTContext();
  424. BuryPointer(CI.takeASTConsumer().get());
  425. } else {
  426. CI.setSema(nullptr);
  427. CI.setASTContext(nullptr);
  428. CI.setASTConsumer(nullptr);
  429. }
  430. if (CI.getFrontendOpts().ShowStats) {
  431. llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
  432. CI.getPreprocessor().PrintStats();
  433. CI.getPreprocessor().getIdentifierTable().PrintStats();
  434. CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
  435. CI.getSourceManager().PrintStats();
  436. llvm::errs() << "\n";
  437. }
  438. // Cleanup the output streams, and erase the output files if instructed by the
  439. // FrontendAction.
  440. CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
  441. if (isCurrentFileAST()) {
  442. if (DisableFree) {
  443. CI.resetAndLeakPreprocessor();
  444. CI.resetAndLeakSourceManager();
  445. CI.resetAndLeakFileManager();
  446. } else {
  447. CI.setPreprocessor(nullptr);
  448. CI.setSourceManager(nullptr);
  449. CI.setFileManager(nullptr);
  450. }
  451. }
  452. setCompilerInstance(nullptr);
  453. setCurrentInput(FrontendInputFile());
  454. }
  455. bool FrontendAction::shouldEraseOutputFiles() {
  456. return getCompilerInstance().getDiagnostics().hasErrorOccurred();
  457. }
  458. //===----------------------------------------------------------------------===//
  459. // Utility Actions
  460. //===----------------------------------------------------------------------===//
  461. void ASTFrontendAction::ExecuteAction() {
  462. CompilerInstance &CI = getCompilerInstance();
  463. if (!CI.hasPreprocessor())
  464. return;
  465. // FIXME: Move the truncation aspect of this into Sema, we delayed this till
  466. // here so the source manager would be initialized.
  467. if (hasCodeCompletionSupport() &&
  468. !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
  469. CI.createCodeCompletionConsumer();
  470. // Use a code completion consumer?
  471. CodeCompleteConsumer *CompletionConsumer = nullptr;
  472. if (CI.hasCodeCompletionConsumer())
  473. CompletionConsumer = &CI.getCodeCompletionConsumer();
  474. if (!CI.hasSema())
  475. CI.createSema(getTranslationUnitKind(), CompletionConsumer);
  476. ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
  477. CI.getFrontendOpts().SkipFunctionBodies);
  478. }
  479. void PluginASTAction::anchor() { }
  480. std::unique_ptr<ASTConsumer>
  481. PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
  482. StringRef InFile) {
  483. llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
  484. }
  485. std::unique_ptr<ASTConsumer>
  486. WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
  487. StringRef InFile) {
  488. return WrappedAction->CreateASTConsumer(CI, InFile);
  489. }
  490. bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
  491. return WrappedAction->BeginInvocation(CI);
  492. }
  493. bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
  494. StringRef Filename) {
  495. WrappedAction->setCurrentInput(getCurrentInput());
  496. WrappedAction->setCompilerInstance(&CI);
  497. auto Ret = WrappedAction->BeginSourceFileAction(CI, Filename);
  498. // BeginSourceFileAction may change CurrentInput, e.g. during module builds.
  499. setCurrentInput(WrappedAction->getCurrentInput());
  500. return Ret;
  501. }
  502. void WrapperFrontendAction::ExecuteAction() {
  503. WrappedAction->ExecuteAction();
  504. }
  505. void WrapperFrontendAction::EndSourceFileAction() {
  506. WrappedAction->EndSourceFileAction();
  507. }
  508. bool WrapperFrontendAction::usesPreprocessorOnly() const {
  509. return WrappedAction->usesPreprocessorOnly();
  510. }
  511. TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
  512. return WrappedAction->getTranslationUnitKind();
  513. }
  514. bool WrapperFrontendAction::hasPCHSupport() const {
  515. return WrappedAction->hasPCHSupport();
  516. }
  517. bool WrapperFrontendAction::hasASTFileSupport() const {
  518. return WrappedAction->hasASTFileSupport();
  519. }
  520. bool WrapperFrontendAction::hasIRSupport() const {
  521. return WrappedAction->hasIRSupport();
  522. }
  523. bool WrapperFrontendAction::hasCodeCompletionSupport() const {
  524. return WrappedAction->hasCodeCompletionSupport();
  525. }
  526. WrapperFrontendAction::WrapperFrontendAction(
  527. std::unique_ptr<FrontendAction> WrappedAction)
  528. : WrappedAction(std::move(WrappedAction)) {}