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