CompilerInstance.cpp 52 KB

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