Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 1 | //===--- CompilerInstance.cpp ---------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "clang/Frontend/CompilerInstance.h" |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 11 | #include "clang/Sema/Sema.h" |
Daniel Dunbar | 12ce694 | 2009-11-14 02:47:17 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | 5eb8100 | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Diagnostic.h" |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Version.h" |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 19 | #include "clang/Lex/HeaderSearch.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Lex/PTHManager.h" |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/ChainedDiagnosticClient.h" |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/FrontendAction.h" |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/LogDiagnosticPrinter.h" |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/VerifyDiagnosticsClient.h" |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/Utils.h" |
Sebastian Redl | 6ab7cd8 | 2010-08-18 23:57:17 +0000 | [diff] [blame] | 29 | #include "clang/Serialization/ASTReader.h" |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 30 | #include "clang/Sema/CodeCompleteConsumer.h" |
Michael J. Spencer | 32bef4e | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 31 | #include "llvm/Support/FileSystem.h" |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 95dd558 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/Statistic.h" |
Kovarththanan Rajaratnam | f79bafa | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Timer.h" |
Michael J. Spencer | 03013fa | 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 | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 40 | #include "llvm/Support/system_error.h" |
Dylan Noblesmith | 1626601 | 2011-06-23 12:20:57 +0000 | [diff] [blame] | 41 | #include "llvm/Config/config.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 42 | using namespace clang; |
| 43 | |
Daniel Dunbar | 42e9f8e4 | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 44 | CompilerInstance::CompilerInstance() |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 45 | : Invocation(new CompilerInvocation()), ModuleManager(0) { |
Daniel Dunbar | 6228ca0 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 46 | } |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 47 | |
| 48 | CompilerInstance::~CompilerInstance() { |
Daniel Dunbar | 42e9f8e4 | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Daniel Dunbar | 6228ca0 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 51 | void CompilerInstance::setInvocation(CompilerInvocation *Value) { |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 52 | Invocation = Value; |
Daniel Dunbar | 6228ca0 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 55 | void CompilerInstance::setDiagnostics(Diagnostic *Value) { |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 56 | Diagnostics = Value; |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 59 | void CompilerInstance::setTarget(TargetInfo *Value) { |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 60 | Target = Value; |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void CompilerInstance::setFileManager(FileManager *Value) { |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 64 | FileMgr = Value; |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 67 | void CompilerInstance::setSourceManager(SourceManager *Value) { |
| 68 | SourceMgr = Value; |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 71 | void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; } |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 72 | |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 73 | void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; } |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 75 | void CompilerInstance::setSema(Sema *S) { |
| 76 | TheSema.reset(S); |
| 77 | } |
| 78 | |
Daniel Dunbar | 12ce694 | 2009-11-14 02:47:17 +0000 | [diff] [blame] | 79 | void CompilerInstance::setASTConsumer(ASTConsumer *Value) { |
| 80 | Consumer.reset(Value); |
| 81 | } |
| 82 | |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 83 | void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) { |
| 84 | CompletionConsumer.reset(Value); |
| 85 | } |
| 86 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 87 | // Diagnostics |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 88 | static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts, |
Axel Naumann | 7d0c4cc | 2010-10-11 09:13:46 +0000 | [diff] [blame] | 89 | unsigned argc, const char* const *argv, |
Kovarththanan Rajaratnam | 3d67b1e | 2010-03-17 09:24:48 +0000 | [diff] [blame] | 90 | Diagnostic &Diags) { |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 91 | std::string ErrorInfo; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 92 | llvm::OwningPtr<raw_ostream> OS( |
Kovarththanan Rajaratnam | 6924713 | 2010-03-17 09:47:30 +0000 | [diff] [blame] | 93 | new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo)); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 94 | if (!ErrorInfo.empty()) { |
Kovarththanan Rajaratnam | 3d67b1e | 2010-03-17 09:24:48 +0000 | [diff] [blame] | 95 | Diags.Report(diag::err_fe_unable_to_open_logfile) |
| 96 | << DiagOpts.DumpBuildInformation << ErrorInfo; |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
Daniel Dunbar | dd63b28 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 100 | (*OS) << "clang -cc1 command line arguments: "; |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 101 | for (unsigned i = 0; i != argc; ++i) |
| 102 | (*OS) << argv[i] << ' '; |
| 103 | (*OS) << '\n'; |
| 104 | |
| 105 | // Chain in a diagnostic client which will log the diagnostics. |
| 106 | DiagnosticClient *Logger = |
Kovarththanan Rajaratnam | 6924713 | 2010-03-17 09:47:30 +0000 | [diff] [blame] | 107 | new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true); |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 108 | Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger)); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 111 | static void SetUpDiagnosticLog(const DiagnosticOptions &DiagOpts, |
Daniel Dunbar | b6534bb | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 112 | const CodeGenOptions *CodeGenOpts, |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 113 | Diagnostic &Diags) { |
| 114 | std::string ErrorInfo; |
| 115 | bool OwnsStream = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 116 | raw_ostream *OS = &llvm::errs(); |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 117 | if (DiagOpts.DiagnosticLogFile != "-") { |
| 118 | // Create the output stream. |
| 119 | llvm::raw_fd_ostream *FileOS( |
| 120 | new llvm::raw_fd_ostream(DiagOpts.DiagnosticLogFile.c_str(), |
Daniel Dunbar | e01eceb | 2011-04-07 20:19:21 +0000 | [diff] [blame] | 121 | ErrorInfo, llvm::raw_fd_ostream::F_Append)); |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 122 | if (!ErrorInfo.empty()) { |
| 123 | Diags.Report(diag::warn_fe_cc_log_diagnostics_failure) |
| 124 | << DiagOpts.DumpBuildInformation << ErrorInfo; |
| 125 | } else { |
| 126 | FileOS->SetUnbuffered(); |
| 127 | FileOS->SetUseAtomicWrites(true); |
| 128 | OS = FileOS; |
| 129 | OwnsStream = true; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Chain in the diagnostic client which will log the diagnostics. |
Daniel Dunbar | b6534bb | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 134 | LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts, |
| 135 | OwnsStream); |
| 136 | if (CodeGenOpts) |
| 137 | Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags); |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 138 | Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger)); |
| 139 | } |
| 140 | |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 141 | void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv, |
| 142 | DiagnosticClient *Client) { |
Daniel Dunbar | b6534bb | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 143 | Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client, |
| 144 | &getCodeGenOpts()); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 147 | llvm::IntrusiveRefCntPtr<Diagnostic> |
| 148 | CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts, |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 149 | int Argc, const char* const *Argv, |
Daniel Dunbar | b6534bb | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 150 | DiagnosticClient *Client, |
| 151 | const CodeGenOptions *CodeGenOpts) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 152 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 153 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic(DiagID)); |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 154 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 155 | // Create the diagnostic client for reporting errors or for |
| 156 | // implementing -verify. |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 157 | if (Client) |
| 158 | Diags->setClient(Client); |
| 159 | else |
| 160 | Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts)); |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 161 | |
| 162 | // Chain in -verify checker, if requested. |
| 163 | if (Opts.VerifyDiagnostics) |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 164 | Diags->setClient(new VerifyDiagnosticsClient(*Diags, Diags->takeClient())); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 165 | |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 166 | // Chain in -diagnostic-log-file dumper, if requested. |
| 167 | if (!Opts.DiagnosticLogFile.empty()) |
Daniel Dunbar | b6534bb | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 168 | SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags); |
Daniel Dunbar | 9df2349 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 169 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 170 | if (!Opts.DumpBuildInformation.empty()) |
Kovarththanan Rajaratnam | 3d67b1e | 2010-03-17 09:24:48 +0000 | [diff] [blame] | 171 | SetUpBuildDumpLog(Opts, Argc, Argv, *Diags); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 172 | |
| 173 | // Configure our handling of diagnostics. |
Kovarththanan Rajaratnam | 5bf932b | 2010-03-17 09:36:02 +0000 | [diff] [blame] | 174 | ProcessWarningOptions(*Diags, Opts); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 175 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 176 | return Diags; |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // File Manager |
| 180 | |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 181 | void CompilerInstance::createFileManager() { |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 182 | FileMgr = new FileManager(getFileSystemOpts()); |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 185 | // Source Manager |
| 186 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 187 | void CompilerInstance::createSourceManager(FileManager &FileMgr) { |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 188 | SourceMgr = new SourceManager(getDiagnostics(), FileMgr); |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 189 | } |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 190 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 191 | // Preprocessor |
| 192 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 193 | void CompilerInstance::createPreprocessor() { |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame^] | 194 | const PreprocessorOptions &PPOpts = getPreprocessorOpts(); |
| 195 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 196 | // Create a PTH manager if we are using some form of a token cache. |
| 197 | PTHManager *PTHMgr = 0; |
Daniel Dunbar | 049d3a0 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 198 | if (!PPOpts.TokenCache.empty()) |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame^] | 199 | PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics()); |
| 200 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 201 | // Create the Preprocessor. |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame^] | 202 | HeaderSearch *HeaderInfo = new HeaderSearch(getFileManager()); |
| 203 | PP = new Preprocessor(getDiagnostics(), getLangOpts(), getTarget(), |
| 204 | getSourceManager(), *HeaderInfo, *this, PTHMgr, |
| 205 | /*OwnsHeaderSearch=*/true); |
| 206 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 207 | // Note that this is different then passing PTHMgr to Preprocessor's ctor. |
| 208 | // That argument is used as the IdentifierInfoLookup argument to |
| 209 | // IdentifierTable's ctor. |
| 210 | if (PTHMgr) { |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame^] | 211 | PTHMgr->setPreprocessor(&*PP); |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 212 | PP->setPTHManager(PTHMgr); |
| 213 | } |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame^] | 214 | |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 215 | if (PPOpts.DetailedRecord) |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 216 | PP->createPreprocessingRecord( |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame^] | 217 | PPOpts.DetailedRecordIncludesNestedMacroExpansions); |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 218 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame^] | 219 | InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts()); |
| 220 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 221 | // Handle generating dependencies, if requested. |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame^] | 222 | const DependencyOutputOptions &DepOpts = getDependencyOutputOpts(); |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 223 | if (!DepOpts.OutputFile.empty()) |
| 224 | AttachDependencyFileGen(*PP, DepOpts); |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame^] | 225 | |
Daniel Dunbar | eef63e0 | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 226 | // Handle generating header include information, if requested. |
| 227 | if (DepOpts.ShowHeaderIncludes) |
| 228 | AttachHeaderIncludeGen(*PP); |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 229 | if (!DepOpts.HeaderIncludeOutputFile.empty()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 230 | StringRef OutputPath = DepOpts.HeaderIncludeOutputFile; |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 231 | if (OutputPath == "-") |
| 232 | OutputPath = ""; |
Daniel Dunbar | da60885 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 233 | AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath, |
| 234 | /*ShowDepth=*/false); |
Daniel Dunbar | b34d69b | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 235 | } |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 236 | } |
Daniel Dunbar | 5eb8100 | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 237 | |
| 238 | // ASTContext |
| 239 | |
| 240 | void CompilerInstance::createASTContext() { |
| 241 | Preprocessor &PP = getPreprocessor(); |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 242 | Context = new ASTContext(getLangOpts(), PP.getSourceManager(), |
| 243 | getTarget(), PP.getIdentifierTable(), |
| 244 | PP.getSelectorTable(), PP.getBuiltinInfo(), |
| 245 | /*size_reserve=*/ 0); |
Daniel Dunbar | 5eb8100 | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 246 | } |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 247 | |
| 248 | // ExternalASTSource |
| 249 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 250 | void CompilerInstance::createPCHExternalASTSource(StringRef Path, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 251 | bool DisablePCHValidation, |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 252 | bool DisableStatCache, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 253 | void *DeserializationListener){ |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 254 | llvm::OwningPtr<ExternalASTSource> Source; |
Sebastian Redl | 1d9f1fe | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 255 | bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 256 | Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot, |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 257 | DisablePCHValidation, |
| 258 | DisableStatCache, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 259 | getPreprocessor(), getASTContext(), |
Sebastian Redl | 1d9f1fe | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 260 | DeserializationListener, |
| 261 | Preamble)); |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 262 | ModuleManager = static_cast<ASTReader*>(Source.get()); |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 263 | getASTContext().setExternalSource(Source); |
| 264 | } |
| 265 | |
| 266 | ExternalASTSource * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 267 | CompilerInstance::createPCHExternalASTSource(StringRef Path, |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 268 | const std::string &Sysroot, |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 269 | bool DisablePCHValidation, |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 270 | bool DisableStatCache, |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 271 | Preprocessor &PP, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 272 | ASTContext &Context, |
Sebastian Redl | 1d9f1fe | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 273 | void *DeserializationListener, |
| 274 | bool Preamble) { |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 275 | llvm::OwningPtr<ASTReader> Reader; |
| 276 | Reader.reset(new ASTReader(PP, &Context, |
Douglas Gregor | 832d620 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 277 | Sysroot.empty() ? "" : Sysroot.c_str(), |
Douglas Gregor | 8ef6c8c | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 278 | DisablePCHValidation, DisableStatCache)); |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 279 | |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 280 | Reader->setDeserializationListener( |
Sebastian Redl | 571db7f | 2010-08-18 23:56:56 +0000 | [diff] [blame] | 281 | static_cast<ASTDeserializationListener *>(DeserializationListener)); |
Douglas Gregor | 72a9ae1 | 2011-07-22 16:00:58 +0000 | [diff] [blame] | 282 | switch (Reader->ReadAST(Path, |
| 283 | Preamble ? serialization::MK_Preamble |
| 284 | : serialization::MK_PCH)) { |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 285 | case ASTReader::Success: |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 286 | // Set the predefines buffer as suggested by the PCH reader. Typically, the |
| 287 | // predefines buffer will be empty. |
| 288 | PP.setPredefines(Reader->getSuggestedPredefines()); |
| 289 | return Reader.take(); |
| 290 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 291 | case ASTReader::Failure: |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 292 | // Unrecoverable failure: don't even try to process the input file. |
| 293 | break; |
| 294 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 295 | case ASTReader::IgnorePCH: |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 296 | // No suitable PCH file could be found. Return an error. |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 302 | |
| 303 | // Code Completion |
| 304 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 305 | static bool EnableCodeCompletion(Preprocessor &PP, |
| 306 | const std::string &Filename, |
| 307 | unsigned Line, |
| 308 | unsigned Column) { |
| 309 | // Tell the source manager to chop off the given file at a specific |
| 310 | // line and column. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 311 | const FileEntry *Entry = PP.getFileManager().getFile(Filename); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 312 | if (!Entry) { |
| 313 | PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) |
| 314 | << Filename; |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | // Truncate the named file at the given line/column. |
| 319 | PP.SetCodeCompletionPoint(Entry, Line, Column); |
| 320 | return false; |
| 321 | } |
| 322 | |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 323 | void CompilerInstance::createCodeCompletionConsumer() { |
| 324 | const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 325 | if (!CompletionConsumer) { |
| 326 | CompletionConsumer.reset( |
| 327 | createCodeCompletionConsumer(getPreprocessor(), |
| 328 | Loc.FileName, Loc.Line, Loc.Column, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 329 | getFrontendOpts().ShowMacrosInCodeCompletion, |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 330 | getFrontendOpts().ShowCodePatternsInCodeCompletion, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 331 | getFrontendOpts().ShowGlobalSymbolsInCodeCompletion, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 332 | llvm::outs())); |
| 333 | if (!CompletionConsumer) |
| 334 | return; |
| 335 | } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName, |
| 336 | Loc.Line, Loc.Column)) { |
| 337 | CompletionConsumer.reset(); |
Douglas Gregor | c3d43b7 | 2010-03-16 06:04:47 +0000 | [diff] [blame] | 338 | return; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 339 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 340 | |
| 341 | if (CompletionConsumer->isOutputBinary() && |
| 342 | llvm::sys::Program::ChangeStdoutToBinary()) { |
| 343 | getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary); |
| 344 | CompletionConsumer.reset(); |
| 345 | } |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Kovarththanan Rajaratnam | f79bafa | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 348 | void CompilerInstance::createFrontendTimer() { |
| 349 | FrontendTimer.reset(new llvm::Timer("Clang front-end timer")); |
| 350 | } |
| 351 | |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 352 | CodeCompleteConsumer * |
| 353 | CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP, |
| 354 | const std::string &Filename, |
| 355 | unsigned Line, |
| 356 | unsigned Column, |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 357 | bool ShowMacros, |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 358 | bool ShowCodePatterns, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 359 | bool ShowGlobals, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 360 | raw_ostream &OS) { |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 361 | if (EnableCodeCompletion(PP, Filename, Line, Column)) |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 362 | return 0; |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 363 | |
| 364 | // Set up the creation routine for code-completion. |
Douglas Gregor | a9f4f62 | 2010-10-11 22:12:15 +0000 | [diff] [blame] | 365 | return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 366 | ShowGlobals, OS); |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 367 | } |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 368 | |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 369 | void CompilerInstance::createSema(TranslationUnitKind TUKind, |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 370 | CodeCompleteConsumer *CompletionConsumer) { |
| 371 | TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(), |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 372 | TUKind, CompletionConsumer)); |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 375 | // Output Files |
| 376 | |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 377 | void CompilerInstance::addOutputFile(const OutputFile &OutFile) { |
| 378 | assert(OutFile.OS && "Attempt to add empty stream to output list!"); |
| 379 | OutputFiles.push_back(OutFile); |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Kovarththanan Rajaratnam | e51dd7b | 2010-03-06 12:07:48 +0000 | [diff] [blame] | 382 | void CompilerInstance::clearOutputFiles(bool EraseFiles) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 383 | for (std::list<OutputFile>::iterator |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 384 | it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 385 | delete it->OS; |
| 386 | if (!it->TempFilename.empty()) { |
Anders Carlsson | af036a6 | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 387 | if (EraseFiles) { |
| 388 | bool existed; |
| 389 | llvm::sys::fs::remove(it->TempFilename, existed); |
| 390 | } else { |
| 391 | llvm::SmallString<128> NewOutFile(it->Filename); |
| 392 | |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 393 | // If '-working-directory' was passed, the output filename should be |
| 394 | // relative to that. |
Anders Carlsson | 2e2468e | 2011-03-14 01:13:54 +0000 | [diff] [blame] | 395 | FileMgr->FixupRelativePath(NewOutFile); |
Anders Carlsson | af036a6 | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 396 | if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename, |
| 397 | NewOutFile.str())) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 398 | getDiagnostics().Report(diag::err_fe_unable_to_rename_temp) |
Anders Carlsson | af036a6 | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 399 | << it->TempFilename << it->Filename << ec.message(); |
| 400 | |
| 401 | bool existed; |
| 402 | llvm::sys::fs::remove(it->TempFilename, existed); |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | } else if (!it->Filename.empty() && EraseFiles) |
| 406 | llvm::sys::Path(it->Filename).eraseFromDisk(); |
| 407 | |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 408 | } |
| 409 | OutputFiles.clear(); |
| 410 | } |
| 411 | |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 412 | llvm::raw_fd_ostream * |
| 413 | CompilerInstance::createDefaultOutputFile(bool Binary, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 414 | StringRef InFile, |
| 415 | StringRef Extension) { |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 416 | return createOutputFile(getFrontendOpts().OutputFile, Binary, |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 417 | /*RemoveFileOnSignal=*/true, InFile, Extension); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | llvm::raw_fd_ostream * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 421 | CompilerInstance::createOutputFile(StringRef OutputPath, |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 422 | bool Binary, bool RemoveFileOnSignal, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 423 | StringRef InFile, |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 424 | StringRef Extension, |
| 425 | bool UseTemporary) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 426 | std::string Error, OutputPathName, TempPathName; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 427 | llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary, |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 428 | RemoveFileOnSignal, |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 429 | InFile, Extension, |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 430 | UseTemporary, |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 431 | &OutputPathName, |
| 432 | &TempPathName); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 433 | if (!OS) { |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 434 | getDiagnostics().Report(diag::err_fe_unable_to_open_output) |
| 435 | << OutputPath << Error; |
| 436 | return 0; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | // Add the output file -- but don't try to remove "-", since this means we are |
| 440 | // using stdin. |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 441 | addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "", |
| 442 | TempPathName, OS)); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 443 | |
| 444 | return OS; |
| 445 | } |
| 446 | |
| 447 | llvm::raw_fd_ostream * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 448 | CompilerInstance::createOutputFile(StringRef OutputPath, |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 449 | std::string &Error, |
| 450 | bool Binary, |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 451 | bool RemoveFileOnSignal, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 452 | StringRef InFile, |
| 453 | StringRef Extension, |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 454 | bool UseTemporary, |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 455 | std::string *ResultPathName, |
| 456 | std::string *TempPathName) { |
| 457 | std::string OutFile, TempFile; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 458 | if (!OutputPath.empty()) { |
| 459 | OutFile = OutputPath; |
| 460 | } else if (InFile == "-") { |
| 461 | OutFile = "-"; |
| 462 | } else if (!Extension.empty()) { |
| 463 | llvm::sys::Path Path(InFile); |
| 464 | Path.eraseSuffix(); |
| 465 | Path.appendSuffix(Extension); |
| 466 | OutFile = Path.str(); |
| 467 | } else { |
| 468 | OutFile = "-"; |
| 469 | } |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 470 | |
| 471 | llvm::OwningPtr<llvm::raw_fd_ostream> OS; |
| 472 | std::string OSFile; |
| 473 | |
| 474 | if (UseTemporary && OutFile != "-") { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 475 | llvm::sys::Path OutPath(OutFile); |
| 476 | // Only create the temporary if we can actually write to OutPath, otherwise |
| 477 | // we want to fail early. |
Michael J. Spencer | 32bef4e | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 478 | bool Exists; |
| 479 | if ((llvm::sys::fs::exists(OutPath.str(), Exists) || !Exists) || |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 480 | (OutPath.isRegularFile() && OutPath.canWrite())) { |
| 481 | // Create a temporary file. |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 482 | llvm::SmallString<128> TempPath; |
| 483 | TempPath = OutFile; |
| 484 | TempPath += "-%%%%%%%%"; |
| 485 | int fd; |
| 486 | if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath, |
| 487 | /*makeAbsolute=*/false) == llvm::errc::success) { |
| 488 | OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true)); |
| 489 | OSFile = TempFile = TempPath.str(); |
| 490 | } |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 494 | if (!OS) { |
| 495 | OSFile = OutFile; |
| 496 | OS.reset( |
| 497 | new llvm::raw_fd_ostream(OSFile.c_str(), Error, |
| 498 | (Binary ? llvm::raw_fd_ostream::F_Binary : 0))); |
| 499 | if (!Error.empty()) |
| 500 | return 0; |
| 501 | } |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 502 | |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 503 | // Make sure the out stream file gets removed if we crash. |
Daniel Dunbar | ff9cd96 | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 504 | if (RemoveFileOnSignal) |
| 505 | llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile)); |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 506 | |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 507 | if (ResultPathName) |
| 508 | *ResultPathName = OutFile; |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 509 | if (TempPathName) |
| 510 | *TempPathName = TempFile; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 511 | |
Daniel Dunbar | fc97102 | 2009-11-20 22:32:38 +0000 | [diff] [blame] | 512 | return OS.take(); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 513 | } |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 514 | |
| 515 | // Initialization Utilities |
| 516 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 517 | bool CompilerInstance::InitializeSourceManager(StringRef InputFile) { |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 518 | return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(), |
| 519 | getSourceManager(), getFrontendOpts()); |
| 520 | } |
| 521 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 522 | bool CompilerInstance::InitializeSourceManager(StringRef InputFile, |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 523 | Diagnostic &Diags, |
| 524 | FileManager &FileMgr, |
| 525 | SourceManager &SourceMgr, |
| 526 | const FrontendOptions &Opts) { |
Douglas Gregor | 414cb64 | 2010-11-30 05:23:00 +0000 | [diff] [blame] | 527 | // Figure out where to get and map in the main file, unless it's already |
| 528 | // been created (e.g., by a precompiled preamble). |
| 529 | if (!SourceMgr.getMainFileID().isInvalid()) { |
| 530 | // Do nothing: the main file has already been set. |
| 531 | } else if (InputFile != "-") { |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 532 | const FileEntry *File = FileMgr.getFile(InputFile); |
Dan Gohman | 694137c | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 533 | if (!File) { |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 534 | Diags.Report(diag::err_fe_error_reading) << InputFile; |
| 535 | return false; |
| 536 | } |
Dan Gohman | 694137c | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 537 | SourceMgr.createMainFileID(File); |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 538 | } else { |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 539 | llvm::OwningPtr<llvm::MemoryBuffer> SB; |
| 540 | if (llvm::MemoryBuffer::getSTDIN(SB)) { |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 541 | // FIXME: Give ec.message() in this diag. |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 542 | Diags.Report(diag::err_fe_error_reading_stdin); |
| 543 | return false; |
| 544 | } |
Dan Gohman | 90d9081 | 2010-10-26 23:21:25 +0000 | [diff] [blame] | 545 | const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(), |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 546 | SB->getBufferSize(), 0); |
Dan Gohman | 90d9081 | 2010-10-26 23:21:25 +0000 | [diff] [blame] | 547 | SourceMgr.createMainFileID(File); |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame] | 548 | SourceMgr.overrideFileContents(File, SB.take()); |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Dan Gohman | 694137c | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 551 | assert(!SourceMgr.getMainFileID().isInvalid() && |
| 552 | "Couldn't establish MainFileID!"); |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 553 | return true; |
| 554 | } |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 555 | |
| 556 | // High-Level Operations |
| 557 | |
| 558 | bool CompilerInstance::ExecuteAction(FrontendAction &Act) { |
| 559 | assert(hasDiagnostics() && "Diagnostics engine is not initialized!"); |
| 560 | assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!"); |
| 561 | assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!"); |
| 562 | |
| 563 | // FIXME: Take this as an argument, once all the APIs we used have moved to |
| 564 | // taking it as an input instead of hard-coding llvm::errs. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 565 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 566 | |
| 567 | // Create the target instance. |
| 568 | setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts())); |
| 569 | if (!hasTarget()) |
| 570 | return false; |
| 571 | |
| 572 | // Inform the target of the language options. |
| 573 | // |
| 574 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 575 | // created. This complexity should be lifted elsewhere. |
| 576 | getTarget().setForcedLangOptions(getLangOpts()); |
| 577 | |
| 578 | // Validate/process some options. |
| 579 | if (getHeaderSearchOpts().Verbose) |
| 580 | OS << "clang -cc1 version " CLANG_VERSION_STRING |
| 581 | << " based upon " << PACKAGE_STRING |
| 582 | << " hosted on " << llvm::sys::getHostTriple() << "\n"; |
| 583 | |
| 584 | if (getFrontendOpts().ShowTimers) |
| 585 | createFrontendTimer(); |
| 586 | |
Douglas Gregor | 95dd558 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 587 | if (getFrontendOpts().ShowStats) |
| 588 | llvm::EnableStatistics(); |
| 589 | |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 590 | for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) { |
| 591 | const std::string &InFile = getFrontendOpts().Inputs[i].second; |
| 592 | |
Daniel Dunbar | 2056048 | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 593 | // Reset the ID tables if we are reusing the SourceManager. |
| 594 | if (hasSourceManager()) |
| 595 | getSourceManager().clearIDTables(); |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 596 | |
Daniel Dunbar | d3598a6 | 2010-06-07 23:23:06 +0000 | [diff] [blame] | 597 | if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) { |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 598 | Act.Execute(); |
| 599 | Act.EndSourceFile(); |
| 600 | } |
| 601 | } |
| 602 | |
Chris Lattner | 53eee7b | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 603 | if (getDiagnosticOpts().ShowCarets) { |
Argyrios Kyrtzidis | f2224d8 | 2010-11-18 20:06:46 +0000 | [diff] [blame] | 604 | // We can have multiple diagnostics sharing one diagnostic client. |
| 605 | // Get the total number of warnings/errors from the client. |
| 606 | unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings(); |
| 607 | unsigned NumErrors = getDiagnostics().getClient()->getNumErrors(); |
Chris Lattner | 53eee7b | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 608 | |
| 609 | if (NumWarnings) |
| 610 | OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s"); |
| 611 | if (NumWarnings && NumErrors) |
| 612 | OS << " and "; |
| 613 | if (NumErrors) |
| 614 | OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s"); |
| 615 | if (NumWarnings || NumErrors) |
| 616 | OS << " generated.\n"; |
| 617 | } |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 618 | |
Daniel Dunbar | 2056048 | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 619 | if (getFrontendOpts().ShowStats && hasFileManager()) { |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 620 | getFileManager().PrintStats(); |
| 621 | OS << "\n"; |
| 622 | } |
| 623 | |
Argyrios Kyrtzidis | ab41b97 | 2010-11-18 21:13:57 +0000 | [diff] [blame] | 624 | return !getDiagnostics().getClient()->getNumErrors(); |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame^] | 627 | ModuleKey CompilerInstance::loadModule(SourceLocation ImportLoc, |
| 628 | IdentifierInfo &ModuleName, |
| 629 | SourceLocation ModuleNameLoc) { |
| 630 | // Determine what file we're searching from. |
| 631 | SourceManager &SourceMgr = getSourceManager(); |
| 632 | SourceLocation ExpandedImportLoc = SourceMgr.getExpansionLoc(ImportLoc); |
| 633 | const FileEntry *CurFile |
| 634 | = SourceMgr.getFileEntryForID(SourceMgr.getFileID(ExpandedImportLoc)); |
| 635 | if (!CurFile) |
| 636 | CurFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()); |
| 637 | |
| 638 | // Search for a module with the given name. |
| 639 | std::string Filename = ModuleName.getName().str(); |
| 640 | Filename += ".pcm"; |
| 641 | const DirectoryLookup *CurDir = 0; |
| 642 | const FileEntry *ModuleFile |
| 643 | = PP->getHeaderSearchInfo().LookupFile(Filename, /*isAngled=*/false, |
| 644 | /*FromDir=*/0, CurDir, CurFile, |
| 645 | /*SearchPath=*/0, |
| 646 | /*RelativePath=*/0); |
| 647 | if (!ModuleFile) { |
| 648 | getDiagnostics().Report(ModuleNameLoc, diag::warn_module_not_found) |
| 649 | << ModuleName.getName() |
| 650 | << SourceRange(ImportLoc, ModuleNameLoc); |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | // If we don't already have an ASTReader, create one now. |
| 655 | if (!ModuleManager) { |
| 656 | std::string Sysroot = getHeaderSearchOpts().Sysroot; |
| 657 | const PreprocessorOptions &PPOpts = getPreprocessorOpts(); |
| 658 | ModuleManager = new ASTReader(getPreprocessor(), &*Context, |
| 659 | Sysroot.empty() ? "" : Sysroot.c_str(), |
| 660 | PPOpts.DisablePCHValidation, |
| 661 | PPOpts.DisableStatCache); |
| 662 | ModuleManager->setDeserializationListener( |
| 663 | getASTConsumer().GetASTDeserializationListener()); |
| 664 | getASTContext().setASTMutationListener( |
| 665 | getASTConsumer().GetASTMutationListener()); |
| 666 | llvm::OwningPtr<ExternalASTSource> Source; |
| 667 | Source.reset(ModuleManager); |
| 668 | getASTContext().setExternalSource(Source); |
| 669 | ModuleManager->InitializeSema(getSema()); |
| 670 | } |
| 671 | |
| 672 | // Try to load the module we found. |
| 673 | switch (ModuleManager->ReadAST(ModuleFile->getName(), |
| 674 | serialization::MK_Module)) { |
| 675 | case ASTReader::Success: |
| 676 | break; |
| 677 | |
| 678 | case ASTReader::IgnorePCH: |
| 679 | // FIXME: The ASTReader will already have complained, but can we showhorn |
| 680 | // that diagnostic information into a more useful form? |
| 681 | return 0; |
| 682 | |
| 683 | case ASTReader::Failure: |
| 684 | // Already complained. |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | // FIXME: The module file's FileEntry makes a poor key indeed! |
| 689 | return (ModuleKey)ModuleFile; |
| 690 | } |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 691 | |