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