[clang] 767ab81 - [NFC] [Clang] [Sema] Implement CXXConstructExpr::getUnusedResultAttr (#152950)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 11 02:03:04 PDT 2025
Author: halbi2
Date: 2025-08-11T11:03:01+02:00
New Revision: 767ab815f106d856c1cf8ad388a4b463224b5b4b
URL: https://github.com/llvm/llvm-project/commit/767ab815f106d856c1cf8ad388a4b463224b5b4b
DIFF: https://github.com/llvm/llvm-project/commit/767ab815f106d856c1cf8ad388a4b463224b5b4b.diff
LOG: [NFC] [Clang] [Sema] Implement CXXConstructExpr::getUnusedResultAttr (#152950)
This continues my patch series started as #142541 where multiple kinds
of Expr all use the same getUnusedResultAttrImpl.
The test suite indicates there is no change in behavior happening here.
Added:
Modified:
clang/include/clang/AST/ExprCXX.h
clang/lib/Sema/SemaStmt.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h
index 5508890f12b58..9fedb230ce397 100644
--- a/clang/include/clang/AST/ExprCXX.h
+++ b/clang/include/clang/AST/ExprCXX.h
@@ -1712,6 +1712,19 @@ class CXXConstructExpr : public Expr {
CXXConstructExprBits.IsImmediateEscalating = Set;
}
+ /// Returns the WarnUnusedResultAttr that is declared on the callee
+ /// or its return type declaration, together with a NamedDecl that
+ /// refers to the declaration the attribute is attached to.
+ std::pair<const NamedDecl *, const WarnUnusedResultAttr *>
+ getUnusedResultAttr(const ASTContext &Ctx) const {
+ return getUnusedResultAttrImpl(getConstructor(), getType());
+ }
+
+ /// Returns true if this call expression should warn on unused results.
+ bool hasUnusedResultAttr(const ASTContext &Ctx) const {
+ return getUnusedResultAttr(Ctx).second != nullptr;
+ }
+
SourceLocation getBeginLoc() const LLVM_READONLY;
SourceLocation getEndLoc() const LLVM_READONLY;
SourceRange getParenOrBraceRange() const { return ParenOrBraceRange; }
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index efc0b35792613..bc1ddb80961a2 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -316,17 +316,10 @@ void DiagnoseUnused(Sema &S, const Expr *E, std::optional<unsigned> DiagID) {
}
}
} else if (const auto *CE = dyn_cast<CXXConstructExpr>(E)) {
- if (const CXXConstructorDecl *Ctor = CE->getConstructor()) {
- const NamedDecl *OffendingDecl = nullptr;
- const auto *A = Ctor->getAttr<WarnUnusedResultAttr>();
- if (!A) {
- OffendingDecl = Ctor->getParent();
- A = OffendingDecl->getAttr<WarnUnusedResultAttr>();
- }
- if (DiagnoseNoDiscard(S, OffendingDecl, A, Loc, R1, R2,
- /*isCtor=*/true))
- return;
- }
+ auto [OffendingDecl, A] = CE->getUnusedResultAttr(S.Context);
+ if (DiagnoseNoDiscard(S, OffendingDecl, A, Loc, R1, R2,
+ /*isCtor=*/true))
+ return;
} else if (const auto *ILE = dyn_cast<InitListExpr>(E)) {
if (const TagDecl *TD = ILE->getType()->getAsTagDecl()) {
More information about the cfe-commits
mailing list