FrontendAction.cpp 22 KB

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