Daniel Dunbar | 636404a | 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" |
Daniel Dunbar | 56d9c29 | 2009-11-14 02:47:17 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | df3e30c | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | bcfc7d0 | 2011-12-02 23:42:12 +0000 | [diff] [blame] | 13 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 636404a | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Diagnostic.h" |
Daniel Dunbar | 546a676 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | 636404a | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Version.h" |
David Blaikie | 8b00dcb | 2011-09-26 00:21:47 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/ChainedDiagnosticConsumer.h" |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/FrontendAction.h" |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/LogDiagnosticPrinter.h" |
Ted Kremenek | 4610ea2 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/SerializedDiagnosticPrinter.h" |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/Utils.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/VerifyDiagnosticConsumer.h" |
| 28 | #include "clang/Lex/HeaderSearch.h" |
| 29 | #include "clang/Lex/PTHManager.h" |
| 30 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 31 | #include "clang/Sema/CodeCompleteConsumer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 32 | #include "clang/Sema/Sema.h" |
| 33 | #include "clang/Serialization/ASTReader.h" |
Douglas Gregor | 171b780 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 35 | #include "llvm/Config/config.h" |
| 36 | #include "llvm/Support/CrashRecoveryContext.h" |
| 37 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Host.h" |
Douglas Gregor | e212489 | 2012-01-29 20:15:24 +0000 | [diff] [blame] | 39 | #include "llvm/Support/LockFileManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 40 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Path.h" |
| 42 | #include "llvm/Support/Program.h" |
| 43 | #include "llvm/Support/Signals.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Timer.h" |
| 45 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | f25faaa | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 46 | #include "llvm/Support/system_error.h" |
Douglas Gregor | 54a8881 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 47 | |
Daniel Dunbar | 636404a | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 48 | using namespace clang; |
| 49 | |
Daniel Dunbar | e922d9b | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 50 | CompilerInstance::CompilerInstance() |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame^] | 51 | : Invocation(new CompilerInvocation()), ModuleManager(0), |
| 52 | BuildGlobalModuleIndex(false) { |
Daniel Dunbar | 6824225 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 53 | } |
Daniel Dunbar | 636404a | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 54 | |
| 55 | CompilerInstance::~CompilerInstance() { |
Benjamin Kramer | 3c717b4 | 2012-10-14 19:21:21 +0000 | [diff] [blame] | 56 | assert(OutputFiles.empty() && "Still output files in flight?"); |
Daniel Dunbar | e922d9b | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Daniel Dunbar | 6824225 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 59 | void CompilerInstance::setInvocation(CompilerInvocation *Value) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 60 | Invocation = Value; |
Daniel Dunbar | 6824225 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 61 | } |
| 62 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 63 | void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) { |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 64 | Diagnostics = Value; |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 67 | void CompilerInstance::setTarget(TargetInfo *Value) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 68 | Target = Value; |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void CompilerInstance::setFileManager(FileManager *Value) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 72 | FileMgr = Value; |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 73 | } |
| 74 | |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 75 | void CompilerInstance::setSourceManager(SourceManager *Value) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 76 | SourceMgr = Value; |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 79 | void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; } |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 80 | |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 81 | void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; } |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | 0e93f01 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 83 | void CompilerInstance::setSema(Sema *S) { |
| 84 | TheSema.reset(S); |
| 85 | } |
| 86 | |
Daniel Dunbar | 56d9c29 | 2009-11-14 02:47:17 +0000 | [diff] [blame] | 87 | void CompilerInstance::setASTConsumer(ASTConsumer *Value) { |
| 88 | Consumer.reset(Value); |
| 89 | } |
| 90 | |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 91 | void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) { |
| 92 | CompletionConsumer.reset(Value); |
| 93 | } |
| 94 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 95 | // Diagnostics |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 96 | static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts, |
Daniel Dunbar | 7b83306 | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 97 | const CodeGenOptions *CodeGenOpts, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 98 | DiagnosticsEngine &Diags) { |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 99 | std::string ErrorInfo; |
| 100 | bool OwnsStream = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 101 | raw_ostream *OS = &llvm::errs(); |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 102 | if (DiagOpts->DiagnosticLogFile != "-") { |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 103 | // Create the output stream. |
| 104 | llvm::raw_fd_ostream *FileOS( |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 105 | new llvm::raw_fd_ostream(DiagOpts->DiagnosticLogFile.c_str(), |
Daniel Dunbar | 44d9ef7 | 2011-04-07 20:19:21 +0000 | [diff] [blame] | 106 | ErrorInfo, llvm::raw_fd_ostream::F_Append)); |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 107 | if (!ErrorInfo.empty()) { |
| 108 | Diags.Report(diag::warn_fe_cc_log_diagnostics_failure) |
Sean Silva | 2118a5c | 2013-01-20 01:58:26 +0000 | [diff] [blame] | 109 | << DiagOpts->DiagnosticLogFile << ErrorInfo; |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 110 | } else { |
| 111 | FileOS->SetUnbuffered(); |
| 112 | FileOS->SetUseAtomicWrites(true); |
| 113 | OS = FileOS; |
| 114 | OwnsStream = true; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Chain in the diagnostic client which will log the diagnostics. |
Daniel Dunbar | 7b83306 | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 119 | LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts, |
| 120 | OwnsStream); |
| 121 | if (CodeGenOpts) |
| 122 | Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags); |
David Blaikie | 8b00dcb | 2011-09-26 00:21:47 +0000 | [diff] [blame] | 123 | Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger)); |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 126 | static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts, |
Ted Kremenek | 4610ea2 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 127 | DiagnosticsEngine &Diags, |
| 128 | StringRef OutputFile) { |
| 129 | std::string ErrorInfo; |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 130 | OwningPtr<llvm::raw_fd_ostream> OS; |
Ted Kremenek | 4610ea2 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 131 | OS.reset(new llvm::raw_fd_ostream(OutputFile.str().c_str(), ErrorInfo, |
| 132 | llvm::raw_fd_ostream::F_Binary)); |
| 133 | |
| 134 | if (!ErrorInfo.empty()) { |
| 135 | Diags.Report(diag::warn_fe_serialized_diag_failure) |
| 136 | << OutputFile << ErrorInfo; |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | DiagnosticConsumer *SerializedConsumer = |
Ted Kremenek | 4548e04 | 2011-12-17 05:26:11 +0000 | [diff] [blame] | 141 | clang::serialized_diags::create(OS.take(), DiagOpts); |
Ted Kremenek | 4610ea2 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 142 | |
| 143 | |
| 144 | Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), |
| 145 | SerializedConsumer)); |
| 146 | } |
| 147 | |
Sean Silva | f1b49e2 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 148 | void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client, |
Douglas Gregor | d0e9e3a | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 149 | bool ShouldOwnClient, |
| 150 | bool ShouldCloneClient) { |
Sean Silva | f1b49e2 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 151 | Diagnostics = createDiagnostics(&getDiagnosticOpts(), Client, |
Douglas Gregor | d0e9e3a | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 152 | ShouldOwnClient, ShouldCloneClient, |
| 153 | &getCodeGenOpts()); |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 156 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 157 | CompilerInstance::createDiagnostics(DiagnosticOptions *Opts, |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 158 | DiagnosticConsumer *Client, |
Douglas Gregor | 2b9b464 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 159 | bool ShouldOwnClient, |
Douglas Gregor | d0e9e3a | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 160 | bool ShouldCloneClient, |
Daniel Dunbar | 7b83306 | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 161 | const CodeGenOptions *CodeGenOpts) { |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 162 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 163 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 164 | Diags(new DiagnosticsEngine(DiagID, Opts)); |
Daniel Dunbar | 1b39a2e | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 165 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 166 | // Create the diagnostic client for reporting errors or for |
| 167 | // implementing -verify. |
Douglas Gregor | d0e9e3a | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 168 | if (Client) { |
| 169 | if (ShouldCloneClient) |
| 170 | Diags->setClient(Client->clone(*Diags), ShouldOwnClient); |
| 171 | else |
| 172 | Diags->setClient(Client, ShouldOwnClient); |
| 173 | } else |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 174 | Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts)); |
Daniel Dunbar | 50ec0da | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 175 | |
| 176 | // Chain in -verify checker, if requested. |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 177 | if (Opts->VerifyDiagnostics) |
David Blaikie | 69609dc | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 178 | Diags->setClient(new VerifyDiagnosticConsumer(*Diags)); |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 179 | |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 180 | // Chain in -diagnostic-log-file dumper, if requested. |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 181 | if (!Opts->DiagnosticLogFile.empty()) |
Daniel Dunbar | 7b83306 | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 182 | SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 183 | |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 184 | if (!Opts->DiagnosticSerializationFile.empty()) |
Ted Kremenek | 4610ea2 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 185 | SetupSerializedDiagnostics(Opts, *Diags, |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 186 | Opts->DiagnosticSerializationFile); |
Ted Kremenek | 4610ea2 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 187 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 188 | // Configure our handling of diagnostics. |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 189 | ProcessWarningOptions(*Diags, *Opts); |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 190 | |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 191 | return Diags; |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // File Manager |
| 195 | |
Daniel Dunbar | 546a676 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 196 | void CompilerInstance::createFileManager() { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 197 | FileMgr = new FileManager(getFileSystemOpts()); |
Daniel Dunbar | 546a676 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 200 | // Source Manager |
| 201 | |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 202 | void CompilerInstance::createSourceManager(FileManager &FileMgr) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 203 | SourceMgr = new SourceManager(getDiagnostics(), FileMgr); |
Daniel Dunbar | 546a676 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 204 | } |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 205 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 206 | // Preprocessor |
| 207 | |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 208 | void CompilerInstance::createPreprocessor() { |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 209 | const PreprocessorOptions &PPOpts = getPreprocessorOpts(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 210 | |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 211 | // Create a PTH manager if we are using some form of a token cache. |
| 212 | PTHManager *PTHMgr = 0; |
Daniel Dunbar | d6ea902 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 213 | if (!PPOpts.TokenCache.empty()) |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 214 | PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics()); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 215 | |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 216 | // Create the Preprocessor. |
Douglas Gregor | b85b9cc | 2012-10-24 16:19:39 +0000 | [diff] [blame] | 217 | HeaderSearch *HeaderInfo = new HeaderSearch(&getHeaderSearchOpts(), |
| 218 | getFileManager(), |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 219 | getDiagnostics(), |
Douglas Gregor | 8992928 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 220 | getLangOpts(), |
| 221 | &getTarget()); |
Douglas Gregor | 1452ff1 | 2012-10-24 17:46:57 +0000 | [diff] [blame] | 222 | PP = new Preprocessor(&getPreprocessorOpts(), |
| 223 | getDiagnostics(), getLangOpts(), &getTarget(), |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 224 | getSourceManager(), *HeaderInfo, *this, PTHMgr, |
| 225 | /*OwnsHeaderSearch=*/true); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 226 | |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 227 | // Note that this is different then passing PTHMgr to Preprocessor's ctor. |
| 228 | // That argument is used as the IdentifierInfoLookup argument to |
| 229 | // IdentifierTable's ctor. |
| 230 | if (PTHMgr) { |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 231 | PTHMgr->setPreprocessor(&*PP); |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 232 | PP->setPTHManager(PTHMgr); |
| 233 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 234 | |
Douglas Gregor | 7f6d60d | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 235 | if (PPOpts.DetailedRecord) |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 236 | PP->createPreprocessingRecord(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 237 | |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 238 | InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts()); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 239 | |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 240 | // Set up the module path, including the hash for the |
| 241 | // module-creation options. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 242 | SmallString<256> SpecificModuleCache( |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 243 | getHeaderSearchOpts().ModuleCachePath); |
| 244 | if (!getHeaderSearchOpts().DisableModuleHash) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 245 | llvm::sys::path::append(SpecificModuleCache, |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 246 | getInvocation().getModuleHash()); |
Douglas Gregor | 2537a36 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 247 | PP->getHeaderSearchInfo().setModuleCachePath(SpecificModuleCache); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 248 | |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 249 | // Handle generating dependencies, if requested. |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 250 | const DependencyOutputOptions &DepOpts = getDependencyOutputOpts(); |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 251 | if (!DepOpts.OutputFile.empty()) |
| 252 | AttachDependencyFileGen(*PP, DepOpts); |
Douglas Gregor | 2e12965 | 2012-02-02 23:45:13 +0000 | [diff] [blame] | 253 | if (!DepOpts.DOTOutputFile.empty()) |
| 254 | AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile, |
Douglas Gregor | 83d46be | 2012-02-02 00:54:52 +0000 | [diff] [blame] | 255 | getHeaderSearchOpts().Sysroot); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 256 | |
Douglas Gregor | 83d46be | 2012-02-02 00:54:52 +0000 | [diff] [blame] | 257 | |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 258 | // Handle generating header include information, if requested. |
| 259 | if (DepOpts.ShowHeaderIncludes) |
| 260 | AttachHeaderIncludeGen(*PP); |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 261 | if (!DepOpts.HeaderIncludeOutputFile.empty()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 262 | StringRef OutputPath = DepOpts.HeaderIncludeOutputFile; |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 263 | if (OutputPath == "-") |
| 264 | OutputPath = ""; |
Daniel Dunbar | fe908a8 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 265 | AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath, |
| 266 | /*ShowDepth=*/false); |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 267 | } |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 268 | } |
Daniel Dunbar | df3e30c | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 269 | |
| 270 | // ASTContext |
| 271 | |
| 272 | void CompilerInstance::createASTContext() { |
| 273 | Preprocessor &PP = getPreprocessor(); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 274 | Context = new ASTContext(getLangOpts(), PP.getSourceManager(), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 275 | &getTarget(), PP.getIdentifierTable(), |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 276 | PP.getSelectorTable(), PP.getBuiltinInfo(), |
| 277 | /*size_reserve=*/ 0); |
Daniel Dunbar | df3e30c | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 278 | } |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 279 | |
| 280 | // ExternalASTSource |
| 281 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 282 | void CompilerInstance::createPCHExternalASTSource(StringRef Path, |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 283 | bool DisablePCHValidation, |
Argyrios Kyrtzidis | 4a280ff | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 284 | bool AllowPCHWithCompilerErrors, |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 285 | void *DeserializationListener){ |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 286 | OwningPtr<ExternalASTSource> Source; |
Sebastian Redl | 009e7f2 | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 287 | bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 288 | Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 289 | DisablePCHValidation, |
Argyrios Kyrtzidis | 4a280ff | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 290 | AllowPCHWithCompilerErrors, |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 291 | getPreprocessor(), getASTContext(), |
Sebastian Redl | 009e7f2 | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 292 | DeserializationListener, |
| 293 | Preamble)); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 294 | ModuleManager = static_cast<ASTReader*>(Source.get()); |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 295 | getASTContext().setExternalSource(Source); |
| 296 | } |
| 297 | |
| 298 | ExternalASTSource * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 299 | CompilerInstance::createPCHExternalASTSource(StringRef Path, |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 300 | const std::string &Sysroot, |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 301 | bool DisablePCHValidation, |
Argyrios Kyrtzidis | 4a280ff | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 302 | bool AllowPCHWithCompilerErrors, |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 303 | Preprocessor &PP, |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 304 | ASTContext &Context, |
Sebastian Redl | 009e7f2 | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 305 | void *DeserializationListener, |
| 306 | bool Preamble) { |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 307 | OwningPtr<ASTReader> Reader; |
Douglas Gregor | 8835e03 | 2011-09-02 00:26:20 +0000 | [diff] [blame] | 308 | Reader.reset(new ASTReader(PP, Context, |
Douglas Gregor | c567ba2 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 309 | Sysroot.empty() ? "" : Sysroot.c_str(), |
Argyrios Kyrtzidis | d7c16b2 | 2012-10-31 20:59:50 +0000 | [diff] [blame] | 310 | DisablePCHValidation, |
Argyrios Kyrtzidis | 4a280ff | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 311 | AllowPCHWithCompilerErrors)); |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 312 | |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 313 | Reader->setDeserializationListener( |
Sebastian Redl | 3e31c72 | 2010-08-18 23:56:56 +0000 | [diff] [blame] | 314 | static_cast<ASTDeserializationListener *>(DeserializationListener)); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 315 | switch (Reader->ReadAST(Path, |
| 316 | Preamble ? serialization::MK_Preamble |
Douglas Gregor | 4b29c16 | 2012-10-22 23:51:00 +0000 | [diff] [blame] | 317 | : serialization::MK_PCH, |
Argyrios Kyrtzidis | 2ec2936 | 2012-11-15 18:57:22 +0000 | [diff] [blame] | 318 | SourceLocation(), |
Douglas Gregor | 4b29c16 | 2012-10-22 23:51:00 +0000 | [diff] [blame] | 319 | ASTReader::ARR_None)) { |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 320 | case ASTReader::Success: |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 321 | // Set the predefines buffer as suggested by the PCH reader. Typically, the |
| 322 | // predefines buffer will be empty. |
| 323 | PP.setPredefines(Reader->getSuggestedPredefines()); |
| 324 | return Reader.take(); |
| 325 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 326 | case ASTReader::Failure: |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 327 | // Unrecoverable failure: don't even try to process the input file. |
| 328 | break; |
| 329 | |
Douglas Gregor | c9ad5fb | 2012-10-22 22:50:17 +0000 | [diff] [blame] | 330 | case ASTReader::OutOfDate: |
| 331 | case ASTReader::VersionMismatch: |
| 332 | case ASTReader::ConfigurationMismatch: |
| 333 | case ASTReader::HadErrors: |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 334 | // No suitable PCH file could be found. Return an error. |
| 335 | break; |
| 336 | } |
| 337 | |
| 338 | return 0; |
| 339 | } |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 340 | |
| 341 | // Code Completion |
| 342 | |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 343 | static bool EnableCodeCompletion(Preprocessor &PP, |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 344 | const std::string &Filename, |
| 345 | unsigned Line, |
| 346 | unsigned Column) { |
| 347 | // Tell the source manager to chop off the given file at a specific |
| 348 | // line and column. |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 349 | const FileEntry *Entry = PP.getFileManager().getFile(Filename); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 350 | if (!Entry) { |
| 351 | PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) |
| 352 | << Filename; |
| 353 | return true; |
| 354 | } |
| 355 | |
| 356 | // Truncate the named file at the given line/column. |
| 357 | PP.SetCodeCompletionPoint(Entry, Line, Column); |
| 358 | return false; |
| 359 | } |
| 360 | |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 361 | void CompilerInstance::createCodeCompletionConsumer() { |
| 362 | const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 363 | if (!CompletionConsumer) { |
Erik Verbruggen | 2fca3c2 | 2012-04-12 10:31:12 +0000 | [diff] [blame] | 364 | setCodeCompletionConsumer( |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 365 | createCodeCompletionConsumer(getPreprocessor(), |
| 366 | Loc.FileName, Loc.Line, Loc.Column, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 367 | getFrontendOpts().CodeCompleteOpts, |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 368 | llvm::outs())); |
| 369 | if (!CompletionConsumer) |
| 370 | return; |
| 371 | } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName, |
| 372 | Loc.Line, Loc.Column)) { |
Erik Verbruggen | 2fca3c2 | 2012-04-12 10:31:12 +0000 | [diff] [blame] | 373 | setCodeCompletionConsumer(0); |
Douglas Gregor | 00a0cf7 | 2010-03-16 06:04:47 +0000 | [diff] [blame] | 374 | return; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 375 | } |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 376 | |
| 377 | if (CompletionConsumer->isOutputBinary() && |
| 378 | llvm::sys::Program::ChangeStdoutToBinary()) { |
| 379 | getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary); |
Erik Verbruggen | 2fca3c2 | 2012-04-12 10:31:12 +0000 | [diff] [blame] | 380 | setCodeCompletionConsumer(0); |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 381 | } |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Kovarththanan Rajaratnam | 5505dff | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 384 | void CompilerInstance::createFrontendTimer() { |
| 385 | FrontendTimer.reset(new llvm::Timer("Clang front-end timer")); |
| 386 | } |
| 387 | |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 388 | CodeCompleteConsumer * |
| 389 | CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP, |
| 390 | const std::string &Filename, |
| 391 | unsigned Line, |
| 392 | unsigned Column, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 393 | const CodeCompleteOptions &Opts, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 394 | raw_ostream &OS) { |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 395 | if (EnableCodeCompletion(PP, Filename, Line, Column)) |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 396 | return 0; |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 397 | |
| 398 | // Set up the creation routine for code-completion. |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 399 | return new PrintingCodeCompleteConsumer(Opts, OS); |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 400 | } |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 401 | |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 402 | void CompilerInstance::createSema(TranslationUnitKind TUKind, |
Douglas Gregor | 0e93f01 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 403 | CodeCompleteConsumer *CompletionConsumer) { |
| 404 | TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(), |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 405 | TUKind, CompletionConsumer)); |
Douglas Gregor | 0e93f01 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 408 | // Output Files |
| 409 | |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 410 | void CompilerInstance::addOutputFile(const OutputFile &OutFile) { |
| 411 | assert(OutFile.OS && "Attempt to add empty stream to output list!"); |
| 412 | OutputFiles.push_back(OutFile); |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Kovarththanan Rajaratnam | 1c558cd | 2010-03-06 12:07:48 +0000 | [diff] [blame] | 415 | void CompilerInstance::clearOutputFiles(bool EraseFiles) { |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 416 | for (std::list<OutputFile>::iterator |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 417 | it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) { |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 418 | delete it->OS; |
| 419 | if (!it->TempFilename.empty()) { |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 420 | if (EraseFiles) { |
| 421 | bool existed; |
| 422 | llvm::sys::fs::remove(it->TempFilename, existed); |
| 423 | } else { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 424 | SmallString<128> NewOutFile(it->Filename); |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 425 | |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 426 | // If '-working-directory' was passed, the output filename should be |
| 427 | // relative to that. |
Anders Carlsson | 9ba8fb1 | 2011-03-14 01:13:54 +0000 | [diff] [blame] | 428 | FileMgr->FixupRelativePath(NewOutFile); |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 429 | if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename, |
| 430 | NewOutFile.str())) { |
Manuel Klimek | 3ef9c44 | 2012-05-16 20:55:58 +0000 | [diff] [blame] | 431 | getDiagnostics().Report(diag::err_unable_to_rename_temp) |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 432 | << it->TempFilename << it->Filename << ec.message(); |
| 433 | |
| 434 | bool existed; |
| 435 | llvm::sys::fs::remove(it->TempFilename, existed); |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | } else if (!it->Filename.empty() && EraseFiles) |
| 439 | llvm::sys::Path(it->Filename).eraseFromDisk(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 440 | |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 441 | } |
| 442 | OutputFiles.clear(); |
| 443 | } |
| 444 | |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 445 | llvm::raw_fd_ostream * |
| 446 | CompilerInstance::createDefaultOutputFile(bool Binary, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 447 | StringRef InFile, |
| 448 | StringRef Extension) { |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 449 | return createOutputFile(getFrontendOpts().OutputFile, Binary, |
Daniel Dunbar | ae77b3d | 2012-03-03 00:36:06 +0000 | [diff] [blame] | 450 | /*RemoveFileOnSignal=*/true, InFile, Extension, |
| 451 | /*UseTemporary=*/true); |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | llvm::raw_fd_ostream * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 455 | CompilerInstance::createOutputFile(StringRef OutputPath, |
Daniel Dunbar | e326f9b | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 456 | bool Binary, bool RemoveFileOnSignal, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 457 | StringRef InFile, |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 458 | StringRef Extension, |
Daniel Dunbar | b9c62c0 | 2012-03-03 00:36:02 +0000 | [diff] [blame] | 459 | bool UseTemporary, |
| 460 | bool CreateMissingDirectories) { |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 461 | std::string Error, OutputPathName, TempPathName; |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 462 | llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary, |
Daniel Dunbar | e326f9b | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 463 | RemoveFileOnSignal, |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 464 | InFile, Extension, |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 465 | UseTemporary, |
Daniel Dunbar | b9c62c0 | 2012-03-03 00:36:02 +0000 | [diff] [blame] | 466 | CreateMissingDirectories, |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 467 | &OutputPathName, |
| 468 | &TempPathName); |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 469 | if (!OS) { |
Daniel Dunbar | 7554699 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 470 | getDiagnostics().Report(diag::err_fe_unable_to_open_output) |
| 471 | << OutputPath << Error; |
| 472 | return 0; |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | // Add the output file -- but don't try to remove "-", since this means we are |
| 476 | // using stdin. |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 477 | addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "", |
| 478 | TempPathName, OS)); |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 479 | |
| 480 | return OS; |
| 481 | } |
| 482 | |
| 483 | llvm::raw_fd_ostream * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 484 | CompilerInstance::createOutputFile(StringRef OutputPath, |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 485 | std::string &Error, |
| 486 | bool Binary, |
Daniel Dunbar | e326f9b | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 487 | bool RemoveFileOnSignal, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 488 | StringRef InFile, |
| 489 | StringRef Extension, |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 490 | bool UseTemporary, |
Daniel Dunbar | b9c62c0 | 2012-03-03 00:36:02 +0000 | [diff] [blame] | 491 | bool CreateMissingDirectories, |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 492 | std::string *ResultPathName, |
| 493 | std::string *TempPathName) { |
Daniel Dunbar | b9c62c0 | 2012-03-03 00:36:02 +0000 | [diff] [blame] | 494 | assert((!CreateMissingDirectories || UseTemporary) && |
| 495 | "CreateMissingDirectories is only allowed when using temporary files"); |
| 496 | |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 497 | std::string OutFile, TempFile; |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 498 | if (!OutputPath.empty()) { |
| 499 | OutFile = OutputPath; |
| 500 | } else if (InFile == "-") { |
| 501 | OutFile = "-"; |
| 502 | } else if (!Extension.empty()) { |
| 503 | llvm::sys::Path Path(InFile); |
| 504 | Path.eraseSuffix(); |
| 505 | Path.appendSuffix(Extension); |
| 506 | OutFile = Path.str(); |
| 507 | } else { |
| 508 | OutFile = "-"; |
| 509 | } |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 510 | |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 511 | OwningPtr<llvm::raw_fd_ostream> OS; |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 512 | std::string OSFile; |
| 513 | |
| 514 | if (UseTemporary && OutFile != "-") { |
Daniel Dunbar | b9c62c0 | 2012-03-03 00:36:02 +0000 | [diff] [blame] | 515 | // Only create the temporary if the parent directory exists (or create |
| 516 | // missing directories is true) and we can actually write to OutPath, |
| 517 | // otherwise we want to fail early. |
| 518 | SmallString<256> AbsPath(OutputPath); |
| 519 | llvm::sys::fs::make_absolute(AbsPath); |
| 520 | llvm::sys::Path OutPath(AbsPath); |
| 521 | bool ParentExists = false; |
| 522 | if (llvm::sys::fs::exists(llvm::sys::path::parent_path(AbsPath.str()), |
| 523 | ParentExists)) |
| 524 | ParentExists = false; |
Michael J. Spencer | f6efe58 | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 525 | bool Exists; |
Daniel Dunbar | b9c62c0 | 2012-03-03 00:36:02 +0000 | [diff] [blame] | 526 | if ((CreateMissingDirectories || ParentExists) && |
| 527 | ((llvm::sys::fs::exists(AbsPath.str(), Exists) || !Exists) || |
| 528 | (OutPath.isRegularFile() && OutPath.canWrite()))) { |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 529 | // Create a temporary file. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 530 | SmallString<128> TempPath; |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 531 | TempPath = OutFile; |
| 532 | TempPath += "-%%%%%%%%"; |
| 533 | int fd; |
| 534 | if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath, |
Eric Christopher | f6a6346 | 2012-05-11 00:10:07 +0000 | [diff] [blame] | 535 | /*makeAbsolute=*/false, 0664) |
| 536 | == llvm::errc::success) { |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 537 | OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true)); |
| 538 | OSFile = TempFile = TempPath.str(); |
| 539 | } |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 543 | if (!OS) { |
| 544 | OSFile = OutFile; |
| 545 | OS.reset( |
| 546 | new llvm::raw_fd_ostream(OSFile.c_str(), Error, |
| 547 | (Binary ? llvm::raw_fd_ostream::F_Binary : 0))); |
| 548 | if (!Error.empty()) |
| 549 | return 0; |
| 550 | } |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 551 | |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 552 | // Make sure the out stream file gets removed if we crash. |
Daniel Dunbar | e326f9b | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 553 | if (RemoveFileOnSignal) |
| 554 | llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile)); |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 555 | |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 556 | if (ResultPathName) |
| 557 | *ResultPathName = OutFile; |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 558 | if (TempPathName) |
| 559 | *TempPathName = TempFile; |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 560 | |
Daniel Dunbar | 2eaef18 | 2009-11-20 22:32:38 +0000 | [diff] [blame] | 561 | return OS.take(); |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 562 | } |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 563 | |
| 564 | // Initialization Utilities |
| 565 | |
Argyrios Kyrtzidis | 1b3240b | 2012-11-09 19:40:33 +0000 | [diff] [blame] | 566 | bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input){ |
| 567 | return InitializeSourceManager(Input, getDiagnostics(), |
Douglas Gregor | a686e1b | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 568 | getFileManager(), getSourceManager(), |
| 569 | getFrontendOpts()); |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Argyrios Kyrtzidis | 1b3240b | 2012-11-09 19:40:33 +0000 | [diff] [blame] | 572 | bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 573 | DiagnosticsEngine &Diags, |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 574 | FileManager &FileMgr, |
| 575 | SourceManager &SourceMgr, |
| 576 | const FrontendOptions &Opts) { |
Argyrios Kyrtzidis | 1b3240b | 2012-11-09 19:40:33 +0000 | [diff] [blame] | 577 | SrcMgr::CharacteristicKind |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 578 | Kind = Input.isSystem() ? SrcMgr::C_System : SrcMgr::C_User; |
Argyrios Kyrtzidis | 1b3240b | 2012-11-09 19:40:33 +0000 | [diff] [blame] | 579 | |
Argyrios Kyrtzidis | 6566e23 | 2012-11-09 19:40:45 +0000 | [diff] [blame] | 580 | if (Input.isBuffer()) { |
| 581 | SourceMgr.createMainFileIDForMemBuffer(Input.getBuffer(), Kind); |
| 582 | assert(!SourceMgr.getMainFileID().isInvalid() && |
| 583 | "Couldn't establish MainFileID!"); |
| 584 | return true; |
| 585 | } |
| 586 | |
| 587 | StringRef InputFile = Input.getFile(); |
| 588 | |
Argyrios Kyrtzidis | 7c06d86 | 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 | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 591 | const FileEntry *File = FileMgr.getFile(InputFile); |
Dan Gohman | 5276521 | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 592 | if (!File) { |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 593 | Diags.Report(diag::err_fe_error_reading) << InputFile; |
| 594 | return false; |
| 595 | } |
Daniel Dunbar | e2951f4 | 2012-11-05 22:53:33 +0000 | [diff] [blame] | 596 | |
| 597 | // The natural SourceManager infrastructure can't currently handle named |
| 598 | // pipes, but we would at least like to accept them for the main |
| 599 | // file. Detect them here, read them with the more generic MemoryBuffer |
| 600 | // function, and simply override their contents as we do for STDIN. |
| 601 | if (File->isNamedPipe()) { |
| 602 | OwningPtr<llvm::MemoryBuffer> MB; |
| 603 | if (llvm::error_code ec = llvm::MemoryBuffer::getFile(InputFile, MB)) { |
| 604 | Diags.Report(diag::err_cannot_open_file) << InputFile << ec.message(); |
| 605 | return false; |
| 606 | } |
Daniel Dunbar | db0745a | 2012-11-27 00:04:16 +0000 | [diff] [blame] | 607 | |
| 608 | // Create a new virtual file that will have the correct size. |
| 609 | File = FileMgr.getVirtualFile(InputFile, MB->getBufferSize(), 0); |
Daniel Dunbar | e2951f4 | 2012-11-05 22:53:33 +0000 | [diff] [blame] | 610 | SourceMgr.overrideFileContents(File, MB.take()); |
| 611 | } |
Daniel Dunbar | db0745a | 2012-11-27 00:04:16 +0000 | [diff] [blame] | 612 | |
| 613 | SourceMgr.createMainFileID(File, Kind); |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 614 | } else { |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 615 | OwningPtr<llvm::MemoryBuffer> SB; |
Michael J. Spencer | d9da7a1 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 616 | if (llvm::MemoryBuffer::getSTDIN(SB)) { |
Michael J. Spencer | f25faaa | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 617 | // FIXME: Give ec.message() in this diag. |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 618 | Diags.Report(diag::err_fe_error_reading_stdin); |
| 619 | return false; |
| 620 | } |
Dan Gohman | 2f76cd7 | 2010-10-26 23:21:25 +0000 | [diff] [blame] | 621 | const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(), |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 622 | SB->getBufferSize(), 0); |
Douglas Gregor | a686e1b | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 623 | SourceMgr.createMainFileID(File, Kind); |
Michael J. Spencer | d9da7a1 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 624 | SourceMgr.overrideFileContents(File, SB.take()); |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Dan Gohman | 5276521 | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 627 | assert(!SourceMgr.getMainFileID().isInvalid() && |
| 628 | "Couldn't establish MainFileID!"); |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 629 | return true; |
| 630 | } |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 631 | |
| 632 | // High-Level Operations |
| 633 | |
| 634 | bool CompilerInstance::ExecuteAction(FrontendAction &Act) { |
| 635 | assert(hasDiagnostics() && "Diagnostics engine is not initialized!"); |
| 636 | assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!"); |
| 637 | assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!"); |
| 638 | |
| 639 | // FIXME: Take this as an argument, once all the APIs we used have moved to |
| 640 | // taking it as an input instead of hard-coding llvm::errs. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 641 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 642 | |
| 643 | // Create the target instance. |
Douglas Gregor | f8715de | 2012-11-16 04:24:59 +0000 | [diff] [blame] | 644 | setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), &getTargetOpts())); |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 645 | if (!hasTarget()) |
| 646 | return false; |
| 647 | |
| 648 | // Inform the target of the language options. |
| 649 | // |
| 650 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 651 | // created. This complexity should be lifted elsewhere. |
| 652 | getTarget().setForcedLangOptions(getLangOpts()); |
| 653 | |
Fariborz Jahanian | 29898f4 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 654 | // rewriter project will change target built-in bool type from its default. |
| 655 | if (getFrontendOpts().ProgramAction == frontend::RewriteObjC) |
| 656 | getTarget().noSignedCharForObjCBool(); |
| 657 | |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 658 | // Validate/process some options. |
| 659 | if (getHeaderSearchOpts().Verbose) |
| 660 | OS << "clang -cc1 version " CLANG_VERSION_STRING |
| 661 | << " based upon " << PACKAGE_STRING |
Sebastian Pop | 8188c8a | 2011-11-01 21:33:06 +0000 | [diff] [blame] | 662 | << " default target " << llvm::sys::getDefaultTargetTriple() << "\n"; |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 663 | |
| 664 | if (getFrontendOpts().ShowTimers) |
| 665 | createFrontendTimer(); |
| 666 | |
Douglas Gregor | 171b780 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 667 | if (getFrontendOpts().ShowStats) |
| 668 | llvm::EnableStatistics(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 669 | |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 670 | for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) { |
Daniel Dunbar | aed46fc | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 671 | // Reset the ID tables if we are reusing the SourceManager. |
| 672 | if (hasSourceManager()) |
| 673 | getSourceManager().clearIDTables(); |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 674 | |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 675 | if (Act.BeginSourceFile(*this, getFrontendOpts().Inputs[i])) { |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 676 | Act.Execute(); |
| 677 | Act.EndSourceFile(); |
| 678 | } |
| 679 | } |
| 680 | |
Argyrios Kyrtzidis | 7910d7b | 2011-12-07 05:52:12 +0000 | [diff] [blame] | 681 | // Notify the diagnostic client that all files were processed. |
| 682 | getDiagnostics().getClient()->finish(); |
| 683 | |
Chris Lattner | 198cb4d | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 684 | if (getDiagnosticOpts().ShowCarets) { |
Argyrios Kyrtzidis | c79346a | 2010-11-18 20:06:46 +0000 | [diff] [blame] | 685 | // We can have multiple diagnostics sharing one diagnostic client. |
| 686 | // Get the total number of warnings/errors from the client. |
| 687 | unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings(); |
| 688 | unsigned NumErrors = getDiagnostics().getClient()->getNumErrors(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 689 | |
Chris Lattner | 198cb4d | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 690 | if (NumWarnings) |
| 691 | OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s"); |
| 692 | if (NumWarnings && NumErrors) |
| 693 | OS << " and "; |
| 694 | if (NumErrors) |
| 695 | OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s"); |
| 696 | if (NumWarnings || NumErrors) |
| 697 | OS << " generated.\n"; |
| 698 | } |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 699 | |
Daniel Dunbar | aed46fc | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 700 | if (getFrontendOpts().ShowStats && hasFileManager()) { |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 701 | getFileManager().PrintStats(); |
| 702 | OS << "\n"; |
| 703 | } |
| 704 | |
Argyrios Kyrtzidis | bc46793 | 2010-11-18 21:13:57 +0000 | [diff] [blame] | 705 | return !getDiagnostics().getClient()->getNumErrors(); |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 708 | /// \brief Determine the appropriate source input kind based on language |
| 709 | /// options. |
| 710 | static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) { |
| 711 | if (LangOpts.OpenCL) |
| 712 | return IK_OpenCL; |
| 713 | if (LangOpts.CUDA) |
| 714 | return IK_CUDA; |
| 715 | if (LangOpts.ObjC1) |
| 716 | return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC; |
| 717 | return LangOpts.CPlusPlus? IK_CXX : IK_C; |
| 718 | } |
| 719 | |
Douglas Gregor | 51e0b54 | 2011-10-04 00:21:21 +0000 | [diff] [blame] | 720 | namespace { |
Douglas Gregor | 514b636 | 2011-11-29 19:06:37 +0000 | [diff] [blame] | 721 | struct CompileModuleMapData { |
| 722 | CompilerInstance &Instance; |
| 723 | GenerateModuleAction &CreateModuleAction; |
| 724 | }; |
| 725 | } |
| 726 | |
| 727 | /// \brief Helper function that executes the module-generating action under |
| 728 | /// a crash recovery context. |
| 729 | static void doCompileMapModule(void *UserData) { |
| 730 | CompileModuleMapData &Data |
| 731 | = *reinterpret_cast<CompileModuleMapData *>(UserData); |
| 732 | Data.Instance.ExecuteAction(Data.CreateModuleAction); |
| 733 | } |
| 734 | |
Douglas Gregor | 514b636 | 2011-11-29 19:06:37 +0000 | [diff] [blame] | 735 | /// \brief Compile a module file for the given module, using the options |
| 736 | /// provided by the importing compiler instance. |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 737 | static void compileModule(CompilerInstance &ImportingInstance, |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 738 | SourceLocation ImportLoc, |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 739 | Module *Module, |
Douglas Gregor | 6dc5792 | 2011-11-29 18:31:39 +0000 | [diff] [blame] | 740 | StringRef ModuleFileName) { |
Douglas Gregor | e212489 | 2012-01-29 20:15:24 +0000 | [diff] [blame] | 741 | llvm::LockFileManager Locked(ModuleFileName); |
Douglas Gregor | 54a8881 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 742 | switch (Locked) { |
Douglas Gregor | e212489 | 2012-01-29 20:15:24 +0000 | [diff] [blame] | 743 | case llvm::LockFileManager::LFS_Error: |
Douglas Gregor | 54a8881 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 744 | return; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 745 | |
Douglas Gregor | e212489 | 2012-01-29 20:15:24 +0000 | [diff] [blame] | 746 | case llvm::LockFileManager::LFS_Owned: |
Douglas Gregor | 54a8881 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 747 | // We're responsible for building the module ourselves. Do so below. |
| 748 | break; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 749 | |
Douglas Gregor | e212489 | 2012-01-29 20:15:24 +0000 | [diff] [blame] | 750 | case llvm::LockFileManager::LFS_Shared: |
Douglas Gregor | 54a8881 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 751 | // Someone else is responsible for building the module. Wait for them to |
| 752 | // finish. |
| 753 | Locked.waitForUnlock(); |
Douglas Gregor | 188dbef | 2012-11-07 17:46:15 +0000 | [diff] [blame] | 754 | return; |
Douglas Gregor | 54a8881 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 755 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 756 | |
Douglas Gregor | 514b636 | 2011-11-29 19:06:37 +0000 | [diff] [blame] | 757 | ModuleMap &ModMap |
| 758 | = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap(); |
| 759 | |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 760 | // Construct a compiler invocation for creating this module. |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 761 | IntrusiveRefCntPtr<CompilerInvocation> Invocation |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 762 | (new CompilerInvocation(ImportingInstance.getInvocation())); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 763 | |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 764 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
| 765 | |
Douglas Gregor | 44bf68d | 2011-09-15 20:53:28 +0000 | [diff] [blame] | 766 | // For any options that aren't intended to affect how a module is built, |
| 767 | // reset them to their default values. |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 768 | Invocation->getLangOpts()->resetNonModularOptions(); |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 769 | PPOpts.resetNonModularOptions(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 770 | |
Douglas Gregor | 7d106e4 | 2011-11-15 19:35:01 +0000 | [diff] [blame] | 771 | // Note the name of the module we're building. |
Douglas Gregor | 6dc5792 | 2011-11-29 18:31:39 +0000 | [diff] [blame] | 772 | Invocation->getLangOpts()->CurrentModule = Module->getTopLevelModuleName(); |
Douglas Gregor | 7d106e4 | 2011-11-15 19:35:01 +0000 | [diff] [blame] | 773 | |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 774 | // Make sure that the failed-module structure has been allocated in |
| 775 | // the importing instance, and propagate the pointer to the newly-created |
| 776 | // instance. |
| 777 | PreprocessorOptions &ImportingPPOpts |
| 778 | = ImportingInstance.getInvocation().getPreprocessorOpts(); |
| 779 | if (!ImportingPPOpts.FailedModules) |
| 780 | ImportingPPOpts.FailedModules = new PreprocessorOptions::FailedModulesSet; |
| 781 | PPOpts.FailedModules = ImportingPPOpts.FailedModules; |
| 782 | |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 783 | // If there is a module map file, build the module using the module map. |
Douglas Gregor | 44bf68d | 2011-09-15 20:53:28 +0000 | [diff] [blame] | 784 | // Set up the inputs/outputs so that we build the module from its umbrella |
| 785 | // header. |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 786 | FrontendOptions &FrontendOpts = Invocation->getFrontendOpts(); |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 787 | FrontendOpts.OutputFile = ModuleFileName.str(); |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 788 | FrontendOpts.DisableFree = false; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame^] | 789 | FrontendOpts.GenerateModuleIndex = false; |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 790 | FrontendOpts.Inputs.clear(); |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 791 | InputKind IK = getSourceInputKindFromOptions(*Invocation->getLangOpts()); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 792 | |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 793 | // Get or create the module map that we'll use to build this module. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 794 | SmallString<128> TempModuleMapFileName; |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 795 | if (const FileEntry *ModuleMapFile |
| 796 | = ModMap.getContainingModuleMapFile(Module)) { |
| 797 | // Use the module map where this module resides. |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 798 | FrontendOpts.Inputs.push_back(FrontendInputFile(ModuleMapFile->getName(), |
| 799 | IK)); |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 800 | } else { |
| 801 | // Create a temporary module map file. |
| 802 | TempModuleMapFileName = Module->Name; |
| 803 | TempModuleMapFileName += "-%%%%%%%%.map"; |
| 804 | int FD; |
| 805 | if (llvm::sys::fs::unique_file(TempModuleMapFileName.str(), FD, |
| 806 | TempModuleMapFileName, |
Douglas Gregor | 19f9f7b | 2011-12-06 22:05:29 +0000 | [diff] [blame] | 807 | /*makeAbsolute=*/true) |
Douglas Gregor | 2f554c4 | 2011-12-06 23:04:08 +0000 | [diff] [blame] | 808 | != llvm::errc::success) { |
Douglas Gregor | ee78d3e | 2011-12-07 00:54:14 +0000 | [diff] [blame] | 809 | ImportingInstance.getDiagnostics().Report(diag::err_module_map_temp_file) |
| 810 | << TempModuleMapFileName; |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 811 | return; |
Douglas Gregor | 2f554c4 | 2011-12-06 23:04:08 +0000 | [diff] [blame] | 812 | } |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 813 | // Print the module map to this file. |
| 814 | llvm::raw_fd_ostream OS(FD, /*shouldClose=*/true); |
| 815 | Module->print(OS); |
| 816 | FrontendOpts.Inputs.push_back( |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 817 | FrontendInputFile(TempModuleMapFileName.str().str(), IK)); |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | // Don't free the remapped file buffers; they are owned by our caller. |
| 821 | PPOpts.RetainRemappedFileBuffers = true; |
| 822 | |
Douglas Gregor | 2b9b464 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 823 | Invocation->getDiagnosticOpts().VerifyDiagnostics = 0; |
Douglas Gregor | 3728ea7 | 2011-09-13 23:20:27 +0000 | [diff] [blame] | 824 | assert(ImportingInstance.getInvocation().getModuleHash() == |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 825 | Invocation->getModuleHash() && "Module hash mismatch!"); |
| 826 | |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 827 | // Construct a compiler instance that will be used to actually create the |
| 828 | // module. |
| 829 | CompilerInstance Instance; |
| 830 | Instance.setInvocation(&*Invocation); |
Sean Silva | f1b49e2 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 831 | Instance.createDiagnostics(&ImportingInstance.getDiagnosticClient(), |
Douglas Gregor | d0e9e3a | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 832 | /*ShouldOwnClient=*/true, |
| 833 | /*ShouldCloneClient=*/true); |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 834 | |
Douglas Gregor | 6336543 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 835 | // Note that this module is part of the module build stack, so that we |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 836 | // can detect cycles in the module graph. |
| 837 | Instance.createFileManager(); // FIXME: Adopt file manager from importer? |
| 838 | Instance.createSourceManager(Instance.getFileManager()); |
| 839 | SourceManager &SourceMgr = Instance.getSourceManager(); |
Douglas Gregor | 6336543 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 840 | SourceMgr.setModuleBuildStack( |
| 841 | ImportingInstance.getSourceManager().getModuleBuildStack()); |
| 842 | SourceMgr.pushModuleBuildStack(Module->getTopLevelModuleName(), |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 843 | FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager())); |
| 844 | |
| 845 | |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 846 | // Construct a module-generating action. |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 847 | GenerateModuleAction CreateModuleAction; |
| 848 | |
Douglas Gregor | 51e0b54 | 2011-10-04 00:21:21 +0000 | [diff] [blame] | 849 | // Execute the action to actually build the module in-place. Use a separate |
| 850 | // thread so that we get a stack large enough. |
| 851 | const unsigned ThreadStackSize = 8 << 20; |
| 852 | llvm::CrashRecoveryContext CRC; |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 853 | CompileModuleMapData Data = { Instance, CreateModuleAction }; |
| 854 | CRC.RunSafelyOnThread(&doCompileMapModule, &Data, ThreadStackSize); |
| 855 | |
| 856 | // Delete the temporary module map file. |
| 857 | // FIXME: Even though we're executing under crash protection, it would still |
| 858 | // be nice to do this with RemoveFileOnSignal when we can. However, that |
| 859 | // doesn't make sense for all clients, so clean this up manually. |
Benjamin Kramer | 13afbf4 | 2012-10-14 19:50:53 +0000 | [diff] [blame] | 860 | Instance.clearOutputFiles(/*EraseFiles=*/true); |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 861 | if (!TempModuleMapFileName.empty()) |
| 862 | llvm::sys::Path(TempModuleMapFileName).eraseFromDisk(); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame^] | 863 | |
| 864 | // We've rebuilt a module. If we're allowed to generate or update the global |
| 865 | // module index, record that fact in the importing compiler instance. |
| 866 | if (ImportingInstance.getFrontendOpts().GenerateModuleIndex) { |
| 867 | ImportingInstance.setBuildGlobalModuleIndex(true); |
| 868 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 869 | } |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 870 | |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 871 | ModuleLoadResult |
| 872 | CompilerInstance::loadModule(SourceLocation ImportLoc, |
| 873 | ModuleIdPath Path, |
| 874 | Module::NameVisibilityKind Visibility, |
| 875 | bool IsInclusionDirective) { |
Douglas Gregor | 1805b8a | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 876 | // If we've already handled this import, just return the cached result. |
| 877 | // This one-element cache is important to eliminate redundant diagnostics |
| 878 | // when both the preprocessor and parser see the same import declaration. |
Douglas Gregor | ff2be53 | 2011-12-01 17:11:21 +0000 | [diff] [blame] | 879 | if (!ImportLoc.isInvalid() && LastModuleImportLoc == ImportLoc) { |
| 880 | // Make the named module visible. |
| 881 | if (LastModuleImportResult) |
| 882 | ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility); |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 883 | return LastModuleImportResult; |
Douglas Gregor | ff2be53 | 2011-12-01 17:11:21 +0000 | [diff] [blame] | 884 | } |
Douglas Gregor | 1805b8a | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 885 | |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 886 | // Determine what file we're searching from. |
Douglas Gregor | 7194420 | 2011-11-30 00:36:36 +0000 | [diff] [blame] | 887 | StringRef ModuleName = Path[0].first->getName(); |
| 888 | SourceLocation ModuleNameLoc = Path[0].second; |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 889 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 890 | clang::Module *Module = 0; |
Douglas Gregor | 7194420 | 2011-11-30 00:36:36 +0000 | [diff] [blame] | 891 | |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 892 | // If we don't already have information on this module, load the module now. |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 893 | llvm::DenseMap<const IdentifierInfo *, clang::Module *>::iterator Known |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 894 | = KnownModules.find(Path[0].first); |
Douglas Gregor | 2537a36 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 895 | if (Known != KnownModules.end()) { |
| 896 | // Retrieve the cached top-level module. |
| 897 | Module = Known->second; |
| 898 | } else if (ModuleName == getLangOpts().CurrentModule) { |
| 899 | // This is the module we're building. |
| 900 | Module = PP->getHeaderSearchInfo().getModuleMap().findModule(ModuleName); |
| 901 | Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first; |
| 902 | } else { |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 903 | // Search for a module with the given name. |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 904 | Module = PP->getHeaderSearchInfo().lookupModule(ModuleName); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 905 | std::string ModuleFileName; |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 906 | if (Module) |
| 907 | ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(Module); |
| 908 | else |
| 909 | ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(ModuleName); |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 910 | |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 911 | if (ModuleFileName.empty()) { |
| 912 | getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found) |
| 913 | << ModuleName |
| 914 | << SourceRange(ImportLoc, ModuleNameLoc); |
| 915 | LastModuleImportLoc = ImportLoc; |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 916 | LastModuleImportResult = ModuleLoadResult(); |
| 917 | return LastModuleImportResult; |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | const FileEntry *ModuleFile |
| 921 | = getFileManager().getFile(ModuleFileName, /*OpenFile=*/false, |
| 922 | /*CacheFailure=*/false); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 923 | bool BuildingModule = false; |
| 924 | if (!ModuleFile && Module) { |
| 925 | // The module is not cached, but we have a module map from which we can |
| 926 | // build the module. |
| 927 | |
| 928 | // Check whether there is a cycle in the module graph. |
Douglas Gregor | 6336543 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 929 | ModuleBuildStack Path = getSourceManager().getModuleBuildStack(); |
| 930 | ModuleBuildStack::iterator Pos = Path.begin(), PosEnd = Path.end(); |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 931 | for (; Pos != PosEnd; ++Pos) { |
| 932 | if (Pos->first == ModuleName) |
| 933 | break; |
| 934 | } |
| 935 | |
| 936 | if (Pos != PosEnd) { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 937 | SmallString<256> CyclePath; |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 938 | for (; Pos != PosEnd; ++Pos) { |
| 939 | CyclePath += Pos->first; |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 940 | CyclePath += " -> "; |
| 941 | } |
| 942 | CyclePath += ModuleName; |
| 943 | |
| 944 | getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle) |
| 945 | << ModuleName << CyclePath; |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 946 | return ModuleLoadResult(); |
| 947 | } |
| 948 | |
| 949 | // Check whether we have already attempted to build this module (but |
| 950 | // failed). |
| 951 | if (getPreprocessorOpts().FailedModules && |
| 952 | getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)) { |
| 953 | getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built) |
| 954 | << ModuleName |
| 955 | << SourceRange(ImportLoc, ModuleNameLoc); |
| 956 | |
| 957 | return ModuleLoadResult(); |
Douglas Gregor | dff0e89 | 2011-09-15 20:40:10 +0000 | [diff] [blame] | 958 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 959 | |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 960 | BuildingModule = true; |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 961 | compileModule(*this, ModuleNameLoc, Module, ModuleFileName); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 962 | ModuleFile = FileMgr->getFile(ModuleFileName); |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 963 | |
Douglas Gregor | 0f2b463 | 2013-01-10 02:04:18 +0000 | [diff] [blame] | 964 | if (!ModuleFile && getPreprocessorOpts().FailedModules) |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 965 | getPreprocessorOpts().FailedModules->addFailed(ModuleName); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | if (!ModuleFile) { |
| 969 | getDiagnostics().Report(ModuleNameLoc, |
| 970 | BuildingModule? diag::err_module_not_built |
| 971 | : diag::err_module_not_found) |
| 972 | << ModuleName |
| 973 | << SourceRange(ImportLoc, ModuleNameLoc); |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 974 | return ModuleLoadResult(); |
Douglas Gregor | dff0e89 | 2011-09-15 20:40:10 +0000 | [diff] [blame] | 975 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 976 | |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 977 | // If we don't already have an ASTReader, create one now. |
| 978 | if (!ModuleManager) { |
| 979 | if (!hasASTContext()) |
| 980 | createASTContext(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 981 | |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 982 | std::string Sysroot = getHeaderSearchOpts().Sysroot; |
| 983 | const PreprocessorOptions &PPOpts = getPreprocessorOpts(); |
| 984 | ModuleManager = new ASTReader(getPreprocessor(), *Context, |
| 985 | Sysroot.empty() ? "" : Sysroot.c_str(), |
Argyrios Kyrtzidis | d7c16b2 | 2012-10-31 20:59:50 +0000 | [diff] [blame] | 986 | PPOpts.DisablePCHValidation); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 987 | if (hasASTConsumer()) { |
| 988 | ModuleManager->setDeserializationListener( |
| 989 | getASTConsumer().GetASTDeserializationListener()); |
| 990 | getASTContext().setASTMutationListener( |
| 991 | getASTConsumer().GetASTMutationListener()); |
Douglas Gregor | cb28f9d | 2012-10-09 23:05:51 +0000 | [diff] [blame] | 992 | getPreprocessor().setPPMutationListener( |
| 993 | getASTConsumer().GetPPMutationListener()); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 994 | } |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 995 | OwningPtr<ExternalASTSource> Source; |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 996 | Source.reset(ModuleManager); |
| 997 | getASTContext().setExternalSource(Source); |
| 998 | if (hasSema()) |
| 999 | ModuleManager->InitializeSema(getSema()); |
| 1000 | if (hasASTConsumer()) |
| 1001 | ModuleManager->StartTranslationUnit(&getASTConsumer()); |
Douglas Gregor | 21931ef | 2011-09-14 23:13:09 +0000 | [diff] [blame] | 1002 | } |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1003 | |
| 1004 | // Try to load the module we found. |
Douglas Gregor | 188dbef | 2012-11-07 17:46:15 +0000 | [diff] [blame] | 1005 | unsigned ARRFlags = ASTReader::ARR_None; |
| 1006 | if (Module) |
| 1007 | ARRFlags |= ASTReader::ARR_OutOfDate; |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1008 | switch (ModuleManager->ReadAST(ModuleFile->getName(), |
Argyrios Kyrtzidis | 2ec2936 | 2012-11-15 18:57:22 +0000 | [diff] [blame] | 1009 | serialization::MK_Module, ImportLoc, |
Douglas Gregor | 188dbef | 2012-11-07 17:46:15 +0000 | [diff] [blame] | 1010 | ARRFlags)) { |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1011 | case ASTReader::Success: |
| 1012 | break; |
| 1013 | |
Douglas Gregor | 188dbef | 2012-11-07 17:46:15 +0000 | [diff] [blame] | 1014 | case ASTReader::OutOfDate: { |
| 1015 | // The module file is out-of-date. Rebuild it. |
| 1016 | getFileManager().invalidateCache(ModuleFile); |
| 1017 | bool Existed; |
| 1018 | llvm::sys::fs::remove(ModuleFileName, Existed); |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1019 | |
| 1020 | // Check whether we have already attempted to build this module (but |
| 1021 | // failed). |
| 1022 | if (getPreprocessorOpts().FailedModules && |
| 1023 | getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)) { |
| 1024 | getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built) |
| 1025 | << ModuleName |
| 1026 | << SourceRange(ImportLoc, ModuleNameLoc); |
| 1027 | |
| 1028 | return ModuleLoadResult(); |
| 1029 | } |
| 1030 | |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 1031 | compileModule(*this, ModuleNameLoc, Module, ModuleFileName); |
Douglas Gregor | 188dbef | 2012-11-07 17:46:15 +0000 | [diff] [blame] | 1032 | |
| 1033 | // Try loading the module again. |
| 1034 | ModuleFile = FileMgr->getFile(ModuleFileName); |
| 1035 | if (!ModuleFile || |
| 1036 | ModuleManager->ReadAST(ModuleFileName, |
Argyrios Kyrtzidis | 2ec2936 | 2012-11-15 18:57:22 +0000 | [diff] [blame] | 1037 | serialization::MK_Module, ImportLoc, |
Douglas Gregor | 188dbef | 2012-11-07 17:46:15 +0000 | [diff] [blame] | 1038 | ASTReader::ARR_None) != ASTReader::Success) { |
Douglas Gregor | 0f2b463 | 2013-01-10 02:04:18 +0000 | [diff] [blame] | 1039 | if (getPreprocessorOpts().FailedModules) |
| 1040 | getPreprocessorOpts().FailedModules->addFailed(ModuleName); |
Douglas Gregor | 188dbef | 2012-11-07 17:46:15 +0000 | [diff] [blame] | 1041 | KnownModules[Path[0].first] = 0; |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1042 | return ModuleLoadResult(); |
Douglas Gregor | 188dbef | 2012-11-07 17:46:15 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | // Okay, we've rebuilt and now loaded the module. |
| 1046 | break; |
| 1047 | } |
| 1048 | |
Douglas Gregor | c9ad5fb | 2012-10-22 22:50:17 +0000 | [diff] [blame] | 1049 | case ASTReader::VersionMismatch: |
| 1050 | case ASTReader::ConfigurationMismatch: |
| 1051 | case ASTReader::HadErrors: |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1052 | // FIXME: The ASTReader will already have complained, but can we showhorn |
| 1053 | // that diagnostic information into a more useful form? |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1054 | KnownModules[Path[0].first] = 0; |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1055 | return ModuleLoadResult(); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1056 | |
| 1057 | case ASTReader::Failure: |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1058 | // Already complained, but note now that we failed. |
| 1059 | KnownModules[Path[0].first] = 0; |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1060 | return ModuleLoadResult(); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1063 | if (!Module) { |
| 1064 | // If we loaded the module directly, without finding a module map first, |
| 1065 | // we'll have loaded the module's information from the module itself. |
| 1066 | Module = PP->getHeaderSearchInfo().getModuleMap() |
| 1067 | .findModule((Path[0].first->getName())); |
| 1068 | } |
Argyrios Kyrtzidis | 43af513 | 2012-09-29 01:06:04 +0000 | [diff] [blame] | 1069 | |
| 1070 | if (Module) |
| 1071 | Module->setASTFile(ModuleFile); |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1072 | |
| 1073 | // Cache the result of this top-level module lookup for later. |
| 1074 | Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first; |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1075 | } |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1076 | |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1077 | // If we never found the module, fail. |
| 1078 | if (!Module) |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1079 | return ModuleLoadResult(); |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1080 | |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1081 | // Verify that the rest of the module path actually corresponds to |
| 1082 | // a submodule. |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1083 | if (Path.size() > 1) { |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1084 | for (unsigned I = 1, N = Path.size(); I != N; ++I) { |
| 1085 | StringRef Name = Path[I].first->getName(); |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1086 | clang::Module *Sub = Module->findSubmodule(Name); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1087 | |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1088 | if (!Sub) { |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1089 | // Attempt to perform typo correction to find a module name that works. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1090 | SmallVector<StringRef, 2> Best; |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1091 | unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)(); |
| 1092 | |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1093 | for (clang::Module::submodule_iterator J = Module->submodule_begin(), |
| 1094 | JEnd = Module->submodule_end(); |
Matt Beaumont-Gay | eb44eda | 2011-11-30 19:41:21 +0000 | [diff] [blame] | 1095 | J != JEnd; ++J) { |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1096 | unsigned ED = Name.edit_distance((*J)->Name, |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1097 | /*AllowReplacements=*/true, |
| 1098 | BestEditDistance); |
| 1099 | if (ED <= BestEditDistance) { |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1100 | if (ED < BestEditDistance) { |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1101 | Best.clear(); |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1102 | BestEditDistance = ED; |
| 1103 | } |
| 1104 | |
| 1105 | Best.push_back((*J)->Name); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | // If there was a clear winner, user it. |
| 1110 | if (Best.size() == 1) { |
| 1111 | getDiagnostics().Report(Path[I].second, |
| 1112 | diag::err_no_submodule_suggest) |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1113 | << Path[I].first << Module->getFullModuleName() << Best[0] |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1114 | << SourceRange(Path[0].second, Path[I-1].second) |
| 1115 | << FixItHint::CreateReplacement(SourceRange(Path[I].second), |
| 1116 | Best[0]); |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1117 | |
| 1118 | Sub = Module->findSubmodule(Best[0]); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1122 | if (!Sub) { |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1123 | // No submodule by this name. Complain, and don't look for further |
| 1124 | // submodules. |
| 1125 | getDiagnostics().Report(Path[I].second, diag::err_no_submodule) |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1126 | << Path[I].first << Module->getFullModuleName() |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1127 | << SourceRange(Path[0].second, Path[I-1].second); |
| 1128 | break; |
| 1129 | } |
| 1130 | |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1131 | Module = Sub; |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1132 | } |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1133 | } |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1134 | |
Douglas Gregor | 2537a36 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 1135 | // Make the named module visible, if it's not already part of the module |
| 1136 | // we are parsing. |
Douglas Gregor | 98a52db | 2011-12-20 00:28:52 +0000 | [diff] [blame] | 1137 | if (ModuleName != getLangOpts().CurrentModule) { |
| 1138 | if (!Module->IsFromModuleFile) { |
| 1139 | // We have an umbrella header or directory that doesn't actually include |
| 1140 | // all of the headers within the directory it covers. Complain about |
| 1141 | // this missing submodule and recover by forgetting that we ever saw |
| 1142 | // this submodule. |
| 1143 | // FIXME: Should we detect this at module load time? It seems fairly |
| 1144 | // expensive (and rare). |
| 1145 | getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule) |
| 1146 | << Module->getFullModuleName() |
| 1147 | << SourceRange(Path.front().second, Path.back().second); |
| 1148 | |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1149 | return ModuleLoadResult(0, true); |
Douglas Gregor | 98a52db | 2011-12-20 00:28:52 +0000 | [diff] [blame] | 1150 | } |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1151 | |
| 1152 | // Check whether this module is available. |
| 1153 | StringRef Feature; |
Douglas Gregor | 8992928 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 1154 | if (!Module->isAvailable(getLangOpts(), getTarget(), Feature)) { |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1155 | getDiagnostics().Report(ImportLoc, diag::err_module_unavailable) |
| 1156 | << Module->getFullModuleName() |
| 1157 | << Feature |
| 1158 | << SourceRange(Path.front().second, Path.back().second); |
| 1159 | LastModuleImportLoc = ImportLoc; |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1160 | LastModuleImportResult = ModuleLoadResult(); |
| 1161 | return ModuleLoadResult(); |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Douglas Gregor | 2537a36 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 1164 | ModuleManager->makeModuleVisible(Module, Visibility); |
Douglas Gregor | 98a52db | 2011-12-20 00:28:52 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Douglas Gregor | bcfc7d0 | 2011-12-02 23:42:12 +0000 | [diff] [blame] | 1167 | // If this module import was due to an inclusion directive, create an |
| 1168 | // implicit import declaration to capture it in the AST. |
| 1169 | if (IsInclusionDirective && hasASTContext()) { |
| 1170 | TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 72d1aa3 | 2012-10-03 01:58:37 +0000 | [diff] [blame] | 1171 | ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, |
| 1172 | ImportLoc, Module, |
| 1173 | Path.back().second); |
| 1174 | TU->addDecl(ImportD); |
| 1175 | if (Consumer) |
| 1176 | Consumer->HandleImplicitImportDecl(ImportD); |
Douglas Gregor | bcfc7d0 | 2011-12-02 23:42:12 +0000 | [diff] [blame] | 1177 | } |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1178 | |
Douglas Gregor | 1805b8a | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 1179 | LastModuleImportLoc = ImportLoc; |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1180 | LastModuleImportResult = ModuleLoadResult(Module, false); |
| 1181 | return LastModuleImportResult; |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1182 | } |
Douglas Gregor | c147b0b | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 1183 | |
| 1184 | void CompilerInstance::makeModuleVisible(Module *Mod, |
| 1185 | Module::NameVisibilityKind Visibility){ |
| 1186 | ModuleManager->makeModuleVisible(Mod, Visibility); |
| 1187 | } |
| 1188 | |