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 | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/CompilerInstance.h" |
| 21 | #include "clang/Frontend/FrontendActions.h" |
| 22 | #include "clang/Frontend/FrontendOptions.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 23 | #include "clang/Lex/HeaderSearch.h" |
| 24 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetOptions.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 26 | #include "clang/Basic/TargetInfo.h" |
| 27 | #include "clang/Basic/Diagnostic.h" |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 28 | #include "llvm/LLVMContext.h" |
Benjamin Kramer | 4a630d3 | 2009-10-18 11:34:14 +0000 | [diff] [blame] | 29 | #include "llvm/System/Path.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 32 | ASTUnit::ASTUnit(DiagnosticClient *diagClient) : tempFile(false) { |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 33 | Diags.setClient(diagClient ? diagClient : new TextDiagnosticBuffer()); |
Steve Naroff | 36c4464 | 2009-10-19 14:34:22 +0000 | [diff] [blame] | 34 | } |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 35 | ASTUnit::~ASTUnit() { |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 36 | if (tempFile) |
Benjamin Kramer | 4a630d3 | 2009-10-18 11:34:14 +0000 | [diff] [blame] | 37 | llvm::sys::Path(getPCHFileName()).eraseFromDisk(); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 39 | // The ASTUnit object owns the DiagnosticClient. |
| 40 | delete Diags.getClient(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 41 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 42 | |
| 43 | namespace { |
| 44 | |
| 45 | /// \brief Gathers information from PCHReader that will be used to initialize |
| 46 | /// a Preprocessor. |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 47 | class PCHInfoCollector : public PCHReaderListener { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 48 | LangOptions &LangOpt; |
| 49 | HeaderSearch &HSI; |
| 50 | std::string &TargetTriple; |
| 51 | std::string &Predefines; |
| 52 | unsigned &Counter; |
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 | unsigned NumHeaderInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 56 | public: |
| 57 | PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI, |
| 58 | std::string &TargetTriple, std::string &Predefines, |
| 59 | unsigned &Counter) |
| 60 | : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple), |
| 61 | Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 63 | virtual bool ReadLanguageOptions(const LangOptions &LangOpts) { |
| 64 | LangOpt = LangOpts; |
| 65 | return false; |
| 66 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | |
Daniel Dunbar | dc3c0d2 | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 68 | virtual bool ReadTargetTriple(llvm::StringRef Triple) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 69 | TargetTriple = Triple; |
| 70 | return false; |
| 71 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Daniel Dunbar | dc3c0d2 | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 73 | virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 74 | FileID PCHBufferID, |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 75 | llvm::StringRef OriginalFileName, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 76 | std::string &SuggestedPredefines) { |
| 77 | Predefines = PCHPredef; |
| 78 | return false; |
| 79 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 81 | virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) { |
| 82 | HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++); |
| 83 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 85 | virtual void ReadCounter(unsigned Value) { |
| 86 | Counter = Value; |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | } // anonymous namespace |
| 91 | |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 92 | const std::string &ASTUnit::getOriginalSourceFileName() { |
| 93 | return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile(); |
| 94 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 95 | |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 96 | const std::string &ASTUnit::getPCHFileName() { |
| 97 | return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName(); |
| 98 | } |
| 99 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 100 | ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 101 | std::string *ErrMsg, |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 102 | DiagnosticClient *diagClient, |
Ted Kremenek | 5cf4876 | 2009-10-17 00:34:24 +0000 | [diff] [blame] | 103 | bool OnlyLocalDecls, |
| 104 | bool UseBumpAllocator) { |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 105 | llvm::OwningPtr<ASTUnit> AST(new ASTUnit(diagClient)); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 106 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Steve Naroff | 36c4464 | 2009-10-19 14:34:22 +0000 | [diff] [blame] | 107 | AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager())); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 108 | |
| 109 | // Gather Info for preprocessor construction later on. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 111 | LangOptions LangInfo; |
| 112 | HeaderSearch &HeaderInfo = *AST->HeaderInfo.get(); |
| 113 | std::string TargetTriple; |
| 114 | std::string Predefines; |
| 115 | unsigned Counter; |
| 116 | |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 117 | llvm::OwningPtr<PCHReader> Reader; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 118 | llvm::OwningPtr<ExternalASTSource> Source; |
| 119 | |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 120 | Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(), |
| 121 | AST->Diags)); |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 122 | Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple, |
| 123 | Predefines, Counter)); |
| 124 | |
| 125 | switch (Reader->ReadPCH(Filename)) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 126 | case PCHReader::Success: |
| 127 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 129 | case PCHReader::Failure: |
Argyrios Kyrtzidis | 106c998 | 2009-06-25 18:22:30 +0000 | [diff] [blame] | 130 | case PCHReader::IgnorePCH: |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 131 | if (ErrMsg) |
| 132 | *ErrMsg = "Could not load PCH file"; |
| 133 | return NULL; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 134 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 136 | // PCH loaded successfully. Now create the preprocessor. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 138 | // Get information about the target being compiled for. |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 139 | // |
| 140 | // FIXME: This is broken, we should store the TargetOptions in the PCH. |
| 141 | TargetOptions TargetOpts; |
| 142 | TargetOpts.ABI = ""; |
| 143 | TargetOpts.CPU = ""; |
| 144 | TargetOpts.Features.clear(); |
| 145 | TargetOpts.Triple = TargetTriple; |
| 146 | AST->Target.reset(TargetInfo::CreateTargetInfo(AST->Diags, TargetOpts)); |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 147 | AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(), |
| 148 | AST->getSourceManager(), HeaderInfo)); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 149 | Preprocessor &PP = *AST->PP.get(); |
| 150 | |
Daniel Dunbar | d5b6126 | 2009-09-21 03:03:47 +0000 | [diff] [blame] | 151 | PP.setPredefines(Reader->getSuggestedPredefines()); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 152 | PP.setCounterValue(Counter); |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 153 | Reader->setPreprocessor(PP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 155 | // Create and initialize the ASTContext. |
| 156 | |
| 157 | AST->Ctx.reset(new ASTContext(LangInfo, |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 158 | AST->getSourceManager(), |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 159 | *AST->Target.get(), |
| 160 | PP.getIdentifierTable(), |
| 161 | PP.getSelectorTable(), |
| 162 | PP.getBuiltinInfo(), |
Ted Kremenek | 5cf4876 | 2009-10-17 00:34:24 +0000 | [diff] [blame] | 163 | /* FreeMemory = */ !UseBumpAllocator, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 164 | /* size_reserve = */0)); |
| 165 | ASTContext &Context = *AST->Ctx.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 167 | Reader->InitializeContext(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 169 | // Attach the PCH reader to the AST context as an external AST |
| 170 | // source, so that declarations will be deserialized from the |
| 171 | // PCH file as needed. |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 172 | Source.reset(Reader.take()); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 173 | Context.setExternalSource(Source); |
| 174 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | return AST.take(); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 176 | } |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 177 | |
| 178 | namespace { |
| 179 | |
| 180 | class NullAction : public ASTFrontendAction { |
| 181 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 182 | llvm::StringRef InFile) { |
| 183 | return new ASTConsumer(); |
| 184 | } |
| 185 | |
| 186 | public: |
| 187 | virtual bool hasCodeCompletionSupport() const { return false; } |
| 188 | }; |
| 189 | |
| 190 | } |
| 191 | |
| 192 | ASTUnit *ASTUnit::LoadFromCompilerInvocation(const CompilerInvocation &CI, |
| 193 | Diagnostic &Diags, |
| 194 | bool OnlyLocalDecls, |
| 195 | bool UseBumpAllocator) { |
| 196 | // Create the compiler instance to use for building the AST. |
| 197 | CompilerInstance Clang(&llvm::getGlobalContext(), false); |
| 198 | llvm::OwningPtr<ASTUnit> AST; |
| 199 | NullAction Act; |
| 200 | |
| 201 | Clang.getInvocation() = CI; |
| 202 | |
| 203 | Clang.setDiagnostics(&Diags); |
| 204 | Clang.setDiagnosticClient(Diags.getClient()); |
| 205 | |
| 206 | // Create the target instance. |
| 207 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 208 | Clang.getTargetOpts())); |
| 209 | if (!Clang.hasTarget()) |
| 210 | goto error; |
| 211 | |
| 212 | // Inform the target of the language options. |
| 213 | // |
| 214 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 215 | // created. This complexity should be lifted elsewhere. |
| 216 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
| 217 | |
| 218 | assert(Clang.getFrontendOpts().Inputs.size() == 1 && |
| 219 | "Invocation must have exactly one source file!"); |
| 220 | assert(Clang.getFrontendOpts().Inputs[0].first != FrontendOptions::IK_AST && |
| 221 | "FIXME: AST inputs not yet supported here!"); |
| 222 | |
| 223 | // Create the AST unit. |
| 224 | // |
| 225 | // FIXME: Use the provided diagnostic client. |
| 226 | AST.reset(new ASTUnit()); |
| 227 | |
| 228 | // Create a file manager object to provide access to and cache the filesystem. |
| 229 | Clang.setFileManager(&AST->getFileManager()); |
| 230 | |
| 231 | // Create the source manager. |
| 232 | Clang.setSourceManager(&AST->getSourceManager()); |
| 233 | |
| 234 | // Create the preprocessor. |
| 235 | Clang.createPreprocessor(); |
| 236 | |
| 237 | if (!Act.BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, |
| 238 | /*IsAST=*/false)) |
| 239 | goto error; |
| 240 | |
| 241 | Act.Execute(); |
| 242 | |
Daniel Dunbar | 64a32ba | 2009-12-01 21:57:33 +0000 | [diff] [blame^] | 243 | // Steal the created target, context, and preprocessor, and take back the |
| 244 | // source and file managers. |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 245 | AST->Ctx.reset(Clang.takeASTContext()); |
| 246 | AST->PP.reset(Clang.takePreprocessor()); |
| 247 | Clang.takeSourceManager(); |
| 248 | Clang.takeFileManager(); |
Daniel Dunbar | 64a32ba | 2009-12-01 21:57:33 +0000 | [diff] [blame^] | 249 | AST->Target.reset(Clang.takeTarget()); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 250 | |
| 251 | Act.EndSourceFile(); |
| 252 | |
| 253 | Clang.takeDiagnosticClient(); |
| 254 | Clang.takeDiagnostics(); |
| 255 | |
| 256 | return AST.take(); |
| 257 | |
| 258 | error: |
| 259 | Clang.takeSourceManager(); |
| 260 | Clang.takeFileManager(); |
| 261 | Clang.takeDiagnosticClient(); |
| 262 | Clang.takeDiagnostics(); |
| 263 | return 0; |
| 264 | } |