[clang] Check whether EvaluatedStmt::Value is valid in VarDecl::hasInit (PR #94515)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 5 11:30:43 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Akira Hatanaka (ahatanak)

<details>
<summary>Changes</summary>

VarDecl::isNull() doesn't tell whether the VarDecl has an initializer as methods like ensureEvaluatedStmt can create an EvaluatedStmt even when there isn't an initializer.

Revert e1c3e16d24b5cc097ff08e9283f53319acd3f245 as the change isn't needed anymore with this change.

See the discussion in https://github.com/llvm/llvm-project/pull/93749.

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


1 Files Affected:

- (modified) clang/lib/AST/Decl.cpp (+6-4) 


``````````diff
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 1f19dadafa44e..fc04f877b2268 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -2390,6 +2390,9 @@ bool VarDecl::hasInit() const {
     if (P->hasUnparsedDefaultArg() || P->hasUninstantiatedDefaultArg())
       return false;
 
+  if (auto *Eval = getEvaluatedStmt())
+    return Eval->Value.isValid();
+
   return !Init.isNull();
 }
 
@@ -2402,10 +2405,9 @@ Expr *VarDecl::getInit() {
 
   auto *Eval = getEvaluatedStmt();
 
-  return cast_if_present<Expr>(
-      Eval->Value.isOffset()
-          ? Eval->Value.get(getASTContext().getExternalSource())
-          : Eval->Value.get(nullptr));
+  return cast<Expr>(Eval->Value.isOffset()
+                        ? Eval->Value.get(getASTContext().getExternalSource())
+                        : Eval->Value.get(nullptr));
 }
 
 Stmt **VarDecl::getInitAddress() {

``````````

</details>


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


More information about the cfe-commits mailing list