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