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