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