[clang] [clang] Fix missing check for nullptr in CallExpr::getUnusedResultAttr (PR #118636)
Yihe Li via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 4 05:48:59 PST 2024
https://github.com/Mick235711 created https://github.com/llvm/llvm-project/pull/118636
Fixes #117975, a regression introduced by #112521 due to me forgetting to check for `nullptr` before dereferencing in `CallExpr::getUnusedResultAttr`.
A regression test has been added as per the comments on the fixed issue.
>From e81792ade5cda7afbba6ba161a3a9b9184065d82 Mon Sep 17 00:00:00 2001
From: Yihe Li <winmikedows at hotmail.com>
Date: Wed, 4 Dec 2024 21:45:19 +0800
Subject: [PATCH] [clang] Fix missing check for nullptr in
CallExpr::getUnusedResultAttr
---
clang/lib/AST/Expr.cpp | 5 +++--
clang/test/SemaCXX/warn-unused-result.cpp | 9 +++++++++
2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index a4fb4d5a1f2ec4..286f02ded27196 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -1619,8 +1619,9 @@ std::pair<const NamedDecl *, const Attr *>
CallExpr::getUnusedResultAttr(const ASTContext &Ctx) const {
// If the callee is marked nodiscard, return that attribute
const Decl *D = getCalleeDecl();
- if (const auto *A = D->getAttr<WarnUnusedResultAttr>())
- return {nullptr, A};
+ if (D != nullptr)
+ if (const auto *A = D->getAttr<WarnUnusedResultAttr>())
+ return {nullptr, A};
// If the return type is a struct, union, or enum that is marked nodiscard,
// then return the return type attribute.
diff --git a/clang/test/SemaCXX/warn-unused-result.cpp b/clang/test/SemaCXX/warn-unused-result.cpp
index 682c500dc1d96d..5105f347db8b53 100644
--- a/clang/test/SemaCXX/warn-unused-result.cpp
+++ b/clang/test/SemaCXX/warn-unused-result.cpp
@@ -355,3 +355,12 @@ void use2() {
(void)G{"Hello"};
}
} // namespace nodiscard_specialization
+
+namespace GH117975 {
+// Test for a regression for ICE in CallExpr::getUnusedResultAttr
+int f() { return 0; }
+void id_print_name() {
+ (int) // expected-warning {{expression result unused}}
+ ((int(*)())f)();
+}
+} // namespace GH117975
More information about the cfe-commits
mailing list