Skip to content

Commit 4995d09

Browse files
authored
[analyzer][Solver] Improve getSymVal and friends (1/2) (#112583)
1 parent 1a871b2 commit 4995d09

File tree

3 files changed

+14
-39
lines changed

3 files changed

+14
-39
lines changed

clang/lib/StaticAnalyzer/Checkers/BitwiseShiftChecker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ BugReportPtr BitwiseShiftValidator::checkOvershift() {
177177
RightOpStr = formatv(" '{0}'", ConcreteRight->getValue());
178178
else {
179179
SValBuilder &SVB = Ctx.getSValBuilder();
180-
if (const llvm::APSInt *MinRight = SVB.getMinValue(FoldedState, Right)) {
180+
if (const llvm::APSInt *MinRight = SVB.getMinValue(FoldedState, Right);
181+
MinRight && *MinRight >= LHSBitWidth) {
181182
LowerBoundStr = formatv(" >= {0},", MinRight->getExtValue());
182183
}
183184
}

clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,11 +1939,8 @@ class RangeConstraintManager : public RangedConstraintManager {
19391939
RangeSet::Factory F;
19401940

19411941
RangeSet getRange(ProgramStateRef State, SymbolRef Sym);
1942-
RangeSet getRange(ProgramStateRef State, EquivalenceClass Class);
19431942
ProgramStateRef setRange(ProgramStateRef State, SymbolRef Sym,
19441943
RangeSet Range);
1945-
ProgramStateRef setRange(ProgramStateRef State, EquivalenceClass Class,
1946-
RangeSet Range);
19471944

19481945
RangeSet getSymLTRange(ProgramStateRef St, SymbolRef Sym,
19491946
const llvm::APSInt &Int,
@@ -2866,24 +2863,22 @@ ConditionTruthVal RangeConstraintManager::checkNull(ProgramStateRef State,
28662863

28672864
const llvm::APSInt *RangeConstraintManager::getSymVal(ProgramStateRef St,
28682865
SymbolRef Sym) const {
2869-
const RangeSet *T = getConstraint(St, Sym);
2870-
return T ? T->getConcreteValue() : nullptr;
2866+
auto &MutableSelf = const_cast<RangeConstraintManager &>(*this);
2867+
return MutableSelf.getRange(St, Sym).getConcreteValue();
28712868
}
28722869

28732870
const llvm::APSInt *RangeConstraintManager::getSymMinVal(ProgramStateRef St,
28742871
SymbolRef Sym) const {
2875-
const RangeSet *T = getConstraint(St, Sym);
2876-
if (!T || T->isEmpty())
2877-
return nullptr;
2878-
return &T->getMinValue();
2872+
auto &MutableSelf = const_cast<RangeConstraintManager &>(*this);
2873+
RangeSet Range = MutableSelf.getRange(St, Sym);
2874+
return Range.isEmpty() ? nullptr : &Range.getMinValue();
28792875
}
28802876

28812877
const llvm::APSInt *RangeConstraintManager::getSymMaxVal(ProgramStateRef St,
28822878
SymbolRef Sym) const {
2883-
const RangeSet *T = getConstraint(St, Sym);
2884-
if (!T || T->isEmpty())
2885-
return nullptr;
2886-
return &T->getMaxValue();
2879+
auto &MutableSelf = const_cast<RangeConstraintManager &>(*this);
2880+
RangeSet Range = MutableSelf.getRange(St, Sym);
2881+
return Range.isEmpty() ? nullptr : &Range.getMaxValue();
28872882
}
28882883

28892884
//===----------------------------------------------------------------------===//

clang/test/Analysis/infeasible-sink.c

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void test1(int x) {
3838
}
3939

4040
int a, b, c, d, e;
41-
void test2() {
41+
void test2(void) {
4242

4343
if (a == 0)
4444
return;
@@ -50,31 +50,10 @@ void test2() {
5050
b = d;
5151
a -= d;
5252

53-
if (a != 0)
54-
return;
55-
56-
clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
53+
clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
5754

58-
/* The BASELINE passes these checks ('wrning' is used to avoid lit to match)
59-
// The parent state is already infeasible, look at this contradiction:
60-
clang_analyzer_eval(b > 0); // expected-wrning{{FALSE}}
61-
clang_analyzer_eval(b <= 0); // expected-wrning{{FALSE}}
62-
// Crashes with expensive checks.
63-
if (b > 0) {
64-
clang_analyzer_warnIfReached(); // no-warning, OK
55+
if (a != 0)
6556
return;
66-
}
67-
// Should not be reachable.
68-
clang_analyzer_warnIfReached(); // expected-wrning{{REACHABLE}}
69-
*/
7057

71-
// The parent state is already infeasible, but we realize that only if b is
72-
// constrained.
73-
clang_analyzer_eval(b > 0); // expected-warning{{UNKNOWN}}
74-
clang_analyzer_eval(b <= 0); // expected-warning{{UNKNOWN}}
75-
if (b > 0) {
76-
clang_analyzer_warnIfReached(); // no-warning
77-
return;
78-
}
79-
clang_analyzer_warnIfReached(); // no-warning
58+
clang_analyzer_warnIfReached(); // no-warning: Unreachable due to contradiction.
8059
}

0 commit comments

Comments
 (0)