bricci created this revision. bricci added a reviewer: rsmith. bricci added a project: clang. Herald added a subscriber: cfe-commits.
Use TrailingObjects for CXXTryStmt instead of hand-rolling it. This hides the reinterpret_casts and make it more consistent with the other classes. Repository: rC Clang https://reviews.llvm.org/D48873 Files: include/clang/AST/StmtCXX.h lib/AST/StmtCXX.cpp
Index: lib/AST/StmtCXX.cpp =================================================================== --- lib/AST/StmtCXX.cpp +++ lib/AST/StmtCXX.cpp @@ -25,26 +25,22 @@ CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc, Stmt *tryBlock, ArrayRef<Stmt *> handlers) { - std::size_t Size = sizeof(CXXTryStmt); - Size += ((handlers.size() + 1) * sizeof(Stmt *)); - + const size_t Size = totalSizeToAlloc<Stmt *>(handlers.size() + 1); void *Mem = C.Allocate(Size, alignof(CXXTryStmt)); return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers); } CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty, unsigned numHandlers) { - std::size_t Size = sizeof(CXXTryStmt); - Size += ((numHandlers + 1) * sizeof(Stmt *)); - + const size_t Size = totalSizeToAlloc<Stmt *>(numHandlers + 1); void *Mem = C.Allocate(Size, alignof(CXXTryStmt)); return new (Mem) CXXTryStmt(Empty, numHandlers); } CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, ArrayRef<Stmt *> handlers) : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) { - Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1); + Stmt **Stmts = getTrailingObjects<Stmt *>(); Stmts[0] = tryBlock; std::copy(handlers.begin(), handlers.end(), Stmts + 1); } Index: include/clang/AST/StmtCXX.h =================================================================== --- include/clang/AST/StmtCXX.h +++ include/clang/AST/StmtCXX.h @@ -62,21 +62,21 @@ /// CXXTryStmt - A C++ try block, including all handlers. /// -class CXXTryStmt : public Stmt { +class CXXTryStmt final : public Stmt, + private llvm::TrailingObjects<CXXTryStmt, Stmt *> { + + friend TrailingObjects; + SourceLocation TryLoc; unsigned NumHandlers; + size_t numTrailingObjects(OverloadToken<Stmt *>) const { return NumHandlers; } CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, ArrayRef<Stmt*> handlers); - CXXTryStmt(EmptyShell Empty, unsigned numHandlers) : Stmt(CXXTryStmtClass), NumHandlers(numHandlers) { } - Stmt const * const *getStmts() const { - return reinterpret_cast<Stmt const * const*>(this + 1); - } - Stmt **getStmts() { - return reinterpret_cast<Stmt **>(this + 1); - } + Stmt * const *getStmts() const { return getTrailingObjects<Stmt *>(); } + Stmt **getStmts() { return getTrailingObjects<Stmt *>(); } public: static CXXTryStmt *Create(const ASTContext &C, SourceLocation tryLoc, @@ -90,30 +90,31 @@ SourceLocation getTryLoc() const { return TryLoc; } SourceLocation getEndLoc() const { - return getStmts()[NumHandlers]->getLocEnd(); + return getTrailingObjects<Stmt *>()[NumHandlers]->getLocEnd(); } CompoundStmt *getTryBlock() { - return cast<CompoundStmt>(getStmts()[0]); + return cast<CompoundStmt>(getTrailingObjects<Stmt *>()[0]); } const CompoundStmt *getTryBlock() const { - return cast<CompoundStmt>(getStmts()[0]); + return cast<CompoundStmt>(getTrailingObjects<Stmt *>()[0]); } unsigned getNumHandlers() const { return NumHandlers; } CXXCatchStmt *getHandler(unsigned i) { - return cast<CXXCatchStmt>(getStmts()[i + 1]); + return cast<CXXCatchStmt>(getTrailingObjects<Stmt *>()[i + 1]); } const CXXCatchStmt *getHandler(unsigned i) const { - return cast<CXXCatchStmt>(getStmts()[i + 1]); + return cast<CXXCatchStmt>(getTrailingObjects<Stmt *>()[i + 1]); } static bool classof(const Stmt *T) { return T->getStmtClass() == CXXTryStmtClass; } child_range children() { - return child_range(getStmts(), getStmts() + getNumHandlers() + 1); + return child_range(getTrailingObjects<Stmt *>(), + getTrailingObjects<Stmt *>() + getNumHandlers() + 1); } friend class ASTStmtReader;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits