Argyrios Kyrtzidis | 4b562cf | 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 | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTUnit.h" |
| 15 | #include "clang/Frontend/PCHReader.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclVisitor.h" |
| 19 | #include "clang/AST/StmtVisitor.h" |
Daniel Dunbar | 7b55668 | 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 | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/CompilerInstance.h" |
| 25 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/FrontendOptions.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 28 | #include "clang/Lex/HeaderSearch.h" |
| 29 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetOptions.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetInfo.h" |
| 32 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 34 | #include "llvm/System/Host.h" |
Benjamin Kramer | 4a630d3 | 2009-10-18 11:34:14 +0000 | [diff] [blame] | 35 | #include "llvm/System/Path.h" |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 36 | #include <cstdlib> |
Zhongxing Xu | ad23ebe | 2010-07-23 02:15:08 +0000 | [diff] [blame] | 37 | #include <cstdio> |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 38 | using namespace clang; |
| 39 | |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 40 | ASTUnit::ASTUnit(bool _MainFileIsAST) |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 41 | : CaptureDiagnostics(false), MainFileIsAST(_MainFileIsAST), |
| 42 | ConcurrencyCheckValue(CheckUnlocked) { } |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 43 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 44 | ASTUnit::~ASTUnit() { |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 45 | ConcurrencyCheckValue = CheckLocked; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 46 | CleanTemporaryFiles(); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 47 | if (!PreambleFile.empty()) |
| 48 | PreambleFile.eraseFromDisk(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void ASTUnit::CleanTemporaryFiles() { |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 52 | for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) |
| 53 | TemporaryFiles[I].eraseFromDisk(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 54 | TemporaryFiles.clear(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 55 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 56 | |
| 57 | namespace { |
| 58 | |
| 59 | /// \brief Gathers information from PCHReader that will be used to initialize |
| 60 | /// a Preprocessor. |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 61 | class PCHInfoCollector : public PCHReaderListener { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 62 | LangOptions &LangOpt; |
| 63 | HeaderSearch &HSI; |
| 64 | std::string &TargetTriple; |
| 65 | std::string &Predefines; |
| 66 | unsigned &Counter; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 68 | unsigned NumHeaderInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 70 | public: |
| 71 | PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI, |
| 72 | std::string &TargetTriple, std::string &Predefines, |
| 73 | unsigned &Counter) |
| 74 | : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple), |
| 75 | Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 77 | virtual bool ReadLanguageOptions(const LangOptions &LangOpts) { |
| 78 | LangOpt = LangOpts; |
| 79 | return false; |
| 80 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Daniel Dunbar | dc3c0d2 | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 82 | virtual bool ReadTargetTriple(llvm::StringRef Triple) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 83 | TargetTriple = Triple; |
| 84 | return false; |
| 85 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Sebastian Redl | cb481aa | 2010-07-14 23:29:55 +0000 | [diff] [blame] | 87 | virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 88 | llvm::StringRef OriginalFileName, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 89 | std::string &SuggestedPredefines) { |
Sebastian Redl | cb481aa | 2010-07-14 23:29:55 +0000 | [diff] [blame] | 90 | Predefines = Buffers[0].Data; |
| 91 | for (unsigned I = 1, N = Buffers.size(); I != N; ++I) { |
| 92 | Predefines += Buffers[I].Data; |
| 93 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 94 | return false; |
| 95 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Douglas Gregor | ec1afbf | 2010-03-16 19:09:18 +0000 | [diff] [blame] | 97 | virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 98 | HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++); |
| 99 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 101 | virtual void ReadCounter(unsigned Value) { |
| 102 | Counter = Value; |
| 103 | } |
| 104 | }; |
| 105 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 106 | class StoredDiagnosticClient : public DiagnosticClient { |
| 107 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags; |
| 108 | |
| 109 | public: |
| 110 | explicit StoredDiagnosticClient( |
| 111 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags) |
| 112 | : StoredDiags(StoredDiags) { } |
| 113 | |
| 114 | virtual void HandleDiagnostic(Diagnostic::Level Level, |
| 115 | const DiagnosticInfo &Info); |
| 116 | }; |
| 117 | |
| 118 | /// \brief RAII object that optionally captures diagnostics, if |
| 119 | /// there is no diagnostic client to capture them already. |
| 120 | class CaptureDroppedDiagnostics { |
| 121 | Diagnostic &Diags; |
| 122 | StoredDiagnosticClient Client; |
| 123 | DiagnosticClient *PreviousClient; |
| 124 | |
| 125 | public: |
| 126 | CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags, |
| 127 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags) |
| 128 | : Diags(Diags), Client(StoredDiags), PreviousClient(Diags.getClient()) |
| 129 | { |
| 130 | if (RequestCapture || Diags.getClient() == 0) |
| 131 | Diags.setClient(&Client); |
| 132 | } |
| 133 | |
| 134 | ~CaptureDroppedDiagnostics() { |
| 135 | Diags.setClient(PreviousClient); |
| 136 | } |
| 137 | }; |
| 138 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 139 | } // anonymous namespace |
| 140 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 141 | void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level, |
| 142 | const DiagnosticInfo &Info) { |
| 143 | StoredDiags.push_back(StoredDiagnostic(Level, Info)); |
| 144 | } |
| 145 | |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 146 | const std::string &ASTUnit::getOriginalSourceFileName() { |
Daniel Dunbar | 68d40e2 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 147 | return OriginalSourceFile; |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 148 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 149 | |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 150 | const std::string &ASTUnit::getPCHFileName() { |
Daniel Dunbar | c7822db | 2009-12-02 21:47:43 +0000 | [diff] [blame] | 151 | assert(isMainFileAST() && "Not an ASTUnit from a PCH file!"); |
Benjamin Kramer | 7297c18 | 2010-01-30 16:23:25 +0000 | [diff] [blame] | 152 | return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 155 | ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 156 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
Ted Kremenek | 5cf4876 | 2009-10-17 00:34:24 +0000 | [diff] [blame] | 157 | bool OnlyLocalDecls, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 158 | RemappedFile *RemappedFiles, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 159 | unsigned NumRemappedFiles, |
| 160 | bool CaptureDiagnostics) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 161 | llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true)); |
| 162 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 163 | if (!Diags.getPtr()) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 164 | // No diagnostics engine was provided, so create our own diagnostics object |
| 165 | // with the default options. |
| 166 | DiagnosticOptions DiagOpts; |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 167 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 168 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 169 | |
| 170 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 171 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 172 | AST->Diagnostics = Diags; |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 173 | AST->FileMgr.reset(new FileManager); |
| 174 | AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics())); |
Steve Naroff | 36c4464 | 2009-10-19 14:34:22 +0000 | [diff] [blame] | 175 | AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager())); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 176 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 177 | // If requested, capture diagnostics in the ASTUnit. |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 178 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, AST->getDiagnostics(), |
Douglas Gregor | 405634b | 2010-04-05 18:10:21 +0000 | [diff] [blame] | 179 | AST->StoredDiagnostics); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 180 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 181 | for (unsigned I = 0; I != NumRemappedFiles; ++I) { |
| 182 | // Create the file entry for the file that we're mapping from. |
| 183 | const FileEntry *FromFile |
| 184 | = AST->getFileManager().getVirtualFile(RemappedFiles[I].first, |
| 185 | RemappedFiles[I].second->getBufferSize(), |
| 186 | 0); |
| 187 | if (!FromFile) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 188 | AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file) |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 189 | << RemappedFiles[I].first; |
Douglas Gregor | c8dfe5e | 2010-02-27 01:32:48 +0000 | [diff] [blame] | 190 | delete RemappedFiles[I].second; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 191 | continue; |
| 192 | } |
| 193 | |
| 194 | // Override the contents of the "from" file with the contents of |
| 195 | // the "to" file. |
| 196 | AST->getSourceManager().overrideFileContents(FromFile, |
| 197 | RemappedFiles[I].second); |
| 198 | } |
| 199 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 200 | // Gather Info for preprocessor construction later on. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 202 | LangOptions LangInfo; |
| 203 | HeaderSearch &HeaderInfo = *AST->HeaderInfo.get(); |
| 204 | std::string TargetTriple; |
| 205 | std::string Predefines; |
| 206 | unsigned Counter; |
| 207 | |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 208 | llvm::OwningPtr<PCHReader> Reader; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 209 | llvm::OwningPtr<ExternalASTSource> Source; |
| 210 | |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 211 | Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(), |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 212 | AST->getDiagnostics())); |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 213 | Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple, |
| 214 | Predefines, Counter)); |
| 215 | |
| 216 | switch (Reader->ReadPCH(Filename)) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 217 | case PCHReader::Success: |
| 218 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 220 | case PCHReader::Failure: |
Argyrios Kyrtzidis | 106c998 | 2009-06-25 18:22:30 +0000 | [diff] [blame] | 221 | case PCHReader::IgnorePCH: |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 222 | AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 223 | return NULL; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 224 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | |
Daniel Dunbar | 68d40e2 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 226 | AST->OriginalSourceFile = Reader->getOriginalSourceFile(); |
| 227 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 228 | // PCH loaded successfully. Now create the preprocessor. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 230 | // Get information about the target being compiled for. |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 231 | // |
| 232 | // FIXME: This is broken, we should store the TargetOptions in the PCH. |
| 233 | TargetOptions TargetOpts; |
| 234 | TargetOpts.ABI = ""; |
Charles Davis | 98b7c5c | 2010-06-11 01:06:47 +0000 | [diff] [blame] | 235 | TargetOpts.CXXABI = "itanium"; |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 236 | TargetOpts.CPU = ""; |
| 237 | TargetOpts.Features.clear(); |
| 238 | TargetOpts.Triple = TargetTriple; |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 239 | AST->Target.reset(TargetInfo::CreateTargetInfo(AST->getDiagnostics(), |
| 240 | TargetOpts)); |
| 241 | AST->PP.reset(new Preprocessor(AST->getDiagnostics(), LangInfo, |
| 242 | *AST->Target.get(), |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 243 | AST->getSourceManager(), HeaderInfo)); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 244 | Preprocessor &PP = *AST->PP.get(); |
| 245 | |
Daniel Dunbar | d5b6126 | 2009-09-21 03:03:47 +0000 | [diff] [blame] | 246 | PP.setPredefines(Reader->getSuggestedPredefines()); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 247 | PP.setCounterValue(Counter); |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 248 | Reader->setPreprocessor(PP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 250 | // Create and initialize the ASTContext. |
| 251 | |
| 252 | AST->Ctx.reset(new ASTContext(LangInfo, |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 253 | AST->getSourceManager(), |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 254 | *AST->Target.get(), |
| 255 | PP.getIdentifierTable(), |
| 256 | PP.getSelectorTable(), |
| 257 | PP.getBuiltinInfo(), |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 258 | /* size_reserve = */0)); |
| 259 | ASTContext &Context = *AST->Ctx.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 261 | Reader->InitializeContext(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 263 | // Attach the PCH reader to the AST context as an external AST |
| 264 | // source, so that declarations will be deserialized from the |
| 265 | // PCH file as needed. |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 266 | Source.reset(Reader.take()); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 267 | Context.setExternalSource(Source); |
| 268 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 269 | return AST.take(); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 270 | } |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 271 | |
| 272 | namespace { |
| 273 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 274 | class TopLevelDeclTrackerConsumer : public ASTConsumer { |
| 275 | ASTUnit &Unit; |
| 276 | |
| 277 | public: |
| 278 | TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {} |
| 279 | |
| 280 | void HandleTopLevelDecl(DeclGroupRef D) { |
Ted Kremenek | da5a428 | 2010-05-03 20:16:35 +0000 | [diff] [blame] | 281 | for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) { |
| 282 | Decl *D = *it; |
| 283 | // FIXME: Currently ObjC method declarations are incorrectly being |
| 284 | // reported as top-level declarations, even though their DeclContext |
| 285 | // is the containing ObjC @interface/@implementation. This is a |
| 286 | // fundamental problem in the parser right now. |
| 287 | if (isa<ObjCMethodDecl>(D)) |
| 288 | continue; |
| 289 | Unit.getTopLevelDecls().push_back(D); |
| 290 | } |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 291 | } |
| 292 | }; |
| 293 | |
| 294 | class TopLevelDeclTrackerAction : public ASTFrontendAction { |
| 295 | public: |
| 296 | ASTUnit &Unit; |
| 297 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 298 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 299 | llvm::StringRef InFile) { |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 300 | return new TopLevelDeclTrackerConsumer(Unit); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | public: |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 304 | TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {} |
| 305 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 306 | virtual bool hasCodeCompletionSupport() const { return false; } |
| 307 | }; |
| 308 | |
| 309 | } |
| 310 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 311 | /// Parse the source file into a translation unit using the given compiler |
| 312 | /// invocation, replacing the current translation unit. |
| 313 | /// |
| 314 | /// \returns True if a failure occurred that causes the ASTUnit not to |
| 315 | /// contain any translation-unit information, false otherwise. |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 316 | bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) { |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 317 | if (!Invocation.get()) |
| 318 | return true; |
| 319 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 320 | // Create the compiler instance to use for building the AST. |
Daniel Dunbar | cb6dda1 | 2009-12-02 08:43:56 +0000 | [diff] [blame] | 321 | CompilerInstance Clang; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 322 | Clang.setInvocation(Invocation.take()); |
| 323 | OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; |
| 324 | |
| 325 | // Set up diagnostics. |
| 326 | Clang.setDiagnostics(&getDiagnostics()); |
| 327 | Clang.setDiagnosticClient(getDiagnostics().getClient()); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 328 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 329 | // Create the target instance. |
| 330 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 331 | Clang.getTargetOpts())); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 332 | if (!Clang.hasTarget()) { |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 333 | Clang.takeDiagnosticClient(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 334 | return true; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 335 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 336 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 337 | // Inform the target of the language options. |
| 338 | // |
| 339 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 340 | // created. This complexity should be lifted elsewhere. |
| 341 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 342 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 343 | assert(Clang.getFrontendOpts().Inputs.size() == 1 && |
| 344 | "Invocation must have exactly one source file!"); |
Daniel Dunbar | c34ce3f | 2010-06-07 23:22:09 +0000 | [diff] [blame] | 345 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 346 | "FIXME: AST inputs not yet supported here!"); |
Daniel Dunbar | faddc3e | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 347 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
| 348 | "IR inputs not support here!"); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 349 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 350 | // Configure the various subsystems. |
| 351 | // FIXME: Should we retain the previous file manager? |
| 352 | FileMgr.reset(new FileManager); |
| 353 | SourceMgr.reset(new SourceManager(getDiagnostics())); |
| 354 | Ctx.reset(); |
| 355 | PP.reset(); |
| 356 | |
| 357 | // Clear out old caches and data. |
| 358 | TopLevelDecls.clear(); |
| 359 | StoredDiagnostics.clear(); |
| 360 | CleanTemporaryFiles(); |
| 361 | PreprocessedEntitiesByFile.clear(); |
| 362 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 363 | // Capture any diagnostics that would otherwise be dropped. |
| 364 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, |
| 365 | Clang.getDiagnostics(), |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 366 | StoredDiagnostics); |
| 367 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 368 | // Create a file manager object to provide access to and cache the filesystem. |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 369 | Clang.setFileManager(&getFileManager()); |
| 370 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 371 | // Create the source manager. |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 372 | Clang.setSourceManager(&getSourceManager()); |
| 373 | |
| 374 | llvm::OwningPtr<TopLevelDeclTrackerAction> Act; |
| 375 | Act.reset(new TopLevelDeclTrackerAction(*this)); |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 376 | if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, |
Daniel Dunbar | d3598a6 | 2010-06-07 23:23:06 +0000 | [diff] [blame] | 377 | Clang.getFrontendOpts().Inputs[0].first)) |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 378 | goto error; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 379 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 380 | Act->Execute(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 381 | |
Daniel Dunbar | 64a32ba | 2009-12-01 21:57:33 +0000 | [diff] [blame] | 382 | // Steal the created target, context, and preprocessor, and take back the |
| 383 | // source and file managers. |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 384 | Ctx.reset(Clang.takeASTContext()); |
| 385 | PP.reset(Clang.takePreprocessor()); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 386 | Clang.takeSourceManager(); |
| 387 | Clang.takeFileManager(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 388 | Target.reset(Clang.takeTarget()); |
| 389 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 390 | Act->EndSourceFile(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 391 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 392 | Clang.takeDiagnosticClient(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 393 | |
| 394 | Invocation.reset(Clang.takeInvocation()); |
| 395 | return false; |
| 396 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 397 | error: |
| 398 | Clang.takeSourceManager(); |
| 399 | Clang.takeFileManager(); |
| 400 | Clang.takeDiagnosticClient(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 401 | Invocation.reset(Clang.takeInvocation()); |
| 402 | return true; |
| 403 | } |
| 404 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 405 | /// \brief Simple function to retrieve a path for a preamble precompiled header. |
| 406 | static std::string GetPreamblePCHPath() { |
| 407 | // FIXME: This is lame; sys::Path should provide this function (in particular, |
| 408 | // it should know how to find the temporary files dir). |
| 409 | // FIXME: This is really lame. I copied this code from the Driver! |
| 410 | std::string Error; |
| 411 | const char *TmpDir = ::getenv("TMPDIR"); |
| 412 | if (!TmpDir) |
| 413 | TmpDir = ::getenv("TEMP"); |
| 414 | if (!TmpDir) |
| 415 | TmpDir = ::getenv("TMP"); |
| 416 | if (!TmpDir) |
| 417 | TmpDir = "/tmp"; |
| 418 | llvm::sys::Path P(TmpDir); |
| 419 | P.appendComponent("preamble"); |
| 420 | if (P.createTemporaryFileOnDisk()) |
| 421 | return std::string(); |
| 422 | |
| 423 | P.appendSuffix("pch"); |
| 424 | return P.str(); |
| 425 | } |
| 426 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 427 | /// \brief Compute the preamble for the main file, providing |
| 428 | std::pair<llvm::MemoryBuffer *, unsigned> |
| 429 | ASTUnit::ComputePreamble(CompilerInvocation &Invocation, bool &CreatedBuffer) { |
| 430 | FrontendOptions &FrontendOpts = Invocation.getFrontendOpts(); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 431 | PreprocessorOptions &PreprocessorOpts |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 432 | = Invocation.getPreprocessorOpts(); |
| 433 | CreatedBuffer = false; |
| 434 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 435 | // Try to determine if the main file has been remapped, either from the |
| 436 | // command line (to another file) or directly through the compiler invocation |
| 437 | // (to a memory buffer). |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 438 | llvm::MemoryBuffer *Buffer = 0; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 439 | llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second); |
| 440 | if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) { |
| 441 | // Check whether there is a file-file remapping of the main file |
| 442 | for (PreprocessorOptions::remapped_file_iterator |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 443 | M = PreprocessorOpts.remapped_file_begin(), |
| 444 | E = PreprocessorOpts.remapped_file_end(); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 445 | M != E; |
| 446 | ++M) { |
| 447 | llvm::sys::PathWithStatus MPath(M->first); |
| 448 | if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { |
| 449 | if (MainFileStatus->uniqueID == MStatus->uniqueID) { |
| 450 | // We found a remapping. Try to load the resulting, remapped source. |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 451 | if (CreatedBuffer) { |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 452 | delete Buffer; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 453 | CreatedBuffer = false; |
| 454 | } |
| 455 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 456 | Buffer = llvm::MemoryBuffer::getFile(M->second); |
| 457 | if (!Buffer) |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 458 | return std::make_pair((llvm::MemoryBuffer*)0, 0); |
| 459 | CreatedBuffer = true; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 460 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 461 | // Remove this remapping. We've captured the buffer already. |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 462 | M = PreprocessorOpts.eraseRemappedFile(M); |
| 463 | E = PreprocessorOpts.remapped_file_end(); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | // Check whether there is a file-buffer remapping. It supercedes the |
| 469 | // file-file remapping. |
| 470 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 471 | M = PreprocessorOpts.remapped_file_buffer_begin(), |
| 472 | E = PreprocessorOpts.remapped_file_buffer_end(); |
| 473 | M != E; |
| 474 | ++M) { |
| 475 | llvm::sys::PathWithStatus MPath(M->first); |
| 476 | if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { |
| 477 | if (MainFileStatus->uniqueID == MStatus->uniqueID) { |
| 478 | // We found a remapping. |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 479 | if (CreatedBuffer) { |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 480 | delete Buffer; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 481 | CreatedBuffer = false; |
| 482 | } |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 483 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 484 | Buffer = const_cast<llvm::MemoryBuffer *>(M->second); |
| 485 | |
| 486 | // Remove this remapping. We've captured the buffer already. |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 487 | M = PreprocessorOpts.eraseRemappedFile(M); |
| 488 | E = PreprocessorOpts.remapped_file_buffer_end(); |
| 489 | } |
| 490 | } |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 491 | } |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // If the main source file was not remapped, load it now. |
| 495 | if (!Buffer) { |
| 496 | Buffer = llvm::MemoryBuffer::getFile(FrontendOpts.Inputs[0].second); |
| 497 | if (!Buffer) |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 498 | return std::make_pair((llvm::MemoryBuffer*)0, 0); |
| 499 | |
| 500 | CreatedBuffer = true; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 503 | return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer)); |
| 504 | } |
| 505 | |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 506 | static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old, |
| 507 | bool DeleteOld, |
| 508 | unsigned NewSize, |
| 509 | llvm::StringRef NewName) { |
| 510 | llvm::MemoryBuffer *Result |
| 511 | = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName); |
| 512 | memcpy(const_cast<char*>(Result->getBufferStart()), |
| 513 | Old->getBufferStart(), Old->getBufferSize()); |
| 514 | memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(), |
Douglas Gregor | 592508e | 2010-07-24 00:42:07 +0000 | [diff] [blame] | 515 | ' ', NewSize - Old->getBufferSize() - 2); |
| 516 | const_cast<char*>(Result->getBufferEnd())[-2] = '\n'; |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 517 | const_cast<char*>(Result->getBufferEnd())[-1] = 0; |
| 518 | |
| 519 | if (DeleteOld) |
| 520 | delete Old; |
| 521 | |
| 522 | return Result; |
| 523 | } |
| 524 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 525 | /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing |
| 526 | /// the source file. |
| 527 | /// |
| 528 | /// This routine will compute the preamble of the main source file. If a |
| 529 | /// non-trivial preamble is found, it will precompile that preamble into a |
| 530 | /// precompiled header so that the precompiled preamble can be used to reduce |
| 531 | /// reparsing time. If a precompiled preamble has already been constructed, |
| 532 | /// this routine will determine if it is still valid and, if so, avoid |
| 533 | /// rebuilding the precompiled preamble. |
| 534 | /// |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 535 | /// \returns If the precompiled preamble can be used, returns a newly-allocated |
| 536 | /// buffer that should be used in place of the main file when doing so. |
| 537 | /// Otherwise, returns a NULL pointer. |
| 538 | llvm::MemoryBuffer *ASTUnit::BuildPrecompiledPreamble() { |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 539 | CompilerInvocation PreambleInvocation(*Invocation); |
| 540 | FrontendOptions &FrontendOpts = PreambleInvocation.getFrontendOpts(); |
| 541 | PreprocessorOptions &PreprocessorOpts |
| 542 | = PreambleInvocation.getPreprocessorOpts(); |
| 543 | |
| 544 | bool CreatedPreambleBuffer = false; |
| 545 | std::pair<llvm::MemoryBuffer *, unsigned> NewPreamble |
| 546 | = ComputePreamble(PreambleInvocation, CreatedPreambleBuffer); |
| 547 | |
| 548 | if (!NewPreamble.second) { |
| 549 | // We couldn't find a preamble in the main source. Clear out the current |
| 550 | // preamble, if we have one. It's obviously no good any more. |
| 551 | Preamble.clear(); |
| 552 | if (!PreambleFile.empty()) { |
| 553 | PreambleFile.eraseFromDisk(); |
| 554 | PreambleFile.clear(); |
| 555 | } |
| 556 | if (CreatedPreambleBuffer) |
| 557 | delete NewPreamble.first; |
| 558 | |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 559 | return 0; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | if (!Preamble.empty()) { |
| 563 | // We've previously computed a preamble. Check whether we have the same |
| 564 | // preamble now that we did before, and that there's enough space in |
| 565 | // the main-file buffer within the precompiled preamble to fit the |
| 566 | // new main file. |
| 567 | if (Preamble.size() == NewPreamble.second && |
Douglas Gregor | 592508e | 2010-07-24 00:42:07 +0000 | [diff] [blame] | 568 | NewPreamble.first->getBufferSize() < PreambleReservedSize-2 && |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 569 | memcmp(&Preamble[0], NewPreamble.first->getBufferStart(), |
| 570 | NewPreamble.second) == 0) { |
| 571 | // The preamble has not changed. We may be able to re-use the precompiled |
| 572 | // preamble. |
| 573 | // FIXME: Check that none of the files used by the preamble have changed. |
| 574 | |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 575 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 576 | // Okay! Re-use the precompiled preamble. |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 577 | return CreatePaddedMainFileBuffer(NewPreamble.first, |
| 578 | CreatedPreambleBuffer, |
| 579 | PreambleReservedSize, |
| 580 | FrontendOpts.Inputs[0].second); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | // We can't reuse the previously-computed preamble. Build a new one. |
| 584 | Preamble.clear(); |
| 585 | PreambleFile.eraseFromDisk(); |
| 586 | } |
| 587 | |
| 588 | // We did not previously compute a preamble, or it can't be reused anyway. |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 589 | |
| 590 | // Create a new buffer that stores the preamble. The buffer also contains |
| 591 | // extra space for the original contents of the file (which will be present |
| 592 | // when we actually parse the file) along with more room in case the file |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 593 | // grows. |
| 594 | PreambleReservedSize = NewPreamble.first->getBufferSize(); |
| 595 | if (PreambleReservedSize < 4096) |
| 596 | PreambleReservedSize = 8192; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 597 | else |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 598 | PreambleReservedSize *= 2; |
| 599 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 600 | llvm::MemoryBuffer *PreambleBuffer |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 601 | = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize, |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 602 | FrontendOpts.Inputs[0].second); |
| 603 | memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()), |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 604 | NewPreamble.first->getBufferStart(), Preamble.size()); |
| 605 | memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(), |
Douglas Gregor | 592508e | 2010-07-24 00:42:07 +0000 | [diff] [blame] | 606 | ' ', PreambleReservedSize - Preamble.size() - 2); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 607 | const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = 0; |
Douglas Gregor | 592508e | 2010-07-24 00:42:07 +0000 | [diff] [blame] | 608 | const_cast<char*>(PreambleBuffer->getBufferEnd())[-2] = '\n'; |
| 609 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 610 | // Save the preamble text for later; we'll need to compare against it for |
| 611 | // subsequent reparses. |
| 612 | Preamble.assign(NewPreamble.first->getBufferStart(), |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 613 | NewPreamble.first->getBufferStart() + NewPreamble.second); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 614 | |
| 615 | // Remap the main source file to the preamble buffer. |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 616 | llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 617 | PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer); |
| 618 | |
| 619 | // Tell the compiler invocation to generate a temporary precompiled header. |
| 620 | FrontendOpts.ProgramAction = frontend::GeneratePCH; |
| 621 | // FIXME: Set ChainedPCH, once it is ready. |
| 622 | // FIXME: Generate the precompiled header into memory? |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 623 | if (PreambleFile.isEmpty()) |
| 624 | FrontendOpts.OutputFile = GetPreamblePCHPath(); |
| 625 | else |
| 626 | FrontendOpts.OutputFile = PreambleFile.str(); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 627 | |
| 628 | // Create the compiler instance to use for building the precompiled preamble. |
| 629 | CompilerInstance Clang; |
| 630 | Clang.setInvocation(&PreambleInvocation); |
| 631 | OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; |
| 632 | |
| 633 | // Set up diagnostics. |
| 634 | Clang.setDiagnostics(&getDiagnostics()); |
| 635 | Clang.setDiagnosticClient(getDiagnostics().getClient()); |
| 636 | |
| 637 | // Create the target instance. |
| 638 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 639 | Clang.getTargetOpts())); |
| 640 | if (!Clang.hasTarget()) { |
| 641 | Clang.takeDiagnosticClient(); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 642 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 643 | Preamble.clear(); |
| 644 | if (CreatedPreambleBuffer) |
| 645 | delete NewPreamble.first; |
| 646 | |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 647 | return 0; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | // Inform the target of the language options. |
| 651 | // |
| 652 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 653 | // created. This complexity should be lifted elsewhere. |
| 654 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
| 655 | |
| 656 | assert(Clang.getFrontendOpts().Inputs.size() == 1 && |
| 657 | "Invocation must have exactly one source file!"); |
| 658 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && |
| 659 | "FIXME: AST inputs not yet supported here!"); |
| 660 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
| 661 | "IR inputs not support here!"); |
| 662 | |
| 663 | // Clear out old caches and data. |
| 664 | StoredDiagnostics.clear(); |
| 665 | |
| 666 | // Capture any diagnostics that would otherwise be dropped. |
| 667 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, |
| 668 | Clang.getDiagnostics(), |
| 669 | StoredDiagnostics); |
| 670 | |
| 671 | // Create a file manager object to provide access to and cache the filesystem. |
| 672 | Clang.setFileManager(new FileManager); |
| 673 | |
| 674 | // Create the source manager. |
| 675 | Clang.setSourceManager(new SourceManager(getDiagnostics())); |
| 676 | |
| 677 | // FIXME: Eventually, we'll have to track top-level declarations here, too. |
| 678 | llvm::OwningPtr<GeneratePCHAction> Act; |
| 679 | Act.reset(new GeneratePCHAction); |
| 680 | if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, |
| 681 | Clang.getFrontendOpts().Inputs[0].first)) { |
| 682 | Clang.takeDiagnosticClient(); |
| 683 | Clang.takeInvocation(); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 684 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 685 | Preamble.clear(); |
| 686 | if (CreatedPreambleBuffer) |
| 687 | delete NewPreamble.first; |
| 688 | |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 689 | return 0; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | Act->Execute(); |
| 693 | Act->EndSourceFile(); |
| 694 | Clang.takeDiagnosticClient(); |
| 695 | Clang.takeInvocation(); |
| 696 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 697 | if (Diagnostics->getNumErrors() > 0) { |
| 698 | // There were errors parsing the preamble, so no precompiled header was |
| 699 | // generated. Forget that we even tried. |
| 700 | // FIXME: Should we leave a note for ourselves to try again? |
| 701 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 702 | Preamble.clear(); |
| 703 | if (CreatedPreambleBuffer) |
| 704 | delete NewPreamble.first; |
| 705 | |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 706 | return 0; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | // Keep track of the preamble we precompiled. |
| 710 | PreambleFile = FrontendOpts.OutputFile; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 711 | fprintf(stderr, "Preamble PCH: %s\n", FrontendOpts.OutputFile.c_str()); |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 712 | return CreatePaddedMainFileBuffer(NewPreamble.first, |
| 713 | CreatedPreambleBuffer, |
| 714 | PreambleReservedSize, |
| 715 | FrontendOpts.Inputs[0].second); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 716 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 717 | |
| 718 | ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI, |
| 719 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
| 720 | bool OnlyLocalDecls, |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 721 | bool CaptureDiagnostics, |
| 722 | bool PrecompilePreamble) { |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 723 | if (!Diags.getPtr()) { |
| 724 | // No diagnostics engine was provided, so create our own diagnostics object |
| 725 | // with the default options. |
| 726 | DiagnosticOptions DiagOpts; |
| 727 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
| 728 | } |
| 729 | |
| 730 | // Create the AST unit. |
| 731 | llvm::OwningPtr<ASTUnit> AST; |
| 732 | AST.reset(new ASTUnit(false)); |
| 733 | AST->Diagnostics = Diags; |
| 734 | AST->CaptureDiagnostics = CaptureDiagnostics; |
| 735 | AST->OnlyLocalDecls = OnlyLocalDecls; |
| 736 | AST->Invocation.reset(CI); |
| 737 | |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 738 | llvm::MemoryBuffer *OverrideMainBuffer = 0; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 739 | if (PrecompilePreamble) |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 740 | OverrideMainBuffer = AST->BuildPrecompiledPreamble(); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 742 | if (!AST->Parse(OverrideMainBuffer)) |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 743 | return AST.take(); |
| 744 | |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 745 | delete OverrideMainBuffer; |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 746 | return 0; |
| 747 | } |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 748 | |
| 749 | ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin, |
| 750 | const char **ArgEnd, |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 751 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 752 | llvm::StringRef ResourceFilesPath, |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 753 | bool OnlyLocalDecls, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 754 | RemappedFile *RemappedFiles, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 755 | unsigned NumRemappedFiles, |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 756 | bool CaptureDiagnostics, |
| 757 | bool PrecompilePreamble) { |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 758 | if (!Diags.getPtr()) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 759 | // No diagnostics engine was provided, so create our own diagnostics object |
| 760 | // with the default options. |
| 761 | DiagnosticOptions DiagOpts; |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 762 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 765 | llvm::SmallVector<const char *, 16> Args; |
| 766 | Args.push_back("<clang>"); // FIXME: Remove dummy argument. |
| 767 | Args.insert(Args.end(), ArgBegin, ArgEnd); |
| 768 | |
| 769 | // FIXME: Find a cleaner way to force the driver into restricted modes. We |
| 770 | // also want to force it to use clang. |
| 771 | Args.push_back("-fsyntax-only"); |
| 772 | |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 773 | // FIXME: We shouldn't have to pass in the path info. |
Daniel Dunbar | 0bbad51 | 2010-07-19 00:44:04 +0000 | [diff] [blame] | 774 | driver::Driver TheDriver("clang", llvm::sys::getHostTriple(), |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 775 | "a.out", false, false, *Diags); |
Daniel Dunbar | 3bd54cc | 2010-01-25 00:44:02 +0000 | [diff] [blame] | 776 | |
| 777 | // Don't check that inputs exist, they have been remapped. |
| 778 | TheDriver.setCheckInputsExist(false); |
| 779 | |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 780 | llvm::OwningPtr<driver::Compilation> C( |
| 781 | TheDriver.BuildCompilation(Args.size(), Args.data())); |
| 782 | |
| 783 | // We expect to get back exactly one command job, if we didn't something |
| 784 | // failed. |
| 785 | const driver::JobList &Jobs = C->getJobs(); |
| 786 | if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) { |
| 787 | llvm::SmallString<256> Msg; |
| 788 | llvm::raw_svector_ostream OS(Msg); |
| 789 | C->PrintJob(OS, C->getJobs(), "; ", true); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 790 | Diags->Report(diag::err_fe_expected_compiler_job) << OS.str(); |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin()); |
| 795 | if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 796 | Diags->Report(diag::err_fe_expected_clang_command); |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | const driver::ArgStringList &CCArgs = Cmd->getArguments(); |
Daniel Dunbar | 807b061 | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 801 | llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation); |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 802 | CompilerInvocation::CreateFromArgs(*CI, |
| 803 | const_cast<const char **>(CCArgs.data()), |
| 804 | const_cast<const char **>(CCArgs.data()) + |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 805 | CCArgs.size(), |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 806 | *Diags); |
Daniel Dunbar | 1e69fe3 | 2009-12-13 03:45:58 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 808 | // Override any files that need remapping |
| 809 | for (unsigned I = 0; I != NumRemappedFiles; ++I) |
Daniel Dunbar | 807b061 | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 810 | CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, |
Daniel Dunbar | b26d483 | 2010-02-16 01:55:04 +0000 | [diff] [blame] | 811 | RemappedFiles[I].second); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 812 | |
Daniel Dunbar | 8b9adfe | 2009-12-15 00:06:45 +0000 | [diff] [blame] | 813 | // Override the resources path. |
Daniel Dunbar | 807b061 | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 814 | CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 815 | |
Daniel Dunbar | b26d483 | 2010-02-16 01:55:04 +0000 | [diff] [blame] | 816 | CI->getFrontendOpts().DisableFree = true; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 817 | return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls, |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 818 | CaptureDiagnostics, PrecompilePreamble); |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 819 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 820 | |
| 821 | bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) { |
| 822 | if (!Invocation.get()) |
| 823 | return true; |
| 824 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 825 | // If we have a preamble file lying around, build or reuse the precompiled |
| 826 | // preamble. |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 827 | llvm::MemoryBuffer *OverrideMainBuffer = 0; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 828 | if (!PreambleFile.empty()) |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 829 | OverrideMainBuffer = BuildPrecompiledPreamble(); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 830 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 831 | // Clear out the diagnostics state. |
| 832 | getDiagnostics().Reset(); |
| 833 | |
| 834 | // Remap files. |
| 835 | Invocation->getPreprocessorOpts().clearRemappedFiles(); |
| 836 | for (unsigned I = 0; I != NumRemappedFiles; ++I) |
| 837 | Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 838 | RemappedFiles[I].second); |
| 839 | |
| 840 | // Parse the sources |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 841 | bool Result = Parse(OverrideMainBuffer); |
| 842 | delete OverrideMainBuffer; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 843 | return Result; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 844 | } |