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