Skip to content

[NFC][Clang] Adopt simplified getTrailingObjects in Stmt #143250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 43 additions & 68 deletions clang/include/clang/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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.
Expand All @@ -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; }
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}

Expand All @@ -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.
///
Expand All @@ -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; }
Expand All @@ -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());
}
};

Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand All @@ -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; }
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/Stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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());
}

Expand Down
Loading