CompilerInstance.cpp 52 KB

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