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" |
| 17 | #include "clang/AST/DeclVisitor.h" |
| 18 | #include "clang/AST/StmtVisitor.h" |
| 19 | #include "clang/Lex/HeaderSearch.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Basic/TargetInfo.h" |
| 22 | #include "clang/Basic/Diagnostic.h" |
| 23 | #include "llvm/Support/Compiler.h" |
Benjamin Kramer | 4a630d3 | 2009-10-18 11:34:14 +0000 | [diff] [blame^] | 24 | #include "llvm/System/Path.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 28 | ASTUnit::ASTUnit(Diagnostic &_Diags) : Diags(_Diags), tempFile(false) { } |
| 29 | ASTUnit::~ASTUnit() { |
| 30 | if (tempFile) |
Benjamin Kramer | 4a630d3 | 2009-10-18 11:34:14 +0000 | [diff] [blame^] | 31 | llvm::sys::Path(getPCHFileName()).eraseFromDisk(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 32 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 33 | |
| 34 | namespace { |
| 35 | |
| 36 | /// \brief Gathers information from PCHReader that will be used to initialize |
| 37 | /// a Preprocessor. |
| 38 | class VISIBILITY_HIDDEN PCHInfoCollector : public PCHReaderListener { |
| 39 | LangOptions &LangOpt; |
| 40 | HeaderSearch &HSI; |
| 41 | std::string &TargetTriple; |
| 42 | std::string &Predefines; |
| 43 | unsigned &Counter; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 45 | unsigned NumHeaderInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 47 | public: |
| 48 | PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI, |
| 49 | std::string &TargetTriple, std::string &Predefines, |
| 50 | unsigned &Counter) |
| 51 | : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple), |
| 52 | Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 54 | virtual bool ReadLanguageOptions(const LangOptions &LangOpts) { |
| 55 | LangOpt = LangOpts; |
| 56 | return false; |
| 57 | } |
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 | virtual bool ReadTargetTriple(const std::string &Triple) { |
| 60 | TargetTriple = Triple; |
| 61 | return false; |
| 62 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | |
| 64 | virtual bool ReadPredefinesBuffer(const char *PCHPredef, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 65 | unsigned PCHPredefLen, |
| 66 | FileID PCHBufferID, |
| 67 | std::string &SuggestedPredefines) { |
| 68 | Predefines = PCHPredef; |
| 69 | return false; |
| 70 | } |
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 | virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) { |
| 73 | HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++); |
| 74 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 76 | virtual void ReadCounter(unsigned Value) { |
| 77 | Counter = Value; |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | } // anonymous namespace |
| 82 | |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 83 | const std::string &ASTUnit::getOriginalSourceFileName() { |
| 84 | return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile(); |
| 85 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 86 | |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 87 | const std::string &ASTUnit::getPCHFileName() { |
| 88 | return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName(); |
| 89 | } |
| 90 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 91 | FileManager &ASTUnit::getFileManager() { |
| 92 | return HeaderInfo->getFileMgr(); |
| 93 | } |
| 94 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 95 | ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 96 | Diagnostic &Diags, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 97 | FileManager &FileMgr, |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 98 | std::string *ErrMsg, |
Ted Kremenek | 5cf4876 | 2009-10-17 00:34:24 +0000 | [diff] [blame] | 99 | bool OnlyLocalDecls, |
| 100 | bool UseBumpAllocator) { |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 101 | llvm::OwningPtr<ASTUnit> AST(new ASTUnit(Diags)); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 102 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 103 | AST->HeaderInfo.reset(new HeaderSearch(FileMgr)); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 104 | |
| 105 | // Gather Info for preprocessor construction later on. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 107 | LangOptions LangInfo; |
| 108 | HeaderSearch &HeaderInfo = *AST->HeaderInfo.get(); |
| 109 | std::string TargetTriple; |
| 110 | std::string Predefines; |
| 111 | unsigned Counter; |
| 112 | |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 113 | llvm::OwningPtr<PCHReader> Reader; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 114 | llvm::OwningPtr<ExternalASTSource> Source; |
| 115 | |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 116 | Reader.reset(new PCHReader(AST->getSourceManager(), FileMgr, AST->Diags)); |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 117 | Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple, |
| 118 | Predefines, Counter)); |
| 119 | |
| 120 | switch (Reader->ReadPCH(Filename)) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 121 | case PCHReader::Success: |
| 122 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 124 | case PCHReader::Failure: |
Argyrios Kyrtzidis | 106c998 | 2009-06-25 18:22:30 +0000 | [diff] [blame] | 125 | case PCHReader::IgnorePCH: |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 126 | if (ErrMsg) |
| 127 | *ErrMsg = "Could not load PCH file"; |
| 128 | return NULL; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 129 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 131 | // PCH loaded successfully. Now create the preprocessor. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 133 | // Get information about the target being compiled for. |
| 134 | AST->Target.reset(TargetInfo::CreateTargetInfo(TargetTriple)); |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 135 | AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(), |
| 136 | AST->getSourceManager(), HeaderInfo)); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 137 | Preprocessor &PP = *AST->PP.get(); |
| 138 | |
Daniel Dunbar | d5b6126 | 2009-09-21 03:03:47 +0000 | [diff] [blame] | 139 | PP.setPredefines(Reader->getSuggestedPredefines()); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 140 | PP.setCounterValue(Counter); |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 141 | Reader->setPreprocessor(PP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 143 | // Create and initialize the ASTContext. |
| 144 | |
| 145 | AST->Ctx.reset(new ASTContext(LangInfo, |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 146 | AST->getSourceManager(), |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 147 | *AST->Target.get(), |
| 148 | PP.getIdentifierTable(), |
| 149 | PP.getSelectorTable(), |
| 150 | PP.getBuiltinInfo(), |
Ted Kremenek | 5cf4876 | 2009-10-17 00:34:24 +0000 | [diff] [blame] | 151 | /* FreeMemory = */ !UseBumpAllocator, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 152 | /* size_reserve = */0)); |
| 153 | ASTContext &Context = *AST->Ctx.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 155 | Reader->InitializeContext(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 157 | // Attach the PCH reader to the AST context as an external AST |
| 158 | // source, so that declarations will be deserialized from the |
| 159 | // PCH file as needed. |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 160 | Source.reset(Reader.take()); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 161 | Context.setExternalSource(Source); |
| 162 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | return AST.take(); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 164 | } |