================
@@ -2113,37 +2115,96 @@ SVal 
RegionStoreManager::getBindingForElement(RegionBindingsConstRef B,
   return getBindingForFieldOrElementCommon(B, R, R->getElementType());
 }
 
+std::optional<SVal>
+RegionStoreManager::getConstantValFromInitializer(const FieldRegion *R,
+                                                  bool IsMainAnalysis) {
+  SmallVector<const SubRegion *, 4> Path;
+  const MemRegion *Cur = R;
+  while (Cur && !isa<VarRegion>(Cur)) {
+    if (isa<FieldRegion, ElementRegion>(Cur)) {
+      Path.push_back(cast<SubRegion>(Cur));
+      Cur = cast<SubRegion>(Cur)->getSuperRegion();
+    } else {
+      return std::nullopt;
+    }
+  }
+
+  const auto *VR = dyn_cast_or_null<VarRegion>(Cur);
+  if (!VR)
+    return std::nullopt;
+
+  const VarDecl *VD = VR->getDecl();
+  QualType LeafTy = R->getDecl()->getType();
+
+  // We trust initializers of constants and globals when analyzing main().
+  if (!VD->getType().isConstQualified() && !LeafTy.isConstQualified() &&
+      (!IsMainAnalysis || !VD->hasGlobalStorage()))
+    return std::nullopt;
----------------
NagyDonat wrote:

```suggestion
  bool TrustInit = (VD->getType().isConstQualified() ||
                    LeafTy.isConstQualified() ||
                    (IsMainAnalysis && VD->hasGlobalStorage()));
  if (!TrustInit)
    return std::nullopt;
```
The comment was ambiguous -- it was unclear whether "when analyzing main()" 
referred to "constants and globals" or just "globals": Instead of rephrasing 
the comment, I would suggest writing the actual code in a more readable way. 

https://github.com/llvm/llvm-project/pull/189361
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to