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