This is an archive of the discontinued LLVM Phabricator instance.

[analyzer] Handling non-POD multidimensional arrays in ArrayInitLoopExpr
ClosedPublic

Authored by isuckatcs on Aug 13 2022, 8:43 AM.

Details

Summary

This patch makes it possible for lambdas, implicit copy/move ctors and structured bindings to
handle non-POD multidimensional arrays.

Diff Detail

Event Timeline

isuckatcs created this revision.Aug 13 2022, 8:43 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 13 2022, 8:43 AM
NoQ accepted this revision.Aug 14 2022, 12:06 AM

Lovely, thanks!

This revision is now accepted and ready to land.Aug 14 2022, 12:06 AM
xazax.hun added inline comments.Aug 14 2022, 9:35 AM
clang/lib/Analysis/CFG.cpp
1724

Would a regular dyn_cast work? I would not expect getSubExpr to return nullptr. This applies to some other places as well.

3432–3436

Instead of duplicating both the code and adding this comment, I wonder if a well named small utility function can make this code equally clear and reduce duplication.

clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
569

Do you need a dyn_cast here? ``AILE alreadt has the right type.

isuckatcs added inline comments.Aug 14 2022, 1:39 PM
clang/lib/Analysis/CFG.cpp
3432–3436

I've been thinking about the same thing. I would also place the getArrayInitLoopExprSize() helper function into ASTContext as it also has a similar helper called getConstantArrayElementCount().

clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
569

It handles if the user passes a nullptr to the function. Also because AILE is an ArrayInitLoopExpr *, in the loop I also have
AILE = dyn_cast<ArrayInitLoopExpr>(E->getSubExpr()) which can still make a nullptr from AILE.

This might not be the best solution but this helper will be reworked anyway due to a mistake I've made below.

575
xazax.hun added inline comments.Aug 14 2022, 1:45 PM
clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
569

I think if you just declare E to be const ArrayInitLoopExpr*, you might not need the cast at all. while should already guard against nullptr from the user.

isuckatcs updated this revision to Diff 452622.Aug 15 2022, 3:42 AM
isuckatcs marked 4 inline comments as done.
  • Moved getArrayInitLoopExprSize() to ASTContext.
  • Addressed comments.
xazax.hun accepted this revision.Aug 15 2022, 8:32 AM

Some nits inline but overall looks good to me.

clang/lib/AST/ASTContext.cpp
6917

Did you see examples where the SubExpr of an ArrayInitLoopExpr returned a nullptr? If not, I'd go with dyn_cast for now.

clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
544–545

Shouldn't this also use extractElementInitializerFromNestedAILE?

clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
544–545

extractElementInitializerFromNestedAILE?

1169–1170

extractElementInitializerFromNestedAILE?

isuckatcs updated this revision to Diff 452932.Aug 16 2022, 2:42 AM
isuckatcs marked 4 inline comments as done.
  • Rebased
  • Moved extractElementInitializerFromNestedAILE() to ExprEngine
  • Addressed comments
isuckatcs added inline comments.
clang/lib/AST/ASTContext.cpp
6917

getSubExpr() uses a regular cast, so it cannot return nullptr.

isuckatcs added inline comments.Aug 16 2022, 2:47 AM
clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h
983 ↗(On Diff #452932)

The definition is in the header, because this method is used in CFG.cpp, but that source file is not linked against any of the ExprEngine source files and I don't want to change the linking of libraries.

@xazax.hun do you have any suggestions, where to put a function that is referenced in both the Analysis and the Static Analyzer library? I want to avoid creating the same helper twice.

xazax.hun added inline comments.Aug 16 2022, 8:48 AM
clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h
983 ↗(On Diff #452932)

Are you sure? CFG.cpp is linked into clangAnalysis and clangAnalysis is linked a dependency of the static analyzer core. I'd expect putting this in CFG.cpp should work perfectly well. Just be sure to not put the implementation in an anonymous namespace.

xazax.hun accepted this revision.Aug 16 2022, 8:51 AM

Overall looks good to me. I think extractElementInitializerFromNestedAILE should live somewhere in the clangAnalysis component due to how layering is done, but otherwise it looks wonderful, thanks!

isuckatcs updated this revision to Diff 453830.EditedAug 18 2022, 4:25 PM

Moved extractElementInitializerFromNestedAILE to CFG.cpp.

@xazax.hun is this how you meant it?

xazax.hun accepted this revision.Aug 19 2022, 2:16 AM

Moved extractElementInitializerFromNestedAILE to CFG.cpp.

@xazax.hun is this how you meant it?

Yep, looks great, thanks!

xazax.hun added inline comments.Aug 19 2022, 2:18 AM
clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
236–237

Would moving this declaration to CFG.h work?

isuckatcs updated this revision to Diff 454007.EditedAug 19 2022, 8:10 AM

Added a testcase for the crashing snippet. (Wrong revision)

isuckatcs updated this revision to Diff 454014.Aug 19 2022, 8:27 AM

Moved the declaration of extractElementInitializerFromNestedAILE to CFG.h.

isuckatcs marked an inline comment as done.Aug 19 2022, 8:28 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 22 2022, 4:54 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript