CompilerInstance.cpp revision 2572849d5ee3e5729a89ca934e1b04199eec5a5b
12a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar//===--- CompilerInstance.cpp ---------------------------------------------===// 22a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar// 32a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar// The LLVM Compiler Infrastructure 42a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar// 52a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar// This file is distributed under the University of Illinois Open Source 62a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar// License. See LICENSE.TXT for details. 72a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar// 82a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar//===----------------------------------------------------------------------===// 92a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar 102a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar#include "clang/Frontend/CompilerInstance.h" 11f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor#include "clang/Sema/Sema.h" 1212ce6943aae499225708ecf364c5a8b0a3269c87Daniel Dunbar#include "clang/AST/ASTConsumer.h" 135eb810024dc8a1d12d5f066c02c978f07c4fcb00Daniel Dunbar#include "clang/AST/ASTContext.h" 142a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar#include "clang/Basic/Diagnostic.h" 1516b7449d86b843d0926b04f87104cf3fff7149feDaniel Dunbar#include "clang/Basic/FileManager.h" 1616b7449d86b843d0926b04f87104cf3fff7149feDaniel Dunbar#include "clang/Basic/SourceManager.h" 172a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar#include "clang/Basic/TargetInfo.h" 180397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar#include "clang/Basic/Version.h" 1922dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar#include "clang/Lex/HeaderSearch.h" 2022dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar#include "clang/Lex/Preprocessor.h" 2122dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar#include "clang/Lex/PTHManager.h" 224e85b8ae413fa00fa42aa3e625c2db3b1932f8d3David Blaikie#include "clang/Frontend/ChainedDiagnosticConsumer.h" 230397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar#include "clang/Frontend/FrontendAction.h" 2421cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor#include "clang/Frontend/FrontendActions.h" 25c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar#include "clang/Frontend/FrontendDiagnostic.h" 269df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar#include "clang/Frontend/LogDiagnosticPrinter.h" 270fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar#include "clang/Frontend/TextDiagnosticPrinter.h" 28621bc69624599da62abd9bc9e5edd8a63ac99fe6David Blaikie#include "clang/Frontend/VerifyDiagnosticConsumer.h" 2922dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar#include "clang/Frontend/Utils.h" 306ab7cd853e9c15cf986a8a7c3db1f8d20e275409Sebastian Redl#include "clang/Serialization/ASTReader.h" 31c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar#include "clang/Sema/CodeCompleteConsumer.h" 3232bef4edba854303800b3b01cb49a282e5da4f69Michael J. Spencer#include "llvm/Support/FileSystem.h" 33ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar#include "llvm/Support/MemoryBuffer.h" 340fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar#include "llvm/Support/raw_ostream.h" 3595dd5583e3900443a1d7970d557d8c54aa320601Douglas Gregor#include "llvm/ADT/Statistic.h" 36f79bafa608a5d7c49ec40ad199af5e32f3038b47Kovarththanan Rajaratnam#include "llvm/Support/Timer.h" 3703013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Host.h" 3803013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Path.h" 3903013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Program.h" 4003013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Signals.h" 413a321e23f66128dbb986343927456ff6702af617Michael J. Spencer#include "llvm/Support/system_error.h" 420ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor#include "llvm/Support/CrashRecoveryContext.h" 431626601b00a1feccd6b7a3f54e2991f56b659192Dylan Noblesmith#include "llvm/Config/config.h" 442bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 452bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor// Support for FileLockManager 462bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#include <fstream> 472bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#include <sys/types.h> 482bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#include <sys/stat.h> 492bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 502572849d5ee3e5729a89ca934e1b04199eec5a5bDouglas Gregor#if LLVM_ON_WIN32 512572849d5ee3e5729a89ca934e1b04199eec5a5bDouglas Gregor#include <windows.h> 522572849d5ee3e5729a89ca934e1b04199eec5a5bDouglas Gregor#endif 532bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#if LLVM_ON_UNIX 542bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#include <unistd.h> 552bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#endif 562bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 572a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbarusing namespace clang; 582a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar 5942e9f8e4983d50f896ec716207817b9d96e7e79cDaniel DunbarCompilerInstance::CompilerInstance() 60f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor : Invocation(new CompilerInvocation()), ModuleManager(0) { 616228ca00121669ec06a19df4fad87d5049c097cfDaniel Dunbar} 622a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar 632a79e162a3fde25c1941151a67966830d873f2dbDaniel DunbarCompilerInstance::~CompilerInstance() { 6442e9f8e4983d50f896ec716207817b9d96e7e79cDaniel Dunbar} 6542e9f8e4983d50f896ec716207817b9d96e7e79cDaniel Dunbar 666228ca00121669ec06a19df4fad87d5049c097cfDaniel Dunbarvoid CompilerInstance::setInvocation(CompilerInvocation *Value) { 674f32786ac45210143654390177105eb749b614e9Ted Kremenek Invocation = Value; 686228ca00121669ec06a19df4fad87d5049c097cfDaniel Dunbar} 696228ca00121669ec06a19df4fad87d5049c097cfDaniel Dunbar 70d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikievoid CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) { 7128019772db70d4547be05a042eb950bc910f134fDouglas Gregor Diagnostics = Value; 728a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar} 738a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar 748a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbarvoid CompilerInstance::setTarget(TargetInfo *Value) { 754f32786ac45210143654390177105eb749b614e9Ted Kremenek Target = Value; 768a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar} 778a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar 788a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbarvoid CompilerInstance::setFileManager(FileManager *Value) { 794f32786ac45210143654390177105eb749b614e9Ted Kremenek FileMgr = Value; 808a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar} 818a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar 824f32786ac45210143654390177105eb749b614e9Ted Kremenekvoid CompilerInstance::setSourceManager(SourceManager *Value) { 834f32786ac45210143654390177105eb749b614e9Ted Kremenek SourceMgr = Value; 848a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar} 858a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar 864f32786ac45210143654390177105eb749b614e9Ted Kremenekvoid CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; } 878a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar 884f32786ac45210143654390177105eb749b614e9Ted Kremenekvoid CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; } 8912ce6943aae499225708ecf364c5a8b0a3269c87Daniel Dunbar 90f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregorvoid CompilerInstance::setSema(Sema *S) { 91f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor TheSema.reset(S); 92f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor} 93f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor 9412ce6943aae499225708ecf364c5a8b0a3269c87Daniel Dunbarvoid CompilerInstance::setASTConsumer(ASTConsumer *Value) { 9512ce6943aae499225708ecf364c5a8b0a3269c87Daniel Dunbar Consumer.reset(Value); 968a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar} 978a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar 988a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbarvoid CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) { 998a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar CompletionConsumer.reset(Value); 1008a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar} 1018a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar 1020fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar// Diagnostics 1030fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbarstatic void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts, 1047d0c4ccd65b4549283c55e4923602e234f3811c5Axel Naumann unsigned argc, const char* const *argv, 105d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie DiagnosticsEngine &Diags) { 1060fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar std::string ErrorInfo; 1075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner llvm::OwningPtr<raw_ostream> OS( 10869247139f5ce3edf46c48344129792b3a05d090aKovarththanan Rajaratnam new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo)); 1090fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar if (!ErrorInfo.empty()) { 1103d67b1e25847319a5a271f9d5a8d607ef18d804aKovarththanan Rajaratnam Diags.Report(diag::err_fe_unable_to_open_logfile) 1113d67b1e25847319a5a271f9d5a8d607ef18d804aKovarththanan Rajaratnam << DiagOpts.DumpBuildInformation << ErrorInfo; 1120fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar return; 1130fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar } 1140fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar 115dd63b28107f21692b5065588f0e90b4534946f93Daniel Dunbar (*OS) << "clang -cc1 command line arguments: "; 1160fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar for (unsigned i = 0; i != argc; ++i) 1170fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar (*OS) << argv[i] << ' '; 1180fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar (*OS) << '\n'; 1190fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar 1200fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar // Chain in a diagnostic client which will log the diagnostics. 12178ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie DiagnosticConsumer *Logger = 12269247139f5ce3edf46c48344129792b3a05d090aKovarththanan Rajaratnam new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true); 1234e85b8ae413fa00fa42aa3e625c2db3b1932f8d3David Blaikie Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger)); 1240fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar} 1250fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar 1269df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbarstatic void SetUpDiagnosticLog(const DiagnosticOptions &DiagOpts, 127b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar const CodeGenOptions *CodeGenOpts, 128d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie DiagnosticsEngine &Diags) { 1299df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar std::string ErrorInfo; 1309df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar bool OwnsStream = false; 1315f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner raw_ostream *OS = &llvm::errs(); 1329df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar if (DiagOpts.DiagnosticLogFile != "-") { 1339df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar // Create the output stream. 1349df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar llvm::raw_fd_ostream *FileOS( 1359df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar new llvm::raw_fd_ostream(DiagOpts.DiagnosticLogFile.c_str(), 136e01eceb54c9235a41675bfd9f32c50bd005fb8d0Daniel Dunbar ErrorInfo, llvm::raw_fd_ostream::F_Append)); 1379df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar if (!ErrorInfo.empty()) { 1389df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar Diags.Report(diag::warn_fe_cc_log_diagnostics_failure) 1399df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar << DiagOpts.DumpBuildInformation << ErrorInfo; 1409df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar } else { 1419df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar FileOS->SetUnbuffered(); 1429df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar FileOS->SetUseAtomicWrites(true); 1439df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar OS = FileOS; 1449df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar OwnsStream = true; 1459df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar } 1469df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar } 1479df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar 1489df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar // Chain in the diagnostic client which will log the diagnostics. 149b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts, 150b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar OwnsStream); 151b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar if (CodeGenOpts) 152b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags); 1534e85b8ae413fa00fa42aa3e625c2db3b1932f8d3David Blaikie Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger)); 1549df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar} 1559df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar 156e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregorvoid CompilerInstance::createDiagnostics(int Argc, const char* const *Argv, 15778ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie DiagnosticConsumer *Client, 158aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor bool ShouldOwnClient, 159aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor bool ShouldCloneClient) { 160b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client, 161aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor ShouldOwnClient, ShouldCloneClient, 162aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor &getCodeGenOpts()); 1630fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar} 1640fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar 165d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikiellvm::IntrusiveRefCntPtr<DiagnosticsEngine> 16628019772db70d4547be05a042eb950bc910f134fDouglas GregorCompilerInstance::createDiagnostics(const DiagnosticOptions &Opts, 167e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor int Argc, const char* const *Argv, 16878ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie DiagnosticConsumer *Client, 16978243658c533168d51fd076fba328437932ba6f1Douglas Gregor bool ShouldOwnClient, 170aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor bool ShouldCloneClient, 171b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar const CodeGenOptions *CodeGenOpts) { 17233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 173d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie llvm::IntrusiveRefCntPtr<DiagnosticsEngine> 174d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie Diags(new DiagnosticsEngine(DiagID)); 175221c7211c507482a91e97ede1bf6cf65a456ff67Daniel Dunbar 1760fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar // Create the diagnostic client for reporting errors or for 1770fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar // implementing -verify. 178aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor if (Client) { 179aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor if (ShouldCloneClient) 180aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor Diags->setClient(Client->clone(*Diags), ShouldOwnClient); 181aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor else 182aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor Diags->setClient(Client, ShouldOwnClient); 183aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor } else 184e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts)); 185f79dced038c63572965c016b969cfa870670d16eDaniel Dunbar 186f79dced038c63572965c016b969cfa870670d16eDaniel Dunbar // Chain in -verify checker, if requested. 18778243658c533168d51fd076fba328437932ba6f1Douglas Gregor if (Opts.VerifyDiagnostics) 188621bc69624599da62abd9bc9e5edd8a63ac99fe6David Blaikie Diags->setClient(new VerifyDiagnosticConsumer(*Diags)); 1890fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar 1909df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar // Chain in -diagnostic-log-file dumper, if requested. 1919df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar if (!Opts.DiagnosticLogFile.empty()) 192b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags); 1939df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar 1940fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar if (!Opts.DumpBuildInformation.empty()) 1953d67b1e25847319a5a271f9d5a8d607ef18d804aKovarththanan Rajaratnam SetUpBuildDumpLog(Opts, Argc, Argv, *Diags); 1960fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar 1970fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar // Configure our handling of diagnostics. 1985bf932bd0d5db98273938675dbf81cbb2f5ffff7Kovarththanan Rajaratnam ProcessWarningOptions(*Diags, Opts); 1990fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar 20028019772db70d4547be05a042eb950bc910f134fDouglas Gregor return Diags; 2010fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar} 2020fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar 2030fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar// File Manager 2040fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar 20516b7449d86b843d0926b04f87104cf3fff7149feDaniel Dunbarvoid CompilerInstance::createFileManager() { 2064f32786ac45210143654390177105eb749b614e9Ted Kremenek FileMgr = new FileManager(getFileSystemOpts()); 20716b7449d86b843d0926b04f87104cf3fff7149feDaniel Dunbar} 20816b7449d86b843d0926b04f87104cf3fff7149feDaniel Dunbar 2090fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar// Source Manager 2100fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar 21139b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattnervoid CompilerInstance::createSourceManager(FileManager &FileMgr) { 2124f32786ac45210143654390177105eb749b614e9Ted Kremenek SourceMgr = new SourceManager(getDiagnostics(), FileMgr); 21316b7449d86b843d0926b04f87104cf3fff7149feDaniel Dunbar} 21422dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar 2150fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar// Preprocessor 2160fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar 21722dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbarvoid CompilerInstance::createPreprocessor() { 2186aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor const PreprocessorOptions &PPOpts = getPreprocessorOpts(); 2196aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 22022dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar // Create a PTH manager if we are using some form of a token cache. 22122dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar PTHManager *PTHMgr = 0; 222049d3a06ea9f8fc03582488a2b7f24512565a335Daniel Dunbar if (!PPOpts.TokenCache.empty()) 2236aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics()); 2246aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 22522dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar // Create the Preprocessor. 2266aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor HeaderSearch *HeaderInfo = new HeaderSearch(getFileManager()); 227998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor PP = new Preprocessor(getDiagnostics(), getLangOpts(), &getTarget(), 2286aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor getSourceManager(), *HeaderInfo, *this, PTHMgr, 2296aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor /*OwnsHeaderSearch=*/true); 2306aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 23122dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar // Note that this is different then passing PTHMgr to Preprocessor's ctor. 23222dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar // That argument is used as the IdentifierInfoLookup argument to 23322dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar // IdentifierTable's ctor. 23422dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar if (PTHMgr) { 2356aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor PTHMgr->setPreprocessor(&*PP); 23622dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar PP->setPTHManager(PTHMgr); 23722dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar } 2386aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 23994dc8f640ebea52241412512ed48601626edbc58Douglas Gregor if (PPOpts.DetailedRecord) 240dca8ee8b7bc86076916a3a80f553f7a4e98c14afDouglas Gregor PP->createPreprocessingRecord( 2416aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor PPOpts.DetailedRecordIncludesNestedMacroExpansions); 2426aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 2436aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts()); 24494dc8f640ebea52241412512ed48601626edbc58Douglas Gregor 2456e975c4517958bcc11c834336d340797356058dbDouglas Gregor // Set up the module path, including the hash for the 2466e975c4517958bcc11c834336d340797356058dbDouglas Gregor // module-creation options. 2476e975c4517958bcc11c834336d340797356058dbDouglas Gregor llvm::SmallString<256> SpecificModuleCache( 2486e975c4517958bcc11c834336d340797356058dbDouglas Gregor getHeaderSearchOpts().ModuleCachePath); 2496e975c4517958bcc11c834336d340797356058dbDouglas Gregor if (!getHeaderSearchOpts().DisableModuleHash) 2506e975c4517958bcc11c834336d340797356058dbDouglas Gregor llvm::sys::path::append(SpecificModuleCache, 2516e975c4517958bcc11c834336d340797356058dbDouglas Gregor getInvocation().getModuleHash()); 252fba18aa8f2cd1994dc65e8cb9f4be201c560dc0bDouglas Gregor PP->getHeaderSearchInfo().configureModules(SpecificModuleCache, 253fba18aa8f2cd1994dc65e8cb9f4be201c560dc0bDouglas Gregor getPreprocessorOpts().ModuleBuildPath.empty() 254fba18aa8f2cd1994dc65e8cb9f4be201c560dc0bDouglas Gregor ? std::string() 255fba18aa8f2cd1994dc65e8cb9f4be201c560dc0bDouglas Gregor : getPreprocessorOpts().ModuleBuildPath.back()); 2566e975c4517958bcc11c834336d340797356058dbDouglas Gregor 25722dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar // Handle generating dependencies, if requested. 2586aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor const DependencyOutputOptions &DepOpts = getDependencyOutputOpts(); 25922dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar if (!DepOpts.OutputFile.empty()) 26022dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar AttachDependencyFileGen(*PP, DepOpts); 2616aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 262eef63e0997e0f6d6436736ea919b851cfe34955aDaniel Dunbar // Handle generating header include information, if requested. 263eef63e0997e0f6d6436736ea919b851cfe34955aDaniel Dunbar if (DepOpts.ShowHeaderIncludes) 264eef63e0997e0f6d6436736ea919b851cfe34955aDaniel Dunbar AttachHeaderIncludeGen(*PP); 265b34d69b9292534c1c574f168f0ac10aea652adcaDaniel Dunbar if (!DepOpts.HeaderIncludeOutputFile.empty()) { 2665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef OutputPath = DepOpts.HeaderIncludeOutputFile; 267b34d69b9292534c1c574f168f0ac10aea652adcaDaniel Dunbar if (OutputPath == "-") 268b34d69b9292534c1c574f168f0ac10aea652adcaDaniel Dunbar OutputPath = ""; 269da60885cb4ee85d167cf704061f10b758067cf5aDaniel Dunbar AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath, 270da60885cb4ee85d167cf704061f10b758067cf5aDaniel Dunbar /*ShowDepth=*/false); 271b34d69b9292534c1c574f168f0ac10aea652adcaDaniel Dunbar } 27222dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar} 2735eb810024dc8a1d12d5f066c02c978f07c4fcb00Daniel Dunbar 2745eb810024dc8a1d12d5f066c02c978f07c4fcb00Daniel Dunbar// ASTContext 2755eb810024dc8a1d12d5f066c02c978f07c4fcb00Daniel Dunbar 2765eb810024dc8a1d12d5f066c02c978f07c4fcb00Daniel Dunbarvoid CompilerInstance::createASTContext() { 2775eb810024dc8a1d12d5f066c02c978f07c4fcb00Daniel Dunbar Preprocessor &PP = getPreprocessor(); 2784f32786ac45210143654390177105eb749b614e9Ted Kremenek Context = new ASTContext(getLangOpts(), PP.getSourceManager(), 279bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor &getTarget(), PP.getIdentifierTable(), 2804f32786ac45210143654390177105eb749b614e9Ted Kremenek PP.getSelectorTable(), PP.getBuiltinInfo(), 2814f32786ac45210143654390177105eb749b614e9Ted Kremenek /*size_reserve=*/ 0); 2825eb810024dc8a1d12d5f066c02c978f07c4fcb00Daniel Dunbar} 2830f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar 2840f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar// ExternalASTSource 2850f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar 2865f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid CompilerInstance::createPCHExternalASTSource(StringRef Path, 287ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl bool DisablePCHValidation, 2888ef6c8cb6c5627240e2339fd7062c9873f821d7eDouglas Gregor bool DisableStatCache, 289ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl void *DeserializationListener){ 2900f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar llvm::OwningPtr<ExternalASTSource> Source; 2911d9f1fe7173e3084325f43c78af812a36d8a2a7cSebastian Redl bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; 2920f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot, 2938ef6c8cb6c5627240e2339fd7062c9873f821d7eDouglas Gregor DisablePCHValidation, 2948ef6c8cb6c5627240e2339fd7062c9873f821d7eDouglas Gregor DisableStatCache, 295ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl getPreprocessor(), getASTContext(), 2961d9f1fe7173e3084325f43c78af812a36d8a2a7cSebastian Redl DeserializationListener, 2971d9f1fe7173e3084325f43c78af812a36d8a2a7cSebastian Redl Preamble)); 298f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor ModuleManager = static_cast<ASTReader*>(Source.get()); 2990f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar getASTContext().setExternalSource(Source); 3000f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar} 3010f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar 3020f800391ffbfe3820e1c60246a09a97e5f065179Daniel DunbarExternalASTSource * 3035f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerCompilerInstance::createPCHExternalASTSource(StringRef Path, 3040f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar const std::string &Sysroot, 305fae3b2f4743dad616623c4df2fdb0f5128bd36d9Douglas Gregor bool DisablePCHValidation, 3068ef6c8cb6c5627240e2339fd7062c9873f821d7eDouglas Gregor bool DisableStatCache, 3070f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar Preprocessor &PP, 308ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl ASTContext &Context, 3091d9f1fe7173e3084325f43c78af812a36d8a2a7cSebastian Redl void *DeserializationListener, 3101d9f1fe7173e3084325f43c78af812a36d8a2a7cSebastian Redl bool Preamble) { 311c43b54cbc10654ed59de797898042e1a05265246Sebastian Redl llvm::OwningPtr<ASTReader> Reader; 312f8a1e51c48761ee1d7803c3fa35ac94f42ebb55eDouglas Gregor Reader.reset(new ASTReader(PP, Context, 313832d620b4ae0fc5fe28561b885b4cfc65cf5c9abDouglas Gregor Sysroot.empty() ? "" : Sysroot.c_str(), 3148ef6c8cb6c5627240e2339fd7062c9873f821d7eDouglas Gregor DisablePCHValidation, DisableStatCache)); 3150f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar 316ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl Reader->setDeserializationListener( 317571db7f0cb31789737be92fce1c1b738e6dbe795Sebastian Redl static_cast<ASTDeserializationListener *>(DeserializationListener)); 31872a9ae18553bf8b6bdad84d2c54f73741a47e275Douglas Gregor switch (Reader->ReadAST(Path, 31972a9ae18553bf8b6bdad84d2c54f73741a47e275Douglas Gregor Preamble ? serialization::MK_Preamble 32072a9ae18553bf8b6bdad84d2c54f73741a47e275Douglas Gregor : serialization::MK_PCH)) { 321c43b54cbc10654ed59de797898042e1a05265246Sebastian Redl case ASTReader::Success: 3220f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar // Set the predefines buffer as suggested by the PCH reader. Typically, the 3230f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar // predefines buffer will be empty. 3240f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar PP.setPredefines(Reader->getSuggestedPredefines()); 3250f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar return Reader.take(); 3260f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar 327c43b54cbc10654ed59de797898042e1a05265246Sebastian Redl case ASTReader::Failure: 3280f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar // Unrecoverable failure: don't even try to process the input file. 3290f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar break; 3300f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar 331c43b54cbc10654ed59de797898042e1a05265246Sebastian Redl case ASTReader::IgnorePCH: 3320f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar // No suitable PCH file could be found. Return an error. 3330f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar break; 3340f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar } 3350f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar 3360f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar return 0; 3370f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar} 338c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar 339c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar// Code Completion 340c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar 3411abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregorstatic bool EnableCodeCompletion(Preprocessor &PP, 3421abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor const std::string &Filename, 3431abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor unsigned Line, 3441abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor unsigned Column) { 3451abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor // Tell the source manager to chop off the given file at a specific 3461abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor // line and column. 34739b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner const FileEntry *Entry = PP.getFileManager().getFile(Filename); 3481abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor if (!Entry) { 3491abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) 3501abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor << Filename; 3511abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor return true; 3521abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor } 3531abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor 3541abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor // Truncate the named file at the given line/column. 3551abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor PP.SetCodeCompletionPoint(Entry, Line, Column); 3561abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor return false; 3571abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor} 3581abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor 359c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbarvoid CompilerInstance::createCodeCompletionConsumer() { 360c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt; 3611abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor if (!CompletionConsumer) { 3621abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor CompletionConsumer.reset( 3631abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor createCodeCompletionConsumer(getPreprocessor(), 3641abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor Loc.FileName, Loc.Line, Loc.Column, 3651abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor getFrontendOpts().ShowMacrosInCodeCompletion, 366d8e8a58ee35ab334ab9d0c2154dca029c1822e8aDouglas Gregor getFrontendOpts().ShowCodePatternsInCodeCompletion, 3678071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor getFrontendOpts().ShowGlobalSymbolsInCodeCompletion, 3681abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor llvm::outs())); 3691abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor if (!CompletionConsumer) 3701abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor return; 3711abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName, 3721abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor Loc.Line, Loc.Column)) { 3731abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor CompletionConsumer.reset(); 374c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor return; 3751abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor } 3762b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor 3772b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor if (CompletionConsumer->isOutputBinary() && 3782b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor llvm::sys::Program::ChangeStdoutToBinary()) { 3792b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary); 3802b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor CompletionConsumer.reset(); 3812b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor } 382c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar} 383c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar 384f79bafa608a5d7c49ec40ad199af5e32f3038b47Kovarththanan Rajaratnamvoid CompilerInstance::createFrontendTimer() { 385f79bafa608a5d7c49ec40ad199af5e32f3038b47Kovarththanan Rajaratnam FrontendTimer.reset(new llvm::Timer("Clang front-end timer")); 386f79bafa608a5d7c49ec40ad199af5e32f3038b47Kovarththanan Rajaratnam} 387f79bafa608a5d7c49ec40ad199af5e32f3038b47Kovarththanan Rajaratnam 388c2f484f1f05216a9a427ac84b5773789a4661111Daniel DunbarCodeCompleteConsumer * 389c2f484f1f05216a9a427ac84b5773789a4661111Daniel DunbarCompilerInstance::createCodeCompletionConsumer(Preprocessor &PP, 390c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar const std::string &Filename, 391c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar unsigned Line, 392c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar unsigned Column, 393c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar bool ShowMacros, 394d8e8a58ee35ab334ab9d0c2154dca029c1822e8aDouglas Gregor bool ShowCodePatterns, 3958071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor bool ShowGlobals, 3965f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner raw_ostream &OS) { 3971abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor if (EnableCodeCompletion(PP, Filename, Line, Column)) 398c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar return 0; 399c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar 400c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar // Set up the creation routine for code-completion. 401a9f4f620daf073805b89e893afcdc5eb7a9bdc50Douglas Gregor return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns, 4028071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor ShowGlobals, OS); 403c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar} 404a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar 405467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregorvoid CompilerInstance::createSema(TranslationUnitKind TUKind, 406f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor CodeCompleteConsumer *CompletionConsumer) { 407f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(), 408467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor TUKind, CompletionConsumer)); 409f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor} 410f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor 411a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar// Output Files 412a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar 413dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidisvoid CompilerInstance::addOutputFile(const OutputFile &OutFile) { 414dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis assert(OutFile.OS && "Attempt to add empty stream to output list!"); 415dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis OutputFiles.push_back(OutFile); 416a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar} 417a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar 418e51dd7be67808d52c80c09b832d875e9655ce6e0Kovarththanan Rajaratnamvoid CompilerInstance::clearOutputFiles(bool EraseFiles) { 419dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis for (std::list<OutputFile>::iterator 420a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) { 421dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis delete it->OS; 422dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis if (!it->TempFilename.empty()) { 423af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson if (EraseFiles) { 424af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson bool existed; 425af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson llvm::sys::fs::remove(it->TempFilename, existed); 426af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson } else { 427af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson llvm::SmallString<128> NewOutFile(it->Filename); 428af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson 429389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis // If '-working-directory' was passed, the output filename should be 430389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis // relative to that. 4312e2468e2d2ccbb9a38fe3b6b754009af7e5d39a9Anders Carlsson FileMgr->FixupRelativePath(NewOutFile); 432af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename, 433af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson NewOutFile.str())) { 434dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis getDiagnostics().Report(diag::err_fe_unable_to_rename_temp) 435af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson << it->TempFilename << it->Filename << ec.message(); 436af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson 437af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson bool existed; 438af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson llvm::sys::fs::remove(it->TempFilename, existed); 439dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis } 440dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis } 441dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis } else if (!it->Filename.empty() && EraseFiles) 442dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis llvm::sys::Path(it->Filename).eraseFromDisk(); 443dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis 444a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar } 445a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar OutputFiles.clear(); 446a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar} 447a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar 448f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbarllvm::raw_fd_ostream * 449f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel DunbarCompilerInstance::createDefaultOutputFile(bool Binary, 4505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef InFile, 4515f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Extension) { 452f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar return createOutputFile(getFrontendOpts().OutputFile, Binary, 453ff9cd968cd5b623e3ec7e5f862b598cd22f7ec79Daniel Dunbar /*RemoveFileOnSignal=*/true, InFile, Extension); 454f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar} 455f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar 456f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbarllvm::raw_fd_ostream * 4575f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerCompilerInstance::createOutputFile(StringRef OutputPath, 458ff9cd968cd5b623e3ec7e5f862b598cd22f7ec79Daniel Dunbar bool Binary, bool RemoveFileOnSignal, 4595f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef InFile, 4607e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis StringRef Extension, 4617e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis bool UseTemporary) { 462dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis std::string Error, OutputPathName, TempPathName; 463f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary, 464ff9cd968cd5b623e3ec7e5f862b598cd22f7ec79Daniel Dunbar RemoveFileOnSignal, 465f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar InFile, Extension, 4667e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis UseTemporary, 467dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis &OutputPathName, 468dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis &TempPathName); 469f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar if (!OS) { 470360435908c9b90429cfe192fab22854af1d4497cDaniel Dunbar getDiagnostics().Report(diag::err_fe_unable_to_open_output) 471360435908c9b90429cfe192fab22854af1d4497cDaniel Dunbar << OutputPath << Error; 472360435908c9b90429cfe192fab22854af1d4497cDaniel Dunbar return 0; 473f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar } 474f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar 475f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar // Add the output file -- but don't try to remove "-", since this means we are 476f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar // using stdin. 477dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "", 478dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis TempPathName, OS)); 479f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar 480f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar return OS; 481f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar} 482f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar 483f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbarllvm::raw_fd_ostream * 4845f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerCompilerInstance::createOutputFile(StringRef OutputPath, 485f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar std::string &Error, 486f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar bool Binary, 487ff9cd968cd5b623e3ec7e5f862b598cd22f7ec79Daniel Dunbar bool RemoveFileOnSignal, 4885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef InFile, 4895f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Extension, 4907e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis bool UseTemporary, 491dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis std::string *ResultPathName, 492dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis std::string *TempPathName) { 493dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis std::string OutFile, TempFile; 494f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar if (!OutputPath.empty()) { 495f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar OutFile = OutputPath; 496f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar } else if (InFile == "-") { 497f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar OutFile = "-"; 498f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar } else if (!Extension.empty()) { 499f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar llvm::sys::Path Path(InFile); 500f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar Path.eraseSuffix(); 501f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar Path.appendSuffix(Extension); 502f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar OutFile = Path.str(); 503f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar } else { 504f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar OutFile = "-"; 505f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar } 5067e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis 5077e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis llvm::OwningPtr<llvm::raw_fd_ostream> OS; 5087e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis std::string OSFile; 5097e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis 5107e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis if (UseTemporary && OutFile != "-") { 511dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis llvm::sys::Path OutPath(OutFile); 512dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis // Only create the temporary if we can actually write to OutPath, otherwise 513dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis // we want to fail early. 51432bef4edba854303800b3b01cb49a282e5da4f69Michael J. Spencer bool Exists; 51532bef4edba854303800b3b01cb49a282e5da4f69Michael J. Spencer if ((llvm::sys::fs::exists(OutPath.str(), Exists) || !Exists) || 516dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis (OutPath.isRegularFile() && OutPath.canWrite())) { 517dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis // Create a temporary file. 5187e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis llvm::SmallString<128> TempPath; 5197e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis TempPath = OutFile; 5207e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis TempPath += "-%%%%%%%%"; 5217e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis int fd; 5227e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath, 5237e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis /*makeAbsolute=*/false) == llvm::errc::success) { 5247e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true)); 5257e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis OSFile = TempFile = TempPath.str(); 5267e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis } 527dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis } 528dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis } 529dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis 5307e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis if (!OS) { 5317e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis OSFile = OutFile; 5327e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis OS.reset( 5337e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis new llvm::raw_fd_ostream(OSFile.c_str(), Error, 5347e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis (Binary ? llvm::raw_fd_ostream::F_Binary : 0))); 5357e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis if (!Error.empty()) 5367e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis return 0; 5377e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis } 538f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar 539dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis // Make sure the out stream file gets removed if we crash. 540ff9cd968cd5b623e3ec7e5f862b598cd22f7ec79Daniel Dunbar if (RemoveFileOnSignal) 541ff9cd968cd5b623e3ec7e5f862b598cd22f7ec79Daniel Dunbar llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile)); 542dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis 543f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar if (ResultPathName) 544f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar *ResultPathName = OutFile; 545dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis if (TempPathName) 546dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis *TempPathName = TempFile; 547f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar 548fc97102a80bfe0afaa25883a2aa6b5e1d7307d0aDaniel Dunbar return OS.take(); 549f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar} 550ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar 551ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar// Initialization Utilities 552ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar 5535f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerbool CompilerInstance::InitializeSourceManager(StringRef InputFile) { 554ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(), 555ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar getSourceManager(), getFrontendOpts()); 556ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar} 557ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar 5585f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerbool CompilerInstance::InitializeSourceManager(StringRef InputFile, 559d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie DiagnosticsEngine &Diags, 560ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar FileManager &FileMgr, 561ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar SourceManager &SourceMgr, 562ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar const FrontendOptions &Opts) { 563507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis // Figure out where to get and map in the main file. 564507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis if (InputFile != "-") { 56539b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner const FileEntry *File = FileMgr.getFile(InputFile); 566694137c54c79a33c9ac6c07e68327750dcd5adf7Dan Gohman if (!File) { 567ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar Diags.Report(diag::err_fe_error_reading) << InputFile; 568ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar return false; 569ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar } 570694137c54c79a33c9ac6c07e68327750dcd5adf7Dan Gohman SourceMgr.createMainFileID(File); 571ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar } else { 5724eeebc464e1f968d9968a4786c82558f18ac2ed8Michael J. Spencer llvm::OwningPtr<llvm::MemoryBuffer> SB; 5734eeebc464e1f968d9968a4786c82558f18ac2ed8Michael J. Spencer if (llvm::MemoryBuffer::getSTDIN(SB)) { 5743a321e23f66128dbb986343927456ff6702af617Michael J. Spencer // FIXME: Give ec.message() in this diag. 575ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar Diags.Report(diag::err_fe_error_reading_stdin); 576ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar return false; 577ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar } 57890d9081cacb4b0163f2c7527f666d6515257067cDan Gohman const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(), 57939b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner SB->getBufferSize(), 0); 58090d9081cacb4b0163f2c7527f666d6515257067cDan Gohman SourceMgr.createMainFileID(File); 5814eeebc464e1f968d9968a4786c82558f18ac2ed8Michael J. Spencer SourceMgr.overrideFileContents(File, SB.take()); 582ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar } 583ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar 584694137c54c79a33c9ac6c07e68327750dcd5adf7Dan Gohman assert(!SourceMgr.getMainFileID().isInvalid() && 585694137c54c79a33c9ac6c07e68327750dcd5adf7Dan Gohman "Couldn't establish MainFileID!"); 586ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar return true; 587ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar} 5880397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 5890397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar// High-Level Operations 5900397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 5910397af277e3bba16da1fd125ddba07415686b429Daniel Dunbarbool CompilerInstance::ExecuteAction(FrontendAction &Act) { 5920397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar assert(hasDiagnostics() && "Diagnostics engine is not initialized!"); 5930397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!"); 5940397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!"); 5950397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 5960397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar // FIXME: Take this as an argument, once all the APIs we used have moved to 5970397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar // taking it as an input instead of hard-coding llvm::errs. 5985f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner raw_ostream &OS = llvm::errs(); 5990397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 6000397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar // Create the target instance. 6010397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts())); 6020397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar if (!hasTarget()) 6030397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar return false; 6040397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 6050397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar // Inform the target of the language options. 6060397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar // 6070397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar // FIXME: We shouldn't need to do this, the target should be immutable once 6080397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar // created. This complexity should be lifted elsewhere. 6090397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar getTarget().setForcedLangOptions(getLangOpts()); 6100397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 6110397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar // Validate/process some options. 6120397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar if (getHeaderSearchOpts().Verbose) 6130397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar OS << "clang -cc1 version " CLANG_VERSION_STRING 6140397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar << " based upon " << PACKAGE_STRING 6150397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar << " hosted on " << llvm::sys::getHostTriple() << "\n"; 6160397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 6170397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar if (getFrontendOpts().ShowTimers) 6180397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar createFrontendTimer(); 6190397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 62095dd5583e3900443a1d7970d557d8c54aa320601Douglas Gregor if (getFrontendOpts().ShowStats) 62195dd5583e3900443a1d7970d557d8c54aa320601Douglas Gregor llvm::EnableStatistics(); 62295dd5583e3900443a1d7970d557d8c54aa320601Douglas Gregor 6230397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) { 6240397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar const std::string &InFile = getFrontendOpts().Inputs[i].second; 6250397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 6262056048f0f619adadc9a5416a2c4cdf95c58eef7Daniel Dunbar // Reset the ID tables if we are reusing the SourceManager. 6272056048f0f619adadc9a5416a2c4cdf95c58eef7Daniel Dunbar if (hasSourceManager()) 6282056048f0f619adadc9a5416a2c4cdf95c58eef7Daniel Dunbar getSourceManager().clearIDTables(); 6290397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 630d3598a65716e120aef45aa2841d730e03f7101feDaniel Dunbar if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) { 6310397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar Act.Execute(); 6320397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar Act.EndSourceFile(); 6330397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar } 6340397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar } 6350397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 63653eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner if (getDiagnosticOpts().ShowCarets) { 637f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis // We can have multiple diagnostics sharing one diagnostic client. 638f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis // Get the total number of warnings/errors from the client. 639f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings(); 640f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis unsigned NumErrors = getDiagnostics().getClient()->getNumErrors(); 64153eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner 64253eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner if (NumWarnings) 64353eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s"); 64453eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner if (NumWarnings && NumErrors) 64553eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner OS << " and "; 64653eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner if (NumErrors) 64753eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s"); 64853eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner if (NumWarnings || NumErrors) 64953eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner OS << " generated.\n"; 65053eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner } 6510397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 6522056048f0f619adadc9a5416a2c4cdf95c58eef7Daniel Dunbar if (getFrontendOpts().ShowStats && hasFileManager()) { 6530397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar getFileManager().PrintStats(); 6540397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar OS << "\n"; 6550397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar } 6560397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 657ab41b97c3cddf1bcdd8bf82ab09ed3ceafcd05b1Argyrios Kyrtzidis return !getDiagnostics().getClient()->getNumErrors(); 6580397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar} 6590397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 66021cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor/// \brief Determine the appropriate source input kind based on language 66121cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor/// options. 66221cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregorstatic InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) { 66321cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor if (LangOpts.OpenCL) 66421cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor return IK_OpenCL; 66521cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor if (LangOpts.CUDA) 66621cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor return IK_CUDA; 66721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor if (LangOpts.ObjC1) 66821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC; 66921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor return LangOpts.CPlusPlus? IK_CXX : IK_C; 67021cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor} 67121cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor 6720ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregornamespace { 6730ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor struct CompileModuleData { 6740ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor CompilerInstance &Instance; 6750ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor GeneratePCHAction &CreateModuleAction; 6760ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor }; 6770ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor} 6780ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor 6790ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor/// \brief Helper function that executes the module-generating action under 6800ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor/// a crash recovery context. 6810ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregorstatic void doCompileModule(void *UserData) { 6820ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor CompileModuleData &Data = *reinterpret_cast<CompileModuleData *>(UserData); 6830ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor Data.Instance.ExecuteAction(Data.CreateModuleAction); 6840ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor} 6850ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor 6862bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregornamespace { 6872bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// \brief Class that manages the creation of a lock file to aid 6882bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// implicit coordination between different processes. 6892bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// 6902bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// The implicit coordination works by creating a ".lock" file alongside 6912bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// the file that we're coordinating for, using the atomicity of the file 6922bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// system to ensure that only a single process can create that ".lock" file. 6932bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// When the lock file is removed, the owning process has finished the 6942bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// operation. 6952bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor class LockFileManager { 6962bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor public: 6972bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// \brief Describes the state of a lock file. 6982bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor enum LockFileState { 6992bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// \brief The lock file has been created and is owned by this instance 7002bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// of the object. 7012bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor LFS_Owned, 7022bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// \brief The lock file already exists and is owned by some other 7032bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// instance. 7042bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor LFS_Shared, 7052bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// \brief An error occurred while trying to create or find the lock 7062bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// file. 7072bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor LFS_Error 7082bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor }; 7092bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7102bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor private: 7112bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor llvm::SmallString<128> LockFileName; 7122bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor llvm::SmallString<128> UniqueLockFileName; 7132bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7142bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor llvm::Optional<std::pair<std::string, int> > Owner; 7152bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor llvm::Optional<llvm::error_code> Error; 7162bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7172bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor LockFileManager(const LockFileManager &); 7182bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor LockFileManager &operator=(const LockFileManager &); 7192bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7202bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor static llvm::Optional<std::pair<std::string, int> > 7212bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor readLockFile(StringRef LockFileName); 7222bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7232bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor static bool processStillExecuting(StringRef Hostname, int PID); 7242bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7252bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor public: 7262bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7272bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor LockFileManager(StringRef FileName); 7282bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor ~LockFileManager(); 7292bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7302bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// \brief Determine the state of the lock file. 7312bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor LockFileState getState() const; 7322bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7332bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor operator LockFileState() const { return getState(); } 7342bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7352bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /// \brief For a shared lock, wait until the owner releases the lock. 7362bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor void waitForUnlock(); 7372bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor }; 7382bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor} 7392bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7402bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor/// \brief Attempt to read the lock file with the given name, if it exists. 7412bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor/// 7422bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor/// \param LockFileName The name of the lock file to read. 7432bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor/// 7442bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor/// \returns The process ID of the process that owns this lock file 7452bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregorllvm::Optional<std::pair<std::string, int> > 7462bc750700487163e31e4f67cd0e0886868209e3cDouglas GregorLockFileManager::readLockFile(StringRef LockFileName) { 7472bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Check whether the lock file exists. If not, clearly there's nothing 7482bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // to read, so we just return. 7492bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor bool Exists = false; 7502bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if (llvm::sys::fs::exists(LockFileName, Exists) || !Exists) 7512bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return llvm::Optional<std::pair<std::string, int> >(); 7522bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7532bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Read the owning host and PID out of the lock file. If it appears that the 7542bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // owning process is dead, the lock file is invalid. 7552bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor int PID = 0; 7562bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor std::string Hostname; 7572bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor std::ifstream Input(LockFileName.str().c_str()); 7582bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if (Input >> Hostname >> PID && PID > 0 && 7592bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor processStillExecuting(Hostname, PID)) 7602bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return std::make_pair(Hostname, PID); 7612bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7622bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Delete the lock file. It's invalid anyway. 7632bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor bool Existed; 7642bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor llvm::sys::fs::remove(LockFileName, Existed); 7652bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return llvm::Optional<std::pair<std::string, int> >(); 7662bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor} 7672bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7682bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregorbool LockFileManager::processStillExecuting(StringRef Hostname, int PID) { 7692bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#if LLVM_ON_UNIX 7702bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor char MyHostname[256]; 7712bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor MyHostname[255] = 0; 7722bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor MyHostname[0] = 0; 7732bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor gethostname(MyHostname, 255); 7742bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Check whether the process is dead. If so, we're done. 7752bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if (MyHostname == Hostname && getsid(PID) == -1 && errno == ESRCH) 7762bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return false; 7772bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#endif 7782bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7792bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return true; 7802bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor} 7812bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7822bc750700487163e31e4f67cd0e0886868209e3cDouglas GregorLockFileManager::LockFileManager(StringRef FileName) 7832bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor{ 7842bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor LockFileName = FileName; 7852bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor LockFileName += ".lock"; 7862bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7872bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // If the lock file already exists, don't bother to try to create our own 7882bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // lock file; it won't work anyway. Just figure out who owns this lock file. 7892bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if ((Owner = readLockFile(LockFileName))) 7902bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return; 7912bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 7922bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Create a lock file that is unique to this instance. 7932bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor UniqueLockFileName = LockFileName; 7942bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor UniqueLockFileName += "-%%%%%%%%"; 7952bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor int UniqueLockFileID; 7962bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if (llvm::error_code EC 7972bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor = llvm::sys::fs::unique_file(UniqueLockFileName.str(), 7982bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor UniqueLockFileID, 7992bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor UniqueLockFileName, 8002bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor /*makeAbsolute=*/false)) { 8012bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor Error = EC; 8022bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return; 8032bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor } 8042bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8052bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Write our process ID to our unique lock file. 8062bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor { 8072bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor llvm::raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true); 8082bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8092bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#if LLVM_ON_UNIX 8102bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // FIXME: move getpid() call into LLVM 8112bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor char hostname[256]; 8122bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor hostname[255] = 0; 8132bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor hostname[0] = 0; 8142bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor gethostname(hostname, 255); 8152bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor Out << hostname << ' ' << getpid(); 8162bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#else 8172bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor Out << "localhost 1"; 8182bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#endif 8192bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor Out.close(); 8202bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8212bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if (Out.has_error()) { 8222bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // We failed to write out PID, so make up an excuse, remove the 8232bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // unique lock file, and fail. 8242bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor Error = llvm::make_error_code(llvm::errc::no_space_on_device); 8252bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor bool Existed; 8262bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor llvm::sys::fs::remove(UniqueLockFileName.c_str(), Existed); 8272bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return; 8282bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor } 8292bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor } 8302bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8312bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Create a hard link from the lock file name. If this succeeds, we're done. 8322bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor llvm::error_code EC 8332bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor = llvm::sys::fs::create_hard_link(UniqueLockFileName.str(), 8342bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor LockFileName.str()); 8352bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if (EC == llvm::errc::success) 8362bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return; 8372bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8382bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Creating the hard link failed. 8392bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8402bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#ifdef LLVM_ON_UNIX 8412bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // The creation of the hard link may appear to fail, but if stat'ing the 8422bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // unique file returns a link count of 2, then we can still declare success. 8432bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor struct stat StatBuf; 8442bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if (stat(UniqueLockFileName.c_str(), &StatBuf) == 0 && 8452bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor StatBuf.st_nlink == 2) 8462bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return; 8472bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#endif 8482bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8492bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Someone else managed to create the lock file first. Wipe out our unique 8502bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // lock file (it's useless now) and read the process ID from the lock file. 8512bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor bool Existed; 8522bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor llvm::sys::fs::remove(UniqueLockFileName.str(), Existed); 8532bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if ((Owner = readLockFile(LockFileName))) 8542bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return; 8552bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8562bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // There is a lock file that nobody owns; try to clean it up and report 8572bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // an error. 8582bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor llvm::sys::fs::remove(LockFileName.str(), Existed); 8592bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor Error = EC; 8602bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor} 8612bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8622bc750700487163e31e4f67cd0e0886868209e3cDouglas GregorLockFileManager::LockFileState LockFileManager::getState() const { 8632bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if (Owner) 8642bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return LFS_Shared; 8652bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8662bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if (Error) 8672bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return LFS_Error; 8682bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8692bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return LFS_Owned; 8702bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor} 8712bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8722bc750700487163e31e4f67cd0e0886868209e3cDouglas GregorLockFileManager::~LockFileManager() { 8732bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if (getState() != LFS_Owned) 8742bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return; 8752bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8762bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Since we own the lock, remove the lock file and our own unique lock file. 8772bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor bool Existed; 8782bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor llvm::sys::fs::remove(LockFileName.str(), Existed); 8792bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor llvm::sys::fs::remove(UniqueLockFileName.str(), Existed); 8802bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor} 8812bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8822bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregorvoid LockFileManager::waitForUnlock() { 8832bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if (getState() != LFS_Shared) 8842bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return; 8852bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 8861872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor#if LLVM_ON_WIN32 8871872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor unsigned long Interval = 1; 8881872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor#else 8892bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor struct timespec Interval; 8902bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor Interval.tv_sec = 0; 8912bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor Interval.tv_nsec = 1000000; 8921872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor#endif 8932bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Don't wait more than an hour for the file to appear. 8942bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor const unsigned MaxSeconds = 3600; 8952bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor do { 8962bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Sleep for the designated interval, to allow the owning process time to 8972bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // finish up and 8982bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // FIXME: Should we hook in to system APIs to get a notification when the 8992bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // lock file is deleted? 9002572849d5ee3e5729a89ca934e1b04199eec5a5bDouglas Gregor#if LLVM_ON_WIN32 9012572849d5ee3e5729a89ca934e1b04199eec5a5bDouglas Gregor Sleep(Interval); 9022572849d5ee3e5729a89ca934e1b04199eec5a5bDouglas Gregor#else 9032bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor nanosleep(&Interval, NULL); 9042572849d5ee3e5729a89ca934e1b04199eec5a5bDouglas Gregor#endif 9052bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // If the file no longer exists, we're done. 9062bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor bool Exists = false; 9072bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if (!llvm::sys::fs::exists(LockFileName.str(), Exists) && !Exists) 9082bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return; 9092bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 9102bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if (!processStillExecuting((*Owner).first, (*Owner).second)) 9112bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return; 9122bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 9132bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Exponentially increase the time we wait for the lock to be removed. 9141872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor#if LLVM_ON_WIN32 9151872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor Interval *= 2; 9161872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor#else 9172bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor Interval.tv_sec *= 2; 9182bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor Interval.tv_nsec *= 2; 9192bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor if (Interval.tv_nsec >= 1000000000) { 9202bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor ++Interval.tv_sec; 9212bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor Interval.tv_nsec -= 1000000000; 9222bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor } 9231872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor#endif 9241872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor } while ( 9251872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor#if LLVM_ON_WIN32 9261872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor Interval < MaxSeconds * 1000 9271872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor#else 9281872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor Interval.tv_sec < MaxSeconds 9291872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor#endif 9301872e7916ce14a0a0b697eed534e27cb4d2b9ec2Douglas Gregor ); 9312bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 9322bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Give up. 9332bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor} 9342bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 93521cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor/// \brief Compile a module file for the given module name with the given 93621cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor/// umbrella header, using the options provided by the importing compiler 93721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor/// instance. 93821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregorstatic void compileModule(CompilerInstance &ImportingInstance, 93921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor StringRef ModuleName, 9406e975c4517958bcc11c834336d340797356058dbDouglas Gregor StringRef ModuleFileName, 94121cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor StringRef UmbrellaHeader) { 9422bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor LockFileManager Locked(ModuleFileName); 9432bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor switch (Locked) { 9442bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor case LockFileManager::LFS_Error: 9452bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor return; 9462bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 9472bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor case LockFileManager::LFS_Owned: 9482bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // We're responsible for building the module ourselves. Do so below. 9492bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor break; 9502bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 9512bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor case LockFileManager::LFS_Shared: 9522bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // Someone else is responsible for building the module. Wait for them to 9532bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor // finish. 9542bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor Locked.waitForUnlock(); 9552bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor break; 9562bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor } 9572bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor 95821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor // Construct a compiler invocation for creating this module. 95921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation 96021cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor (new CompilerInvocation(ImportingInstance.getInvocation())); 961b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor 962b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor // For any options that aren't intended to affect how a module is built, 963b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor // reset them to their default values. 9641c7e0472f5683a8ade62285f366637050cf113e5Douglas Gregor Invocation->getLangOpts().resetNonModularOptions(); 9651c7e0472f5683a8ade62285f366637050cf113e5Douglas Gregor Invocation->getPreprocessorOpts().resetNonModularOptions(); 966b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor 967b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor // Note that this module is part of the module build path, so that we 968b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor // can detect cycles in the module graph. 9694ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor Invocation->getPreprocessorOpts().ModuleBuildPath.push_back(ModuleName); 9701c7e0472f5683a8ade62285f366637050cf113e5Douglas Gregor 971b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor // Set up the inputs/outputs so that we build the module from its umbrella 972b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor // header. 97321cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor FrontendOptions &FrontendOpts = Invocation->getFrontendOpts(); 9746e975c4517958bcc11c834336d340797356058dbDouglas Gregor FrontendOpts.OutputFile = ModuleFileName.str(); 97521cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor FrontendOpts.DisableFree = false; 97621cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor FrontendOpts.Inputs.clear(); 97721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor FrontendOpts.Inputs.push_back( 97821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor std::make_pair(getSourceInputKindFromOptions(Invocation->getLangOpts()), 97921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor UmbrellaHeader)); 98078243658c533168d51fd076fba328437932ba6f1Douglas Gregor 98178243658c533168d51fd076fba328437932ba6f1Douglas Gregor Invocation->getDiagnosticOpts().VerifyDiagnostics = 0; 98278243658c533168d51fd076fba328437932ba6f1Douglas Gregor 9834ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor 98476d991ec6c5cd247ee18fe65c35d43c2d47cf094Douglas Gregor assert(ImportingInstance.getInvocation().getModuleHash() == 98576d991ec6c5cd247ee18fe65c35d43c2d47cf094Douglas Gregor Invocation->getModuleHash() && "Module hash mismatch!"); 98621cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor 98721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor // Construct a compiler instance that will be used to actually create the 98821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor // module. 98921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor CompilerInstance Instance; 99021cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor Instance.setInvocation(&*Invocation); 99178243658c533168d51fd076fba328437932ba6f1Douglas Gregor Instance.createDiagnostics(/*argc=*/0, /*argv=*/0, 99278243658c533168d51fd076fba328437932ba6f1Douglas Gregor &ImportingInstance.getDiagnosticClient(), 993aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor /*ShouldOwnClient=*/true, 994aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor /*ShouldCloneClient=*/true); 99521cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor 99621cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor // Construct a module-generating action. 99721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor GeneratePCHAction CreateModuleAction(true); 99821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor 9990ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor // Execute the action to actually build the module in-place. Use a separate 10000ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor // thread so that we get a stack large enough. 10010ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor const unsigned ThreadStackSize = 8 << 20; 10020ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor llvm::CrashRecoveryContext CRC; 10030ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor CompileModuleData Data = { Instance, CreateModuleAction }; 10040ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor CRC.RunSafelyOnThread(&doCompileModule, &Data, ThreadStackSize); 100578243658c533168d51fd076fba328437932ba6f1Douglas Gregor} 100621cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor 10076aa52ec6b969faabf3764baf79d89810b8249a7eDouglas GregorModuleKey CompilerInstance::loadModule(SourceLocation ImportLoc, 10086aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor IdentifierInfo &ModuleName, 10096aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor SourceLocation ModuleNameLoc) { 10106aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor // Determine what file we're searching from. 10116aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor SourceManager &SourceMgr = getSourceManager(); 10126aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor SourceLocation ExpandedImportLoc = SourceMgr.getExpansionLoc(ImportLoc); 10136aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor const FileEntry *CurFile 10146aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor = SourceMgr.getFileEntryForID(SourceMgr.getFileID(ExpandedImportLoc)); 10156aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor if (!CurFile) 10166aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor CurFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()); 10176aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 10186aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor // Search for a module with the given name. 101921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor std::string UmbrellaHeader; 10206e975c4517958bcc11c834336d340797356058dbDouglas Gregor std::string ModuleFileName; 10216aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor const FileEntry *ModuleFile 102221cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor = PP->getHeaderSearchInfo().lookupModule(ModuleName.getName(), 10236e975c4517958bcc11c834336d340797356058dbDouglas Gregor &ModuleFileName, 102421cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor &UmbrellaHeader); 102521cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor 102621cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor bool BuildingModule = false; 102721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor if (!ModuleFile && !UmbrellaHeader.empty()) { 102821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor // We didn't find the module, but there is an umbrella header that 102921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor // can be used to create the module file. Create a separate compilation 103021cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor // module to do so. 10314ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor 10324ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor // Check whether there is a cycle in the module graph. 10334ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor SmallVectorImpl<std::string> &ModuleBuildPath 10344ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor = getPreprocessorOpts().ModuleBuildPath; 10354ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor SmallVectorImpl<std::string>::iterator Pos 10364ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor = std::find(ModuleBuildPath.begin(), ModuleBuildPath.end(), 10374ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor ModuleName.getName()); 10384ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor if (Pos != ModuleBuildPath.end()) { 10394ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor llvm::SmallString<256> CyclePath; 10404ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor for (; Pos != ModuleBuildPath.end(); ++Pos) { 10414ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor CyclePath += *Pos; 10424ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor CyclePath += " -> "; 10434ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor } 10444ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor CyclePath += ModuleName.getName(); 10454ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor 10464ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle) 10474ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor << ModuleName.getName() << CyclePath; 10484ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor return 0; 10494ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor } 10504ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor 105108d6acf6e5efa6df57345922d412476127f856fcDouglas Gregor getDiagnostics().Report(ModuleNameLoc, diag::warn_module_build) 105208d6acf6e5efa6df57345922d412476127f856fcDouglas Gregor << ModuleName.getName(); 105321cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor BuildingModule = true; 10546e975c4517958bcc11c834336d340797356058dbDouglas Gregor compileModule(*this, ModuleName.getName(), ModuleFileName, UmbrellaHeader); 105521cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor ModuleFile = PP->getHeaderSearchInfo().lookupModule(ModuleName.getName()); 105621cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor } 105721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor 10586aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor if (!ModuleFile) { 105921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor getDiagnostics().Report(ModuleNameLoc, 106021cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor BuildingModule? diag::err_module_not_built 106121cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor : diag::err_module_not_found) 10626aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor << ModuleName.getName() 10636aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor << SourceRange(ImportLoc, ModuleNameLoc); 10646aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor return 0; 10656aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor } 10666aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 10676aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor // If we don't already have an ASTReader, create one now. 10686aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor if (!ModuleManager) { 1069de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor if (!hasASTContext()) 1070de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor createASTContext(); 1071de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor 10726aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor std::string Sysroot = getHeaderSearchOpts().Sysroot; 10736aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor const PreprocessorOptions &PPOpts = getPreprocessorOpts(); 1074f8a1e51c48761ee1d7803c3fa35ac94f42ebb55eDouglas Gregor ModuleManager = new ASTReader(getPreprocessor(), *Context, 10756aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor Sysroot.empty() ? "" : Sysroot.c_str(), 10766aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor PPOpts.DisablePCHValidation, 10776aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor PPOpts.DisableStatCache); 1078de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor if (hasASTConsumer()) { 1079de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor ModuleManager->setDeserializationListener( 1080de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor getASTConsumer().GetASTDeserializationListener()); 1081de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor getASTContext().setASTMutationListener( 1082de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor getASTConsumer().GetASTMutationListener()); 1083de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor } 10846aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor llvm::OwningPtr<ExternalASTSource> Source; 10856aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor Source.reset(ModuleManager); 10866aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor getASTContext().setExternalSource(Source); 1087de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor if (hasSema()) 1088de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor ModuleManager->InitializeSema(getSema()); 10891a995ddaa53a20dcd063ea47eb1f533ecb0d243aDouglas Gregor if (hasASTConsumer()) 10901a995ddaa53a20dcd063ea47eb1f533ecb0d243aDouglas Gregor ModuleManager->StartTranslationUnit(&getASTConsumer()); 10916aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor } 10926aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 10936aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor // Try to load the module we found. 10946aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor switch (ModuleManager->ReadAST(ModuleFile->getName(), 10956aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor serialization::MK_Module)) { 10966aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor case ASTReader::Success: 10976aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor break; 10986aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 10996aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor case ASTReader::IgnorePCH: 11006aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor // FIXME: The ASTReader will already have complained, but can we showhorn 11016aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor // that diagnostic information into a more useful form? 11026aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor return 0; 11036aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 11046aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor case ASTReader::Failure: 11056aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor // Already complained. 11066aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor return 0; 11076aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor } 11086aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor 11096aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor // FIXME: The module file's FileEntry makes a poor key indeed! 11106aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor return (ModuleKey)ModuleFile; 11116aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor} 11120397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar 1113