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