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