CompilerInstance.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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/Sema/Sema.h"
  11. #include "clang/AST/ASTConsumer.h"
  12. #include "clang/AST/ASTContext.h"
  13. #include "clang/AST/Decl.h"
  14. #include "clang/Basic/Diagnostic.h"
  15. #include "clang/Basic/FileManager.h"
  16. #include "clang/Basic/SourceManager.h"
  17. #include "clang/Basic/TargetInfo.h"
  18. #include "clang/Basic/Version.h"
  19. #include "clang/Lex/HeaderSearch.h"
  20. #include "clang/Lex/Preprocessor.h"
  21. #include "clang/Lex/PTHManager.h"
  22. #include "clang/Frontend/ChainedDiagnosticConsumer.h"
  23. #include "clang/Frontend/FrontendAction.h"
  24. #include "clang/Frontend/FrontendActions.h"
  25. #include "clang/Frontend/FrontendDiagnostic.h"
  26. #include "clang/Frontend/LogDiagnosticPrinter.h"
  27. #include "clang/Frontend/SerializedDiagnosticPrinter.h"
  28. #include "clang/Frontend/TextDiagnosticPrinter.h"
  29. #include "clang/Frontend/VerifyDiagnosticConsumer.h"
  30. #include "clang/Frontend/Utils.h"
  31. #include "clang/Serialization/ASTReader.h"
  32. #include "clang/Sema/CodeCompleteConsumer.h"
  33. #include "llvm/Support/FileSystem.h"
  34. #include "llvm/Support/MemoryBuffer.h"
  35. #include "llvm/Support/raw_ostream.h"
  36. #include "llvm/ADT/Statistic.h"
  37. #include "llvm/Support/Timer.h"
  38. #include "llvm/Support/Host.h"
  39. #include "llvm/Support/LockFileManager.h"
  40. #include "llvm/Support/Path.h"
  41. #include "llvm/Support/Program.h"
  42. #include "llvm/Support/Signals.h"
  43. #include "llvm/Support/system_error.h"
  44. #include "llvm/Support/CrashRecoveryContext.h"
  45. #include "llvm/Config/config.h"
  46. using namespace clang;
  47. CompilerInstance::CompilerInstance()
  48. : Invocation(new CompilerInvocation()), ModuleManager(0) {
  49. }
  50. CompilerInstance::~CompilerInstance() {
  51. }
  52. void CompilerInstance::setInvocation(CompilerInvocation *Value) {
  53. Invocation = Value;
  54. }
  55. void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) {
  56. Diagnostics = Value;
  57. }
  58. void CompilerInstance::setTarget(TargetInfo *Value) {
  59. Target = Value;
  60. }
  61. void CompilerInstance::setFileManager(FileManager *Value) {
  62. FileMgr = Value;
  63. }
  64. void CompilerInstance::setSourceManager(SourceManager *Value) {
  65. SourceMgr = Value;
  66. }
  67. void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; }
  68. void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; }
  69. void CompilerInstance::setSema(Sema *S) {
  70. TheSema.reset(S);
  71. }
  72. void CompilerInstance::setASTConsumer(ASTConsumer *Value) {
  73. Consumer.reset(Value);
  74. }
  75. void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
  76. CompletionConsumer.reset(Value);
  77. }
  78. // Diagnostics
  79. static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
  80. unsigned argc, const char* const *argv,
  81. DiagnosticsEngine &Diags) {
  82. std::string ErrorInfo;
  83. llvm::OwningPtr<raw_ostream> OS(
  84. new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo));
  85. if (!ErrorInfo.empty()) {
  86. Diags.Report(diag::err_fe_unable_to_open_logfile)
  87. << DiagOpts.DumpBuildInformation << ErrorInfo;
  88. return;
  89. }
  90. (*OS) << "clang -cc1 command line arguments: ";
  91. for (unsigned i = 0; i != argc; ++i)
  92. (*OS) << argv[i] << ' ';
  93. (*OS) << '\n';
  94. // Chain in a diagnostic client which will log the diagnostics.
  95. DiagnosticConsumer *Logger =
  96. new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true);
  97. Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger));
  98. }
  99. static void SetUpDiagnosticLog(const DiagnosticOptions &DiagOpts,
  100. const CodeGenOptions *CodeGenOpts,
  101. DiagnosticsEngine &Diags) {
  102. std::string ErrorInfo;
  103. bool OwnsStream = false;
  104. raw_ostream *OS = &llvm::errs();
  105. if (DiagOpts.DiagnosticLogFile != "-") {
  106. // Create the output stream.
  107. llvm::raw_fd_ostream *FileOS(
  108. new llvm::raw_fd_ostream(DiagOpts.DiagnosticLogFile.c_str(),
  109. ErrorInfo, llvm::raw_fd_ostream::F_Append));
  110. if (!ErrorInfo.empty()) {
  111. Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
  112. << DiagOpts.DumpBuildInformation << ErrorInfo;
  113. } else {
  114. FileOS->SetUnbuffered();
  115. FileOS->SetUseAtomicWrites(true);
  116. OS = FileOS;
  117. OwnsStream = true;
  118. }
  119. }
  120. // Chain in the diagnostic client which will log the diagnostics.
  121. LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts,
  122. OwnsStream);
  123. if (CodeGenOpts)
  124. Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
  125. Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger));
  126. }
  127. static void SetupSerializedDiagnostics(const DiagnosticOptions &DiagOpts,
  128. DiagnosticsEngine &Diags,
  129. StringRef OutputFile) {
  130. std::string ErrorInfo;
  131. llvm::OwningPtr<llvm::raw_fd_ostream> OS;
  132. OS.reset(new llvm::raw_fd_ostream(OutputFile.str().c_str(), ErrorInfo,
  133. llvm::raw_fd_ostream::F_Binary));
  134. if (!ErrorInfo.empty()) {
  135. Diags.Report(diag::warn_fe_serialized_diag_failure)
  136. << OutputFile << ErrorInfo;
  137. return;
  138. }
  139. DiagnosticConsumer *SerializedConsumer =
  140. clang::serialized_diags::create(OS.take(), DiagOpts);
  141. Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(),
  142. SerializedConsumer));
  143. }
  144. void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv,
  145. DiagnosticConsumer *Client,
  146. bool ShouldOwnClient,
  147. bool ShouldCloneClient) {
  148. Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client,
  149. ShouldOwnClient, ShouldCloneClient,
  150. &getCodeGenOpts());
  151. }
  152. llvm::IntrusiveRefCntPtr<DiagnosticsEngine>
  153. CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
  154. int Argc, const char* const *Argv,
  155. DiagnosticConsumer *Client,
  156. bool ShouldOwnClient,
  157. bool ShouldCloneClient,
  158. const CodeGenOptions *CodeGenOpts) {
  159. llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
  160. llvm::IntrusiveRefCntPtr<DiagnosticsEngine>
  161. Diags(new DiagnosticsEngine(DiagID));
  162. // Create the diagnostic client for reporting errors or for
  163. // implementing -verify.
  164. if (Client) {
  165. if (ShouldCloneClient)
  166. Diags->setClient(Client->clone(*Diags), ShouldOwnClient);
  167. else
  168. Diags->setClient(Client, ShouldOwnClient);
  169. } else
  170. Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
  171. // Chain in -verify checker, if requested.
  172. if (Opts.VerifyDiagnostics)
  173. Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
  174. // Chain in -diagnostic-log-file dumper, if requested.
  175. if (!Opts.DiagnosticLogFile.empty())
  176. SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
  177. if (!Opts.DumpBuildInformation.empty())
  178. SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
  179. if (!Opts.DiagnosticSerializationFile.empty())
  180. SetupSerializedDiagnostics(Opts, *Diags,
  181. Opts.DiagnosticSerializationFile);
  182. // Configure our handling of diagnostics.
  183. ProcessWarningOptions(*Diags, Opts);
  184. return Diags;
  185. }
  186. // File Manager
  187. void CompilerInstance::createFileManager() {
  188. FileMgr = new FileManager(getFileSystemOpts());
  189. }
  190. // Source Manager
  191. void CompilerInstance::createSourceManager(FileManager &FileMgr) {
  192. SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
  193. }
  194. // Preprocessor
  195. void CompilerInstance::createPreprocessor() {
  196. const PreprocessorOptions &PPOpts = getPreprocessorOpts();
  197. // Create a PTH manager if we are using some form of a token cache.
  198. PTHManager *PTHMgr = 0;
  199. if (!PPOpts.TokenCache.empty())
  200. PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics());
  201. // Create the Preprocessor.
  202. HeaderSearch *HeaderInfo = new HeaderSearch(getFileManager(),
  203. getDiagnostics(),
  204. getLangOpts(),
  205. &getTarget());
  206. PP = new Preprocessor(getDiagnostics(), getLangOpts(), &getTarget(),
  207. getSourceManager(), *HeaderInfo, *this, PTHMgr,
  208. /*OwnsHeaderSearch=*/true);
  209. // Note that this is different then passing PTHMgr to Preprocessor's ctor.
  210. // That argument is used as the IdentifierInfoLookup argument to
  211. // IdentifierTable's ctor.
  212. if (PTHMgr) {
  213. PTHMgr->setPreprocessor(&*PP);
  214. PP->setPTHManager(PTHMgr);
  215. }
  216. if (PPOpts.DetailedRecord)
  217. PP->createPreprocessingRecord(
  218. PPOpts.DetailedRecordIncludesNestedMacroExpansions);
  219. InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts());
  220. // Set up the module path, including the hash for the
  221. // module-creation options.
  222. llvm::SmallString<256> SpecificModuleCache(
  223. getHeaderSearchOpts().ModuleCachePath);
  224. if (!getHeaderSearchOpts().DisableModuleHash)
  225. llvm::sys::path::append(SpecificModuleCache,
  226. getInvocation().getModuleHash());
  227. PP->getHeaderSearchInfo().setModuleCachePath(SpecificModuleCache);
  228. // Handle generating dependencies, if requested.
  229. const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();
  230. if (!DepOpts.OutputFile.empty())
  231. AttachDependencyFileGen(*PP, DepOpts);
  232. if (!DepOpts.GraphvizOutputFile.empty())
  233. AttachDependencyGraphGen(*PP, DepOpts.GraphvizOutputFile,
  234. getHeaderSearchOpts().Sysroot);
  235. // Handle generating header include information, if requested.
  236. if (DepOpts.ShowHeaderIncludes)
  237. AttachHeaderIncludeGen(*PP);
  238. if (!DepOpts.HeaderIncludeOutputFile.empty()) {
  239. StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
  240. if (OutputPath == "-")
  241. OutputPath = "";
  242. AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath,
  243. /*ShowDepth=*/false);
  244. }
  245. }
  246. // ASTContext
  247. void CompilerInstance::createASTContext() {
  248. Preprocessor &PP = getPreprocessor();
  249. Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
  250. &getTarget(), PP.getIdentifierTable(),
  251. PP.getSelectorTable(), PP.getBuiltinInfo(),
  252. /*size_reserve=*/ 0);
  253. }
  254. // ExternalASTSource
  255. void CompilerInstance::createPCHExternalASTSource(StringRef Path,
  256. bool DisablePCHValidation,
  257. bool DisableStatCache,
  258. void *DeserializationListener){
  259. llvm::OwningPtr<ExternalASTSource> Source;
  260. bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
  261. Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
  262. DisablePCHValidation,
  263. DisableStatCache,
  264. getPreprocessor(), getASTContext(),
  265. DeserializationListener,
  266. Preamble));
  267. ModuleManager = static_cast<ASTReader*>(Source.get());
  268. getASTContext().setExternalSource(Source);
  269. }
  270. ExternalASTSource *
  271. CompilerInstance::createPCHExternalASTSource(StringRef Path,
  272. const std::string &Sysroot,
  273. bool DisablePCHValidation,
  274. bool DisableStatCache,
  275. Preprocessor &PP,
  276. ASTContext &Context,
  277. void *DeserializationListener,
  278. bool Preamble) {
  279. llvm::OwningPtr<ASTReader> Reader;
  280. Reader.reset(new ASTReader(PP, Context,
  281. Sysroot.empty() ? "" : Sysroot.c_str(),
  282. DisablePCHValidation, DisableStatCache));
  283. Reader->setDeserializationListener(
  284. static_cast<ASTDeserializationListener *>(DeserializationListener));
  285. switch (Reader->ReadAST(Path,
  286. Preamble ? serialization::MK_Preamble
  287. : serialization::MK_PCH)) {
  288. case ASTReader::Success:
  289. // Set the predefines buffer as suggested by the PCH reader. Typically, the
  290. // predefines buffer will be empty.
  291. PP.setPredefines(Reader->getSuggestedPredefines());
  292. return Reader.take();
  293. case ASTReader::Failure:
  294. // Unrecoverable failure: don't even try to process the input file.
  295. break;
  296. case ASTReader::IgnorePCH:
  297. // No suitable PCH file could be found. Return an error.
  298. break;
  299. }
  300. return 0;
  301. }
  302. // Code Completion
  303. static bool EnableCodeCompletion(Preprocessor &PP,
  304. const std::string &Filename,
  305. unsigned Line,
  306. unsigned Column) {
  307. // Tell the source manager to chop off the given file at a specific
  308. // line and column.
  309. const FileEntry *Entry = PP.getFileManager().getFile(Filename);
  310. if (!Entry) {
  311. PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
  312. << Filename;
  313. return true;
  314. }
  315. // Truncate the named file at the given line/column.
  316. PP.SetCodeCompletionPoint(Entry, Line, Column);
  317. return false;
  318. }
  319. void CompilerInstance::createCodeCompletionConsumer() {
  320. const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
  321. if (!CompletionConsumer) {
  322. CompletionConsumer.reset(
  323. createCodeCompletionConsumer(getPreprocessor(),
  324. Loc.FileName, Loc.Line, Loc.Column,
  325. getFrontendOpts().ShowMacrosInCodeCompletion,
  326. getFrontendOpts().ShowCodePatternsInCodeCompletion,
  327. getFrontendOpts().ShowGlobalSymbolsInCodeCompletion,
  328. llvm::outs()));
  329. if (!CompletionConsumer)
  330. return;
  331. } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
  332. Loc.Line, Loc.Column)) {
  333. CompletionConsumer.reset();
  334. return;
  335. }
  336. if (CompletionConsumer->isOutputBinary() &&
  337. llvm::sys::Program::ChangeStdoutToBinary()) {
  338. getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
  339. CompletionConsumer.reset();
  340. }
  341. }
  342. void CompilerInstance::createFrontendTimer() {
  343. FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
  344. }
  345. CodeCompleteConsumer *
  346. CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
  347. const std::string &Filename,
  348. unsigned Line,
  349. unsigned Column,
  350. bool ShowMacros,
  351. bool ShowCodePatterns,
  352. bool ShowGlobals,
  353. raw_ostream &OS) {
  354. if (EnableCodeCompletion(PP, Filename, Line, Column))
  355. return 0;
  356. // Set up the creation routine for code-completion.
  357. return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
  358. ShowGlobals, OS);
  359. }
  360. void CompilerInstance::createSema(TranslationUnitKind TUKind,
  361. CodeCompleteConsumer *CompletionConsumer) {
  362. TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
  363. TUKind, CompletionConsumer));
  364. }
  365. // Output Files
  366. void CompilerInstance::addOutputFile(const OutputFile &OutFile) {
  367. assert(OutFile.OS && "Attempt to add empty stream to output list!");
  368. OutputFiles.push_back(OutFile);
  369. }
  370. void CompilerInstance::clearOutputFiles(bool EraseFiles) {
  371. for (std::list<OutputFile>::iterator
  372. it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
  373. delete it->OS;
  374. if (!it->TempFilename.empty()) {
  375. if (EraseFiles) {
  376. bool existed;
  377. llvm::sys::fs::remove(it->TempFilename, existed);
  378. } else {
  379. llvm::SmallString<128> NewOutFile(it->Filename);
  380. // If '-working-directory' was passed, the output filename should be
  381. // relative to that.
  382. FileMgr->FixupRelativePath(NewOutFile);
  383. if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename,
  384. NewOutFile.str())) {
  385. getDiagnostics().Report(diag::err_fe_unable_to_rename_temp)
  386. << it->TempFilename << it->Filename << ec.message();
  387. bool existed;
  388. llvm::sys::fs::remove(it->TempFilename, existed);
  389. }
  390. }
  391. } else if (!it->Filename.empty() && EraseFiles)
  392. llvm::sys::Path(it->Filename).eraseFromDisk();
  393. }
  394. OutputFiles.clear();
  395. }
  396. llvm::raw_fd_ostream *
  397. CompilerInstance::createDefaultOutputFile(bool Binary,
  398. StringRef InFile,
  399. StringRef Extension) {
  400. return createOutputFile(getFrontendOpts().OutputFile, Binary,
  401. /*RemoveFileOnSignal=*/true, InFile, Extension);
  402. }
  403. llvm::raw_fd_ostream *
  404. CompilerInstance::createOutputFile(StringRef OutputPath,
  405. bool Binary, bool RemoveFileOnSignal,
  406. StringRef InFile,
  407. StringRef Extension,
  408. bool UseTemporary) {
  409. std::string Error, OutputPathName, TempPathName;
  410. llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
  411. RemoveFileOnSignal,
  412. InFile, Extension,
  413. UseTemporary,
  414. &OutputPathName,
  415. &TempPathName);
  416. if (!OS) {
  417. getDiagnostics().Report(diag::err_fe_unable_to_open_output)
  418. << OutputPath << Error;
  419. return 0;
  420. }
  421. // Add the output file -- but don't try to remove "-", since this means we are
  422. // using stdin.
  423. addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
  424. TempPathName, OS));
  425. return OS;
  426. }
  427. llvm::raw_fd_ostream *
  428. CompilerInstance::createOutputFile(StringRef OutputPath,
  429. std::string &Error,
  430. bool Binary,
  431. bool RemoveFileOnSignal,
  432. StringRef InFile,
  433. StringRef Extension,
  434. bool UseTemporary,
  435. std::string *ResultPathName,
  436. std::string *TempPathName) {
  437. std::string OutFile, TempFile;
  438. if (!OutputPath.empty()) {
  439. OutFile = OutputPath;
  440. } else if (InFile == "-") {
  441. OutFile = "-";
  442. } else if (!Extension.empty()) {
  443. llvm::sys::Path Path(InFile);
  444. Path.eraseSuffix();
  445. Path.appendSuffix(Extension);
  446. OutFile = Path.str();
  447. } else {
  448. OutFile = "-";
  449. }
  450. llvm::OwningPtr<llvm::raw_fd_ostream> OS;
  451. std::string OSFile;
  452. if (UseTemporary && OutFile != "-") {
  453. llvm::sys::Path OutPath(OutFile);
  454. // Only create the temporary if we can actually write to OutPath, otherwise
  455. // we want to fail early.
  456. bool Exists;
  457. if ((llvm::sys::fs::exists(OutPath.str(), Exists) || !Exists) ||
  458. (OutPath.isRegularFile() && OutPath.canWrite())) {
  459. // Create a temporary file.
  460. llvm::SmallString<128> TempPath;
  461. TempPath = OutFile;
  462. TempPath += "-%%%%%%%%";
  463. int fd;
  464. if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath,
  465. /*makeAbsolute=*/false) == llvm::errc::success) {
  466. OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
  467. OSFile = TempFile = TempPath.str();
  468. }
  469. }
  470. }
  471. if (!OS) {
  472. OSFile = OutFile;
  473. OS.reset(
  474. new llvm::raw_fd_ostream(OSFile.c_str(), Error,
  475. (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
  476. if (!Error.empty())
  477. return 0;
  478. }
  479. // Make sure the out stream file gets removed if we crash.
  480. if (RemoveFileOnSignal)
  481. llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile));
  482. if (ResultPathName)
  483. *ResultPathName = OutFile;
  484. if (TempPathName)
  485. *TempPathName = TempFile;
  486. return OS.take();
  487. }
  488. // Initialization Utilities
  489. bool CompilerInstance::InitializeSourceManager(StringRef InputFile,
  490. SrcMgr::CharacteristicKind Kind){
  491. return InitializeSourceManager(InputFile, Kind, getDiagnostics(),
  492. getFileManager(), getSourceManager(),
  493. getFrontendOpts());
  494. }
  495. bool CompilerInstance::InitializeSourceManager(StringRef InputFile,
  496. SrcMgr::CharacteristicKind Kind,
  497. DiagnosticsEngine &Diags,
  498. FileManager &FileMgr,
  499. SourceManager &SourceMgr,
  500. const FrontendOptions &Opts) {
  501. // Figure out where to get and map in the main file.
  502. if (InputFile != "-") {
  503. const FileEntry *File = FileMgr.getFile(InputFile);
  504. if (!File) {
  505. Diags.Report(diag::err_fe_error_reading) << InputFile;
  506. return false;
  507. }
  508. SourceMgr.createMainFileID(File, Kind);
  509. } else {
  510. llvm::OwningPtr<llvm::MemoryBuffer> SB;
  511. if (llvm::MemoryBuffer::getSTDIN(SB)) {
  512. // FIXME: Give ec.message() in this diag.
  513. Diags.Report(diag::err_fe_error_reading_stdin);
  514. return false;
  515. }
  516. const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
  517. SB->getBufferSize(), 0);
  518. SourceMgr.createMainFileID(File, Kind);
  519. SourceMgr.overrideFileContents(File, SB.take());
  520. }
  521. assert(!SourceMgr.getMainFileID().isInvalid() &&
  522. "Couldn't establish MainFileID!");
  523. return true;
  524. }
  525. // High-Level Operations
  526. bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
  527. assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
  528. assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
  529. assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
  530. // FIXME: Take this as an argument, once all the APIs we used have moved to
  531. // taking it as an input instead of hard-coding llvm::errs.
  532. raw_ostream &OS = llvm::errs();
  533. // Create the target instance.
  534. setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
  535. if (!hasTarget())
  536. return false;
  537. // Inform the target of the language options.
  538. //
  539. // FIXME: We shouldn't need to do this, the target should be immutable once
  540. // created. This complexity should be lifted elsewhere.
  541. getTarget().setForcedLangOptions(getLangOpts());
  542. // Validate/process some options.
  543. if (getHeaderSearchOpts().Verbose)
  544. OS << "clang -cc1 version " CLANG_VERSION_STRING
  545. << " based upon " << PACKAGE_STRING
  546. << " default target " << llvm::sys::getDefaultTargetTriple() << "\n";
  547. if (getFrontendOpts().ShowTimers)
  548. createFrontendTimer();
  549. if (getFrontendOpts().ShowStats)
  550. llvm::EnableStatistics();
  551. for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
  552. // Reset the ID tables if we are reusing the SourceManager.
  553. if (hasSourceManager())
  554. getSourceManager().clearIDTables();
  555. if (Act.BeginSourceFile(*this, getFrontendOpts().Inputs[i])) {
  556. Act.Execute();
  557. Act.EndSourceFile();
  558. }
  559. }
  560. // Notify the diagnostic client that all files were processed.
  561. getDiagnostics().getClient()->finish();
  562. if (getDiagnosticOpts().ShowCarets) {
  563. // We can have multiple diagnostics sharing one diagnostic client.
  564. // Get the total number of warnings/errors from the client.
  565. unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
  566. unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
  567. if (NumWarnings)
  568. OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
  569. if (NumWarnings && NumErrors)
  570. OS << " and ";
  571. if (NumErrors)
  572. OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
  573. if (NumWarnings || NumErrors)
  574. OS << " generated.\n";
  575. }
  576. if (getFrontendOpts().ShowStats && hasFileManager()) {
  577. getFileManager().PrintStats();
  578. OS << "\n";
  579. }
  580. return !getDiagnostics().getClient()->getNumErrors();
  581. }
  582. /// \brief Determine the appropriate source input kind based on language
  583. /// options.
  584. static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) {
  585. if (LangOpts.OpenCL)
  586. return IK_OpenCL;
  587. if (LangOpts.CUDA)
  588. return IK_CUDA;
  589. if (LangOpts.ObjC1)
  590. return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC;
  591. return LangOpts.CPlusPlus? IK_CXX : IK_C;
  592. }
  593. namespace {
  594. struct CompileModuleMapData {
  595. CompilerInstance &Instance;
  596. GenerateModuleAction &CreateModuleAction;
  597. };
  598. }
  599. /// \brief Helper function that executes the module-generating action under
  600. /// a crash recovery context.
  601. static void doCompileMapModule(void *UserData) {
  602. CompileModuleMapData &Data
  603. = *reinterpret_cast<CompileModuleMapData *>(UserData);
  604. Data.Instance.ExecuteAction(Data.CreateModuleAction);
  605. }
  606. /// \brief Compile a module file for the given module, using the options
  607. /// provided by the importing compiler instance.
  608. static void compileModule(CompilerInstance &ImportingInstance,
  609. Module *Module,
  610. StringRef ModuleFileName) {
  611. llvm::LockFileManager Locked(ModuleFileName);
  612. switch (Locked) {
  613. case llvm::LockFileManager::LFS_Error:
  614. return;
  615. case llvm::LockFileManager::LFS_Owned:
  616. // We're responsible for building the module ourselves. Do so below.
  617. break;
  618. case llvm::LockFileManager::LFS_Shared:
  619. // Someone else is responsible for building the module. Wait for them to
  620. // finish.
  621. Locked.waitForUnlock();
  622. break;
  623. }
  624. ModuleMap &ModMap
  625. = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap();
  626. // Construct a compiler invocation for creating this module.
  627. llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation
  628. (new CompilerInvocation(ImportingInstance.getInvocation()));
  629. PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
  630. // For any options that aren't intended to affect how a module is built,
  631. // reset them to their default values.
  632. Invocation->getLangOpts()->resetNonModularOptions();
  633. PPOpts.resetNonModularOptions();
  634. // Note the name of the module we're building.
  635. Invocation->getLangOpts()->CurrentModule = Module->getTopLevelModuleName();
  636. // Note that this module is part of the module build path, so that we
  637. // can detect cycles in the module graph.
  638. PPOpts.ModuleBuildPath.push_back(Module->getTopLevelModuleName());
  639. // If there is a module map file, build the module using the module map.
  640. // Set up the inputs/outputs so that we build the module from its umbrella
  641. // header.
  642. FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
  643. FrontendOpts.OutputFile = ModuleFileName.str();
  644. FrontendOpts.DisableFree = false;
  645. FrontendOpts.Inputs.clear();
  646. InputKind IK = getSourceInputKindFromOptions(*Invocation->getLangOpts());
  647. // Get or create the module map that we'll use to build this module.
  648. llvm::SmallString<128> TempModuleMapFileName;
  649. if (const FileEntry *ModuleMapFile
  650. = ModMap.getContainingModuleMapFile(Module)) {
  651. // Use the module map where this module resides.
  652. FrontendOpts.Inputs.push_back(FrontendInputFile(ModuleMapFile->getName(),
  653. IK));
  654. } else {
  655. // Create a temporary module map file.
  656. TempModuleMapFileName = Module->Name;
  657. TempModuleMapFileName += "-%%%%%%%%.map";
  658. int FD;
  659. if (llvm::sys::fs::unique_file(TempModuleMapFileName.str(), FD,
  660. TempModuleMapFileName,
  661. /*makeAbsolute=*/true)
  662. != llvm::errc::success) {
  663. ImportingInstance.getDiagnostics().Report(diag::err_module_map_temp_file)
  664. << TempModuleMapFileName;
  665. return;
  666. }
  667. // Print the module map to this file.
  668. llvm::raw_fd_ostream OS(FD, /*shouldClose=*/true);
  669. Module->print(OS);
  670. FrontendOpts.Inputs.push_back(
  671. FrontendInputFile(TempModuleMapFileName.str().str(), IK));
  672. }
  673. // Don't free the remapped file buffers; they are owned by our caller.
  674. PPOpts.RetainRemappedFileBuffers = true;
  675. Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
  676. assert(ImportingInstance.getInvocation().getModuleHash() ==
  677. Invocation->getModuleHash() && "Module hash mismatch!");
  678. // Construct a compiler instance that will be used to actually create the
  679. // module.
  680. CompilerInstance Instance;
  681. Instance.setInvocation(&*Invocation);
  682. Instance.createDiagnostics(/*argc=*/0, /*argv=*/0,
  683. &ImportingInstance.getDiagnosticClient(),
  684. /*ShouldOwnClient=*/true,
  685. /*ShouldCloneClient=*/true);
  686. // Construct a module-generating action.
  687. GenerateModuleAction CreateModuleAction;
  688. // Execute the action to actually build the module in-place. Use a separate
  689. // thread so that we get a stack large enough.
  690. const unsigned ThreadStackSize = 8 << 20;
  691. llvm::CrashRecoveryContext CRC;
  692. CompileModuleMapData Data = { Instance, CreateModuleAction };
  693. CRC.RunSafelyOnThread(&doCompileMapModule, &Data, ThreadStackSize);
  694. // Delete the temporary module map file.
  695. // FIXME: Even though we're executing under crash protection, it would still
  696. // be nice to do this with RemoveFileOnSignal when we can. However, that
  697. // doesn't make sense for all clients, so clean this up manually.
  698. if (!TempModuleMapFileName.empty())
  699. llvm::sys::Path(TempModuleMapFileName).eraseFromDisk();
  700. }
  701. Module *CompilerInstance::loadModule(SourceLocation ImportLoc,
  702. ModuleIdPath Path,
  703. Module::NameVisibilityKind Visibility,
  704. bool IsInclusionDirective) {
  705. // If we've already handled this import, just return the cached result.
  706. // This one-element cache is important to eliminate redundant diagnostics
  707. // when both the preprocessor and parser see the same import declaration.
  708. if (!ImportLoc.isInvalid() && LastModuleImportLoc == ImportLoc) {
  709. // Make the named module visible.
  710. if (LastModuleImportResult)
  711. ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility);
  712. return LastModuleImportResult;
  713. }
  714. // Determine what file we're searching from.
  715. SourceManager &SourceMgr = getSourceManager();
  716. SourceLocation ExpandedImportLoc = SourceMgr.getExpansionLoc(ImportLoc);
  717. const FileEntry *CurFile
  718. = SourceMgr.getFileEntryForID(SourceMgr.getFileID(ExpandedImportLoc));
  719. if (!CurFile)
  720. CurFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
  721. StringRef ModuleName = Path[0].first->getName();
  722. SourceLocation ModuleNameLoc = Path[0].second;
  723. clang::Module *Module = 0;
  724. // If we don't already have information on this module, load the module now.
  725. llvm::DenseMap<const IdentifierInfo *, clang::Module *>::iterator Known
  726. = KnownModules.find(Path[0].first);
  727. if (Known != KnownModules.end()) {
  728. // Retrieve the cached top-level module.
  729. Module = Known->second;
  730. } else if (ModuleName == getLangOpts().CurrentModule) {
  731. // This is the module we're building.
  732. Module = PP->getHeaderSearchInfo().getModuleMap().findModule(ModuleName);
  733. Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
  734. } else {
  735. // Search for a module with the given name.
  736. Module = PP->getHeaderSearchInfo().lookupModule(ModuleName);
  737. std::string ModuleFileName;
  738. if (Module)
  739. ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(Module);
  740. else
  741. ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(ModuleName);
  742. if (ModuleFileName.empty()) {
  743. getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
  744. << ModuleName
  745. << SourceRange(ImportLoc, ModuleNameLoc);
  746. LastModuleImportLoc = ImportLoc;
  747. LastModuleImportResult = 0;
  748. return 0;
  749. }
  750. const FileEntry *ModuleFile
  751. = getFileManager().getFile(ModuleFileName, /*OpenFile=*/false,
  752. /*CacheFailure=*/false);
  753. bool BuildingModule = false;
  754. if (!ModuleFile && Module) {
  755. // The module is not cached, but we have a module map from which we can
  756. // build the module.
  757. // Check whether there is a cycle in the module graph.
  758. SmallVectorImpl<std::string> &ModuleBuildPath
  759. = getPreprocessorOpts().ModuleBuildPath;
  760. SmallVectorImpl<std::string>::iterator Pos
  761. = std::find(ModuleBuildPath.begin(), ModuleBuildPath.end(), ModuleName);
  762. if (Pos != ModuleBuildPath.end()) {
  763. llvm::SmallString<256> CyclePath;
  764. for (; Pos != ModuleBuildPath.end(); ++Pos) {
  765. CyclePath += *Pos;
  766. CyclePath += " -> ";
  767. }
  768. CyclePath += ModuleName;
  769. getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
  770. << ModuleName << CyclePath;
  771. return 0;
  772. }
  773. getDiagnostics().Report(ModuleNameLoc, diag::warn_module_build)
  774. << ModuleName;
  775. BuildingModule = true;
  776. compileModule(*this, Module, ModuleFileName);
  777. ModuleFile = FileMgr->getFile(ModuleFileName);
  778. }
  779. if (!ModuleFile) {
  780. getDiagnostics().Report(ModuleNameLoc,
  781. BuildingModule? diag::err_module_not_built
  782. : diag::err_module_not_found)
  783. << ModuleName
  784. << SourceRange(ImportLoc, ModuleNameLoc);
  785. return 0;
  786. }
  787. // If we don't already have an ASTReader, create one now.
  788. if (!ModuleManager) {
  789. if (!hasASTContext())
  790. createASTContext();
  791. std::string Sysroot = getHeaderSearchOpts().Sysroot;
  792. const PreprocessorOptions &PPOpts = getPreprocessorOpts();
  793. ModuleManager = new ASTReader(getPreprocessor(), *Context,
  794. Sysroot.empty() ? "" : Sysroot.c_str(),
  795. PPOpts.DisablePCHValidation,
  796. PPOpts.DisableStatCache);
  797. if (hasASTConsumer()) {
  798. ModuleManager->setDeserializationListener(
  799. getASTConsumer().GetASTDeserializationListener());
  800. getASTContext().setASTMutationListener(
  801. getASTConsumer().GetASTMutationListener());
  802. }
  803. llvm::OwningPtr<ExternalASTSource> Source;
  804. Source.reset(ModuleManager);
  805. getASTContext().setExternalSource(Source);
  806. if (hasSema())
  807. ModuleManager->InitializeSema(getSema());
  808. if (hasASTConsumer())
  809. ModuleManager->StartTranslationUnit(&getASTConsumer());
  810. }
  811. // Try to load the module we found.
  812. switch (ModuleManager->ReadAST(ModuleFile->getName(),
  813. serialization::MK_Module)) {
  814. case ASTReader::Success:
  815. break;
  816. case ASTReader::IgnorePCH:
  817. // FIXME: The ASTReader will already have complained, but can we showhorn
  818. // that diagnostic information into a more useful form?
  819. KnownModules[Path[0].first] = 0;
  820. return 0;
  821. case ASTReader::Failure:
  822. // Already complained, but note now that we failed.
  823. KnownModules[Path[0].first] = 0;
  824. return 0;
  825. }
  826. if (!Module) {
  827. // If we loaded the module directly, without finding a module map first,
  828. // we'll have loaded the module's information from the module itself.
  829. Module = PP->getHeaderSearchInfo().getModuleMap()
  830. .findModule((Path[0].first->getName()));
  831. }
  832. // Cache the result of this top-level module lookup for later.
  833. Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
  834. }
  835. // If we never found the module, fail.
  836. if (!Module)
  837. return 0;
  838. // Verify that the rest of the module path actually corresponds to
  839. // a submodule.
  840. if (Path.size() > 1) {
  841. for (unsigned I = 1, N = Path.size(); I != N; ++I) {
  842. StringRef Name = Path[I].first->getName();
  843. clang::Module *Sub = Module->findSubmodule(Name);
  844. if (!Sub) {
  845. // Attempt to perform typo correction to find a module name that works.
  846. llvm::SmallVector<StringRef, 2> Best;
  847. unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)();
  848. for (clang::Module::submodule_iterator J = Module->submodule_begin(),
  849. JEnd = Module->submodule_end();
  850. J != JEnd; ++J) {
  851. unsigned ED = Name.edit_distance((*J)->Name,
  852. /*AllowReplacements=*/true,
  853. BestEditDistance);
  854. if (ED <= BestEditDistance) {
  855. if (ED < BestEditDistance) {
  856. Best.clear();
  857. BestEditDistance = ED;
  858. }
  859. Best.push_back((*J)->Name);
  860. }
  861. }
  862. // If there was a clear winner, user it.
  863. if (Best.size() == 1) {
  864. getDiagnostics().Report(Path[I].second,
  865. diag::err_no_submodule_suggest)
  866. << Path[I].first << Module->getFullModuleName() << Best[0]
  867. << SourceRange(Path[0].second, Path[I-1].second)
  868. << FixItHint::CreateReplacement(SourceRange(Path[I].second),
  869. Best[0]);
  870. Sub = Module->findSubmodule(Best[0]);
  871. }
  872. }
  873. if (!Sub) {
  874. // No submodule by this name. Complain, and don't look for further
  875. // submodules.
  876. getDiagnostics().Report(Path[I].second, diag::err_no_submodule)
  877. << Path[I].first << Module->getFullModuleName()
  878. << SourceRange(Path[0].second, Path[I-1].second);
  879. break;
  880. }
  881. Module = Sub;
  882. }
  883. }
  884. // Make the named module visible, if it's not already part of the module
  885. // we are parsing.
  886. if (ModuleName != getLangOpts().CurrentModule) {
  887. if (!Module->IsFromModuleFile) {
  888. // We have an umbrella header or directory that doesn't actually include
  889. // all of the headers within the directory it covers. Complain about
  890. // this missing submodule and recover by forgetting that we ever saw
  891. // this submodule.
  892. // FIXME: Should we detect this at module load time? It seems fairly
  893. // expensive (and rare).
  894. getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule)
  895. << Module->getFullModuleName()
  896. << SourceRange(Path.front().second, Path.back().second);
  897. return 0;
  898. }
  899. // Check whether this module is available.
  900. StringRef Feature;
  901. if (!Module->isAvailable(getLangOpts(), getTarget(), Feature)) {
  902. getDiagnostics().Report(ImportLoc, diag::err_module_unavailable)
  903. << Module->getFullModuleName()
  904. << Feature
  905. << SourceRange(Path.front().second, Path.back().second);
  906. LastModuleImportLoc = ImportLoc;
  907. LastModuleImportResult = 0;
  908. return 0;
  909. }
  910. ModuleManager->makeModuleVisible(Module, Visibility);
  911. }
  912. // If this module import was due to an inclusion directive, create an
  913. // implicit import declaration to capture it in the AST.
  914. if (IsInclusionDirective && hasASTContext()) {
  915. TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
  916. TU->addDecl(ImportDecl::CreateImplicit(getASTContext(), TU,
  917. ImportLoc, Module,
  918. Path.back().second));
  919. }
  920. LastModuleImportLoc = ImportLoc;
  921. LastModuleImportResult = Module;
  922. return Module;
  923. }