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