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