FrontendAction.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. //===--- FrontendAction.cpp -----------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "clang/Frontend/FrontendAction.h"
  9. #include "clang/AST/ASTConsumer.h"
  10. #include "clang/AST/ASTContext.h"
  11. #include "clang/AST/DeclGroup.h"
  12. #include "clang/Basic/Builtins.h"
  13. #include "clang/Basic/DiagnosticOptions.h"
  14. #include "clang/Basic/FileEntry.h"
  15. #include "clang/Basic/LangStandard.h"
  16. #include "clang/Basic/Sarif.h"
  17. #include "clang/Frontend/ASTUnit.h"
  18. #include "clang/Frontend/CompilerInstance.h"
  19. #include "clang/Frontend/FrontendDiagnostic.h"
  20. #include "clang/Frontend/FrontendPluginRegistry.h"
  21. #include "clang/Frontend/LayoutOverrideSource.h"
  22. #include "clang/Frontend/MultiplexConsumer.h"
  23. #include "clang/Frontend/SARIFDiagnosticPrinter.h"
  24. #include "clang/Frontend/Utils.h"
  25. #include "clang/Lex/HeaderSearch.h"
  26. #include "clang/Lex/LiteralSupport.h"
  27. #include "clang/Lex/Preprocessor.h"
  28. #include "clang/Lex/PreprocessorOptions.h"
  29. #include "clang/Parse/ParseAST.h"
  30. #include "clang/Sema/HLSLExternalSemaSource.h"
  31. #include "clang/Sema/MultiplexExternalSemaSource.h"
  32. #include "clang/Serialization/ASTDeserializationListener.h"
  33. #include "clang/Serialization/ASTReader.h"
  34. #include "clang/Serialization/GlobalModuleIndex.h"
  35. #include "llvm/ADT/ScopeExit.h"
  36. #include "llvm/Support/BuryPointer.h"
  37. #include "llvm/Support/ErrorHandling.h"
  38. #include "llvm/Support/FileSystem.h"
  39. #include "llvm/Support/Path.h"
  40. #include "llvm/Support/Timer.h"
  41. #include "llvm/Support/raw_ostream.h"
  42. #include <memory>
  43. #include <system_error>
  44. using namespace clang;
  45. LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
  46. namespace {
  47. class DelegatingDeserializationListener : public ASTDeserializationListener {
  48. ASTDeserializationListener *Previous;
  49. bool DeletePrevious;
  50. public:
  51. explicit DelegatingDeserializationListener(
  52. ASTDeserializationListener *Previous, bool DeletePrevious)
  53. : Previous(Previous), DeletePrevious(DeletePrevious) {}
  54. ~DelegatingDeserializationListener() override {
  55. if (DeletePrevious)
  56. delete Previous;
  57. }
  58. void ReaderInitialized(ASTReader *Reader) override {
  59. if (Previous)
  60. Previous->ReaderInitialized(Reader);
  61. }
  62. void IdentifierRead(serialization::IdentID ID,
  63. IdentifierInfo *II) override {
  64. if (Previous)
  65. Previous->IdentifierRead(ID, II);
  66. }
  67. void TypeRead(serialization::TypeIdx Idx, QualType T) override {
  68. if (Previous)
  69. Previous->TypeRead(Idx, T);
  70. }
  71. void DeclRead(serialization::DeclID ID, const Decl *D) override {
  72. if (Previous)
  73. Previous->DeclRead(ID, D);
  74. }
  75. void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
  76. if (Previous)
  77. Previous->SelectorRead(ID, Sel);
  78. }
  79. void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
  80. MacroDefinitionRecord *MD) override {
  81. if (Previous)
  82. Previous->MacroDefinitionRead(PPID, MD);
  83. }
  84. };
  85. /// Dumps deserialized declarations.
  86. class DeserializedDeclsDumper : public DelegatingDeserializationListener {
  87. public:
  88. explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
  89. bool DeletePrevious)
  90. : DelegatingDeserializationListener(Previous, DeletePrevious) {}
  91. void DeclRead(serialization::DeclID ID, const Decl *D) override {
  92. llvm::outs() << "PCH DECL: " << D->getDeclKindName();
  93. if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
  94. llvm::outs() << " - ";
  95. ND->printQualifiedName(llvm::outs());
  96. }
  97. llvm::outs() << "\n";
  98. DelegatingDeserializationListener::DeclRead(ID, D);
  99. }
  100. };
  101. /// Checks deserialized declarations and emits error if a name
  102. /// matches one given in command-line using -error-on-deserialized-decl.
  103. class DeserializedDeclsChecker : public DelegatingDeserializationListener {
  104. ASTContext &Ctx;
  105. std::set<std::string> NamesToCheck;
  106. public:
  107. DeserializedDeclsChecker(ASTContext &Ctx,
  108. const std::set<std::string> &NamesToCheck,
  109. ASTDeserializationListener *Previous,
  110. bool DeletePrevious)
  111. : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
  112. NamesToCheck(NamesToCheck) {}
  113. void DeclRead(serialization::DeclID ID, const Decl *D) override {
  114. if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
  115. if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
  116. unsigned DiagID
  117. = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
  118. "%0 was deserialized");
  119. Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
  120. << ND;
  121. }
  122. DelegatingDeserializationListener::DeclRead(ID, D);
  123. }
  124. };
  125. } // end anonymous namespace
  126. FrontendAction::FrontendAction() : Instance(nullptr) {}
  127. FrontendAction::~FrontendAction() {}
  128. void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
  129. std::unique_ptr<ASTUnit> AST) {
  130. this->CurrentInput = CurrentInput;
  131. CurrentASTUnit = std::move(AST);
  132. }
  133. Module *FrontendAction::getCurrentModule() const {
  134. CompilerInstance &CI = getCompilerInstance();
  135. return CI.getPreprocessor().getHeaderSearchInfo().lookupModule(
  136. CI.getLangOpts().CurrentModule, SourceLocation(), /*AllowSearch*/false);
  137. }
  138. std::unique_ptr<ASTConsumer>
  139. FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
  140. StringRef InFile) {
  141. std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
  142. if (!Consumer)
  143. return nullptr;
  144. // Validate -add-plugin args.
  145. bool FoundAllPlugins = true;
  146. for (const std::string &Arg : CI.getFrontendOpts().AddPluginActions) {
  147. bool Found = false;
  148. for (const FrontendPluginRegistry::entry &Plugin :
  149. FrontendPluginRegistry::entries()) {
  150. if (Plugin.getName() == Arg)
  151. Found = true;
  152. }
  153. if (!Found) {
  154. CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) << Arg;
  155. FoundAllPlugins = false;
  156. }
  157. }
  158. if (!FoundAllPlugins)
  159. return nullptr;
  160. // If there are no registered plugins we don't need to wrap the consumer
  161. if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end())
  162. return Consumer;
  163. // If this is a code completion run, avoid invoking the plugin consumers
  164. if (CI.hasCodeCompletionConsumer())
  165. return Consumer;
  166. // Collect the list of plugins that go before the main action (in Consumers)
  167. // or after it (in AfterConsumers)
  168. std::vector<std::unique_ptr<ASTConsumer>> Consumers;
  169. std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
  170. for (const FrontendPluginRegistry::entry &Plugin :
  171. FrontendPluginRegistry::entries()) {
  172. std::unique_ptr<PluginASTAction> P = Plugin.instantiate();
  173. PluginASTAction::ActionType ActionType = P->getActionType();
  174. if (ActionType == PluginASTAction::CmdlineAfterMainAction ||
  175. ActionType == PluginASTAction::CmdlineBeforeMainAction) {
  176. // This is O(|plugins| * |add_plugins|), but since both numbers are
  177. // way below 50 in practice, that's ok.
  178. if (llvm::is_contained(CI.getFrontendOpts().AddPluginActions,
  179. Plugin.getName())) {
  180. if (ActionType == PluginASTAction::CmdlineBeforeMainAction)
  181. ActionType = PluginASTAction::AddBeforeMainAction;
  182. else
  183. ActionType = PluginASTAction::AddAfterMainAction;
  184. }
  185. }
  186. if ((ActionType == PluginASTAction::AddBeforeMainAction ||
  187. ActionType == PluginASTAction::AddAfterMainAction) &&
  188. P->ParseArgs(
  189. CI,
  190. CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())])) {
  191. std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
  192. if (ActionType == PluginASTAction::AddBeforeMainAction) {
  193. Consumers.push_back(std::move(PluginConsumer));
  194. } else {
  195. AfterConsumers.push_back(std::move(PluginConsumer));
  196. }
  197. }
  198. }
  199. // Add to Consumers the main consumer, then all the plugins that go after it
  200. Consumers.push_back(std::move(Consumer));
  201. if (!AfterConsumers.empty()) {
  202. // If we have plugins after the main consumer, which may be the codegen
  203. // action, they likely will need the ASTContext, so don't clear it in the
  204. // codegen action.
  205. CI.getCodeGenOpts().ClearASTBeforeBackend = false;
  206. for (auto &C : AfterConsumers)
  207. Consumers.push_back(std::move(C));
  208. }
  209. return std::make_unique<MultiplexConsumer>(std::move(Consumers));
  210. }
  211. /// For preprocessed files, if the first line is the linemarker and specifies
  212. /// the original source file name, use that name as the input file name.
  213. /// Returns the location of the first token after the line marker directive.
  214. ///
  215. /// \param CI The compiler instance.
  216. /// \param InputFile Populated with the filename from the line marker.
  217. /// \param IsModuleMap If \c true, add a line note corresponding to this line
  218. /// directive. (We need to do this because the directive will not be
  219. /// visited by the preprocessor.)
  220. static SourceLocation ReadOriginalFileName(CompilerInstance &CI,
  221. std::string &InputFile,
  222. bool IsModuleMap = false) {
  223. auto &SourceMgr = CI.getSourceManager();
  224. auto MainFileID = SourceMgr.getMainFileID();
  225. auto MainFileBuf = SourceMgr.getBufferOrNone(MainFileID);
  226. if (!MainFileBuf)
  227. return SourceLocation();
  228. std::unique_ptr<Lexer> RawLexer(
  229. new Lexer(MainFileID, *MainFileBuf, SourceMgr, CI.getLangOpts()));
  230. // If the first line has the syntax of
  231. //
  232. // # NUM "FILENAME"
  233. //
  234. // we use FILENAME as the input file name.
  235. Token T;
  236. if (RawLexer->LexFromRawLexer(T) || T.getKind() != tok::hash)
  237. return SourceLocation();
  238. if (RawLexer->LexFromRawLexer(T) || T.isAtStartOfLine() ||
  239. T.getKind() != tok::numeric_constant)
  240. return SourceLocation();
  241. unsigned LineNo;
  242. SourceLocation LineNoLoc = T.getLocation();
  243. if (IsModuleMap) {
  244. llvm::SmallString<16> Buffer;
  245. if (Lexer::getSpelling(LineNoLoc, Buffer, SourceMgr, CI.getLangOpts())
  246. .getAsInteger(10, LineNo))
  247. return SourceLocation();
  248. }
  249. RawLexer->LexFromRawLexer(T);
  250. if (T.isAtStartOfLine() || T.getKind() != tok::string_literal)
  251. return SourceLocation();
  252. StringLiteralParser Literal(T, CI.getPreprocessor());
  253. if (Literal.hadError)
  254. return SourceLocation();
  255. RawLexer->LexFromRawLexer(T);
  256. if (T.isNot(tok::eof) && !T.isAtStartOfLine())
  257. return SourceLocation();
  258. InputFile = Literal.GetString().str();
  259. if (IsModuleMap)
  260. CI.getSourceManager().AddLineNote(
  261. LineNoLoc, LineNo, SourceMgr.getLineTableFilenameID(InputFile), false,
  262. false, SrcMgr::C_User_ModuleMap);
  263. return T.getLocation();
  264. }
  265. static SmallVectorImpl<char> &
  266. operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
  267. Includes.append(RHS.begin(), RHS.end());
  268. return Includes;
  269. }
  270. static void addHeaderInclude(StringRef HeaderName,
  271. SmallVectorImpl<char> &Includes,
  272. const LangOptions &LangOpts,
  273. bool IsExternC) {
  274. if (IsExternC && LangOpts.CPlusPlus)
  275. Includes += "extern \"C\" {\n";
  276. if (LangOpts.ObjC)
  277. Includes += "#import \"";
  278. else
  279. Includes += "#include \"";
  280. Includes += HeaderName;
  281. Includes += "\"\n";
  282. if (IsExternC && LangOpts.CPlusPlus)
  283. Includes += "}\n";
  284. }
  285. /// Collect the set of header includes needed to construct the given
  286. /// module and update the TopHeaders file set of the module.
  287. ///
  288. /// \param Module The module we're collecting includes from.
  289. ///
  290. /// \param Includes Will be augmented with the set of \#includes or \#imports
  291. /// needed to load all of the named headers.
  292. static std::error_code collectModuleHeaderIncludes(
  293. const LangOptions &LangOpts, FileManager &FileMgr, DiagnosticsEngine &Diag,
  294. ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl<char> &Includes) {
  295. // Don't collect any headers for unavailable modules.
  296. if (!Module->isAvailable())
  297. return std::error_code();
  298. // Resolve all lazy header directives to header files.
  299. ModMap.resolveHeaderDirectives(Module, /*File=*/std::nullopt);
  300. // If any headers are missing, we can't build this module. In most cases,
  301. // diagnostics for this should have already been produced; we only get here
  302. // if explicit stat information was provided.
  303. // FIXME: If the name resolves to a file with different stat information,
  304. // produce a better diagnostic.
  305. if (!Module->MissingHeaders.empty()) {
  306. auto &MissingHeader = Module->MissingHeaders.front();
  307. Diag.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
  308. << MissingHeader.IsUmbrella << MissingHeader.FileName;
  309. return std::error_code();
  310. }
  311. // Add includes for each of these headers.
  312. for (auto HK : {Module::HK_Normal, Module::HK_Private}) {
  313. for (Module::Header &H : Module->Headers[HK]) {
  314. Module->addTopHeader(H.Entry);
  315. // Use the path as specified in the module map file. We'll look for this
  316. // file relative to the module build directory (the directory containing
  317. // the module map file) so this will find the same file that we found
  318. // while parsing the module map.
  319. addHeaderInclude(H.PathRelativeToRootModuleDirectory, Includes, LangOpts,
  320. Module->IsExternC);
  321. }
  322. }
  323. // Note that Module->PrivateHeaders will not be a TopHeader.
  324. if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
  325. Module->addTopHeader(UmbrellaHeader.Entry);
  326. if (Module->Parent)
  327. // Include the umbrella header for submodules.
  328. addHeaderInclude(UmbrellaHeader.PathRelativeToRootModuleDirectory,
  329. Includes, LangOpts, Module->IsExternC);
  330. } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
  331. // Add all of the headers we find in this subdirectory.
  332. std::error_code EC;
  333. SmallString<128> DirNative;
  334. llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
  335. llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
  336. SmallVector<
  337. std::pair<std::string, OptionalFileEntryRefDegradesToFileEntryPtr>, 8>
  338. Headers;
  339. for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
  340. Dir != End && !EC; Dir.increment(EC)) {
  341. // Check whether this entry has an extension typically associated with
  342. // headers.
  343. if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
  344. .Cases(".h", ".H", ".hh", ".hpp", true)
  345. .Default(false))
  346. continue;
  347. auto Header = FileMgr.getOptionalFileRef(Dir->path());
  348. // FIXME: This shouldn't happen unless there is a file system race. Is
  349. // that worth diagnosing?
  350. if (!Header)
  351. continue;
  352. // If this header is marked 'unavailable' in this module, don't include
  353. // it.
  354. if (ModMap.isHeaderUnavailableInModule(*Header, Module))
  355. continue;
  356. // Compute the relative path from the directory to this file.
  357. SmallVector<StringRef, 16> Components;
  358. auto PathIt = llvm::sys::path::rbegin(Dir->path());
  359. for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
  360. Components.push_back(*PathIt);
  361. SmallString<128> RelativeHeader(
  362. UmbrellaDir.PathRelativeToRootModuleDirectory);
  363. for (auto It = Components.rbegin(), End = Components.rend(); It != End;
  364. ++It)
  365. llvm::sys::path::append(RelativeHeader, *It);
  366. std::string RelName = RelativeHeader.c_str();
  367. Headers.push_back(std::make_pair(RelName, *Header));
  368. }
  369. if (EC)
  370. return EC;
  371. // Sort header paths and make the header inclusion order deterministic
  372. // across different OSs and filesystems.
  373. llvm::sort(Headers, llvm::less_first());
  374. for (auto &H : Headers) {
  375. // Include this header as part of the umbrella directory.
  376. Module->addTopHeader(H.second);
  377. addHeaderInclude(H.first, Includes, LangOpts, Module->IsExternC);
  378. }
  379. }
  380. // Recurse into submodules.
  381. for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
  382. SubEnd = Module->submodule_end();
  383. Sub != SubEnd; ++Sub)
  384. if (std::error_code Err = collectModuleHeaderIncludes(
  385. LangOpts, FileMgr, Diag, ModMap, *Sub, Includes))
  386. return Err;
  387. return std::error_code();
  388. }
  389. static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
  390. bool IsPreprocessed,
  391. std::string &PresumedModuleMapFile,
  392. unsigned &Offset) {
  393. auto &SrcMgr = CI.getSourceManager();
  394. HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
  395. // Map the current input to a file.
  396. FileID ModuleMapID = SrcMgr.getMainFileID();
  397. const FileEntry *ModuleMap = SrcMgr.getFileEntryForID(ModuleMapID);
  398. // If the module map is preprocessed, handle the initial line marker;
  399. // line directives are not part of the module map syntax in general.
  400. Offset = 0;
  401. if (IsPreprocessed) {
  402. SourceLocation EndOfLineMarker =
  403. ReadOriginalFileName(CI, PresumedModuleMapFile, /*IsModuleMap*/ true);
  404. if (EndOfLineMarker.isValid())
  405. Offset = CI.getSourceManager().getDecomposedLoc(EndOfLineMarker).second;
  406. }
  407. // Load the module map file.
  408. if (HS.loadModuleMapFile(ModuleMap, IsSystem, ModuleMapID, &Offset,
  409. PresumedModuleMapFile))
  410. return true;
  411. if (SrcMgr.getBufferOrFake(ModuleMapID).getBufferSize() == Offset)
  412. Offset = 0;
  413. // Infer framework module if possible.
  414. if (HS.getModuleMap().canInferFrameworkModule(ModuleMap->getDir())) {
  415. SmallString<128> InferredFrameworkPath = ModuleMap->getDir()->getName();
  416. llvm::sys::path::append(InferredFrameworkPath,
  417. CI.getLangOpts().ModuleName + ".framework");
  418. if (auto Dir = CI.getFileManager().getDirectory(InferredFrameworkPath))
  419. (void)HS.getModuleMap().inferFrameworkModule(*Dir, IsSystem, nullptr);
  420. }
  421. return false;
  422. }
  423. static Module *prepareToBuildModule(CompilerInstance &CI,
  424. StringRef ModuleMapFilename) {
  425. if (CI.getLangOpts().CurrentModule.empty()) {
  426. CI.getDiagnostics().Report(diag::err_missing_module_name);
  427. // FIXME: Eventually, we could consider asking whether there was just
  428. // a single module described in the module map, and use that as a
  429. // default. Then it would be fairly trivial to just "compile" a module
  430. // map with a single module (the common case).
  431. return nullptr;
  432. }
  433. // Dig out the module definition.
  434. HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
  435. Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule, SourceLocation(),
  436. /*AllowSearch=*/true);
  437. if (!M) {
  438. CI.getDiagnostics().Report(diag::err_missing_module)
  439. << CI.getLangOpts().CurrentModule << ModuleMapFilename;
  440. return nullptr;
  441. }
  442. // Check whether we can build this module at all.
  443. if (Preprocessor::checkModuleIsAvailable(CI.getLangOpts(), CI.getTarget(),
  444. CI.getDiagnostics(), M))
  445. return nullptr;
  446. // Inform the preprocessor that includes from within the input buffer should
  447. // be resolved relative to the build directory of the module map file.
  448. CI.getPreprocessor().setMainFileDir(M->Directory);
  449. // If the module was inferred from a different module map (via an expanded
  450. // umbrella module definition), track that fact.
  451. // FIXME: It would be preferable to fill this in as part of processing
  452. // the module map, rather than adding it after the fact.
  453. StringRef OriginalModuleMapName = CI.getFrontendOpts().OriginalModuleMap;
  454. if (!OriginalModuleMapName.empty()) {
  455. auto OriginalModuleMap =
  456. CI.getFileManager().getFile(OriginalModuleMapName,
  457. /*openFile*/ true);
  458. if (!OriginalModuleMap) {
  459. CI.getDiagnostics().Report(diag::err_module_map_not_found)
  460. << OriginalModuleMapName;
  461. return nullptr;
  462. }
  463. if (*OriginalModuleMap != CI.getSourceManager().getFileEntryForID(
  464. CI.getSourceManager().getMainFileID())) {
  465. M->IsInferred = true;
  466. CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()
  467. .setInferredModuleAllowedBy(M, *OriginalModuleMap);
  468. }
  469. }
  470. // If we're being run from the command-line, the module build stack will not
  471. // have been filled in yet, so complete it now in order to allow us to detect
  472. // module cycles.
  473. SourceManager &SourceMgr = CI.getSourceManager();
  474. if (SourceMgr.getModuleBuildStack().empty())
  475. SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule,
  476. FullSourceLoc(SourceLocation(), SourceMgr));
  477. return M;
  478. }
  479. /// Compute the input buffer that should be used to build the specified module.
  480. static std::unique_ptr<llvm::MemoryBuffer>
  481. getInputBufferForModule(CompilerInstance &CI, Module *M) {
  482. FileManager &FileMgr = CI.getFileManager();
  483. // Collect the set of #includes we need to build the module.
  484. SmallString<256> HeaderContents;
  485. std::error_code Err = std::error_code();
  486. if (Module::Header UmbrellaHeader = M->getUmbrellaHeader())
  487. addHeaderInclude(UmbrellaHeader.PathRelativeToRootModuleDirectory,
  488. HeaderContents, CI.getLangOpts(), M->IsExternC);
  489. Err = collectModuleHeaderIncludes(
  490. CI.getLangOpts(), FileMgr, CI.getDiagnostics(),
  491. CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), M,
  492. HeaderContents);
  493. if (Err) {
  494. CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
  495. << M->getFullModuleName() << Err.message();
  496. return nullptr;
  497. }
  498. return llvm::MemoryBuffer::getMemBufferCopy(
  499. HeaderContents, Module::getModuleInputBufferName());
  500. }
  501. bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
  502. const FrontendInputFile &RealInput) {
  503. FrontendInputFile Input(RealInput);
  504. assert(!Instance && "Already processing a source file!");
  505. assert(!Input.isEmpty() && "Unexpected empty filename!");
  506. setCurrentInput(Input);
  507. setCompilerInstance(&CI);
  508. bool HasBegunSourceFile = false;
  509. bool ReplayASTFile = Input.getKind().getFormat() == InputKind::Precompiled &&
  510. usesPreprocessorOnly();
  511. // If we fail, reset state since the client will not end up calling the
  512. // matching EndSourceFile(). All paths that return true should release this.
  513. auto FailureCleanup = llvm::make_scope_exit([&]() {
  514. if (HasBegunSourceFile)
  515. CI.getDiagnosticClient().EndSourceFile();
  516. CI.setASTConsumer(nullptr);
  517. CI.clearOutputFiles(/*EraseFiles=*/true);
  518. CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
  519. setCurrentInput(FrontendInputFile());
  520. setCompilerInstance(nullptr);
  521. });
  522. if (!BeginInvocation(CI))
  523. return false;
  524. // If we're replaying the build of an AST file, import it and set up
  525. // the initial state from its build.
  526. if (ReplayASTFile) {
  527. IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
  528. // The AST unit populates its own diagnostics engine rather than ours.
  529. IntrusiveRefCntPtr<DiagnosticsEngine> ASTDiags(
  530. new DiagnosticsEngine(Diags->getDiagnosticIDs(),
  531. &Diags->getDiagnosticOptions()));
  532. ASTDiags->setClient(Diags->getClient(), /*OwnsClient*/false);
  533. // FIXME: What if the input is a memory buffer?
  534. StringRef InputFile = Input.getFile();
  535. std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
  536. std::string(InputFile), CI.getPCHContainerReader(),
  537. ASTUnit::LoadPreprocessorOnly, ASTDiags, CI.getFileSystemOpts(),
  538. CI.getCodeGenOpts().DebugTypeExtRefs);
  539. if (!AST)
  540. return false;
  541. // Options relating to how we treat the input (but not what we do with it)
  542. // are inherited from the AST unit.
  543. CI.getHeaderSearchOpts() = AST->getHeaderSearchOpts();
  544. CI.getPreprocessorOpts() = AST->getPreprocessorOpts();
  545. CI.getLangOpts() = AST->getLangOpts();
  546. // Set the shared objects, these are reset when we finish processing the
  547. // file, otherwise the CompilerInstance will happily destroy them.
  548. CI.setFileManager(&AST->getFileManager());
  549. CI.createSourceManager(CI.getFileManager());
  550. CI.getSourceManager().initializeForReplay(AST->getSourceManager());
  551. // Preload all the module files loaded transitively by the AST unit. Also
  552. // load all module map files that were parsed as part of building the AST
  553. // unit.
  554. if (auto ASTReader = AST->getASTReader()) {
  555. auto &MM = ASTReader->getModuleManager();
  556. auto &PrimaryModule = MM.getPrimaryModule();
  557. for (serialization::ModuleFile &MF : MM)
  558. if (&MF != &PrimaryModule)
  559. CI.getFrontendOpts().ModuleFiles.push_back(MF.FileName);
  560. ASTReader->visitTopLevelModuleMaps(PrimaryModule, [&](FileEntryRef FE) {
  561. CI.getFrontendOpts().ModuleMapFiles.push_back(
  562. std::string(FE.getName()));
  563. });
  564. }
  565. // Set up the input file for replay purposes.
  566. auto Kind = AST->getInputKind();
  567. if (Kind.getFormat() == InputKind::ModuleMap) {
  568. Module *ASTModule =
  569. AST->getPreprocessor().getHeaderSearchInfo().lookupModule(
  570. AST->getLangOpts().CurrentModule, SourceLocation(),
  571. /*AllowSearch*/ false);
  572. assert(ASTModule && "module file does not define its own module");
  573. Input = FrontendInputFile(ASTModule->PresumedModuleMapFile, Kind);
  574. } else {
  575. auto &OldSM = AST->getSourceManager();
  576. FileID ID = OldSM.getMainFileID();
  577. if (auto *File = OldSM.getFileEntryForID(ID))
  578. Input = FrontendInputFile(File->getName(), Kind);
  579. else
  580. Input = FrontendInputFile(OldSM.getBufferOrFake(ID), Kind);
  581. }
  582. setCurrentInput(Input, std::move(AST));
  583. }
  584. // AST files follow a very different path, since they share objects via the
  585. // AST unit.
  586. if (Input.getKind().getFormat() == InputKind::Precompiled) {
  587. assert(!usesPreprocessorOnly() && "this case was handled above");
  588. assert(hasASTFileSupport() &&
  589. "This action does not have AST file support!");
  590. IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
  591. // FIXME: What if the input is a memory buffer?
  592. StringRef InputFile = Input.getFile();
  593. std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
  594. std::string(InputFile), CI.getPCHContainerReader(),
  595. ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts(),
  596. CI.getCodeGenOpts().DebugTypeExtRefs);
  597. if (!AST)
  598. return false;
  599. // Inform the diagnostic client we are processing a source file.
  600. CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
  601. HasBegunSourceFile = true;
  602. // Set the shared objects, these are reset when we finish processing the
  603. // file, otherwise the CompilerInstance will happily destroy them.
  604. CI.setFileManager(&AST->getFileManager());
  605. CI.setSourceManager(&AST->getSourceManager());
  606. CI.setPreprocessor(AST->getPreprocessorPtr());
  607. Preprocessor &PP = CI.getPreprocessor();
  608. PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
  609. PP.getLangOpts());
  610. CI.setASTContext(&AST->getASTContext());
  611. setCurrentInput(Input, std::move(AST));
  612. // Initialize the action.
  613. if (!BeginSourceFileAction(CI))
  614. return false;
  615. // Create the AST consumer.
  616. CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
  617. if (!CI.hasASTConsumer())
  618. return false;
  619. FailureCleanup.release();
  620. return true;
  621. }
  622. // Set up the file and source managers, if needed.
  623. if (!CI.hasFileManager()) {
  624. if (!CI.createFileManager()) {
  625. return false;
  626. }
  627. }
  628. if (!CI.hasSourceManager()) {
  629. CI.createSourceManager(CI.getFileManager());
  630. if (CI.getDiagnosticOpts().getFormat() == DiagnosticOptions::SARIF) {
  631. static_cast<SARIFDiagnosticPrinter *>(&CI.getDiagnosticClient())
  632. ->setSarifWriter(
  633. std::make_unique<SarifDocumentWriter>(CI.getSourceManager()));
  634. }
  635. }
  636. // Set up embedding for any specified files. Do this before we load any
  637. // source files, including the primary module map for the compilation.
  638. for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
  639. if (auto FE = CI.getFileManager().getFile(F, /*openFile*/true))
  640. CI.getSourceManager().setFileIsTransient(*FE);
  641. else
  642. CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F;
  643. }
  644. if (CI.getFrontendOpts().ModulesEmbedAllFiles)
  645. CI.getSourceManager().setAllFilesAreTransient(true);
  646. // IR files bypass the rest of initialization.
  647. if (Input.getKind().getLanguage() == Language::LLVM_IR) {
  648. assert(hasIRSupport() &&
  649. "This action does not have IR file support!");
  650. // Inform the diagnostic client we are processing a source file.
  651. CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
  652. HasBegunSourceFile = true;
  653. // Initialize the action.
  654. if (!BeginSourceFileAction(CI))
  655. return false;
  656. // Initialize the main file entry.
  657. if (!CI.InitializeSourceManager(CurrentInput))
  658. return false;
  659. FailureCleanup.release();
  660. return true;
  661. }
  662. // If the implicit PCH include is actually a directory, rather than
  663. // a single file, search for a suitable PCH file in that directory.
  664. if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
  665. FileManager &FileMgr = CI.getFileManager();
  666. PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
  667. StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
  668. std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath();
  669. if (auto PCHDir = FileMgr.getOptionalDirectoryRef(PCHInclude)) {
  670. std::error_code EC;
  671. SmallString<128> DirNative;
  672. llvm::sys::path::native(PCHDir->getName(), DirNative);
  673. bool Found = false;
  674. llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
  675. for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC),
  676. DirEnd;
  677. Dir != DirEnd && !EC; Dir.increment(EC)) {
  678. // Check whether this is an acceptable AST file.
  679. if (ASTReader::isAcceptableASTFile(
  680. Dir->path(), FileMgr, CI.getModuleCache(),
  681. CI.getPCHContainerReader(), CI.getLangOpts(),
  682. CI.getTargetOpts(), CI.getPreprocessorOpts(),
  683. SpecificModuleCachePath, /*RequireStrictOptionMatches=*/true)) {
  684. PPOpts.ImplicitPCHInclude = std::string(Dir->path());
  685. Found = true;
  686. break;
  687. }
  688. }
  689. if (!Found) {
  690. CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
  691. return false;
  692. }
  693. }
  694. }
  695. // Set up the preprocessor if needed. When parsing model files the
  696. // preprocessor of the original source is reused.
  697. if (!isModelParsingAction())
  698. CI.createPreprocessor(getTranslationUnitKind());
  699. // Inform the diagnostic client we are processing a source file.
  700. CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
  701. &CI.getPreprocessor());
  702. HasBegunSourceFile = true;
  703. // Handle C++20 header units.
  704. // Here, the user has the option to specify that the header name should be
  705. // looked up in the pre-processor search paths (and the main filename as
  706. // passed by the driver might therefore be incomplete until that look-up).
  707. if (CI.getLangOpts().CPlusPlusModules && Input.getKind().isHeaderUnit() &&
  708. !Input.getKind().isPreprocessed()) {
  709. StringRef FileName = Input.getFile();
  710. InputKind Kind = Input.getKind();
  711. if (Kind.getHeaderUnitKind() != InputKind::HeaderUnit_Abs) {
  712. assert(CI.hasPreprocessor() &&
  713. "trying to build a header unit without a Pre-processor?");
  714. HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
  715. // Relative searches begin from CWD.
  716. const DirectoryEntry *Dir = nullptr;
  717. if (auto DirOrErr = CI.getFileManager().getDirectory("."))
  718. Dir = *DirOrErr;
  719. SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 1> CWD;
  720. CWD.push_back({nullptr, Dir});
  721. OptionalFileEntryRef FE =
  722. HS.LookupFile(FileName, SourceLocation(),
  723. /*Angled*/ Input.getKind().getHeaderUnitKind() ==
  724. InputKind::HeaderUnit_System,
  725. nullptr, nullptr, CWD, nullptr, nullptr, nullptr,
  726. nullptr, nullptr, nullptr);
  727. if (!FE) {
  728. CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
  729. << FileName;
  730. return false;
  731. }
  732. // We now have the filename...
  733. FileName = FE->getFileEntry().getName();
  734. // ... still a header unit, but now use the path as written.
  735. Kind = Input.getKind().withHeaderUnit(InputKind::HeaderUnit_Abs);
  736. Input = FrontendInputFile(FileName, Kind, Input.isSystem());
  737. }
  738. // Unless the user has overridden the name, the header unit module name is
  739. // the pathname for the file.
  740. if (CI.getLangOpts().ModuleName.empty())
  741. CI.getLangOpts().ModuleName = std::string(FileName);
  742. CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName;
  743. }
  744. if (!CI.InitializeSourceManager(Input))
  745. return false;
  746. if (CI.getLangOpts().CPlusPlusModules && Input.getKind().isHeaderUnit() &&
  747. Input.getKind().isPreprocessed() && !usesPreprocessorOnly()) {
  748. // We have an input filename like foo.iih, but we want to find the right
  749. // module name (and original file, to build the map entry).
  750. // Check if the first line specifies the original source file name with a
  751. // linemarker.
  752. std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
  753. ReadOriginalFileName(CI, PresumedInputFile);
  754. // Unless the user overrides this, the module name is the name by which the
  755. // original file was known.
  756. if (CI.getLangOpts().ModuleName.empty())
  757. CI.getLangOpts().ModuleName = std::string(PresumedInputFile);
  758. CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName;
  759. }
  760. // For module map files, we first parse the module map and synthesize a
  761. // "<module-includes>" buffer before more conventional processing.
  762. if (Input.getKind().getFormat() == InputKind::ModuleMap) {
  763. CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap);
  764. std::string PresumedModuleMapFile;
  765. unsigned OffsetToContents;
  766. if (loadModuleMapForModuleBuild(CI, Input.isSystem(),
  767. Input.isPreprocessed(),
  768. PresumedModuleMapFile, OffsetToContents))
  769. return false;
  770. auto *CurrentModule = prepareToBuildModule(CI, Input.getFile());
  771. if (!CurrentModule)
  772. return false;
  773. CurrentModule->PresumedModuleMapFile = PresumedModuleMapFile;
  774. if (OffsetToContents)
  775. // If the module contents are in the same file, skip to them.
  776. CI.getPreprocessor().setSkipMainFilePreamble(OffsetToContents, true);
  777. else {
  778. // Otherwise, convert the module description to a suitable input buffer.
  779. auto Buffer = getInputBufferForModule(CI, CurrentModule);
  780. if (!Buffer)
  781. return false;
  782. // Reinitialize the main file entry to refer to the new input.
  783. auto Kind = CurrentModule->IsSystem ? SrcMgr::C_System : SrcMgr::C_User;
  784. auto &SourceMgr = CI.getSourceManager();
  785. auto BufferID = SourceMgr.createFileID(std::move(Buffer), Kind);
  786. assert(BufferID.isValid() && "couldn't create module buffer ID");
  787. SourceMgr.setMainFileID(BufferID);
  788. }
  789. }
  790. // Initialize the action.
  791. if (!BeginSourceFileAction(CI))
  792. return false;
  793. // If we were asked to load any module map files, do so now.
  794. for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
  795. if (auto File = CI.getFileManager().getFile(Filename))
  796. CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
  797. *File, /*IsSystem*/false);
  798. else
  799. CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
  800. }
  801. // If compiling implementation of a module, load its module map file now.
  802. (void)CI.getPreprocessor().getCurrentModuleImplementation();
  803. // Add a module declaration scope so that modules from -fmodule-map-file
  804. // arguments may shadow modules found implicitly in search paths.
  805. CI.getPreprocessor()
  806. .getHeaderSearchInfo()
  807. .getModuleMap()
  808. .finishModuleDeclarationScope();
  809. // Create the AST context and consumer unless this is a preprocessor only
  810. // action.
  811. if (!usesPreprocessorOnly()) {
  812. // Parsing a model file should reuse the existing ASTContext.
  813. if (!isModelParsingAction())
  814. CI.createASTContext();
  815. // For preprocessed files, check if the first line specifies the original
  816. // source file name with a linemarker.
  817. std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
  818. if (Input.isPreprocessed())
  819. ReadOriginalFileName(CI, PresumedInputFile);
  820. std::unique_ptr<ASTConsumer> Consumer =
  821. CreateWrappedASTConsumer(CI, PresumedInputFile);
  822. if (!Consumer)
  823. return false;
  824. // FIXME: should not overwrite ASTMutationListener when parsing model files?
  825. if (!isModelParsingAction())
  826. CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
  827. if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
  828. // Convert headers to PCH and chain them.
  829. IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
  830. source = createChainedIncludesSource(CI, FinalReader);
  831. if (!source)
  832. return false;
  833. CI.setASTReader(static_cast<ASTReader *>(FinalReader.get()));
  834. CI.getASTContext().setExternalSource(source);
  835. } else if (CI.getLangOpts().Modules ||
  836. !CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
  837. // Use PCM or PCH.
  838. assert(hasPCHSupport() && "This action does not have PCH support!");
  839. ASTDeserializationListener *DeserialListener =
  840. Consumer->GetASTDeserializationListener();
  841. bool DeleteDeserialListener = false;
  842. if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
  843. DeserialListener = new DeserializedDeclsDumper(DeserialListener,
  844. DeleteDeserialListener);
  845. DeleteDeserialListener = true;
  846. }
  847. if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
  848. DeserialListener = new DeserializedDeclsChecker(
  849. CI.getASTContext(),
  850. CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
  851. DeserialListener, DeleteDeserialListener);
  852. DeleteDeserialListener = true;
  853. }
  854. if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
  855. CI.createPCHExternalASTSource(
  856. CI.getPreprocessorOpts().ImplicitPCHInclude,
  857. CI.getPreprocessorOpts().DisablePCHOrModuleValidation,
  858. CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
  859. DeserialListener, DeleteDeserialListener);
  860. if (!CI.getASTContext().getExternalSource())
  861. return false;
  862. }
  863. // If modules are enabled, create the AST reader before creating
  864. // any builtins, so that all declarations know that they might be
  865. // extended by an external source.
  866. if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
  867. !CI.getASTContext().getExternalSource()) {
  868. CI.createASTReader();
  869. CI.getASTReader()->setDeserializationListener(DeserialListener,
  870. DeleteDeserialListener);
  871. }
  872. }
  873. CI.setASTConsumer(std::move(Consumer));
  874. if (!CI.hasASTConsumer())
  875. return false;
  876. }
  877. // Initialize built-in info as long as we aren't using an external AST
  878. // source.
  879. if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
  880. !CI.getASTContext().getExternalSource()) {
  881. Preprocessor &PP = CI.getPreprocessor();
  882. PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
  883. PP.getLangOpts());
  884. } else {
  885. // FIXME: If this is a problem, recover from it by creating a multiplex
  886. // source.
  887. assert((!CI.getLangOpts().Modules || CI.getASTReader()) &&
  888. "modules enabled but created an external source that "
  889. "doesn't support modules");
  890. }
  891. // If we were asked to load any module files, do so now.
  892. for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
  893. if (!CI.loadModuleFile(ModuleFile))
  894. return false;
  895. // If there is a layout overrides file, attach an external AST source that
  896. // provides the layouts from that file.
  897. if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
  898. CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
  899. IntrusiveRefCntPtr<ExternalASTSource>
  900. Override(new LayoutOverrideSource(
  901. CI.getFrontendOpts().OverrideRecordLayoutsFile));
  902. CI.getASTContext().setExternalSource(Override);
  903. }
  904. // Setup HLSL External Sema Source
  905. if (CI.getLangOpts().HLSL && CI.hasASTContext()) {
  906. IntrusiveRefCntPtr<ExternalSemaSource> HLSLSema(
  907. new HLSLExternalSemaSource());
  908. if (auto *SemaSource = dyn_cast_if_present<ExternalSemaSource>(
  909. CI.getASTContext().getExternalSource())) {
  910. IntrusiveRefCntPtr<ExternalSemaSource> MultiSema(
  911. new MultiplexExternalSemaSource(SemaSource, HLSLSema.get()));
  912. CI.getASTContext().setExternalSource(MultiSema);
  913. } else
  914. CI.getASTContext().setExternalSource(HLSLSema);
  915. }
  916. FailureCleanup.release();
  917. return true;
  918. }
  919. llvm::Error FrontendAction::Execute() {
  920. CompilerInstance &CI = getCompilerInstance();
  921. if (CI.hasFrontendTimer()) {
  922. llvm::TimeRegion Timer(CI.getFrontendTimer());
  923. ExecuteAction();
  924. }
  925. else ExecuteAction();
  926. // If we are supposed to rebuild the global module index, do so now unless
  927. // there were any module-build failures.
  928. if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
  929. CI.hasPreprocessor()) {
  930. StringRef Cache =
  931. CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
  932. if (!Cache.empty()) {
  933. if (llvm::Error Err = GlobalModuleIndex::writeIndex(
  934. CI.getFileManager(), CI.getPCHContainerReader(), Cache)) {
  935. // FIXME this drops the error on the floor, but
  936. // Index/pch-from-libclang.c seems to rely on dropping at least some of
  937. // the error conditions!
  938. consumeError(std::move(Err));
  939. }
  940. }
  941. }
  942. return llvm::Error::success();
  943. }
  944. void FrontendAction::EndSourceFile() {
  945. CompilerInstance &CI = getCompilerInstance();
  946. // Inform the diagnostic client we are done with this source file.
  947. CI.getDiagnosticClient().EndSourceFile();
  948. // Inform the preprocessor we are done.
  949. if (CI.hasPreprocessor())
  950. CI.getPreprocessor().EndSourceFile();
  951. // Finalize the action.
  952. EndSourceFileAction();
  953. // Sema references the ast consumer, so reset sema first.
  954. //
  955. // FIXME: There is more per-file stuff we could just drop here?
  956. bool DisableFree = CI.getFrontendOpts().DisableFree;
  957. if (DisableFree) {
  958. CI.resetAndLeakSema();
  959. CI.resetAndLeakASTContext();
  960. llvm::BuryPointer(CI.takeASTConsumer().get());
  961. } else {
  962. CI.setSema(nullptr);
  963. CI.setASTContext(nullptr);
  964. CI.setASTConsumer(nullptr);
  965. }
  966. if (CI.getFrontendOpts().ShowStats) {
  967. llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFileOrBufferName() << "':\n";
  968. CI.getPreprocessor().PrintStats();
  969. CI.getPreprocessor().getIdentifierTable().PrintStats();
  970. CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
  971. CI.getSourceManager().PrintStats();
  972. llvm::errs() << "\n";
  973. }
  974. // Cleanup the output streams, and erase the output files if instructed by the
  975. // FrontendAction.
  976. CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
  977. if (isCurrentFileAST()) {
  978. if (DisableFree) {
  979. CI.resetAndLeakPreprocessor();
  980. CI.resetAndLeakSourceManager();
  981. CI.resetAndLeakFileManager();
  982. llvm::BuryPointer(std::move(CurrentASTUnit));
  983. } else {
  984. CI.setPreprocessor(nullptr);
  985. CI.setSourceManager(nullptr);
  986. CI.setFileManager(nullptr);
  987. }
  988. }
  989. setCompilerInstance(nullptr);
  990. setCurrentInput(FrontendInputFile());
  991. CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
  992. }
  993. bool FrontendAction::shouldEraseOutputFiles() {
  994. return getCompilerInstance().getDiagnostics().hasErrorOccurred();
  995. }
  996. //===----------------------------------------------------------------------===//
  997. // Utility Actions
  998. //===----------------------------------------------------------------------===//
  999. void ASTFrontendAction::ExecuteAction() {
  1000. CompilerInstance &CI = getCompilerInstance();
  1001. if (!CI.hasPreprocessor())
  1002. return;
  1003. // FIXME: Move the truncation aspect of this into Sema, we delayed this till
  1004. // here so the source manager would be initialized.
  1005. if (hasCodeCompletionSupport() &&
  1006. !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
  1007. CI.createCodeCompletionConsumer();
  1008. // Use a code completion consumer?
  1009. CodeCompleteConsumer *CompletionConsumer = nullptr;
  1010. if (CI.hasCodeCompletionConsumer())
  1011. CompletionConsumer = &CI.getCodeCompletionConsumer();
  1012. if (!CI.hasSema())
  1013. CI.createSema(getTranslationUnitKind(), CompletionConsumer);
  1014. ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
  1015. CI.getFrontendOpts().SkipFunctionBodies);
  1016. }
  1017. void PluginASTAction::anchor() { }
  1018. std::unique_ptr<ASTConsumer>
  1019. PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
  1020. StringRef InFile) {
  1021. llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
  1022. }
  1023. bool WrapperFrontendAction::PrepareToExecuteAction(CompilerInstance &CI) {
  1024. return WrappedAction->PrepareToExecuteAction(CI);
  1025. }
  1026. std::unique_ptr<ASTConsumer>
  1027. WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
  1028. StringRef InFile) {
  1029. return WrappedAction->CreateASTConsumer(CI, InFile);
  1030. }
  1031. bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
  1032. return WrappedAction->BeginInvocation(CI);
  1033. }
  1034. bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI) {
  1035. WrappedAction->setCurrentInput(getCurrentInput());
  1036. WrappedAction->setCompilerInstance(&CI);
  1037. auto Ret = WrappedAction->BeginSourceFileAction(CI);
  1038. // BeginSourceFileAction may change CurrentInput, e.g. during module builds.
  1039. setCurrentInput(WrappedAction->getCurrentInput());
  1040. return Ret;
  1041. }
  1042. void WrapperFrontendAction::ExecuteAction() {
  1043. WrappedAction->ExecuteAction();
  1044. }
  1045. void WrapperFrontendAction::EndSourceFile() { WrappedAction->EndSourceFile(); }
  1046. void WrapperFrontendAction::EndSourceFileAction() {
  1047. WrappedAction->EndSourceFileAction();
  1048. }
  1049. bool WrapperFrontendAction::shouldEraseOutputFiles() {
  1050. return WrappedAction->shouldEraseOutputFiles();
  1051. }
  1052. bool WrapperFrontendAction::usesPreprocessorOnly() const {
  1053. return WrappedAction->usesPreprocessorOnly();
  1054. }
  1055. TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
  1056. return WrappedAction->getTranslationUnitKind();
  1057. }
  1058. bool WrapperFrontendAction::hasPCHSupport() const {
  1059. return WrappedAction->hasPCHSupport();
  1060. }
  1061. bool WrapperFrontendAction::hasASTFileSupport() const {
  1062. return WrappedAction->hasASTFileSupport();
  1063. }
  1064. bool WrapperFrontendAction::hasIRSupport() const {
  1065. return WrappedAction->hasIRSupport();
  1066. }
  1067. bool WrapperFrontendAction::hasCodeCompletionSupport() const {
  1068. return WrappedAction->hasCodeCompletionSupport();
  1069. }
  1070. WrapperFrontendAction::WrapperFrontendAction(
  1071. std::unique_ptr<FrontendAction> WrappedAction)
  1072. : WrappedAction(std::move(WrappedAction)) {}