Argyrios Kyrtzidis | 3a08ec1 | 2009-06-20 08:27:14 +0000 | [diff] [blame] | 1 | //===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===// |
| 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 | // ASTUnit Implementation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTUnit.h" |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/PCHWriter.h" |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclVisitor.h" |
| 19 | #include "clang/AST/StmtVisitor.h" |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 20 | #include "clang/Driver/Compilation.h" |
| 21 | #include "clang/Driver/Driver.h" |
| 22 | #include "clang/Driver/Job.h" |
| 23 | #include "clang/Driver/Tool.h" |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/CompilerInstance.h" |
| 25 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/FrontendOptions.h" |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/PCHReader.h" |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 29 | #include "clang/Lex/HeaderSearch.h" |
| 30 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | b9bbd54 | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetOptions.h" |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 32 | #include "clang/Basic/TargetInfo.h" |
| 33 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 35 | #include "llvm/System/Host.h" |
Benjamin Kramer | 6c839f8 | 2009-10-18 11:34:14 +0000 | [diff] [blame] | 36 | #include "llvm/System/Path.h" |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Timer.h" |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 38 | #include <cstdlib> |
Zhongxing Xu | 318e403 | 2010-07-23 02:15:08 +0000 | [diff] [blame] | 39 | #include <cstdio> |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 40 | #include <sys/stat.h> |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 41 | using namespace clang; |
| 42 | |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 43 | ASTUnit::ASTUnit(bool _MainFileIsAST) |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 44 | : CaptureDiagnostics(false), MainFileIsAST(_MainFileIsAST), |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 45 | ConcurrencyCheckValue(CheckUnlocked), SavedMainFileBuffer(0) { |
| 46 | } |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 47 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 48 | ASTUnit::~ASTUnit() { |
Douglas Gregor | 0c7c2f8 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 49 | ConcurrencyCheckValue = CheckLocked; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 50 | CleanTemporaryFiles(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 51 | if (!PreambleFile.empty()) |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 52 | llvm::sys::Path(PreambleFile).eraseFromDisk(); |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 53 | |
| 54 | // Free the buffers associated with remapped files. We are required to |
| 55 | // perform this operation here because we explicitly request that the |
| 56 | // compiler instance *not* free these buffers for each invocation of the |
| 57 | // parser. |
| 58 | if (Invocation.get()) { |
| 59 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
| 60 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 61 | FB = PPOpts.remapped_file_buffer_begin(), |
| 62 | FBEnd = PPOpts.remapped_file_buffer_end(); |
| 63 | FB != FBEnd; |
| 64 | ++FB) |
| 65 | delete FB->second; |
| 66 | } |
Douglas Gregor | 96c0426 | 2010-07-27 14:52:07 +0000 | [diff] [blame] | 67 | |
| 68 | delete SavedMainFileBuffer; |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 69 | |
| 70 | for (unsigned I = 0, N = Timers.size(); I != N; ++I) |
| 71 | delete Timers[I]; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void ASTUnit::CleanTemporaryFiles() { |
Douglas Gregor | 6cb5ba4 | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 75 | for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) |
| 76 | TemporaryFiles[I].eraseFromDisk(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 77 | TemporaryFiles.clear(); |
Steve Naroff | 44cd60e | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 78 | } |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 79 | |
| 80 | namespace { |
| 81 | |
| 82 | /// \brief Gathers information from PCHReader that will be used to initialize |
| 83 | /// a Preprocessor. |
Benjamin Kramer | 16634c2 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 84 | class PCHInfoCollector : public PCHReaderListener { |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 85 | LangOptions &LangOpt; |
| 86 | HeaderSearch &HSI; |
| 87 | std::string &TargetTriple; |
| 88 | std::string &Predefines; |
| 89 | unsigned &Counter; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 91 | unsigned NumHeaderInfos; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 93 | public: |
| 94 | PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI, |
| 95 | std::string &TargetTriple, std::string &Predefines, |
| 96 | unsigned &Counter) |
| 97 | : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple), |
| 98 | Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 100 | virtual bool ReadLanguageOptions(const LangOptions &LangOpts) { |
| 101 | LangOpt = LangOpts; |
| 102 | return false; |
| 103 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Daniel Dunbar | 20a682d | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 105 | virtual bool ReadTargetTriple(llvm::StringRef Triple) { |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 106 | TargetTriple = Triple; |
| 107 | return false; |
| 108 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Sebastian Redl | 8b41f30 | 2010-07-14 23:29:55 +0000 | [diff] [blame] | 110 | virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, |
Daniel Dunbar | 000c4ff | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 111 | llvm::StringRef OriginalFileName, |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 112 | std::string &SuggestedPredefines) { |
Sebastian Redl | 8b41f30 | 2010-07-14 23:29:55 +0000 | [diff] [blame] | 113 | Predefines = Buffers[0].Data; |
| 114 | for (unsigned I = 1, N = Buffers.size(); I != N; ++I) { |
| 115 | Predefines += Buffers[I].Data; |
| 116 | } |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 117 | return false; |
| 118 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Douglas Gregor | a2f4945 | 2010-03-16 19:09:18 +0000 | [diff] [blame] | 120 | virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) { |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 121 | HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++); |
| 122 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 124 | virtual void ReadCounter(unsigned Value) { |
| 125 | Counter = Value; |
| 126 | } |
| 127 | }; |
| 128 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 129 | class StoredDiagnosticClient : public DiagnosticClient { |
| 130 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags; |
| 131 | |
| 132 | public: |
| 133 | explicit StoredDiagnosticClient( |
| 134 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags) |
| 135 | : StoredDiags(StoredDiags) { } |
| 136 | |
| 137 | virtual void HandleDiagnostic(Diagnostic::Level Level, |
| 138 | const DiagnosticInfo &Info); |
| 139 | }; |
| 140 | |
| 141 | /// \brief RAII object that optionally captures diagnostics, if |
| 142 | /// there is no diagnostic client to capture them already. |
| 143 | class CaptureDroppedDiagnostics { |
| 144 | Diagnostic &Diags; |
| 145 | StoredDiagnosticClient Client; |
| 146 | DiagnosticClient *PreviousClient; |
| 147 | |
| 148 | public: |
| 149 | CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags, |
| 150 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags) |
| 151 | : Diags(Diags), Client(StoredDiags), PreviousClient(Diags.getClient()) |
| 152 | { |
| 153 | if (RequestCapture || Diags.getClient() == 0) |
| 154 | Diags.setClient(&Client); |
| 155 | } |
| 156 | |
| 157 | ~CaptureDroppedDiagnostics() { |
| 158 | Diags.setClient(PreviousClient); |
| 159 | } |
| 160 | }; |
| 161 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 162 | } // anonymous namespace |
| 163 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 164 | void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level, |
| 165 | const DiagnosticInfo &Info) { |
| 166 | StoredDiags.push_back(StoredDiagnostic(Level, Info)); |
| 167 | } |
| 168 | |
Steve Naroff | c0683b9 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 169 | const std::string &ASTUnit::getOriginalSourceFileName() { |
Daniel Dunbar | a8a5093 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 170 | return OriginalSourceFile; |
Steve Naroff | c0683b9 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 171 | } |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 172 | |
Steve Naroff | 44cd60e | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 173 | const std::string &ASTUnit::getPCHFileName() { |
Daniel Dunbar | a18f958 | 2009-12-02 21:47:43 +0000 | [diff] [blame] | 174 | assert(isMainFileAST() && "Not an ASTUnit from a PCH file!"); |
Benjamin Kramer | 2ecf8eb | 2010-01-30 16:23:25 +0000 | [diff] [blame] | 175 | return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName(); |
Steve Naroff | 44cd60e | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 178 | ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 179 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
Ted Kremenek | 8bcb1c6 | 2009-10-17 00:34:24 +0000 | [diff] [blame] | 180 | bool OnlyLocalDecls, |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 181 | RemappedFile *RemappedFiles, |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 182 | unsigned NumRemappedFiles, |
| 183 | bool CaptureDiagnostics) { |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 184 | llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true)); |
| 185 | |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 186 | if (!Diags.getPtr()) { |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 187 | // No diagnostics engine was provided, so create our own diagnostics object |
| 188 | // with the default options. |
| 189 | DiagnosticOptions DiagOpts; |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 190 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 191 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 192 | |
| 193 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Douglas Gregor | 16bef85 | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 194 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 195 | AST->Diagnostics = Diags; |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 196 | AST->FileMgr.reset(new FileManager); |
| 197 | AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics())); |
Steve Naroff | 505fb84 | 2009-10-19 14:34:22 +0000 | [diff] [blame] | 198 | AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager())); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 199 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 200 | // If requested, capture diagnostics in the ASTUnit. |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 201 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, AST->getDiagnostics(), |
Douglas Gregor | a243315 | 2010-04-05 18:10:21 +0000 | [diff] [blame] | 202 | AST->StoredDiagnostics); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 203 | |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 204 | for (unsigned I = 0; I != NumRemappedFiles; ++I) { |
| 205 | // Create the file entry for the file that we're mapping from. |
| 206 | const FileEntry *FromFile |
| 207 | = AST->getFileManager().getVirtualFile(RemappedFiles[I].first, |
| 208 | RemappedFiles[I].second->getBufferSize(), |
| 209 | 0); |
| 210 | if (!FromFile) { |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 211 | AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file) |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 212 | << RemappedFiles[I].first; |
Douglas Gregor | 89a56c5 | 2010-02-27 01:32:48 +0000 | [diff] [blame] | 213 | delete RemappedFiles[I].second; |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 214 | continue; |
| 215 | } |
| 216 | |
| 217 | // Override the contents of the "from" file with the contents of |
| 218 | // the "to" file. |
| 219 | AST->getSourceManager().overrideFileContents(FromFile, |
| 220 | RemappedFiles[I].second); |
| 221 | } |
| 222 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 223 | // Gather Info for preprocessor construction later on. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 225 | LangOptions LangInfo; |
| 226 | HeaderSearch &HeaderInfo = *AST->HeaderInfo.get(); |
| 227 | std::string TargetTriple; |
| 228 | std::string Predefines; |
| 229 | unsigned Counter; |
| 230 | |
Daniel Dunbar | 3a0637b | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 231 | llvm::OwningPtr<PCHReader> Reader; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 232 | llvm::OwningPtr<ExternalASTSource> Source; |
| 233 | |
Ted Kremenek | 428c637 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 234 | Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(), |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 235 | AST->getDiagnostics())); |
Daniel Dunbar | 2d9c740 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 236 | Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple, |
| 237 | Predefines, Counter)); |
| 238 | |
| 239 | switch (Reader->ReadPCH(Filename)) { |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 240 | case PCHReader::Success: |
| 241 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 243 | case PCHReader::Failure: |
Argyrios Kyrtzidis | 55c3411 | 2009-06-25 18:22:30 +0000 | [diff] [blame] | 244 | case PCHReader::IgnorePCH: |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 245 | AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 246 | return NULL; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 247 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Daniel Dunbar | a8a5093 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 249 | AST->OriginalSourceFile = Reader->getOriginalSourceFile(); |
| 250 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 251 | // PCH loaded successfully. Now create the preprocessor. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 253 | // Get information about the target being compiled for. |
Daniel Dunbar | b9bbd54 | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 254 | // |
| 255 | // FIXME: This is broken, we should store the TargetOptions in the PCH. |
| 256 | TargetOptions TargetOpts; |
| 257 | TargetOpts.ABI = ""; |
Charles Davis | 95a546e | 2010-06-11 01:06:47 +0000 | [diff] [blame] | 258 | TargetOpts.CXXABI = "itanium"; |
Daniel Dunbar | b9bbd54 | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 259 | TargetOpts.CPU = ""; |
| 260 | TargetOpts.Features.clear(); |
| 261 | TargetOpts.Triple = TargetTriple; |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 262 | AST->Target.reset(TargetInfo::CreateTargetInfo(AST->getDiagnostics(), |
| 263 | TargetOpts)); |
| 264 | AST->PP.reset(new Preprocessor(AST->getDiagnostics(), LangInfo, |
| 265 | *AST->Target.get(), |
Daniel Dunbar | 7cd285f | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 266 | AST->getSourceManager(), HeaderInfo)); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 267 | Preprocessor &PP = *AST->PP.get(); |
| 268 | |
Daniel Dunbar | b7bbfdd | 2009-09-21 03:03:47 +0000 | [diff] [blame] | 269 | PP.setPredefines(Reader->getSuggestedPredefines()); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 270 | PP.setCounterValue(Counter); |
Daniel Dunbar | 2d9c740 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 271 | Reader->setPreprocessor(PP); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 273 | // Create and initialize the ASTContext. |
| 274 | |
| 275 | AST->Ctx.reset(new ASTContext(LangInfo, |
Daniel Dunbar | 7cd285f | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 276 | AST->getSourceManager(), |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 277 | *AST->Target.get(), |
| 278 | PP.getIdentifierTable(), |
| 279 | PP.getSelectorTable(), |
| 280 | PP.getBuiltinInfo(), |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 281 | /* size_reserve = */0)); |
| 282 | ASTContext &Context = *AST->Ctx.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Daniel Dunbar | 2d9c740 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 284 | Reader->InitializeContext(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 286 | // Attach the PCH reader to the AST context as an external AST |
| 287 | // source, so that declarations will be deserialized from the |
| 288 | // PCH file as needed. |
Daniel Dunbar | 2d9c740 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 289 | Source.reset(Reader.take()); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 290 | Context.setExternalSource(Source); |
| 291 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | return AST.take(); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 293 | } |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 294 | |
| 295 | namespace { |
| 296 | |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 297 | class TopLevelDeclTrackerConsumer : public ASTConsumer { |
| 298 | ASTUnit &Unit; |
| 299 | |
| 300 | public: |
| 301 | TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {} |
| 302 | |
| 303 | void HandleTopLevelDecl(DeclGroupRef D) { |
Ted Kremenek | acc59c3 | 2010-05-03 20:16:35 +0000 | [diff] [blame] | 304 | for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) { |
| 305 | Decl *D = *it; |
| 306 | // FIXME: Currently ObjC method declarations are incorrectly being |
| 307 | // reported as top-level declarations, even though their DeclContext |
| 308 | // is the containing ObjC @interface/@implementation. This is a |
| 309 | // fundamental problem in the parser right now. |
| 310 | if (isa<ObjCMethodDecl>(D)) |
| 311 | continue; |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 312 | Unit.addTopLevelDecl(D); |
Ted Kremenek | acc59c3 | 2010-05-03 20:16:35 +0000 | [diff] [blame] | 313 | } |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 314 | } |
| 315 | }; |
| 316 | |
| 317 | class TopLevelDeclTrackerAction : public ASTFrontendAction { |
| 318 | public: |
| 319 | ASTUnit &Unit; |
| 320 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 321 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 322 | llvm::StringRef InFile) { |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 323 | return new TopLevelDeclTrackerConsumer(Unit); |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | public: |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 327 | TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {} |
| 328 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 329 | virtual bool hasCodeCompletionSupport() const { return false; } |
| 330 | }; |
| 331 | |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 332 | class PrecompilePreambleConsumer : public PCHGenerator { |
| 333 | ASTUnit &Unit; |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 334 | std::vector<Decl *> TopLevelDecls; |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 335 | |
| 336 | public: |
| 337 | PrecompilePreambleConsumer(ASTUnit &Unit, |
| 338 | const Preprocessor &PP, bool Chaining, |
| 339 | const char *isysroot, llvm::raw_ostream *Out) |
| 340 | : PCHGenerator(PP, Chaining, isysroot, Out), Unit(Unit) { } |
| 341 | |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 342 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 343 | for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) { |
| 344 | Decl *D = *it; |
| 345 | // FIXME: Currently ObjC method declarations are incorrectly being |
| 346 | // reported as top-level declarations, even though their DeclContext |
| 347 | // is the containing ObjC @interface/@implementation. This is a |
| 348 | // fundamental problem in the parser right now. |
| 349 | if (isa<ObjCMethodDecl>(D)) |
| 350 | continue; |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 351 | TopLevelDecls.push_back(D); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
| 356 | PCHGenerator::HandleTranslationUnit(Ctx); |
| 357 | if (!Unit.getDiagnostics().hasErrorOccurred()) { |
| 358 | // Translate the top-level declarations we captured during |
| 359 | // parsing into declaration IDs in the precompiled |
| 360 | // preamble. This will allow us to deserialize those top-level |
| 361 | // declarations when requested. |
| 362 | for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I) |
| 363 | Unit.addTopLevelDeclFromPreamble( |
| 364 | getWriter().getDeclID(TopLevelDecls[I])); |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | }; |
| 368 | |
| 369 | class PrecompilePreambleAction : public ASTFrontendAction { |
| 370 | ASTUnit &Unit; |
| 371 | |
| 372 | public: |
| 373 | explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {} |
| 374 | |
| 375 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 376 | llvm::StringRef InFile) { |
| 377 | std::string Sysroot; |
| 378 | llvm::raw_ostream *OS = 0; |
| 379 | bool Chaining; |
| 380 | if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot, |
| 381 | OS, Chaining)) |
| 382 | return 0; |
| 383 | |
| 384 | const char *isysroot = CI.getFrontendOpts().RelocatablePCH ? |
| 385 | Sysroot.c_str() : 0; |
| 386 | return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Chaining, |
| 387 | isysroot, OS); |
| 388 | } |
| 389 | |
| 390 | virtual bool hasCodeCompletionSupport() const { return false; } |
| 391 | virtual bool hasASTFileSupport() const { return false; } |
| 392 | }; |
| 393 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 396 | /// Parse the source file into a translation unit using the given compiler |
| 397 | /// invocation, replacing the current translation unit. |
| 398 | /// |
| 399 | /// \returns True if a failure occurred that causes the ASTUnit not to |
| 400 | /// contain any translation-unit information, false otherwise. |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 401 | bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) { |
Douglas Gregor | 96c0426 | 2010-07-27 14:52:07 +0000 | [diff] [blame] | 402 | delete SavedMainFileBuffer; |
| 403 | SavedMainFileBuffer = 0; |
| 404 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 405 | if (!Invocation.get()) |
| 406 | return true; |
| 407 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 408 | // Create the compiler instance to use for building the AST. |
Daniel Dunbar | 7afbb8c | 2009-12-02 08:43:56 +0000 | [diff] [blame] | 409 | CompilerInstance Clang; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 410 | Clang.setInvocation(Invocation.take()); |
| 411 | OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; |
| 412 | |
| 413 | // Set up diagnostics. |
| 414 | Clang.setDiagnostics(&getDiagnostics()); |
| 415 | Clang.setDiagnosticClient(getDiagnostics().getClient()); |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 416 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 417 | // Create the target instance. |
| 418 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 419 | Clang.getTargetOpts())); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 420 | if (!Clang.hasTarget()) { |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 421 | Clang.takeDiagnosticClient(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 422 | return true; |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 423 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 424 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 425 | // Inform the target of the language options. |
| 426 | // |
| 427 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 428 | // created. This complexity should be lifted elsewhere. |
| 429 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 430 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 431 | assert(Clang.getFrontendOpts().Inputs.size() == 1 && |
| 432 | "Invocation must have exactly one source file!"); |
Daniel Dunbar | 9b491e7 | 2010-06-07 23:22:09 +0000 | [diff] [blame] | 433 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 434 | "FIXME: AST inputs not yet supported here!"); |
Daniel Dunbar | 9507f9c | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 435 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
| 436 | "IR inputs not support here!"); |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 437 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 438 | // Configure the various subsystems. |
| 439 | // FIXME: Should we retain the previous file manager? |
| 440 | FileMgr.reset(new FileManager); |
| 441 | SourceMgr.reset(new SourceManager(getDiagnostics())); |
| 442 | Ctx.reset(); |
| 443 | PP.reset(); |
| 444 | |
| 445 | // Clear out old caches and data. |
| 446 | TopLevelDecls.clear(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 447 | CleanTemporaryFiles(); |
| 448 | PreprocessedEntitiesByFile.clear(); |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 449 | |
| 450 | if (!OverrideMainBuffer) |
| 451 | StoredDiagnostics.clear(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 452 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 453 | // Capture any diagnostics that would otherwise be dropped. |
| 454 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, |
| 455 | Clang.getDiagnostics(), |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 456 | StoredDiagnostics); |
| 457 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 458 | // Create a file manager object to provide access to and cache the filesystem. |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 459 | Clang.setFileManager(&getFileManager()); |
| 460 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 461 | // Create the source manager. |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 462 | Clang.setSourceManager(&getSourceManager()); |
| 463 | |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 464 | // If the main file has been overridden due to the use of a preamble, |
| 465 | // make that override happen and introduce the preamble. |
| 466 | PreprocessorOptions &PreprocessorOpts = Clang.getPreprocessorOpts(); |
| 467 | if (OverrideMainBuffer) { |
| 468 | PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer); |
| 469 | PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size(); |
| 470 | PreprocessorOpts.PrecompiledPreambleBytes.second |
| 471 | = PreambleEndsAtStartOfLine; |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 472 | PreprocessorOpts.ImplicitPCHInclude = PreambleFile; |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 473 | PreprocessorOpts.DisablePCHValidation = true; |
Douglas Gregor | 96c0426 | 2010-07-27 14:52:07 +0000 | [diff] [blame] | 474 | |
| 475 | // Keep track of the override buffer; |
| 476 | SavedMainFileBuffer = OverrideMainBuffer; |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 477 | |
| 478 | // The stored diagnostic has the old source manager in it; update |
| 479 | // the locations to refer into the new source manager. Since we've |
| 480 | // been careful to make sure that the source manager's state |
| 481 | // before and after are identical, so that we can reuse the source |
| 482 | // location itself. |
| 483 | for (unsigned I = 0, N = StoredDiagnostics.size(); I != N; ++I) { |
| 484 | FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), |
| 485 | getSourceManager()); |
| 486 | StoredDiagnostics[I].setLocation(Loc); |
| 487 | } |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 490 | llvm::OwningPtr<TopLevelDeclTrackerAction> Act; |
| 491 | Act.reset(new TopLevelDeclTrackerAction(*this)); |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 492 | if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, |
Daniel Dunbar | 8654638 | 2010-06-07 23:23:06 +0000 | [diff] [blame] | 493 | Clang.getFrontendOpts().Inputs[0].first)) |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 494 | goto error; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 495 | |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 496 | Act->Execute(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 497 | |
Daniel Dunbar | d2f8be3 | 2009-12-01 21:57:33 +0000 | [diff] [blame] | 498 | // Steal the created target, context, and preprocessor, and take back the |
| 499 | // source and file managers. |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 500 | Ctx.reset(Clang.takeASTContext()); |
| 501 | PP.reset(Clang.takePreprocessor()); |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 502 | Clang.takeSourceManager(); |
| 503 | Clang.takeFileManager(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 504 | Target.reset(Clang.takeTarget()); |
| 505 | |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 506 | Act->EndSourceFile(); |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 507 | |
| 508 | // Remove the overridden buffer we used for the preamble. |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 509 | if (OverrideMainBuffer) |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 510 | PreprocessorOpts.eraseRemappedFile( |
| 511 | PreprocessorOpts.remapped_file_buffer_end() - 1); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 512 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 513 | Clang.takeDiagnosticClient(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 514 | |
| 515 | Invocation.reset(Clang.takeInvocation()); |
| 516 | return false; |
| 517 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 518 | error: |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 519 | // Remove the overridden buffer we used for the preamble. |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 520 | if (OverrideMainBuffer) { |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 521 | PreprocessorOpts.eraseRemappedFile( |
| 522 | PreprocessorOpts.remapped_file_buffer_end() - 1); |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 523 | PreprocessorOpts.DisablePCHValidation = true; |
| 524 | } |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 525 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 526 | Clang.takeSourceManager(); |
| 527 | Clang.takeFileManager(); |
| 528 | Clang.takeDiagnosticClient(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 529 | Invocation.reset(Clang.takeInvocation()); |
| 530 | return true; |
| 531 | } |
| 532 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 533 | /// \brief Simple function to retrieve a path for a preamble precompiled header. |
| 534 | static std::string GetPreamblePCHPath() { |
| 535 | // FIXME: This is lame; sys::Path should provide this function (in particular, |
| 536 | // it should know how to find the temporary files dir). |
| 537 | // FIXME: This is really lame. I copied this code from the Driver! |
| 538 | std::string Error; |
| 539 | const char *TmpDir = ::getenv("TMPDIR"); |
| 540 | if (!TmpDir) |
| 541 | TmpDir = ::getenv("TEMP"); |
| 542 | if (!TmpDir) |
| 543 | TmpDir = ::getenv("TMP"); |
| 544 | if (!TmpDir) |
| 545 | TmpDir = "/tmp"; |
| 546 | llvm::sys::Path P(TmpDir); |
| 547 | P.appendComponent("preamble"); |
| 548 | if (P.createTemporaryFileOnDisk()) |
| 549 | return std::string(); |
| 550 | |
| 551 | P.appendSuffix("pch"); |
| 552 | return P.str(); |
| 553 | } |
| 554 | |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 555 | /// \brief Compute the preamble for the main file, providing the source buffer |
| 556 | /// that corresponds to the main file along with a pair (bytes, start-of-line) |
| 557 | /// that describes the preamble. |
| 558 | std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 559 | ASTUnit::ComputePreamble(CompilerInvocation &Invocation, bool &CreatedBuffer) { |
| 560 | FrontendOptions &FrontendOpts = Invocation.getFrontendOpts(); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 561 | PreprocessorOptions &PreprocessorOpts |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 562 | = Invocation.getPreprocessorOpts(); |
| 563 | CreatedBuffer = false; |
| 564 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 565 | // Try to determine if the main file has been remapped, either from the |
| 566 | // command line (to another file) or directly through the compiler invocation |
| 567 | // (to a memory buffer). |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 568 | llvm::MemoryBuffer *Buffer = 0; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 569 | llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second); |
| 570 | if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) { |
| 571 | // Check whether there is a file-file remapping of the main file |
| 572 | for (PreprocessorOptions::remapped_file_iterator |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 573 | M = PreprocessorOpts.remapped_file_begin(), |
| 574 | E = PreprocessorOpts.remapped_file_end(); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 575 | M != E; |
| 576 | ++M) { |
| 577 | llvm::sys::PathWithStatus MPath(M->first); |
| 578 | if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { |
| 579 | if (MainFileStatus->uniqueID == MStatus->uniqueID) { |
| 580 | // We found a remapping. Try to load the resulting, remapped source. |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 581 | if (CreatedBuffer) { |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 582 | delete Buffer; |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 583 | CreatedBuffer = false; |
| 584 | } |
| 585 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 586 | Buffer = llvm::MemoryBuffer::getFile(M->second); |
| 587 | if (!Buffer) |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 588 | return std::make_pair((llvm::MemoryBuffer*)0, |
| 589 | std::make_pair(0, true)); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 590 | CreatedBuffer = true; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 591 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 592 | // Remove this remapping. We've captured the buffer already. |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 593 | M = PreprocessorOpts.eraseRemappedFile(M); |
| 594 | E = PreprocessorOpts.remapped_file_end(); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | // Check whether there is a file-buffer remapping. It supercedes the |
| 600 | // file-file remapping. |
| 601 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 602 | M = PreprocessorOpts.remapped_file_buffer_begin(), |
| 603 | E = PreprocessorOpts.remapped_file_buffer_end(); |
| 604 | M != E; |
| 605 | ++M) { |
| 606 | llvm::sys::PathWithStatus MPath(M->first); |
| 607 | if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { |
| 608 | if (MainFileStatus->uniqueID == MStatus->uniqueID) { |
| 609 | // We found a remapping. |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 610 | if (CreatedBuffer) { |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 611 | delete Buffer; |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 612 | CreatedBuffer = false; |
| 613 | } |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 614 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 615 | Buffer = const_cast<llvm::MemoryBuffer *>(M->second); |
| 616 | |
| 617 | // Remove this remapping. We've captured the buffer already. |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 618 | M = PreprocessorOpts.eraseRemappedFile(M); |
| 619 | E = PreprocessorOpts.remapped_file_buffer_end(); |
| 620 | } |
| 621 | } |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 622 | } |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | // If the main source file was not remapped, load it now. |
| 626 | if (!Buffer) { |
| 627 | Buffer = llvm::MemoryBuffer::getFile(FrontendOpts.Inputs[0].second); |
| 628 | if (!Buffer) |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 629 | return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true)); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 630 | |
| 631 | CreatedBuffer = true; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 634 | return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer)); |
| 635 | } |
| 636 | |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 637 | static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old, |
| 638 | bool DeleteOld, |
| 639 | unsigned NewSize, |
| 640 | llvm::StringRef NewName) { |
| 641 | llvm::MemoryBuffer *Result |
| 642 | = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName); |
| 643 | memcpy(const_cast<char*>(Result->getBufferStart()), |
| 644 | Old->getBufferStart(), Old->getBufferSize()); |
| 645 | memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(), |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 646 | ' ', NewSize - Old->getBufferSize() - 1); |
| 647 | const_cast<char*>(Result->getBufferEnd())[-1] = '\n'; |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 648 | |
| 649 | if (DeleteOld) |
| 650 | delete Old; |
| 651 | |
| 652 | return Result; |
| 653 | } |
| 654 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 655 | /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing |
| 656 | /// the source file. |
| 657 | /// |
| 658 | /// This routine will compute the preamble of the main source file. If a |
| 659 | /// non-trivial preamble is found, it will precompile that preamble into a |
| 660 | /// precompiled header so that the precompiled preamble can be used to reduce |
| 661 | /// reparsing time. If a precompiled preamble has already been constructed, |
| 662 | /// this routine will determine if it is still valid and, if so, avoid |
| 663 | /// rebuilding the precompiled preamble. |
| 664 | /// |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 665 | /// \returns If the precompiled preamble can be used, returns a newly-allocated |
| 666 | /// buffer that should be used in place of the main file when doing so. |
| 667 | /// Otherwise, returns a NULL pointer. |
| 668 | llvm::MemoryBuffer *ASTUnit::BuildPrecompiledPreamble() { |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 669 | CompilerInvocation PreambleInvocation(*Invocation); |
| 670 | FrontendOptions &FrontendOpts = PreambleInvocation.getFrontendOpts(); |
| 671 | PreprocessorOptions &PreprocessorOpts |
| 672 | = PreambleInvocation.getPreprocessorOpts(); |
| 673 | |
| 674 | bool CreatedPreambleBuffer = false; |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 675 | std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 676 | = ComputePreamble(PreambleInvocation, CreatedPreambleBuffer); |
| 677 | |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 678 | if (!NewPreamble.second.first) { |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 679 | // We couldn't find a preamble in the main source. Clear out the current |
| 680 | // preamble, if we have one. It's obviously no good any more. |
| 681 | Preamble.clear(); |
| 682 | if (!PreambleFile.empty()) { |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 683 | llvm::sys::Path(PreambleFile).eraseFromDisk(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 684 | PreambleFile.clear(); |
| 685 | } |
| 686 | if (CreatedPreambleBuffer) |
| 687 | delete NewPreamble.first; |
| 688 | |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 689 | return 0; |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | if (!Preamble.empty()) { |
| 693 | // We've previously computed a preamble. Check whether we have the same |
| 694 | // preamble now that we did before, and that there's enough space in |
| 695 | // the main-file buffer within the precompiled preamble to fit the |
| 696 | // new main file. |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 697 | if (Preamble.size() == NewPreamble.second.first && |
| 698 | PreambleEndsAtStartOfLine == NewPreamble.second.second && |
Douglas Gregor | f5275a8 | 2010-07-24 00:42:07 +0000 | [diff] [blame] | 699 | NewPreamble.first->getBufferSize() < PreambleReservedSize-2 && |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 700 | memcmp(&Preamble[0], NewPreamble.first->getBufferStart(), |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 701 | NewPreamble.second.first) == 0) { |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 702 | // The preamble has not changed. We may be able to re-use the precompiled |
| 703 | // preamble. |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 704 | |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 705 | // Check that none of the files used by the preamble have changed. |
| 706 | bool AnyFileChanged = false; |
| 707 | |
| 708 | // First, make a record of those files that have been overridden via |
| 709 | // remapping or unsaved_files. |
| 710 | llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles; |
| 711 | for (PreprocessorOptions::remapped_file_iterator |
| 712 | R = PreprocessorOpts.remapped_file_begin(), |
| 713 | REnd = PreprocessorOpts.remapped_file_end(); |
| 714 | !AnyFileChanged && R != REnd; |
| 715 | ++R) { |
| 716 | struct stat StatBuf; |
| 717 | if (stat(R->second.c_str(), &StatBuf)) { |
| 718 | // If we can't stat the file we're remapping to, assume that something |
| 719 | // horrible happened. |
| 720 | AnyFileChanged = true; |
| 721 | break; |
| 722 | } |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 723 | |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 724 | OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size, |
| 725 | StatBuf.st_mtime); |
| 726 | } |
| 727 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 728 | R = PreprocessorOpts.remapped_file_buffer_begin(), |
| 729 | REnd = PreprocessorOpts.remapped_file_buffer_end(); |
| 730 | !AnyFileChanged && R != REnd; |
| 731 | ++R) { |
| 732 | // FIXME: Should we actually compare the contents of file->buffer |
| 733 | // remappings? |
| 734 | OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(), |
| 735 | 0); |
| 736 | } |
| 737 | |
| 738 | // Check whether anything has changed. |
| 739 | for (llvm::StringMap<std::pair<off_t, time_t> >::iterator |
| 740 | F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end(); |
| 741 | !AnyFileChanged && F != FEnd; |
| 742 | ++F) { |
| 743 | llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden |
| 744 | = OverriddenFiles.find(F->first()); |
| 745 | if (Overridden != OverriddenFiles.end()) { |
| 746 | // This file was remapped; check whether the newly-mapped file |
| 747 | // matches up with the previous mapping. |
| 748 | if (Overridden->second != F->second) |
| 749 | AnyFileChanged = true; |
| 750 | continue; |
| 751 | } |
| 752 | |
| 753 | // The file was not remapped; check whether it has changed on disk. |
| 754 | struct stat StatBuf; |
| 755 | if (stat(F->first(), &StatBuf)) { |
| 756 | // If we can't stat the file, assume that something horrible happened. |
| 757 | AnyFileChanged = true; |
| 758 | } else if (StatBuf.st_size != F->second.first || |
| 759 | StatBuf.st_mtime != F->second.second) |
| 760 | AnyFileChanged = true; |
| 761 | } |
| 762 | |
| 763 | if (!AnyFileChanged) { |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 764 | // Okay! We can re-use the precompiled preamble. |
| 765 | |
| 766 | // Set the state of the diagnostic object to mimic its state |
| 767 | // after parsing the preamble. |
| 768 | getDiagnostics().Reset(); |
| 769 | getDiagnostics().setNumWarnings(NumWarningsInPreamble); |
| 770 | if (StoredDiagnostics.size() > NumStoredDiagnosticsInPreamble) |
| 771 | StoredDiagnostics.erase( |
| 772 | StoredDiagnostics.begin() + NumStoredDiagnosticsInPreamble, |
| 773 | StoredDiagnostics.end()); |
| 774 | |
| 775 | // Create a version of the main file buffer that is padded to |
| 776 | // buffer size we reserved when creating the preamble. |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 777 | return CreatePaddedMainFileBuffer(NewPreamble.first, |
| 778 | CreatedPreambleBuffer, |
| 779 | PreambleReservedSize, |
| 780 | FrontendOpts.Inputs[0].second); |
| 781 | } |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | // We can't reuse the previously-computed preamble. Build a new one. |
| 785 | Preamble.clear(); |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 786 | llvm::sys::Path(PreambleFile).eraseFromDisk(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | // We did not previously compute a preamble, or it can't be reused anyway. |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 790 | llvm::Timer *PreambleTimer = 0; |
| 791 | if (TimerGroup.get()) { |
| 792 | PreambleTimer = new llvm::Timer("Precompiling preamble", *TimerGroup); |
| 793 | PreambleTimer->startTimer(); |
| 794 | Timers.push_back(PreambleTimer); |
| 795 | } |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 796 | |
| 797 | // Create a new buffer that stores the preamble. The buffer also contains |
| 798 | // extra space for the original contents of the file (which will be present |
| 799 | // when we actually parse the file) along with more room in case the file |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 800 | // grows. |
| 801 | PreambleReservedSize = NewPreamble.first->getBufferSize(); |
| 802 | if (PreambleReservedSize < 4096) |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 803 | PreambleReservedSize = 8191; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 804 | else |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 805 | PreambleReservedSize *= 2; |
| 806 | |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 807 | // Save the preamble text for later; we'll need to compare against it for |
| 808 | // subsequent reparses. |
| 809 | Preamble.assign(NewPreamble.first->getBufferStart(), |
| 810 | NewPreamble.first->getBufferStart() |
| 811 | + NewPreamble.second.first); |
| 812 | PreambleEndsAtStartOfLine = NewPreamble.second.second; |
| 813 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 814 | llvm::MemoryBuffer *PreambleBuffer |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 815 | = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize, |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 816 | FrontendOpts.Inputs[0].second); |
| 817 | memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()), |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 818 | NewPreamble.first->getBufferStart(), Preamble.size()); |
| 819 | memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(), |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 820 | ' ', PreambleReservedSize - Preamble.size() - 1); |
| 821 | const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n'; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 822 | |
| 823 | // Remap the main source file to the preamble buffer. |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 824 | llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 825 | PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer); |
| 826 | |
| 827 | // Tell the compiler invocation to generate a temporary precompiled header. |
| 828 | FrontendOpts.ProgramAction = frontend::GeneratePCH; |
| 829 | // FIXME: Set ChainedPCH, once it is ready. |
| 830 | // FIXME: Generate the precompiled header into memory? |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 831 | FrontendOpts.OutputFile = GetPreamblePCHPath(); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 832 | |
| 833 | // Create the compiler instance to use for building the precompiled preamble. |
| 834 | CompilerInstance Clang; |
| 835 | Clang.setInvocation(&PreambleInvocation); |
| 836 | OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; |
| 837 | |
| 838 | // Set up diagnostics. |
| 839 | Clang.setDiagnostics(&getDiagnostics()); |
| 840 | Clang.setDiagnosticClient(getDiagnostics().getClient()); |
| 841 | |
| 842 | // Create the target instance. |
| 843 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 844 | Clang.getTargetOpts())); |
| 845 | if (!Clang.hasTarget()) { |
| 846 | Clang.takeDiagnosticClient(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 847 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 848 | Preamble.clear(); |
| 849 | if (CreatedPreambleBuffer) |
| 850 | delete NewPreamble.first; |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 851 | if (PreambleTimer) |
| 852 | PreambleTimer->stopTimer(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 853 | |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 854 | return 0; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | // Inform the target of the language options. |
| 858 | // |
| 859 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 860 | // created. This complexity should be lifted elsewhere. |
| 861 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
| 862 | |
| 863 | assert(Clang.getFrontendOpts().Inputs.size() == 1 && |
| 864 | "Invocation must have exactly one source file!"); |
| 865 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && |
| 866 | "FIXME: AST inputs not yet supported here!"); |
| 867 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
| 868 | "IR inputs not support here!"); |
| 869 | |
| 870 | // Clear out old caches and data. |
| 871 | StoredDiagnostics.clear(); |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 872 | TopLevelDecls.clear(); |
| 873 | TopLevelDeclsInPreamble.clear(); |
| 874 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 875 | // Capture any diagnostics that would otherwise be dropped. |
| 876 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 877 | getDiagnostics(), |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 878 | StoredDiagnostics); |
| 879 | |
| 880 | // Create a file manager object to provide access to and cache the filesystem. |
| 881 | Clang.setFileManager(new FileManager); |
| 882 | |
| 883 | // Create the source manager. |
| 884 | Clang.setSourceManager(new SourceManager(getDiagnostics())); |
| 885 | |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 886 | llvm::OwningPtr<PrecompilePreambleAction> Act; |
| 887 | Act.reset(new PrecompilePreambleAction(*this)); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 888 | if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, |
| 889 | Clang.getFrontendOpts().Inputs[0].first)) { |
| 890 | Clang.takeDiagnosticClient(); |
| 891 | Clang.takeInvocation(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 892 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 893 | Preamble.clear(); |
| 894 | if (CreatedPreambleBuffer) |
| 895 | delete NewPreamble.first; |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 896 | if (PreambleTimer) |
| 897 | PreambleTimer->stopTimer(); |
| 898 | |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 899 | return 0; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | Act->Execute(); |
| 903 | Act->EndSourceFile(); |
| 904 | Clang.takeDiagnosticClient(); |
| 905 | Clang.takeInvocation(); |
| 906 | |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 907 | if (Diagnostics->hasErrorOccurred()) { |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 908 | // There were errors parsing the preamble, so no precompiled header was |
| 909 | // generated. Forget that we even tried. |
| 910 | // FIXME: Should we leave a note for ourselves to try again? |
| 911 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 912 | Preamble.clear(); |
| 913 | if (CreatedPreambleBuffer) |
| 914 | delete NewPreamble.first; |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 915 | if (PreambleTimer) |
| 916 | PreambleTimer->stopTimer(); |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 917 | TopLevelDeclsInPreamble.clear(); |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 918 | return 0; |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | // Keep track of the preamble we precompiled. |
| 922 | PreambleFile = FrontendOpts.OutputFile; |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 923 | NumStoredDiagnosticsInPreamble = StoredDiagnostics.size(); |
| 924 | NumWarningsInPreamble = getDiagnostics().getNumWarnings(); |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 925 | |
| 926 | // Keep track of all of the files that the source manager knows about, |
| 927 | // so we can verify whether they have changed or not. |
| 928 | FilesInPreamble.clear(); |
| 929 | SourceManager &SourceMgr = Clang.getSourceManager(); |
| 930 | const llvm::MemoryBuffer *MainFileBuffer |
| 931 | = SourceMgr.getBuffer(SourceMgr.getMainFileID()); |
| 932 | for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(), |
| 933 | FEnd = SourceMgr.fileinfo_end(); |
| 934 | F != FEnd; |
| 935 | ++F) { |
| 936 | const FileEntry *File = F->second->Entry; |
| 937 | if (!File || F->second->getRawBuffer() == MainFileBuffer) |
| 938 | continue; |
| 939 | |
| 940 | FilesInPreamble[File->getName()] |
| 941 | = std::make_pair(F->second->getSize(), File->getModificationTime()); |
| 942 | } |
| 943 | |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 944 | if (PreambleTimer) |
| 945 | PreambleTimer->stopTimer(); |
| 946 | |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 947 | return CreatePaddedMainFileBuffer(NewPreamble.first, |
| 948 | CreatedPreambleBuffer, |
| 949 | PreambleReservedSize, |
| 950 | FrontendOpts.Inputs[0].second); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 951 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 952 | |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 953 | void ASTUnit::RealizeTopLevelDeclsFromPreamble() { |
| 954 | std::vector<Decl *> Resolved; |
| 955 | Resolved.reserve(TopLevelDeclsInPreamble.size()); |
| 956 | ExternalASTSource &Source = *getASTContext().getExternalSource(); |
| 957 | for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) { |
| 958 | // Resolve the declaration ID to an actual declaration, possibly |
| 959 | // deserializing the declaration in the process. |
| 960 | Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]); |
| 961 | if (D) |
| 962 | Resolved.push_back(D); |
| 963 | } |
| 964 | TopLevelDeclsInPreamble.clear(); |
| 965 | TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end()); |
| 966 | } |
| 967 | |
| 968 | unsigned ASTUnit::getMaxPCHLevel() const { |
| 969 | if (!getOnlyLocalDecls()) |
| 970 | return Decl::MaxPCHLevel; |
| 971 | |
| 972 | unsigned Result = 0; |
| 973 | if (isMainFileAST() || SavedMainFileBuffer) |
| 974 | ++Result; |
| 975 | return Result; |
| 976 | } |
| 977 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 978 | ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI, |
| 979 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
| 980 | bool OnlyLocalDecls, |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 981 | bool CaptureDiagnostics, |
| 982 | bool PrecompilePreamble) { |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 983 | if (!Diags.getPtr()) { |
| 984 | // No diagnostics engine was provided, so create our own diagnostics object |
| 985 | // with the default options. |
| 986 | DiagnosticOptions DiagOpts; |
| 987 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
| 988 | } |
| 989 | |
| 990 | // Create the AST unit. |
| 991 | llvm::OwningPtr<ASTUnit> AST; |
| 992 | AST.reset(new ASTUnit(false)); |
| 993 | AST->Diagnostics = Diags; |
| 994 | AST->CaptureDiagnostics = CaptureDiagnostics; |
| 995 | AST->OnlyLocalDecls = OnlyLocalDecls; |
| 996 | AST->Invocation.reset(CI); |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 997 | CI->getPreprocessorOpts().RetainRemappedFileBuffers = true; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 998 | |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 999 | if (getenv("LIBCLANG_TIMING")) |
| 1000 | AST->TimerGroup.reset( |
| 1001 | new llvm::TimerGroup(CI->getFrontendOpts().Inputs[0].second)); |
| 1002 | |
| 1003 | |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1004 | llvm::MemoryBuffer *OverrideMainBuffer = 0; |
Douglas Gregor | a5fd522 | 2010-07-28 22:12:37 +0000 | [diff] [blame] | 1005 | // FIXME: When C++ PCH is ready, allow use of it for a precompiled preamble. |
| 1006 | if (PrecompilePreamble && !CI->getLangOpts().CPlusPlus) |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1007 | OverrideMainBuffer = AST->BuildPrecompiledPreamble(); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1008 | |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1009 | llvm::Timer *ParsingTimer = 0; |
| 1010 | if (AST->TimerGroup.get()) { |
| 1011 | ParsingTimer = new llvm::Timer("Initial parse", *AST->TimerGroup); |
| 1012 | ParsingTimer->startTimer(); |
| 1013 | AST->Timers.push_back(ParsingTimer); |
| 1014 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1015 | |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1016 | bool Failed = AST->Parse(OverrideMainBuffer); |
| 1017 | if (ParsingTimer) |
| 1018 | ParsingTimer->stopTimer(); |
| 1019 | |
| 1020 | return Failed? 0 : AST.take(); |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1021 | } |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1022 | |
| 1023 | ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin, |
| 1024 | const char **ArgEnd, |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 1025 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
Daniel Dunbar | 8d4a202 | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 1026 | llvm::StringRef ResourceFilesPath, |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1027 | bool OnlyLocalDecls, |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1028 | RemappedFile *RemappedFiles, |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1029 | unsigned NumRemappedFiles, |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1030 | bool CaptureDiagnostics, |
| 1031 | bool PrecompilePreamble) { |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 1032 | if (!Diags.getPtr()) { |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1033 | // No diagnostics engine was provided, so create our own diagnostics object |
| 1034 | // with the default options. |
| 1035 | DiagnosticOptions DiagOpts; |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 1036 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1039 | llvm::SmallVector<const char *, 16> Args; |
| 1040 | Args.push_back("<clang>"); // FIXME: Remove dummy argument. |
| 1041 | Args.insert(Args.end(), ArgBegin, ArgEnd); |
| 1042 | |
| 1043 | // FIXME: Find a cleaner way to force the driver into restricted modes. We |
| 1044 | // also want to force it to use clang. |
| 1045 | Args.push_back("-fsyntax-only"); |
| 1046 | |
Daniel Dunbar | 8d4a202 | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 1047 | // FIXME: We shouldn't have to pass in the path info. |
Daniel Dunbar | e38764c | 2010-07-19 00:44:04 +0000 | [diff] [blame] | 1048 | driver::Driver TheDriver("clang", llvm::sys::getHostTriple(), |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1049 | "a.out", false, false, *Diags); |
Daniel Dunbar | fcf2d42 | 2010-01-25 00:44:02 +0000 | [diff] [blame] | 1050 | |
| 1051 | // Don't check that inputs exist, they have been remapped. |
| 1052 | TheDriver.setCheckInputsExist(false); |
| 1053 | |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1054 | llvm::OwningPtr<driver::Compilation> C( |
| 1055 | TheDriver.BuildCompilation(Args.size(), Args.data())); |
| 1056 | |
| 1057 | // We expect to get back exactly one command job, if we didn't something |
| 1058 | // failed. |
| 1059 | const driver::JobList &Jobs = C->getJobs(); |
| 1060 | if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) { |
| 1061 | llvm::SmallString<256> Msg; |
| 1062 | llvm::raw_svector_ostream OS(Msg); |
| 1063 | C->PrintJob(OS, C->getJobs(), "; ", true); |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1064 | Diags->Report(diag::err_fe_expected_compiler_job) << OS.str(); |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin()); |
| 1069 | if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") { |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1070 | Diags->Report(diag::err_fe_expected_clang_command); |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | const driver::ArgStringList &CCArgs = Cmd->getArguments(); |
Daniel Dunbar | 6b03ece | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 1075 | llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation); |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 1076 | CompilerInvocation::CreateFromArgs(*CI, |
| 1077 | const_cast<const char **>(CCArgs.data()), |
| 1078 | const_cast<const char **>(CCArgs.data()) + |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1079 | CCArgs.size(), |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1080 | *Diags); |
Daniel Dunbar | d613677 | 2009-12-13 03:45:58 +0000 | [diff] [blame] | 1081 | |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1082 | // Override any files that need remapping |
| 1083 | for (unsigned I = 0; I != NumRemappedFiles; ++I) |
Daniel Dunbar | 6b03ece | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 1084 | CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, |
Daniel Dunbar | 1951192 | 2010-02-16 01:55:04 +0000 | [diff] [blame] | 1085 | RemappedFiles[I].second); |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1086 | |
Daniel Dunbar | a5a166d | 2009-12-15 00:06:45 +0000 | [diff] [blame] | 1087 | // Override the resources path. |
Daniel Dunbar | 6b03ece | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 1088 | CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1089 | |
Daniel Dunbar | 1951192 | 2010-02-16 01:55:04 +0000 | [diff] [blame] | 1090 | CI->getFrontendOpts().DisableFree = true; |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1091 | return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls, |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1092 | CaptureDiagnostics, PrecompilePreamble); |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1093 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1094 | |
| 1095 | bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) { |
| 1096 | if (!Invocation.get()) |
| 1097 | return true; |
| 1098 | |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1099 | llvm::Timer *ReparsingTimer = 0; |
| 1100 | if (TimerGroup.get()) { |
| 1101 | ReparsingTimer = new llvm::Timer("Reparse", *TimerGroup); |
| 1102 | ReparsingTimer->startTimer(); |
| 1103 | Timers.push_back(ReparsingTimer); |
| 1104 | } |
| 1105 | |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1106 | // Remap files. |
| 1107 | // FIXME: Do we want to remove old mappings for these files? |
| 1108 | Invocation->getPreprocessorOpts().clearRemappedFiles(); |
| 1109 | for (unsigned I = 0; I != NumRemappedFiles; ++I) |
| 1110 | Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, |
| 1111 | RemappedFiles[I].second); |
| 1112 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1113 | // If we have a preamble file lying around, build or reuse the precompiled |
| 1114 | // preamble. |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1115 | llvm::MemoryBuffer *OverrideMainBuffer = 0; |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1116 | if (!PreambleFile.empty()) |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1117 | OverrideMainBuffer = BuildPrecompiledPreamble(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1118 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1119 | // Clear out the diagnostics state. |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1120 | if (!OverrideMainBuffer) |
| 1121 | getDiagnostics().Reset(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1122 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1123 | // Parse the sources |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1124 | bool Result = Parse(OverrideMainBuffer); |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1125 | if (ReparsingTimer) |
| 1126 | ReparsingTimer->stopTimer(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1127 | return Result; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1128 | } |