-
Notifications
You must be signed in to change notification settings - Fork 15.6k
[analyzer] Support pack indexing expressions #173186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1740,7 +1740,6 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred, | |||||||||
| case Stmt::RecoveryExprClass: | ||||||||||
| case Stmt::CXXNoexceptExprClass: | ||||||||||
| case Stmt::PackExpansionExprClass: | ||||||||||
| case Stmt::PackIndexingExprClass: | ||||||||||
| case Stmt::SubstNonTypeTemplateParmPackExprClass: | ||||||||||
| case Stmt::FunctionParmPackExprClass: | ||||||||||
| case Stmt::CoroutineBodyStmtClass: | ||||||||||
|
|
@@ -2292,6 +2291,13 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred, | |||||||||
| Bldr.addNodes(Dst); | ||||||||||
| break; | ||||||||||
|
|
||||||||||
| case Stmt::PackIndexingExprClass: { | ||||||||||
| Bldr.takeNodes(Pred); | ||||||||||
| VisitPackIndexingExpr(cast<PackIndexingExpr>(S), Pred, Dst); | ||||||||||
| Bldr.addNodes(Dst); | ||||||||||
| break; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| case Stmt::ImplicitCastExprClass: | ||||||||||
| case Stmt::CStyleCastExprClass: | ||||||||||
| case Stmt::CXXStaticCastExprClass: | ||||||||||
|
|
@@ -3296,6 +3302,14 @@ void ExprEngine::VisitCommonDeclRefExpr(const Expr *Ex, const NamedDecl *D, | |||||||||
|
|
||||||||||
| SVal V = UnknownVal(); | ||||||||||
|
|
||||||||||
| // For pack indexing expressions. Binding is not available here but through | ||||||||||
| // the expanded expressions in the PackIndexingExpr node. | ||||||||||
| if (BD->isParameterPack()) { | ||||||||||
| Bldr.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, V), nullptr, | ||||||||||
| ProgramPoint::PostLValueKind); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Handle binding to data members | ||||||||||
| if (const auto *ME = dyn_cast<MemberExpr>(BD->getBinding())) { | ||||||||||
| const auto *Field = cast<FieldDecl>(ME->getMemberDecl()); | ||||||||||
|
|
@@ -3447,6 +3461,22 @@ void ExprEngine::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *Ex, | |||||||||
| getCheckerManager().runCheckersForPostStmt(Dst, EvalSet, Ex, *this); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| void ExprEngine::VisitPackIndexingExpr(const PackIndexingExpr *E, | ||||||||||
| ExplodedNode *Pred, | ||||||||||
| ExplodedNodeSet &Dst) { | ||||||||||
| if (!E->isFullySubstituted()) { | ||||||||||
| llvm_unreachable("Unsubstituted pack indexing expression"); | ||||||||||
| } | ||||||||||
|
Comment on lines
+3467
to
+3469
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
WDYT? |
||||||||||
|
|
||||||||||
| if (const auto *DRE = dyn_cast<DeclRefExpr>(E->getSelectedExpr())) { | ||||||||||
| VisitCommonDeclRefExpr(E, DRE->getDecl(), Pred, Dst); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); | ||||||||||
| Bldr.generateSink(E, Pred, Pred->getState()); | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A sink node usually indicates an error. How about we add a Also is it guaranteed that the selected expression is a |
||||||||||
| } | ||||||||||
|
|
||||||||||
| /// VisitArraySubscriptExpr - Transfer function for array accesses | ||||||||||
| void ExprEngine::VisitArraySubscriptExpr(const ArraySubscriptExpr *A, | ||||||||||
| ExplodedNode *Pred, | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // RUN: %clang_analyze_cc1 -analyzer-checker=core -std=c++26 -verify %s | ||
|
|
||
| void clang_analyzer_eval(bool); | ||
|
|
||
| template <class T> | ||
| constexpr decltype(auto) get0(const T& val) noexcept { | ||
| auto& [...members] = val; | ||
| auto&& r = members...[0]; // no-crash | ||
| return r; | ||
| } | ||
|
|
||
| struct A { | ||
| int a; | ||
| }; | ||
|
|
||
| void no_crash_negative() { | ||
| const int& x = get0(A{1}); | ||
| clang_analyzer_eval(x == 1); | ||
| } | ||
|
|
||
| void uninitialized() { | ||
| A a; | ||
| const int& x = get0(a); | ||
| clang_analyzer_eval(x == 0); // expected-warning{{The left operand of '==' is a garbage value}} | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably add a FIXME here instead of just binding
UnknownValto the bind expression. This should eventually be handled.In this case the binding is a sub-region of the pack (e.g.: only the first/last N elements), right? Can't we model this somehow?