CompilerInstance.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  1. //===--- CompilerInstance.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/CompilerInstance.h"
  10. #include "clang/AST/ASTConsumer.h"
  11. #include "clang/AST/ASTContext.h"
  12. #include "clang/AST/Decl.h"
  13. #include "clang/Basic/Diagnostic.h"
  14. #include "clang/Basic/FileManager.h"
  15. #include "clang/Basic/SourceManager.h"
  16. #include "clang/Basic/TargetInfo.h"
  17. #include "clang/Basic/Version.h"
  18. #include "clang/Config/config.h"
  19. #include "clang/Frontend/ChainedDiagnosticConsumer.h"
  20. #include "clang/Frontend/FrontendAction.h"
  21. #include "clang/Frontend/FrontendActions.h"
  22. #include "clang/Frontend/FrontendDiagnostic.h"
  23. #include "clang/Frontend/LogDiagnosticPrinter.h"
  24. #include "clang/Frontend/SerializedDiagnosticPrinter.h"
  25. #include "clang/Frontend/TextDiagnosticPrinter.h"
  26. #include "clang/Frontend/Utils.h"
  27. #include "clang/Frontend/VerifyDiagnosticConsumer.h"
  28. #include "clang/Lex/HeaderSearch.h"
  29. #include "clang/Lex/PTHManager.h"
  30. #include "clang/Lex/Preprocessor.h"
  31. #include "clang/Sema/CodeCompleteConsumer.h"
  32. #include "clang/Sema/Sema.h"
  33. #include "clang/Serialization/ASTReader.h"
  34. #include "clang/Serialization/GlobalModuleIndex.h"
  35. #include "llvm/ADT/Statistic.h"
  36. #include "llvm/Support/CrashRecoveryContext.h"
  37. #include "llvm/Support/Errc.h"
  38. #include "llvm/Support/FileSystem.h"
  39. #include "llvm/Support/Host.h"
  40. #include "llvm/Support/LockFileManager.h"
  41. #include "llvm/Support/MemoryBuffer.h"
  42. #include "llvm/Support/Path.h"
  43. #include "llvm/Support/Program.h"
  44. #include "llvm/Support/Signals.h"
  45. #include "llvm/Support/Timer.h"
  46. #include "llvm/Support/raw_ostream.h"
  47. #include <sys/stat.h>
  48. #include <system_error>
  49. #include <time.h>
  50. using namespace clang;
  51. CompilerInstance::CompilerInstance(
  52. std::shared_ptr<PCHContainerOperations> PCHContainerOps,
  53. bool BuildingModule)
  54. : ModuleLoader(BuildingModule), Invocation(new CompilerInvocation()),
  55. ModuleManager(nullptr), ThePCHContainerOperations(PCHContainerOps),
  56. BuildGlobalModuleIndex(false), HaveFullGlobalModuleIndex(false),
  57. ModuleBuildFailed(false) {}
  58. CompilerInstance::~CompilerInstance() {
  59. assert(OutputFiles.empty() && "Still output files in flight?");
  60. }
  61. void CompilerInstance::setInvocation(CompilerInvocation *Value) {
  62. Invocation = Value;
  63. }
  64. bool CompilerInstance::shouldBuildGlobalModuleIndex() const {
  65. return (BuildGlobalModuleIndex ||
  66. (ModuleManager && ModuleManager->isGlobalIndexUnavailable() &&
  67. getFrontendOpts().GenerateGlobalModuleIndex)) &&
  68. !ModuleBuildFailed;
  69. }
  70. void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) {
  71. Diagnostics = Value;
  72. }
  73. void CompilerInstance::setTarget(TargetInfo *Value) { Target = Value; }
  74. void CompilerInstance::setAuxTarget(TargetInfo *Value) { AuxTarget = Value; }
  75. void CompilerInstance::setFileManager(FileManager *Value) {
  76. FileMgr = Value;
  77. if (Value)
  78. VirtualFileSystem = Value->getVirtualFileSystem();
  79. else
  80. VirtualFileSystem.reset();
  81. }
  82. void CompilerInstance::setSourceManager(SourceManager *Value) {
  83. SourceMgr = Value;
  84. }
  85. void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; }
  86. void CompilerInstance::setASTContext(ASTContext *Value) {
  87. Context = Value;
  88. if (Context && Consumer)
  89. getASTConsumer().Initialize(getASTContext());
  90. }
  91. void CompilerInstance::setSema(Sema *S) {
  92. TheSema.reset(S);
  93. }
  94. void CompilerInstance::setASTConsumer(std::unique_ptr<ASTConsumer> Value) {
  95. Consumer = std::move(Value);
  96. if (Context && Consumer)
  97. getASTConsumer().Initialize(getASTContext());
  98. }
  99. void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
  100. CompletionConsumer.reset(Value);
  101. }
  102. std::unique_ptr<Sema> CompilerInstance::takeSema() {
  103. return std::move(TheSema);
  104. }
  105. IntrusiveRefCntPtr<ASTReader> CompilerInstance::getModuleManager() const {
  106. return ModuleManager;
  107. }
  108. void CompilerInstance::setModuleManager(IntrusiveRefCntPtr<ASTReader> Reader) {
  109. ModuleManager = Reader;
  110. }
  111. std::shared_ptr<ModuleDependencyCollector>
  112. CompilerInstance::getModuleDepCollector() const {
  113. return ModuleDepCollector;
  114. }
  115. void CompilerInstance::setModuleDepCollector(
  116. std::shared_ptr<ModuleDependencyCollector> Collector) {
  117. ModuleDepCollector = Collector;
  118. }
  119. // Diagnostics
  120. static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts,
  121. const CodeGenOptions *CodeGenOpts,
  122. DiagnosticsEngine &Diags) {
  123. std::error_code EC;
  124. std::unique_ptr<raw_ostream> StreamOwner;
  125. raw_ostream *OS = &llvm::errs();
  126. if (DiagOpts->DiagnosticLogFile != "-") {
  127. // Create the output stream.
  128. auto FileOS = llvm::make_unique<llvm::raw_fd_ostream>(
  129. DiagOpts->DiagnosticLogFile, EC,
  130. llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
  131. if (EC) {
  132. Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
  133. << DiagOpts->DiagnosticLogFile << EC.message();
  134. } else {
  135. FileOS->SetUnbuffered();
  136. FileOS->SetUseAtomicWrites(true);
  137. OS = FileOS.get();
  138. StreamOwner = std::move(FileOS);
  139. }
  140. }
  141. // Chain in the diagnostic client which will log the diagnostics.
  142. auto Logger = llvm::make_unique<LogDiagnosticPrinter>(*OS, DiagOpts,
  143. std::move(StreamOwner));
  144. if (CodeGenOpts)
  145. Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
  146. assert(Diags.ownsClient());
  147. Diags.setClient(
  148. new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger)));
  149. }
  150. static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts,
  151. DiagnosticsEngine &Diags,
  152. StringRef OutputFile) {
  153. auto SerializedConsumer =
  154. clang::serialized_diags::create(OutputFile, DiagOpts);
  155. if (Diags.ownsClient()) {
  156. Diags.setClient(new ChainedDiagnosticConsumer(
  157. Diags.takeClient(), std::move(SerializedConsumer)));
  158. } else {
  159. Diags.setClient(new ChainedDiagnosticConsumer(
  160. Diags.getClient(), std::move(SerializedConsumer)));
  161. }
  162. }
  163. void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client,
  164. bool ShouldOwnClient) {
  165. Diagnostics = createDiagnostics(&getDiagnosticOpts(), Client,
  166. ShouldOwnClient, &getCodeGenOpts());
  167. }
  168. IntrusiveRefCntPtr<DiagnosticsEngine>
  169. CompilerInstance::createDiagnostics(DiagnosticOptions *Opts,
  170. DiagnosticConsumer *Client,
  171. bool ShouldOwnClient,
  172. const CodeGenOptions *CodeGenOpts) {
  173. IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
  174. IntrusiveRefCntPtr<DiagnosticsEngine>
  175. Diags(new DiagnosticsEngine(DiagID, Opts));
  176. // Create the diagnostic client for reporting errors or for
  177. // implementing -verify.
  178. if (Client) {
  179. Diags->setClient(Client, ShouldOwnClient);
  180. } else
  181. Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
  182. // Chain in -verify checker, if requested.
  183. if (Opts->VerifyDiagnostics)
  184. Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
  185. // Chain in -diagnostic-log-file dumper, if requested.
  186. if (!Opts->DiagnosticLogFile.empty())
  187. SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
  188. if (!Opts->DiagnosticSerializationFile.empty())
  189. SetupSerializedDiagnostics(Opts, *Diags,
  190. Opts->DiagnosticSerializationFile);
  191. // Configure our handling of diagnostics.
  192. ProcessWarningOptions(*Diags, *Opts);
  193. return Diags;
  194. }
  195. // File Manager
  196. void CompilerInstance::createFileManager() {
  197. if (!hasVirtualFileSystem()) {
  198. // TODO: choose the virtual file system based on the CompilerInvocation.
  199. setVirtualFileSystem(vfs::getRealFileSystem());
  200. }
  201. FileMgr = new FileManager(getFileSystemOpts(), VirtualFileSystem);
  202. }
  203. // Source Manager
  204. void CompilerInstance::createSourceManager(FileManager &FileMgr) {
  205. SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
  206. }
  207. // Initialize the remapping of files to alternative contents, e.g.,
  208. // those specified through other files.
  209. static void InitializeFileRemapping(DiagnosticsEngine &Diags,
  210. SourceManager &SourceMgr,
  211. FileManager &FileMgr,
  212. const PreprocessorOptions &InitOpts) {
  213. // Remap files in the source manager (with buffers).
  214. for (const auto &RB : InitOpts.RemappedFileBuffers) {
  215. // Create the file entry for the file that we're mapping from.
  216. const FileEntry *FromFile =
  217. FileMgr.getVirtualFile(RB.first, RB.second->getBufferSize(), 0);
  218. if (!FromFile) {
  219. Diags.Report(diag::err_fe_remap_missing_from_file) << RB.first;
  220. if (!InitOpts.RetainRemappedFileBuffers)
  221. delete RB.second;
  222. continue;
  223. }
  224. // Override the contents of the "from" file with the contents of
  225. // the "to" file.
  226. SourceMgr.overrideFileContents(FromFile, RB.second,
  227. InitOpts.RetainRemappedFileBuffers);
  228. }
  229. // Remap files in the source manager (with other files).
  230. for (const auto &RF : InitOpts.RemappedFiles) {
  231. // Find the file that we're mapping to.
  232. const FileEntry *ToFile = FileMgr.getFile(RF.second);
  233. if (!ToFile) {
  234. Diags.Report(diag::err_fe_remap_missing_to_file) << RF.first << RF.second;
  235. continue;
  236. }
  237. // Create the file entry for the file that we're mapping from.
  238. const FileEntry *FromFile =
  239. FileMgr.getVirtualFile(RF.first, ToFile->getSize(), 0);
  240. if (!FromFile) {
  241. Diags.Report(diag::err_fe_remap_missing_from_file) << RF.first;
  242. continue;
  243. }
  244. // Override the contents of the "from" file with the contents of
  245. // the "to" file.
  246. SourceMgr.overrideFileContents(FromFile, ToFile);
  247. }
  248. SourceMgr.setOverridenFilesKeepOriginalName(
  249. InitOpts.RemappedFilesKeepOriginalName);
  250. }
  251. // Preprocessor
  252. void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {
  253. const PreprocessorOptions &PPOpts = getPreprocessorOpts();
  254. // Create a PTH manager if we are using some form of a token cache.
  255. PTHManager *PTHMgr = nullptr;
  256. if (!PPOpts.TokenCache.empty())
  257. PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics());
  258. // Create the Preprocessor.
  259. HeaderSearch *HeaderInfo = new HeaderSearch(&getHeaderSearchOpts(),
  260. getSourceManager(),
  261. getDiagnostics(),
  262. getLangOpts(),
  263. &getTarget());
  264. PP = new Preprocessor(&getPreprocessorOpts(), getDiagnostics(), getLangOpts(),
  265. getSourceManager(), *HeaderInfo, *this, PTHMgr,
  266. /*OwnsHeaderSearch=*/true, TUKind);
  267. PP->Initialize(getTarget(), getAuxTarget());
  268. // Note that this is different then passing PTHMgr to Preprocessor's ctor.
  269. // That argument is used as the IdentifierInfoLookup argument to
  270. // IdentifierTable's ctor.
  271. if (PTHMgr) {
  272. PTHMgr->setPreprocessor(&*PP);
  273. PP->setPTHManager(PTHMgr);
  274. }
  275. if (PPOpts.DetailedRecord)
  276. PP->createPreprocessingRecord();
  277. // Apply remappings to the source manager.
  278. InitializeFileRemapping(PP->getDiagnostics(), PP->getSourceManager(),
  279. PP->getFileManager(), PPOpts);
  280. // Predefine macros and configure the preprocessor.
  281. InitializePreprocessor(*PP, PPOpts, getPCHContainerReader(),
  282. getFrontendOpts());
  283. // Initialize the header search object.
  284. ApplyHeaderSearchOptions(PP->getHeaderSearchInfo(), getHeaderSearchOpts(),
  285. PP->getLangOpts(), PP->getTargetInfo().getTriple());
  286. PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP);
  287. if (PP->getLangOpts().Modules && PP->getLangOpts().ImplicitModules)
  288. PP->getHeaderSearchInfo().setModuleCachePath(getSpecificModuleCachePath());
  289. // Handle generating dependencies, if requested.
  290. const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();
  291. if (!DepOpts.OutputFile.empty())
  292. TheDependencyFileGenerator.reset(
  293. DependencyFileGenerator::CreateAndAttachToPreprocessor(*PP, DepOpts));
  294. if (!DepOpts.DOTOutputFile.empty())
  295. AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile,
  296. getHeaderSearchOpts().Sysroot);
  297. for (auto &Listener : DependencyCollectors)
  298. Listener->attachToPreprocessor(*PP);
  299. // If we don't have a collector, but we are collecting module dependencies,
  300. // then we're the top level compiler instance and need to create one.
  301. if (!ModuleDepCollector && !DepOpts.ModuleDependencyOutputDir.empty())
  302. ModuleDepCollector = std::make_shared<ModuleDependencyCollector>(
  303. DepOpts.ModuleDependencyOutputDir);
  304. // Handle generating header include information, if requested.
  305. if (DepOpts.ShowHeaderIncludes)
  306. AttachHeaderIncludeGen(*PP, DepOpts.ExtraDeps);
  307. if (!DepOpts.HeaderIncludeOutputFile.empty()) {
  308. StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
  309. if (OutputPath == "-")
  310. OutputPath = "";
  311. AttachHeaderIncludeGen(*PP, DepOpts.ExtraDeps,
  312. /*ShowAllHeaders=*/true, OutputPath,
  313. /*ShowDepth=*/false);
  314. }
  315. if (DepOpts.PrintShowIncludes) {
  316. AttachHeaderIncludeGen(*PP, DepOpts.ExtraDeps,
  317. /*ShowAllHeaders=*/false, /*OutputPath=*/"",
  318. /*ShowDepth=*/true, /*MSStyle=*/true);
  319. }
  320. }
  321. std::string CompilerInstance::getSpecificModuleCachePath() {
  322. // Set up the module path, including the hash for the
  323. // module-creation options.
  324. SmallString<256> SpecificModuleCache(getHeaderSearchOpts().ModuleCachePath);
  325. if (!SpecificModuleCache.empty() && !getHeaderSearchOpts().DisableModuleHash)
  326. llvm::sys::path::append(SpecificModuleCache,
  327. getInvocation().getModuleHash());
  328. return SpecificModuleCache.str();
  329. }
  330. // ASTContext
  331. void CompilerInstance::createASTContext() {
  332. Preprocessor &PP = getPreprocessor();
  333. auto *Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
  334. PP.getIdentifierTable(), PP.getSelectorTable(),
  335. PP.getBuiltinInfo());
  336. Context->InitBuiltinTypes(getTarget(), getAuxTarget());
  337. setASTContext(Context);
  338. }
  339. // ExternalASTSource
  340. void CompilerInstance::createPCHExternalASTSource(
  341. StringRef Path, bool DisablePCHValidation, bool AllowPCHWithCompilerErrors,
  342. void *DeserializationListener, bool OwnDeserializationListener) {
  343. bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
  344. ModuleManager = createPCHExternalASTSource(
  345. Path, getHeaderSearchOpts().Sysroot, DisablePCHValidation,
  346. AllowPCHWithCompilerErrors, getPreprocessor(), getASTContext(),
  347. getPCHContainerReader(), DeserializationListener,
  348. OwnDeserializationListener, Preamble,
  349. getFrontendOpts().UseGlobalModuleIndex);
  350. }
  351. IntrusiveRefCntPtr<ASTReader> CompilerInstance::createPCHExternalASTSource(
  352. StringRef Path, StringRef Sysroot, bool DisablePCHValidation,
  353. bool AllowPCHWithCompilerErrors, Preprocessor &PP, ASTContext &Context,
  354. const PCHContainerReader &PCHContainerRdr,
  355. void *DeserializationListener, bool OwnDeserializationListener,
  356. bool Preamble, bool UseGlobalModuleIndex) {
  357. HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
  358. IntrusiveRefCntPtr<ASTReader> Reader(new ASTReader(
  359. PP, Context, PCHContainerRdr, Sysroot.empty() ? "" : Sysroot.data(),
  360. DisablePCHValidation, AllowPCHWithCompilerErrors,
  361. /*AllowConfigurationMismatch*/ false, HSOpts.ModulesValidateSystemHeaders,
  362. UseGlobalModuleIndex));
  363. // We need the external source to be set up before we read the AST, because
  364. // eagerly-deserialized declarations may use it.
  365. Context.setExternalSource(Reader.get());
  366. Reader->setDeserializationListener(
  367. static_cast<ASTDeserializationListener *>(DeserializationListener),
  368. /*TakeOwnership=*/OwnDeserializationListener);
  369. switch (Reader->ReadAST(Path,
  370. Preamble ? serialization::MK_Preamble
  371. : serialization::MK_PCH,
  372. SourceLocation(),
  373. ASTReader::ARR_None)) {
  374. case ASTReader::Success:
  375. // Set the predefines buffer as suggested by the PCH reader. Typically, the
  376. // predefines buffer will be empty.
  377. PP.setPredefines(Reader->getSuggestedPredefines());
  378. return Reader;
  379. case ASTReader::Failure:
  380. // Unrecoverable failure: don't even try to process the input file.
  381. break;
  382. case ASTReader::Missing:
  383. case ASTReader::OutOfDate:
  384. case ASTReader::VersionMismatch:
  385. case ASTReader::ConfigurationMismatch:
  386. case ASTReader::HadErrors:
  387. // No suitable PCH file could be found. Return an error.
  388. break;
  389. }
  390. Context.setExternalSource(nullptr);
  391. return nullptr;
  392. }
  393. // Code Completion
  394. static bool EnableCodeCompletion(Preprocessor &PP,
  395. const std::string &Filename,
  396. unsigned Line,
  397. unsigned Column) {
  398. // Tell the source manager to chop off the given file at a specific
  399. // line and column.
  400. const FileEntry *Entry = PP.getFileManager().getFile(Filename);
  401. if (!Entry) {
  402. PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
  403. << Filename;
  404. return true;
  405. }
  406. // Truncate the named file at the given line/column.
  407. PP.SetCodeCompletionPoint(Entry, Line, Column);
  408. return false;
  409. }
  410. void CompilerInstance::createCodeCompletionConsumer() {
  411. const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
  412. if (!CompletionConsumer) {
  413. setCodeCompletionConsumer(
  414. createCodeCompletionConsumer(getPreprocessor(),
  415. Loc.FileName, Loc.Line, Loc.Column,
  416. getFrontendOpts().CodeCompleteOpts,
  417. llvm::outs()));
  418. if (!CompletionConsumer)
  419. return;
  420. } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
  421. Loc.Line, Loc.Column)) {
  422. setCodeCompletionConsumer(nullptr);
  423. return;
  424. }
  425. if (CompletionConsumer->isOutputBinary() &&
  426. llvm::sys::ChangeStdoutToBinary()) {
  427. getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
  428. setCodeCompletionConsumer(nullptr);
  429. }
  430. }
  431. void CompilerInstance::createFrontendTimer() {
  432. FrontendTimerGroup.reset(new llvm::TimerGroup("Clang front-end time report"));
  433. FrontendTimer.reset(
  434. new llvm::Timer("Clang front-end timer", *FrontendTimerGroup));
  435. }
  436. CodeCompleteConsumer *
  437. CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
  438. StringRef Filename,
  439. unsigned Line,
  440. unsigned Column,
  441. const CodeCompleteOptions &Opts,
  442. raw_ostream &OS) {
  443. if (EnableCodeCompletion(PP, Filename, Line, Column))
  444. return nullptr;
  445. // Set up the creation routine for code-completion.
  446. return new PrintingCodeCompleteConsumer(Opts, OS);
  447. }
  448. void CompilerInstance::createSema(TranslationUnitKind TUKind,
  449. CodeCompleteConsumer *CompletionConsumer) {
  450. TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
  451. TUKind, CompletionConsumer));
  452. }
  453. // Output Files
  454. void CompilerInstance::addOutputFile(OutputFile &&OutFile) {
  455. assert(OutFile.OS && "Attempt to add empty stream to output list!");
  456. OutputFiles.push_back(std::move(OutFile));
  457. }
  458. void CompilerInstance::clearOutputFiles(bool EraseFiles) {
  459. for (OutputFile &OF : OutputFiles) {
  460. // Manually close the stream before we rename it.
  461. OF.OS.reset();
  462. if (!OF.TempFilename.empty()) {
  463. if (EraseFiles) {
  464. llvm::sys::fs::remove(OF.TempFilename);
  465. } else {
  466. SmallString<128> NewOutFile(OF.Filename);
  467. // If '-working-directory' was passed, the output filename should be
  468. // relative to that.
  469. FileMgr->FixupRelativePath(NewOutFile);
  470. if (std::error_code ec =
  471. llvm::sys::fs::rename(OF.TempFilename, NewOutFile)) {
  472. getDiagnostics().Report(diag::err_unable_to_rename_temp)
  473. << OF.TempFilename << OF.Filename << ec.message();
  474. llvm::sys::fs::remove(OF.TempFilename);
  475. }
  476. }
  477. } else if (!OF.Filename.empty() && EraseFiles)
  478. llvm::sys::fs::remove(OF.Filename);
  479. }
  480. OutputFiles.clear();
  481. NonSeekStream.reset();
  482. }
  483. raw_pwrite_stream *
  484. CompilerInstance::createDefaultOutputFile(bool Binary, StringRef InFile,
  485. StringRef Extension) {
  486. return createOutputFile(getFrontendOpts().OutputFile, Binary,
  487. /*RemoveFileOnSignal=*/true, InFile, Extension,
  488. /*UseTemporary=*/true);
  489. }
  490. llvm::raw_null_ostream *CompilerInstance::createNullOutputFile() {
  491. auto OS = llvm::make_unique<llvm::raw_null_ostream>();
  492. llvm::raw_null_ostream *Ret = OS.get();
  493. addOutputFile(OutputFile("", "", std::move(OS)));
  494. return Ret;
  495. }
  496. raw_pwrite_stream *
  497. CompilerInstance::createOutputFile(StringRef OutputPath, bool Binary,
  498. bool RemoveFileOnSignal, StringRef InFile,
  499. StringRef Extension, bool UseTemporary,
  500. bool CreateMissingDirectories) {
  501. std::string OutputPathName, TempPathName;
  502. std::error_code EC;
  503. std::unique_ptr<raw_pwrite_stream> OS = createOutputFile(
  504. OutputPath, EC, Binary, RemoveFileOnSignal, InFile, Extension,
  505. UseTemporary, CreateMissingDirectories, &OutputPathName, &TempPathName);
  506. if (!OS) {
  507. getDiagnostics().Report(diag::err_fe_unable_to_open_output) << OutputPath
  508. << EC.message();
  509. return nullptr;
  510. }
  511. raw_pwrite_stream *Ret = OS.get();
  512. // Add the output file -- but don't try to remove "-", since this means we are
  513. // using stdin.
  514. addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
  515. TempPathName, std::move(OS)));
  516. return Ret;
  517. }
  518. std::unique_ptr<llvm::raw_pwrite_stream> CompilerInstance::createOutputFile(
  519. StringRef OutputPath, std::error_code &Error, bool Binary,
  520. bool RemoveFileOnSignal, StringRef InFile, StringRef Extension,
  521. bool UseTemporary, bool CreateMissingDirectories,
  522. std::string *ResultPathName, std::string *TempPathName) {
  523. assert((!CreateMissingDirectories || UseTemporary) &&
  524. "CreateMissingDirectories is only allowed when using temporary files");
  525. std::string OutFile, TempFile;
  526. if (!OutputPath.empty()) {
  527. OutFile = OutputPath;
  528. } else if (InFile == "-") {
  529. OutFile = "-";
  530. } else if (!Extension.empty()) {
  531. SmallString<128> Path(InFile);
  532. llvm::sys::path::replace_extension(Path, Extension);
  533. OutFile = Path.str();
  534. } else {
  535. OutFile = "-";
  536. }
  537. std::unique_ptr<llvm::raw_fd_ostream> OS;
  538. std::string OSFile;
  539. if (UseTemporary) {
  540. if (OutFile == "-")
  541. UseTemporary = false;
  542. else {
  543. llvm::sys::fs::file_status Status;
  544. llvm::sys::fs::status(OutputPath, Status);
  545. if (llvm::sys::fs::exists(Status)) {
  546. // Fail early if we can't write to the final destination.
  547. if (!llvm::sys::fs::can_write(OutputPath)) {
  548. Error = std::make_error_code(std::errc::operation_not_permitted);
  549. return nullptr;
  550. }
  551. // Don't use a temporary if the output is a special file. This handles
  552. // things like '-o /dev/null'
  553. if (!llvm::sys::fs::is_regular_file(Status))
  554. UseTemporary = false;
  555. }
  556. }
  557. }
  558. if (UseTemporary) {
  559. // Create a temporary file.
  560. SmallString<128> TempPath;
  561. TempPath = OutFile;
  562. TempPath += "-%%%%%%%%";
  563. int fd;
  564. std::error_code EC =
  565. llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath);
  566. if (CreateMissingDirectories &&
  567. EC == llvm::errc::no_such_file_or_directory) {
  568. StringRef Parent = llvm::sys::path::parent_path(OutputPath);
  569. EC = llvm::sys::fs::create_directories(Parent);
  570. if (!EC) {
  571. EC = llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath);
  572. }
  573. }
  574. if (!EC) {
  575. OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
  576. OSFile = TempFile = TempPath.str();
  577. }
  578. // If we failed to create the temporary, fallback to writing to the file
  579. // directly. This handles the corner case where we cannot write to the
  580. // directory, but can write to the file.
  581. }
  582. if (!OS) {
  583. OSFile = OutFile;
  584. OS.reset(new llvm::raw_fd_ostream(
  585. OSFile, Error,
  586. (Binary ? llvm::sys::fs::F_None : llvm::sys::fs::F_Text)));
  587. if (Error)
  588. return nullptr;
  589. }
  590. // Make sure the out stream file gets removed if we crash.
  591. if (RemoveFileOnSignal)
  592. llvm::sys::RemoveFileOnSignal(OSFile);
  593. if (ResultPathName)
  594. *ResultPathName = OutFile;
  595. if (TempPathName)
  596. *TempPathName = TempFile;
  597. if (!Binary || OS->supportsSeeking())
  598. return std::move(OS);
  599. auto B = llvm::make_unique<llvm::buffer_ostream>(*OS);
  600. assert(!NonSeekStream);
  601. NonSeekStream = std::move(OS);
  602. return std::move(B);
  603. }
  604. // Initialization Utilities
  605. bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input){
  606. return InitializeSourceManager(Input, getDiagnostics(),
  607. getFileManager(), getSourceManager(),
  608. getFrontendOpts());
  609. }
  610. bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input,
  611. DiagnosticsEngine &Diags,
  612. FileManager &FileMgr,
  613. SourceManager &SourceMgr,
  614. const FrontendOptions &Opts) {
  615. SrcMgr::CharacteristicKind
  616. Kind = Input.isSystem() ? SrcMgr::C_System : SrcMgr::C_User;
  617. if (Input.isBuffer()) {
  618. SourceMgr.setMainFileID(SourceMgr.createFileID(
  619. std::unique_ptr<llvm::MemoryBuffer>(Input.getBuffer()), Kind));
  620. assert(!SourceMgr.getMainFileID().isInvalid() &&
  621. "Couldn't establish MainFileID!");
  622. return true;
  623. }
  624. StringRef InputFile = Input.getFile();
  625. // Figure out where to get and map in the main file.
  626. if (InputFile != "-") {
  627. const FileEntry *File = FileMgr.getFile(InputFile, /*OpenFile=*/true);
  628. if (!File) {
  629. Diags.Report(diag::err_fe_error_reading) << InputFile;
  630. return false;
  631. }
  632. // The natural SourceManager infrastructure can't currently handle named
  633. // pipes, but we would at least like to accept them for the main
  634. // file. Detect them here, read them with the volatile flag so FileMgr will
  635. // pick up the correct size, and simply override their contents as we do for
  636. // STDIN.
  637. if (File->isNamedPipe()) {
  638. auto MB = FileMgr.getBufferForFile(File, /*isVolatile=*/true);
  639. if (MB) {
  640. // Create a new virtual file that will have the correct size.
  641. File = FileMgr.getVirtualFile(InputFile, (*MB)->getBufferSize(), 0);
  642. SourceMgr.overrideFileContents(File, std::move(*MB));
  643. } else {
  644. Diags.Report(diag::err_cannot_open_file) << InputFile
  645. << MB.getError().message();
  646. return false;
  647. }
  648. }
  649. SourceMgr.setMainFileID(
  650. SourceMgr.createFileID(File, SourceLocation(), Kind));
  651. } else {
  652. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> SBOrErr =
  653. llvm::MemoryBuffer::getSTDIN();
  654. if (std::error_code EC = SBOrErr.getError()) {
  655. Diags.Report(diag::err_fe_error_reading_stdin) << EC.message();
  656. return false;
  657. }
  658. std::unique_ptr<llvm::MemoryBuffer> SB = std::move(SBOrErr.get());
  659. const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
  660. SB->getBufferSize(), 0);
  661. SourceMgr.setMainFileID(
  662. SourceMgr.createFileID(File, SourceLocation(), Kind));
  663. SourceMgr.overrideFileContents(File, std::move(SB));
  664. }
  665. assert(!SourceMgr.getMainFileID().isInvalid() &&
  666. "Couldn't establish MainFileID!");
  667. return true;
  668. }
  669. // High-Level Operations
  670. bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
  671. assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
  672. assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
  673. assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
  674. // FIXME: Take this as an argument, once all the APIs we used have moved to
  675. // taking it as an input instead of hard-coding llvm::errs.
  676. raw_ostream &OS = llvm::errs();
  677. // Create the target instance.
  678. setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(),
  679. getInvocation().TargetOpts));
  680. if (!hasTarget())
  681. return false;
  682. // Create TargetInfo for the other side of CUDA compilation.
  683. if (getLangOpts().CUDA && !getFrontendOpts().AuxTriple.empty()) {
  684. std::shared_ptr<TargetOptions> TO(new TargetOptions);
  685. TO->Triple = getFrontendOpts().AuxTriple;
  686. setAuxTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), TO));
  687. }
  688. // Inform the target of the language options.
  689. //
  690. // FIXME: We shouldn't need to do this, the target should be immutable once
  691. // created. This complexity should be lifted elsewhere.
  692. getTarget().adjust(getLangOpts());
  693. // rewriter project will change target built-in bool type from its default.
  694. if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
  695. getTarget().noSignedCharForObjCBool();
  696. // Validate/process some options.
  697. if (getHeaderSearchOpts().Verbose)
  698. OS << "clang -cc1 version " CLANG_VERSION_STRING
  699. << " based upon " << BACKEND_PACKAGE_STRING
  700. << " default target " << llvm::sys::getDefaultTargetTriple() << "\n";
  701. if (getFrontendOpts().ShowTimers)
  702. createFrontendTimer();
  703. if (getFrontendOpts().ShowStats)
  704. llvm::EnableStatistics();
  705. for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
  706. // Reset the ID tables if we are reusing the SourceManager and parsing
  707. // regular files.
  708. if (hasSourceManager() && !Act.isModelParsingAction())
  709. getSourceManager().clearIDTables();
  710. if (Act.BeginSourceFile(*this, getFrontendOpts().Inputs[i])) {
  711. Act.Execute();
  712. Act.EndSourceFile();
  713. }
  714. }
  715. // Notify the diagnostic client that all files were processed.
  716. getDiagnostics().getClient()->finish();
  717. if (getDiagnosticOpts().ShowCarets) {
  718. // We can have multiple diagnostics sharing one diagnostic client.
  719. // Get the total number of warnings/errors from the client.
  720. unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
  721. unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
  722. if (NumWarnings)
  723. OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
  724. if (NumWarnings && NumErrors)
  725. OS << " and ";
  726. if (NumErrors)
  727. OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
  728. if (NumWarnings || NumErrors)
  729. OS << " generated.\n";
  730. }
  731. if (getFrontendOpts().ShowStats && hasFileManager()) {
  732. getFileManager().PrintStats();
  733. OS << "\n";
  734. }
  735. return !getDiagnostics().getClient()->getNumErrors();
  736. }
  737. /// \brief Determine the appropriate source input kind based on language
  738. /// options.
  739. static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) {
  740. if (LangOpts.OpenCL)
  741. return IK_OpenCL;
  742. if (LangOpts.CUDA)
  743. return IK_CUDA;
  744. if (LangOpts.ObjC1)
  745. return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC;
  746. return LangOpts.CPlusPlus? IK_CXX : IK_C;
  747. }
  748. /// \brief Compile a module file for the given module, using the options
  749. /// provided by the importing compiler instance. Returns true if the module
  750. /// was built without errors.
  751. static bool compileModuleImpl(CompilerInstance &ImportingInstance,
  752. SourceLocation ImportLoc,
  753. Module *Module,
  754. StringRef ModuleFileName) {
  755. ModuleMap &ModMap
  756. = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap();
  757. // Construct a compiler invocation for creating this module.
  758. IntrusiveRefCntPtr<CompilerInvocation> Invocation
  759. (new CompilerInvocation(ImportingInstance.getInvocation()));
  760. PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
  761. // For any options that aren't intended to affect how a module is built,
  762. // reset them to their default values.
  763. Invocation->getLangOpts()->resetNonModularOptions();
  764. PPOpts.resetNonModularOptions();
  765. // Remove any macro definitions that are explicitly ignored by the module.
  766. // They aren't supposed to affect how the module is built anyway.
  767. const HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts();
  768. PPOpts.Macros.erase(
  769. std::remove_if(PPOpts.Macros.begin(), PPOpts.Macros.end(),
  770. [&HSOpts](const std::pair<std::string, bool> &def) {
  771. StringRef MacroDef = def.first;
  772. return HSOpts.ModulesIgnoreMacros.count(MacroDef.split('=').first) > 0;
  773. }),
  774. PPOpts.Macros.end());
  775. // Note the name of the module we're building.
  776. Invocation->getLangOpts()->CurrentModule = Module->getTopLevelModuleName();
  777. // Make sure that the failed-module structure has been allocated in
  778. // the importing instance, and propagate the pointer to the newly-created
  779. // instance.
  780. PreprocessorOptions &ImportingPPOpts
  781. = ImportingInstance.getInvocation().getPreprocessorOpts();
  782. if (!ImportingPPOpts.FailedModules)
  783. ImportingPPOpts.FailedModules = new PreprocessorOptions::FailedModulesSet;
  784. PPOpts.FailedModules = ImportingPPOpts.FailedModules;
  785. // If there is a module map file, build the module using the module map.
  786. // Set up the inputs/outputs so that we build the module from its umbrella
  787. // header.
  788. FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
  789. FrontendOpts.OutputFile = ModuleFileName.str();
  790. FrontendOpts.DisableFree = false;
  791. FrontendOpts.GenerateGlobalModuleIndex = false;
  792. FrontendOpts.BuildingImplicitModule = true;
  793. FrontendOpts.Inputs.clear();
  794. InputKind IK = getSourceInputKindFromOptions(*Invocation->getLangOpts());
  795. // Don't free the remapped file buffers; they are owned by our caller.
  796. PPOpts.RetainRemappedFileBuffers = true;
  797. Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
  798. assert(ImportingInstance.getInvocation().getModuleHash() ==
  799. Invocation->getModuleHash() && "Module hash mismatch!");
  800. // Construct a compiler instance that will be used to actually create the
  801. // module.
  802. CompilerInstance Instance(ImportingInstance.getPCHContainerOperations(),
  803. /*BuildingModule=*/true);
  804. Instance.setInvocation(&*Invocation);
  805. Instance.createDiagnostics(new ForwardingDiagnosticConsumer(
  806. ImportingInstance.getDiagnosticClient()),
  807. /*ShouldOwnClient=*/true);
  808. Instance.setVirtualFileSystem(&ImportingInstance.getVirtualFileSystem());
  809. // Note that this module is part of the module build stack, so that we
  810. // can detect cycles in the module graph.
  811. Instance.setFileManager(&ImportingInstance.getFileManager());
  812. Instance.createSourceManager(Instance.getFileManager());
  813. SourceManager &SourceMgr = Instance.getSourceManager();
  814. SourceMgr.setModuleBuildStack(
  815. ImportingInstance.getSourceManager().getModuleBuildStack());
  816. SourceMgr.pushModuleBuildStack(Module->getTopLevelModuleName(),
  817. FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager()));
  818. // If we're collecting module dependencies, we need to share a collector
  819. // between all of the module CompilerInstances. Other than that, we don't
  820. // want to produce any dependency output from the module build.
  821. Instance.setModuleDepCollector(ImportingInstance.getModuleDepCollector());
  822. Invocation->getDependencyOutputOpts() = DependencyOutputOptions();
  823. // Get or create the module map that we'll use to build this module.
  824. std::string InferredModuleMapContent;
  825. if (const FileEntry *ModuleMapFile =
  826. ModMap.getContainingModuleMapFile(Module)) {
  827. // Use the module map where this module resides.
  828. FrontendOpts.Inputs.emplace_back(ModuleMapFile->getName(), IK);
  829. } else {
  830. SmallString<128> FakeModuleMapFile(Module->Directory->getName());
  831. llvm::sys::path::append(FakeModuleMapFile, "__inferred_module.map");
  832. FrontendOpts.Inputs.emplace_back(FakeModuleMapFile, IK);
  833. llvm::raw_string_ostream OS(InferredModuleMapContent);
  834. Module->print(OS);
  835. OS.flush();
  836. std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer =
  837. llvm::MemoryBuffer::getMemBuffer(InferredModuleMapContent);
  838. ModuleMapFile = Instance.getFileManager().getVirtualFile(
  839. FakeModuleMapFile, InferredModuleMapContent.size(), 0);
  840. SourceMgr.overrideFileContents(ModuleMapFile, std::move(ModuleMapBuffer));
  841. }
  842. // Construct a module-generating action. Passing through the module map is
  843. // safe because the FileManager is shared between the compiler instances.
  844. GenerateModuleAction CreateModuleAction(
  845. ModMap.getModuleMapFileForUniquing(Module), Module->IsSystem);
  846. ImportingInstance.getDiagnostics().Report(ImportLoc,
  847. diag::remark_module_build)
  848. << Module->Name << ModuleFileName;
  849. // Execute the action to actually build the module in-place. Use a separate
  850. // thread so that we get a stack large enough.
  851. const unsigned ThreadStackSize = 8 << 20;
  852. llvm::CrashRecoveryContext CRC;
  853. CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(CreateModuleAction); },
  854. ThreadStackSize);
  855. ImportingInstance.getDiagnostics().Report(ImportLoc,
  856. diag::remark_module_build_done)
  857. << Module->Name;
  858. // Delete the temporary module map file.
  859. // FIXME: Even though we're executing under crash protection, it would still
  860. // be nice to do this with RemoveFileOnSignal when we can. However, that
  861. // doesn't make sense for all clients, so clean this up manually.
  862. Instance.clearOutputFiles(/*EraseFiles=*/true);
  863. // We've rebuilt a module. If we're allowed to generate or update the global
  864. // module index, record that fact in the importing compiler instance.
  865. if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) {
  866. ImportingInstance.setBuildGlobalModuleIndex(true);
  867. }
  868. return !Instance.getDiagnostics().hasErrorOccurred();
  869. }
  870. static bool compileAndLoadModule(CompilerInstance &ImportingInstance,
  871. SourceLocation ImportLoc,
  872. SourceLocation ModuleNameLoc, Module *Module,
  873. StringRef ModuleFileName) {
  874. DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
  875. auto diagnoseBuildFailure = [&] {
  876. Diags.Report(ModuleNameLoc, diag::err_module_not_built)
  877. << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
  878. };
  879. // FIXME: have LockFileManager return an error_code so that we can
  880. // avoid the mkdir when the directory already exists.
  881. StringRef Dir = llvm::sys::path::parent_path(ModuleFileName);
  882. llvm::sys::fs::create_directories(Dir);
  883. while (1) {
  884. unsigned ModuleLoadCapabilities = ASTReader::ARR_Missing;
  885. llvm::LockFileManager Locked(ModuleFileName);
  886. switch (Locked) {
  887. case llvm::LockFileManager::LFS_Error:
  888. Diags.Report(ModuleNameLoc, diag::err_module_lock_failure)
  889. << Module->Name;
  890. return false;
  891. case llvm::LockFileManager::LFS_Owned:
  892. // We're responsible for building the module ourselves.
  893. if (!compileModuleImpl(ImportingInstance, ModuleNameLoc, Module,
  894. ModuleFileName)) {
  895. diagnoseBuildFailure();
  896. return false;
  897. }
  898. break;
  899. case llvm::LockFileManager::LFS_Shared:
  900. // Someone else is responsible for building the module. Wait for them to
  901. // finish.
  902. switch (Locked.waitForUnlock()) {
  903. case llvm::LockFileManager::Res_Success:
  904. ModuleLoadCapabilities |= ASTReader::ARR_OutOfDate;
  905. break;
  906. case llvm::LockFileManager::Res_OwnerDied:
  907. continue; // try again to get the lock.
  908. case llvm::LockFileManager::Res_Timeout:
  909. Diags.Report(ModuleNameLoc, diag::err_module_lock_timeout)
  910. << Module->Name;
  911. // Clear the lock file so that future invokations can make progress.
  912. Locked.unsafeRemoveLockFile();
  913. return false;
  914. }
  915. break;
  916. }
  917. // Try to read the module file, now that we've compiled it.
  918. ASTReader::ASTReadResult ReadResult =
  919. ImportingInstance.getModuleManager()->ReadAST(
  920. ModuleFileName, serialization::MK_ImplicitModule, ImportLoc,
  921. ModuleLoadCapabilities);
  922. if (ReadResult == ASTReader::OutOfDate &&
  923. Locked == llvm::LockFileManager::LFS_Shared) {
  924. // The module may be out of date in the presence of file system races,
  925. // or if one of its imports depends on header search paths that are not
  926. // consistent with this ImportingInstance. Try again...
  927. continue;
  928. } else if (ReadResult == ASTReader::Missing) {
  929. diagnoseBuildFailure();
  930. } else if (ReadResult != ASTReader::Success &&
  931. !Diags.hasErrorOccurred()) {
  932. // The ASTReader didn't diagnose the error, so conservatively report it.
  933. diagnoseBuildFailure();
  934. }
  935. return ReadResult == ASTReader::Success;
  936. }
  937. }
  938. /// \brief Diagnose differences between the current definition of the given
  939. /// configuration macro and the definition provided on the command line.
  940. static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
  941. Module *Mod, SourceLocation ImportLoc) {
  942. IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro);
  943. SourceManager &SourceMgr = PP.getSourceManager();
  944. // If this identifier has never had a macro definition, then it could
  945. // not have changed.
  946. if (!Id->hadMacroDefinition())
  947. return;
  948. auto *LatestLocalMD = PP.getLocalMacroDirectiveHistory(Id);
  949. // Find the macro definition from the command line.
  950. MacroInfo *CmdLineDefinition = nullptr;
  951. for (auto *MD = LatestLocalMD; MD; MD = MD->getPrevious()) {
  952. // We only care about the predefines buffer.
  953. FileID FID = SourceMgr.getFileID(MD->getLocation());
  954. if (FID.isInvalid() || FID != PP.getPredefinesFileID())
  955. continue;
  956. if (auto *DMD = dyn_cast<DefMacroDirective>(MD))
  957. CmdLineDefinition = DMD->getMacroInfo();
  958. break;
  959. }
  960. auto *CurrentDefinition = PP.getMacroInfo(Id);
  961. if (CurrentDefinition == CmdLineDefinition) {
  962. // Macro matches. Nothing to do.
  963. } else if (!CurrentDefinition) {
  964. // This macro was defined on the command line, then #undef'd later.
  965. // Complain.
  966. PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
  967. << true << ConfigMacro << Mod->getFullModuleName();
  968. auto LatestDef = LatestLocalMD->getDefinition();
  969. assert(LatestDef.isUndefined() &&
  970. "predefined macro went away with no #undef?");
  971. PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here)
  972. << true;
  973. return;
  974. } else if (!CmdLineDefinition) {
  975. // There was no definition for this macro in the predefines buffer,
  976. // but there was a local definition. Complain.
  977. PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
  978. << false << ConfigMacro << Mod->getFullModuleName();
  979. PP.Diag(CurrentDefinition->getDefinitionLoc(),
  980. diag::note_module_def_undef_here)
  981. << false;
  982. } else if (!CurrentDefinition->isIdenticalTo(*CmdLineDefinition, PP,
  983. /*Syntactically=*/true)) {
  984. // The macro definitions differ.
  985. PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
  986. << false << ConfigMacro << Mod->getFullModuleName();
  987. PP.Diag(CurrentDefinition->getDefinitionLoc(),
  988. diag::note_module_def_undef_here)
  989. << false;
  990. }
  991. }
  992. /// \brief Write a new timestamp file with the given path.
  993. static void writeTimestampFile(StringRef TimestampFile) {
  994. std::error_code EC;
  995. llvm::raw_fd_ostream Out(TimestampFile.str(), EC, llvm::sys::fs::F_None);
  996. }
  997. /// \brief Prune the module cache of modules that haven't been accessed in
  998. /// a long time.
  999. static void pruneModuleCache(const HeaderSearchOptions &HSOpts) {
  1000. struct stat StatBuf;
  1001. llvm::SmallString<128> TimestampFile;
  1002. TimestampFile = HSOpts.ModuleCachePath;
  1003. assert(!TimestampFile.empty());
  1004. llvm::sys::path::append(TimestampFile, "modules.timestamp");
  1005. // Try to stat() the timestamp file.
  1006. if (::stat(TimestampFile.c_str(), &StatBuf)) {
  1007. // If the timestamp file wasn't there, create one now.
  1008. if (errno == ENOENT) {
  1009. writeTimestampFile(TimestampFile);
  1010. }
  1011. return;
  1012. }
  1013. // Check whether the time stamp is older than our pruning interval.
  1014. // If not, do nothing.
  1015. time_t TimeStampModTime = StatBuf.st_mtime;
  1016. time_t CurrentTime = time(nullptr);
  1017. if (CurrentTime - TimeStampModTime <= time_t(HSOpts.ModuleCachePruneInterval))
  1018. return;
  1019. // Write a new timestamp file so that nobody else attempts to prune.
  1020. // There is a benign race condition here, if two Clang instances happen to
  1021. // notice at the same time that the timestamp is out-of-date.
  1022. writeTimestampFile(TimestampFile);
  1023. // Walk the entire module cache, looking for unused module files and module
  1024. // indices.
  1025. std::error_code EC;
  1026. SmallString<128> ModuleCachePathNative;
  1027. llvm::sys::path::native(HSOpts.ModuleCachePath, ModuleCachePathNative);
  1028. for (llvm::sys::fs::directory_iterator Dir(ModuleCachePathNative, EC), DirEnd;
  1029. Dir != DirEnd && !EC; Dir.increment(EC)) {
  1030. // If we don't have a directory, there's nothing to look into.
  1031. if (!llvm::sys::fs::is_directory(Dir->path()))
  1032. continue;
  1033. // Walk all of the files within this directory.
  1034. for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd;
  1035. File != FileEnd && !EC; File.increment(EC)) {
  1036. // We only care about module and global module index files.
  1037. StringRef Extension = llvm::sys::path::extension(File->path());
  1038. if (Extension != ".pcm" && Extension != ".timestamp" &&
  1039. llvm::sys::path::filename(File->path()) != "modules.idx")
  1040. continue;
  1041. // Look at this file. If we can't stat it, there's nothing interesting
  1042. // there.
  1043. if (::stat(File->path().c_str(), &StatBuf))
  1044. continue;
  1045. // If the file has been used recently enough, leave it there.
  1046. time_t FileAccessTime = StatBuf.st_atime;
  1047. if (CurrentTime - FileAccessTime <=
  1048. time_t(HSOpts.ModuleCachePruneAfter)) {
  1049. continue;
  1050. }
  1051. // Remove the file.
  1052. llvm::sys::fs::remove(File->path());
  1053. // Remove the timestamp file.
  1054. std::string TimpestampFilename = File->path() + ".timestamp";
  1055. llvm::sys::fs::remove(TimpestampFilename);
  1056. }
  1057. // If we removed all of the files in the directory, remove the directory
  1058. // itself.
  1059. if (llvm::sys::fs::directory_iterator(Dir->path(), EC) ==
  1060. llvm::sys::fs::directory_iterator() && !EC)
  1061. llvm::sys::fs::remove(Dir->path());
  1062. }
  1063. }
  1064. void CompilerInstance::createModuleManager() {
  1065. if (!ModuleManager) {
  1066. if (!hasASTContext())
  1067. createASTContext();
  1068. // If we're implicitly building modules but not currently recursively
  1069. // building a module, check whether we need to prune the module cache.
  1070. if (getSourceManager().getModuleBuildStack().empty() &&
  1071. !getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty() &&
  1072. getHeaderSearchOpts().ModuleCachePruneInterval > 0 &&
  1073. getHeaderSearchOpts().ModuleCachePruneAfter > 0) {
  1074. pruneModuleCache(getHeaderSearchOpts());
  1075. }
  1076. HeaderSearchOptions &HSOpts = getHeaderSearchOpts();
  1077. std::string Sysroot = HSOpts.Sysroot;
  1078. const PreprocessorOptions &PPOpts = getPreprocessorOpts();
  1079. std::unique_ptr<llvm::Timer> ReadTimer;
  1080. if (FrontendTimerGroup)
  1081. ReadTimer = llvm::make_unique<llvm::Timer>("Reading modules",
  1082. *FrontendTimerGroup);
  1083. ModuleManager = new ASTReader(
  1084. getPreprocessor(), getASTContext(), getPCHContainerReader(),
  1085. Sysroot.empty() ? "" : Sysroot.c_str(), PPOpts.DisablePCHValidation,
  1086. /*AllowASTWithCompilerErrors=*/false,
  1087. /*AllowConfigurationMismatch=*/false,
  1088. HSOpts.ModulesValidateSystemHeaders,
  1089. getFrontendOpts().UseGlobalModuleIndex,
  1090. std::move(ReadTimer));
  1091. if (hasASTConsumer()) {
  1092. ModuleManager->setDeserializationListener(
  1093. getASTConsumer().GetASTDeserializationListener());
  1094. getASTContext().setASTMutationListener(
  1095. getASTConsumer().GetASTMutationListener());
  1096. }
  1097. getASTContext().setExternalSource(ModuleManager);
  1098. if (hasSema())
  1099. ModuleManager->InitializeSema(getSema());
  1100. if (hasASTConsumer())
  1101. ModuleManager->StartTranslationUnit(&getASTConsumer());
  1102. if (TheDependencyFileGenerator)
  1103. TheDependencyFileGenerator->AttachToASTReader(*ModuleManager);
  1104. if (ModuleDepCollector)
  1105. ModuleDepCollector->attachToASTReader(*ModuleManager);
  1106. for (auto &Listener : DependencyCollectors)
  1107. Listener->attachToASTReader(*ModuleManager);
  1108. }
  1109. }
  1110. bool CompilerInstance::loadModuleFile(StringRef FileName) {
  1111. llvm::Timer Timer;
  1112. if (FrontendTimerGroup)
  1113. Timer.init("Preloading " + FileName.str(), *FrontendTimerGroup);
  1114. llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr);
  1115. // Helper to recursively read the module names for all modules we're adding.
  1116. // We mark these as known and redirect any attempt to load that module to
  1117. // the files we were handed.
  1118. struct ReadModuleNames : ASTReaderListener {
  1119. CompilerInstance &CI;
  1120. llvm::SmallVector<IdentifierInfo*, 8> LoadedModules;
  1121. ReadModuleNames(CompilerInstance &CI) : CI(CI) {}
  1122. void ReadModuleName(StringRef ModuleName) override {
  1123. LoadedModules.push_back(
  1124. CI.getPreprocessor().getIdentifierInfo(ModuleName));
  1125. }
  1126. void registerAll() {
  1127. for (auto *II : LoadedModules) {
  1128. CI.KnownModules[II] = CI.getPreprocessor()
  1129. .getHeaderSearchInfo()
  1130. .getModuleMap()
  1131. .findModule(II->getName());
  1132. }
  1133. LoadedModules.clear();
  1134. }
  1135. };
  1136. // If we don't already have an ASTReader, create one now.
  1137. if (!ModuleManager)
  1138. createModuleManager();
  1139. auto Listener = llvm::make_unique<ReadModuleNames>(*this);
  1140. auto &ListenerRef = *Listener;
  1141. ASTReader::ListenerScope ReadModuleNamesListener(*ModuleManager,
  1142. std::move(Listener));
  1143. // Try to load the module file.
  1144. if (ModuleManager->ReadAST(FileName, serialization::MK_ExplicitModule,
  1145. SourceLocation(), ASTReader::ARR_None)
  1146. != ASTReader::Success)
  1147. return false;
  1148. // We successfully loaded the module file; remember the set of provided
  1149. // modules so that we don't try to load implicit modules for them.
  1150. ListenerRef.registerAll();
  1151. return true;
  1152. }
  1153. ModuleLoadResult
  1154. CompilerInstance::loadModule(SourceLocation ImportLoc,
  1155. ModuleIdPath Path,
  1156. Module::NameVisibilityKind Visibility,
  1157. bool IsInclusionDirective) {
  1158. // Determine what file we're searching from.
  1159. StringRef ModuleName = Path[0].first->getName();
  1160. SourceLocation ModuleNameLoc = Path[0].second;
  1161. // If we've already handled this import, just return the cached result.
  1162. // This one-element cache is important to eliminate redundant diagnostics
  1163. // when both the preprocessor and parser see the same import declaration.
  1164. if (!ImportLoc.isInvalid() && LastModuleImportLoc == ImportLoc) {
  1165. // Make the named module visible.
  1166. if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule &&
  1167. ModuleName != getLangOpts().ImplementationOfModule)
  1168. ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility,
  1169. ImportLoc);
  1170. return LastModuleImportResult;
  1171. }
  1172. clang::Module *Module = nullptr;
  1173. // If we don't already have information on this module, load the module now.
  1174. llvm::DenseMap<const IdentifierInfo *, clang::Module *>::iterator Known
  1175. = KnownModules.find(Path[0].first);
  1176. if (Known != KnownModules.end()) {
  1177. // Retrieve the cached top-level module.
  1178. Module = Known->second;
  1179. } else if (ModuleName == getLangOpts().CurrentModule ||
  1180. ModuleName == getLangOpts().ImplementationOfModule) {
  1181. // This is the module we're building.
  1182. Module = PP->getHeaderSearchInfo().lookupModule(ModuleName);
  1183. Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
  1184. } else {
  1185. // Search for a module with the given name.
  1186. Module = PP->getHeaderSearchInfo().lookupModule(ModuleName);
  1187. if (!Module) {
  1188. getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
  1189. << ModuleName
  1190. << SourceRange(ImportLoc, ModuleNameLoc);
  1191. ModuleBuildFailed = true;
  1192. return ModuleLoadResult();
  1193. }
  1194. std::string ModuleFileName =
  1195. PP->getHeaderSearchInfo().getModuleFileName(Module);
  1196. if (ModuleFileName.empty()) {
  1197. getDiagnostics().Report(ModuleNameLoc, diag::err_module_build_disabled)
  1198. << ModuleName;
  1199. ModuleBuildFailed = true;
  1200. return ModuleLoadResult();
  1201. }
  1202. // If we don't already have an ASTReader, create one now.
  1203. if (!ModuleManager)
  1204. createModuleManager();
  1205. llvm::Timer Timer;
  1206. if (FrontendTimerGroup)
  1207. Timer.init("Loading " + ModuleFileName, *FrontendTimerGroup);
  1208. llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr);
  1209. // Try to load the module file.
  1210. unsigned ARRFlags = ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing;
  1211. switch (ModuleManager->ReadAST(ModuleFileName,
  1212. serialization::MK_ImplicitModule,
  1213. ImportLoc, ARRFlags)) {
  1214. case ASTReader::Success:
  1215. break;
  1216. case ASTReader::OutOfDate:
  1217. case ASTReader::Missing: {
  1218. // The module file is missing or out-of-date. Build it.
  1219. assert(Module && "missing module file");
  1220. // Check whether there is a cycle in the module graph.
  1221. ModuleBuildStack ModPath = getSourceManager().getModuleBuildStack();
  1222. ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end();
  1223. for (; Pos != PosEnd; ++Pos) {
  1224. if (Pos->first == ModuleName)
  1225. break;
  1226. }
  1227. if (Pos != PosEnd) {
  1228. SmallString<256> CyclePath;
  1229. for (; Pos != PosEnd; ++Pos) {
  1230. CyclePath += Pos->first;
  1231. CyclePath += " -> ";
  1232. }
  1233. CyclePath += ModuleName;
  1234. getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
  1235. << ModuleName << CyclePath;
  1236. return ModuleLoadResult();
  1237. }
  1238. // Check whether we have already attempted to build this module (but
  1239. // failed).
  1240. if (getPreprocessorOpts().FailedModules &&
  1241. getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)) {
  1242. getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built)
  1243. << ModuleName
  1244. << SourceRange(ImportLoc, ModuleNameLoc);
  1245. ModuleBuildFailed = true;
  1246. return ModuleLoadResult();
  1247. }
  1248. // Try to compile and then load the module.
  1249. if (!compileAndLoadModule(*this, ImportLoc, ModuleNameLoc, Module,
  1250. ModuleFileName)) {
  1251. assert(getDiagnostics().hasErrorOccurred() &&
  1252. "undiagnosed error in compileAndLoadModule");
  1253. if (getPreprocessorOpts().FailedModules)
  1254. getPreprocessorOpts().FailedModules->addFailed(ModuleName);
  1255. KnownModules[Path[0].first] = nullptr;
  1256. ModuleBuildFailed = true;
  1257. return ModuleLoadResult();
  1258. }
  1259. // Okay, we've rebuilt and now loaded the module.
  1260. break;
  1261. }
  1262. case ASTReader::VersionMismatch:
  1263. case ASTReader::ConfigurationMismatch:
  1264. case ASTReader::HadErrors:
  1265. ModuleLoader::HadFatalFailure = true;
  1266. // FIXME: The ASTReader will already have complained, but can we shoehorn
  1267. // that diagnostic information into a more useful form?
  1268. KnownModules[Path[0].first] = nullptr;
  1269. return ModuleLoadResult();
  1270. case ASTReader::Failure:
  1271. ModuleLoader::HadFatalFailure = true;
  1272. // Already complained, but note now that we failed.
  1273. KnownModules[Path[0].first] = nullptr;
  1274. ModuleBuildFailed = true;
  1275. return ModuleLoadResult();
  1276. }
  1277. // Cache the result of this top-level module lookup for later.
  1278. Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
  1279. }
  1280. // If we never found the module, fail.
  1281. if (!Module)
  1282. return ModuleLoadResult();
  1283. // Verify that the rest of the module path actually corresponds to
  1284. // a submodule.
  1285. if (Path.size() > 1) {
  1286. for (unsigned I = 1, N = Path.size(); I != N; ++I) {
  1287. StringRef Name = Path[I].first->getName();
  1288. clang::Module *Sub = Module->findSubmodule(Name);
  1289. if (!Sub) {
  1290. // Attempt to perform typo correction to find a module name that works.
  1291. SmallVector<StringRef, 2> Best;
  1292. unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)();
  1293. for (clang::Module::submodule_iterator J = Module->submodule_begin(),
  1294. JEnd = Module->submodule_end();
  1295. J != JEnd; ++J) {
  1296. unsigned ED = Name.edit_distance((*J)->Name,
  1297. /*AllowReplacements=*/true,
  1298. BestEditDistance);
  1299. if (ED <= BestEditDistance) {
  1300. if (ED < BestEditDistance) {
  1301. Best.clear();
  1302. BestEditDistance = ED;
  1303. }
  1304. Best.push_back((*J)->Name);
  1305. }
  1306. }
  1307. // If there was a clear winner, user it.
  1308. if (Best.size() == 1) {
  1309. getDiagnostics().Report(Path[I].second,
  1310. diag::err_no_submodule_suggest)
  1311. << Path[I].first << Module->getFullModuleName() << Best[0]
  1312. << SourceRange(Path[0].second, Path[I-1].second)
  1313. << FixItHint::CreateReplacement(SourceRange(Path[I].second),
  1314. Best[0]);
  1315. Sub = Module->findSubmodule(Best[0]);
  1316. }
  1317. }
  1318. if (!Sub) {
  1319. // No submodule by this name. Complain, and don't look for further
  1320. // submodules.
  1321. getDiagnostics().Report(Path[I].second, diag::err_no_submodule)
  1322. << Path[I].first << Module->getFullModuleName()
  1323. << SourceRange(Path[0].second, Path[I-1].second);
  1324. break;
  1325. }
  1326. Module = Sub;
  1327. }
  1328. }
  1329. // Don't make the module visible if we are in the implementation.
  1330. if (ModuleName == getLangOpts().ImplementationOfModule)
  1331. return ModuleLoadResult(Module, false);
  1332. // Make the named module visible, if it's not already part of the module
  1333. // we are parsing.
  1334. if (ModuleName != getLangOpts().CurrentModule) {
  1335. if (!Module->IsFromModuleFile) {
  1336. // We have an umbrella header or directory that doesn't actually include
  1337. // all of the headers within the directory it covers. Complain about
  1338. // this missing submodule and recover by forgetting that we ever saw
  1339. // this submodule.
  1340. // FIXME: Should we detect this at module load time? It seems fairly
  1341. // expensive (and rare).
  1342. getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule)
  1343. << Module->getFullModuleName()
  1344. << SourceRange(Path.front().second, Path.back().second);
  1345. return ModuleLoadResult(nullptr, true);
  1346. }
  1347. // Check whether this module is available.
  1348. clang::Module::Requirement Requirement;
  1349. clang::Module::UnresolvedHeaderDirective MissingHeader;
  1350. if (!Module->isAvailable(getLangOpts(), getTarget(), Requirement,
  1351. MissingHeader)) {
  1352. if (MissingHeader.FileNameLoc.isValid()) {
  1353. getDiagnostics().Report(MissingHeader.FileNameLoc,
  1354. diag::err_module_header_missing)
  1355. << MissingHeader.IsUmbrella << MissingHeader.FileName;
  1356. } else {
  1357. getDiagnostics().Report(ImportLoc, diag::err_module_unavailable)
  1358. << Module->getFullModuleName()
  1359. << Requirement.second << Requirement.first
  1360. << SourceRange(Path.front().second, Path.back().second);
  1361. }
  1362. LastModuleImportLoc = ImportLoc;
  1363. LastModuleImportResult = ModuleLoadResult();
  1364. return ModuleLoadResult();
  1365. }
  1366. ModuleManager->makeModuleVisible(Module, Visibility, ImportLoc);
  1367. }
  1368. // Check for any configuration macros that have changed.
  1369. clang::Module *TopModule = Module->getTopLevelModule();
  1370. for (unsigned I = 0, N = TopModule->ConfigMacros.size(); I != N; ++I) {
  1371. checkConfigMacro(getPreprocessor(), TopModule->ConfigMacros[I],
  1372. Module, ImportLoc);
  1373. }
  1374. LastModuleImportLoc = ImportLoc;
  1375. LastModuleImportResult = ModuleLoadResult(Module, false);
  1376. return LastModuleImportResult;
  1377. }
  1378. void CompilerInstance::makeModuleVisible(Module *Mod,
  1379. Module::NameVisibilityKind Visibility,
  1380. SourceLocation ImportLoc) {
  1381. if (!ModuleManager)
  1382. createModuleManager();
  1383. if (!ModuleManager)
  1384. return;
  1385. ModuleManager->makeModuleVisible(Mod, Visibility, ImportLoc);
  1386. }
  1387. GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(
  1388. SourceLocation TriggerLoc) {
  1389. if (getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty())
  1390. return nullptr;
  1391. if (!ModuleManager)
  1392. createModuleManager();
  1393. // Can't do anything if we don't have the module manager.
  1394. if (!ModuleManager)
  1395. return nullptr;
  1396. // Get an existing global index. This loads it if not already
  1397. // loaded.
  1398. ModuleManager->loadGlobalIndex();
  1399. GlobalModuleIndex *GlobalIndex = ModuleManager->getGlobalIndex();
  1400. // If the global index doesn't exist, create it.
  1401. if (!GlobalIndex && shouldBuildGlobalModuleIndex() && hasFileManager() &&
  1402. hasPreprocessor()) {
  1403. llvm::sys::fs::create_directories(
  1404. getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
  1405. GlobalModuleIndex::writeIndex(
  1406. getFileManager(), getPCHContainerReader(),
  1407. getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
  1408. ModuleManager->resetForReload();
  1409. ModuleManager->loadGlobalIndex();
  1410. GlobalIndex = ModuleManager->getGlobalIndex();
  1411. }
  1412. // For finding modules needing to be imported for fixit messages,
  1413. // we need to make the global index cover all modules, so we do that here.
  1414. if (!HaveFullGlobalModuleIndex && GlobalIndex && !buildingModule()) {
  1415. ModuleMap &MMap = getPreprocessor().getHeaderSearchInfo().getModuleMap();
  1416. bool RecreateIndex = false;
  1417. for (ModuleMap::module_iterator I = MMap.module_begin(),
  1418. E = MMap.module_end(); I != E; ++I) {
  1419. Module *TheModule = I->second;
  1420. const FileEntry *Entry = TheModule->getASTFile();
  1421. if (!Entry) {
  1422. SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
  1423. Path.push_back(std::make_pair(
  1424. getPreprocessor().getIdentifierInfo(TheModule->Name), TriggerLoc));
  1425. std::reverse(Path.begin(), Path.end());
  1426. // Load a module as hidden. This also adds it to the global index.
  1427. loadModule(TheModule->DefinitionLoc, Path, Module::Hidden, false);
  1428. RecreateIndex = true;
  1429. }
  1430. }
  1431. if (RecreateIndex) {
  1432. GlobalModuleIndex::writeIndex(
  1433. getFileManager(), getPCHContainerReader(),
  1434. getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
  1435. ModuleManager->resetForReload();
  1436. ModuleManager->loadGlobalIndex();
  1437. GlobalIndex = ModuleManager->getGlobalIndex();
  1438. }
  1439. HaveFullGlobalModuleIndex = true;
  1440. }
  1441. return GlobalIndex;
  1442. }
  1443. // Check global module index for missing imports.
  1444. bool
  1445. CompilerInstance::lookupMissingImports(StringRef Name,
  1446. SourceLocation TriggerLoc) {
  1447. // Look for the symbol in non-imported modules, but only if an error
  1448. // actually occurred.
  1449. if (!buildingModule()) {
  1450. // Load global module index, or retrieve a previously loaded one.
  1451. GlobalModuleIndex *GlobalIndex = loadGlobalModuleIndex(
  1452. TriggerLoc);
  1453. // Only if we have a global index.
  1454. if (GlobalIndex) {
  1455. GlobalModuleIndex::HitSet FoundModules;
  1456. // Find the modules that reference the identifier.
  1457. // Note that this only finds top-level modules.
  1458. // We'll let diagnoseTypo find the actual declaration module.
  1459. if (GlobalIndex->lookupIdentifier(Name, FoundModules))
  1460. return true;
  1461. }
  1462. }
  1463. return false;
  1464. }
  1465. void CompilerInstance::resetAndLeakSema() { BuryPointer(takeSema()); }