Moved LangOptions from TranslationUnit to ASTContext. This induced a variety of cleanups in some ASTConsumers.

llvm-svn: 51943
This commit is contained in:
Ted Kremenek 2008-06-04 15:55:15 +00:00
parent a6bb22e9bc
commit b147ad1051
9 changed files with 53 additions and 63 deletions

View File

@ -868,12 +868,10 @@ namespace {
class ASTSerializer : public ASTConsumer {
protected:
Diagnostic &Diags;
const LangOptions& lang;
TranslationUnit* TU;
public:
ASTSerializer(Diagnostic& diags, const LangOptions& LO)
: Diags(diags), lang(LO), TU(0) {}
ASTSerializer(Diagnostic& diags) : Diags(diags), TU(0) {}
virtual ~ASTSerializer() { delete TU; }
@ -892,9 +890,8 @@ public:
class SingleFileSerializer : public ASTSerializer {
const llvm::sys::Path FName;
public:
SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags,
const LangOptions &LO)
: ASTSerializer(diags,LO), FName(F) {}
SingleFileSerializer(const llvm::sys::Path& F, Diagnostic &diags)
: ASTSerializer(diags), FName(F) {}
~SingleFileSerializer() {
EmitASTBitcodeFile(TU, FName);
@ -904,9 +901,8 @@ public:
class BuildSerializer : public ASTSerializer {
llvm::sys::Path EmitDir;
public:
BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags,
const LangOptions &LO)
: ASTSerializer(diags,LO), EmitDir(dir) {}
BuildSerializer(const llvm::sys::Path& dir, Diagnostic &diags)
: ASTSerializer(diags), EmitDir(dir) {}
~BuildSerializer() {
@ -950,8 +946,7 @@ public:
ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
const std::string& OutputFile,
Diagnostic &Diags,
const LangOptions &Features) {
Diagnostic &Diags) {
if (OutputFile.size()) {
if (InFile == "-") {
@ -982,7 +977,7 @@ ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
// FIXME: We should probably only allow using BuildSerializer when
// the ASTs come from parsed source files, and not from .ast files.
return new BuildSerializer(EmitDir, Diags, Features);
return new BuildSerializer(EmitDir, Diags);
}
// The user did not specify an output directory for serialized ASTs.
@ -991,5 +986,5 @@ ASTConsumer* clang::CreateASTSerializer(const std::string& InFile,
llvm::sys::Path FName(InFile.c_str());
FName.appendSuffix("ast");
return new SingleFileSerializer(FName, Diags, Features);
return new SingleFileSerializer(FName, Diags);
}

View File

@ -67,13 +67,11 @@ ASTConsumer* CreateHTMLPrinter(const std::string &OutFile, Diagnostic &D,
Preprocessor *PP, PreprocessorFactory* PPF);
ASTConsumer *CreateSerializationTest(Diagnostic &Diags,
FileManager& FMgr,
const LangOptions &LOpts);
FileManager& FMgr);
ASTConsumer *CreateASTSerializer(const std::string& InFile,
const std::string& EmitDir,
Diagnostic &Diags,
const LangOptions &LOpts);
Diagnostic &Diags);
} // end clang namespace

View File

@ -33,26 +33,16 @@ using namespace clang;
namespace {
class SerializationTest : public ASTConsumer {
llvm::OwningPtr<TranslationUnit> TU;
TranslationUnit* TU;
Diagnostic &Diags;
FileManager &FMgr;
const LangOptions& lopts;
public:
SerializationTest(Diagnostic &d, FileManager& fmgr, const LangOptions& LOpts)
: Diags(d), FMgr(fmgr), lopts(LOpts) {}
SerializationTest(Diagnostic &d, FileManager& fmgr)
: TU(0), Diags(d), FMgr(fmgr) {}
~SerializationTest();
virtual void Initialize(ASTContext& context) {
if (!TU) {
TU.reset(new TranslationUnit(context, lopts));
TU->SetOwnsDecls(false);
}
}
virtual void HandleTopLevelDecl(Decl *D) {
TU->AddTopLevelDecl(D);
}
virtual void InitializeTU(TranslationUnit& tu) { TU = &tu; }
private:
bool Serialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint);
@ -62,10 +52,8 @@ private:
} // end anonymous namespace
ASTConsumer*
clang::CreateSerializationTest(Diagnostic &Diags, FileManager& FMgr,
const LangOptions &LOpts) {
return new SerializationTest(Diags,FMgr,LOpts);
clang::CreateSerializationTest(Diagnostic &Diags, FileManager& FMgr) {
return new SerializationTest(Diags, FMgr);
}
@ -89,7 +77,7 @@ bool SerializationTest::Deserialize(llvm::sys::Path& Filename,
llvm::sys::Path& FNameDeclPrint) {
// Deserialize the translation unit.
TranslationUnit* NewTU = ReadASTBitcodeFile(Filename,FMgr);
TranslationUnit* NewTU = ReadASTBitcodeFile(Filename, FMgr);
if (!NewTU)
return false;

View File

@ -1189,7 +1189,7 @@ static ASTConsumer* CreateASTConsumer(const std::string& InFile,
OutputFile, VisualizeEG, TrimGraph, AnalyzeAll);
case TestSerialization:
return CreateSerializationTest(Diag, FileMgr, LangOpts);
return CreateSerializationTest(Diag, FileMgr);
case EmitLLVM:
case EmitBC:
@ -1198,7 +1198,7 @@ static ASTConsumer* CreateASTConsumer(const std::string& InFile,
case SerializeAST:
// FIXME: Allow user to tailor where the file is written.
return CreateASTSerializer(InFile, OutputFile, Diag, LangOpts);
return CreateASTSerializer(InFile, OutputFile, Diag);
case RewriteObjC:
return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts);
@ -1362,8 +1362,8 @@ static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag,
// translation unit, rather than InFile.
llvm::Module *DestModule;
llvm::OwningPtr<ASTConsumer>
Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOpts(), 0, 0,
DestModule));
Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOptions(),
0, 0, DestModule));
if (!Consumer) {
fprintf(stderr, "Unsupported program action with serialized ASTs!\n");

View File

@ -14,6 +14,7 @@
#ifndef LLVM_CLANG_AST_ASTCONTEXT_H
#define LLVM_CLANG_AST_ASTCONTEXT_H
#include "clang/Basic/LangOptions.h"
#include "clang/AST/Builtins.h"
#include "clang/AST/Expr.h"
#include "clang/AST/RecordLayout.h"
@ -77,15 +78,24 @@ class ASTContext {
TranslationUnitDecl *TUDecl;
/// SourceMgr - The associated SourceManager object.
SourceManager &SourceMgr;
/// LangOpts - The language options used to create the AST associated with
/// this ASTContext object.
LangOptions LangOpts;
/// Allocator - The allocator object used to create AST objects.
llvm::MallocAllocator Allocator;
public:
TargetInfo &Target;
IdentifierTable &Idents;
SelectorTable &Selectors;
SourceManager& getSourceManager() { return SourceMgr; }
llvm::MallocAllocator &getAllocator() { return Allocator; }
llvm::MallocAllocator &getAllocator() { return Allocator; }
const LangOptions& getLangOptions() const { return LangOpts; }
FullSourceLoc getFullLoc(SourceLocation Loc) const {
return FullSourceLoc(Loc,SourceMgr);
@ -109,9 +119,10 @@ public:
QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
QualType VoidPtrTy;
ASTContext(SourceManager &SM, TargetInfo &t, IdentifierTable &idents,
SelectorTable &sels, unsigned size_reserve=0 ) :
CFConstantStringTypeDecl(0), SourceMgr(SM), Target(t),
ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t,
IdentifierTable &idents, SelectorTable &sels,
unsigned size_reserve=0 ) :
CFConstantStringTypeDecl(0), SourceMgr(SM), LangOpts(LOpts), Target(t),
Idents(idents), Selectors(sels) {
if (size_reserve > 0) Types.reserve(size_reserve);

View File

@ -13,7 +13,7 @@
#ifndef LLVM_CLANG_TRANSLATION_UNIT_H
#define LLVM_CLANG_TRANSLATION_UNIT_H
#include "clang/Basic/LangOptions.h"
#include "clang/AST/ASTContext.h"
#include "llvm/Bitcode/SerializationFwd.h"
#include "llvm/System/Path.h"
#include <vector>
@ -31,7 +31,6 @@ class Decl;
class FileEntry;
class TranslationUnit {
LangOptions LangOpts;
ASTContext* Context;
std::vector<Decl*> TopLevelDecls;
bool OwnsMetaData;
@ -42,15 +41,13 @@ class TranslationUnit {
OwnsDecls(true) {}
public:
explicit TranslationUnit(ASTContext& Ctx, const LangOptions& lopt)
: LangOpts(lopt), Context(&Ctx), OwnsMetaData(false),
OwnsDecls(true) {}
explicit TranslationUnit(ASTContext& Ctx)
: Context(&Ctx), OwnsMetaData(false), OwnsDecls(true) {}
void SetOwnsDecls(bool val) { OwnsDecls = val; }
~TranslationUnit();
const LangOptions& getLangOpts() const { return LangOpts; }
const std::string& getSourceFile() const;
/// Emit - Emit the translation unit to an arbitray bitcode stream.
@ -60,7 +57,7 @@ public:
static TranslationUnit* Create(llvm::Deserializer& D, FileManager& FMgr);
// Accessors
const LangOptions& getLangOptions() const { return LangOpts; }
const LangOptions& getLangOptions() const { return Context->getLangOptions();}
ASTContext& getContext() { return *Context; }
const ASTContext& getContext() const { return *Context; }

View File

@ -1701,6 +1701,7 @@ bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
/// Emit - Serialize an ASTContext object to Bitcode.
void ASTContext::Emit(llvm::Serializer& S) const {
S.Emit(LangOpts);
S.EmitRef(SourceMgr);
S.EmitRef(Target);
S.EmitRef(Idents);
@ -1720,6 +1721,11 @@ void ASTContext::Emit(llvm::Serializer& S) const {
}
ASTContext* ASTContext::Create(llvm::Deserializer& D) {
// Read the language options.
LangOptions LOpts;
LOpts.Read(D);
SourceManager &SM = D.ReadRef<SourceManager>();
TargetInfo &t = D.ReadRef<TargetInfo>();
IdentifierTable &idents = D.ReadRef<IdentifierTable>();
@ -1727,7 +1733,7 @@ ASTContext* ASTContext::Create(llvm::Deserializer& D) {
unsigned size_reserve = D.ReadInt();
ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
for (unsigned i = 0; i < size_reserve; ++i)
Type::Create(*A,i,D);

View File

@ -124,16 +124,13 @@ void TranslationUnit::Emit(llvm::Serializer& Sezr) const {
Sezr.EnterBlock(BasicMetadataBlock);
// Block for SourceManager, LangOptions, and Target. Allows easy skipping
// Block for SourceManager and Target. Allows easy skipping
// around to the block for the Selectors during deserialization.
Sezr.EnterBlock();
// Emit the SourceManager.
Sezr.Emit(Context->getSourceManager());
// Emit the LangOptions.
Sezr.Emit(LangOpts);
// Emit the Target.
Sezr.EmitPtr(&Context->Target);
Sezr.EmitCStr(Context->Target.getTargetTriple());
@ -211,10 +208,7 @@ TranslationUnit* TranslationUnit::Create(llvm::Deserializer& Dezr,
// Read the SourceManager.
SourceManager::CreateAndRegister(Dezr,FMgr);
// Read the LangOptions.
TU->LangOpts.Read(Dezr);
{ // Read the TargetInfo.
llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
char* triple = Dezr.ReadCStr(NULL,0,true);

View File

@ -34,10 +34,11 @@ void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, bool PrintStats) {
Stmt::CollectingStats(true);
}
ASTContext Context(PP.getSourceManager(), PP.getTargetInfo(),
ASTContext Context(PP.getLangOptions(), PP.getSourceManager(),
PP.getTargetInfo(),
PP.getIdentifierTable(), PP.getSelectorTable());
TranslationUnit TU(Context, PP.getLangOptions());
TranslationUnit TU(Context);
Sema S(PP, Context, *Consumer);
Parser P(PP, S);