Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 1 | //===--- CompilerInstance.cpp ---------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "clang/Frontend/CompilerInstance.h" |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 11 | #include "clang/Sema/Sema.h" |
Daniel Dunbar | 12ce694 | 2009-11-14 02:47:17 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | 5eb8100 | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Diagnostic.h" |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Version.h" |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 19 | #include "clang/Lex/HeaderSearch.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Lex/PTHManager.h" |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/ChainedDiagnosticClient.h" |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/FrontendAction.h" |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/VerifyDiagnosticsClient.h" |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/Utils.h" |
Sebastian Redl | 6ab7cd8 | 2010-08-18 23:57:17 +0000 | [diff] [blame] | 28 | #include "clang/Serialization/ASTReader.h" |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 29 | #include "clang/Sema/CodeCompleteConsumer.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 30 | #include "llvm/LLVMContext.h" |
Michael J. Spencer | 32bef4e | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 31 | #include "llvm/Support/FileSystem.h" |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 95dd558 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/Statistic.h" |
Kovarththanan Rajaratnam | f79bafa | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Timer.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Host.h" |
| 37 | #include "llvm/Support/Path.h" |
| 38 | #include "llvm/Support/Program.h" |
| 39 | #include "llvm/Support/Signals.h" |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 40 | #include "llvm/Support/system_error.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 41 | using namespace clang; |
| 42 | |
Daniel Dunbar | 42e9f8e4 | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 43 | CompilerInstance::CompilerInstance() |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 44 | : Invocation(new CompilerInvocation()) { |
Daniel Dunbar | 6228ca0 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 45 | } |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 46 | |
| 47 | CompilerInstance::~CompilerInstance() { |
Daniel Dunbar | 42e9f8e4 | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void CompilerInstance::setLLVMContext(llvm::LLVMContext *Value) { |
| 51 | LLVMContext.reset(Value); |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 52 | } |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 53 | |
Daniel Dunbar | 6228ca0 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 54 | void CompilerInstance::setInvocation(CompilerInvocation *Value) { |
| 55 | Invocation.reset(Value); |
| 56 | } |
| 57 | |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 58 | void CompilerInstance::setDiagnostics(Diagnostic *Value) { |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 59 | Diagnostics = Value; |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 62 | void CompilerInstance::setTarget(TargetInfo *Value) { |
| 63 | Target.reset(Value); |
| 64 | } |
| 65 | |
| 66 | void CompilerInstance::setFileManager(FileManager *Value) { |
| 67 | FileMgr.reset(Value); |
| 68 | } |
| 69 | |
| 70 | void CompilerInstance::setSourceManager(SourceManager *Value) { |
| 71 | SourceMgr.reset(Value); |
| 72 | } |
| 73 | |
| 74 | void CompilerInstance::setPreprocessor(Preprocessor *Value) { |
| 75 | PP.reset(Value); |
| 76 | } |
| 77 | |
| 78 | void CompilerInstance::setASTContext(ASTContext *Value) { |
| 79 | Context.reset(Value); |
| 80 | } |
| 81 | |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 82 | void CompilerInstance::setSema(Sema *S) { |
| 83 | TheSema.reset(S); |
| 84 | } |
| 85 | |
Daniel Dunbar | 12ce694 | 2009-11-14 02:47:17 +0000 | [diff] [blame] | 86 | void CompilerInstance::setASTConsumer(ASTConsumer *Value) { |
| 87 | Consumer.reset(Value); |
| 88 | } |
| 89 | |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 90 | void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) { |
| 91 | CompletionConsumer.reset(Value); |
| 92 | } |
| 93 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 94 | // Diagnostics |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 95 | static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts, |
Axel Naumann | 7d0c4cc | 2010-10-11 09:13:46 +0000 | [diff] [blame] | 96 | unsigned argc, const char* const *argv, |
Kovarththanan Rajaratnam | 3d67b1e | 2010-03-17 09:24:48 +0000 | [diff] [blame] | 97 | Diagnostic &Diags) { |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 98 | std::string ErrorInfo; |
Kovarththanan Rajaratnam | 6924713 | 2010-03-17 09:47:30 +0000 | [diff] [blame] | 99 | llvm::OwningPtr<llvm::raw_ostream> OS( |
| 100 | new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo)); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 101 | if (!ErrorInfo.empty()) { |
Kovarththanan Rajaratnam | 3d67b1e | 2010-03-17 09:24:48 +0000 | [diff] [blame] | 102 | Diags.Report(diag::err_fe_unable_to_open_logfile) |
| 103 | << DiagOpts.DumpBuildInformation << ErrorInfo; |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | |
Daniel Dunbar | dd63b28 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 107 | (*OS) << "clang -cc1 command line arguments: "; |
Daniel Dunbar | 0fbb3d9 | 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. |
| 113 | DiagnosticClient *Logger = |
Kovarththanan Rajaratnam | 6924713 | 2010-03-17 09:47:30 +0000 | [diff] [blame] | 114 | new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true); |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 115 | Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger)); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 118 | void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv, |
| 119 | DiagnosticClient *Client) { |
| 120 | Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 123 | llvm::IntrusiveRefCntPtr<Diagnostic> |
| 124 | CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts, |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 125 | int Argc, const char* const *Argv, |
| 126 | DiagnosticClient *Client) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 127 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 128 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic(DiagID)); |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 129 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 130 | // Create the diagnostic client for reporting errors or for |
| 131 | // implementing -verify. |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 132 | if (Client) |
| 133 | Diags->setClient(Client); |
| 134 | else |
| 135 | Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts)); |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 136 | |
| 137 | // Chain in -verify checker, if requested. |
| 138 | if (Opts.VerifyDiagnostics) |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 139 | Diags->setClient(new VerifyDiagnosticsClient(*Diags, Diags->takeClient())); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 140 | |
| 141 | if (!Opts.DumpBuildInformation.empty()) |
Kovarththanan Rajaratnam | 3d67b1e | 2010-03-17 09:24:48 +0000 | [diff] [blame] | 142 | SetUpBuildDumpLog(Opts, Argc, Argv, *Diags); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 143 | |
| 144 | // Configure our handling of diagnostics. |
Kovarththanan Rajaratnam | 5bf932b | 2010-03-17 09:36:02 +0000 | [diff] [blame] | 145 | ProcessWarningOptions(*Diags, Opts); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 146 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 147 | return Diags; |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // File Manager |
| 151 | |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 152 | void CompilerInstance::createFileManager() { |
Chris Lattner | 7ad97ff | 2010-11-23 07:51:02 +0000 | [diff] [blame] | 153 | FileMgr.reset(new FileManager(getFileSystemOpts())); |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 156 | // Source Manager |
| 157 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 158 | void CompilerInstance::createSourceManager(FileManager &FileMgr) { |
| 159 | SourceMgr.reset(new SourceManager(getDiagnostics(), FileMgr)); |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 160 | } |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 161 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 162 | // Preprocessor |
| 163 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 164 | void CompilerInstance::createPreprocessor() { |
| 165 | PP.reset(createPreprocessor(getDiagnostics(), getLangOpts(), |
| 166 | getPreprocessorOpts(), getHeaderSearchOpts(), |
| 167 | getDependencyOutputOpts(), getTarget(), |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 168 | getFrontendOpts(), getSourceManager(), |
| 169 | getFileManager())); |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | Preprocessor * |
| 173 | CompilerInstance::createPreprocessor(Diagnostic &Diags, |
| 174 | const LangOptions &LangInfo, |
| 175 | const PreprocessorOptions &PPOpts, |
| 176 | const HeaderSearchOptions &HSOpts, |
| 177 | const DependencyOutputOptions &DepOpts, |
| 178 | const TargetInfo &Target, |
Fariborz Jahanian | 7d95747 | 2010-01-13 18:51:17 +0000 | [diff] [blame] | 179 | const FrontendOptions &FEOpts, |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 180 | SourceManager &SourceMgr, |
| 181 | FileManager &FileMgr) { |
| 182 | // Create a PTH manager if we are using some form of a token cache. |
| 183 | PTHManager *PTHMgr = 0; |
Daniel Dunbar | 049d3a0 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 184 | if (!PPOpts.TokenCache.empty()) |
Chris Lattner | 681c74a | 2010-11-23 09:01:31 +0000 | [diff] [blame] | 185 | PTHMgr = PTHManager::Create(PPOpts.TokenCache, Diags); |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 186 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 187 | // Create the Preprocessor. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 188 | HeaderSearch *HeaderInfo = new HeaderSearch(FileMgr); |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 189 | Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target, |
| 190 | SourceMgr, *HeaderInfo, PTHMgr, |
| 191 | /*OwnsHeaderSearch=*/true); |
| 192 | |
| 193 | // Note that this is different then passing PTHMgr to Preprocessor's ctor. |
| 194 | // That argument is used as the IdentifierInfoLookup argument to |
| 195 | // IdentifierTable's ctor. |
| 196 | if (PTHMgr) { |
| 197 | PTHMgr->setPreprocessor(PP); |
| 198 | PP->setPTHManager(PTHMgr); |
| 199 | } |
| 200 | |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 201 | if (PPOpts.DetailedRecord) |
| 202 | PP->createPreprocessingRecord(); |
| 203 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 204 | InitializePreprocessor(*PP, PPOpts, HSOpts, FEOpts); |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 205 | |
| 206 | // Handle generating dependencies, if requested. |
| 207 | if (!DepOpts.OutputFile.empty()) |
| 208 | AttachDependencyFileGen(*PP, DepOpts); |
| 209 | |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 210 | // Handle generating header include information, if requested. |
| 211 | if (DepOpts.ShowHeaderIncludes) |
| 212 | AttachHeaderIncludeGen(*PP); |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 213 | if (!DepOpts.HeaderIncludeOutputFile.empty()) { |
| 214 | llvm::StringRef OutputPath = DepOpts.HeaderIncludeOutputFile; |
| 215 | if (OutputPath == "-") |
| 216 | OutputPath = ""; |
| 217 | AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath); |
| 218 | } |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 219 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 220 | return PP; |
| 221 | } |
Daniel Dunbar | 5eb8100 | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 222 | |
| 223 | // ASTContext |
| 224 | |
| 225 | void CompilerInstance::createASTContext() { |
| 226 | Preprocessor &PP = getPreprocessor(); |
| 227 | Context.reset(new ASTContext(getLangOpts(), PP.getSourceManager(), |
| 228 | getTarget(), PP.getIdentifierTable(), |
| 229 | PP.getSelectorTable(), PP.getBuiltinInfo(), |
Daniel Dunbar | 5eb8100 | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 230 | /*size_reserve=*/ 0)); |
| 231 | } |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 232 | |
| 233 | // ExternalASTSource |
| 234 | |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 235 | void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 236 | bool DisablePCHValidation, |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 237 | bool DisableStatCache, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 238 | void *DeserializationListener){ |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 239 | llvm::OwningPtr<ExternalASTSource> Source; |
Sebastian Redl | 1d9f1fe | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 240 | bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 241 | Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot, |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 242 | DisablePCHValidation, |
| 243 | DisableStatCache, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 244 | getPreprocessor(), getASTContext(), |
Sebastian Redl | 1d9f1fe | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 245 | DeserializationListener, |
| 246 | Preamble)); |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 247 | getASTContext().setExternalSource(Source); |
| 248 | } |
| 249 | |
| 250 | ExternalASTSource * |
| 251 | CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path, |
| 252 | const std::string &Sysroot, |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 253 | bool DisablePCHValidation, |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 254 | bool DisableStatCache, |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 255 | Preprocessor &PP, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 256 | ASTContext &Context, |
Sebastian Redl | 1d9f1fe | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 257 | void *DeserializationListener, |
| 258 | bool Preamble) { |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 259 | llvm::OwningPtr<ASTReader> Reader; |
| 260 | Reader.reset(new ASTReader(PP, &Context, |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 261 | Sysroot.empty() ? 0 : Sysroot.c_str(), |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 262 | DisablePCHValidation, DisableStatCache)); |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 263 | |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 264 | Reader->setDeserializationListener( |
Sebastian Redl | 571db7f | 2010-08-18 23:56:56 +0000 | [diff] [blame] | 265 | static_cast<ASTDeserializationListener *>(DeserializationListener)); |
Sebastian Redl | 1d9f1fe | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 266 | switch (Reader->ReadAST(Path, |
| 267 | Preamble ? ASTReader::Preamble : ASTReader::PCH)) { |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 268 | case ASTReader::Success: |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 269 | // Set the predefines buffer as suggested by the PCH reader. Typically, the |
| 270 | // predefines buffer will be empty. |
| 271 | PP.setPredefines(Reader->getSuggestedPredefines()); |
| 272 | return Reader.take(); |
| 273 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 274 | case ASTReader::Failure: |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 275 | // Unrecoverable failure: don't even try to process the input file. |
| 276 | break; |
| 277 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 278 | case ASTReader::IgnorePCH: |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 279 | // No suitable PCH file could be found. Return an error. |
| 280 | break; |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | } |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 285 | |
| 286 | // Code Completion |
| 287 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 288 | static bool EnableCodeCompletion(Preprocessor &PP, |
| 289 | const std::string &Filename, |
| 290 | unsigned Line, |
| 291 | unsigned Column) { |
| 292 | // Tell the source manager to chop off the given file at a specific |
| 293 | // line and column. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 294 | const FileEntry *Entry = PP.getFileManager().getFile(Filename); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 295 | if (!Entry) { |
| 296 | PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) |
| 297 | << Filename; |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | // Truncate the named file at the given line/column. |
| 302 | PP.SetCodeCompletionPoint(Entry, Line, Column); |
| 303 | return false; |
| 304 | } |
| 305 | |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 306 | void CompilerInstance::createCodeCompletionConsumer() { |
| 307 | const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 308 | if (!CompletionConsumer) { |
| 309 | CompletionConsumer.reset( |
| 310 | createCodeCompletionConsumer(getPreprocessor(), |
| 311 | Loc.FileName, Loc.Line, Loc.Column, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 312 | getFrontendOpts().ShowMacrosInCodeCompletion, |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 313 | getFrontendOpts().ShowCodePatternsInCodeCompletion, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 314 | getFrontendOpts().ShowGlobalSymbolsInCodeCompletion, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 315 | llvm::outs())); |
| 316 | if (!CompletionConsumer) |
| 317 | return; |
| 318 | } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName, |
| 319 | Loc.Line, Loc.Column)) { |
| 320 | CompletionConsumer.reset(); |
Douglas Gregor | c3d43b7 | 2010-03-16 06:04:47 +0000 | [diff] [blame] | 321 | return; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 322 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 323 | |
| 324 | if (CompletionConsumer->isOutputBinary() && |
| 325 | llvm::sys::Program::ChangeStdoutToBinary()) { |
| 326 | getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary); |
| 327 | CompletionConsumer.reset(); |
| 328 | } |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Kovarththanan Rajaratnam | f79bafa | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 331 | void CompilerInstance::createFrontendTimer() { |
| 332 | FrontendTimer.reset(new llvm::Timer("Clang front-end timer")); |
| 333 | } |
| 334 | |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 335 | CodeCompleteConsumer * |
| 336 | CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP, |
| 337 | const std::string &Filename, |
| 338 | unsigned Line, |
| 339 | unsigned Column, |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 340 | bool ShowMacros, |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 341 | bool ShowCodePatterns, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 342 | bool ShowGlobals, |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 343 | llvm::raw_ostream &OS) { |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 344 | if (EnableCodeCompletion(PP, Filename, Line, Column)) |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 345 | return 0; |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 346 | |
| 347 | // Set up the creation routine for code-completion. |
Douglas Gregor | a9f4f62 | 2010-10-11 22:12:15 +0000 | [diff] [blame] | 348 | return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 349 | ShowGlobals, OS); |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 350 | } |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 351 | |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 352 | void CompilerInstance::createSema(bool CompleteTranslationUnit, |
| 353 | CodeCompleteConsumer *CompletionConsumer) { |
| 354 | TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(), |
| 355 | CompleteTranslationUnit, CompletionConsumer)); |
| 356 | } |
| 357 | |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 358 | // Output Files |
| 359 | |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 360 | void CompilerInstance::addOutputFile(const OutputFile &OutFile) { |
| 361 | assert(OutFile.OS && "Attempt to add empty stream to output list!"); |
| 362 | OutputFiles.push_back(OutFile); |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Kovarththanan Rajaratnam | e51dd7b | 2010-03-06 12:07:48 +0000 | [diff] [blame] | 365 | void CompilerInstance::clearOutputFiles(bool EraseFiles) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 366 | for (std::list<OutputFile>::iterator |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 367 | it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 368 | delete it->OS; |
| 369 | if (!it->TempFilename.empty()) { |
| 370 | llvm::sys::Path TempPath(it->TempFilename); |
| 371 | if (EraseFiles) |
| 372 | TempPath.eraseFromDisk(); |
| 373 | else { |
| 374 | std::string Error; |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 375 | llvm::sys::Path NewOutFile(it->Filename); |
| 376 | // If '-working-directory' was passed, the output filename should be |
| 377 | // relative to that. |
| 378 | FileManager::FixupRelativePath(NewOutFile, getFileSystemOpts()); |
| 379 | if (TempPath.renamePathOnDisk(NewOutFile, &Error)) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 380 | getDiagnostics().Report(diag::err_fe_unable_to_rename_temp) |
| 381 | << it->TempFilename << it->Filename << Error; |
| 382 | TempPath.eraseFromDisk(); |
| 383 | } |
| 384 | } |
| 385 | } else if (!it->Filename.empty() && EraseFiles) |
| 386 | llvm::sys::Path(it->Filename).eraseFromDisk(); |
| 387 | |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 388 | } |
| 389 | OutputFiles.clear(); |
| 390 | } |
| 391 | |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 392 | llvm::raw_fd_ostream * |
| 393 | CompilerInstance::createDefaultOutputFile(bool Binary, |
| 394 | llvm::StringRef InFile, |
| 395 | llvm::StringRef Extension) { |
| 396 | return createOutputFile(getFrontendOpts().OutputFile, Binary, |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 397 | /*RemoveFileOnSignal=*/true, InFile, Extension); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | llvm::raw_fd_ostream * |
| 401 | CompilerInstance::createOutputFile(llvm::StringRef OutputPath, |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 402 | bool Binary, bool RemoveFileOnSignal, |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 403 | llvm::StringRef InFile, |
| 404 | llvm::StringRef Extension) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 405 | std::string Error, OutputPathName, TempPathName; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 406 | llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary, |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 407 | RemoveFileOnSignal, |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 408 | InFile, Extension, |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 409 | &OutputPathName, |
| 410 | &TempPathName); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 411 | if (!OS) { |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 412 | getDiagnostics().Report(diag::err_fe_unable_to_open_output) |
| 413 | << OutputPath << Error; |
| 414 | return 0; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | // Add the output file -- but don't try to remove "-", since this means we are |
| 418 | // using stdin. |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 419 | addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "", |
| 420 | TempPathName, OS)); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 421 | |
| 422 | return OS; |
| 423 | } |
| 424 | |
| 425 | llvm::raw_fd_ostream * |
| 426 | CompilerInstance::createOutputFile(llvm::StringRef OutputPath, |
| 427 | std::string &Error, |
| 428 | bool Binary, |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 429 | bool RemoveFileOnSignal, |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 430 | llvm::StringRef InFile, |
| 431 | llvm::StringRef Extension, |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 432 | std::string *ResultPathName, |
| 433 | std::string *TempPathName) { |
| 434 | std::string OutFile, TempFile; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 435 | if (!OutputPath.empty()) { |
| 436 | OutFile = OutputPath; |
| 437 | } else if (InFile == "-") { |
| 438 | OutFile = "-"; |
| 439 | } else if (!Extension.empty()) { |
| 440 | llvm::sys::Path Path(InFile); |
| 441 | Path.eraseSuffix(); |
| 442 | Path.appendSuffix(Extension); |
| 443 | OutFile = Path.str(); |
| 444 | } else { |
| 445 | OutFile = "-"; |
| 446 | } |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 447 | |
| 448 | if (OutFile != "-") { |
| 449 | llvm::sys::Path OutPath(OutFile); |
| 450 | // Only create the temporary if we can actually write to OutPath, otherwise |
| 451 | // we want to fail early. |
Michael J. Spencer | 32bef4e | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 452 | bool Exists; |
| 453 | if ((llvm::sys::fs::exists(OutPath.str(), Exists) || !Exists) || |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 454 | (OutPath.isRegularFile() && OutPath.canWrite())) { |
| 455 | // Create a temporary file. |
| 456 | llvm::sys::Path TempPath(OutFile); |
| 457 | if (!TempPath.createTemporaryFileOnDisk()) |
| 458 | TempFile = TempPath.str(); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | std::string OSFile = OutFile; |
| 463 | if (!TempFile.empty()) |
| 464 | OSFile = TempFile; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 465 | |
Daniel Dunbar | fc97102 | 2009-11-20 22:32:38 +0000 | [diff] [blame] | 466 | llvm::OwningPtr<llvm::raw_fd_ostream> OS( |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 467 | new llvm::raw_fd_ostream(OSFile.c_str(), Error, |
Daniel Dunbar | fc97102 | 2009-11-20 22:32:38 +0000 | [diff] [blame] | 468 | (Binary ? llvm::raw_fd_ostream::F_Binary : 0))); |
| 469 | if (!Error.empty()) |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 470 | return 0; |
| 471 | |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 472 | // Make sure the out stream file gets removed if we crash. |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 473 | if (RemoveFileOnSignal) |
| 474 | llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile)); |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 475 | |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 476 | if (ResultPathName) |
| 477 | *ResultPathName = OutFile; |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 478 | if (TempPathName) |
| 479 | *TempPathName = TempFile; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 480 | |
Daniel Dunbar | fc97102 | 2009-11-20 22:32:38 +0000 | [diff] [blame] | 481 | return OS.take(); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 482 | } |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 483 | |
| 484 | // Initialization Utilities |
| 485 | |
| 486 | bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile) { |
| 487 | return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(), |
| 488 | getSourceManager(), getFrontendOpts()); |
| 489 | } |
| 490 | |
| 491 | bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile, |
| 492 | Diagnostic &Diags, |
| 493 | FileManager &FileMgr, |
| 494 | SourceManager &SourceMgr, |
| 495 | const FrontendOptions &Opts) { |
Douglas Gregor | 414cb64 | 2010-11-30 05:23:00 +0000 | [diff] [blame] | 496 | // Figure out where to get and map in the main file, unless it's already |
| 497 | // been created (e.g., by a precompiled preamble). |
| 498 | if (!SourceMgr.getMainFileID().isInvalid()) { |
| 499 | // Do nothing: the main file has already been set. |
| 500 | } else if (InputFile != "-") { |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 501 | const FileEntry *File = FileMgr.getFile(InputFile); |
Dan Gohman | 694137c | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 502 | if (!File) { |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 503 | Diags.Report(diag::err_fe_error_reading) << InputFile; |
| 504 | return false; |
| 505 | } |
Dan Gohman | 694137c | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 506 | SourceMgr.createMainFileID(File); |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 507 | } else { |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 508 | llvm::OwningPtr<llvm::MemoryBuffer> SB; |
| 509 | if (llvm::MemoryBuffer::getSTDIN(SB)) { |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 510 | // FIXME: Give ec.message() in this diag. |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 511 | Diags.Report(diag::err_fe_error_reading_stdin); |
| 512 | return false; |
| 513 | } |
Dan Gohman | 90d9081 | 2010-10-26 23:21:25 +0000 | [diff] [blame] | 514 | const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(), |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 515 | SB->getBufferSize(), 0); |
Dan Gohman | 90d9081 | 2010-10-26 23:21:25 +0000 | [diff] [blame] | 516 | SourceMgr.createMainFileID(File); |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 517 | SourceMgr.overrideFileContents(File, SB.take()); |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Dan Gohman | 694137c | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 520 | assert(!SourceMgr.getMainFileID().isInvalid() && |
| 521 | "Couldn't establish MainFileID!"); |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 522 | return true; |
| 523 | } |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 524 | |
| 525 | // High-Level Operations |
| 526 | |
| 527 | bool CompilerInstance::ExecuteAction(FrontendAction &Act) { |
| 528 | assert(hasDiagnostics() && "Diagnostics engine is not initialized!"); |
| 529 | assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!"); |
| 530 | assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!"); |
| 531 | |
| 532 | // FIXME: Take this as an argument, once all the APIs we used have moved to |
| 533 | // taking it as an input instead of hard-coding llvm::errs. |
| 534 | llvm::raw_ostream &OS = llvm::errs(); |
| 535 | |
| 536 | // Create the target instance. |
| 537 | setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts())); |
| 538 | if (!hasTarget()) |
| 539 | return false; |
| 540 | |
| 541 | // Inform the target of the language options. |
| 542 | // |
| 543 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 544 | // created. This complexity should be lifted elsewhere. |
| 545 | getTarget().setForcedLangOptions(getLangOpts()); |
| 546 | |
| 547 | // Validate/process some options. |
| 548 | if (getHeaderSearchOpts().Verbose) |
| 549 | OS << "clang -cc1 version " CLANG_VERSION_STRING |
| 550 | << " based upon " << PACKAGE_STRING |
| 551 | << " hosted on " << llvm::sys::getHostTriple() << "\n"; |
| 552 | |
| 553 | if (getFrontendOpts().ShowTimers) |
| 554 | createFrontendTimer(); |
| 555 | |
Douglas Gregor | 95dd558 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 556 | if (getFrontendOpts().ShowStats) |
| 557 | llvm::EnableStatistics(); |
| 558 | |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 559 | for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) { |
| 560 | const std::string &InFile = getFrontendOpts().Inputs[i].second; |
| 561 | |
Daniel Dunbar | 2056048 | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 562 | // Reset the ID tables if we are reusing the SourceManager. |
| 563 | if (hasSourceManager()) |
| 564 | getSourceManager().clearIDTables(); |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 565 | |
Daniel Dunbar | d3598a6 | 2010-06-07 23:23:06 +0000 | [diff] [blame] | 566 | if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) { |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 567 | Act.Execute(); |
| 568 | Act.EndSourceFile(); |
| 569 | } |
| 570 | } |
| 571 | |
Chris Lattner | 53eee7b | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 572 | if (getDiagnosticOpts().ShowCarets) { |
Argyrios Kyrtzidis | f2224d8 | 2010-11-18 20:06:46 +0000 | [diff] [blame] | 573 | // We can have multiple diagnostics sharing one diagnostic client. |
| 574 | // Get the total number of warnings/errors from the client. |
| 575 | unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings(); |
| 576 | unsigned NumErrors = getDiagnostics().getClient()->getNumErrors(); |
Chris Lattner | 53eee7b | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 577 | |
| 578 | if (NumWarnings) |
| 579 | OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s"); |
| 580 | if (NumWarnings && NumErrors) |
| 581 | OS << " and "; |
| 582 | if (NumErrors) |
| 583 | OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s"); |
| 584 | if (NumWarnings || NumErrors) |
| 585 | OS << " generated.\n"; |
| 586 | } |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 587 | |
Daniel Dunbar | 2056048 | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 588 | if (getFrontendOpts().ShowStats && hasFileManager()) { |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 589 | getFileManager().PrintStats(); |
| 590 | OS << "\n"; |
| 591 | } |
| 592 | |
Argyrios Kyrtzidis | ab41b97 | 2010-11-18 21:13:57 +0000 | [diff] [blame] | 593 | return !getDiagnostics().getClient()->getNumErrors(); |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | |