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

via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 4 05:49:38 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Yihe Li (Mick235711)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/118636.diff


2 Files Affected:

- (modified) clang/lib/AST/Expr.cpp (+3-2) 
- (modified) clang/test/SemaCXX/warn-unused-result.cpp (+9) 


``````````diff
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

``````````

</details>


https://github.com/llvm/llvm-project/pull/118636


More information about the cfe-commits mailing list