[cfe-commits] r39345 - in /cfe/cfe/trunk: AST/ASTContext.cpp AST/ASTStreamer.cpp AST/Sema.cpp AST/Sema.h AST/SemaExpr.cpp Driver/clang.cpp Sema/ASTStreamer.cpp Sema/Sema.cpp Sema/Sema.h Sema/SemaExpr.cpp include/clang/AST/ASTContext.h include/clang/AST/ASTStreamer.h include/clang/Sema/ASTStreamer.h

snaroff at cs.uiuc.edu snaroff at cs.uiuc.edu
Wed Jul 11 09:43:24 PDT 2007


Author: snaroff
Date: Wed Jul 11 11:43:24 2007
New Revision: 39345

URL: http://llvm.org/viewvc/llvm-project?rev=39345&view=rev
Log:
Make Sema's use of the preprocessor explicit (rather than assume
the prerocessor will be available via ASTContext).
- Removed the public data member "PP" in ASTContext.
- Changed ASTContext's contructor to take TargetInfo/IdentifierTable explicitly.
- Implicitly create an ASTContext in Sema's constructor. This simplifies
the clients job (and makes ASTContext more private).
--As a side effect, added a "PrintStats" hook to Sema.

To support this level of encapsulation, ASTContext is always dynamically
allocated (by Sema). Previously, ASTContext was being allocated on the
stack. I don't believe this should be a performance issue (since ASTContext
is fairly course grain and tied to the lifetime of Sema currently).

Modified:
    cfe/cfe/trunk/AST/ASTContext.cpp
    cfe/cfe/trunk/AST/ASTStreamer.cpp
    cfe/cfe/trunk/AST/Sema.cpp
    cfe/cfe/trunk/AST/Sema.h
    cfe/cfe/trunk/AST/SemaExpr.cpp
    cfe/cfe/trunk/Driver/clang.cpp
    cfe/cfe/trunk/Sema/ASTStreamer.cpp
    cfe/cfe/trunk/Sema/Sema.cpp
    cfe/cfe/trunk/Sema/Sema.h
    cfe/cfe/trunk/Sema/SemaExpr.cpp
    cfe/cfe/trunk/include/clang/AST/ASTContext.h
    cfe/cfe/trunk/include/clang/AST/ASTStreamer.h
    cfe/cfe/trunk/include/clang/Sema/ASTStreamer.h

Modified: cfe/cfe/trunk/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/ASTContext.cpp?rev=39345&r1=39344&r2=39345&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/ASTContext.cpp (original)
+++ cfe/cfe/trunk/AST/ASTContext.cpp Wed Jul 11 11:43:24 2007
@@ -18,12 +18,6 @@
 using namespace llvm;
 using namespace clang;
 
-ASTContext::ASTContext(Preprocessor &pp)
-  : PP(pp), Target(pp.getTargetInfo()) {
-  InitBuiltinTypes();
-  BuiltinInfo.InitializeBuiltins(PP.getIdentifierTable(), Target);
-}
-
 ASTContext::~ASTContext() {
   // Deallocate all the types.
   while (!Types.empty()) {

Modified: cfe/cfe/trunk/AST/ASTStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/ASTStreamer.cpp?rev=39345&r1=39344&r2=39345&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/ASTStreamer.cpp (original)
+++ cfe/cfe/trunk/AST/ASTStreamer.cpp Wed Jul 11 11:43:24 2007
@@ -12,7 +12,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/AST/ASTStreamer.h"
-#include "clang/AST/ASTContext.h"
 #include "Sema.h"
 #include "clang/Parse/Action.h"
 #include "clang/Parse/Parser.h"
@@ -24,9 +23,9 @@
     Parser P;
     std::vector<Decl*> LastInGroupList;
   public:
-    ASTStreamer(ASTContext &Ctx, unsigned MainFileID)
-      : P(Ctx.PP, *new Sema(Ctx, LastInGroupList)) {
-      Ctx.PP.EnterSourceFile(MainFileID, 0, true);
+    ASTStreamer(Preprocessor &pp, unsigned MainFileID)
+      : P(pp, *new Sema(pp, LastInGroupList)) {
+      pp.EnterSourceFile(MainFileID, 0, true);
       
       // Initialize the parser.
       P.Initialize();
@@ -79,7 +78,7 @@
 }
 
 void ASTStreamer::PrintStats() const {
-  
+  static_cast<Sema &>(P.getActions()).PrintStats();
 }
 
 //===----------------------------------------------------------------------===//
@@ -88,9 +87,9 @@
 
 /// ASTStreamer_Init - Create an ASTStreamer with the specified preprocessor
 /// and FileID.
-ASTStreamerTy *llvm::clang::ASTStreamer_Init(ASTContext &Ctx, 
+ASTStreamerTy *llvm::clang::ASTStreamer_Init(Preprocessor &pp,
                                              unsigned MainFileID) {
-  return new ASTStreamer(Ctx, MainFileID);
+  return new ASTStreamer(pp, MainFileID);
 }
 
 /// ASTStreamer_ReadTopLevelDecl - Parse and return one top-level declaration. This

Modified: cfe/cfe/trunk/AST/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Sema.cpp?rev=39345&r1=39344&r2=39345&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/Sema.cpp (original)
+++ cfe/cfe/trunk/AST/Sema.cpp Wed Jul 11 11:43:24 2007
@@ -18,15 +18,24 @@
 using namespace llvm;
 using namespace clang;
 
+Sema::Sema(Preprocessor &pp, std::vector<Decl*> &prevInGroup)
+  : PP(pp), 
+    Context(*new ASTContext(pp.getTargetInfo(), pp.getIdentifierTable())),
+    CurFunctionDecl(0), LastInGroupList(prevInGroup) {
+}
+
+void Sema::PrintStats() { 
+  Context.PrintStats(); 
+}
 
 //===----------------------------------------------------------------------===//
 // Helper functions.
 //===----------------------------------------------------------------------===//
 
 void Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
-  Context.PP.Diag(Loc, DiagID, Msg);
+  PP.Diag(Loc, DiagID, Msg);
 }
 
 const LangOptions &Sema::getLangOptions() const {
-  return Context.PP.getLangOptions();
+  return PP.getLangOptions();
 }

Modified: cfe/cfe/trunk/AST/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Sema.h?rev=39345&r1=39344&r2=39345&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/Sema.h (original)
+++ cfe/cfe/trunk/AST/Sema.h Wed Jul 11 11:43:24 2007
@@ -33,6 +33,8 @@
   
 /// Sema - This implements semantic analysis and AST building for C.
 class Sema : public Action {
+  Preprocessor &PP;
+  
   ASTContext &Context;
   
   /// CurFunctionDecl - If inside of a function body, this contains a pointer to
@@ -45,15 +47,15 @@
   /// ASTStreamer.
   std::vector<Decl*> &LastInGroupList;
 public:
-  Sema(ASTContext &ctx, std::vector<Decl*> &prevInGroup)
-    : Context(ctx), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
-  }
+  Sema(Preprocessor &pp, std::vector<Decl*> &prevInGroup);
   
   const LangOptions &getLangOptions() const;
   
   void Diag(SourceLocation Loc, unsigned DiagID,
             const std::string &Msg = std::string());
   
+  void PrintStats();
+  
   //===--------------------------------------------------------------------===//
   // Type Analysis / Processing: SemaType.cpp.
   //

Modified: cfe/cfe/trunk/AST/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/SemaExpr.cpp?rev=39345&r1=39344&r2=39345&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/SemaExpr.cpp (original)
+++ cfe/cfe/trunk/AST/SemaExpr.cpp Wed Jul 11 11:43:24 2007
@@ -101,7 +101,7 @@
     // Get the spelling of the token, which eliminates trigraphs, etc.  We know
     // that ThisTokBuf points to a buffer that is big enough for the whole token
     // and 'spelled' tokens can only shrink.
-    unsigned ThisTokLen = Context.PP.getSpelling(StringToks[i], ThisTokBuf);
+    unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
     const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1;  // Skip end quote.
     
     // TODO: Input character set mapping support.
@@ -196,7 +196,7 @@
       // Otherwise, these are not valid escapes.
       case '(': case '{': case '[': case '%':
         // GCC accepts these as extensions.  We warn about them as such though.
-        if (!Context.PP.getLangOptions().NoExtensions) {
+        if (!PP.getLangOptions().NoExtensions) {
           Diag(StringToks[i].getLocation(), diag::ext_nonstandard_escape,
                std::string()+(char)ResultChar);
           break;

Modified: cfe/cfe/trunk/Driver/clang.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Driver/clang.cpp?rev=39345&r1=39344&r2=39345&view=diff

==============================================================================
--- cfe/cfe/trunk/Driver/clang.cpp (original)
+++ cfe/cfe/trunk/Driver/clang.cpp Wed Jul 11 11:43:24 2007
@@ -803,16 +803,13 @@
 //===----------------------------------------------------------------------===//
 
 static void BuildASTs(Preprocessor &PP, unsigned MainFileID) {
-  ASTContext Context(PP);
-  
-  ASTStreamerTy *Streamer = ASTStreamer_Init(Context, MainFileID);
+  ASTStreamerTy *Streamer = ASTStreamer_Init(PP, MainFileID);
   while (ASTStreamer_ReadTopLevelDecl(Streamer))
     /* keep reading */;
 
   if (Stats) {
     std::cerr << "\nSTATISTICS:\n";
     ASTStreamer_PrintStats(Streamer);
-    Context.PrintStats();
   }
   
   ASTStreamer_Terminate(Streamer);
@@ -863,8 +860,7 @@
 }
 
 static void PrintASTs(Preprocessor &PP, unsigned MainFileID) {
-  ASTContext Context(PP);
-  ASTStreamerTy *Streamer = ASTStreamer_Init(Context, MainFileID);
+  ASTStreamerTy *Streamer = ASTStreamer_Init(PP, MainFileID);
   
   while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
@@ -879,7 +875,6 @@
   if (Stats) {
     std::cerr << "\nSTATISTICS:\n";
     ASTStreamer_PrintStats(Streamer);
-    Context.PrintStats();
   }
   
   ASTStreamer_Terminate(Streamer);

Modified: cfe/cfe/trunk/Sema/ASTStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/ASTStreamer.cpp?rev=39345&r1=39344&r2=39345&view=diff

==============================================================================
--- cfe/cfe/trunk/Sema/ASTStreamer.cpp (original)
+++ cfe/cfe/trunk/Sema/ASTStreamer.cpp Wed Jul 11 11:43:24 2007
@@ -12,7 +12,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/AST/ASTStreamer.h"
-#include "clang/AST/ASTContext.h"
 #include "Sema.h"
 #include "clang/Parse/Action.h"
 #include "clang/Parse/Parser.h"
@@ -24,9 +23,9 @@
     Parser P;
     std::vector<Decl*> LastInGroupList;
   public:
-    ASTStreamer(ASTContext &Ctx, unsigned MainFileID)
-      : P(Ctx.PP, *new Sema(Ctx, LastInGroupList)) {
-      Ctx.PP.EnterSourceFile(MainFileID, 0, true);
+    ASTStreamer(Preprocessor &pp, unsigned MainFileID)
+      : P(pp, *new Sema(pp, LastInGroupList)) {
+      pp.EnterSourceFile(MainFileID, 0, true);
       
       // Initialize the parser.
       P.Initialize();
@@ -79,7 +78,7 @@
 }
 
 void ASTStreamer::PrintStats() const {
-  
+  static_cast<Sema &>(P.getActions()).PrintStats();
 }
 
 //===----------------------------------------------------------------------===//
@@ -88,9 +87,9 @@
 
 /// ASTStreamer_Init - Create an ASTStreamer with the specified preprocessor
 /// and FileID.
-ASTStreamerTy *llvm::clang::ASTStreamer_Init(ASTContext &Ctx, 
+ASTStreamerTy *llvm::clang::ASTStreamer_Init(Preprocessor &pp,
                                              unsigned MainFileID) {
-  return new ASTStreamer(Ctx, MainFileID);
+  return new ASTStreamer(pp, MainFileID);
 }
 
 /// ASTStreamer_ReadTopLevelDecl - Parse and return one top-level declaration. This

Modified: cfe/cfe/trunk/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/Sema.cpp?rev=39345&r1=39344&r2=39345&view=diff

==============================================================================
--- cfe/cfe/trunk/Sema/Sema.cpp (original)
+++ cfe/cfe/trunk/Sema/Sema.cpp Wed Jul 11 11:43:24 2007
@@ -18,15 +18,24 @@
 using namespace llvm;
 using namespace clang;
 
+Sema::Sema(Preprocessor &pp, std::vector<Decl*> &prevInGroup)
+  : PP(pp), 
+    Context(*new ASTContext(pp.getTargetInfo(), pp.getIdentifierTable())),
+    CurFunctionDecl(0), LastInGroupList(prevInGroup) {
+}
+
+void Sema::PrintStats() { 
+  Context.PrintStats(); 
+}
 
 //===----------------------------------------------------------------------===//
 // Helper functions.
 //===----------------------------------------------------------------------===//
 
 void Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
-  Context.PP.Diag(Loc, DiagID, Msg);
+  PP.Diag(Loc, DiagID, Msg);
 }
 
 const LangOptions &Sema::getLangOptions() const {
-  return Context.PP.getLangOptions();
+  return PP.getLangOptions();
 }

Modified: cfe/cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/Sema.h?rev=39345&r1=39344&r2=39345&view=diff

==============================================================================
--- cfe/cfe/trunk/Sema/Sema.h (original)
+++ cfe/cfe/trunk/Sema/Sema.h Wed Jul 11 11:43:24 2007
@@ -33,6 +33,8 @@
   
 /// Sema - This implements semantic analysis and AST building for C.
 class Sema : public Action {
+  Preprocessor &PP;
+  
   ASTContext &Context;
   
   /// CurFunctionDecl - If inside of a function body, this contains a pointer to
@@ -45,15 +47,15 @@
   /// ASTStreamer.
   std::vector<Decl*> &LastInGroupList;
 public:
-  Sema(ASTContext &ctx, std::vector<Decl*> &prevInGroup)
-    : Context(ctx), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
-  }
+  Sema(Preprocessor &pp, std::vector<Decl*> &prevInGroup);
   
   const LangOptions &getLangOptions() const;
   
   void Diag(SourceLocation Loc, unsigned DiagID,
             const std::string &Msg = std::string());
   
+  void PrintStats();
+  
   //===--------------------------------------------------------------------===//
   // Type Analysis / Processing: SemaType.cpp.
   //

Modified: cfe/cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/SemaExpr.cpp?rev=39345&r1=39344&r2=39345&view=diff

==============================================================================
--- cfe/cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/cfe/trunk/Sema/SemaExpr.cpp Wed Jul 11 11:43:24 2007
@@ -101,7 +101,7 @@
     // Get the spelling of the token, which eliminates trigraphs, etc.  We know
     // that ThisTokBuf points to a buffer that is big enough for the whole token
     // and 'spelled' tokens can only shrink.
-    unsigned ThisTokLen = Context.PP.getSpelling(StringToks[i], ThisTokBuf);
+    unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
     const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1;  // Skip end quote.
     
     // TODO: Input character set mapping support.
@@ -196,7 +196,7 @@
       // Otherwise, these are not valid escapes.
       case '(': case '{': case '[': case '%':
         // GCC accepts these as extensions.  We warn about them as such though.
-        if (!Context.PP.getLangOptions().NoExtensions) {
+        if (!PP.getLangOptions().NoExtensions) {
           Diag(StringToks[i].getLocation(), diag::ext_nonstandard_escape,
                std::string()+(char)ResultChar);
           break;

Modified: cfe/cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/AST/ASTContext.h?rev=39345&r1=39344&r2=39345&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/cfe/trunk/include/clang/AST/ASTContext.h Wed Jul 11 11:43:24 2007
@@ -21,7 +21,6 @@
 
 namespace llvm {
 namespace clang {
-  class Preprocessor;
   class TargetInfo;
   
 /// ASTContext - This class holds long-lived AST nodes (such as types and
@@ -33,7 +32,6 @@
   FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos;
   FoldingSet<FunctionTypeProto> FunctionTypeProtos;
 public:
-  Preprocessor &PP;
   TargetInfo &Target;
   Builtin::Context BuiltinInfo;
 
@@ -47,7 +45,10 @@
   TypeRef FloatTy, DoubleTy, LongDoubleTy;
   TypeRef FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
   
-  ASTContext(Preprocessor &pp);
+  ASTContext(TargetInfo &t, IdentifierTable &idents) : Target(t) {
+    InitBuiltinTypes();
+    BuiltinInfo.InitializeBuiltins(idents, Target);
+  }    
   ~ASTContext();
   
   void PrintStats() const;

Modified: cfe/cfe/trunk/include/clang/AST/ASTStreamer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/AST/ASTStreamer.h?rev=39345&r1=39344&r2=39345&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/ASTStreamer.h (original)
+++ cfe/cfe/trunk/include/clang/AST/ASTStreamer.h Wed Jul 11 11:43:24 2007
@@ -16,7 +16,7 @@
 
 namespace llvm {
 namespace clang {
-  class ASTContext;
+  class Preprocessor;
   class Decl;
   
   /// ASTStreamerTy - This is an opaque type used to reference ASTStreamer
@@ -25,7 +25,7 @@
   
   /// ASTStreamer_Init - Create an ASTStreamer with the specified ASTContext
   /// and FileID.
-  ASTStreamerTy *ASTStreamer_Init(ASTContext &Context, unsigned MainFileID);
+  ASTStreamerTy *ASTStreamer_Init(Preprocessor &pp, unsigned MainFileID);
   
   /// ASTStreamer_ReadTopLevelDecl - Parse and return one top-level declaration.
   /// This returns null at end of file.

Modified: cfe/cfe/trunk/include/clang/Sema/ASTStreamer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Sema/ASTStreamer.h?rev=39345&r1=39344&r2=39345&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Sema/ASTStreamer.h (original)
+++ cfe/cfe/trunk/include/clang/Sema/ASTStreamer.h Wed Jul 11 11:43:24 2007
@@ -16,7 +16,7 @@
 
 namespace llvm {
 namespace clang {
-  class ASTContext;
+  class Preprocessor;
   class Decl;
   
   /// ASTStreamerTy - This is an opaque type used to reference ASTStreamer
@@ -25,7 +25,7 @@
   
   /// ASTStreamer_Init - Create an ASTStreamer with the specified ASTContext
   /// and FileID.
-  ASTStreamerTy *ASTStreamer_Init(ASTContext &Context, unsigned MainFileID);
+  ASTStreamerTy *ASTStreamer_Init(Preprocessor &pp, unsigned MainFileID);
   
   /// ASTStreamer_ReadTopLevelDecl - Parse and return one top-level declaration.
   /// This returns null at end of file.





More information about the cfe-commits mailing list