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" |
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" |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 19 | #include "clang/Lex/HeaderSearch.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Lex/PTHManager.h" |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/ChainedDiagnosticClient.h" |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/FrontendAction.h" |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame^] | 24 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/LogDiagnosticPrinter.h" |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Daniel Dunbar | 50ec0da | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/VerifyDiagnosticsClient.h" |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/Utils.h" |
Sebastian Redl | f5b1346 | 2010-08-18 23:57:17 +0000 | [diff] [blame] | 30 | #include "clang/Serialization/ASTReader.h" |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 31 | #include "clang/Sema/CodeCompleteConsumer.h" |
Michael J. Spencer | f6efe58 | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 32 | #include "llvm/Support/FileSystem.h" |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 171b780 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Statistic.h" |
Kovarththanan Rajaratnam | 5505dff | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Timer.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Host.h" |
| 38 | #include "llvm/Support/Path.h" |
| 39 | #include "llvm/Support/Program.h" |
| 40 | #include "llvm/Support/Signals.h" |
Michael J. Spencer | f25faaa | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 41 | #include "llvm/Support/system_error.h" |
Dylan Noblesmith | 321cdb8 | 2011-06-23 12:20:57 +0000 | [diff] [blame] | 42 | #include "llvm/Config/config.h" |
Daniel Dunbar | 636404a | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 43 | using namespace clang; |
| 44 | |
Daniel Dunbar | e922d9b | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 45 | CompilerInstance::CompilerInstance() |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 46 | : Invocation(new CompilerInvocation()), ModuleManager(0) { |
Daniel Dunbar | 6824225 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 47 | } |
Daniel Dunbar | 636404a | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 48 | |
| 49 | CompilerInstance::~CompilerInstance() { |
Daniel Dunbar | e922d9b | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Daniel Dunbar | 6824225 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 52 | void CompilerInstance::setInvocation(CompilerInvocation *Value) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 53 | Invocation = Value; |
Daniel Dunbar | 6824225 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 56 | void CompilerInstance::setDiagnostics(Diagnostic *Value) { |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 57 | Diagnostics = Value; |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 60 | void CompilerInstance::setTarget(TargetInfo *Value) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 61 | Target = Value; |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void CompilerInstance::setFileManager(FileManager *Value) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 65 | FileMgr = Value; |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 68 | void CompilerInstance::setSourceManager(SourceManager *Value) { |
| 69 | SourceMgr = Value; |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 72 | void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; } |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 74 | void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; } |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 75 | |
Douglas Gregor | 0e93f01 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 76 | void CompilerInstance::setSema(Sema *S) { |
| 77 | TheSema.reset(S); |
| 78 | } |
| 79 | |
Daniel Dunbar | 56d9c29 | 2009-11-14 02:47:17 +0000 | [diff] [blame] | 80 | void CompilerInstance::setASTConsumer(ASTConsumer *Value) { |
| 81 | Consumer.reset(Value); |
| 82 | } |
| 83 | |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 84 | void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) { |
| 85 | CompletionConsumer.reset(Value); |
| 86 | } |
| 87 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 88 | // Diagnostics |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 89 | static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts, |
Axel Naumann | 89c3149 | 2010-10-11 09:13:46 +0000 | [diff] [blame] | 90 | unsigned argc, const char* const *argv, |
Kovarththanan Rajaratnam | 4a94ba5 | 2010-03-17 09:24:48 +0000 | [diff] [blame] | 91 | Diagnostic &Diags) { |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 92 | std::string ErrorInfo; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 93 | llvm::OwningPtr<raw_ostream> OS( |
Kovarththanan Rajaratnam | eeed0cc | 2010-03-17 09:47:30 +0000 | [diff] [blame] | 94 | new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo)); |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 95 | if (!ErrorInfo.empty()) { |
Kovarththanan Rajaratnam | 4a94ba5 | 2010-03-17 09:24:48 +0000 | [diff] [blame] | 96 | Diags.Report(diag::err_fe_unable_to_open_logfile) |
| 97 | << DiagOpts.DumpBuildInformation << ErrorInfo; |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
Daniel Dunbar | 520d1e6 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 101 | (*OS) << "clang -cc1 command line arguments: "; |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 102 | for (unsigned i = 0; i != argc; ++i) |
| 103 | (*OS) << argv[i] << ' '; |
| 104 | (*OS) << '\n'; |
| 105 | |
| 106 | // Chain in a diagnostic client which will log the diagnostics. |
| 107 | DiagnosticClient *Logger = |
Kovarththanan Rajaratnam | eeed0cc | 2010-03-17 09:47:30 +0000 | [diff] [blame] | 108 | new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true); |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 109 | Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger)); |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 112 | static void SetUpDiagnosticLog(const DiagnosticOptions &DiagOpts, |
Daniel Dunbar | 7b83306 | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 113 | const CodeGenOptions *CodeGenOpts, |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 114 | Diagnostic &Diags) { |
| 115 | std::string ErrorInfo; |
| 116 | bool OwnsStream = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 117 | raw_ostream *OS = &llvm::errs(); |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 118 | if (DiagOpts.DiagnosticLogFile != "-") { |
| 119 | // Create the output stream. |
| 120 | llvm::raw_fd_ostream *FileOS( |
| 121 | new llvm::raw_fd_ostream(DiagOpts.DiagnosticLogFile.c_str(), |
Daniel Dunbar | 44d9ef7 | 2011-04-07 20:19:21 +0000 | [diff] [blame] | 122 | ErrorInfo, llvm::raw_fd_ostream::F_Append)); |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 123 | if (!ErrorInfo.empty()) { |
| 124 | Diags.Report(diag::warn_fe_cc_log_diagnostics_failure) |
| 125 | << DiagOpts.DumpBuildInformation << ErrorInfo; |
| 126 | } else { |
| 127 | FileOS->SetUnbuffered(); |
| 128 | FileOS->SetUseAtomicWrites(true); |
| 129 | OS = FileOS; |
| 130 | OwnsStream = true; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Chain in the diagnostic client which will log the diagnostics. |
Daniel Dunbar | 7b83306 | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 135 | LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts, |
| 136 | OwnsStream); |
| 137 | if (CodeGenOpts) |
| 138 | Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags); |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 139 | Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger)); |
| 140 | } |
| 141 | |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 142 | void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv, |
| 143 | DiagnosticClient *Client) { |
Daniel Dunbar | 7b83306 | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 144 | Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client, |
| 145 | &getCodeGenOpts()); |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 148 | llvm::IntrusiveRefCntPtr<Diagnostic> |
| 149 | CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts, |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 150 | int Argc, const char* const *Argv, |
Daniel Dunbar | 7b83306 | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 151 | DiagnosticClient *Client, |
| 152 | const CodeGenOptions *CodeGenOpts) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 153 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 154 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic(DiagID)); |
Daniel Dunbar | 1b39a2e | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 155 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 156 | // Create the diagnostic client for reporting errors or for |
| 157 | // implementing -verify. |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 158 | if (Client) |
| 159 | Diags->setClient(Client); |
| 160 | else |
| 161 | Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts)); |
Daniel Dunbar | 50ec0da | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 162 | |
| 163 | // Chain in -verify checker, if requested. |
| 164 | if (Opts.VerifyDiagnostics) |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 165 | Diags->setClient(new VerifyDiagnosticsClient(*Diags, Diags->takeClient())); |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 166 | |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 167 | // Chain in -diagnostic-log-file dumper, if requested. |
| 168 | if (!Opts.DiagnosticLogFile.empty()) |
Daniel Dunbar | 7b83306 | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 169 | SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags); |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 170 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 171 | if (!Opts.DumpBuildInformation.empty()) |
Kovarththanan Rajaratnam | 4a94ba5 | 2010-03-17 09:24:48 +0000 | [diff] [blame] | 172 | SetUpBuildDumpLog(Opts, Argc, Argv, *Diags); |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 173 | |
| 174 | // Configure our handling of diagnostics. |
Kovarththanan Rajaratnam | 9ff84d9 | 2010-03-17 09:36:02 +0000 | [diff] [blame] | 175 | ProcessWarningOptions(*Diags, Opts); |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 176 | |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 177 | return Diags; |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // File Manager |
| 181 | |
Daniel Dunbar | 546a676 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 182 | void CompilerInstance::createFileManager() { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 183 | FileMgr = new FileManager(getFileSystemOpts()); |
Daniel Dunbar | 546a676 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 186 | // Source Manager |
| 187 | |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 188 | void CompilerInstance::createSourceManager(FileManager &FileMgr) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 189 | SourceMgr = new SourceManager(getDiagnostics(), FileMgr); |
Daniel Dunbar | 546a676 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 190 | } |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 191 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 192 | // Preprocessor |
| 193 | |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 194 | void CompilerInstance::createPreprocessor() { |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 195 | const PreprocessorOptions &PPOpts = getPreprocessorOpts(); |
| 196 | |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 197 | // Create a PTH manager if we are using some form of a token cache. |
| 198 | PTHManager *PTHMgr = 0; |
Daniel Dunbar | d6ea902 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 199 | if (!PPOpts.TokenCache.empty()) |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 200 | PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics()); |
| 201 | |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 202 | // Create the Preprocessor. |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 203 | HeaderSearch *HeaderInfo = new HeaderSearch(getFileManager()); |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 204 | PP = new Preprocessor(getDiagnostics(), getLangOpts(), &getTarget(), |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 205 | getSourceManager(), *HeaderInfo, *this, PTHMgr, |
| 206 | /*OwnsHeaderSearch=*/true); |
| 207 | |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 208 | // Note that this is different then passing PTHMgr to Preprocessor's ctor. |
| 209 | // That argument is used as the IdentifierInfoLookup argument to |
| 210 | // IdentifierTable's ctor. |
| 211 | if (PTHMgr) { |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 212 | PTHMgr->setPreprocessor(&*PP); |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 213 | PP->setPTHManager(PTHMgr); |
| 214 | } |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 215 | |
Douglas Gregor | 7f6d60d | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 216 | if (PPOpts.DetailedRecord) |
Douglas Gregor | 998caea | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 217 | PP->createPreprocessingRecord( |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 218 | PPOpts.DetailedRecordIncludesNestedMacroExpansions); |
Douglas Gregor | 7f6d60d | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 219 | |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 220 | InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts()); |
| 221 | |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 222 | // Handle generating dependencies, if requested. |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 223 | const DependencyOutputOptions &DepOpts = getDependencyOutputOpts(); |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 224 | if (!DepOpts.OutputFile.empty()) |
| 225 | AttachDependencyFileGen(*PP, DepOpts); |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 226 | |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 227 | // Handle generating header include information, if requested. |
| 228 | if (DepOpts.ShowHeaderIncludes) |
| 229 | AttachHeaderIncludeGen(*PP); |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 230 | if (!DepOpts.HeaderIncludeOutputFile.empty()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 231 | StringRef OutputPath = DepOpts.HeaderIncludeOutputFile; |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 232 | if (OutputPath == "-") |
| 233 | OutputPath = ""; |
Daniel Dunbar | fe908a8 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 234 | AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath, |
| 235 | /*ShowDepth=*/false); |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 236 | } |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 237 | } |
Daniel Dunbar | df3e30c | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 238 | |
| 239 | // ASTContext |
| 240 | |
| 241 | void CompilerInstance::createASTContext() { |
| 242 | Preprocessor &PP = getPreprocessor(); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 243 | Context = new ASTContext(getLangOpts(), PP.getSourceManager(), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 244 | &getTarget(), PP.getIdentifierTable(), |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 245 | PP.getSelectorTable(), PP.getBuiltinInfo(), |
| 246 | /*size_reserve=*/ 0); |
Daniel Dunbar | df3e30c | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 247 | } |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 248 | |
| 249 | // ExternalASTSource |
| 250 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 251 | void CompilerInstance::createPCHExternalASTSource(StringRef Path, |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 252 | bool DisablePCHValidation, |
Douglas Gregor | 606c4ac | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 253 | bool DisableStatCache, |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 254 | void *DeserializationListener){ |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 255 | llvm::OwningPtr<ExternalASTSource> Source; |
Sebastian Redl | 009e7f2 | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 256 | bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 257 | Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot, |
Douglas Gregor | 606c4ac | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 258 | DisablePCHValidation, |
| 259 | DisableStatCache, |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 260 | getPreprocessor(), getASTContext(), |
Sebastian Redl | 009e7f2 | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 261 | DeserializationListener, |
| 262 | Preamble)); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 263 | ModuleManager = static_cast<ASTReader*>(Source.get()); |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 264 | getASTContext().setExternalSource(Source); |
| 265 | } |
| 266 | |
| 267 | ExternalASTSource * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 268 | CompilerInstance::createPCHExternalASTSource(StringRef Path, |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 269 | const std::string &Sysroot, |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 270 | bool DisablePCHValidation, |
Douglas Gregor | 606c4ac | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 271 | bool DisableStatCache, |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 272 | Preprocessor &PP, |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 273 | ASTContext &Context, |
Sebastian Redl | 009e7f2 | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 274 | void *DeserializationListener, |
| 275 | bool Preamble) { |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 276 | llvm::OwningPtr<ASTReader> Reader; |
Douglas Gregor | 8835e03 | 2011-09-02 00:26:20 +0000 | [diff] [blame] | 277 | Reader.reset(new ASTReader(PP, Context, |
Douglas Gregor | c567ba2 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 278 | Sysroot.empty() ? "" : Sysroot.c_str(), |
Douglas Gregor | 606c4ac | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 279 | DisablePCHValidation, DisableStatCache)); |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 280 | |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 281 | Reader->setDeserializationListener( |
Sebastian Redl | 3e31c72 | 2010-08-18 23:56:56 +0000 | [diff] [blame] | 282 | static_cast<ASTDeserializationListener *>(DeserializationListener)); |
Douglas Gregor | a6895d8 | 2011-07-22 16:00:58 +0000 | [diff] [blame] | 283 | switch (Reader->ReadAST(Path, |
| 284 | Preamble ? serialization::MK_Preamble |
| 285 | : serialization::MK_PCH)) { |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 286 | case ASTReader::Success: |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 287 | // Set the predefines buffer as suggested by the PCH reader. Typically, the |
| 288 | // predefines buffer will be empty. |
| 289 | PP.setPredefines(Reader->getSuggestedPredefines()); |
| 290 | return Reader.take(); |
| 291 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 292 | case ASTReader::Failure: |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 293 | // Unrecoverable failure: don't even try to process the input file. |
| 294 | break; |
| 295 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 296 | case ASTReader::IgnorePCH: |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 297 | // No suitable PCH file could be found. Return an error. |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | return 0; |
| 302 | } |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 303 | |
| 304 | // Code Completion |
| 305 | |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 306 | static bool EnableCodeCompletion(Preprocessor &PP, |
| 307 | const std::string &Filename, |
| 308 | unsigned Line, |
| 309 | unsigned Column) { |
| 310 | // Tell the source manager to chop off the given file at a specific |
| 311 | // line and column. |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 312 | const FileEntry *Entry = PP.getFileManager().getFile(Filename); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 313 | if (!Entry) { |
| 314 | PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) |
| 315 | << Filename; |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | // Truncate the named file at the given line/column. |
| 320 | PP.SetCodeCompletionPoint(Entry, Line, Column); |
| 321 | return false; |
| 322 | } |
| 323 | |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 324 | void CompilerInstance::createCodeCompletionConsumer() { |
| 325 | const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 326 | if (!CompletionConsumer) { |
| 327 | CompletionConsumer.reset( |
| 328 | createCodeCompletionConsumer(getPreprocessor(), |
| 329 | Loc.FileName, Loc.Line, Loc.Column, |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 330 | getFrontendOpts().ShowMacrosInCodeCompletion, |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 331 | getFrontendOpts().ShowCodePatternsInCodeCompletion, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 332 | getFrontendOpts().ShowGlobalSymbolsInCodeCompletion, |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 333 | llvm::outs())); |
| 334 | if (!CompletionConsumer) |
| 335 | return; |
| 336 | } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName, |
| 337 | Loc.Line, Loc.Column)) { |
| 338 | CompletionConsumer.reset(); |
Douglas Gregor | 00a0cf7 | 2010-03-16 06:04:47 +0000 | [diff] [blame] | 339 | return; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 340 | } |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 341 | |
| 342 | if (CompletionConsumer->isOutputBinary() && |
| 343 | llvm::sys::Program::ChangeStdoutToBinary()) { |
| 344 | getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary); |
| 345 | CompletionConsumer.reset(); |
| 346 | } |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Kovarththanan Rajaratnam | 5505dff | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 349 | void CompilerInstance::createFrontendTimer() { |
| 350 | FrontendTimer.reset(new llvm::Timer("Clang front-end timer")); |
| 351 | } |
| 352 | |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 353 | CodeCompleteConsumer * |
| 354 | CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP, |
| 355 | const std::string &Filename, |
| 356 | unsigned Line, |
| 357 | unsigned Column, |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 358 | bool ShowMacros, |
Douglas Gregor | f64acca | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 359 | bool ShowCodePatterns, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 360 | bool ShowGlobals, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 361 | raw_ostream &OS) { |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 362 | if (EnableCodeCompletion(PP, Filename, Line, Column)) |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 363 | return 0; |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 364 | |
| 365 | // Set up the creation routine for code-completion. |
Douglas Gregor | b9ab0ed | 2010-10-11 22:12:15 +0000 | [diff] [blame] | 366 | return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 367 | ShowGlobals, OS); |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 368 | } |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 369 | |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 370 | void CompilerInstance::createSema(TranslationUnitKind TUKind, |
Douglas Gregor | 0e93f01 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 371 | CodeCompleteConsumer *CompletionConsumer) { |
| 372 | TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(), |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 373 | TUKind, CompletionConsumer)); |
Douglas Gregor | 0e93f01 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 376 | // Output Files |
| 377 | |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 378 | void CompilerInstance::addOutputFile(const OutputFile &OutFile) { |
| 379 | assert(OutFile.OS && "Attempt to add empty stream to output list!"); |
| 380 | OutputFiles.push_back(OutFile); |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Kovarththanan Rajaratnam | 1c558cd | 2010-03-06 12:07:48 +0000 | [diff] [blame] | 383 | void CompilerInstance::clearOutputFiles(bool EraseFiles) { |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 384 | for (std::list<OutputFile>::iterator |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 385 | it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) { |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 386 | delete it->OS; |
| 387 | if (!it->TempFilename.empty()) { |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 388 | if (EraseFiles) { |
| 389 | bool existed; |
| 390 | llvm::sys::fs::remove(it->TempFilename, existed); |
| 391 | } else { |
| 392 | llvm::SmallString<128> NewOutFile(it->Filename); |
| 393 | |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 394 | // If '-working-directory' was passed, the output filename should be |
| 395 | // relative to that. |
Anders Carlsson | 9ba8fb1 | 2011-03-14 01:13:54 +0000 | [diff] [blame] | 396 | FileMgr->FixupRelativePath(NewOutFile); |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 397 | if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename, |
| 398 | NewOutFile.str())) { |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 399 | getDiagnostics().Report(diag::err_fe_unable_to_rename_temp) |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 400 | << it->TempFilename << it->Filename << ec.message(); |
| 401 | |
| 402 | bool existed; |
| 403 | llvm::sys::fs::remove(it->TempFilename, existed); |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | } else if (!it->Filename.empty() && EraseFiles) |
| 407 | llvm::sys::Path(it->Filename).eraseFromDisk(); |
| 408 | |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 409 | } |
| 410 | OutputFiles.clear(); |
| 411 | } |
| 412 | |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 413 | llvm::raw_fd_ostream * |
| 414 | CompilerInstance::createDefaultOutputFile(bool Binary, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 415 | StringRef InFile, |
| 416 | StringRef Extension) { |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 417 | return createOutputFile(getFrontendOpts().OutputFile, Binary, |
Daniel Dunbar | e326f9b | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 418 | /*RemoveFileOnSignal=*/true, InFile, Extension); |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | llvm::raw_fd_ostream * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 422 | CompilerInstance::createOutputFile(StringRef OutputPath, |
Daniel Dunbar | e326f9b | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 423 | bool Binary, bool RemoveFileOnSignal, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 424 | StringRef InFile, |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 425 | StringRef Extension, |
| 426 | bool UseTemporary) { |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 427 | std::string Error, OutputPathName, TempPathName; |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 428 | llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary, |
Daniel Dunbar | e326f9b | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 429 | RemoveFileOnSignal, |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 430 | InFile, Extension, |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 431 | UseTemporary, |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 432 | &OutputPathName, |
| 433 | &TempPathName); |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 434 | if (!OS) { |
Daniel Dunbar | 7554699 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 435 | getDiagnostics().Report(diag::err_fe_unable_to_open_output) |
| 436 | << OutputPath << Error; |
| 437 | return 0; |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | // Add the output file -- but don't try to remove "-", since this means we are |
| 441 | // using stdin. |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 442 | addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "", |
| 443 | TempPathName, OS)); |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 444 | |
| 445 | return OS; |
| 446 | } |
| 447 | |
| 448 | llvm::raw_fd_ostream * |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 449 | CompilerInstance::createOutputFile(StringRef OutputPath, |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 450 | std::string &Error, |
| 451 | bool Binary, |
Daniel Dunbar | e326f9b | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 452 | bool RemoveFileOnSignal, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 453 | StringRef InFile, |
| 454 | StringRef Extension, |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 455 | bool UseTemporary, |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 456 | std::string *ResultPathName, |
| 457 | std::string *TempPathName) { |
| 458 | std::string OutFile, TempFile; |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 459 | if (!OutputPath.empty()) { |
| 460 | OutFile = OutputPath; |
| 461 | } else if (InFile == "-") { |
| 462 | OutFile = "-"; |
| 463 | } else if (!Extension.empty()) { |
| 464 | llvm::sys::Path Path(InFile); |
| 465 | Path.eraseSuffix(); |
| 466 | Path.appendSuffix(Extension); |
| 467 | OutFile = Path.str(); |
| 468 | } else { |
| 469 | OutFile = "-"; |
| 470 | } |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 471 | |
| 472 | llvm::OwningPtr<llvm::raw_fd_ostream> OS; |
| 473 | std::string OSFile; |
| 474 | |
| 475 | if (UseTemporary && OutFile != "-") { |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 476 | llvm::sys::Path OutPath(OutFile); |
| 477 | // Only create the temporary if we can actually write to OutPath, otherwise |
| 478 | // we want to fail early. |
Michael J. Spencer | f6efe58 | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 479 | bool Exists; |
| 480 | if ((llvm::sys::fs::exists(OutPath.str(), Exists) || !Exists) || |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 481 | (OutPath.isRegularFile() && OutPath.canWrite())) { |
| 482 | // Create a temporary file. |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 483 | llvm::SmallString<128> TempPath; |
| 484 | TempPath = OutFile; |
| 485 | TempPath += "-%%%%%%%%"; |
| 486 | int fd; |
| 487 | if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath, |
| 488 | /*makeAbsolute=*/false) == llvm::errc::success) { |
| 489 | OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true)); |
| 490 | OSFile = TempFile = TempPath.str(); |
| 491 | } |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 495 | if (!OS) { |
| 496 | OSFile = OutFile; |
| 497 | OS.reset( |
| 498 | new llvm::raw_fd_ostream(OSFile.c_str(), Error, |
| 499 | (Binary ? llvm::raw_fd_ostream::F_Binary : 0))); |
| 500 | if (!Error.empty()) |
| 501 | return 0; |
| 502 | } |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 503 | |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 504 | // Make sure the out stream file gets removed if we crash. |
Daniel Dunbar | e326f9b | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 505 | if (RemoveFileOnSignal) |
| 506 | llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile)); |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 507 | |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 508 | if (ResultPathName) |
| 509 | *ResultPathName = OutFile; |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 510 | if (TempPathName) |
| 511 | *TempPathName = TempFile; |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 512 | |
Daniel Dunbar | 2eaef18 | 2009-11-20 22:32:38 +0000 | [diff] [blame] | 513 | return OS.take(); |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 514 | } |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 515 | |
| 516 | // Initialization Utilities |
| 517 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 518 | bool CompilerInstance::InitializeSourceManager(StringRef InputFile) { |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 519 | return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(), |
| 520 | getSourceManager(), getFrontendOpts()); |
| 521 | } |
| 522 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 523 | bool CompilerInstance::InitializeSourceManager(StringRef InputFile, |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 524 | Diagnostic &Diags, |
| 525 | FileManager &FileMgr, |
| 526 | SourceManager &SourceMgr, |
| 527 | const FrontendOptions &Opts) { |
Douglas Gregor | 936a5b4 | 2010-11-30 05:23:00 +0000 | [diff] [blame] | 528 | // Figure out where to get and map in the main file, unless it's already |
| 529 | // been created (e.g., by a precompiled preamble). |
| 530 | if (!SourceMgr.getMainFileID().isInvalid()) { |
| 531 | // Do nothing: the main file has already been set. |
| 532 | } else if (InputFile != "-") { |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 533 | const FileEntry *File = FileMgr.getFile(InputFile); |
Dan Gohman | 5276521 | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 534 | if (!File) { |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 535 | Diags.Report(diag::err_fe_error_reading) << InputFile; |
| 536 | return false; |
| 537 | } |
Dan Gohman | 5276521 | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 538 | SourceMgr.createMainFileID(File); |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 539 | } else { |
Michael J. Spencer | d9da7a1 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 540 | llvm::OwningPtr<llvm::MemoryBuffer> SB; |
| 541 | if (llvm::MemoryBuffer::getSTDIN(SB)) { |
Michael J. Spencer | f25faaa | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 542 | // FIXME: Give ec.message() in this diag. |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 543 | Diags.Report(diag::err_fe_error_reading_stdin); |
| 544 | return false; |
| 545 | } |
Dan Gohman | 2f76cd7 | 2010-10-26 23:21:25 +0000 | [diff] [blame] | 546 | const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(), |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 547 | SB->getBufferSize(), 0); |
Dan Gohman | 2f76cd7 | 2010-10-26 23:21:25 +0000 | [diff] [blame] | 548 | SourceMgr.createMainFileID(File); |
Michael J. Spencer | d9da7a1 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 549 | SourceMgr.overrideFileContents(File, SB.take()); |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Dan Gohman | 5276521 | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 552 | assert(!SourceMgr.getMainFileID().isInvalid() && |
| 553 | "Couldn't establish MainFileID!"); |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 554 | return true; |
| 555 | } |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 556 | |
| 557 | // High-Level Operations |
| 558 | |
| 559 | bool CompilerInstance::ExecuteAction(FrontendAction &Act) { |
| 560 | assert(hasDiagnostics() && "Diagnostics engine is not initialized!"); |
| 561 | assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!"); |
| 562 | assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!"); |
| 563 | |
| 564 | // FIXME: Take this as an argument, once all the APIs we used have moved to |
| 565 | // taking it as an input instead of hard-coding llvm::errs. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 566 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 567 | |
| 568 | // Create the target instance. |
| 569 | setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts())); |
| 570 | if (!hasTarget()) |
| 571 | return false; |
| 572 | |
| 573 | // Inform the target of the language options. |
| 574 | // |
| 575 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 576 | // created. This complexity should be lifted elsewhere. |
| 577 | getTarget().setForcedLangOptions(getLangOpts()); |
| 578 | |
| 579 | // Validate/process some options. |
| 580 | if (getHeaderSearchOpts().Verbose) |
| 581 | OS << "clang -cc1 version " CLANG_VERSION_STRING |
| 582 | << " based upon " << PACKAGE_STRING |
| 583 | << " hosted on " << llvm::sys::getHostTriple() << "\n"; |
| 584 | |
| 585 | if (getFrontendOpts().ShowTimers) |
| 586 | createFrontendTimer(); |
| 587 | |
Douglas Gregor | 171b780 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 588 | if (getFrontendOpts().ShowStats) |
| 589 | llvm::EnableStatistics(); |
| 590 | |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 591 | for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) { |
| 592 | const std::string &InFile = getFrontendOpts().Inputs[i].second; |
| 593 | |
Daniel Dunbar | aed46fc | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 594 | // Reset the ID tables if we are reusing the SourceManager. |
| 595 | if (hasSourceManager()) |
| 596 | getSourceManager().clearIDTables(); |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 597 | |
Daniel Dunbar | 8654638 | 2010-06-07 23:23:06 +0000 | [diff] [blame] | 598 | if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) { |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 599 | Act.Execute(); |
| 600 | Act.EndSourceFile(); |
| 601 | } |
| 602 | } |
| 603 | |
Chris Lattner | 198cb4d | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 604 | if (getDiagnosticOpts().ShowCarets) { |
Argyrios Kyrtzidis | c79346a | 2010-11-18 20:06:46 +0000 | [diff] [blame] | 605 | // We can have multiple diagnostics sharing one diagnostic client. |
| 606 | // Get the total number of warnings/errors from the client. |
| 607 | unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings(); |
| 608 | unsigned NumErrors = getDiagnostics().getClient()->getNumErrors(); |
Chris Lattner | 198cb4d | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 609 | |
| 610 | if (NumWarnings) |
| 611 | OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s"); |
| 612 | if (NumWarnings && NumErrors) |
| 613 | OS << " and "; |
| 614 | if (NumErrors) |
| 615 | OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s"); |
| 616 | if (NumWarnings || NumErrors) |
| 617 | OS << " generated.\n"; |
| 618 | } |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 619 | |
Daniel Dunbar | aed46fc | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 620 | if (getFrontendOpts().ShowStats && hasFileManager()) { |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 621 | getFileManager().PrintStats(); |
| 622 | OS << "\n"; |
| 623 | } |
| 624 | |
Argyrios Kyrtzidis | bc46793 | 2010-11-18 21:13:57 +0000 | [diff] [blame] | 625 | return !getDiagnostics().getClient()->getNumErrors(); |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame^] | 628 | /// \brief Determine the appropriate source input kind based on language |
| 629 | /// options. |
| 630 | static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) { |
| 631 | if (LangOpts.OpenCL) |
| 632 | return IK_OpenCL; |
| 633 | if (LangOpts.CUDA) |
| 634 | return IK_CUDA; |
| 635 | if (LangOpts.ObjC1) |
| 636 | return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC; |
| 637 | return LangOpts.CPlusPlus? IK_CXX : IK_C; |
| 638 | } |
| 639 | |
| 640 | /// \brief Compile a module file for the given module name with the given |
| 641 | /// umbrella header, using the options provided by the importing compiler |
| 642 | /// instance. |
| 643 | static void compileModule(CompilerInstance &ImportingInstance, |
| 644 | StringRef ModuleName, |
| 645 | StringRef UmbrellaHeader) { |
| 646 | // Determine the file that we'll be writing to. |
| 647 | llvm::SmallString<128> ModuleFile; |
| 648 | ModuleFile += |
| 649 | ImportingInstance.getInvocation().getHeaderSearchOpts().ModuleCachePath; |
| 650 | llvm::sys::path::append(ModuleFile, ModuleName + ".pcm"); |
| 651 | |
| 652 | // Construct a compiler invocation for creating this module. |
| 653 | llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation |
| 654 | (new CompilerInvocation(ImportingInstance.getInvocation())); |
| 655 | FrontendOptions &FrontendOpts = Invocation->getFrontendOpts(); |
| 656 | FrontendOpts.OutputFile = ModuleFile.str(); |
| 657 | FrontendOpts.DisableFree = false; |
| 658 | FrontendOpts.Inputs.clear(); |
| 659 | FrontendOpts.Inputs.push_back( |
| 660 | std::make_pair(getSourceInputKindFromOptions(Invocation->getLangOpts()), |
| 661 | UmbrellaHeader)); |
| 662 | // FIXME: Strip away all of the compilation options that won't be transferred |
| 663 | // down to the module. This presumably includes -D flags, optimization |
| 664 | // settings, etc. |
| 665 | |
| 666 | // Construct a compiler instance that will be used to actually create the |
| 667 | // module. |
| 668 | CompilerInstance Instance; |
| 669 | Instance.setInvocation(&*Invocation); |
| 670 | // Instance.setDiagnostics(&ImportingInstance.getDiagnostics()); |
| 671 | // FIXME: Need to route diagnostics over to the same diagnostic client! |
| 672 | Instance.createDiagnostics(0, 0, 0); |
| 673 | |
| 674 | // Construct a module-generating action. |
| 675 | GeneratePCHAction CreateModuleAction(true); |
| 676 | |
| 677 | // Execute the action to actually build the module in-place. |
| 678 | // FIXME: Need to synchronize when multiple processes do this. |
| 679 | Instance.ExecuteAction(CreateModuleAction); |
| 680 | |
| 681 | // Tell the importing instance's file manager to forget about the module |
| 682 | // file, since we've just created it. |
| 683 | ImportingInstance.getFileManager().forgetFile(ModuleFile); |
| 684 | } |
| 685 | |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 686 | ModuleKey CompilerInstance::loadModule(SourceLocation ImportLoc, |
| 687 | IdentifierInfo &ModuleName, |
| 688 | SourceLocation ModuleNameLoc) { |
| 689 | // Determine what file we're searching from. |
| 690 | SourceManager &SourceMgr = getSourceManager(); |
| 691 | SourceLocation ExpandedImportLoc = SourceMgr.getExpansionLoc(ImportLoc); |
| 692 | const FileEntry *CurFile |
| 693 | = SourceMgr.getFileEntryForID(SourceMgr.getFileID(ExpandedImportLoc)); |
| 694 | if (!CurFile) |
| 695 | CurFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()); |
| 696 | |
| 697 | // Search for a module with the given name. |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame^] | 698 | std::string UmbrellaHeader; |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 699 | const FileEntry *ModuleFile |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame^] | 700 | = PP->getHeaderSearchInfo().lookupModule(ModuleName.getName(), |
| 701 | &UmbrellaHeader); |
| 702 | |
| 703 | bool BuildingModule = false; |
| 704 | if (!ModuleFile && !UmbrellaHeader.empty()) { |
| 705 | // We didn't find the module, but there is an umbrella header that |
| 706 | // can be used to create the module file. Create a separate compilation |
| 707 | // module to do so. |
| 708 | BuildingModule = true; |
| 709 | compileModule(*this, ModuleName.getName(), UmbrellaHeader); |
| 710 | ModuleFile = PP->getHeaderSearchInfo().lookupModule(ModuleName.getName()); |
| 711 | } |
| 712 | |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 713 | if (!ModuleFile) { |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame^] | 714 | getDiagnostics().Report(ModuleNameLoc, |
| 715 | BuildingModule? diag::err_module_not_built |
| 716 | : diag::err_module_not_found) |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 717 | << ModuleName.getName() |
| 718 | << SourceRange(ImportLoc, ModuleNameLoc); |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | // If we don't already have an ASTReader, create one now. |
| 723 | if (!ModuleManager) { |
| 724 | std::string Sysroot = getHeaderSearchOpts().Sysroot; |
| 725 | const PreprocessorOptions &PPOpts = getPreprocessorOpts(); |
Douglas Gregor | 8835e03 | 2011-09-02 00:26:20 +0000 | [diff] [blame] | 726 | ModuleManager = new ASTReader(getPreprocessor(), *Context, |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 727 | Sysroot.empty() ? "" : Sysroot.c_str(), |
| 728 | PPOpts.DisablePCHValidation, |
| 729 | PPOpts.DisableStatCache); |
| 730 | ModuleManager->setDeserializationListener( |
| 731 | getASTConsumer().GetASTDeserializationListener()); |
| 732 | getASTContext().setASTMutationListener( |
| 733 | getASTConsumer().GetASTMutationListener()); |
| 734 | llvm::OwningPtr<ExternalASTSource> Source; |
| 735 | Source.reset(ModuleManager); |
| 736 | getASTContext().setExternalSource(Source); |
| 737 | ModuleManager->InitializeSema(getSema()); |
| 738 | } |
| 739 | |
| 740 | // Try to load the module we found. |
| 741 | switch (ModuleManager->ReadAST(ModuleFile->getName(), |
| 742 | serialization::MK_Module)) { |
| 743 | case ASTReader::Success: |
| 744 | break; |
| 745 | |
| 746 | case ASTReader::IgnorePCH: |
| 747 | // FIXME: The ASTReader will already have complained, but can we showhorn |
| 748 | // that diagnostic information into a more useful form? |
| 749 | return 0; |
| 750 | |
| 751 | case ASTReader::Failure: |
| 752 | // Already complained. |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | // FIXME: The module file's FileEntry makes a poor key indeed! |
| 757 | return (ModuleKey)ModuleFile; |
| 758 | } |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 759 | |