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" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 36 | using namespace clang; |
| 37 | |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 38 | ASTUnit::ASTUnit(bool _MainFileIsAST) |
| 39 | : tempFile(false), MainFileIsAST(_MainFileIsAST) { |
Steve Naroff | 36c4464 | 2009-10-19 14:34:22 +0000 | [diff] [blame] | 40 | } |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 41 | ASTUnit::~ASTUnit() { |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 42 | if (tempFile) |
Benjamin Kramer | 4a630d3 | 2009-10-18 11:34:14 +0000 | [diff] [blame] | 43 | llvm::sys::Path(getPCHFileName()).eraseFromDisk(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 44 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 45 | |
| 46 | namespace { |
| 47 | |
| 48 | /// \brief Gathers information from PCHReader that will be used to initialize |
| 49 | /// a Preprocessor. |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 50 | class PCHInfoCollector : public PCHReaderListener { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 51 | LangOptions &LangOpt; |
| 52 | HeaderSearch &HSI; |
| 53 | std::string &TargetTriple; |
| 54 | std::string &Predefines; |
| 55 | unsigned &Counter; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 57 | unsigned NumHeaderInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 59 | public: |
| 60 | PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI, |
| 61 | std::string &TargetTriple, std::string &Predefines, |
| 62 | unsigned &Counter) |
| 63 | : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple), |
| 64 | Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 66 | virtual bool ReadLanguageOptions(const LangOptions &LangOpts) { |
| 67 | LangOpt = LangOpts; |
| 68 | return false; |
| 69 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | |
Daniel Dunbar | dc3c0d2 | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 71 | virtual bool ReadTargetTriple(llvm::StringRef Triple) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 72 | TargetTriple = Triple; |
| 73 | return false; |
| 74 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Daniel Dunbar | dc3c0d2 | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 76 | virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 77 | FileID PCHBufferID, |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 78 | llvm::StringRef OriginalFileName, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 79 | std::string &SuggestedPredefines) { |
| 80 | Predefines = PCHPredef; |
| 81 | return false; |
| 82 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 84 | virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) { |
| 85 | HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++); |
| 86 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 88 | virtual void ReadCounter(unsigned Value) { |
| 89 | Counter = Value; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | } // anonymous namespace |
| 94 | |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 95 | const std::string &ASTUnit::getOriginalSourceFileName() { |
Daniel Dunbar | 68d40e2 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 96 | return OriginalSourceFile; |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 97 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 98 | |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 99 | const std::string &ASTUnit::getPCHFileName() { |
Daniel Dunbar | c7822db | 2009-12-02 21:47:43 +0000 | [diff] [blame] | 100 | assert(isMainFileAST() && "Not an ASTUnit from a PCH file!"); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 101 | return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName(); |
| 102 | } |
| 103 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 104 | ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 105 | Diagnostic &Diags, |
Ted Kremenek | 5cf4876 | 2009-10-17 00:34:24 +0000 | [diff] [blame] | 106 | bool OnlyLocalDecls, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 107 | bool UseBumpAllocator, |
| 108 | RemappedFile *RemappedFiles, |
| 109 | unsigned NumRemappedFiles) { |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 110 | llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true)); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 111 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Steve Naroff | 36c4464 | 2009-10-19 14:34:22 +0000 | [diff] [blame] | 112 | AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager())); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 113 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 114 | for (unsigned I = 0; I != NumRemappedFiles; ++I) { |
| 115 | // Create the file entry for the file that we're mapping from. |
| 116 | const FileEntry *FromFile |
| 117 | = AST->getFileManager().getVirtualFile(RemappedFiles[I].first, |
| 118 | RemappedFiles[I].second->getBufferSize(), |
| 119 | 0); |
| 120 | if (!FromFile) { |
| 121 | Diags.Report(diag::err_fe_remap_missing_from_file) |
| 122 | << RemappedFiles[I].first; |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | // Override the contents of the "from" file with the contents of |
| 127 | // the "to" file. |
| 128 | AST->getSourceManager().overrideFileContents(FromFile, |
| 129 | RemappedFiles[I].second); |
| 130 | } |
| 131 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 132 | // Gather Info for preprocessor construction later on. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 134 | LangOptions LangInfo; |
| 135 | HeaderSearch &HeaderInfo = *AST->HeaderInfo.get(); |
| 136 | std::string TargetTriple; |
| 137 | std::string Predefines; |
| 138 | unsigned Counter; |
| 139 | |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 140 | llvm::OwningPtr<PCHReader> Reader; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 141 | llvm::OwningPtr<ExternalASTSource> Source; |
| 142 | |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 143 | Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(), |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 144 | Diags)); |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 145 | Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple, |
| 146 | Predefines, Counter)); |
| 147 | |
| 148 | switch (Reader->ReadPCH(Filename)) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 149 | case PCHReader::Success: |
| 150 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 152 | case PCHReader::Failure: |
Argyrios Kyrtzidis | 106c998 | 2009-06-25 18:22:30 +0000 | [diff] [blame] | 153 | case PCHReader::IgnorePCH: |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 154 | Diags.Report(diag::err_fe_unable_to_load_pch); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 155 | return NULL; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Daniel Dunbar | 68d40e2 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 158 | AST->OriginalSourceFile = Reader->getOriginalSourceFile(); |
| 159 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 160 | // PCH loaded successfully. Now create the preprocessor. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 162 | // Get information about the target being compiled for. |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 163 | // |
| 164 | // FIXME: This is broken, we should store the TargetOptions in the PCH. |
| 165 | TargetOptions TargetOpts; |
| 166 | TargetOpts.ABI = ""; |
| 167 | TargetOpts.CPU = ""; |
| 168 | TargetOpts.Features.clear(); |
| 169 | TargetOpts.Triple = TargetTriple; |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 170 | AST->Target.reset(TargetInfo::CreateTargetInfo(Diags, TargetOpts)); |
| 171 | AST->PP.reset(new Preprocessor(Diags, LangInfo, *AST->Target.get(), |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 172 | AST->getSourceManager(), HeaderInfo)); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 173 | Preprocessor &PP = *AST->PP.get(); |
| 174 | |
Daniel Dunbar | d5b6126 | 2009-09-21 03:03:47 +0000 | [diff] [blame] | 175 | PP.setPredefines(Reader->getSuggestedPredefines()); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 176 | PP.setCounterValue(Counter); |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 177 | Reader->setPreprocessor(PP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 179 | // Create and initialize the ASTContext. |
| 180 | |
| 181 | AST->Ctx.reset(new ASTContext(LangInfo, |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 182 | AST->getSourceManager(), |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 183 | *AST->Target.get(), |
| 184 | PP.getIdentifierTable(), |
| 185 | PP.getSelectorTable(), |
| 186 | PP.getBuiltinInfo(), |
Ted Kremenek | 5cf4876 | 2009-10-17 00:34:24 +0000 | [diff] [blame] | 187 | /* FreeMemory = */ !UseBumpAllocator, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 188 | /* size_reserve = */0)); |
| 189 | ASTContext &Context = *AST->Ctx.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 191 | Reader->InitializeContext(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 193 | // Attach the PCH reader to the AST context as an external AST |
| 194 | // source, so that declarations will be deserialized from the |
| 195 | // PCH file as needed. |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 196 | Source.reset(Reader.take()); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 197 | Context.setExternalSource(Source); |
| 198 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | return AST.take(); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 200 | } |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 201 | |
| 202 | namespace { |
| 203 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 204 | class TopLevelDeclTrackerConsumer : public ASTConsumer { |
| 205 | ASTUnit &Unit; |
| 206 | |
| 207 | public: |
| 208 | TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {} |
| 209 | |
| 210 | void HandleTopLevelDecl(DeclGroupRef D) { |
| 211 | for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) |
| 212 | Unit.getTopLevelDecls().push_back(*it); |
| 213 | } |
| 214 | }; |
| 215 | |
| 216 | class TopLevelDeclTrackerAction : public ASTFrontendAction { |
| 217 | public: |
| 218 | ASTUnit &Unit; |
| 219 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 220 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 221 | llvm::StringRef InFile) { |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 222 | return new TopLevelDeclTrackerConsumer(Unit); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | public: |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 226 | TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {} |
| 227 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 228 | virtual bool hasCodeCompletionSupport() const { return false; } |
| 229 | }; |
| 230 | |
| 231 | } |
| 232 | |
| 233 | ASTUnit *ASTUnit::LoadFromCompilerInvocation(const CompilerInvocation &CI, |
| 234 | Diagnostic &Diags, |
Daniel Dunbar | 68ea2ac | 2009-12-02 21:47:32 +0000 | [diff] [blame] | 235 | bool OnlyLocalDecls) { |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 236 | // Create the compiler instance to use for building the AST. |
Daniel Dunbar | cb6dda1 | 2009-12-02 08:43:56 +0000 | [diff] [blame] | 237 | CompilerInstance Clang; |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 238 | llvm::OwningPtr<ASTUnit> AST; |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 239 | llvm::OwningPtr<TopLevelDeclTrackerAction> Act; |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 240 | |
| 241 | Clang.getInvocation() = CI; |
| 242 | |
| 243 | Clang.setDiagnostics(&Diags); |
| 244 | Clang.setDiagnosticClient(Diags.getClient()); |
| 245 | |
| 246 | // Create the target instance. |
| 247 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 248 | Clang.getTargetOpts())); |
| 249 | if (!Clang.hasTarget()) |
| 250 | goto error; |
| 251 | |
| 252 | // Inform the target of the language options. |
| 253 | // |
| 254 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 255 | // created. This complexity should be lifted elsewhere. |
| 256 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
| 257 | |
| 258 | assert(Clang.getFrontendOpts().Inputs.size() == 1 && |
| 259 | "Invocation must have exactly one source file!"); |
| 260 | assert(Clang.getFrontendOpts().Inputs[0].first != FrontendOptions::IK_AST && |
| 261 | "FIXME: AST inputs not yet supported here!"); |
| 262 | |
| 263 | // Create the AST unit. |
Daniel Dunbar | c7822db | 2009-12-02 21:47:43 +0000 | [diff] [blame] | 264 | AST.reset(new ASTUnit(false)); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 265 | |
Daniel Dunbar | 68ea2ac | 2009-12-02 21:47:32 +0000 | [diff] [blame] | 266 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Daniel Dunbar | 68d40e2 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 267 | AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; |
| 268 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 269 | // Create a file manager object to provide access to and cache the filesystem. |
| 270 | Clang.setFileManager(&AST->getFileManager()); |
| 271 | |
| 272 | // Create the source manager. |
| 273 | Clang.setSourceManager(&AST->getSourceManager()); |
| 274 | |
| 275 | // Create the preprocessor. |
| 276 | Clang.createPreprocessor(); |
| 277 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 278 | Act.reset(new TopLevelDeclTrackerAction(*AST)); |
| 279 | if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 280 | /*IsAST=*/false)) |
| 281 | goto error; |
| 282 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 283 | Act->Execute(); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 284 | |
Daniel Dunbar | 64a32ba | 2009-12-01 21:57:33 +0000 | [diff] [blame] | 285 | // Steal the created target, context, and preprocessor, and take back the |
| 286 | // source and file managers. |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 287 | AST->Ctx.reset(Clang.takeASTContext()); |
| 288 | AST->PP.reset(Clang.takePreprocessor()); |
| 289 | Clang.takeSourceManager(); |
| 290 | Clang.takeFileManager(); |
Daniel Dunbar | 64a32ba | 2009-12-01 21:57:33 +0000 | [diff] [blame] | 291 | AST->Target.reset(Clang.takeTarget()); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 292 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 293 | Act->EndSourceFile(); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 294 | |
| 295 | Clang.takeDiagnosticClient(); |
| 296 | Clang.takeDiagnostics(); |
| 297 | |
| 298 | return AST.take(); |
| 299 | |
| 300 | error: |
| 301 | Clang.takeSourceManager(); |
| 302 | Clang.takeFileManager(); |
| 303 | Clang.takeDiagnosticClient(); |
| 304 | Clang.takeDiagnostics(); |
| 305 | return 0; |
| 306 | } |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 307 | |
| 308 | ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin, |
| 309 | const char **ArgEnd, |
| 310 | Diagnostic &Diags, |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 311 | llvm::StringRef ResourceFilesPath, |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 312 | bool OnlyLocalDecls, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 313 | bool UseBumpAllocator, |
| 314 | RemappedFile *RemappedFiles, |
| 315 | unsigned NumRemappedFiles) { |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 316 | llvm::SmallVector<const char *, 16> Args; |
| 317 | Args.push_back("<clang>"); // FIXME: Remove dummy argument. |
| 318 | Args.insert(Args.end(), ArgBegin, ArgEnd); |
| 319 | |
| 320 | // FIXME: Find a cleaner way to force the driver into restricted modes. We |
| 321 | // also want to force it to use clang. |
| 322 | Args.push_back("-fsyntax-only"); |
| 323 | |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 324 | // FIXME: We shouldn't have to pass in the path info. |
| 325 | driver::Driver TheDriver("clang", "/", llvm::sys::getHostTriple(), |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 326 | "a.out", false, Diags); |
Daniel Dunbar | 3bd54cc | 2010-01-25 00:44:02 +0000 | [diff] [blame] | 327 | |
| 328 | // Don't check that inputs exist, they have been remapped. |
| 329 | TheDriver.setCheckInputsExist(false); |
| 330 | |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 331 | llvm::OwningPtr<driver::Compilation> C( |
| 332 | TheDriver.BuildCompilation(Args.size(), Args.data())); |
| 333 | |
| 334 | // We expect to get back exactly one command job, if we didn't something |
| 335 | // failed. |
| 336 | const driver::JobList &Jobs = C->getJobs(); |
| 337 | if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) { |
| 338 | llvm::SmallString<256> Msg; |
| 339 | llvm::raw_svector_ostream OS(Msg); |
| 340 | C->PrintJob(OS, C->getJobs(), "; ", true); |
| 341 | Diags.Report(diag::err_fe_expected_compiler_job) << OS.str(); |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin()); |
| 346 | if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") { |
| 347 | Diags.Report(diag::err_fe_expected_clang_command); |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | const driver::ArgStringList &CCArgs = Cmd->getArguments(); |
| 352 | CompilerInvocation CI; |
| 353 | CompilerInvocation::CreateFromArgs(CI, (const char**) CCArgs.data(), |
| 354 | (const char**) CCArgs.data()+CCArgs.size(), |
Daniel Dunbar | 1e69fe3 | 2009-12-13 03:45:58 +0000 | [diff] [blame] | 355 | Diags); |
| 356 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 357 | // Override any files that need remapping |
| 358 | for (unsigned I = 0; I != NumRemappedFiles; ++I) |
| 359 | CI.getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, |
| 360 | RemappedFiles[I].second); |
| 361 | |
Daniel Dunbar | 8b9adfe | 2009-12-15 00:06:45 +0000 | [diff] [blame] | 362 | // Override the resources path. |
| 363 | CI.getHeaderSearchOpts().ResourceDir = ResourceFilesPath; |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 364 | |
Daniel Dunbar | 68ea2ac | 2009-12-02 21:47:32 +0000 | [diff] [blame] | 365 | CI.getFrontendOpts().DisableFree = UseBumpAllocator; |
| 366 | return LoadFromCompilerInvocation(CI, Diags, OnlyLocalDecls); |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 367 | } |