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