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