[clang] 7c10c9d - [clang] Convert ObjCAtTryStmt to llvm::TrailingObjects

Nico Weber via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 27 05:57:13 PDT 2021


Author: Nico Weber
Date: 2021-10-27T08:57:05-04:00
New Revision: 7c10c9d8e8004a09b7477806eef53cbebee82333

URL: https://github.com/llvm/llvm-project/commit/7c10c9d8e8004a09b7477806eef53cbebee82333
DIFF: https://github.com/llvm/llvm-project/commit/7c10c9d8e8004a09b7477806eef53cbebee82333.diff

LOG: [clang] Convert ObjCAtTryStmt to llvm::TrailingObjects

And make it final while here.

No behavior change.

Differential Revision: https://reviews.llvm.org/D112542

Added: 
    

Modified: 
    clang/include/clang/AST/StmtObjC.h
    clang/lib/AST/StmtObjC.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/StmtObjC.h b/clang/include/clang/AST/StmtObjC.h
index 948ef2421cb97..5f56f49358f93 100644
--- a/clang/include/clang/AST/StmtObjC.h
+++ b/clang/include/clang/AST/StmtObjC.h
@@ -162,8 +162,14 @@ class ObjCAtFinallyStmt : public Stmt {
 };
 
 /// Represents Objective-C's \@try ... \@catch ... \@finally statement.
-class ObjCAtTryStmt : public Stmt {
-private:
+class ObjCAtTryStmt final
+    : public Stmt,
+      private llvm::TrailingObjects<ObjCAtTryStmt, Stmt *> {
+  friend TrailingObjects;
+  size_t numTrailingObjects(OverloadToken<Stmt *>) const {
+    return 1 + NumCatchStmts + HasFinally;
+  }
+
   // The location of the @ in the \@try.
   SourceLocation AtTryLoc;
 
@@ -178,10 +184,8 @@ class ObjCAtTryStmt : public Stmt {
   /// The order of the statements in memory follows the order in the source,
   /// with the \@try body first, followed by the \@catch statements (if any)
   /// and, finally, the \@finally (if it exists).
-  Stmt **getStmts() { return reinterpret_cast<Stmt **> (this + 1); }
-  const Stmt* const *getStmts() const {
-    return reinterpret_cast<const Stmt * const*> (this + 1);
-  }
+  Stmt **getStmts() { return getTrailingObjects<Stmt *>(); }
+  Stmt *const *getStmts() const { return getTrailingObjects<Stmt *>(); }
 
   ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
                 Stmt **CatchStmts, unsigned NumCatchStmts,
@@ -257,8 +261,8 @@ class ObjCAtTryStmt : public Stmt {
   }
 
   child_range children() {
-    return child_range(getStmts(),
-                       getStmts() + 1 + NumCatchStmts + HasFinally);
+    return child_range(
+        getStmts(), getStmts() + numTrailingObjects(OverloadToken<Stmt *>()));
   }
 
   const_child_range children() const {

diff  --git a/clang/lib/AST/StmtObjC.cpp b/clang/lib/AST/StmtObjC.cpp
index 3d586795517c4..12d8a9e7dac8e 100644
--- a/clang/lib/AST/StmtObjC.cpp
+++ b/clang/lib/AST/StmtObjC.cpp
@@ -46,9 +46,8 @@ ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
                                      SourceLocation atTryLoc, Stmt *atTryStmt,
                                      Stmt **CatchStmts, unsigned NumCatchStmts,
                                      Stmt *atFinallyStmt) {
-  unsigned Size =
-      sizeof(ObjCAtTryStmt) +
-      (1 + NumCatchStmts + (atFinallyStmt != nullptr)) * sizeof(Stmt *);
+  size_t Size =
+      totalSizeToAlloc<Stmt *>(1 + NumCatchStmts + (atFinallyStmt != nullptr));
   void *Mem = Context.Allocate(Size, alignof(ObjCAtTryStmt));
   return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
                                  atFinallyStmt);
@@ -57,8 +56,7 @@ ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
 ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(const ASTContext &Context,
                                           unsigned NumCatchStmts,
                                           bool HasFinally) {
-  unsigned Size =
-      sizeof(ObjCAtTryStmt) + (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
+  size_t Size = totalSizeToAlloc<Stmt *>(1 + NumCatchStmts + HasFinally);
   void *Mem = Context.Allocate(Size, alignof(ObjCAtTryStmt));
   return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
 }


        


More information about the cfe-commits mailing list