Skip to content

Commit 3736854

Browse files
committed
Cleanup
1 parent 02c009d commit 3736854

File tree

5 files changed

+55
-70
lines changed

5 files changed

+55
-70
lines changed

clang/include/clang/AST/AttrIterator.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "clang/Basic/LLVM.h"
1717
#include "llvm/ADT/ADL.h"
1818
#include "llvm/ADT/SmallVector.h"
19+
#include "llvm/ADT/iterator_range.h"
1920
#include "llvm/Support/Casting.h"
2021
#include <cassert>
2122
#include <cstddef>
@@ -124,6 +125,17 @@ inline auto *getSpecificAttr(const Container &container) {
124125
return It != specific_attr_end<IterTy>(container) ? *It : nullptr;
125126
}
126127

128+
template <typename SpecificAttr, typename Container>
129+
inline auto getSpecificAttrs(const Container &container) {
130+
using ValueTy = llvm::detail::ValueOfRange<Container>;
131+
using ValuePointeeTy = std::remove_pointer_t<ValueTy>;
132+
using IterTy = std::conditional_t<std::is_const_v<ValuePointeeTy>,
133+
const SpecificAttr, SpecificAttr>;
134+
auto Begin = specific_attr_begin<IterTy>(container);
135+
auto End = specific_attr_end<IterTy>(container);
136+
return llvm::make_range(Begin, End);
137+
}
138+
127139
} // namespace clang
128140

129141
#endif // LLVM_CLANG_AST_ATTRITERATOR_H

clang/lib/Analysis/CFG.cpp

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ class reverse_children {
433433
ArrayRef<Stmt *> children;
434434

435435
public:
436-
reverse_children(Stmt *S, ASTContext *astContext = nullptr);
436+
reverse_children(Stmt *S, ASTContext &Ctx);
437437

438438
using iterator = ArrayRef<Stmt *>::reverse_iterator;
439439

@@ -443,61 +443,47 @@ class reverse_children {
443443

444444
} // namespace
445445

446-
reverse_children::reverse_children(Stmt *S, ASTContext *AstC) {
447-
if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
448-
children = CE->getRawSubExprs();
446+
reverse_children::reverse_children(Stmt *S, ASTContext &Ctx) {
447+
switch (S->getStmtClass()) {
448+
case Stmt::CallExprClass: {
449+
children = cast<CallExpr>(S)->getRawSubExprs();
449450
return;
450451
}
451-
switch (S->getStmtClass()) {
452-
// Note: Fill in this switch with more cases we want to optimize.
453-
case Stmt::InitListExprClass: {
454-
InitListExpr *IE = cast<InitListExpr>(S);
455-
children = llvm::ArrayRef(reinterpret_cast<Stmt **>(IE->getInits()),
456-
IE->getNumInits());
457-
return;
458-
}
459-
case Stmt::AttributedStmtClass: {
460-
assert(S->getStmtClass() == Stmt::AttributedStmtClass);
461-
assert(AstC &&
462-
"Attributes need the ast context to determine side-effects");
463-
AttributedStmt *AS = cast<AttributedStmt>(S);
464-
assert(AS);
465-
466-
// for an attributed stmt, the "children()" returns only the NullStmt
467-
// (;) but semantically the "children" are supposed to be the
468-
// expressions _within_ i.e. the two square brackets i.e. [[ HERE ]]
469-
// so we add the subexpressions first, _then_ add the "children"
470-
for (const Attr *Attr : AS->getAttrs()) {
471-
// Only handles [[ assume(<assumption>) ]] right now
472-
CXXAssumeAttr const *AssumeAttr = llvm::dyn_cast<CXXAssumeAttr>(Attr);
473-
if (!AssumeAttr) {
474-
continue;
475-
}
476-
Expr *AssumeExpr = AssumeAttr->getAssumption();
477-
// If we skip adding the assumption expression to CFG,
478-
// it doesn't get "branch"-ed by symbol analysis engine
479-
// presumably because it's literally not in the CFG
480452

481-
if (AssumeExpr->HasSideEffects(*AstC)) {
482-
continue;
453+
// Note: Fill in this switch with more cases we want to optimize.
454+
case Stmt::InitListExprClass: {
455+
InitListExpr *IE = cast<InitListExpr>(S);
456+
children = llvm::ArrayRef(reinterpret_cast<Stmt **>(IE->getInits()),
457+
IE->getNumInits());
458+
return;
459+
}
460+
case Stmt::AttributedStmtClass: {
461+
auto *AS = cast<AttributedStmt>(S);
462+
463+
// for an attributed stmt, the "children()" returns only the NullStmt
464+
// (;) but semantically the "children" are supposed to be the
465+
// expressions _within_ i.e. the two square brackets i.e. [[ HERE ]]
466+
// so we add the subexpressions first, _then_ add the "children"
467+
468+
for (const auto *Attr : AS->getAttrs()) {
469+
if (const auto *AssumeAttr = dyn_cast<CXXAssumeAttr>(Attr)) {
470+
Expr *AssumeExpr = AssumeAttr->getAssumption();
471+
if (!AssumeExpr->HasSideEffects(Ctx)) {
472+
childrenBuf.push_back(AssumeExpr);
483473
}
484-
childrenBuf.push_back(AssumeExpr);
485474
}
486-
// children() for an CXXAssumeAttr is NullStmt(;)
487-
// for others, it will have existing behavior
475+
// Visit the actual children AST nodes.
476+
// For CXXAssumeAttrs, this is always a NullStmt.
488477
llvm::append_range(childrenBuf, AS->children());
489478
children = childrenBuf;
490-
return;
491479
}
492-
default:
493-
break;
480+
return;
481+
}
482+
default:
483+
// Default case for all other statements.
484+
llvm::append_range(childrenBuf, S->children());
485+
children = childrenBuf;
494486
}
495-
496-
// Default case for all other statements.
497-
llvm::append_range(childrenBuf, S->children());
498-
499-
// This needs to be done *after* childrenBuf has been populated.
500-
children = childrenBuf;
501487
}
502488

503489
namespace {
@@ -2464,7 +2450,7 @@ CFGBlock *CFGBuilder::VisitChildren(Stmt *S) {
24642450

24652451
// Visit the children in their reverse order so that they appear in
24662452
// left-to-right (natural) order in the CFG.
2467-
reverse_children RChildren(S, Context);
2453+
reverse_children RChildren(S, *Context);
24682454
for (Stmt *Child : RChildren) {
24692455
if (Child)
24702456
if (CFGBlock *R = Visit(Child))
@@ -2480,7 +2466,7 @@ CFGBlock *CFGBuilder::VisitInitListExpr(InitListExpr *ILE, AddStmtChoice asc) {
24802466
}
24812467
CFGBlock *B = Block;
24822468

2483-
reverse_children RChildren(ILE);
2469+
reverse_children RChildren(ILE, *Context);
24842470
for (Stmt *Child : RChildren) {
24852471
if (!Child)
24862472
continue;

clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,7 @@ void ExprEngine::VisitGuardedExpr(const Expr *Ex,
785785
const Expr *R,
786786
ExplodedNode *Pred,
787787
ExplodedNodeSet &Dst) {
788-
assert(L && "L does not exist!");
789-
assert(R && "R does not exist!");
788+
assert(L && R);
790789

791790
StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
792791
ProgramStateRef state = Pred->getState();
@@ -797,17 +796,16 @@ void ExprEngine::VisitGuardedExpr(const Expr *Ex,
797796
ProgramStateRef SrcState = state;
798797

799798
for (const ExplodedNode *N = Pred ; N ; N = *N->pred_begin()) {
800-
ProgramPoint PP = N->getLocation();
801-
if (PP.getAs<PreStmtPurgeDeadSymbols>() || PP.getAs<BlockEntrance>() || PP.getAs<StmtPoint>()) {
799+
auto Edge = N->getLocationAs<BlockEdge>();
800+
if (!Edge.has_value()) {
802801
// If the state N has multiple predecessors P, it means that successors
803802
// of P are all equivalent.
804803
// In turn, that means that all nodes at P are equivalent in terms
805804
// of observable behavior at N, and we can follow any of them.
806805
// FIXME: a more robust solution which does not walk up the tree.
807806
continue;
808807
}
809-
assert(PP.getAs<BlockEdge>());
810-
SrcBlock = PP.castAs<BlockEdge>().getSrc();
808+
SrcBlock = Edge->getSrc();
811809
SrcState = N->getState();
812810
break;
813811
}

clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,17 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "clang/AST/ASTContext.h"
13+
#include "clang/AST/AttrIterator.h"
1414
#include "clang/AST/DeclCXX.h"
1515
#include "clang/AST/ParentMap.h"
1616
#include "clang/AST/StmtCXX.h"
17-
#include "clang/Analysis/AnalysisDeclContext.h"
1817
#include "clang/Analysis/ConstructionContext.h"
19-
#include "clang/Analysis/ProgramPoint.h"
2018
#include "clang/Basic/PrettyStackTrace.h"
2119
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
2220
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
2321
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
24-
#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
2522
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
26-
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
27-
#include "clang/StaticAnalyzer/Core/PathSensitive/RangedConstraintManager.h"
2823
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
29-
#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
3024
#include "llvm/ADT/STLExtras.h"
3125
#include "llvm/ADT/Sequence.h"
3226
#include <optional>
@@ -1216,14 +1210,9 @@ void ExprEngine::VisitAttributedStmt(const AttributedStmt *A,
12161210
ExplodedNodeSet EvalSet;
12171211
StmtNodeBuilder Bldr(CheckerPreStmt, EvalSet, *currBldrCtx);
12181212

1219-
for (const auto *attr : A->getAttrs()) {
1220-
CXXAssumeAttr const *AssumeAttr = llvm::dyn_cast<CXXAssumeAttr>(attr);
1221-
if (!AssumeAttr) {
1222-
continue;
1223-
}
1224-
Expr *AssumeExpr = AssumeAttr->getAssumption();
1213+
for (const auto *Attr : getSpecificAttrs<CXXAssumeAttr>(A->getAttrs())) {
12251214
for (ExplodedNode *N : CheckerPreStmt) {
1226-
Visit(AssumeExpr, N, EvalSet);
1215+
Visit(Attr->getAssumption(), N, EvalSet);
12271216
}
12281217
}
12291218

clang/test/Analysis/cxx23-assume-attribute.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ int ternary_in_builtin_assume(int a, int b) {
2828
int ternary_in_assume(int a, int b) {
2929
// FIXME notes
3030
// Currently, if this test is run without the core.builtin.Builtin checker, the above function with the __builtin_assume behaves identically to the following test
31-
// i.e. calls to `clang_analyzer_dump` result in "extraneous" prints of the SVal(s) `reg_$2<int > b ...`
31+
// i.e. calls to `clang_analyzer_dump` result in "extraneous" prints of the SVal(s) `reg_$2<int > b ...`
3232
// as opposed to 4 or 10
3333
// which likely implies the Program State(s) did not get narrowed.
3434
// A new checker is likely needed to be implemented to properly handle the expressions within `[[assume]]` to eliminate the states where `b` is not narrowed.

0 commit comments

Comments
 (0)