[clang] 3edbe36 - [clang] Fix missing check for nullptr in CallExpr::getUnusedResultAttr (#118636)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 6 04:25:43 PST 2025


Author: Yihe Li
Date: 2025-01-06T13:25:40+01:00
New Revision: 3edbe36c3eb01d1c35ac1761da108e3a493258ee

URL: https://github.com/llvm/llvm-project/commit/3edbe36c3eb01d1c35ac1761da108e3a493258ee
DIFF: https://github.com/llvm/llvm-project/commit/3edbe36c3eb01d1c35ac1761da108e3a493258ee.diff

LOG: [clang] Fix missing check for nullptr in CallExpr::getUnusedResultAttr (#118636)

Fixes #117975, a regression introduced by #112521 due to forgetting
to check for `nullptr` before dereferencing in
`CallExpr::getUnusedResultAttr`.

Added: 
    

Modified: 
    clang/lib/AST/Expr.cpp
    clang/test/SemaCXX/warn-unused-result.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 8c8ccdb61dc01c..ba66d362785674 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -1618,9 +1618,9 @@ QualType CallExpr::getCallReturnType(const ASTContext &Ctx) const {
 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 (const Decl *D = getCalleeDecl())
+    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