[cfe-commits] r95199 - in /cfe/trunk: include/clang/AST/StmtCXX.h lib/AST/Stmt.cpp lib/Sema/SemaStmt.cpp

Sam Weinig sam.weinig at gmail.com
Tue Feb 2 19:56:39 PST 2010


Author: weinig
Date: Tue Feb  2 21:56:39 2010
New Revision: 95199

URL: http://llvm.org/viewvc/llvm-project?rev=95199&view=rev
Log:
Implement Doug's suggestion. Eliminate the Stmts pointer from CXXTryStmt and instead allocate the statements after the object.

Modified:
    cfe/trunk/include/clang/AST/StmtCXX.h
    cfe/trunk/lib/AST/Stmt.cpp
    cfe/trunk/lib/Sema/SemaStmt.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/StmtCXX.h (original)
+++ cfe/trunk/include/clang/AST/StmtCXX.h Tue Feb  2 21:56:39 2010
@@ -59,35 +59,42 @@
 ///
 class CXXTryStmt : public Stmt {
   SourceLocation TryLoc;
-
-  // First place is the guarded CompoundStatement. Subsequent are the handlers.
-  Stmt **Stmts;
   unsigned NumHandlers;
 
-protected:
-  virtual void DoDestroy(ASTContext &Ctx);
+  CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, Stmt **handlers,
+             unsigned numHandlers);
 
 public:
-  CXXTryStmt(ASTContext &C, SourceLocation tryLoc, Stmt *tryBlock,
-             Stmt **handlers, unsigned numHandlers);
+  static CXXTryStmt *Create(ASTContext &C, SourceLocation tryLoc,
+                            Stmt *tryBlock, Stmt **handlers,
+                            unsigned numHandlers);
 
   virtual SourceRange getSourceRange() const {
     return SourceRange(getTryLoc(), getEndLoc());
   }
 
   SourceLocation getTryLoc() const { return TryLoc; }
-  SourceLocation getEndLoc() const { return Stmts[NumHandlers]->getLocEnd(); }
+  SourceLocation getEndLoc() const {
+    Stmt const * const*Stmts = reinterpret_cast<Stmt const * const*>(this + 1);
+    return Stmts[NumHandlers]->getLocEnd();
+  }
 
-  CompoundStmt *getTryBlock() { return llvm::cast<CompoundStmt>(Stmts[0]); }
+  CompoundStmt *getTryBlock() {
+    Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
+    return llvm::cast<CompoundStmt>(Stmts[0]);
+  }
   const CompoundStmt *getTryBlock() const {
+    Stmt const * const*Stmts = reinterpret_cast<Stmt const * const*>(this + 1);
     return llvm::cast<CompoundStmt>(Stmts[0]);
   }
 
   unsigned getNumHandlers() const { return NumHandlers; }
   CXXCatchStmt *getHandler(unsigned i) {
+    Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
     return llvm::cast<CXXCatchStmt>(Stmts[i + 1]);
   }
   const CXXCatchStmt *getHandler(unsigned i) const {
+    Stmt const * const*Stmts = reinterpret_cast<Stmt const * const*>(this + 1);
     return llvm::cast<CXXCatchStmt>(Stmts[i + 1]);
   }
 

Modified: cfe/trunk/lib/AST/Stmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Stmt.cpp?rev=95199&r1=95198&r2=95199&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Stmt.cpp (original)
+++ cfe/trunk/lib/AST/Stmt.cpp Tue Feb  2 21:56:39 2010
@@ -407,10 +407,20 @@
   RParenLoc = rparenloc;
 }
 
-CXXTryStmt::CXXTryStmt(ASTContext &C, SourceLocation tryLoc, Stmt *tryBlock,
+CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
+                               Stmt *tryBlock, Stmt **handlers, 
+                               unsigned numHandlers) {
+  std::size_t Size = sizeof(CXXTryStmt);
+  Size += ((numHandlers + 1) * sizeof(Stmt));
+
+  void *Mem = C.Allocate(Size, llvm::alignof<CXXTryStmt>());
+  return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
+}
+
+CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
                        Stmt **handlers, unsigned numHandlers)
   : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
-  Stmts = new (C) Stmt*[NumHandlers + 1];
+  Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
   Stmts[0] = tryBlock;
   std::copy(handlers, handlers + NumHandlers, Stmts + 1);
 }
@@ -493,14 +503,6 @@
   C.Deallocate((void *)this);
 }
 
-void CXXTryStmt::DoDestroy(ASTContext& C) {
-  DestroyChildren(C);
-  C.Deallocate(Stmts);
-
-  this->~CXXTryStmt();
-  C.Deallocate((void *)this);
-}
-
 //===----------------------------------------------------------------------===//
 //  Child Iterators for iterating over subexpressions/substatements
 //===----------------------------------------------------------------------===//
@@ -664,5 +666,10 @@
 }
 
 // CXXTryStmt
-Stmt::child_iterator CXXTryStmt::child_begin() { return &Stmts[0]; }
-Stmt::child_iterator CXXTryStmt::child_end() { return &Stmts[0]+NumHandlers+1; }
+Stmt::child_iterator CXXTryStmt::child_begin() {
+  return reinterpret_cast<Stmt **>(this + 1);
+}
+
+Stmt::child_iterator CXXTryStmt::child_end() {
+  return reinterpret_cast<Stmt **>(this + 1) + NumHandlers + 1;
+}

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=95199&r1=95198&r2=95199&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Tue Feb  2 21:56:39 2010
@@ -1551,7 +1551,7 @@
 
   CurFunctionNeedsScopeChecking = true;
   RawHandlers.release();
-  return Owned(new (Context) CXXTryStmt(Context, TryLoc,
-                                        static_cast<Stmt*>(TryBlock.release()),
-                                        Handlers, NumHandlers));
+  return Owned(CXXTryStmt::Create(Context, TryLoc,
+                                  static_cast<Stmt*>(TryBlock.release()),
+                                  Handlers, NumHandlers));
 }





More information about the cfe-commits mailing list