From 2c055d2b2b9d8387610ae52c63f4daba986224f5 Mon Sep 17 00:00:00 2001 From: Steve Naroff Date: Wed, 28 Feb 2007 19:32:13 +0000 Subject: [PATCH] Go back to having the clang driver create ASTContext explicitly, passing it to Sema/ASTStreamer (separating the lifetime of ASTContext from the lifetime of Sema). One day it might be useful to consider creating a context object implicitly if one isn't provided (using default arguments in Sema's constructor). At this point, adding this convenience isn't necessary. llvm-svn: 39346 --- clang/AST/ASTStreamer.cpp | 9 +++++---- clang/AST/Sema.cpp | 10 ++-------- clang/AST/Sema.h | 4 +--- clang/Driver/clang.cpp | 8 ++++++-- clang/Sema/ASTStreamer.cpp | 9 +++++---- clang/Sema/Sema.cpp | 10 ++-------- clang/Sema/Sema.h | 4 +--- clang/include/clang/AST/ASTStreamer.h | 4 +++- clang/include/clang/Sema/ASTStreamer.h | 4 +++- 9 files changed, 28 insertions(+), 34 deletions(-) diff --git a/clang/AST/ASTStreamer.cpp b/clang/AST/ASTStreamer.cpp index 2046fe62fdac..25319ea2a18e 100644 --- a/clang/AST/ASTStreamer.cpp +++ b/clang/AST/ASTStreamer.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "clang/AST/ASTStreamer.h" +#include "clang/AST/ASTContext.h" #include "Sema.h" #include "clang/Parse/Action.h" #include "clang/Parse/Parser.h" @@ -23,8 +24,8 @@ namespace { Parser P; std::vector LastInGroupList; public: - ASTStreamer(Preprocessor &pp, unsigned MainFileID) - : P(pp, *new Sema(pp, LastInGroupList)) { + ASTStreamer(Preprocessor &pp, ASTContext &ctxt, unsigned MainFileID) + : P(pp, *new Sema(pp, ctxt, LastInGroupList)) { pp.EnterSourceFile(MainFileID, 0, true); // Initialize the parser. @@ -78,7 +79,6 @@ Decl *ASTStreamer::ReadTopLevelDecl() { } void ASTStreamer::PrintStats() const { - static_cast(P.getActions()).PrintStats(); } //===----------------------------------------------------------------------===// @@ -88,8 +88,9 @@ void ASTStreamer::PrintStats() const { /// ASTStreamer_Init - Create an ASTStreamer with the specified preprocessor /// and FileID. ASTStreamerTy *llvm::clang::ASTStreamer_Init(Preprocessor &pp, + ASTContext &ctxt, unsigned MainFileID) { - return new ASTStreamer(pp, MainFileID); + return new ASTStreamer(pp, ctxt, MainFileID); } /// ASTStreamer_ReadTopLevelDecl - Parse and return one top-level declaration. This diff --git a/clang/AST/Sema.cpp b/clang/AST/Sema.cpp index 9933e872e5c1..11c548fa85b5 100644 --- a/clang/AST/Sema.cpp +++ b/clang/AST/Sema.cpp @@ -18,14 +18,8 @@ using namespace llvm; using namespace clang; -Sema::Sema(Preprocessor &pp, std::vector &prevInGroup) - : PP(pp), - Context(*new ASTContext(pp.getTargetInfo(), pp.getIdentifierTable())), - CurFunctionDecl(0), LastInGroupList(prevInGroup) { -} - -void Sema::PrintStats() { - Context.PrintStats(); +Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector &prevInGroup) + : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) { } //===----------------------------------------------------------------------===// diff --git a/clang/AST/Sema.h b/clang/AST/Sema.h index f38715b74637..23c525b7ea65 100644 --- a/clang/AST/Sema.h +++ b/clang/AST/Sema.h @@ -47,15 +47,13 @@ class Sema : public Action { /// ASTStreamer. std::vector &LastInGroupList; public: - Sema(Preprocessor &pp, std::vector &prevInGroup); + Sema(Preprocessor &pp, ASTContext &ctxt, std::vector &prevInGroup); const LangOptions &getLangOptions() const; void Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg = std::string()); - void PrintStats(); - //===--------------------------------------------------------------------===// // Type Analysis / Processing: SemaType.cpp. // diff --git a/clang/Driver/clang.cpp b/clang/Driver/clang.cpp index a53ce94bb465..8a980a653f3e 100644 --- a/clang/Driver/clang.cpp +++ b/clang/Driver/clang.cpp @@ -803,13 +803,15 @@ static void ParseFile(Preprocessor &PP, MinimalAction *PA, unsigned MainFileID){ //===----------------------------------------------------------------------===// static void BuildASTs(Preprocessor &PP, unsigned MainFileID) { - ASTStreamerTy *Streamer = ASTStreamer_Init(PP, MainFileID); + ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable()); + ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID); while (ASTStreamer_ReadTopLevelDecl(Streamer)) /* keep reading */; if (Stats) { std::cerr << "\nSTATISTICS:\n"; ASTStreamer_PrintStats(Streamer); + Context.PrintStats(); } ASTStreamer_Terminate(Streamer); @@ -860,7 +862,8 @@ static void PrintTypeDefDecl(TypedefDecl *TD) { } static void PrintASTs(Preprocessor &PP, unsigned MainFileID) { - ASTStreamerTy *Streamer = ASTStreamer_Init(PP, MainFileID); + ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable()); + ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID); while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) { if (FunctionDecl *FD = dyn_cast(D)) { @@ -875,6 +878,7 @@ static void PrintASTs(Preprocessor &PP, unsigned MainFileID) { if (Stats) { std::cerr << "\nSTATISTICS:\n"; ASTStreamer_PrintStats(Streamer); + Context.PrintStats(); } ASTStreamer_Terminate(Streamer); diff --git a/clang/Sema/ASTStreamer.cpp b/clang/Sema/ASTStreamer.cpp index 2046fe62fdac..25319ea2a18e 100644 --- a/clang/Sema/ASTStreamer.cpp +++ b/clang/Sema/ASTStreamer.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "clang/AST/ASTStreamer.h" +#include "clang/AST/ASTContext.h" #include "Sema.h" #include "clang/Parse/Action.h" #include "clang/Parse/Parser.h" @@ -23,8 +24,8 @@ namespace { Parser P; std::vector LastInGroupList; public: - ASTStreamer(Preprocessor &pp, unsigned MainFileID) - : P(pp, *new Sema(pp, LastInGroupList)) { + ASTStreamer(Preprocessor &pp, ASTContext &ctxt, unsigned MainFileID) + : P(pp, *new Sema(pp, ctxt, LastInGroupList)) { pp.EnterSourceFile(MainFileID, 0, true); // Initialize the parser. @@ -78,7 +79,6 @@ Decl *ASTStreamer::ReadTopLevelDecl() { } void ASTStreamer::PrintStats() const { - static_cast(P.getActions()).PrintStats(); } //===----------------------------------------------------------------------===// @@ -88,8 +88,9 @@ void ASTStreamer::PrintStats() const { /// ASTStreamer_Init - Create an ASTStreamer with the specified preprocessor /// and FileID. ASTStreamerTy *llvm::clang::ASTStreamer_Init(Preprocessor &pp, + ASTContext &ctxt, unsigned MainFileID) { - return new ASTStreamer(pp, MainFileID); + return new ASTStreamer(pp, ctxt, MainFileID); } /// ASTStreamer_ReadTopLevelDecl - Parse and return one top-level declaration. This diff --git a/clang/Sema/Sema.cpp b/clang/Sema/Sema.cpp index 9933e872e5c1..11c548fa85b5 100644 --- a/clang/Sema/Sema.cpp +++ b/clang/Sema/Sema.cpp @@ -18,14 +18,8 @@ using namespace llvm; using namespace clang; -Sema::Sema(Preprocessor &pp, std::vector &prevInGroup) - : PP(pp), - Context(*new ASTContext(pp.getTargetInfo(), pp.getIdentifierTable())), - CurFunctionDecl(0), LastInGroupList(prevInGroup) { -} - -void Sema::PrintStats() { - Context.PrintStats(); +Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector &prevInGroup) + : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) { } //===----------------------------------------------------------------------===// diff --git a/clang/Sema/Sema.h b/clang/Sema/Sema.h index f38715b74637..23c525b7ea65 100644 --- a/clang/Sema/Sema.h +++ b/clang/Sema/Sema.h @@ -47,15 +47,13 @@ class Sema : public Action { /// ASTStreamer. std::vector &LastInGroupList; public: - Sema(Preprocessor &pp, std::vector &prevInGroup); + Sema(Preprocessor &pp, ASTContext &ctxt, std::vector &prevInGroup); const LangOptions &getLangOptions() const; void Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg = std::string()); - void PrintStats(); - //===--------------------------------------------------------------------===// // Type Analysis / Processing: SemaType.cpp. // diff --git a/clang/include/clang/AST/ASTStreamer.h b/clang/include/clang/AST/ASTStreamer.h index 39be651b2af8..b57593c91b56 100644 --- a/clang/include/clang/AST/ASTStreamer.h +++ b/clang/include/clang/AST/ASTStreamer.h @@ -17,6 +17,7 @@ namespace llvm { namespace clang { class Preprocessor; + class ASTContext; class Decl; /// ASTStreamerTy - This is an opaque type used to reference ASTStreamer @@ -25,7 +26,8 @@ namespace clang { /// ASTStreamer_Init - Create an ASTStreamer with the specified ASTContext /// and FileID. - ASTStreamerTy *ASTStreamer_Init(Preprocessor &pp, unsigned MainFileID); + ASTStreamerTy *ASTStreamer_Init(Preprocessor &pp, ASTContext &ctxt, + unsigned MainFileID); /// ASTStreamer_ReadTopLevelDecl - Parse and return one top-level declaration. /// This returns null at end of file. diff --git a/clang/include/clang/Sema/ASTStreamer.h b/clang/include/clang/Sema/ASTStreamer.h index 39be651b2af8..b57593c91b56 100644 --- a/clang/include/clang/Sema/ASTStreamer.h +++ b/clang/include/clang/Sema/ASTStreamer.h @@ -17,6 +17,7 @@ namespace llvm { namespace clang { class Preprocessor; + class ASTContext; class Decl; /// ASTStreamerTy - This is an opaque type used to reference ASTStreamer @@ -25,7 +26,8 @@ namespace clang { /// ASTStreamer_Init - Create an ASTStreamer with the specified ASTContext /// and FileID. - ASTStreamerTy *ASTStreamer_Init(Preprocessor &pp, unsigned MainFileID); + ASTStreamerTy *ASTStreamer_Init(Preprocessor &pp, ASTContext &ctxt, + unsigned MainFileID); /// ASTStreamer_ReadTopLevelDecl - Parse and return one top-level declaration. /// This returns null at end of file.