Skip to content

Commit f3e804b

Browse files
authored
[analyzer][clang-tidy][NFC] Clean up eagerly-assume handling (#112209)
This commit is a collection of several very minor code quality improvements. The main goal is removing the misleading "Bin" substring from the names of several methods and variables (like `evalEagerlyAssumedBinOpBifurcation`) that are also applied for the unary logical not operator. In addition to this, I clarified the doc-comment of the method `evalEagerlyAssumedBinOpBifurcation` and refactored the body of this method to fix the capitalization of variable names and replace an obsolete use of `std::tie` with a structured binding. Finally, the data member `eagerlyAssumeBinOpBifurcation` of the class `AnalyzerOptions` was completely removed (including a line in clang-tidy that sets it to true), because it was never read by any code. Note that the eagerly-assume mode of the analyzer is controlled by a different boolean member of `AnalyzerOptions` which is called `ShouldEagerlyAssume` and is defined via the macro magic from `AnalyzerOptions.def`.
1 parent 682fa79 commit f3e804b

File tree

6 files changed

+33
-43
lines changed

6 files changed

+33
-43
lines changed

clang-tools-extra/clang-tidy/ClangTidy.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@ ClangTidyASTConsumerFactory::createASTConsumer(
458458
if (!AnalyzerOptions.CheckersAndPackages.empty()) {
459459
setStaticAnalyzerCheckerOpts(Context.getOptions(), AnalyzerOptions);
460460
AnalyzerOptions.AnalysisDiagOpt = PD_NONE;
461-
AnalyzerOptions.eagerlyAssumeBinOpBifurcation = true;
462461
std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer =
463462
ento::CreateAnalysisConsumer(Compiler);
464463
AnalysisConsumer->AddDiagnosticConsumer(

clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.def

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,12 @@ ANALYZER_OPTION(
299299

300300
ANALYZER_OPTION(
301301
bool, ShouldEagerlyAssume, "eagerly-assume",
302-
"Whether we should eagerly assume evaluations of conditionals, thus, "
303-
"bifurcating the path. This indicates how the engine should handle "
304-
"expressions such as: 'x = (y != 0)'. When this is true then the "
305-
"subexpression 'y != 0' will be eagerly assumed to be true or false, thus "
306-
"evaluating it to the integers 0 or 1 respectively. The upside is that "
307-
"this can increase analysis precision until we have a better way to lazily "
308-
"evaluate such logic. The downside is that it eagerly bifurcates paths.",
302+
"If this is enabled (the default behavior), when the analyzer encounters "
303+
"a comparison operator or logical negation, it immediately splits the "
304+
"state to separate the case when the expression is true and the case when "
305+
"it's false. The upside is that this can increase analysis precision until "
306+
"we have a better way to lazily evaluate such logic; the downside is that "
307+
"it eagerly bifurcates paths.",
309308
true)
310309

311310
ANALYZER_OPTION(

clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,6 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
229229
unsigned AnalyzerDisplayProgress : 1;
230230
unsigned AnalyzerNoteAnalysisEntryPoints : 1;
231231

232-
unsigned eagerlyAssumeBinOpBifurcation : 1;
233-
234232
unsigned TrimGraph : 1;
235233
unsigned visualizeExplodedGraphWithGraphViz : 1;
236234
unsigned UnoptimizedCFG : 1;
@@ -293,9 +291,9 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
293291
ShowConfigOptionsList(false),
294292
ShouldEmitErrorsOnInvalidConfigValue(false), AnalyzeAll(false),
295293
AnalyzerDisplayProgress(false), AnalyzerNoteAnalysisEntryPoints(false),
296-
eagerlyAssumeBinOpBifurcation(false), TrimGraph(false),
297-
visualizeExplodedGraphWithGraphViz(false), UnoptimizedCFG(false),
298-
PrintStats(false), NoRetryExhausted(false), AnalyzerWerror(false) {}
294+
TrimGraph(false), visualizeExplodedGraphWithGraphViz(false),
295+
UnoptimizedCFG(false), PrintStats(false), NoRetryExhausted(false),
296+
AnalyzerWerror(false) {}
299297

300298
/// Interprets an option's string value as a boolean. The "true" string is
301299
/// interpreted as true and the "false" string is interpreted as false.

clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,14 +583,13 @@ class ExprEngine {
583583
ExplodedNode *Pred,
584584
ExplodedNodeSet &Dst);
585585

586-
/// evalEagerlyAssumeBinOpBifurcation - Given the nodes in 'Src', eagerly assume symbolic
587-
/// expressions of the form 'x != 0' and generate new nodes (stored in Dst)
588-
/// with those assumptions.
589-
void evalEagerlyAssumeBinOpBifurcation(ExplodedNodeSet &Dst, ExplodedNodeSet &Src,
590-
const Expr *Ex);
586+
/// evalEagerlyAssumeBifurcation - Given the nodes in 'Src', eagerly assume
587+
/// concrete boolean values for 'Ex', storing the resulting nodes in 'Dst'.
588+
void evalEagerlyAssumeBifurcation(ExplodedNodeSet &Dst, ExplodedNodeSet &Src,
589+
const Expr *Ex);
591590

592591
static std::pair<const ProgramPointTag *, const ProgramPointTag *>
593-
geteagerlyAssumeBinOpBifurcationTags();
592+
getEagerlyAssumeBifurcationTags();
594593

595594
ProgramStateRef handleLValueBitCast(ProgramStateRef state, const Expr *Ex,
596595
const LocationContext *LCtx, QualType T,

clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2695,7 +2695,7 @@ ConditionBRVisitor::VisitNodeImpl(const ExplodedNode *N,
26952695
PathSensitiveBugReport &BR) {
26962696
ProgramPoint ProgPoint = N->getLocation();
26972697
const std::pair<const ProgramPointTag *, const ProgramPointTag *> &Tags =
2698-
ExprEngine::geteagerlyAssumeBinOpBifurcationTags();
2698+
ExprEngine::getEagerlyAssumeBifurcationTags();
26992699

27002700
// If an assumption was made on a branch, it should be caught
27012701
// here by looking at the state transition.

clang/lib/StaticAnalyzer/Core/ExprEngine.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,7 +2129,7 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred,
21292129
(B->isRelationalOp() || B->isEqualityOp())) {
21302130
ExplodedNodeSet Tmp;
21312131
VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
2132-
evalEagerlyAssumeBinOpBifurcation(Dst, Tmp, cast<Expr>(S));
2132+
evalEagerlyAssumeBifurcation(Dst, Tmp, cast<Expr>(S));
21332133
}
21342134
else
21352135
VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
@@ -2402,7 +2402,7 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred,
24022402
if (AMgr.options.ShouldEagerlyAssume && (U->getOpcode() == UO_LNot)) {
24032403
ExplodedNodeSet Tmp;
24042404
VisitUnaryOperator(U, Pred, Tmp);
2405-
evalEagerlyAssumeBinOpBifurcation(Dst, Tmp, U);
2405+
evalEagerlyAssumeBifurcation(Dst, Tmp, U);
24062406
}
24072407
else
24082408
VisitUnaryOperator(U, Pred, Dst);
@@ -3742,23 +3742,20 @@ void ExprEngine::evalLocation(ExplodedNodeSet &Dst,
37423742
BldrTop.addNodes(Tmp);
37433743
}
37443744

3745-
std::pair<const ProgramPointTag *, const ProgramPointTag*>
3746-
ExprEngine::geteagerlyAssumeBinOpBifurcationTags() {
3747-
static SimpleProgramPointTag
3748-
eagerlyAssumeBinOpBifurcationTrue(TagProviderName,
3749-
"Eagerly Assume True"),
3750-
eagerlyAssumeBinOpBifurcationFalse(TagProviderName,
3751-
"Eagerly Assume False");
3752-
return std::make_pair(&eagerlyAssumeBinOpBifurcationTrue,
3753-
&eagerlyAssumeBinOpBifurcationFalse);
3745+
std::pair<const ProgramPointTag *, const ProgramPointTag *>
3746+
ExprEngine::getEagerlyAssumeBifurcationTags() {
3747+
static SimpleProgramPointTag TrueTag(TagProviderName, "Eagerly Assume True"),
3748+
FalseTag(TagProviderName, "Eagerly Assume False");
3749+
3750+
return std::make_pair(&TrueTag, &FalseTag);
37543751
}
37553752

3756-
void ExprEngine::evalEagerlyAssumeBinOpBifurcation(ExplodedNodeSet &Dst,
3757-
ExplodedNodeSet &Src,
3758-
const Expr *Ex) {
3753+
void ExprEngine::evalEagerlyAssumeBifurcation(ExplodedNodeSet &Dst,
3754+
ExplodedNodeSet &Src,
3755+
const Expr *Ex) {
37593756
StmtNodeBuilder Bldr(Src, Dst, *currBldrCtx);
37603757

3761-
for (const auto Pred : Src) {
3758+
for (ExplodedNode *Pred : Src) {
37623759
// Test if the previous node was as the same expression. This can happen
37633760
// when the expression fails to evaluate to anything meaningful and
37643761
// (as an optimization) we don't generate a node.
@@ -3767,28 +3764,26 @@ void ExprEngine::evalEagerlyAssumeBinOpBifurcation(ExplodedNodeSet &Dst,
37673764
continue;
37683765
}
37693766

3770-
ProgramStateRef state = Pred->getState();
3771-
SVal V = state->getSVal(Ex, Pred->getLocationContext());
3767+
ProgramStateRef State = Pred->getState();
3768+
SVal V = State->getSVal(Ex, Pred->getLocationContext());
37723769
std::optional<nonloc::SymbolVal> SEV = V.getAs<nonloc::SymbolVal>();
37733770
if (SEV && SEV->isExpression()) {
3774-
const std::pair<const ProgramPointTag *, const ProgramPointTag*> &tags =
3775-
geteagerlyAssumeBinOpBifurcationTags();
3771+
const auto &[TrueTag, FalseTag] = getEagerlyAssumeBifurcationTags();
37763772

3777-
ProgramStateRef StateTrue, StateFalse;
3778-
std::tie(StateTrue, StateFalse) = state->assume(*SEV);
3773+
auto [StateTrue, StateFalse] = State->assume(*SEV);
37793774

37803775
// First assume that the condition is true.
37813776
if (StateTrue) {
37823777
SVal Val = svalBuilder.makeIntVal(1U, Ex->getType());
37833778
StateTrue = StateTrue->BindExpr(Ex, Pred->getLocationContext(), Val);
3784-
Bldr.generateNode(Ex, Pred, StateTrue, tags.first);
3779+
Bldr.generateNode(Ex, Pred, StateTrue, TrueTag);
37853780
}
37863781

37873782
// Next, assume that the condition is false.
37883783
if (StateFalse) {
37893784
SVal Val = svalBuilder.makeIntVal(0U, Ex->getType());
37903785
StateFalse = StateFalse->BindExpr(Ex, Pred->getLocationContext(), Val);
3791-
Bldr.generateNode(Ex, Pred, StateFalse, tags.second);
3786+
Bldr.generateNode(Ex, Pred, StateFalse, FalseTag);
37923787
}
37933788
}
37943789
}

0 commit comments

Comments
 (0)