CompilerInstance.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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(new ForwardingDiagnosticConsumer(
  742. ImportingInstance.getDiagnosticClient()),
  743. /*ShouldOwnClient=*/true,
  744. /*ShouldCloneClient=*/false);
  745. // Note that this module is part of the module build stack, so that we
  746. // can detect cycles in the module graph.
  747. Instance.createFileManager(); // FIXME: Adopt file manager from importer?
  748. Instance.createSourceManager(Instance.getFileManager());
  749. SourceManager &SourceMgr = Instance.getSourceManager();
  750. SourceMgr.setModuleBuildStack(
  751. ImportingInstance.getSourceManager().getModuleBuildStack());
  752. SourceMgr.pushModuleBuildStack(Module->getTopLevelModuleName(),
  753. FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager()));
  754. // Construct a module-generating action.
  755. GenerateModuleAction CreateModuleAction;
  756. // Execute the action to actually build the module in-place. Use a separate
  757. // thread so that we get a stack large enough.
  758. const unsigned ThreadStackSize = 8 << 20;
  759. llvm::CrashRecoveryContext CRC;
  760. CompileModuleMapData Data = { Instance, CreateModuleAction };
  761. CRC.RunSafelyOnThread(&doCompileMapModule, &Data, ThreadStackSize);
  762. // Delete the temporary module map file.
  763. // FIXME: Even though we're executing under crash protection, it would still
  764. // be nice to do this with RemoveFileOnSignal when we can. However, that
  765. // doesn't make sense for all clients, so clean this up manually.
  766. Instance.clearOutputFiles(/*EraseFiles=*/true);
  767. if (!TempModuleMapFileName.empty())
  768. llvm::sys::Path(TempModuleMapFileName).eraseFromDisk();
  769. // We've rebuilt a module. If we're allowed to generate or update the global
  770. // module index, record that fact in the importing compiler instance.
  771. if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) {
  772. ImportingInstance.setBuildGlobalModuleIndex(true);
  773. }
  774. }
  775. /// \brief Diagnose differences between the current definition of the given
  776. /// configuration macro and the definition provided on the command line.
  777. static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
  778. Module *Mod, SourceLocation ImportLoc) {
  779. IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro);
  780. SourceManager &SourceMgr = PP.getSourceManager();
  781. // If this identifier has never had a macro definition, then it could
  782. // not have changed.
  783. if (!Id->hadMacroDefinition())
  784. return;
  785. // If this identifier does not currently have a macro definition,
  786. // check whether it had one on the command line.
  787. if (!Id->hasMacroDefinition()) {
  788. MacroDirective::DefInfo LatestDef =
  789. PP.getMacroDirectiveHistory(Id)->getDefinition();
  790. for (MacroDirective::DefInfo Def = LatestDef; Def;
  791. Def = Def.getPreviousDefinition()) {
  792. FileID FID = SourceMgr.getFileID(Def.getLocation());
  793. if (FID.isInvalid())
  794. continue;
  795. // We only care about the predefines buffer.
  796. if (FID != PP.getPredefinesFileID())
  797. continue;
  798. // This macro was defined on the command line, then #undef'd later.
  799. // Complain.
  800. PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
  801. << true << ConfigMacro << Mod->getFullModuleName();
  802. if (LatestDef.isUndefined())
  803. PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here)
  804. << true;
  805. return;
  806. }
  807. // Okay: no definition in the predefines buffer.
  808. return;
  809. }
  810. // This identifier has a macro definition. Check whether we had a definition
  811. // on the command line.
  812. MacroDirective::DefInfo LatestDef =
  813. PP.getMacroDirectiveHistory(Id)->getDefinition();
  814. MacroDirective::DefInfo PredefinedDef;
  815. for (MacroDirective::DefInfo Def = LatestDef; Def;
  816. Def = Def.getPreviousDefinition()) {
  817. FileID FID = SourceMgr.getFileID(Def.getLocation());
  818. if (FID.isInvalid())
  819. continue;
  820. // We only care about the predefines buffer.
  821. if (FID != PP.getPredefinesFileID())
  822. continue;
  823. PredefinedDef = Def;
  824. break;
  825. }
  826. // If there was no definition for this macro in the predefines buffer,
  827. // complain.
  828. if (!PredefinedDef ||
  829. (!PredefinedDef.getLocation().isValid() &&
  830. PredefinedDef.getUndefLocation().isValid())) {
  831. PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
  832. << false << ConfigMacro << Mod->getFullModuleName();
  833. PP.Diag(LatestDef.getLocation(), diag::note_module_def_undef_here)
  834. << false;
  835. return;
  836. }
  837. // If the current macro definition is the same as the predefined macro
  838. // definition, it's okay.
  839. if (LatestDef.getMacroInfo() == PredefinedDef.getMacroInfo() ||
  840. LatestDef.getMacroInfo()->isIdenticalTo(*PredefinedDef.getMacroInfo(),PP,
  841. /*Syntactically=*/true))
  842. return;
  843. // The macro definitions differ.
  844. PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
  845. << false << ConfigMacro << Mod->getFullModuleName();
  846. PP.Diag(LatestDef.getLocation(), diag::note_module_def_undef_here)
  847. << false;
  848. }
  849. /// \brief Write a new timestamp file with the given path.
  850. static void writeTimestampFile(StringRef TimestampFile) {
  851. std::string ErrorInfo;
  852. llvm::raw_fd_ostream Out(TimestampFile.str().c_str(), ErrorInfo,
  853. llvm::raw_fd_ostream::F_Binary);
  854. }
  855. /// \brief Prune the module cache of modules that haven't been accessed in
  856. /// a long time.
  857. static void pruneModuleCache(const HeaderSearchOptions &HSOpts) {
  858. struct stat StatBuf;
  859. llvm::SmallString<128> TimestampFile;
  860. TimestampFile = HSOpts.ModuleCachePath;
  861. llvm::sys::path::append(TimestampFile, "modules.timestamp");
  862. // Try to stat() the timestamp file.
  863. if (::stat(TimestampFile.c_str(), &StatBuf)) {
  864. // If the timestamp file wasn't there, create one now.
  865. if (errno == ENOENT) {
  866. writeTimestampFile(TimestampFile);
  867. }
  868. return;
  869. }
  870. // Check whether the time stamp is older than our pruning interval.
  871. // If not, do nothing.
  872. time_t TimeStampModTime = StatBuf.st_mtime;
  873. time_t CurrentTime = time(0);
  874. if (CurrentTime - TimeStampModTime <= time_t(HSOpts.ModuleCachePruneInterval))
  875. return;
  876. // Write a new timestamp file so that nobody else attempts to prune.
  877. // There is a benign race condition here, if two Clang instances happen to
  878. // notice at the same time that the timestamp is out-of-date.
  879. writeTimestampFile(TimestampFile);
  880. // Walk the entire module cache, looking for unused module files and module
  881. // indices.
  882. llvm::error_code EC;
  883. SmallString<128> ModuleCachePathNative;
  884. llvm::sys::path::native(HSOpts.ModuleCachePath, ModuleCachePathNative);
  885. for (llvm::sys::fs::directory_iterator
  886. Dir(ModuleCachePathNative.str(), EC), DirEnd;
  887. Dir != DirEnd && !EC; Dir.increment(EC)) {
  888. // If we don't have a directory, there's nothing to look into.
  889. bool IsDirectory;
  890. if (llvm::sys::fs::is_directory(Dir->path(), IsDirectory) || !IsDirectory)
  891. continue;
  892. // Walk all of the files within this directory.
  893. bool RemovedAllFiles = true;
  894. for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd;
  895. File != FileEnd && !EC; File.increment(EC)) {
  896. // We only care about module and global module index files.
  897. if (llvm::sys::path::extension(File->path()) != ".pcm" &&
  898. llvm::sys::path::filename(File->path()) != "modules.idx") {
  899. RemovedAllFiles = false;
  900. continue;
  901. }
  902. // Look at this file. If we can't stat it, there's nothing interesting
  903. // there.
  904. if (::stat(File->path().c_str(), &StatBuf)) {
  905. RemovedAllFiles = false;
  906. continue;
  907. }
  908. // If the file has been used recently enough, leave it there.
  909. time_t FileAccessTime = StatBuf.st_atime;
  910. if (CurrentTime - FileAccessTime <=
  911. time_t(HSOpts.ModuleCachePruneAfter)) {
  912. RemovedAllFiles = false;
  913. continue;
  914. }
  915. // Remove the file.
  916. bool Existed;
  917. if (llvm::sys::fs::remove(File->path(), Existed) || !Existed) {
  918. RemovedAllFiles = false;
  919. }
  920. }
  921. // If we removed all of the files in the directory, remove the directory
  922. // itself.
  923. if (RemovedAllFiles) {
  924. bool Existed;
  925. llvm::sys::fs::remove(Dir->path(), Existed);
  926. }
  927. }
  928. }
  929. ModuleLoadResult
  930. CompilerInstance::loadModule(SourceLocation ImportLoc,
  931. ModuleIdPath Path,
  932. Module::NameVisibilityKind Visibility,
  933. bool IsInclusionDirective) {
  934. // If we've already handled this import, just return the cached result.
  935. // This one-element cache is important to eliminate redundant diagnostics
  936. // when both the preprocessor and parser see the same import declaration.
  937. if (!ImportLoc.isInvalid() && LastModuleImportLoc == ImportLoc) {
  938. // Make the named module visible.
  939. if (LastModuleImportResult)
  940. ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility,
  941. ImportLoc, /*Complain=*/false);
  942. return LastModuleImportResult;
  943. }
  944. // Determine what file we're searching from.
  945. StringRef ModuleName = Path[0].first->getName();
  946. SourceLocation ModuleNameLoc = Path[0].second;
  947. clang::Module *Module = 0;
  948. // If we don't already have information on this module, load the module now.
  949. llvm::DenseMap<const IdentifierInfo *, clang::Module *>::iterator Known
  950. = KnownModules.find(Path[0].first);
  951. if (Known != KnownModules.end()) {
  952. // Retrieve the cached top-level module.
  953. Module = Known->second;
  954. } else if (ModuleName == getLangOpts().CurrentModule) {
  955. // This is the module we're building.
  956. Module = PP->getHeaderSearchInfo().getModuleMap().findModule(ModuleName);
  957. Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
  958. } else {
  959. // Search for a module with the given name.
  960. Module = PP->getHeaderSearchInfo().lookupModule(ModuleName);
  961. std::string ModuleFileName;
  962. if (Module) {
  963. ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(Module);
  964. } else
  965. ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(ModuleName);
  966. // If we don't already have an ASTReader, create one now.
  967. if (!ModuleManager) {
  968. if (!hasASTContext())
  969. createASTContext();
  970. // If we're not recursively building a module, check whether we
  971. // need to prune the module cache.
  972. if (getSourceManager().getModuleBuildStack().empty() &&
  973. getHeaderSearchOpts().ModuleCachePruneInterval > 0 &&
  974. getHeaderSearchOpts().ModuleCachePruneAfter > 0) {
  975. pruneModuleCache(getHeaderSearchOpts());
  976. }
  977. std::string Sysroot = getHeaderSearchOpts().Sysroot;
  978. const PreprocessorOptions &PPOpts = getPreprocessorOpts();
  979. ModuleManager = new ASTReader(getPreprocessor(), *Context,
  980. Sysroot.empty() ? "" : Sysroot.c_str(),
  981. PPOpts.DisablePCHValidation,
  982. /*AllowASTWithCompilerErrors=*/false,
  983. getFrontendOpts().UseGlobalModuleIndex);
  984. if (hasASTConsumer()) {
  985. ModuleManager->setDeserializationListener(
  986. getASTConsumer().GetASTDeserializationListener());
  987. getASTContext().setASTMutationListener(
  988. getASTConsumer().GetASTMutationListener());
  989. }
  990. OwningPtr<ExternalASTSource> Source;
  991. Source.reset(ModuleManager);
  992. getASTContext().setExternalSource(Source);
  993. if (hasSema())
  994. ModuleManager->InitializeSema(getSema());
  995. if (hasASTConsumer())
  996. ModuleManager->StartTranslationUnit(&getASTConsumer());
  997. }
  998. // Try to load the module file.
  999. unsigned ARRFlags = ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing;
  1000. switch (ModuleManager->ReadAST(ModuleFileName, serialization::MK_Module,
  1001. ImportLoc, ARRFlags)) {
  1002. case ASTReader::Success:
  1003. break;
  1004. case ASTReader::OutOfDate: {
  1005. // The module file is out-of-date. Remove it, then rebuild it.
  1006. bool Existed;
  1007. llvm::sys::fs::remove(ModuleFileName, Existed);
  1008. }
  1009. // Fall through to build the module again.
  1010. case ASTReader::Missing: {
  1011. // The module file is (now) missing. Build it.
  1012. // If we don't have a module, we don't know how to build the module file.
  1013. // Complain and return.
  1014. if (!Module) {
  1015. getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
  1016. << ModuleName
  1017. << SourceRange(ImportLoc, ModuleNameLoc);
  1018. ModuleBuildFailed = true;
  1019. return ModuleLoadResult();
  1020. }
  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. // FIXME: The ASTReader will already have complained, but can we showhorn
  1077. // that diagnostic information into a more useful form?
  1078. KnownModules[Path[0].first] = 0;
  1079. return ModuleLoadResult();
  1080. case ASTReader::Failure:
  1081. // Already complained, but note now that we failed.
  1082. KnownModules[Path[0].first] = 0;
  1083. ModuleBuildFailed = true;
  1084. return ModuleLoadResult();
  1085. }
  1086. if (!Module) {
  1087. // If we loaded the module directly, without finding a module map first,
  1088. // we'll have loaded the module's information from the module itself.
  1089. Module = PP->getHeaderSearchInfo().getModuleMap()
  1090. .findModule((Path[0].first->getName()));
  1091. }
  1092. // Cache the result of this top-level module lookup for later.
  1093. Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
  1094. }
  1095. // If we never found the module, fail.
  1096. if (!Module)
  1097. return ModuleLoadResult();
  1098. // Verify that the rest of the module path actually corresponds to
  1099. // a submodule.
  1100. if (Path.size() > 1) {
  1101. for (unsigned I = 1, N = Path.size(); I != N; ++I) {
  1102. StringRef Name = Path[I].first->getName();
  1103. clang::Module *Sub = Module->findSubmodule(Name);
  1104. if (!Sub) {
  1105. // Attempt to perform typo correction to find a module name that works.
  1106. SmallVector<StringRef, 2> Best;
  1107. unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)();
  1108. for (clang::Module::submodule_iterator J = Module->submodule_begin(),
  1109. JEnd = Module->submodule_end();
  1110. J != JEnd; ++J) {
  1111. unsigned ED = Name.edit_distance((*J)->Name,
  1112. /*AllowReplacements=*/true,
  1113. BestEditDistance);
  1114. if (ED <= BestEditDistance) {
  1115. if (ED < BestEditDistance) {
  1116. Best.clear();
  1117. BestEditDistance = ED;
  1118. }
  1119. Best.push_back((*J)->Name);
  1120. }
  1121. }
  1122. // If there was a clear winner, user it.
  1123. if (Best.size() == 1) {
  1124. getDiagnostics().Report(Path[I].second,
  1125. diag::err_no_submodule_suggest)
  1126. << Path[I].first << Module->getFullModuleName() << Best[0]
  1127. << SourceRange(Path[0].second, Path[I-1].second)
  1128. << FixItHint::CreateReplacement(SourceRange(Path[I].second),
  1129. Best[0]);
  1130. Sub = Module->findSubmodule(Best[0]);
  1131. }
  1132. }
  1133. if (!Sub) {
  1134. // No submodule by this name. Complain, and don't look for further
  1135. // submodules.
  1136. getDiagnostics().Report(Path[I].second, diag::err_no_submodule)
  1137. << Path[I].first << Module->getFullModuleName()
  1138. << SourceRange(Path[0].second, Path[I-1].second);
  1139. break;
  1140. }
  1141. Module = Sub;
  1142. }
  1143. }
  1144. // Make the named module visible, if it's not already part of the module
  1145. // we are parsing.
  1146. if (ModuleName != getLangOpts().CurrentModule) {
  1147. if (!Module->IsFromModuleFile) {
  1148. // We have an umbrella header or directory that doesn't actually include
  1149. // all of the headers within the directory it covers. Complain about
  1150. // this missing submodule and recover by forgetting that we ever saw
  1151. // this submodule.
  1152. // FIXME: Should we detect this at module load time? It seems fairly
  1153. // expensive (and rare).
  1154. getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule)
  1155. << Module->getFullModuleName()
  1156. << SourceRange(Path.front().second, Path.back().second);
  1157. return ModuleLoadResult(0, true);
  1158. }
  1159. // Check whether this module is available.
  1160. StringRef Feature;
  1161. if (!Module->isAvailable(getLangOpts(), getTarget(), Feature)) {
  1162. getDiagnostics().Report(ImportLoc, diag::err_module_unavailable)
  1163. << Module->getFullModuleName()
  1164. << Feature
  1165. << SourceRange(Path.front().second, Path.back().second);
  1166. LastModuleImportLoc = ImportLoc;
  1167. LastModuleImportResult = ModuleLoadResult();
  1168. return ModuleLoadResult();
  1169. }
  1170. ModuleManager->makeModuleVisible(Module, Visibility, ImportLoc,
  1171. /*Complain=*/true);
  1172. }
  1173. // Check for any configuration macros that have changed.
  1174. clang::Module *TopModule = Module->getTopLevelModule();
  1175. for (unsigned I = 0, N = TopModule->ConfigMacros.size(); I != N; ++I) {
  1176. checkConfigMacro(getPreprocessor(), TopModule->ConfigMacros[I],
  1177. Module, ImportLoc);
  1178. }
  1179. // If this module import was due to an inclusion directive, create an
  1180. // implicit import declaration to capture it in the AST.
  1181. if (IsInclusionDirective && hasASTContext()) {
  1182. TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
  1183. ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU,
  1184. ImportLoc, Module,
  1185. Path.back().second);
  1186. TU->addDecl(ImportD);
  1187. if (Consumer)
  1188. Consumer->HandleImplicitImportDecl(ImportD);
  1189. }
  1190. LastModuleImportLoc = ImportLoc;
  1191. LastModuleImportResult = ModuleLoadResult(Module, false);
  1192. return LastModuleImportResult;
  1193. }
  1194. void CompilerInstance::makeModuleVisible(Module *Mod,
  1195. Module::NameVisibilityKind Visibility,
  1196. SourceLocation ImportLoc,
  1197. bool Complain){
  1198. ModuleManager->makeModuleVisible(Mod, Visibility, ImportLoc, Complain);
  1199. }