CompilerInstance.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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/Frontend/ChainedDiagnosticConsumer.h"
  19. #include "clang/Frontend/FrontendAction.h"
  20. #include "clang/Frontend/FrontendActions.h"
  21. #include "clang/Frontend/FrontendDiagnostic.h"
  22. #include "clang/Frontend/LogDiagnosticPrinter.h"
  23. #include "clang/Frontend/SerializedDiagnosticPrinter.h"
  24. #include "clang/Frontend/TextDiagnosticPrinter.h"
  25. #include "clang/Frontend/Utils.h"
  26. #include "clang/Frontend/VerifyDiagnosticConsumer.h"
  27. #include "clang/Lex/HeaderSearch.h"
  28. #include "clang/Lex/PTHManager.h"
  29. #include "clang/Lex/Preprocessor.h"
  30. #include "clang/Sema/CodeCompleteConsumer.h"
  31. #include "clang/Sema/Sema.h"
  32. #include "clang/Serialization/ASTReader.h"
  33. #include "llvm/ADT/Statistic.h"
  34. #include "llvm/Config/config.h"
  35. #include "llvm/Support/CrashRecoveryContext.h"
  36. #include "llvm/Support/FileSystem.h"
  37. #include "llvm/Support/Host.h"
  38. #include "llvm/Support/LockFileManager.h"
  39. #include "llvm/Support/MemoryBuffer.h"
  40. #include "llvm/Support/Path.h"
  41. #include "llvm/Support/Program.h"
  42. #include "llvm/Support/Signals.h"
  43. #include "llvm/Support/Timer.h"
  44. #include "llvm/Support/raw_ostream.h"
  45. #include "llvm/Support/system_error.h"
  46. #include <sys/stat.h>
  47. #include <time.h>
  48. using namespace clang;
  49. CompilerInstance::CompilerInstance()
  50. : Invocation(new CompilerInvocation()), ModuleManager(0),
  51. BuildGlobalModuleIndex(false), ModuleBuildFailed(false) {
  52. }
  53. CompilerInstance::~CompilerInstance() {
  54. assert(OutputFiles.empty() && "Still output files in flight?");
  55. }
  56. void CompilerInstance::setInvocation(CompilerInvocation *Value) {
  57. Invocation = Value;
  58. }
  59. bool CompilerInstance::shouldBuildGlobalModuleIndex() const {
  60. return (BuildGlobalModuleIndex ||
  61. (ModuleManager && ModuleManager->isGlobalIndexUnavailable() &&
  62. getFrontendOpts().GenerateGlobalModuleIndex)) &&
  63. !ModuleBuildFailed;
  64. }
  65. void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) {
  66. Diagnostics = Value;
  67. }
  68. void CompilerInstance::setTarget(TargetInfo *Value) {
  69. Target = Value;
  70. }
  71. void CompilerInstance::setFileManager(FileManager *Value) {
  72. FileMgr = Value;
  73. if (Value)
  74. VirtualFileSystem = Value->getVirtualFileSystem();
  75. else
  76. VirtualFileSystem.reset();
  77. }
  78. void CompilerInstance::setSourceManager(SourceManager *Value) {
  79. SourceMgr = Value;
  80. }
  81. void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; }
  82. void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; }
  83. void CompilerInstance::setSema(Sema *S) {
  84. TheSema.reset(S);
  85. }
  86. void CompilerInstance::setASTConsumer(ASTConsumer *Value) {
  87. Consumer.reset(Value);
  88. }
  89. void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
  90. CompletionConsumer.reset(Value);
  91. }
  92. IntrusiveRefCntPtr<ASTReader> CompilerInstance::getModuleManager() const {
  93. return ModuleManager;
  94. }
  95. void CompilerInstance::setModuleManager(IntrusiveRefCntPtr<ASTReader> Reader) {
  96. ModuleManager = Reader;
  97. }
  98. // Diagnostics
  99. static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts,
  100. const CodeGenOptions *CodeGenOpts,
  101. DiagnosticsEngine &Diags) {
  102. std::string ErrorInfo;
  103. bool OwnsStream = false;
  104. raw_ostream *OS = &llvm::errs();
  105. if (DiagOpts->DiagnosticLogFile != "-") {
  106. // Create the output stream.
  107. llvm::raw_fd_ostream *FileOS(new llvm::raw_fd_ostream(
  108. DiagOpts->DiagnosticLogFile.c_str(), ErrorInfo,
  109. llvm::sys::fs::F_Append | llvm::sys::fs::F_Text));
  110. if (!ErrorInfo.empty()) {
  111. Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
  112. << DiagOpts->DiagnosticLogFile << ErrorInfo;
  113. } else {
  114. FileOS->SetUnbuffered();
  115. FileOS->SetUseAtomicWrites(true);
  116. OS = FileOS;
  117. OwnsStream = true;
  118. }
  119. }
  120. // Chain in the diagnostic client which will log the diagnostics.
  121. LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts,
  122. OwnsStream);
  123. if (CodeGenOpts)
  124. Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
  125. Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger));
  126. }
  127. static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts,
  128. DiagnosticsEngine &Diags,
  129. StringRef OutputFile) {
  130. std::string ErrorInfo;
  131. OwningPtr<llvm::raw_fd_ostream> OS;
  132. OS.reset(new llvm::raw_fd_ostream(OutputFile.str().c_str(), ErrorInfo,
  133. llvm::sys::fs::F_None));
  134. if (!ErrorInfo.empty()) {
  135. Diags.Report(diag::warn_fe_serialized_diag_failure)
  136. << OutputFile << ErrorInfo;
  137. return;
  138. }
  139. DiagnosticConsumer *SerializedConsumer =
  140. clang::serialized_diags::create(OS.take(), DiagOpts);
  141. Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(),
  142. SerializedConsumer));
  143. }
  144. void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client,
  145. bool ShouldOwnClient) {
  146. Diagnostics = createDiagnostics(&getDiagnosticOpts(), Client,
  147. ShouldOwnClient, &getCodeGenOpts());
  148. }
  149. IntrusiveRefCntPtr<DiagnosticsEngine>
  150. CompilerInstance::createDiagnostics(DiagnosticOptions *Opts,
  151. DiagnosticConsumer *Client,
  152. bool ShouldOwnClient,
  153. const CodeGenOptions *CodeGenOpts) {
  154. IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
  155. IntrusiveRefCntPtr<DiagnosticsEngine>
  156. Diags(new DiagnosticsEngine(DiagID, Opts));
  157. // Create the diagnostic client for reporting errors or for
  158. // implementing -verify.
  159. if (Client) {
  160. Diags->setClient(Client, ShouldOwnClient);
  161. } else
  162. Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
  163. // Chain in -verify checker, if requested.
  164. if (Opts->VerifyDiagnostics)
  165. Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
  166. // Chain in -diagnostic-log-file dumper, if requested.
  167. if (!Opts->DiagnosticLogFile.empty())
  168. SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
  169. if (!Opts->DiagnosticSerializationFile.empty())
  170. SetupSerializedDiagnostics(Opts, *Diags,
  171. Opts->DiagnosticSerializationFile);
  172. // Configure our handling of diagnostics.
  173. ProcessWarningOptions(*Diags, *Opts);
  174. return Diags;
  175. }
  176. // File Manager
  177. void CompilerInstance::createFileManager() {
  178. if (!hasVirtualFileSystem()) {
  179. // TODO: choose the virtual file system based on the CompilerInvocation.
  180. setVirtualFileSystem(vfs::getRealFileSystem());
  181. }
  182. FileMgr = new FileManager(getFileSystemOpts(), VirtualFileSystem);
  183. }
  184. // Source Manager
  185. void CompilerInstance::createSourceManager(FileManager &FileMgr) {
  186. SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
  187. }
  188. // Preprocessor
  189. void CompilerInstance::createPreprocessor() {
  190. const PreprocessorOptions &PPOpts = getPreprocessorOpts();
  191. // Create a PTH manager if we are using some form of a token cache.
  192. PTHManager *PTHMgr = 0;
  193. if (!PPOpts.TokenCache.empty())
  194. PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics());
  195. // Create the Preprocessor.
  196. HeaderSearch *HeaderInfo = new HeaderSearch(&getHeaderSearchOpts(),
  197. getSourceManager(),
  198. getDiagnostics(),
  199. getLangOpts(),
  200. &getTarget());
  201. PP = new Preprocessor(&getPreprocessorOpts(),
  202. getDiagnostics(), getLangOpts(), &getTarget(),
  203. getSourceManager(), *HeaderInfo, *this, PTHMgr,
  204. /*OwnsHeaderSearch=*/true);
  205. // Note that this is different then passing PTHMgr to Preprocessor's ctor.
  206. // That argument is used as the IdentifierInfoLookup argument to
  207. // IdentifierTable's ctor.
  208. if (PTHMgr) {
  209. PTHMgr->setPreprocessor(&*PP);
  210. PP->setPTHManager(PTHMgr);
  211. }
  212. if (PPOpts.DetailedRecord)
  213. PP->createPreprocessingRecord();
  214. InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts());
  215. PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP);
  216. // Set up the module path, including the hash for the
  217. // module-creation options.
  218. SmallString<256> SpecificModuleCache(
  219. getHeaderSearchOpts().ModuleCachePath);
  220. if (!getHeaderSearchOpts().DisableModuleHash)
  221. llvm::sys::path::append(SpecificModuleCache,
  222. getInvocation().getModuleHash());
  223. PP->getHeaderSearchInfo().setModuleCachePath(SpecificModuleCache);
  224. // Handle generating dependencies, if requested.
  225. const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();
  226. if (!DepOpts.OutputFile.empty())
  227. AttachDependencyFileGen(*PP, DepOpts);
  228. if (!DepOpts.DOTOutputFile.empty())
  229. AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile,
  230. getHeaderSearchOpts().Sysroot);
  231. // Handle generating header include information, if requested.
  232. if (DepOpts.ShowHeaderIncludes)
  233. AttachHeaderIncludeGen(*PP);
  234. if (!DepOpts.HeaderIncludeOutputFile.empty()) {
  235. StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
  236. if (OutputPath == "-")
  237. OutputPath = "";
  238. AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath,
  239. /*ShowDepth=*/false);
  240. }
  241. if (DepOpts.PrintShowIncludes) {
  242. AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/false, /*OutputPath=*/"",
  243. /*ShowDepth=*/true, /*MSStyle=*/true);
  244. }
  245. }
  246. // ASTContext
  247. void CompilerInstance::createASTContext() {
  248. Preprocessor &PP = getPreprocessor();
  249. Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
  250. &getTarget(), PP.getIdentifierTable(),
  251. PP.getSelectorTable(), PP.getBuiltinInfo(),
  252. /*size_reserve=*/ 0);
  253. }
  254. // ExternalASTSource
  255. void CompilerInstance::createPCHExternalASTSource(StringRef Path,
  256. bool DisablePCHValidation,
  257. bool AllowPCHWithCompilerErrors,
  258. void *DeserializationListener){
  259. IntrusiveRefCntPtr<ExternalASTSource> Source;
  260. bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
  261. Source = createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
  262. DisablePCHValidation,
  263. AllowPCHWithCompilerErrors,
  264. getPreprocessor(), getASTContext(),
  265. DeserializationListener,
  266. Preamble,
  267. getFrontendOpts().UseGlobalModuleIndex);
  268. ModuleManager = static_cast<ASTReader*>(Source.getPtr());
  269. getASTContext().setExternalSource(Source);
  270. }
  271. ExternalASTSource *
  272. CompilerInstance::createPCHExternalASTSource(StringRef Path,
  273. const std::string &Sysroot,
  274. bool DisablePCHValidation,
  275. bool AllowPCHWithCompilerErrors,
  276. Preprocessor &PP,
  277. ASTContext &Context,
  278. void *DeserializationListener,
  279. bool Preamble,
  280. bool UseGlobalModuleIndex) {
  281. OwningPtr<ASTReader> Reader;
  282. Reader.reset(new ASTReader(PP, Context,
  283. Sysroot.empty() ? "" : Sysroot.c_str(),
  284. DisablePCHValidation,
  285. AllowPCHWithCompilerErrors,
  286. /*AllowConfigurationMismatch*/false,
  287. /*ValidateSystemInputs*/false,
  288. UseGlobalModuleIndex));
  289. Reader->setDeserializationListener(
  290. static_cast<ASTDeserializationListener *>(DeserializationListener));
  291. switch (Reader->ReadAST(Path,
  292. Preamble ? serialization::MK_Preamble
  293. : serialization::MK_PCH,
  294. SourceLocation(),
  295. ASTReader::ARR_None)) {
  296. case ASTReader::Success:
  297. // Set the predefines buffer as suggested by the PCH reader. Typically, the
  298. // predefines buffer will be empty.
  299. PP.setPredefines(Reader->getSuggestedPredefines());
  300. return Reader.take();
  301. case ASTReader::Failure:
  302. // Unrecoverable failure: don't even try to process the input file.
  303. break;
  304. case ASTReader::Missing:
  305. case ASTReader::OutOfDate:
  306. case ASTReader::VersionMismatch:
  307. case ASTReader::ConfigurationMismatch:
  308. case ASTReader::HadErrors:
  309. // No suitable PCH file could be found. Return an error.
  310. break;
  311. }
  312. return 0;
  313. }
  314. // Code Completion
  315. static bool EnableCodeCompletion(Preprocessor &PP,
  316. const std::string &Filename,
  317. unsigned Line,
  318. unsigned Column) {
  319. // Tell the source manager to chop off the given file at a specific
  320. // line and column.
  321. const FileEntry *Entry = PP.getFileManager().getFile(Filename);
  322. if (!Entry) {
  323. PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
  324. << Filename;
  325. return true;
  326. }
  327. // Truncate the named file at the given line/column.
  328. PP.SetCodeCompletionPoint(Entry, Line, Column);
  329. return false;
  330. }
  331. void CompilerInstance::createCodeCompletionConsumer() {
  332. const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
  333. if (!CompletionConsumer) {
  334. setCodeCompletionConsumer(
  335. createCodeCompletionConsumer(getPreprocessor(),
  336. Loc.FileName, Loc.Line, Loc.Column,
  337. getFrontendOpts().CodeCompleteOpts,
  338. llvm::outs()));
  339. if (!CompletionConsumer)
  340. return;
  341. } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
  342. Loc.Line, Loc.Column)) {
  343. setCodeCompletionConsumer(0);
  344. return;
  345. }
  346. if (CompletionConsumer->isOutputBinary() &&
  347. llvm::sys::ChangeStdoutToBinary()) {
  348. getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
  349. setCodeCompletionConsumer(0);
  350. }
  351. }
  352. void CompilerInstance::createFrontendTimer() {
  353. FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
  354. }
  355. CodeCompleteConsumer *
  356. CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
  357. const std::string &Filename,
  358. unsigned Line,
  359. unsigned Column,
  360. const CodeCompleteOptions &Opts,
  361. raw_ostream &OS) {
  362. if (EnableCodeCompletion(PP, Filename, Line, Column))
  363. return 0;
  364. // Set up the creation routine for code-completion.
  365. return new PrintingCodeCompleteConsumer(Opts, OS);
  366. }
  367. void CompilerInstance::createSema(TranslationUnitKind TUKind,
  368. CodeCompleteConsumer *CompletionConsumer) {
  369. TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
  370. TUKind, CompletionConsumer));
  371. }
  372. // Output Files
  373. void CompilerInstance::addOutputFile(const OutputFile &OutFile) {
  374. assert(OutFile.OS && "Attempt to add empty stream to output list!");
  375. OutputFiles.push_back(OutFile);
  376. }
  377. void CompilerInstance::clearOutputFiles(bool EraseFiles) {
  378. for (std::list<OutputFile>::iterator
  379. it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
  380. delete it->OS;
  381. if (!it->TempFilename.empty()) {
  382. if (EraseFiles) {
  383. llvm::sys::fs::remove(it->TempFilename);
  384. } else {
  385. SmallString<128> NewOutFile(it->Filename);
  386. // If '-working-directory' was passed, the output filename should be
  387. // relative to that.
  388. FileMgr->FixupRelativePath(NewOutFile);
  389. if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename,
  390. NewOutFile.str())) {
  391. getDiagnostics().Report(diag::err_unable_to_rename_temp)
  392. << it->TempFilename << it->Filename << ec.message();
  393. llvm::sys::fs::remove(it->TempFilename);
  394. }
  395. }
  396. } else if (!it->Filename.empty() && EraseFiles)
  397. llvm::sys::fs::remove(it->Filename);
  398. }
  399. OutputFiles.clear();
  400. }
  401. llvm::raw_fd_ostream *
  402. CompilerInstance::createDefaultOutputFile(bool Binary,
  403. StringRef InFile,
  404. StringRef Extension) {
  405. return createOutputFile(getFrontendOpts().OutputFile, Binary,
  406. /*RemoveFileOnSignal=*/true, InFile, Extension,
  407. /*UseTemporary=*/true);
  408. }
  409. llvm::raw_fd_ostream *
  410. CompilerInstance::createOutputFile(StringRef OutputPath,
  411. bool Binary, bool RemoveFileOnSignal,
  412. StringRef InFile,
  413. StringRef Extension,
  414. bool UseTemporary,
  415. bool CreateMissingDirectories) {
  416. std::string Error, OutputPathName, TempPathName;
  417. llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
  418. RemoveFileOnSignal,
  419. InFile, Extension,
  420. UseTemporary,
  421. CreateMissingDirectories,
  422. &OutputPathName,
  423. &TempPathName);
  424. if (!OS) {
  425. getDiagnostics().Report(diag::err_fe_unable_to_open_output)
  426. << OutputPath << Error;
  427. return 0;
  428. }
  429. // Add the output file -- but don't try to remove "-", since this means we are
  430. // using stdin.
  431. addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
  432. TempPathName, OS));
  433. return OS;
  434. }
  435. llvm::raw_fd_ostream *
  436. CompilerInstance::createOutputFile(StringRef OutputPath,
  437. std::string &Error,
  438. bool Binary,
  439. bool RemoveFileOnSignal,
  440. StringRef InFile,
  441. StringRef Extension,
  442. bool UseTemporary,
  443. bool CreateMissingDirectories,
  444. std::string *ResultPathName,
  445. std::string *TempPathName) {
  446. assert((!CreateMissingDirectories || UseTemporary) &&
  447. "CreateMissingDirectories is only allowed when using temporary files");
  448. std::string OutFile, TempFile;
  449. if (!OutputPath.empty()) {
  450. OutFile = OutputPath;
  451. } else if (InFile == "-") {
  452. OutFile = "-";
  453. } else if (!Extension.empty()) {
  454. SmallString<128> Path(InFile);
  455. llvm::sys::path::replace_extension(Path, Extension);
  456. OutFile = Path.str();
  457. } else {
  458. OutFile = "-";
  459. }
  460. OwningPtr<llvm::raw_fd_ostream> OS;
  461. std::string OSFile;
  462. if (UseTemporary) {
  463. if (OutFile == "-")
  464. UseTemporary = false;
  465. else {
  466. llvm::sys::fs::file_status Status;
  467. llvm::sys::fs::status(OutputPath, Status);
  468. if (llvm::sys::fs::exists(Status)) {
  469. // Fail early if we can't write to the final destination.
  470. if (!llvm::sys::fs::can_write(OutputPath))
  471. return 0;
  472. // Don't use a temporary if the output is a special file. This handles
  473. // things like '-o /dev/null'
  474. if (!llvm::sys::fs::is_regular_file(Status))
  475. UseTemporary = false;
  476. }
  477. }
  478. }
  479. if (UseTemporary) {
  480. // Create a temporary file.
  481. SmallString<128> TempPath;
  482. TempPath = OutFile;
  483. TempPath += "-%%%%%%%%";
  484. int fd;
  485. llvm::error_code EC =
  486. llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath);
  487. if (CreateMissingDirectories &&
  488. EC == llvm::errc::no_such_file_or_directory) {
  489. StringRef Parent = llvm::sys::path::parent_path(OutputPath);
  490. EC = llvm::sys::fs::create_directories(Parent);
  491. if (!EC) {
  492. EC = llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath);
  493. }
  494. }
  495. if (!EC) {
  496. OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
  497. OSFile = TempFile = TempPath.str();
  498. }
  499. // If we failed to create the temporary, fallback to writing to the file
  500. // directly. This handles the corner case where we cannot write to the
  501. // directory, but can write to the file.
  502. }
  503. if (!OS) {
  504. OSFile = OutFile;
  505. OS.reset(new llvm::raw_fd_ostream(
  506. OSFile.c_str(), Error,
  507. (Binary ? llvm::sys::fs::F_None : llvm::sys::fs::F_Text)));
  508. if (!Error.empty())
  509. return 0;
  510. }
  511. // Make sure the out stream file gets removed if we crash.
  512. if (RemoveFileOnSignal)
  513. llvm::sys::RemoveFileOnSignal(OSFile);
  514. if (ResultPathName)
  515. *ResultPathName = OutFile;
  516. if (TempPathName)
  517. *TempPathName = TempFile;
  518. return OS.take();
  519. }
  520. // Initialization Utilities
  521. bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input){
  522. return InitializeSourceManager(Input, getDiagnostics(),
  523. getFileManager(), getSourceManager(),
  524. getFrontendOpts());
  525. }
  526. bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input,
  527. DiagnosticsEngine &Diags,
  528. FileManager &FileMgr,
  529. SourceManager &SourceMgr,
  530. const FrontendOptions &Opts) {
  531. SrcMgr::CharacteristicKind
  532. Kind = Input.isSystem() ? SrcMgr::C_System : SrcMgr::C_User;
  533. if (Input.isBuffer()) {
  534. SourceMgr.createMainFileIDForMemBuffer(Input.getBuffer(), Kind);
  535. assert(!SourceMgr.getMainFileID().isInvalid() &&
  536. "Couldn't establish MainFileID!");
  537. return true;
  538. }
  539. StringRef InputFile = Input.getFile();
  540. // Figure out where to get and map in the main file.
  541. if (InputFile != "-") {
  542. const FileEntry *File = FileMgr.getFile(InputFile, /*OpenFile=*/true);
  543. if (!File) {
  544. Diags.Report(diag::err_fe_error_reading) << InputFile;
  545. return false;
  546. }
  547. // The natural SourceManager infrastructure can't currently handle named
  548. // pipes, but we would at least like to accept them for the main
  549. // file. Detect them here, read them with the volatile flag so FileMgr will
  550. // pick up the correct size, and simply override their contents as we do for
  551. // STDIN.
  552. if (File->isNamedPipe()) {
  553. std::string ErrorStr;
  554. if (llvm::MemoryBuffer *MB =
  555. FileMgr.getBufferForFile(File, &ErrorStr, /*isVolatile=*/true)) {
  556. // Create a new virtual file that will have the correct size.
  557. File = FileMgr.getVirtualFile(InputFile, MB->getBufferSize(), 0);
  558. SourceMgr.overrideFileContents(File, MB);
  559. } else {
  560. Diags.Report(diag::err_cannot_open_file) << InputFile << ErrorStr;
  561. return false;
  562. }
  563. }
  564. SourceMgr.createMainFileID(File, Kind);
  565. } else {
  566. OwningPtr<llvm::MemoryBuffer> SB;
  567. if (llvm::error_code ec = llvm::MemoryBuffer::getSTDIN(SB)) {
  568. Diags.Report(diag::err_fe_error_reading_stdin) << ec.message();
  569. return false;
  570. }
  571. const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
  572. SB->getBufferSize(), 0);
  573. SourceMgr.createMainFileID(File, Kind);
  574. SourceMgr.overrideFileContents(File, SB.take());
  575. }
  576. assert(!SourceMgr.getMainFileID().isInvalid() &&
  577. "Couldn't establish MainFileID!");
  578. return true;
  579. }
  580. // High-Level Operations
  581. bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
  582. assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
  583. assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
  584. assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
  585. // FIXME: Take this as an argument, once all the APIs we used have moved to
  586. // taking it as an input instead of hard-coding llvm::errs.
  587. raw_ostream &OS = llvm::errs();
  588. // Create the target instance.
  589. setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), &getTargetOpts()));
  590. if (!hasTarget())
  591. return false;
  592. // Inform the target of the language options.
  593. //
  594. // FIXME: We shouldn't need to do this, the target should be immutable once
  595. // created. This complexity should be lifted elsewhere.
  596. getTarget().setForcedLangOptions(getLangOpts());
  597. // rewriter project will change target built-in bool type from its default.
  598. if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
  599. getTarget().noSignedCharForObjCBool();
  600. // Validate/process some options.
  601. if (getHeaderSearchOpts().Verbose)
  602. OS << "clang -cc1 version " CLANG_VERSION_STRING
  603. << " based upon " << PACKAGE_STRING
  604. << " default target " << llvm::sys::getDefaultTargetTriple() << "\n";
  605. if (getFrontendOpts().ShowTimers)
  606. createFrontendTimer();
  607. if (getFrontendOpts().ShowStats)
  608. llvm::EnableStatistics();
  609. for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
  610. // Reset the ID tables if we are reusing the SourceManager.
  611. if (hasSourceManager())
  612. getSourceManager().clearIDTables();
  613. if (Act.BeginSourceFile(*this, getFrontendOpts().Inputs[i])) {
  614. Act.Execute();
  615. Act.EndSourceFile();
  616. }
  617. }
  618. // Notify the diagnostic client that all files were processed.
  619. getDiagnostics().getClient()->finish();
  620. if (getDiagnosticOpts().ShowCarets) {
  621. // We can have multiple diagnostics sharing one diagnostic client.
  622. // Get the total number of warnings/errors from the client.
  623. unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
  624. unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
  625. if (NumWarnings)
  626. OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
  627. if (NumWarnings && NumErrors)
  628. OS << " and ";
  629. if (NumErrors)
  630. OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
  631. if (NumWarnings || NumErrors)
  632. OS << " generated.\n";
  633. }
  634. if (getFrontendOpts().ShowStats && hasFileManager()) {
  635. getFileManager().PrintStats();
  636. OS << "\n";
  637. }
  638. return !getDiagnostics().getClient()->getNumErrors();
  639. }
  640. /// \brief Determine the appropriate source input kind based on language
  641. /// options.
  642. static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) {
  643. if (LangOpts.OpenCL)
  644. return IK_OpenCL;
  645. if (LangOpts.CUDA)
  646. return IK_CUDA;
  647. if (LangOpts.ObjC1)
  648. return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC;
  649. return LangOpts.CPlusPlus? IK_CXX : IK_C;
  650. }
  651. namespace {
  652. struct CompileModuleMapData {
  653. CompilerInstance &Instance;
  654. GenerateModuleAction &CreateModuleAction;
  655. };
  656. }
  657. /// \brief Helper function that executes the module-generating action under
  658. /// a crash recovery context.
  659. static void doCompileMapModule(void *UserData) {
  660. CompileModuleMapData &Data
  661. = *reinterpret_cast<CompileModuleMapData *>(UserData);
  662. Data.Instance.ExecuteAction(Data.CreateModuleAction);
  663. }
  664. namespace {
  665. /// \brief Function object that checks with the given macro definition should
  666. /// be removed, because it is one of the ignored macros.
  667. class RemoveIgnoredMacro {
  668. const HeaderSearchOptions &HSOpts;
  669. public:
  670. explicit RemoveIgnoredMacro(const HeaderSearchOptions &HSOpts)
  671. : HSOpts(HSOpts) { }
  672. bool operator()(const std::pair<std::string, bool> &def) const {
  673. StringRef MacroDef = def.first;
  674. return HSOpts.ModulesIgnoreMacros.count(MacroDef.split('=').first) > 0;
  675. }
  676. };
  677. }
  678. /// \brief Compile a module file for the given module, using the options
  679. /// provided by the importing compiler instance.
  680. static void compileModule(CompilerInstance &ImportingInstance,
  681. SourceLocation ImportLoc,
  682. Module *Module,
  683. StringRef ModuleFileName) {
  684. // FIXME: have LockFileManager return an error_code so that we can
  685. // avoid the mkdir when the directory already exists.
  686. StringRef Dir = llvm::sys::path::parent_path(ModuleFileName);
  687. llvm::sys::fs::create_directories(Dir);
  688. llvm::LockFileManager Locked(ModuleFileName);
  689. switch (Locked) {
  690. case llvm::LockFileManager::LFS_Error:
  691. return;
  692. case llvm::LockFileManager::LFS_Owned:
  693. // We're responsible for building the module ourselves. Do so below.
  694. break;
  695. case llvm::LockFileManager::LFS_Shared:
  696. // Someone else is responsible for building the module. Wait for them to
  697. // finish.
  698. Locked.waitForUnlock();
  699. return;
  700. }
  701. ModuleMap &ModMap
  702. = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap();
  703. // Construct a compiler invocation for creating this module.
  704. IntrusiveRefCntPtr<CompilerInvocation> Invocation
  705. (new CompilerInvocation(ImportingInstance.getInvocation()));
  706. PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
  707. // For any options that aren't intended to affect how a module is built,
  708. // reset them to their default values.
  709. Invocation->getLangOpts()->resetNonModularOptions();
  710. PPOpts.resetNonModularOptions();
  711. // Remove any macro definitions that are explicitly ignored by the module.
  712. // They aren't supposed to affect how the module is built anyway.
  713. const HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts();
  714. PPOpts.Macros.erase(std::remove_if(PPOpts.Macros.begin(), PPOpts.Macros.end(),
  715. RemoveIgnoredMacro(HSOpts)),
  716. PPOpts.Macros.end());
  717. // Note the name of the module we're building.
  718. Invocation->getLangOpts()->CurrentModule = Module->getTopLevelModuleName();
  719. // Make sure that the failed-module structure has been allocated in
  720. // the importing instance, and propagate the pointer to the newly-created
  721. // instance.
  722. PreprocessorOptions &ImportingPPOpts
  723. = ImportingInstance.getInvocation().getPreprocessorOpts();
  724. if (!ImportingPPOpts.FailedModules)
  725. ImportingPPOpts.FailedModules = new PreprocessorOptions::FailedModulesSet;
  726. PPOpts.FailedModules = ImportingPPOpts.FailedModules;
  727. // If there is a module map file, build the module using the module map.
  728. // Set up the inputs/outputs so that we build the module from its umbrella
  729. // header.
  730. FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
  731. FrontendOpts.OutputFile = ModuleFileName.str();
  732. FrontendOpts.DisableFree = false;
  733. FrontendOpts.GenerateGlobalModuleIndex = false;
  734. FrontendOpts.Inputs.clear();
  735. InputKind IK = getSourceInputKindFromOptions(*Invocation->getLangOpts());
  736. // Don't free the remapped file buffers; they are owned by our caller.
  737. PPOpts.RetainRemappedFileBuffers = true;
  738. Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
  739. assert(ImportingInstance.getInvocation().getModuleHash() ==
  740. Invocation->getModuleHash() && "Module hash mismatch!");
  741. // Construct a compiler instance that will be used to actually create the
  742. // module.
  743. CompilerInstance Instance;
  744. Instance.setInvocation(&*Invocation);
  745. Instance.createDiagnostics(new ForwardingDiagnosticConsumer(
  746. ImportingInstance.getDiagnosticClient()),
  747. /*ShouldOwnClient=*/true);
  748. Instance.setVirtualFileSystem(&ImportingInstance.getVirtualFileSystem());
  749. // Note that this module is part of the module build stack, so that we
  750. // can detect cycles in the module graph.
  751. Instance.setFileManager(&ImportingInstance.getFileManager());
  752. Instance.createSourceManager(Instance.getFileManager());
  753. SourceManager &SourceMgr = Instance.getSourceManager();
  754. SourceMgr.setModuleBuildStack(
  755. ImportingInstance.getSourceManager().getModuleBuildStack());
  756. SourceMgr.pushModuleBuildStack(Module->getTopLevelModuleName(),
  757. FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager()));
  758. // Get or create the module map that we'll use to build this module.
  759. std::string InferredModuleMapContent;
  760. if (const FileEntry *ModuleMapFile =
  761. ModMap.getContainingModuleMapFile(Module)) {
  762. // Use the module map where this module resides.
  763. FrontendOpts.Inputs.push_back(
  764. FrontendInputFile(ModuleMapFile->getName(), IK));
  765. } else {
  766. llvm::raw_string_ostream OS(InferredModuleMapContent);
  767. Module->print(OS);
  768. OS.flush();
  769. FrontendOpts.Inputs.push_back(
  770. FrontendInputFile("__inferred_module.map", IK));
  771. const llvm::MemoryBuffer *ModuleMapBuffer =
  772. llvm::MemoryBuffer::getMemBuffer(InferredModuleMapContent);
  773. ModuleMapFile = Instance.getFileManager().getVirtualFile(
  774. "__inferred_module.map", InferredModuleMapContent.size(), 0);
  775. SourceMgr.overrideFileContents(ModuleMapFile, ModuleMapBuffer);
  776. }
  777. // Construct a module-generating action.
  778. GenerateModuleAction CreateModuleAction(Module->IsSystem);
  779. // Execute the action to actually build the module in-place. Use a separate
  780. // thread so that we get a stack large enough.
  781. const unsigned ThreadStackSize = 8 << 20;
  782. llvm::CrashRecoveryContext CRC;
  783. CompileModuleMapData Data = { Instance, CreateModuleAction };
  784. CRC.RunSafelyOnThread(&doCompileMapModule, &Data, ThreadStackSize);
  785. // Delete the temporary module map file.
  786. // FIXME: Even though we're executing under crash protection, it would still
  787. // be nice to do this with RemoveFileOnSignal when we can. However, that
  788. // doesn't make sense for all clients, so clean this up manually.
  789. Instance.clearOutputFiles(/*EraseFiles=*/true);
  790. // We've rebuilt a module. If we're allowed to generate or update the global
  791. // module index, record that fact in the importing compiler instance.
  792. if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) {
  793. ImportingInstance.setBuildGlobalModuleIndex(true);
  794. }
  795. }
  796. /// \brief Diagnose differences between the current definition of the given
  797. /// configuration macro and the definition provided on the command line.
  798. static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
  799. Module *Mod, SourceLocation ImportLoc) {
  800. IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro);
  801. SourceManager &SourceMgr = PP.getSourceManager();
  802. // If this identifier has never had a macro definition, then it could
  803. // not have changed.
  804. if (!Id->hadMacroDefinition())
  805. return;
  806. // If this identifier does not currently have a macro definition,
  807. // check whether it had one on the command line.
  808. if (!Id->hasMacroDefinition()) {
  809. MacroDirective::DefInfo LatestDef =
  810. PP.getMacroDirectiveHistory(Id)->getDefinition();
  811. for (MacroDirective::DefInfo Def = LatestDef; Def;
  812. Def = Def.getPreviousDefinition()) {
  813. FileID FID = SourceMgr.getFileID(Def.getLocation());
  814. if (FID.isInvalid())
  815. continue;
  816. // We only care about the predefines buffer.
  817. if (FID != PP.getPredefinesFileID())
  818. continue;
  819. // This macro was defined on the command line, then #undef'd later.
  820. // Complain.
  821. PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
  822. << true << ConfigMacro << Mod->getFullModuleName();
  823. if (LatestDef.isUndefined())
  824. PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here)
  825. << true;
  826. return;
  827. }
  828. // Okay: no definition in the predefines buffer.
  829. return;
  830. }
  831. // This identifier has a macro definition. Check whether we had a definition
  832. // on the command line.
  833. MacroDirective::DefInfo LatestDef =
  834. PP.getMacroDirectiveHistory(Id)->getDefinition();
  835. MacroDirective::DefInfo PredefinedDef;
  836. for (MacroDirective::DefInfo Def = LatestDef; Def;
  837. Def = Def.getPreviousDefinition()) {
  838. FileID FID = SourceMgr.getFileID(Def.getLocation());
  839. if (FID.isInvalid())
  840. continue;
  841. // We only care about the predefines buffer.
  842. if (FID != PP.getPredefinesFileID())
  843. continue;
  844. PredefinedDef = Def;
  845. break;
  846. }
  847. // If there was no definition for this macro in the predefines buffer,
  848. // complain.
  849. if (!PredefinedDef ||
  850. (!PredefinedDef.getLocation().isValid() &&
  851. PredefinedDef.getUndefLocation().isValid())) {
  852. PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
  853. << false << ConfigMacro << Mod->getFullModuleName();
  854. PP.Diag(LatestDef.getLocation(), diag::note_module_def_undef_here)
  855. << false;
  856. return;
  857. }
  858. // If the current macro definition is the same as the predefined macro
  859. // definition, it's okay.
  860. if (LatestDef.getMacroInfo() == PredefinedDef.getMacroInfo() ||
  861. LatestDef.getMacroInfo()->isIdenticalTo(*PredefinedDef.getMacroInfo(),PP,
  862. /*Syntactically=*/true))
  863. return;
  864. // The macro definitions differ.
  865. PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
  866. << false << ConfigMacro << Mod->getFullModuleName();
  867. PP.Diag(LatestDef.getLocation(), diag::note_module_def_undef_here)
  868. << false;
  869. }
  870. /// \brief Write a new timestamp file with the given path.
  871. static void writeTimestampFile(StringRef TimestampFile) {
  872. std::string ErrorInfo;
  873. llvm::raw_fd_ostream Out(TimestampFile.str().c_str(), ErrorInfo,
  874. llvm::sys::fs::F_None);
  875. }
  876. /// \brief Prune the module cache of modules that haven't been accessed in
  877. /// a long time.
  878. static void pruneModuleCache(const HeaderSearchOptions &HSOpts) {
  879. struct stat StatBuf;
  880. llvm::SmallString<128> TimestampFile;
  881. TimestampFile = HSOpts.ModuleCachePath;
  882. llvm::sys::path::append(TimestampFile, "modules.timestamp");
  883. // Try to stat() the timestamp file.
  884. if (::stat(TimestampFile.c_str(), &StatBuf)) {
  885. // If the timestamp file wasn't there, create one now.
  886. if (errno == ENOENT) {
  887. writeTimestampFile(TimestampFile);
  888. }
  889. return;
  890. }
  891. // Check whether the time stamp is older than our pruning interval.
  892. // If not, do nothing.
  893. time_t TimeStampModTime = StatBuf.st_mtime;
  894. time_t CurrentTime = time(0);
  895. if (CurrentTime - TimeStampModTime <= time_t(HSOpts.ModuleCachePruneInterval))
  896. return;
  897. // Write a new timestamp file so that nobody else attempts to prune.
  898. // There is a benign race condition here, if two Clang instances happen to
  899. // notice at the same time that the timestamp is out-of-date.
  900. writeTimestampFile(TimestampFile);
  901. // Walk the entire module cache, looking for unused module files and module
  902. // indices.
  903. llvm::error_code EC;
  904. SmallString<128> ModuleCachePathNative;
  905. llvm::sys::path::native(HSOpts.ModuleCachePath, ModuleCachePathNative);
  906. for (llvm::sys::fs::directory_iterator
  907. Dir(ModuleCachePathNative.str(), EC), DirEnd;
  908. Dir != DirEnd && !EC; Dir.increment(EC)) {
  909. // If we don't have a directory, there's nothing to look into.
  910. if (!llvm::sys::fs::is_directory(Dir->path()))
  911. continue;
  912. // Walk all of the files within this directory.
  913. for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd;
  914. File != FileEnd && !EC; File.increment(EC)) {
  915. // We only care about module and global module index files.
  916. StringRef Extension = llvm::sys::path::extension(File->path());
  917. if (Extension != ".pcm" && Extension != ".timestamp" &&
  918. llvm::sys::path::filename(File->path()) != "modules.idx")
  919. continue;
  920. // Look at this file. If we can't stat it, there's nothing interesting
  921. // there.
  922. if (::stat(File->path().c_str(), &StatBuf))
  923. continue;
  924. // If the file has been used recently enough, leave it there.
  925. time_t FileAccessTime = StatBuf.st_atime;
  926. if (CurrentTime - FileAccessTime <=
  927. time_t(HSOpts.ModuleCachePruneAfter)) {
  928. continue;
  929. }
  930. // Remove the file.
  931. llvm::sys::fs::remove(File->path());
  932. // Remove the timestamp file.
  933. std::string TimpestampFilename = File->path() + ".timestamp";
  934. llvm::sys::fs::remove(TimpestampFilename);
  935. }
  936. // If we removed all of the files in the directory, remove the directory
  937. // itself.
  938. if (llvm::sys::fs::directory_iterator(Dir->path(), EC) ==
  939. llvm::sys::fs::directory_iterator() && !EC)
  940. llvm::sys::fs::remove(Dir->path());
  941. }
  942. }
  943. ModuleLoadResult
  944. CompilerInstance::loadModule(SourceLocation ImportLoc,
  945. ModuleIdPath Path,
  946. Module::NameVisibilityKind Visibility,
  947. bool IsInclusionDirective) {
  948. // Determine what file we're searching from.
  949. StringRef ModuleName = Path[0].first->getName();
  950. SourceLocation ModuleNameLoc = Path[0].second;
  951. // If we've already handled this import, just return the cached result.
  952. // This one-element cache is important to eliminate redundant diagnostics
  953. // when both the preprocessor and parser see the same import declaration.
  954. if (!ImportLoc.isInvalid() && LastModuleImportLoc == ImportLoc) {
  955. // Make the named module visible.
  956. if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule)
  957. ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility,
  958. ImportLoc, /*Complain=*/false);
  959. return LastModuleImportResult;
  960. }
  961. clang::Module *Module = 0;
  962. // If we don't already have information on this module, load the module now.
  963. llvm::DenseMap<const IdentifierInfo *, clang::Module *>::iterator Known
  964. = KnownModules.find(Path[0].first);
  965. if (Known != KnownModules.end()) {
  966. // Retrieve the cached top-level module.
  967. Module = Known->second;
  968. } else if (ModuleName == getLangOpts().CurrentModule) {
  969. // This is the module we're building.
  970. Module = PP->getHeaderSearchInfo().getModuleMap().findModule(ModuleName);
  971. Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
  972. } else {
  973. // Search for a module with the given name.
  974. Module = PP->getHeaderSearchInfo().lookupModule(ModuleName);
  975. if (!Module) {
  976. getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
  977. << ModuleName
  978. << SourceRange(ImportLoc, ModuleNameLoc);
  979. ModuleBuildFailed = true;
  980. return ModuleLoadResult();
  981. }
  982. std::string ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(Module);
  983. // If we don't already have an ASTReader, create one now.
  984. if (!ModuleManager) {
  985. if (!hasASTContext())
  986. createASTContext();
  987. // If we're not recursively building a module, check whether we
  988. // need to prune the module cache.
  989. if (getSourceManager().getModuleBuildStack().empty() &&
  990. getHeaderSearchOpts().ModuleCachePruneInterval > 0 &&
  991. getHeaderSearchOpts().ModuleCachePruneAfter > 0) {
  992. pruneModuleCache(getHeaderSearchOpts());
  993. }
  994. std::string Sysroot = getHeaderSearchOpts().Sysroot;
  995. const PreprocessorOptions &PPOpts = getPreprocessorOpts();
  996. ModuleManager = new ASTReader(getPreprocessor(), *Context,
  997. Sysroot.empty() ? "" : Sysroot.c_str(),
  998. PPOpts.DisablePCHValidation,
  999. /*AllowASTWithCompilerErrors=*/false,
  1000. /*AllowConfigurationMismatch=*/false,
  1001. /*ValidateSystemInputs=*/false,
  1002. getFrontendOpts().UseGlobalModuleIndex);
  1003. if (hasASTConsumer()) {
  1004. ModuleManager->setDeserializationListener(
  1005. getASTConsumer().GetASTDeserializationListener());
  1006. getASTContext().setASTMutationListener(
  1007. getASTConsumer().GetASTMutationListener());
  1008. }
  1009. getASTContext().setExternalSource(ModuleManager);
  1010. if (hasSema())
  1011. ModuleManager->InitializeSema(getSema());
  1012. if (hasASTConsumer())
  1013. ModuleManager->StartTranslationUnit(&getASTConsumer());
  1014. }
  1015. // Try to load the module file.
  1016. unsigned ARRFlags = ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing;
  1017. switch (ModuleManager->ReadAST(ModuleFileName, serialization::MK_Module,
  1018. ImportLoc, ARRFlags)) {
  1019. case ASTReader::Success:
  1020. break;
  1021. case ASTReader::OutOfDate:
  1022. case ASTReader::Missing: {
  1023. // The module file is missing or out-of-date. Build it.
  1024. assert(Module && "missing module file");
  1025. // Check whether there is a cycle in the module graph.
  1026. ModuleBuildStack ModPath = getSourceManager().getModuleBuildStack();
  1027. ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end();
  1028. for (; Pos != PosEnd; ++Pos) {
  1029. if (Pos->first == ModuleName)
  1030. break;
  1031. }
  1032. if (Pos != PosEnd) {
  1033. SmallString<256> CyclePath;
  1034. for (; Pos != PosEnd; ++Pos) {
  1035. CyclePath += Pos->first;
  1036. CyclePath += " -> ";
  1037. }
  1038. CyclePath += ModuleName;
  1039. getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
  1040. << ModuleName << CyclePath;
  1041. return ModuleLoadResult();
  1042. }
  1043. // Check whether we have already attempted to build this module (but
  1044. // failed).
  1045. if (getPreprocessorOpts().FailedModules &&
  1046. getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)) {
  1047. getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built)
  1048. << ModuleName
  1049. << SourceRange(ImportLoc, ModuleNameLoc);
  1050. ModuleBuildFailed = true;
  1051. return ModuleLoadResult();
  1052. }
  1053. // Try to compile the module.
  1054. compileModule(*this, ModuleNameLoc, Module, ModuleFileName);
  1055. // Try to read the module file, now that we've compiled it.
  1056. ASTReader::ASTReadResult ReadResult
  1057. = ModuleManager->ReadAST(ModuleFileName,
  1058. serialization::MK_Module, ImportLoc,
  1059. ASTReader::ARR_Missing);
  1060. if (ReadResult != ASTReader::Success) {
  1061. if (ReadResult == ASTReader::Missing) {
  1062. getDiagnostics().Report(ModuleNameLoc,
  1063. Module? diag::err_module_not_built
  1064. : diag::err_module_not_found)
  1065. << ModuleName
  1066. << SourceRange(ImportLoc, ModuleNameLoc);
  1067. }
  1068. if (getPreprocessorOpts().FailedModules)
  1069. getPreprocessorOpts().FailedModules->addFailed(ModuleName);
  1070. KnownModules[Path[0].first] = 0;
  1071. ModuleBuildFailed = true;
  1072. return ModuleLoadResult();
  1073. }
  1074. // Okay, we've rebuilt and now loaded the module.
  1075. break;
  1076. }
  1077. case ASTReader::VersionMismatch:
  1078. case ASTReader::ConfigurationMismatch:
  1079. case ASTReader::HadErrors:
  1080. ModuleLoader::HadFatalFailure = true;
  1081. // FIXME: The ASTReader will already have complained, but can we showhorn
  1082. // that diagnostic information into a more useful form?
  1083. KnownModules[Path[0].first] = 0;
  1084. return ModuleLoadResult();
  1085. case ASTReader::Failure:
  1086. ModuleLoader::HadFatalFailure = true;
  1087. // Already complained, but note now that we failed.
  1088. KnownModules[Path[0].first] = 0;
  1089. ModuleBuildFailed = true;
  1090. return ModuleLoadResult();
  1091. }
  1092. // Cache the result of this top-level module lookup for later.
  1093. Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
  1094. }
  1095. // If we never found the module, fail.
  1096. if (!Module)
  1097. return ModuleLoadResult();
  1098. // Verify that the rest of the module path actually corresponds to
  1099. // a submodule.
  1100. if (Path.size() > 1) {
  1101. for (unsigned I = 1, N = Path.size(); I != N; ++I) {
  1102. StringRef Name = Path[I].first->getName();
  1103. clang::Module *Sub = Module->findSubmodule(Name);
  1104. if (!Sub) {
  1105. // Attempt to perform typo correction to find a module name that works.
  1106. SmallVector<StringRef, 2> Best;
  1107. unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)();
  1108. for (clang::Module::submodule_iterator J = Module->submodule_begin(),
  1109. JEnd = Module->submodule_end();
  1110. J != JEnd; ++J) {
  1111. unsigned ED = Name.edit_distance((*J)->Name,
  1112. /*AllowReplacements=*/true,
  1113. BestEditDistance);
  1114. if (ED <= BestEditDistance) {
  1115. if (ED < BestEditDistance) {
  1116. Best.clear();
  1117. BestEditDistance = ED;
  1118. }
  1119. Best.push_back((*J)->Name);
  1120. }
  1121. }
  1122. // If there was a clear winner, user it.
  1123. if (Best.size() == 1) {
  1124. getDiagnostics().Report(Path[I].second,
  1125. diag::err_no_submodule_suggest)
  1126. << Path[I].first << Module->getFullModuleName() << Best[0]
  1127. << SourceRange(Path[0].second, Path[I-1].second)
  1128. << FixItHint::CreateReplacement(SourceRange(Path[I].second),
  1129. Best[0]);
  1130. Sub = Module->findSubmodule(Best[0]);
  1131. }
  1132. }
  1133. if (!Sub) {
  1134. // No submodule by this name. Complain, and don't look for further
  1135. // submodules.
  1136. getDiagnostics().Report(Path[I].second, diag::err_no_submodule)
  1137. << Path[I].first << Module->getFullModuleName()
  1138. << SourceRange(Path[0].second, Path[I-1].second);
  1139. break;
  1140. }
  1141. Module = Sub;
  1142. }
  1143. }
  1144. // Make the named module visible, if it's not already part of the module
  1145. // we are parsing.
  1146. if (ModuleName != getLangOpts().CurrentModule) {
  1147. if (!Module->IsFromModuleFile) {
  1148. // We have an umbrella header or directory that doesn't actually include
  1149. // all of the headers within the directory it covers. Complain about
  1150. // this missing submodule and recover by forgetting that we ever saw
  1151. // this submodule.
  1152. // FIXME: Should we detect this at module load time? It seems fairly
  1153. // expensive (and rare).
  1154. getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule)
  1155. << Module->getFullModuleName()
  1156. << SourceRange(Path.front().second, Path.back().second);
  1157. return ModuleLoadResult(0, true);
  1158. }
  1159. // Check whether this module is available.
  1160. clang::Module::Requirement Requirement;
  1161. clang::Module::HeaderDirective MissingHeader;
  1162. if (!Module->isAvailable(getLangOpts(), getTarget(), Requirement,
  1163. MissingHeader)) {
  1164. if (MissingHeader.FileNameLoc.isValid()) {
  1165. getDiagnostics().Report(MissingHeader.FileNameLoc,
  1166. diag::err_module_header_missing)
  1167. << MissingHeader.IsUmbrella << MissingHeader.FileName;
  1168. } else {
  1169. getDiagnostics().Report(ImportLoc, diag::err_module_unavailable)
  1170. << Module->getFullModuleName()
  1171. << Requirement.second << Requirement.first
  1172. << SourceRange(Path.front().second, Path.back().second);
  1173. }
  1174. LastModuleImportLoc = ImportLoc;
  1175. LastModuleImportResult = ModuleLoadResult();
  1176. return ModuleLoadResult();
  1177. }
  1178. ModuleManager->makeModuleVisible(Module, Visibility, ImportLoc,
  1179. /*Complain=*/true);
  1180. }
  1181. // Check for any configuration macros that have changed.
  1182. clang::Module *TopModule = Module->getTopLevelModule();
  1183. for (unsigned I = 0, N = TopModule->ConfigMacros.size(); I != N; ++I) {
  1184. checkConfigMacro(getPreprocessor(), TopModule->ConfigMacros[I],
  1185. Module, ImportLoc);
  1186. }
  1187. // If this module import was due to an inclusion directive, create an
  1188. // implicit import declaration to capture it in the AST.
  1189. if (IsInclusionDirective && hasASTContext()) {
  1190. TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
  1191. ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU,
  1192. ImportLoc, Module,
  1193. Path.back().second);
  1194. TU->addDecl(ImportD);
  1195. if (Consumer)
  1196. Consumer->HandleImplicitImportDecl(ImportD);
  1197. }
  1198. LastModuleImportLoc = ImportLoc;
  1199. LastModuleImportResult = ModuleLoadResult(Module, false);
  1200. return LastModuleImportResult;
  1201. }
  1202. void CompilerInstance::makeModuleVisible(Module *Mod,
  1203. Module::NameVisibilityKind Visibility,
  1204. SourceLocation ImportLoc,
  1205. bool Complain){
  1206. ModuleManager->makeModuleVisible(Mod, Visibility, ImportLoc, Complain);
  1207. }