2009-06-20 16:27:14 +08:00
|
|
|
//===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// ASTUnit Implementation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#include "clang/Frontend/PCHReader.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/DeclVisitor.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-09-21 11:03:39 +08:00
|
|
|
ASTUnit::ASTUnit(Diagnostic &_Diags) : Diags(_Diags) { }
|
2009-06-20 16:08:23 +08:00
|
|
|
ASTUnit::~ASTUnit() { }
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// \brief Gathers information from PCHReader that will be used to initialize
|
|
|
|
/// a Preprocessor.
|
|
|
|
class VISIBILITY_HIDDEN PCHInfoCollector : public PCHReaderListener {
|
|
|
|
LangOptions &LangOpt;
|
|
|
|
HeaderSearch &HSI;
|
|
|
|
std::string &TargetTriple;
|
|
|
|
std::string &Predefines;
|
|
|
|
unsigned &Counter;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
unsigned NumHeaderInfos;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
public:
|
|
|
|
PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
|
|
|
|
std::string &TargetTriple, std::string &Predefines,
|
|
|
|
unsigned &Counter)
|
|
|
|
: LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
|
|
|
|
Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
|
|
|
|
LangOpt = LangOpts;
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
virtual bool ReadTargetTriple(const std::string &Triple) {
|
|
|
|
TargetTriple = Triple;
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
virtual bool ReadPredefinesBuffer(const char *PCHPredef,
|
2009-06-20 16:08:23 +08:00
|
|
|
unsigned PCHPredefLen,
|
|
|
|
FileID PCHBufferID,
|
|
|
|
std::string &SuggestedPredefines) {
|
|
|
|
Predefines = PCHPredef;
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
|
|
|
|
HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
virtual void ReadCounter(unsigned Value) {
|
|
|
|
Counter = Value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2009-09-04 02:19:54 +08:00
|
|
|
const std::string &ASTUnit::getOriginalSourceFileName() {
|
|
|
|
return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile();
|
|
|
|
}
|
2009-06-20 16:08:23 +08:00
|
|
|
|
2009-09-04 23:44:05 +08:00
|
|
|
FileManager &ASTUnit::getFileManager() {
|
|
|
|
return HeaderInfo->getFileMgr();
|
|
|
|
}
|
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
|
2009-09-21 11:03:39 +08:00
|
|
|
Diagnostic &Diags,
|
2009-06-20 16:08:23 +08:00
|
|
|
FileManager &FileMgr,
|
|
|
|
std::string *ErrMsg) {
|
2009-09-21 11:03:39 +08:00
|
|
|
llvm::OwningPtr<ASTUnit> AST(new ASTUnit(Diags));
|
2009-06-20 16:08:23 +08:00
|
|
|
|
|
|
|
AST->HeaderInfo.reset(new HeaderSearch(FileMgr));
|
|
|
|
|
|
|
|
// Gather Info for preprocessor construction later on.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
LangOptions LangInfo;
|
|
|
|
HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
|
|
|
|
std::string TargetTriple;
|
|
|
|
std::string Predefines;
|
|
|
|
unsigned Counter;
|
|
|
|
|
2009-09-03 13:59:50 +08:00
|
|
|
llvm::OwningPtr<PCHReader> Reader;
|
2009-06-20 16:08:23 +08:00
|
|
|
llvm::OwningPtr<ExternalASTSource> Source;
|
|
|
|
|
2009-09-21 11:03:39 +08:00
|
|
|
Reader.reset(new PCHReader(AST->getSourceManager(), FileMgr, AST->Diags));
|
2009-09-03 13:59:35 +08:00
|
|
|
Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
|
|
|
|
Predefines, Counter));
|
|
|
|
|
|
|
|
switch (Reader->ReadPCH(Filename)) {
|
2009-06-20 16:08:23 +08:00
|
|
|
case PCHReader::Success:
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
case PCHReader::Failure:
|
2009-06-26 02:22:30 +08:00
|
|
|
case PCHReader::IgnorePCH:
|
2009-06-20 16:08:23 +08:00
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Could not load PCH file";
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
// PCH loaded successfully. Now create the preprocessor.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
// Get information about the target being compiled for.
|
|
|
|
AST->Target.reset(TargetInfo::CreateTargetInfo(TargetTriple));
|
2009-09-21 11:03:39 +08:00
|
|
|
AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
|
|
|
|
AST->getSourceManager(), HeaderInfo));
|
2009-06-20 16:08:23 +08:00
|
|
|
Preprocessor &PP = *AST->PP.get();
|
|
|
|
|
2009-09-21 11:03:47 +08:00
|
|
|
PP.setPredefines(Reader->getSuggestedPredefines());
|
2009-06-20 16:08:23 +08:00
|
|
|
PP.setCounterValue(Counter);
|
2009-09-03 13:59:35 +08:00
|
|
|
Reader->setPreprocessor(PP);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
// Create and initialize the ASTContext.
|
|
|
|
|
|
|
|
AST->Ctx.reset(new ASTContext(LangInfo,
|
2009-09-21 11:03:39 +08:00
|
|
|
AST->getSourceManager(),
|
2009-06-20 16:08:23 +08:00
|
|
|
*AST->Target.get(),
|
|
|
|
PP.getIdentifierTable(),
|
|
|
|
PP.getSelectorTable(),
|
|
|
|
PP.getBuiltinInfo(),
|
|
|
|
/* FreeMemory = */ true,
|
|
|
|
/* size_reserve = */0));
|
|
|
|
ASTContext &Context = *AST->Ctx.get();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 13:59:35 +08:00
|
|
|
Reader->InitializeContext(Context);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
// Attach the PCH reader to the AST context as an external AST
|
|
|
|
// source, so that declarations will be deserialized from the
|
|
|
|
// PCH file as needed.
|
2009-09-03 13:59:35 +08:00
|
|
|
Source.reset(Reader.take());
|
2009-06-20 16:08:23 +08:00
|
|
|
Context.setExternalSource(Source);
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
return AST.take();
|
2009-06-20 16:08:23 +08:00
|
|
|
}
|