[PATCH] D111542: [analyzer] Retrieve incomplete array extent from its redeclaration.

Gabor Marton via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 12 09:09:42 PDT 2021


martong added inline comments.


================
Comment at: clang/lib/StaticAnalyzer/Core/RegionStore.cpp:1663
     // We can trust a const value or a value of a global initializer in main().
-    const VarDecl *VD = VR->getDecl();
+    const VarDecl *VD = VR->getDecl()->getMostRecentDecl();
     if (VD->getType().isConstQualified() ||
----------------
ASDenysPetrov wrote:
> NoQ wrote:
> > ASDenysPetrov wrote:
> > > steakhal wrote:
> > > > I think you supposed to use the `getCanonicalDecl()` instead.
> > > Using `getCanonicalDecl` does not fix the issue. I've checked.
> > Aha ok, can you try iterating over `redecls()`?
> > 
> > Separately, I suspect that this should be performed before the `VarRegion` is constructed in the first place. Maybe in its constructor we should `assert(VD->isThisDeclarationADefinition())` or something like that.
> > Aha ok, can you try iterating over redecls()?
> Do you assume that in a list `{redecl1, redecl2, redecl3}` **redecl2** may be our guy but **1** and **3** may not?
> > Separately, I suspect that this should be performed before the VarRegion is constructed in the first place. 
> I'm not sure I got what you mean, but as I undestood that it is not be a part of this fix, right?
I think this should be part of this fix, but you don't have to do that iteration, there is a better thing for the job: `getAnyInitializer`.

What we need is to find that Decl that has the InitExpr attached. This may not be the canonical nor the most recent redecl.
Please see my code change suggestion below.


================
Comment at: clang/lib/StaticAnalyzer/Core/RegionStore.cpp:1661-1663
     // Check if the containing array has an initialized value that we can trust.
     // We can trust a const value or a value of a global initializer in main().
+    const VarDecl *VD = VR->getDecl()->getMostRecentDecl();
----------------



================
Comment at: clang/test/Analysis/initialization.c:101-102
+
+const int glob_arr3[];              // Incomplete array declaration
+const int glob_arr3[4] = {1, 2, 3}; // Incomplete Array redeclaration
+void foo() {
----------------
I'd like to see some more elaborate test cases. Notably
```
const int glob_arr3[];              // Incomplete array declaration
const int glob_arr3[4] = {1, 2, 3}; // Incomplete Array redeclaration
const int glob_arr3[];              // Incomplete array redeclaration
```
here neither the canonical nor the most recent decl have the initexpr.
And I think this is what @balazske tried to point out.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D111542/new/

https://reviews.llvm.org/D111542



More information about the cfe-commits mailing list