Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 1 | //===--- CompilerInstance.cpp ---------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "clang/Frontend/CompilerInstance.h" |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 11 | #include "clang/Sema/Sema.h" |
Daniel Dunbar | 12ce694 | 2009-11-14 02:47:17 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | 5eb8100 | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Diagnostic.h" |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Version.h" |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 19 | #include "clang/Lex/HeaderSearch.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Lex/PTHManager.h" |
David Blaikie | 4e85b8a | 2011-09-26 00:21:47 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/ChainedDiagnosticConsumer.h" |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/FrontendAction.h" |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/LogDiagnosticPrinter.h" |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/SerializedDiagnosticPrinter.h" |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/VerifyDiagnosticConsumer.h" |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/Utils.h" |
Sebastian Redl | 6ab7cd8 | 2010-08-18 23:57:17 +0000 | [diff] [blame] | 31 | #include "clang/Serialization/ASTReader.h" |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 32 | #include "clang/Sema/CodeCompleteConsumer.h" |
Michael J. Spencer | 32bef4e | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 33 | #include "llvm/Support/FileSystem.h" |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 95dd558 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/Statistic.h" |
Kovarththanan Rajaratnam | f79bafa | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Timer.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Host.h" |
| 39 | #include "llvm/Support/Path.h" |
| 40 | #include "llvm/Support/Program.h" |
| 41 | #include "llvm/Support/Signals.h" |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 42 | #include "llvm/Support/system_error.h" |
Douglas Gregor | 0ced799 | 2011-10-04 00:21:21 +0000 | [diff] [blame] | 43 | #include "llvm/Support/CrashRecoveryContext.h" |
Dylan Noblesmith | 1626601 | 2011-06-23 12:20:57 +0000 | [diff] [blame] | 44 | #include "llvm/Config/config.h" |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 45 | |
| 46 | // Support for FileLockManager |
| 47 | #include <fstream> |
| 48 | #include <sys/types.h> |
| 49 | #include <sys/stat.h> |
| 50 | |
Douglas Gregor | 2572849 | 2011-10-05 14:59:36 +0000 | [diff] [blame] | 51 | #if LLVM_ON_WIN32 |
| 52 | #include <windows.h> |
| 53 | #endif |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 54 | #if LLVM_ON_UNIX |
| 55 | #include <unistd.h> |
| 56 | #endif |
| 57 | |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 58 | using namespace clang; |
| 59 | |
Daniel Dunbar | 42e9f8e4 | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 60 | CompilerInstance::CompilerInstance() |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 61 | : Invocation(new CompilerInvocation()), ModuleManager(0) { |
Daniel Dunbar | 6228ca0 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 62 | } |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 63 | |
| 64 | CompilerInstance::~CompilerInstance() { |
Daniel Dunbar | 42e9f8e4 | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Daniel Dunbar | 6228ca0 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 67 | void CompilerInstance::setInvocation(CompilerInvocation *Value) { |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 68 | Invocation = Value; |
Daniel Dunbar | 6228ca0 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 69 | } |
| 70 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 71 | void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) { |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 72 | Diagnostics = Value; |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 75 | void CompilerInstance::setTarget(TargetInfo *Value) { |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 76 | Target = Value; |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void CompilerInstance::setFileManager(FileManager *Value) { |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 80 | FileMgr = Value; |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 81 | } |
| 82 | |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 83 | void CompilerInstance::setSourceManager(SourceManager *Value) { |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 84 | SourceMgr = Value; |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 87 | void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; } |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 89 | void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; } |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 90 | |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 91 | void CompilerInstance::setSema(Sema *S) { |
| 92 | TheSema.reset(S); |
| 93 | } |
| 94 | |
Daniel Dunbar | 12ce694 | 2009-11-14 02:47:17 +0000 | [diff] [blame] | 95 | void CompilerInstance::setASTConsumer(ASTConsumer *Value) { |
| 96 | Consumer.reset(Value); |
| 97 | } |
| 98 | |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 99 | void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) { |
| 100 | CompletionConsumer.reset(Value); |
| 101 | } |
| 102 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 103 | // Diagnostics |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 104 | static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts, |
Axel Naumann | 7d0c4cc | 2010-10-11 09:13:46 +0000 | [diff] [blame] | 105 | unsigned argc, const char* const *argv, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 106 | DiagnosticsEngine &Diags) { |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 107 | std::string ErrorInfo; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 108 | llvm::OwningPtr<raw_ostream> OS( |
Kovarththanan Rajaratnam | 6924713 | 2010-03-17 09:47:30 +0000 | [diff] [blame] | 109 | new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo)); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 110 | if (!ErrorInfo.empty()) { |
Kovarththanan Rajaratnam | 3d67b1e | 2010-03-17 09:24:48 +0000 | [diff] [blame] | 111 | Diags.Report(diag::err_fe_unable_to_open_logfile) |
| 112 | << DiagOpts.DumpBuildInformation << ErrorInfo; |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | |
Daniel Dunbar | dd63b28 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 116 | (*OS) << "clang -cc1 command line arguments: "; |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 117 | for (unsigned i = 0; i != argc; ++i) |
| 118 | (*OS) << argv[i] << ' '; |
| 119 | (*OS) << '\n'; |
| 120 | |
| 121 | // Chain in a diagnostic client which will log the diagnostics. |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 122 | DiagnosticConsumer *Logger = |
Kovarththanan Rajaratnam | 6924713 | 2010-03-17 09:47:30 +0000 | [diff] [blame] | 123 | new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true); |
David Blaikie | 4e85b8a | 2011-09-26 00:21:47 +0000 | [diff] [blame] | 124 | Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger)); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 127 | static void SetUpDiagnosticLog(const DiagnosticOptions &DiagOpts, |
Daniel Dunbar | b6534bb | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 128 | const CodeGenOptions *CodeGenOpts, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 129 | DiagnosticsEngine &Diags) { |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 130 | std::string ErrorInfo; |
| 131 | bool OwnsStream = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 132 | raw_ostream *OS = &llvm::errs(); |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 133 | if (DiagOpts.DiagnosticLogFile != "-") { |
| 134 | // Create the output stream. |
| 135 | llvm::raw_fd_ostream *FileOS( |
| 136 | new llvm::raw_fd_ostream(DiagOpts.DiagnosticLogFile.c_str(), |
Daniel Dunbar | e01eceb | 2011-04-07 20:19:21 +0000 | [diff] [blame] | 137 | ErrorInfo, llvm::raw_fd_ostream::F_Append)); |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 138 | if (!ErrorInfo.empty()) { |
| 139 | Diags.Report(diag::warn_fe_cc_log_diagnostics_failure) |
| 140 | << DiagOpts.DumpBuildInformation << ErrorInfo; |
| 141 | } else { |
| 142 | FileOS->SetUnbuffered(); |
| 143 | FileOS->SetUseAtomicWrites(true); |
| 144 | OS = FileOS; |
| 145 | OwnsStream = true; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Chain in the diagnostic client which will log the diagnostics. |
Daniel Dunbar | b6534bb | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 150 | LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts, |
| 151 | OwnsStream); |
| 152 | if (CodeGenOpts) |
| 153 | Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags); |
David Blaikie | 4e85b8a | 2011-09-26 00:21:47 +0000 | [diff] [blame] | 154 | Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger)); |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 157 | static void SetupSerializedDiagnostics(const DiagnosticOptions &DiagOpts, |
| 158 | DiagnosticsEngine &Diags, |
| 159 | StringRef OutputFile) { |
| 160 | std::string ErrorInfo; |
| 161 | llvm::OwningPtr<llvm::raw_fd_ostream> OS; |
| 162 | OS.reset(new llvm::raw_fd_ostream(OutputFile.str().c_str(), ErrorInfo, |
| 163 | llvm::raw_fd_ostream::F_Binary)); |
| 164 | |
| 165 | if (!ErrorInfo.empty()) { |
| 166 | Diags.Report(diag::warn_fe_serialized_diag_failure) |
| 167 | << OutputFile << ErrorInfo; |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | DiagnosticConsumer *SerializedConsumer = |
| 172 | clang::serialized_diags::create(OS.take(), Diags); |
| 173 | |
| 174 | |
| 175 | Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), |
| 176 | SerializedConsumer)); |
| 177 | } |
| 178 | |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 179 | void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv, |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 180 | DiagnosticConsumer *Client, |
Douglas Gregor | aee526e | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 181 | bool ShouldOwnClient, |
| 182 | bool ShouldCloneClient) { |
Daniel Dunbar | b6534bb | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 183 | Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client, |
Douglas Gregor | aee526e | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 184 | ShouldOwnClient, ShouldCloneClient, |
| 185 | &getCodeGenOpts()); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 186 | } |
| 187 | |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 188 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 189 | CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts, |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 190 | int Argc, const char* const *Argv, |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 191 | DiagnosticConsumer *Client, |
Douglas Gregor | 7824365 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 192 | bool ShouldOwnClient, |
Douglas Gregor | aee526e | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 193 | bool ShouldCloneClient, |
Daniel Dunbar | b6534bb | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 194 | const CodeGenOptions *CodeGenOpts) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 195 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 196 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> |
| 197 | Diags(new DiagnosticsEngine(DiagID)); |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 198 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 199 | // Create the diagnostic client for reporting errors or for |
| 200 | // implementing -verify. |
Douglas Gregor | aee526e | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 201 | if (Client) { |
| 202 | if (ShouldCloneClient) |
| 203 | Diags->setClient(Client->clone(*Diags), ShouldOwnClient); |
| 204 | else |
| 205 | Diags->setClient(Client, ShouldOwnClient); |
| 206 | } else |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 207 | Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts)); |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 208 | |
| 209 | // Chain in -verify checker, if requested. |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 210 | if (Opts.VerifyDiagnostics) |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 211 | Diags->setClient(new VerifyDiagnosticConsumer(*Diags)); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 212 | |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 213 | // Chain in -diagnostic-log-file dumper, if requested. |
| 214 | if (!Opts.DiagnosticLogFile.empty()) |
Daniel Dunbar | b6534bb | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 215 | SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 216 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 217 | if (!Opts.DumpBuildInformation.empty()) |
Kovarththanan Rajaratnam | 3d67b1e | 2010-03-17 09:24:48 +0000 | [diff] [blame] | 218 | SetUpBuildDumpLog(Opts, Argc, Argv, *Diags); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 219 | |
Ted Kremenek | 7800212 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 220 | if (!Opts.DiagnosticSerializationFile.empty()) |
| 221 | SetupSerializedDiagnostics(Opts, *Diags, |
| 222 | Opts.DiagnosticSerializationFile); |
| 223 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 224 | // Configure our handling of diagnostics. |
Kovarththanan Rajaratnam | 5bf932b | 2010-03-17 09:36:02 +0000 | [diff] [blame] | 225 | ProcessWarningOptions(*Diags, Opts); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 226 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 227 | return Diags; |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // File Manager |
| 231 | |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 232 | void CompilerInstance::createFileManager() { |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 233 | FileMgr = new FileManager(getFileSystemOpts()); |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 236 | // Source Manager |
| 237 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 238 | void CompilerInstance::createSourceManager(FileManager &FileMgr) { |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 239 | SourceMgr = new SourceManager(getDiagnostics(), FileMgr); |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 240 | } |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 241 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 242 | // Preprocessor |
| 243 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 244 | void CompilerInstance::createPreprocessor() { |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 245 | const PreprocessorOptions &PPOpts = getPreprocessorOpts(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 246 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 247 | // Create a PTH manager if we are using some form of a token cache. |
| 248 | PTHManager *PTHMgr = 0; |
Daniel Dunbar | 049d3a0 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 249 | if (!PPOpts.TokenCache.empty()) |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 250 | PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics()); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 251 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 252 | // Create the Preprocessor. |
Douglas Gregor | 8e23806 | 2011-11-11 00:35:06 +0000 | [diff] [blame] | 253 | HeaderSearch *HeaderInfo = new HeaderSearch(getFileManager(), |
| 254 | getDiagnostics()); |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 255 | PP = new Preprocessor(getDiagnostics(), getLangOpts(), &getTarget(), |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 256 | getSourceManager(), *HeaderInfo, *this, PTHMgr, |
| 257 | /*OwnsHeaderSearch=*/true); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 258 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 259 | // Note that this is different then passing PTHMgr to Preprocessor's ctor. |
| 260 | // That argument is used as the IdentifierInfoLookup argument to |
| 261 | // IdentifierTable's ctor. |
| 262 | if (PTHMgr) { |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 263 | PTHMgr->setPreprocessor(&*PP); |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 264 | PP->setPTHManager(PTHMgr); |
| 265 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 266 | |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 267 | if (PPOpts.DetailedRecord) |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 268 | PP->createPreprocessingRecord( |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 269 | PPOpts.DetailedRecordIncludesNestedMacroExpansions); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 270 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 271 | InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts()); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 272 | |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 273 | // Set up the module path, including the hash for the |
| 274 | // module-creation options. |
| 275 | llvm::SmallString<256> SpecificModuleCache( |
| 276 | getHeaderSearchOpts().ModuleCachePath); |
| 277 | if (!getHeaderSearchOpts().DisableModuleHash) |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 278 | llvm::sys::path::append(SpecificModuleCache, |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 279 | getInvocation().getModuleHash()); |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 280 | PP->getHeaderSearchInfo().configureModules(SpecificModuleCache, |
Douglas Gregor | b86b8dc | 2011-11-15 19:35:01 +0000 | [diff] [blame] | 281 | getLangOpts().CurrentModule); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 282 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 283 | // Handle generating dependencies, if requested. |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 284 | const DependencyOutputOptions &DepOpts = getDependencyOutputOpts(); |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 285 | if (!DepOpts.OutputFile.empty()) |
| 286 | AttachDependencyFileGen(*PP, DepOpts); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 287 | |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 288 | // Handle generating header include information, if requested. |
| 289 | if (DepOpts.ShowHeaderIncludes) |
| 290 | AttachHeaderIncludeGen(*PP); |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 291 | if (!DepOpts.HeaderIncludeOutputFile.empty()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 292 | StringRef OutputPath = DepOpts.HeaderIncludeOutputFile; |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 293 | if (OutputPath == "-") |
| 294 | OutputPath = ""; |
Daniel Dunbar | da60885 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 295 | AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath, |
| 296 | /*ShowDepth=*/false); |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 297 | } |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 298 | } |
Daniel Dunbar | 5eb8100 | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 299 | |
| 300 | // ASTContext |
| 301 | |
| 302 | void CompilerInstance::createASTContext() { |
| 303 | Preprocessor &PP = getPreprocessor(); |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 304 | Context = new ASTContext(getLangOpts(), PP.getSourceManager(), |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 305 | &getTarget(), PP.getIdentifierTable(), |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 306 | PP.getSelectorTable(), PP.getBuiltinInfo(), |
| 307 | /*size_reserve=*/ 0); |
Daniel Dunbar | 5eb8100 | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 308 | } |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 309 | |
| 310 | // ExternalASTSource |
| 311 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 312 | void CompilerInstance::createPCHExternalASTSource(StringRef Path, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 313 | bool DisablePCHValidation, |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 314 | bool DisableStatCache, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 315 | void *DeserializationListener){ |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 316 | llvm::OwningPtr<ExternalASTSource> Source; |
Sebastian Redl | 1d9f1fe | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 317 | bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 318 | Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot, |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 319 | DisablePCHValidation, |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 320 | DisableStatCache, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 321 | getPreprocessor(), getASTContext(), |
Sebastian Redl | 1d9f1fe | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 322 | DeserializationListener, |
| 323 | Preamble)); |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 324 | ModuleManager = static_cast<ASTReader*>(Source.get()); |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 325 | getASTContext().setExternalSource(Source); |
| 326 | } |
| 327 | |
| 328 | ExternalASTSource * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 329 | CompilerInstance::createPCHExternalASTSource(StringRef Path, |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 330 | const std::string &Sysroot, |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 331 | bool DisablePCHValidation, |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 332 | bool DisableStatCache, |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 333 | Preprocessor &PP, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 334 | ASTContext &Context, |
Sebastian Redl | 1d9f1fe | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 335 | void *DeserializationListener, |
| 336 | bool Preamble) { |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 337 | llvm::OwningPtr<ASTReader> Reader; |
Douglas Gregor | f8a1e51 | 2011-09-02 00:26:20 +0000 | [diff] [blame] | 338 | Reader.reset(new ASTReader(PP, Context, |
Douglas Gregor | 832d620 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 339 | Sysroot.empty() ? "" : Sysroot.c_str(), |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 340 | DisablePCHValidation, DisableStatCache)); |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 341 | |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 342 | Reader->setDeserializationListener( |
Sebastian Redl | 571db7f | 2010-08-18 23:56:56 +0000 | [diff] [blame] | 343 | static_cast<ASTDeserializationListener *>(DeserializationListener)); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 344 | switch (Reader->ReadAST(Path, |
| 345 | Preamble ? serialization::MK_Preamble |
Douglas Gregor | 72a9ae1 | 2011-07-22 16:00:58 +0000 | [diff] [blame] | 346 | : serialization::MK_PCH)) { |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 347 | case ASTReader::Success: |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 348 | // Set the predefines buffer as suggested by the PCH reader. Typically, the |
| 349 | // predefines buffer will be empty. |
| 350 | PP.setPredefines(Reader->getSuggestedPredefines()); |
| 351 | return Reader.take(); |
| 352 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 353 | case ASTReader::Failure: |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 354 | // Unrecoverable failure: don't even try to process the input file. |
| 355 | break; |
| 356 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 357 | case ASTReader::IgnorePCH: |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 358 | // No suitable PCH file could be found. Return an error. |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | return 0; |
| 363 | } |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 364 | |
| 365 | // Code Completion |
| 366 | |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 367 | static bool EnableCodeCompletion(Preprocessor &PP, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 368 | const std::string &Filename, |
| 369 | unsigned Line, |
| 370 | unsigned Column) { |
| 371 | // Tell the source manager to chop off the given file at a specific |
| 372 | // line and column. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 373 | const FileEntry *Entry = PP.getFileManager().getFile(Filename); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 374 | if (!Entry) { |
| 375 | PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) |
| 376 | << Filename; |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | // Truncate the named file at the given line/column. |
| 381 | PP.SetCodeCompletionPoint(Entry, Line, Column); |
| 382 | return false; |
| 383 | } |
| 384 | |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 385 | void CompilerInstance::createCodeCompletionConsumer() { |
| 386 | const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 387 | if (!CompletionConsumer) { |
| 388 | CompletionConsumer.reset( |
| 389 | createCodeCompletionConsumer(getPreprocessor(), |
| 390 | Loc.FileName, Loc.Line, Loc.Column, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 391 | getFrontendOpts().ShowMacrosInCodeCompletion, |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 392 | getFrontendOpts().ShowCodePatternsInCodeCompletion, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 393 | getFrontendOpts().ShowGlobalSymbolsInCodeCompletion, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 394 | llvm::outs())); |
| 395 | if (!CompletionConsumer) |
| 396 | return; |
| 397 | } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName, |
| 398 | Loc.Line, Loc.Column)) { |
| 399 | CompletionConsumer.reset(); |
Douglas Gregor | c3d43b7 | 2010-03-16 06:04:47 +0000 | [diff] [blame] | 400 | return; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 401 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 402 | |
| 403 | if (CompletionConsumer->isOutputBinary() && |
| 404 | llvm::sys::Program::ChangeStdoutToBinary()) { |
| 405 | getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary); |
| 406 | CompletionConsumer.reset(); |
| 407 | } |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Kovarththanan Rajaratnam | f79bafa | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 410 | void CompilerInstance::createFrontendTimer() { |
| 411 | FrontendTimer.reset(new llvm::Timer("Clang front-end timer")); |
| 412 | } |
| 413 | |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 414 | CodeCompleteConsumer * |
| 415 | CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP, |
| 416 | const std::string &Filename, |
| 417 | unsigned Line, |
| 418 | unsigned Column, |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 419 | bool ShowMacros, |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 420 | bool ShowCodePatterns, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 421 | bool ShowGlobals, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 422 | raw_ostream &OS) { |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 423 | if (EnableCodeCompletion(PP, Filename, Line, Column)) |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 424 | return 0; |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 425 | |
| 426 | // Set up the creation routine for code-completion. |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 427 | return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 428 | ShowGlobals, OS); |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 429 | } |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 430 | |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 431 | void CompilerInstance::createSema(TranslationUnitKind TUKind, |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 432 | CodeCompleteConsumer *CompletionConsumer) { |
| 433 | TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(), |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 434 | TUKind, CompletionConsumer)); |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 437 | // Output Files |
| 438 | |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 439 | void CompilerInstance::addOutputFile(const OutputFile &OutFile) { |
| 440 | assert(OutFile.OS && "Attempt to add empty stream to output list!"); |
| 441 | OutputFiles.push_back(OutFile); |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Kovarththanan Rajaratnam | e51dd7b | 2010-03-06 12:07:48 +0000 | [diff] [blame] | 444 | void CompilerInstance::clearOutputFiles(bool EraseFiles) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 445 | for (std::list<OutputFile>::iterator |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 446 | it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 447 | delete it->OS; |
| 448 | if (!it->TempFilename.empty()) { |
Anders Carlsson | af036a6 | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 449 | if (EraseFiles) { |
| 450 | bool existed; |
| 451 | llvm::sys::fs::remove(it->TempFilename, existed); |
| 452 | } else { |
| 453 | llvm::SmallString<128> NewOutFile(it->Filename); |
| 454 | |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 455 | // If '-working-directory' was passed, the output filename should be |
| 456 | // relative to that. |
Anders Carlsson | 2e2468e | 2011-03-14 01:13:54 +0000 | [diff] [blame] | 457 | FileMgr->FixupRelativePath(NewOutFile); |
Anders Carlsson | af036a6 | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 458 | if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename, |
| 459 | NewOutFile.str())) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 460 | getDiagnostics().Report(diag::err_fe_unable_to_rename_temp) |
Anders Carlsson | af036a6 | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 461 | << it->TempFilename << it->Filename << ec.message(); |
| 462 | |
| 463 | bool existed; |
| 464 | llvm::sys::fs::remove(it->TempFilename, existed); |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | } else if (!it->Filename.empty() && EraseFiles) |
| 468 | llvm::sys::Path(it->Filename).eraseFromDisk(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 469 | |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 470 | } |
| 471 | OutputFiles.clear(); |
| 472 | } |
| 473 | |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 474 | llvm::raw_fd_ostream * |
| 475 | CompilerInstance::createDefaultOutputFile(bool Binary, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 476 | StringRef InFile, |
| 477 | StringRef Extension) { |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 478 | return createOutputFile(getFrontendOpts().OutputFile, Binary, |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 479 | /*RemoveFileOnSignal=*/true, InFile, Extension); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | llvm::raw_fd_ostream * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 483 | CompilerInstance::createOutputFile(StringRef OutputPath, |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 484 | bool Binary, bool RemoveFileOnSignal, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 485 | StringRef InFile, |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 486 | StringRef Extension, |
| 487 | bool UseTemporary) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 488 | std::string Error, OutputPathName, TempPathName; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 489 | llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary, |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 490 | RemoveFileOnSignal, |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 491 | InFile, Extension, |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 492 | UseTemporary, |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 493 | &OutputPathName, |
| 494 | &TempPathName); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 495 | if (!OS) { |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 496 | getDiagnostics().Report(diag::err_fe_unable_to_open_output) |
| 497 | << OutputPath << Error; |
| 498 | return 0; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // Add the output file -- but don't try to remove "-", since this means we are |
| 502 | // using stdin. |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 503 | addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "", |
| 504 | TempPathName, OS)); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 505 | |
| 506 | return OS; |
| 507 | } |
| 508 | |
| 509 | llvm::raw_fd_ostream * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 510 | CompilerInstance::createOutputFile(StringRef OutputPath, |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 511 | std::string &Error, |
| 512 | bool Binary, |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 513 | bool RemoveFileOnSignal, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 514 | StringRef InFile, |
| 515 | StringRef Extension, |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 516 | bool UseTemporary, |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 517 | std::string *ResultPathName, |
| 518 | std::string *TempPathName) { |
| 519 | std::string OutFile, TempFile; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 520 | if (!OutputPath.empty()) { |
| 521 | OutFile = OutputPath; |
| 522 | } else if (InFile == "-") { |
| 523 | OutFile = "-"; |
| 524 | } else if (!Extension.empty()) { |
| 525 | llvm::sys::Path Path(InFile); |
| 526 | Path.eraseSuffix(); |
| 527 | Path.appendSuffix(Extension); |
| 528 | OutFile = Path.str(); |
| 529 | } else { |
| 530 | OutFile = "-"; |
| 531 | } |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 532 | |
| 533 | llvm::OwningPtr<llvm::raw_fd_ostream> OS; |
| 534 | std::string OSFile; |
| 535 | |
| 536 | if (UseTemporary && OutFile != "-") { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 537 | llvm::sys::Path OutPath(OutFile); |
| 538 | // Only create the temporary if we can actually write to OutPath, otherwise |
| 539 | // we want to fail early. |
Michael J. Spencer | 32bef4e | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 540 | bool Exists; |
| 541 | if ((llvm::sys::fs::exists(OutPath.str(), Exists) || !Exists) || |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 542 | (OutPath.isRegularFile() && OutPath.canWrite())) { |
| 543 | // Create a temporary file. |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 544 | llvm::SmallString<128> TempPath; |
| 545 | TempPath = OutFile; |
| 546 | TempPath += "-%%%%%%%%"; |
| 547 | int fd; |
| 548 | if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath, |
| 549 | /*makeAbsolute=*/false) == llvm::errc::success) { |
| 550 | OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true)); |
| 551 | OSFile = TempFile = TempPath.str(); |
| 552 | } |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 556 | if (!OS) { |
| 557 | OSFile = OutFile; |
| 558 | OS.reset( |
| 559 | new llvm::raw_fd_ostream(OSFile.c_str(), Error, |
| 560 | (Binary ? llvm::raw_fd_ostream::F_Binary : 0))); |
| 561 | if (!Error.empty()) |
| 562 | return 0; |
| 563 | } |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 564 | |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 565 | // Make sure the out stream file gets removed if we crash. |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 566 | if (RemoveFileOnSignal) |
| 567 | llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile)); |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 568 | |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 569 | if (ResultPathName) |
| 570 | *ResultPathName = OutFile; |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 571 | if (TempPathName) |
| 572 | *TempPathName = TempFile; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 573 | |
Daniel Dunbar | fc97102 | 2009-11-20 22:32:38 +0000 | [diff] [blame] | 574 | return OS.take(); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 575 | } |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 576 | |
| 577 | // Initialization Utilities |
| 578 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 579 | bool CompilerInstance::InitializeSourceManager(StringRef InputFile) { |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 580 | return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(), |
| 581 | getSourceManager(), getFrontendOpts()); |
| 582 | } |
| 583 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 584 | bool CompilerInstance::InitializeSourceManager(StringRef InputFile, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 585 | DiagnosticsEngine &Diags, |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 586 | FileManager &FileMgr, |
| 587 | SourceManager &SourceMgr, |
| 588 | const FrontendOptions &Opts) { |
Argyrios Kyrtzidis | 507097e | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 589 | // Figure out where to get and map in the main file. |
| 590 | if (InputFile != "-") { |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 591 | const FileEntry *File = FileMgr.getFile(InputFile); |
Dan Gohman | 694137c | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 592 | if (!File) { |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 593 | Diags.Report(diag::err_fe_error_reading) << InputFile; |
| 594 | return false; |
| 595 | } |
Dan Gohman | 694137c | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 596 | SourceMgr.createMainFileID(File); |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 597 | } else { |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 598 | llvm::OwningPtr<llvm::MemoryBuffer> SB; |
| 599 | if (llvm::MemoryBuffer::getSTDIN(SB)) { |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 600 | // FIXME: Give ec.message() in this diag. |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 601 | Diags.Report(diag::err_fe_error_reading_stdin); |
| 602 | return false; |
| 603 | } |
Dan Gohman | 90d9081 | 2010-10-26 23:21:25 +0000 | [diff] [blame] | 604 | const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(), |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 605 | SB->getBufferSize(), 0); |
Dan Gohman | 90d9081 | 2010-10-26 23:21:25 +0000 | [diff] [blame] | 606 | SourceMgr.createMainFileID(File); |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 607 | SourceMgr.overrideFileContents(File, SB.take()); |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Dan Gohman | 694137c | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 610 | assert(!SourceMgr.getMainFileID().isInvalid() && |
| 611 | "Couldn't establish MainFileID!"); |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 612 | return true; |
| 613 | } |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 614 | |
| 615 | // High-Level Operations |
| 616 | |
| 617 | bool CompilerInstance::ExecuteAction(FrontendAction &Act) { |
| 618 | assert(hasDiagnostics() && "Diagnostics engine is not initialized!"); |
| 619 | assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!"); |
| 620 | assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!"); |
| 621 | |
| 622 | // FIXME: Take this as an argument, once all the APIs we used have moved to |
| 623 | // taking it as an input instead of hard-coding llvm::errs. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 624 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 625 | |
| 626 | // Create the target instance. |
| 627 | setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts())); |
| 628 | if (!hasTarget()) |
| 629 | return false; |
| 630 | |
| 631 | // Inform the target of the language options. |
| 632 | // |
| 633 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 634 | // created. This complexity should be lifted elsewhere. |
| 635 | getTarget().setForcedLangOptions(getLangOpts()); |
| 636 | |
| 637 | // Validate/process some options. |
| 638 | if (getHeaderSearchOpts().Verbose) |
| 639 | OS << "clang -cc1 version " CLANG_VERSION_STRING |
| 640 | << " based upon " << PACKAGE_STRING |
Sebastian Pop | 5d8b954 | 2011-11-01 21:33:06 +0000 | [diff] [blame] | 641 | << " default target " << llvm::sys::getDefaultTargetTriple() << "\n"; |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 642 | |
| 643 | if (getFrontendOpts().ShowTimers) |
| 644 | createFrontendTimer(); |
| 645 | |
Douglas Gregor | 95dd558 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 646 | if (getFrontendOpts().ShowStats) |
| 647 | llvm::EnableStatistics(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 648 | |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 649 | for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) { |
Douglas Gregor | d2536a6 | 2011-11-15 22:58:25 +0000 | [diff] [blame] | 650 | const std::string &InFile = getFrontendOpts().Inputs[i].second; |
| 651 | |
Daniel Dunbar | 2056048 | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 652 | // Reset the ID tables if we are reusing the SourceManager. |
| 653 | if (hasSourceManager()) |
| 654 | getSourceManager().clearIDTables(); |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 655 | |
Douglas Gregor | d2536a6 | 2011-11-15 22:58:25 +0000 | [diff] [blame] | 656 | if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) { |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 657 | Act.Execute(); |
| 658 | Act.EndSourceFile(); |
| 659 | } |
| 660 | } |
| 661 | |
Chris Lattner | 53eee7b | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 662 | if (getDiagnosticOpts().ShowCarets) { |
Argyrios Kyrtzidis | f2224d8 | 2010-11-18 20:06:46 +0000 | [diff] [blame] | 663 | // We can have multiple diagnostics sharing one diagnostic client. |
| 664 | // Get the total number of warnings/errors from the client. |
| 665 | unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings(); |
| 666 | unsigned NumErrors = getDiagnostics().getClient()->getNumErrors(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 667 | |
Chris Lattner | 53eee7b | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 668 | if (NumWarnings) |
| 669 | OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s"); |
| 670 | if (NumWarnings && NumErrors) |
| 671 | OS << " and "; |
| 672 | if (NumErrors) |
| 673 | OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s"); |
| 674 | if (NumWarnings || NumErrors) |
| 675 | OS << " generated.\n"; |
| 676 | } |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 677 | |
Daniel Dunbar | 2056048 | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 678 | if (getFrontendOpts().ShowStats && hasFileManager()) { |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 679 | getFileManager().PrintStats(); |
| 680 | OS << "\n"; |
| 681 | } |
| 682 | |
Argyrios Kyrtzidis | ab41b97 | 2010-11-18 21:13:57 +0000 | [diff] [blame] | 683 | return !getDiagnostics().getClient()->getNumErrors(); |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 686 | /// \brief Determine the appropriate source input kind based on language |
| 687 | /// options. |
| 688 | static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) { |
| 689 | if (LangOpts.OpenCL) |
| 690 | return IK_OpenCL; |
| 691 | if (LangOpts.CUDA) |
| 692 | return IK_CUDA; |
| 693 | if (LangOpts.ObjC1) |
| 694 | return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC; |
| 695 | return LangOpts.CPlusPlus? IK_CXX : IK_C; |
| 696 | } |
| 697 | |
Douglas Gregor | 0ced799 | 2011-10-04 00:21:21 +0000 | [diff] [blame] | 698 | namespace { |
Douglas Gregor | f9e357d | 2011-11-29 19:06:37 +0000 | [diff] [blame] | 699 | struct CompileModuleMapData { |
| 700 | CompilerInstance &Instance; |
| 701 | GenerateModuleAction &CreateModuleAction; |
| 702 | }; |
| 703 | } |
| 704 | |
| 705 | /// \brief Helper function that executes the module-generating action under |
| 706 | /// a crash recovery context. |
| 707 | static void doCompileMapModule(void *UserData) { |
| 708 | CompileModuleMapData &Data |
| 709 | = *reinterpret_cast<CompileModuleMapData *>(UserData); |
| 710 | Data.Instance.ExecuteAction(Data.CreateModuleAction); |
| 711 | } |
| 712 | |
| 713 | namespace { |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 714 | /// \brief Class that manages the creation of a lock file to aid |
| 715 | /// implicit coordination between different processes. |
| 716 | /// |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 717 | /// The implicit coordination works by creating a ".lock" file alongside |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 718 | /// the file that we're coordinating for, using the atomicity of the file |
| 719 | /// system to ensure that only a single process can create that ".lock" file. |
| 720 | /// When the lock file is removed, the owning process has finished the |
| 721 | /// operation. |
| 722 | class LockFileManager { |
| 723 | public: |
| 724 | /// \brief Describes the state of a lock file. |
| 725 | enum LockFileState { |
| 726 | /// \brief The lock file has been created and is owned by this instance |
| 727 | /// of the object. |
| 728 | LFS_Owned, |
| 729 | /// \brief The lock file already exists and is owned by some other |
| 730 | /// instance. |
| 731 | LFS_Shared, |
| 732 | /// \brief An error occurred while trying to create or find the lock |
| 733 | /// file. |
| 734 | LFS_Error |
| 735 | }; |
| 736 | |
| 737 | private: |
| 738 | llvm::SmallString<128> LockFileName; |
| 739 | llvm::SmallString<128> UniqueLockFileName; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 740 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 741 | llvm::Optional<std::pair<std::string, int> > Owner; |
| 742 | llvm::Optional<llvm::error_code> Error; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 743 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 744 | LockFileManager(const LockFileManager &); |
| 745 | LockFileManager &operator=(const LockFileManager &); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 746 | |
| 747 | static llvm::Optional<std::pair<std::string, int> > |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 748 | readLockFile(StringRef LockFileName); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 749 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 750 | static bool processStillExecuting(StringRef Hostname, int PID); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 751 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 752 | public: |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 753 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 754 | LockFileManager(StringRef FileName); |
| 755 | ~LockFileManager(); |
| 756 | |
| 757 | /// \brief Determine the state of the lock file. |
| 758 | LockFileState getState() const; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 759 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 760 | operator LockFileState() const { return getState(); } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 761 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 762 | /// \brief For a shared lock, wait until the owner releases the lock. |
| 763 | void waitForUnlock(); |
| 764 | }; |
| 765 | } |
| 766 | |
| 767 | /// \brief Attempt to read the lock file with the given name, if it exists. |
| 768 | /// |
| 769 | /// \param LockFileName The name of the lock file to read. |
| 770 | /// |
| 771 | /// \returns The process ID of the process that owns this lock file |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 772 | llvm::Optional<std::pair<std::string, int> > |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 773 | LockFileManager::readLockFile(StringRef LockFileName) { |
| 774 | // Check whether the lock file exists. If not, clearly there's nothing |
| 775 | // to read, so we just return. |
| 776 | bool Exists = false; |
| 777 | if (llvm::sys::fs::exists(LockFileName, Exists) || !Exists) |
| 778 | return llvm::Optional<std::pair<std::string, int> >(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 779 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 780 | // Read the owning host and PID out of the lock file. If it appears that the |
| 781 | // owning process is dead, the lock file is invalid. |
| 782 | int PID = 0; |
| 783 | std::string Hostname; |
| 784 | std::ifstream Input(LockFileName.str().c_str()); |
| 785 | if (Input >> Hostname >> PID && PID > 0 && |
| 786 | processStillExecuting(Hostname, PID)) |
| 787 | return std::make_pair(Hostname, PID); |
| 788 | |
| 789 | // Delete the lock file. It's invalid anyway. |
| 790 | bool Existed; |
| 791 | llvm::sys::fs::remove(LockFileName, Existed); |
| 792 | return llvm::Optional<std::pair<std::string, int> >(); |
| 793 | } |
| 794 | |
| 795 | bool LockFileManager::processStillExecuting(StringRef Hostname, int PID) { |
| 796 | #if LLVM_ON_UNIX |
| 797 | char MyHostname[256]; |
| 798 | MyHostname[255] = 0; |
| 799 | MyHostname[0] = 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 800 | gethostname(MyHostname, 255); |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 801 | // Check whether the process is dead. If so, we're done. |
| 802 | if (MyHostname == Hostname && getsid(PID) == -1 && errno == ESRCH) |
| 803 | return false; |
| 804 | #endif |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 805 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 806 | return true; |
| 807 | } |
| 808 | |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 809 | LockFileManager::LockFileManager(StringRef FileName) |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 810 | { |
| 811 | LockFileName = FileName; |
| 812 | LockFileName += ".lock"; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 813 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 814 | // If the lock file already exists, don't bother to try to create our own |
| 815 | // lock file; it won't work anyway. Just figure out who owns this lock file. |
| 816 | if ((Owner = readLockFile(LockFileName))) |
| 817 | return; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 818 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 819 | // Create a lock file that is unique to this instance. |
| 820 | UniqueLockFileName = LockFileName; |
| 821 | UniqueLockFileName += "-%%%%%%%%"; |
| 822 | int UniqueLockFileID; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 823 | if (llvm::error_code EC |
| 824 | = llvm::sys::fs::unique_file(UniqueLockFileName.str(), |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 825 | UniqueLockFileID, |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 826 | UniqueLockFileName, |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 827 | /*makeAbsolute=*/false)) { |
| 828 | Error = EC; |
| 829 | return; |
| 830 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 831 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 832 | // Write our process ID to our unique lock file. |
| 833 | { |
| 834 | llvm::raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 835 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 836 | #if LLVM_ON_UNIX |
| 837 | // FIXME: move getpid() call into LLVM |
| 838 | char hostname[256]; |
| 839 | hostname[255] = 0; |
| 840 | hostname[0] = 0; |
| 841 | gethostname(hostname, 255); |
| 842 | Out << hostname << ' ' << getpid(); |
| 843 | #else |
| 844 | Out << "localhost 1"; |
| 845 | #endif |
| 846 | Out.close(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 847 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 848 | if (Out.has_error()) { |
| 849 | // We failed to write out PID, so make up an excuse, remove the |
| 850 | // unique lock file, and fail. |
| 851 | Error = llvm::make_error_code(llvm::errc::no_space_on_device); |
| 852 | bool Existed; |
| 853 | llvm::sys::fs::remove(UniqueLockFileName.c_str(), Existed); |
| 854 | return; |
| 855 | } |
| 856 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 857 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 858 | // Create a hard link from the lock file name. If this succeeds, we're done. |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 859 | llvm::error_code EC |
| 860 | = llvm::sys::fs::create_hard_link(UniqueLockFileName.str(), |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 861 | LockFileName.str()); |
| 862 | if (EC == llvm::errc::success) |
| 863 | return; |
| 864 | |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 865 | // Creating the hard link failed. |
| 866 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 867 | #ifdef LLVM_ON_UNIX |
| 868 | // The creation of the hard link may appear to fail, but if stat'ing the |
| 869 | // unique file returns a link count of 2, then we can still declare success. |
| 870 | struct stat StatBuf; |
| 871 | if (stat(UniqueLockFileName.c_str(), &StatBuf) == 0 && |
| 872 | StatBuf.st_nlink == 2) |
| 873 | return; |
| 874 | #endif |
| 875 | |
| 876 | // Someone else managed to create the lock file first. Wipe out our unique |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 877 | // lock file (it's useless now) and read the process ID from the lock file. |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 878 | bool Existed; |
| 879 | llvm::sys::fs::remove(UniqueLockFileName.str(), Existed); |
| 880 | if ((Owner = readLockFile(LockFileName))) |
| 881 | return; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 882 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 883 | // There is a lock file that nobody owns; try to clean it up and report |
| 884 | // an error. |
| 885 | llvm::sys::fs::remove(LockFileName.str(), Existed); |
| 886 | Error = EC; |
| 887 | } |
| 888 | |
| 889 | LockFileManager::LockFileState LockFileManager::getState() const { |
| 890 | if (Owner) |
| 891 | return LFS_Shared; |
| 892 | |
| 893 | if (Error) |
| 894 | return LFS_Error; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 895 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 896 | return LFS_Owned; |
| 897 | } |
| 898 | |
| 899 | LockFileManager::~LockFileManager() { |
| 900 | if (getState() != LFS_Owned) |
| 901 | return; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 902 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 903 | // Since we own the lock, remove the lock file and our own unique lock file. |
| 904 | bool Existed; |
| 905 | llvm::sys::fs::remove(LockFileName.str(), Existed); |
| 906 | llvm::sys::fs::remove(UniqueLockFileName.str(), Existed); |
| 907 | } |
| 908 | |
| 909 | void LockFileManager::waitForUnlock() { |
| 910 | if (getState() != LFS_Shared) |
| 911 | return; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 912 | |
Douglas Gregor | 1872e79 | 2011-10-05 14:58:46 +0000 | [diff] [blame] | 913 | #if LLVM_ON_WIN32 |
| 914 | unsigned long Interval = 1; |
| 915 | #else |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 916 | struct timespec Interval; |
| 917 | Interval.tv_sec = 0; |
| 918 | Interval.tv_nsec = 1000000; |
Douglas Gregor | 1872e79 | 2011-10-05 14:58:46 +0000 | [diff] [blame] | 919 | #endif |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 920 | // Don't wait more than an hour for the file to appear. |
| 921 | const unsigned MaxSeconds = 3600; |
| 922 | do { |
| 923 | // Sleep for the designated interval, to allow the owning process time to |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 924 | // finish up and |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 925 | // FIXME: Should we hook in to system APIs to get a notification when the |
| 926 | // lock file is deleted? |
Douglas Gregor | 2572849 | 2011-10-05 14:59:36 +0000 | [diff] [blame] | 927 | #if LLVM_ON_WIN32 |
| 928 | Sleep(Interval); |
| 929 | #else |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 930 | nanosleep(&Interval, NULL); |
Douglas Gregor | 2572849 | 2011-10-05 14:59:36 +0000 | [diff] [blame] | 931 | #endif |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 932 | // If the file no longer exists, we're done. |
| 933 | bool Exists = false; |
| 934 | if (!llvm::sys::fs::exists(LockFileName.str(), Exists) && !Exists) |
| 935 | return; |
| 936 | |
| 937 | if (!processStillExecuting((*Owner).first, (*Owner).second)) |
| 938 | return; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 939 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 940 | // Exponentially increase the time we wait for the lock to be removed. |
Douglas Gregor | 1872e79 | 2011-10-05 14:58:46 +0000 | [diff] [blame] | 941 | #if LLVM_ON_WIN32 |
| 942 | Interval *= 2; |
| 943 | #else |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 944 | Interval.tv_sec *= 2; |
| 945 | Interval.tv_nsec *= 2; |
| 946 | if (Interval.tv_nsec >= 1000000000) { |
| 947 | ++Interval.tv_sec; |
| 948 | Interval.tv_nsec -= 1000000000; |
| 949 | } |
Douglas Gregor | 1872e79 | 2011-10-05 14:58:46 +0000 | [diff] [blame] | 950 | #endif |
| 951 | } while ( |
| 952 | #if LLVM_ON_WIN32 |
| 953 | Interval < MaxSeconds * 1000 |
| 954 | #else |
NAKAMURA Takumi | 0caed28 | 2011-10-08 11:31:58 +0000 | [diff] [blame] | 955 | Interval.tv_sec < (time_t)MaxSeconds |
Douglas Gregor | 1872e79 | 2011-10-05 14:58:46 +0000 | [diff] [blame] | 956 | #endif |
| 957 | ); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 958 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 959 | // Give up. |
| 960 | } |
| 961 | |
Douglas Gregor | f9e357d | 2011-11-29 19:06:37 +0000 | [diff] [blame] | 962 | /// \brief Compile a module file for the given module, using the options |
| 963 | /// provided by the importing compiler instance. |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 964 | static void compileModule(CompilerInstance &ImportingInstance, |
Douglas Gregor | 933e7a6 | 2011-11-29 18:31:39 +0000 | [diff] [blame] | 965 | ModuleMap::Module *Module, |
| 966 | StringRef ModuleFileName) { |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 967 | LockFileManager Locked(ModuleFileName); |
| 968 | switch (Locked) { |
| 969 | case LockFileManager::LFS_Error: |
| 970 | return; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 971 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 972 | case LockFileManager::LFS_Owned: |
| 973 | // We're responsible for building the module ourselves. Do so below. |
| 974 | break; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 975 | |
Douglas Gregor | 2bc7507 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 976 | case LockFileManager::LFS_Shared: |
| 977 | // Someone else is responsible for building the module. Wait for them to |
| 978 | // finish. |
| 979 | Locked.waitForUnlock(); |
| 980 | break; |
| 981 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 982 | |
Douglas Gregor | f9e357d | 2011-11-29 19:06:37 +0000 | [diff] [blame] | 983 | ModuleMap &ModMap |
| 984 | = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap(); |
| 985 | |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 986 | // Construct a compiler invocation for creating this module. |
| 987 | llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation |
| 988 | (new CompilerInvocation(ImportingInstance.getInvocation())); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 989 | |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 990 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
| 991 | |
Douglas Gregor | b2d39c2 | 2011-09-15 20:53:28 +0000 | [diff] [blame] | 992 | // For any options that aren't intended to affect how a module is built, |
| 993 | // reset them to their default values. |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 994 | Invocation->getLangOpts()->resetNonModularOptions(); |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 995 | PPOpts.resetNonModularOptions(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 996 | |
Douglas Gregor | b86b8dc | 2011-11-15 19:35:01 +0000 | [diff] [blame] | 997 | // Note the name of the module we're building. |
Douglas Gregor | 933e7a6 | 2011-11-29 18:31:39 +0000 | [diff] [blame] | 998 | Invocation->getLangOpts()->CurrentModule = Module->getTopLevelModuleName(); |
Douglas Gregor | b86b8dc | 2011-11-15 19:35:01 +0000 | [diff] [blame] | 999 | |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1000 | // Note that this module is part of the module build path, so that we |
Douglas Gregor | b2d39c2 | 2011-09-15 20:53:28 +0000 | [diff] [blame] | 1001 | // can detect cycles in the module graph. |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1002 | PPOpts.ModuleBuildPath.push_back(Module->getTopLevelModuleName()); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1003 | |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1004 | // If there is a module map file, build the module using the module map. |
Douglas Gregor | b2d39c2 | 2011-09-15 20:53:28 +0000 | [diff] [blame] | 1005 | // Set up the inputs/outputs so that we build the module from its umbrella |
| 1006 | // header. |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1007 | FrontendOptions &FrontendOpts = Invocation->getFrontendOpts(); |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 1008 | FrontendOpts.OutputFile = ModuleFileName.str(); |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1009 | FrontendOpts.DisableFree = false; |
| 1010 | FrontendOpts.Inputs.clear(); |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1011 | InputKind IK = getSourceInputKindFromOptions(*Invocation->getLangOpts()); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1012 | |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1013 | // Get or create the module map that we'll use to build this module. |
| 1014 | llvm::SmallString<128> TempModuleMapFileName; |
| 1015 | if (const FileEntry *ModuleMapFile |
| 1016 | = ModMap.getContainingModuleMapFile(Module)) { |
| 1017 | // Use the module map where this module resides. |
| 1018 | FrontendOpts.Inputs.push_back(std::make_pair(IK, ModuleMapFile->getName())); |
| 1019 | } else { |
| 1020 | // Create a temporary module map file. |
| 1021 | TempModuleMapFileName = Module->Name; |
| 1022 | TempModuleMapFileName += "-%%%%%%%%.map"; |
| 1023 | int FD; |
| 1024 | if (llvm::sys::fs::unique_file(TempModuleMapFileName.str(), FD, |
| 1025 | TempModuleMapFileName, |
| 1026 | /*makeAbsolute=*/false) |
| 1027 | != llvm::errc::success) |
| 1028 | return; |
| 1029 | |
| 1030 | // Print the module map to this file. |
| 1031 | llvm::raw_fd_ostream OS(FD, /*shouldClose=*/true); |
| 1032 | Module->print(OS); |
| 1033 | FrontendOpts.Inputs.push_back( |
| 1034 | std::make_pair(IK, TempModuleMapFileName.str().str())); |
| 1035 | } |
| 1036 | |
| 1037 | // Don't free the remapped file buffers; they are owned by our caller. |
| 1038 | PPOpts.RetainRemappedFileBuffers = true; |
| 1039 | |
Douglas Gregor | 7824365 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 1040 | Invocation->getDiagnosticOpts().VerifyDiagnostics = 0; |
Douglas Gregor | 76d991e | 2011-09-13 23:20:27 +0000 | [diff] [blame] | 1041 | assert(ImportingInstance.getInvocation().getModuleHash() == |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1042 | Invocation->getModuleHash() && "Module hash mismatch!"); |
| 1043 | |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1044 | // Construct a compiler instance that will be used to actually create the |
| 1045 | // module. |
| 1046 | CompilerInstance Instance; |
| 1047 | Instance.setInvocation(&*Invocation); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1048 | Instance.createDiagnostics(/*argc=*/0, /*argv=*/0, |
Douglas Gregor | 7824365 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 1049 | &ImportingInstance.getDiagnosticClient(), |
Douglas Gregor | aee526e | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 1050 | /*ShouldOwnClient=*/true, |
| 1051 | /*ShouldCloneClient=*/true); |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1052 | |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1053 | // Construct a module-generating action. |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1054 | GenerateModuleAction CreateModuleAction; |
| 1055 | |
Douglas Gregor | 0ced799 | 2011-10-04 00:21:21 +0000 | [diff] [blame] | 1056 | // Execute the action to actually build the module in-place. Use a separate |
| 1057 | // thread so that we get a stack large enough. |
| 1058 | const unsigned ThreadStackSize = 8 << 20; |
| 1059 | llvm::CrashRecoveryContext CRC; |
Douglas Gregor | 18ee547 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1060 | CompileModuleMapData Data = { Instance, CreateModuleAction }; |
| 1061 | CRC.RunSafelyOnThread(&doCompileMapModule, &Data, ThreadStackSize); |
| 1062 | |
| 1063 | // Delete the temporary module map file. |
| 1064 | // FIXME: Even though we're executing under crash protection, it would still |
| 1065 | // be nice to do this with RemoveFileOnSignal when we can. However, that |
| 1066 | // doesn't make sense for all clients, so clean this up manually. |
| 1067 | if (!TempModuleMapFileName.empty()) |
| 1068 | llvm::sys::Path(TempModuleMapFileName).eraseFromDisk(); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1069 | } |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1070 | |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame^] | 1071 | ModuleKey CompilerInstance::loadModule(SourceLocation ImportLoc, |
| 1072 | ModuleIdPath Path) { |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1073 | // Determine what file we're searching from. |
| 1074 | SourceManager &SourceMgr = getSourceManager(); |
| 1075 | SourceLocation ExpandedImportLoc = SourceMgr.getExpansionLoc(ImportLoc); |
| 1076 | const FileEntry *CurFile |
| 1077 | = SourceMgr.getFileEntryForID(SourceMgr.getFileID(ExpandedImportLoc)); |
| 1078 | if (!CurFile) |
| 1079 | CurFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()); |
| 1080 | |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame^] | 1081 | StringRef ModuleName = Path[0].first->getName(); |
| 1082 | SourceLocation ModuleNameLoc = Path[0].second; |
| 1083 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1084 | // Search for a module with the given name. |
Douglas Gregor | a4d36a6 | 2011-11-28 23:16:06 +0000 | [diff] [blame] | 1085 | ModuleMap::Module *Module = 0; |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 1086 | std::string ModuleFileName; |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1087 | const FileEntry *ModuleFile |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame^] | 1088 | = PP->getHeaderSearchInfo().lookupModule(ModuleName, Module, |
Douglas Gregor | a4d36a6 | 2011-11-28 23:16:06 +0000 | [diff] [blame] | 1089 | &ModuleFileName); |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1090 | |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame^] | 1091 | // FIXME: Verify that the rest of the module path actually corresponds to |
| 1092 | // a submodule, and pass that information through. |
| 1093 | |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1094 | bool BuildingModule = false; |
Douglas Gregor | 933e7a6 | 2011-11-29 18:31:39 +0000 | [diff] [blame] | 1095 | if (!ModuleFile && Module) { |
| 1096 | // The module is not cached, but we have a module map from which we can |
| 1097 | // build the module. |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1098 | |
Douglas Gregor | 4ebd45f | 2011-09-15 20:40:10 +0000 | [diff] [blame] | 1099 | // Check whether there is a cycle in the module graph. |
| 1100 | SmallVectorImpl<std::string> &ModuleBuildPath |
| 1101 | = getPreprocessorOpts().ModuleBuildPath; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1102 | SmallVectorImpl<std::string>::iterator Pos |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame^] | 1103 | = std::find(ModuleBuildPath.begin(), ModuleBuildPath.end(), ModuleName); |
Douglas Gregor | 4ebd45f | 2011-09-15 20:40:10 +0000 | [diff] [blame] | 1104 | if (Pos != ModuleBuildPath.end()) { |
| 1105 | llvm::SmallString<256> CyclePath; |
| 1106 | for (; Pos != ModuleBuildPath.end(); ++Pos) { |
| 1107 | CyclePath += *Pos; |
| 1108 | CyclePath += " -> "; |
| 1109 | } |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame^] | 1110 | CyclePath += ModuleName; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1111 | |
Douglas Gregor | 4ebd45f | 2011-09-15 20:40:10 +0000 | [diff] [blame] | 1112 | getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle) |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame^] | 1113 | << ModuleName << CyclePath; |
Douglas Gregor | 4ebd45f | 2011-09-15 20:40:10 +0000 | [diff] [blame] | 1114 | return 0; |
| 1115 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1116 | |
Douglas Gregor | 08d6acf | 2011-09-29 00:52:27 +0000 | [diff] [blame] | 1117 | getDiagnostics().Report(ModuleNameLoc, diag::warn_module_build) |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame^] | 1118 | << ModuleName; |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1119 | BuildingModule = true; |
Douglas Gregor | 933e7a6 | 2011-11-29 18:31:39 +0000 | [diff] [blame] | 1120 | compileModule(*this, Module, ModuleFileName); |
Douglas Gregor | a4d36a6 | 2011-11-28 23:16:06 +0000 | [diff] [blame] | 1121 | ModuleFile = FileMgr->getFile(ModuleFileName); |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1122 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1124 | if (!ModuleFile) { |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1125 | getDiagnostics().Report(ModuleNameLoc, |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1126 | BuildingModule? diag::err_module_not_built |
| 1127 | : diag::err_module_not_found) |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame^] | 1128 | << ModuleName |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1129 | << SourceRange(ImportLoc, ModuleNameLoc); |
| 1130 | return 0; |
| 1131 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1132 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1133 | // If we don't already have an ASTReader, create one now. |
| 1134 | if (!ModuleManager) { |
Douglas Gregor | de8a905 | 2011-09-14 23:13:09 +0000 | [diff] [blame] | 1135 | if (!hasASTContext()) |
| 1136 | createASTContext(); |
| 1137 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1138 | std::string Sysroot = getHeaderSearchOpts().Sysroot; |
| 1139 | const PreprocessorOptions &PPOpts = getPreprocessorOpts(); |
Douglas Gregor | f8a1e51 | 2011-09-02 00:26:20 +0000 | [diff] [blame] | 1140 | ModuleManager = new ASTReader(getPreprocessor(), *Context, |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1141 | Sysroot.empty() ? "" : Sysroot.c_str(), |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1142 | PPOpts.DisablePCHValidation, |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1143 | PPOpts.DisableStatCache); |
Douglas Gregor | de8a905 | 2011-09-14 23:13:09 +0000 | [diff] [blame] | 1144 | if (hasASTConsumer()) { |
| 1145 | ModuleManager->setDeserializationListener( |
| 1146 | getASTConsumer().GetASTDeserializationListener()); |
| 1147 | getASTContext().setASTMutationListener( |
| 1148 | getASTConsumer().GetASTMutationListener()); |
| 1149 | } |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1150 | llvm::OwningPtr<ExternalASTSource> Source; |
| 1151 | Source.reset(ModuleManager); |
| 1152 | getASTContext().setExternalSource(Source); |
Douglas Gregor | de8a905 | 2011-09-14 23:13:09 +0000 | [diff] [blame] | 1153 | if (hasSema()) |
| 1154 | ModuleManager->InitializeSema(getSema()); |
Douglas Gregor | 1a995dd | 2011-09-15 18:47:32 +0000 | [diff] [blame] | 1155 | if (hasASTConsumer()) |
| 1156 | ModuleManager->StartTranslationUnit(&getASTConsumer()); |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1157 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1158 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1159 | // Try to load the module we found. |
| 1160 | switch (ModuleManager->ReadAST(ModuleFile->getName(), |
| 1161 | serialization::MK_Module)) { |
| 1162 | case ASTReader::Success: |
| 1163 | break; |
| 1164 | |
| 1165 | case ASTReader::IgnorePCH: |
| 1166 | // FIXME: The ASTReader will already have complained, but can we showhorn |
| 1167 | // that diagnostic information into a more useful form? |
| 1168 | return 0; |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1169 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1170 | case ASTReader::Failure: |
| 1171 | // Already complained. |
| 1172 | return 0; |
| 1173 | } |
NAKAMURA Takumi | a789ca9 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1174 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1175 | // FIXME: The module file's FileEntry makes a poor key indeed! |
| 1176 | return (ModuleKey)ModuleFile; |
| 1177 | } |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 1178 | |