[clang] [analyzer] Fix uninitialized base class with initializer list when ctor is not declared in the base class (#70464) (PR #70792)

Ella Ma via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 31 09:28:30 PDT 2023


================
@@ -1222,6 +1222,15 @@ void ExprEngine::ProcessInitializer(const CFGInitializer CFGInit,
       PostInitializer PP(BMI, FieldLoc.getAsRegion(), stackFrame);
       evalBind(Tmp, Init, Pred, FieldLoc, InitVal, /*isInit=*/true, &PP);
     }
+  } else if (BMI->isBaseInitializer() && isa<InitListExpr>(Init)) {
+    // When the base class is initialized with an initialization list, there
+    // will not be a CXXConstructExpr to initialize the base region. Hence, we
+    // need to make the bind for it.
+    StoreManager &StoreMgr = State->getStateManager().getStoreManager();
+    SVal BaseLoc = StoreMgr.evalDerivedToBase(
+        thisVal, QualType(BMI->getBaseClass(), 0), BMI->isBaseVirtual());
+    SVal InitVal = State->getSVal(Init, stackFrame);
+    evalBind(Tmp, Init, Pred, BaseLoc, InitVal, true);
----------------
Snape3058 wrote:

`evalBind` will add a `PostStore` point if not given.
IMO, the initializer list here is just to make the assignment, just like `T test = {1};`, which has an AST structure of
```
DeclStmt 0x55977c889f98 <col:14, col:20>
 `-VarDecl 0x55977c889e80 <col:14, col:19> col:16 t 'T':'T' listinit
    `-InitListExpr 0x55977c889f50 <col:17, col:19> 'T':'T'
       `-IntegerLiteral 0x55977c889ee8 <col:18> 'int' 1
```
The `DeclStmt` here will use the `PostStore` point and wrap it with a `PostStmt` of `DeclStmt`.
So, a `PostStore` point here makes sense to me.

Besides, in the original `else` branch below, node `Pred` has a PostStmt point of CXXConstructExpr, which is also different from the `PostInitializer`.
And the `PostInitializer` point is used to notice the diagnostic system that a field assignment is here.
However, this can be done with the `PostStore` assigned with `evalBind`.

So, I think adding a `PostInitializer` here for the `evalBind` may not be necessary.

I am not very familiar with program points. If I get it incorrectly, I will correct this in the next submission.

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


More information about the cfe-commits mailing list