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