Argyrios Kyrtzidis | 4b562cf | 2009-06-20 08:27:14 +0000 | [diff] [blame] | 1 | //===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // ASTUnit Implementation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTUnit.h" |
| 15 | #include "clang/Frontend/PCHReader.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclVisitor.h" |
| 19 | #include "clang/AST/StmtVisitor.h" |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 20 | #include "clang/Driver/Compilation.h" |
| 21 | #include "clang/Driver/Driver.h" |
| 22 | #include "clang/Driver/Job.h" |
| 23 | #include "clang/Driver/Tool.h" |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/CompilerInstance.h" |
| 25 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/FrontendOptions.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 28 | #include "clang/Lex/HeaderSearch.h" |
| 29 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetOptions.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetInfo.h" |
| 32 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 34 | #include "llvm/System/Host.h" |
Benjamin Kramer | 4a630d3 | 2009-10-18 11:34:14 +0000 | [diff] [blame] | 35 | #include "llvm/System/Path.h" |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 36 | #include <cstdlib> |
Zhongxing Xu | ad23ebe | 2010-07-23 02:15:08 +0000 | [diff] [blame^] | 37 | #include <cstdio> |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 38 | using namespace clang; |
| 39 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 40 | PrecompiledPreamble::~PrecompiledPreamble() { |
| 41 | PreambleFile.eraseFromDisk(); |
| 42 | } |
| 43 | |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 44 | ASTUnit::ASTUnit(bool _MainFileIsAST) |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 45 | : CaptureDiagnostics(false), MainFileIsAST(_MainFileIsAST), |
| 46 | ConcurrencyCheckValue(CheckUnlocked) { } |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 47 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 48 | ASTUnit::~ASTUnit() { |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 49 | ConcurrencyCheckValue = CheckLocked; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 50 | CleanTemporaryFiles(); |
| 51 | } |
| 52 | |
| 53 | void ASTUnit::CleanTemporaryFiles() { |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 54 | for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) |
| 55 | TemporaryFiles[I].eraseFromDisk(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 56 | TemporaryFiles.clear(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 57 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 58 | |
| 59 | namespace { |
| 60 | |
| 61 | /// \brief Gathers information from PCHReader that will be used to initialize |
| 62 | /// a Preprocessor. |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 63 | class PCHInfoCollector : public PCHReaderListener { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 64 | LangOptions &LangOpt; |
| 65 | HeaderSearch &HSI; |
| 66 | std::string &TargetTriple; |
| 67 | std::string &Predefines; |
| 68 | unsigned &Counter; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 70 | unsigned NumHeaderInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 72 | public: |
| 73 | PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI, |
| 74 | std::string &TargetTriple, std::string &Predefines, |
| 75 | unsigned &Counter) |
| 76 | : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple), |
| 77 | Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 79 | virtual bool ReadLanguageOptions(const LangOptions &LangOpts) { |
| 80 | LangOpt = LangOpts; |
| 81 | return false; |
| 82 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
Daniel Dunbar | dc3c0d2 | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 84 | virtual bool ReadTargetTriple(llvm::StringRef Triple) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 85 | TargetTriple = Triple; |
| 86 | return false; |
| 87 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
Sebastian Redl | cb481aa | 2010-07-14 23:29:55 +0000 | [diff] [blame] | 89 | virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 90 | llvm::StringRef OriginalFileName, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 91 | std::string &SuggestedPredefines) { |
Sebastian Redl | cb481aa | 2010-07-14 23:29:55 +0000 | [diff] [blame] | 92 | Predefines = Buffers[0].Data; |
| 93 | for (unsigned I = 1, N = Buffers.size(); I != N; ++I) { |
| 94 | Predefines += Buffers[I].Data; |
| 95 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 96 | return false; |
| 97 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | ec1afbf | 2010-03-16 19:09:18 +0000 | [diff] [blame] | 99 | virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 100 | HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++); |
| 101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 103 | virtual void ReadCounter(unsigned Value) { |
| 104 | Counter = Value; |
| 105 | } |
| 106 | }; |
| 107 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 108 | class StoredDiagnosticClient : public DiagnosticClient { |
| 109 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags; |
| 110 | |
| 111 | public: |
| 112 | explicit StoredDiagnosticClient( |
| 113 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags) |
| 114 | : StoredDiags(StoredDiags) { } |
| 115 | |
| 116 | virtual void HandleDiagnostic(Diagnostic::Level Level, |
| 117 | const DiagnosticInfo &Info); |
| 118 | }; |
| 119 | |
| 120 | /// \brief RAII object that optionally captures diagnostics, if |
| 121 | /// there is no diagnostic client to capture them already. |
| 122 | class CaptureDroppedDiagnostics { |
| 123 | Diagnostic &Diags; |
| 124 | StoredDiagnosticClient Client; |
| 125 | DiagnosticClient *PreviousClient; |
| 126 | |
| 127 | public: |
| 128 | CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags, |
| 129 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags) |
| 130 | : Diags(Diags), Client(StoredDiags), PreviousClient(Diags.getClient()) |
| 131 | { |
| 132 | if (RequestCapture || Diags.getClient() == 0) |
| 133 | Diags.setClient(&Client); |
| 134 | } |
| 135 | |
| 136 | ~CaptureDroppedDiagnostics() { |
| 137 | Diags.setClient(PreviousClient); |
| 138 | } |
| 139 | }; |
| 140 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 141 | } // anonymous namespace |
| 142 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 143 | void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level, |
| 144 | const DiagnosticInfo &Info) { |
| 145 | StoredDiags.push_back(StoredDiagnostic(Level, Info)); |
| 146 | } |
| 147 | |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 148 | const std::string &ASTUnit::getOriginalSourceFileName() { |
Daniel Dunbar | 68d40e2 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 149 | return OriginalSourceFile; |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 150 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 151 | |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 152 | const std::string &ASTUnit::getPCHFileName() { |
Daniel Dunbar | c7822db | 2009-12-02 21:47:43 +0000 | [diff] [blame] | 153 | assert(isMainFileAST() && "Not an ASTUnit from a PCH file!"); |
Benjamin Kramer | 7297c18 | 2010-01-30 16:23:25 +0000 | [diff] [blame] | 154 | return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 157 | ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 158 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
Ted Kremenek | 5cf4876 | 2009-10-17 00:34:24 +0000 | [diff] [blame] | 159 | bool OnlyLocalDecls, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 160 | RemappedFile *RemappedFiles, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 161 | unsigned NumRemappedFiles, |
| 162 | bool CaptureDiagnostics) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 163 | llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true)); |
| 164 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 165 | if (!Diags.getPtr()) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 166 | // No diagnostics engine was provided, so create our own diagnostics object |
| 167 | // with the default options. |
| 168 | DiagnosticOptions DiagOpts; |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 169 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 170 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 171 | |
| 172 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 173 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 174 | AST->Diagnostics = Diags; |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 175 | AST->FileMgr.reset(new FileManager); |
| 176 | AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics())); |
Steve Naroff | 36c4464 | 2009-10-19 14:34:22 +0000 | [diff] [blame] | 177 | AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager())); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 178 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 179 | // If requested, capture diagnostics in the ASTUnit. |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 180 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, AST->getDiagnostics(), |
Douglas Gregor | 405634b | 2010-04-05 18:10:21 +0000 | [diff] [blame] | 181 | AST->StoredDiagnostics); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 182 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 183 | for (unsigned I = 0; I != NumRemappedFiles; ++I) { |
| 184 | // Create the file entry for the file that we're mapping from. |
| 185 | const FileEntry *FromFile |
| 186 | = AST->getFileManager().getVirtualFile(RemappedFiles[I].first, |
| 187 | RemappedFiles[I].second->getBufferSize(), |
| 188 | 0); |
| 189 | if (!FromFile) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 190 | AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file) |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 191 | << RemappedFiles[I].first; |
Douglas Gregor | c8dfe5e | 2010-02-27 01:32:48 +0000 | [diff] [blame] | 192 | delete RemappedFiles[I].second; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 193 | continue; |
| 194 | } |
| 195 | |
| 196 | // Override the contents of the "from" file with the contents of |
| 197 | // the "to" file. |
| 198 | AST->getSourceManager().overrideFileContents(FromFile, |
| 199 | RemappedFiles[I].second); |
| 200 | } |
| 201 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 202 | // Gather Info for preprocessor construction later on. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 204 | LangOptions LangInfo; |
| 205 | HeaderSearch &HeaderInfo = *AST->HeaderInfo.get(); |
| 206 | std::string TargetTriple; |
| 207 | std::string Predefines; |
| 208 | unsigned Counter; |
| 209 | |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 210 | llvm::OwningPtr<PCHReader> Reader; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 211 | llvm::OwningPtr<ExternalASTSource> Source; |
| 212 | |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 213 | Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(), |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 214 | AST->getDiagnostics())); |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 215 | Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple, |
| 216 | Predefines, Counter)); |
| 217 | |
| 218 | switch (Reader->ReadPCH(Filename)) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 219 | case PCHReader::Success: |
| 220 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 222 | case PCHReader::Failure: |
Argyrios Kyrtzidis | 106c998 | 2009-06-25 18:22:30 +0000 | [diff] [blame] | 223 | case PCHReader::IgnorePCH: |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 224 | AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 225 | return NULL; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 226 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Daniel Dunbar | 68d40e2 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 228 | AST->OriginalSourceFile = Reader->getOriginalSourceFile(); |
| 229 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 230 | // PCH loaded successfully. Now create the preprocessor. |
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 | // Get information about the target being compiled for. |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 233 | // |
| 234 | // FIXME: This is broken, we should store the TargetOptions in the PCH. |
| 235 | TargetOptions TargetOpts; |
| 236 | TargetOpts.ABI = ""; |
Charles Davis | 98b7c5c | 2010-06-11 01:06:47 +0000 | [diff] [blame] | 237 | TargetOpts.CXXABI = "itanium"; |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 238 | TargetOpts.CPU = ""; |
| 239 | TargetOpts.Features.clear(); |
| 240 | TargetOpts.Triple = TargetTriple; |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 241 | AST->Target.reset(TargetInfo::CreateTargetInfo(AST->getDiagnostics(), |
| 242 | TargetOpts)); |
| 243 | AST->PP.reset(new Preprocessor(AST->getDiagnostics(), LangInfo, |
| 244 | *AST->Target.get(), |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 245 | AST->getSourceManager(), HeaderInfo)); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 246 | Preprocessor &PP = *AST->PP.get(); |
| 247 | |
Daniel Dunbar | d5b6126 | 2009-09-21 03:03:47 +0000 | [diff] [blame] | 248 | PP.setPredefines(Reader->getSuggestedPredefines()); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 249 | PP.setCounterValue(Counter); |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 250 | Reader->setPreprocessor(PP); |
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 | // Create and initialize the ASTContext. |
| 253 | |
| 254 | AST->Ctx.reset(new ASTContext(LangInfo, |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 255 | AST->getSourceManager(), |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 256 | *AST->Target.get(), |
| 257 | PP.getIdentifierTable(), |
| 258 | PP.getSelectorTable(), |
| 259 | PP.getBuiltinInfo(), |
Daniel Dunbar | b26d483 | 2010-02-16 01:55:04 +0000 | [diff] [blame] | 260 | /* FreeMemory = */ false, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 261 | /* size_reserve = */0)); |
| 262 | ASTContext &Context = *AST->Ctx.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 264 | Reader->InitializeContext(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 266 | // Attach the PCH reader to the AST context as an external AST |
| 267 | // source, so that declarations will be deserialized from the |
| 268 | // PCH file as needed. |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 269 | Source.reset(Reader.take()); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 270 | Context.setExternalSource(Source); |
| 271 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | return AST.take(); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 273 | } |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 274 | |
| 275 | namespace { |
| 276 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 277 | class TopLevelDeclTrackerConsumer : public ASTConsumer { |
| 278 | ASTUnit &Unit; |
| 279 | |
| 280 | public: |
| 281 | TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {} |
| 282 | |
| 283 | void HandleTopLevelDecl(DeclGroupRef D) { |
Ted Kremenek | da5a428 | 2010-05-03 20:16:35 +0000 | [diff] [blame] | 284 | for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) { |
| 285 | Decl *D = *it; |
| 286 | // FIXME: Currently ObjC method declarations are incorrectly being |
| 287 | // reported as top-level declarations, even though their DeclContext |
| 288 | // is the containing ObjC @interface/@implementation. This is a |
| 289 | // fundamental problem in the parser right now. |
| 290 | if (isa<ObjCMethodDecl>(D)) |
| 291 | continue; |
| 292 | Unit.getTopLevelDecls().push_back(D); |
| 293 | } |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 294 | } |
| 295 | }; |
| 296 | |
| 297 | class TopLevelDeclTrackerAction : public ASTFrontendAction { |
| 298 | public: |
| 299 | ASTUnit &Unit; |
| 300 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 301 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 302 | llvm::StringRef InFile) { |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 303 | return new TopLevelDeclTrackerConsumer(Unit); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | public: |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 307 | TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {} |
| 308 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 309 | virtual bool hasCodeCompletionSupport() const { return false; } |
| 310 | }; |
| 311 | |
| 312 | } |
| 313 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 314 | /// Parse the source file into a translation unit using the given compiler |
| 315 | /// invocation, replacing the current translation unit. |
| 316 | /// |
| 317 | /// \returns True if a failure occurred that causes the ASTUnit not to |
| 318 | /// contain any translation-unit information, false otherwise. |
| 319 | bool ASTUnit::Parse() { |
| 320 | if (!Invocation.get()) |
| 321 | return true; |
| 322 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 323 | // Create the compiler instance to use for building the AST. |
Daniel Dunbar | cb6dda1 | 2009-12-02 08:43:56 +0000 | [diff] [blame] | 324 | CompilerInstance Clang; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 325 | Clang.setInvocation(Invocation.take()); |
| 326 | OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; |
| 327 | |
| 328 | // Set up diagnostics. |
| 329 | Clang.setDiagnostics(&getDiagnostics()); |
| 330 | Clang.setDiagnosticClient(getDiagnostics().getClient()); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 331 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 332 | // Create the target instance. |
| 333 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 334 | Clang.getTargetOpts())); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 335 | if (!Clang.hasTarget()) { |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 336 | Clang.takeDiagnosticClient(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 337 | return true; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 338 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 339 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 340 | // Inform the target of the language options. |
| 341 | // |
| 342 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 343 | // created. This complexity should be lifted elsewhere. |
| 344 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 345 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 346 | assert(Clang.getFrontendOpts().Inputs.size() == 1 && |
| 347 | "Invocation must have exactly one source file!"); |
Daniel Dunbar | c34ce3f | 2010-06-07 23:22:09 +0000 | [diff] [blame] | 348 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 349 | "FIXME: AST inputs not yet supported here!"); |
Daniel Dunbar | faddc3e | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 350 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
| 351 | "IR inputs not support here!"); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 352 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 353 | // Configure the various subsystems. |
| 354 | // FIXME: Should we retain the previous file manager? |
| 355 | FileMgr.reset(new FileManager); |
| 356 | SourceMgr.reset(new SourceManager(getDiagnostics())); |
| 357 | Ctx.reset(); |
| 358 | PP.reset(); |
| 359 | |
| 360 | // Clear out old caches and data. |
| 361 | TopLevelDecls.clear(); |
| 362 | StoredDiagnostics.clear(); |
| 363 | CleanTemporaryFiles(); |
| 364 | PreprocessedEntitiesByFile.clear(); |
| 365 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 366 | // Capture any diagnostics that would otherwise be dropped. |
| 367 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, |
| 368 | Clang.getDiagnostics(), |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 369 | StoredDiagnostics); |
| 370 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 371 | // 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] | 372 | Clang.setFileManager(&getFileManager()); |
| 373 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 374 | // Create the source manager. |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 375 | Clang.setSourceManager(&getSourceManager()); |
| 376 | |
| 377 | llvm::OwningPtr<TopLevelDeclTrackerAction> Act; |
| 378 | Act.reset(new TopLevelDeclTrackerAction(*this)); |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 379 | if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, |
Daniel Dunbar | d3598a6 | 2010-06-07 23:23:06 +0000 | [diff] [blame] | 380 | Clang.getFrontendOpts().Inputs[0].first)) |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 381 | goto error; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 382 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 383 | Act->Execute(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 384 | |
Daniel Dunbar | 64a32ba | 2009-12-01 21:57:33 +0000 | [diff] [blame] | 385 | // Steal the created target, context, and preprocessor, and take back the |
| 386 | // source and file managers. |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 387 | Ctx.reset(Clang.takeASTContext()); |
| 388 | PP.reset(Clang.takePreprocessor()); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 389 | Clang.takeSourceManager(); |
| 390 | Clang.takeFileManager(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 391 | Target.reset(Clang.takeTarget()); |
| 392 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 393 | Act->EndSourceFile(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 394 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 395 | Clang.takeDiagnosticClient(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 396 | |
| 397 | Invocation.reset(Clang.takeInvocation()); |
| 398 | return false; |
| 399 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 400 | error: |
| 401 | Clang.takeSourceManager(); |
| 402 | Clang.takeFileManager(); |
| 403 | Clang.takeDiagnosticClient(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 404 | Invocation.reset(Clang.takeInvocation()); |
| 405 | return true; |
| 406 | } |
| 407 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 408 | /// \brief Simple function to retrieve a path for a preamble precompiled header. |
| 409 | static std::string GetPreamblePCHPath() { |
| 410 | // FIXME: This is lame; sys::Path should provide this function (in particular, |
| 411 | // it should know how to find the temporary files dir). |
| 412 | // FIXME: This is really lame. I copied this code from the Driver! |
| 413 | std::string Error; |
| 414 | const char *TmpDir = ::getenv("TMPDIR"); |
| 415 | if (!TmpDir) |
| 416 | TmpDir = ::getenv("TEMP"); |
| 417 | if (!TmpDir) |
| 418 | TmpDir = ::getenv("TMP"); |
| 419 | if (!TmpDir) |
| 420 | TmpDir = "/tmp"; |
| 421 | llvm::sys::Path P(TmpDir); |
| 422 | P.appendComponent("preamble"); |
| 423 | if (P.createTemporaryFileOnDisk()) |
| 424 | return std::string(); |
| 425 | |
| 426 | P.appendSuffix("pch"); |
| 427 | return P.str(); |
| 428 | } |
| 429 | |
| 430 | void ASTUnit::BuildPrecompiledPreamble() { |
| 431 | CompilerInvocation PreambleInvocation(*Invocation); |
| 432 | FrontendOptions &FrontendOpts = PreambleInvocation.getFrontendOpts(); |
| 433 | PreprocessorOptions &PreprocessorOpts |
| 434 | = PreambleInvocation.getPreprocessorOpts(); |
| 435 | |
| 436 | // Try to determine if the main file has been remapped, either from the |
| 437 | // command line (to another file) or directly through the compiler invocation |
| 438 | // (to a memory buffer). |
| 439 | llvm::MemoryBuffer *Buffer = 0; |
| 440 | llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second); |
| 441 | if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) { |
| 442 | // Check whether there is a file-file remapping of the main file |
| 443 | for (PreprocessorOptions::remapped_file_iterator |
| 444 | M = PreprocessorOpts.remapped_file_begin(), |
| 445 | E = PreprocessorOpts.remapped_file_end(); |
| 446 | M != E; |
| 447 | ++M) { |
| 448 | llvm::sys::PathWithStatus MPath(M->first); |
| 449 | if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { |
| 450 | if (MainFileStatus->uniqueID == MStatus->uniqueID) { |
| 451 | // We found a remapping. Try to load the resulting, remapped source. |
| 452 | if (Buffer) |
| 453 | delete Buffer; |
| 454 | Buffer = llvm::MemoryBuffer::getFile(M->second); |
| 455 | if (!Buffer) |
| 456 | return; |
| 457 | |
| 458 | // Remove the file-file remapping. |
| 459 | M = PreprocessorOpts.eraseRemappedFile(M); |
| 460 | E = PreprocessorOpts.remapped_file_end(); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // Check whether there is a file-buffer remapping. It supercedes the |
| 466 | // file-file remapping. |
| 467 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 468 | M = PreprocessorOpts.remapped_file_buffer_begin(), |
| 469 | E = PreprocessorOpts.remapped_file_buffer_end(); |
| 470 | M != E; |
| 471 | ++M) { |
| 472 | llvm::sys::PathWithStatus MPath(M->first); |
| 473 | if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { |
| 474 | if (MainFileStatus->uniqueID == MStatus->uniqueID) { |
| 475 | // We found a remapping. |
| 476 | if (Buffer) |
| 477 | delete Buffer; |
| 478 | Buffer = const_cast<llvm::MemoryBuffer *>(M->second); |
| 479 | |
| 480 | // Remove the file-buffer remapping. |
| 481 | M = PreprocessorOpts.eraseRemappedFile(M); |
| 482 | E = PreprocessorOpts.remapped_file_buffer_end(); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // If the main source file was not remapped, load it now. |
| 489 | if (!Buffer) { |
| 490 | Buffer = llvm::MemoryBuffer::getFile(FrontendOpts.Inputs[0].second); |
| 491 | if (!Buffer) |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | // Try to compute the preamble. |
| 496 | unsigned PreambleLength = Lexer::ComputePreamble(Buffer); |
| 497 | if (PreambleLength == 0) |
| 498 | return; |
| 499 | |
| 500 | // Create a new buffer that stores the preamble. The buffer also contains |
| 501 | // extra space for the original contents of the file (which will be present |
| 502 | // when we actually parse the file) along with more room in case the file |
| 503 | // grows. |
| 504 | unsigned PreambleBufferSize = Buffer->getBufferSize(); |
| 505 | if (PreambleBufferSize < 4096) |
| 506 | PreambleBufferSize = 8192; |
| 507 | else |
| 508 | PreambleBufferSize *= 2; |
| 509 | |
| 510 | llvm::MemoryBuffer *PreambleBuffer |
| 511 | = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleBufferSize, |
| 512 | FrontendOpts.Inputs[0].second); |
| 513 | memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()), |
| 514 | Buffer->getBufferStart(), PreambleLength); |
| 515 | memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + PreambleLength, |
| 516 | ' ', PreambleBufferSize - PreambleLength - 1); |
| 517 | const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = 0; |
| 518 | delete Buffer; |
| 519 | |
| 520 | // Remap the main source file to the preamble buffer. |
| 521 | PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer); |
| 522 | |
| 523 | // Tell the compiler invocation to generate a temporary precompiled header. |
| 524 | FrontendOpts.ProgramAction = frontend::GeneratePCH; |
| 525 | // FIXME: Set ChainedPCH, once it is ready. |
| 526 | // FIXME: Generate the precompiled header into memory? |
| 527 | FrontendOpts.OutputFile = GetPreamblePCHPath(); |
| 528 | |
| 529 | // Create the compiler instance to use for building the precompiled preamble. |
| 530 | CompilerInstance Clang; |
| 531 | Clang.setInvocation(&PreambleInvocation); |
| 532 | OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; |
| 533 | |
| 534 | // Set up diagnostics. |
| 535 | Clang.setDiagnostics(&getDiagnostics()); |
| 536 | Clang.setDiagnosticClient(getDiagnostics().getClient()); |
| 537 | |
| 538 | // Create the target instance. |
| 539 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 540 | Clang.getTargetOpts())); |
| 541 | if (!Clang.hasTarget()) { |
| 542 | Clang.takeDiagnosticClient(); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | // Inform the target of the language options. |
| 547 | // |
| 548 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 549 | // created. This complexity should be lifted elsewhere. |
| 550 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
| 551 | |
| 552 | assert(Clang.getFrontendOpts().Inputs.size() == 1 && |
| 553 | "Invocation must have exactly one source file!"); |
| 554 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && |
| 555 | "FIXME: AST inputs not yet supported here!"); |
| 556 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
| 557 | "IR inputs not support here!"); |
| 558 | |
| 559 | // Clear out old caches and data. |
| 560 | StoredDiagnostics.clear(); |
| 561 | |
| 562 | // Capture any diagnostics that would otherwise be dropped. |
| 563 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, |
| 564 | Clang.getDiagnostics(), |
| 565 | StoredDiagnostics); |
| 566 | |
| 567 | // Create a file manager object to provide access to and cache the filesystem. |
| 568 | Clang.setFileManager(new FileManager); |
| 569 | |
| 570 | // Create the source manager. |
| 571 | Clang.setSourceManager(new SourceManager(getDiagnostics())); |
| 572 | |
| 573 | // FIXME: Eventually, we'll have to track top-level declarations here, too. |
| 574 | llvm::OwningPtr<GeneratePCHAction> Act; |
| 575 | Act.reset(new GeneratePCHAction); |
| 576 | if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, |
| 577 | Clang.getFrontendOpts().Inputs[0].first)) { |
| 578 | Clang.takeDiagnosticClient(); |
| 579 | Clang.takeInvocation(); |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | Act->Execute(); |
| 584 | Act->EndSourceFile(); |
| 585 | Clang.takeDiagnosticClient(); |
| 586 | Clang.takeInvocation(); |
| 587 | |
| 588 | // FIXME: Keep track of the actual preamble header we created! |
| 589 | fprintf(stderr, "Preamble PCH: %s\n", FrontendOpts.OutputFile.c_str()); |
| 590 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 591 | |
| 592 | ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI, |
| 593 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
| 594 | bool OnlyLocalDecls, |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 595 | bool CaptureDiagnostics, |
| 596 | bool PrecompilePreamble) { |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 597 | if (!Diags.getPtr()) { |
| 598 | // No diagnostics engine was provided, so create our own diagnostics object |
| 599 | // with the default options. |
| 600 | DiagnosticOptions DiagOpts; |
| 601 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
| 602 | } |
| 603 | |
| 604 | // Create the AST unit. |
| 605 | llvm::OwningPtr<ASTUnit> AST; |
| 606 | AST.reset(new ASTUnit(false)); |
| 607 | AST->Diagnostics = Diags; |
| 608 | AST->CaptureDiagnostics = CaptureDiagnostics; |
| 609 | AST->OnlyLocalDecls = OnlyLocalDecls; |
| 610 | AST->Invocation.reset(CI); |
| 611 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 612 | if (PrecompilePreamble) |
| 613 | AST->BuildPrecompiledPreamble(); |
| 614 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 615 | if (!AST->Parse()) |
| 616 | return AST.take(); |
| 617 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 618 | return 0; |
| 619 | } |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 620 | |
| 621 | ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin, |
| 622 | const char **ArgEnd, |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 623 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 624 | llvm::StringRef ResourceFilesPath, |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 625 | bool OnlyLocalDecls, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 626 | RemappedFile *RemappedFiles, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 627 | unsigned NumRemappedFiles, |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 628 | bool CaptureDiagnostics, |
| 629 | bool PrecompilePreamble) { |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 630 | if (!Diags.getPtr()) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 631 | // No diagnostics engine was provided, so create our own diagnostics object |
| 632 | // with the default options. |
| 633 | DiagnosticOptions DiagOpts; |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 634 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 637 | llvm::SmallVector<const char *, 16> Args; |
| 638 | Args.push_back("<clang>"); // FIXME: Remove dummy argument. |
| 639 | Args.insert(Args.end(), ArgBegin, ArgEnd); |
| 640 | |
| 641 | // FIXME: Find a cleaner way to force the driver into restricted modes. We |
| 642 | // also want to force it to use clang. |
| 643 | Args.push_back("-fsyntax-only"); |
| 644 | |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 645 | // FIXME: We shouldn't have to pass in the path info. |
Daniel Dunbar | 0bbad51 | 2010-07-19 00:44:04 +0000 | [diff] [blame] | 646 | driver::Driver TheDriver("clang", llvm::sys::getHostTriple(), |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 647 | "a.out", false, false, *Diags); |
Daniel Dunbar | 3bd54cc | 2010-01-25 00:44:02 +0000 | [diff] [blame] | 648 | |
| 649 | // Don't check that inputs exist, they have been remapped. |
| 650 | TheDriver.setCheckInputsExist(false); |
| 651 | |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 652 | llvm::OwningPtr<driver::Compilation> C( |
| 653 | TheDriver.BuildCompilation(Args.size(), Args.data())); |
| 654 | |
| 655 | // We expect to get back exactly one command job, if we didn't something |
| 656 | // failed. |
| 657 | const driver::JobList &Jobs = C->getJobs(); |
| 658 | if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) { |
| 659 | llvm::SmallString<256> Msg; |
| 660 | llvm::raw_svector_ostream OS(Msg); |
| 661 | C->PrintJob(OS, C->getJobs(), "; ", true); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 662 | Diags->Report(diag::err_fe_expected_compiler_job) << OS.str(); |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin()); |
| 667 | if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 668 | Diags->Report(diag::err_fe_expected_clang_command); |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | const driver::ArgStringList &CCArgs = Cmd->getArguments(); |
Daniel Dunbar | 807b061 | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 673 | llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation); |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 674 | CompilerInvocation::CreateFromArgs(*CI, |
| 675 | const_cast<const char **>(CCArgs.data()), |
| 676 | const_cast<const char **>(CCArgs.data()) + |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 677 | CCArgs.size(), |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 678 | *Diags); |
Daniel Dunbar | 1e69fe3 | 2009-12-13 03:45:58 +0000 | [diff] [blame] | 679 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 680 | // Override any files that need remapping |
| 681 | for (unsigned I = 0; I != NumRemappedFiles; ++I) |
Daniel Dunbar | 807b061 | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 682 | CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, |
Daniel Dunbar | b26d483 | 2010-02-16 01:55:04 +0000 | [diff] [blame] | 683 | RemappedFiles[I].second); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 684 | |
Daniel Dunbar | 8b9adfe | 2009-12-15 00:06:45 +0000 | [diff] [blame] | 685 | // Override the resources path. |
Daniel Dunbar | 807b061 | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 686 | CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 687 | |
Daniel Dunbar | b26d483 | 2010-02-16 01:55:04 +0000 | [diff] [blame] | 688 | CI->getFrontendOpts().DisableFree = true; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 689 | return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls, |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 690 | CaptureDiagnostics, PrecompilePreamble); |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 691 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 692 | |
| 693 | bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) { |
| 694 | if (!Invocation.get()) |
| 695 | return true; |
| 696 | |
| 697 | // Clear out the diagnostics state. |
| 698 | getDiagnostics().Reset(); |
| 699 | |
| 700 | // Remap files. |
| 701 | Invocation->getPreprocessorOpts().clearRemappedFiles(); |
| 702 | for (unsigned I = 0; I != NumRemappedFiles; ++I) |
| 703 | Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, |
| 704 | RemappedFiles[I].second); |
| 705 | |
| 706 | return Parse(); |
| 707 | } |