[clang] [NFC][Clang] Adopt simplified `getTrailingObjects` in Stmt (PR #143250)
Rahul Joshi via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 7 04:59:24 PDT 2025
https://github.com/jurahul created https://github.com/llvm/llvm-project/pull/143250
None
>From 2aa08553460c49ab6ac1ea572d637ce0c1286d44 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Sat, 7 Jun 2025 04:21:23 -0700
Subject: [PATCH] [NFC][Clang] Adopt simplified `getTrailingObjects` in Stmt
---
clang/include/clang/AST/Stmt.h | 111 +++++++++++++--------------------
clang/lib/AST/Stmt.cpp | 8 +--
2 files changed, 47 insertions(+), 72 deletions(-)
diff --git a/clang/include/clang/AST/Stmt.h b/clang/include/clang/AST/Stmt.h
index 48a6aea4f4b6a..6c4bd6f6946ba 100644
--- a/clang/include/clang/AST/Stmt.h
+++ b/clang/include/clang/AST/Stmt.h
@@ -1953,10 +1953,6 @@ class CaseStmt final
return NumMandatoryStmtPtr + caseStmtIsGNURange();
}
- unsigned numTrailingObjects(OverloadToken<SourceLocation>) const {
- return caseStmtIsGNURange();
- }
-
unsigned lhsOffset() const { return LhsOffset; }
unsigned rhsOffset() const { return LhsOffset + caseStmtIsGNURange(); }
unsigned subStmtOffset() const { return rhsOffset() + SubStmtOffsetFromRhs; }
@@ -2228,10 +2224,8 @@ class AttributedStmt final
std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr);
}
- const Attr *const *getAttrArrayPtr() const {
- return getTrailingObjects<const Attr *>();
- }
- const Attr **getAttrArrayPtr() { return getTrailingObjects<const Attr *>(); }
+ const Attr *const *getAttrArrayPtr() const { return getTrailingObjects(); }
+ const Attr **getAttrArrayPtr() { return getTrailingObjects(); }
public:
static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
@@ -2543,7 +2537,7 @@ class SwitchStmt final : public Stmt,
SourceLocation LParenLoc;
SourceLocation RParenLoc;
- unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
+ unsigned numTrailingStatements() const {
return NumMandatoryStmtPtr + hasInitStorage() + hasVarStorage();
}
@@ -2579,40 +2573,34 @@ class SwitchStmt final : public Stmt,
bool hasVarStorage() const { return SwitchStmtBits.HasVar; }
Expr *getCond() {
- return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
+ return reinterpret_cast<Expr *>(getTrailingObjects()[condOffset()]);
}
const Expr *getCond() const {
- return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
+ return reinterpret_cast<Expr *>(getTrailingObjects()[condOffset()]);
}
void setCond(Expr *Cond) {
- getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
+ getTrailingObjects()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
}
- Stmt *getBody() { return getTrailingObjects<Stmt *>()[bodyOffset()]; }
- const Stmt *getBody() const {
- return getTrailingObjects<Stmt *>()[bodyOffset()];
- }
+ Stmt *getBody() { return getTrailingObjects()[bodyOffset()]; }
+ const Stmt *getBody() const { return getTrailingObjects()[bodyOffset()]; }
- void setBody(Stmt *Body) {
- getTrailingObjects<Stmt *>()[bodyOffset()] = Body;
- }
+ void setBody(Stmt *Body) { getTrailingObjects()[bodyOffset()] = Body; }
Stmt *getInit() {
- return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
- : nullptr;
+ return hasInitStorage() ? getTrailingObjects()[initOffset()] : nullptr;
}
const Stmt *getInit() const {
- return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
- : nullptr;
+ return hasInitStorage() ? getTrailingObjects()[initOffset()] : nullptr;
}
void setInit(Stmt *Init) {
assert(hasInitStorage() &&
"This switch statement has no storage for an init statement!");
- getTrailingObjects<Stmt *>()[initOffset()] = Init;
+ getTrailingObjects()[initOffset()] = Init;
}
/// Retrieve the variable declared in this "switch" statement, if any.
@@ -2636,20 +2624,20 @@ class SwitchStmt final : public Stmt,
/// If this SwitchStmt has a condition variable, return the faux DeclStmt
/// associated with the creation of that condition variable.
DeclStmt *getConditionVariableDeclStmt() {
- return hasVarStorage() ? static_cast<DeclStmt *>(
- getTrailingObjects<Stmt *>()[varOffset()])
- : nullptr;
+ return hasVarStorage()
+ ? static_cast<DeclStmt *>(getTrailingObjects()[varOffset()])
+ : nullptr;
}
const DeclStmt *getConditionVariableDeclStmt() const {
- return hasVarStorage() ? static_cast<DeclStmt *>(
- getTrailingObjects<Stmt *>()[varOffset()])
- : nullptr;
+ return hasVarStorage()
+ ? static_cast<DeclStmt *>(getTrailingObjects()[varOffset()])
+ : nullptr;
}
void setConditionVariableDeclStmt(DeclStmt *CondVar) {
assert(hasVarStorage());
- getTrailingObjects<Stmt *>()[varOffset()] = CondVar;
+ getTrailingObjects()[varOffset()] = CondVar;
}
SwitchCase *getSwitchCaseList() { return FirstCase; }
@@ -2693,15 +2681,13 @@ class SwitchStmt final : public Stmt,
// Iterators
child_range children() {
- return child_range(getTrailingObjects<Stmt *>(),
- getTrailingObjects<Stmt *>() +
- numTrailingObjects(OverloadToken<Stmt *>()));
+ return child_range(getTrailingObjects(),
+ getTrailingObjects() + numTrailingStatements());
}
const_child_range children() const {
- return const_child_range(getTrailingObjects<Stmt *>(),
- getTrailingObjects<Stmt *>() +
- numTrailingObjects(OverloadToken<Stmt *>()));
+ return const_child_range(getTrailingObjects(),
+ getTrailingObjects() + numTrailingStatements());
}
static bool classof(const Stmt *T) {
@@ -2738,7 +2724,7 @@ class WhileStmt final : public Stmt,
unsigned condOffset() const { return VarOffset + hasVarStorage(); }
unsigned bodyOffset() const { return condOffset() + BodyOffsetFromCond; }
- unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
+ unsigned numTrailingStatements() const {
return NumMandatoryStmtPtr + hasVarStorage();
}
@@ -2764,25 +2750,21 @@ class WhileStmt final : public Stmt,
bool hasVarStorage() const { return WhileStmtBits.HasVar; }
Expr *getCond() {
- return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
+ return reinterpret_cast<Expr *>(getTrailingObjects()[condOffset()]);
}
const Expr *getCond() const {
- return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
+ return reinterpret_cast<Expr *>(getTrailingObjects()[condOffset()]);
}
void setCond(Expr *Cond) {
- getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
+ getTrailingObjects()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
}
- Stmt *getBody() { return getTrailingObjects<Stmt *>()[bodyOffset()]; }
- const Stmt *getBody() const {
- return getTrailingObjects<Stmt *>()[bodyOffset()];
- }
+ Stmt *getBody() { return getTrailingObjects()[bodyOffset()]; }
+ const Stmt *getBody() const { return getTrailingObjects()[bodyOffset()]; }
- void setBody(Stmt *Body) {
- getTrailingObjects<Stmt *>()[bodyOffset()] = Body;
- }
+ void setBody(Stmt *Body) { getTrailingObjects()[bodyOffset()] = Body; }
/// Retrieve the variable declared in this "while" statement, if any.
///
@@ -2804,20 +2786,20 @@ class WhileStmt final : public Stmt,
/// If this WhileStmt has a condition variable, return the faux DeclStmt
/// associated with the creation of that condition variable.
DeclStmt *getConditionVariableDeclStmt() {
- return hasVarStorage() ? static_cast<DeclStmt *>(
- getTrailingObjects<Stmt *>()[varOffset()])
- : nullptr;
+ return hasVarStorage()
+ ? static_cast<DeclStmt *>(getTrailingObjects()[varOffset()])
+ : nullptr;
}
const DeclStmt *getConditionVariableDeclStmt() const {
- return hasVarStorage() ? static_cast<DeclStmt *>(
- getTrailingObjects<Stmt *>()[varOffset()])
- : nullptr;
+ return hasVarStorage()
+ ? static_cast<DeclStmt *>(getTrailingObjects()[varOffset()])
+ : nullptr;
}
void setConditionVariableDeclStmt(DeclStmt *CondVar) {
assert(hasVarStorage());
- getTrailingObjects<Stmt *>()[varOffset()] = CondVar;
+ getTrailingObjects()[varOffset()] = CondVar;
}
SourceLocation getWhileLoc() const { return WhileStmtBits.WhileLoc; }
@@ -2839,15 +2821,13 @@ class WhileStmt final : public Stmt,
// Iterators
child_range children() {
- return child_range(getTrailingObjects<Stmt *>(),
- getTrailingObjects<Stmt *>() +
- numTrailingObjects(OverloadToken<Stmt *>()));
+ return child_range(getTrailingObjects(),
+ getTrailingObjects() + numTrailingStatements());
}
const_child_range children() const {
- return const_child_range(getTrailingObjects<Stmt *>(),
- getTrailingObjects<Stmt *>() +
- numTrailingObjects(OverloadToken<Stmt *>()));
+ return const_child_range(getTrailingObjects(),
+ getTrailingObjects() + numTrailingStatements());
}
};
@@ -3158,10 +3138,6 @@ class ReturnStmt final
/// True if this ReturnStmt has storage for an NRVO candidate.
bool hasNRVOCandidate() const { return ReturnStmtBits.HasNRVOCandidate; }
- unsigned numTrailingObjects(OverloadToken<const VarDecl *>) const {
- return hasNRVOCandidate();
- }
-
/// Build a return statement.
ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate);
@@ -3187,8 +3163,7 @@ class ReturnStmt final
/// The optimization itself can only be performed if the variable is
/// also marked as an NRVO object.
const VarDecl *getNRVOCandidate() const {
- return hasNRVOCandidate() ? *getTrailingObjects<const VarDecl *>()
- : nullptr;
+ return hasNRVOCandidate() ? *getTrailingObjects() : nullptr;
}
/// Set the variable that might be used for the named return value
@@ -3197,7 +3172,7 @@ class ReturnStmt final
void setNRVOCandidate(const VarDecl *Var) {
assert(hasNRVOCandidate() &&
"This return statement has no storage for an NRVO candidate!");
- *getTrailingObjects<const VarDecl *>() = Var;
+ *getTrailingObjects() = Var;
}
SourceLocation getReturnLoc() const { return ReturnStmtBits.RetLoc; }
diff --git a/clang/lib/AST/Stmt.cpp b/clang/lib/AST/Stmt.cpp
index be4ba6878bf0c..0b0289cd5ec04 100644
--- a/clang/lib/AST/Stmt.cpp
+++ b/clang/lib/AST/Stmt.cpp
@@ -1154,12 +1154,12 @@ void SwitchStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
"This switch statement has no storage for a condition variable!");
if (!V) {
- getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
+ getTrailingObjects()[varOffset()] = nullptr;
return;
}
SourceRange VarRange = V->getSourceRange();
- getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
+ getTrailingObjects()[varOffset()] = new (Ctx)
DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
}
@@ -1215,12 +1215,12 @@ void WhileStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
"This while statement has no storage for a condition variable!");
if (!V) {
- getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
+ getTrailingObjects()[varOffset()] = nullptr;
return;
}
SourceRange VarRange = V->getSourceRange();
- getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
+ getTrailingObjects()[varOffset()] = new (Ctx)
DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
}
More information about the cfe-commits
mailing list