Skip to content

Commit dc4dda1

Browse files
authored
Merge pull request #14757 from aschackmull/rangeanalysis/simplify
Rangeanalysis: Misc simplifications
2 parents 291bb22 + bf6cfd3 commit dc4dda1

File tree

13 files changed

+57
-200
lines changed

13 files changed

+57
-200
lines changed

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/SemanticSSA.qll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ class SemSsaExplicitUpdate extends SemSsaVariable {
2222

2323
SemSsaExplicitUpdate() { Specific::explicitUpdate(this, sourceExpr) }
2424

25-
final SemExpr getSourceExpr() { result = sourceExpr }
26-
2725
final SemExpr getDefiningExpr() { result = sourceExpr }
2826
}
2927

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/ConstantAnalysis.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ private predicate constantIntegerExpr(SemExpr e, int val) {
1414
// Copy of another constant
1515
exists(SemSsaExplicitUpdate v, SemExpr src |
1616
e = v.getAUse() and
17-
src = v.getSourceExpr() and
17+
src = v.getDefiningExpr() and
1818
constantIntegerExpr(src, val)
1919
)
2020
or

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/RangeAnalysisConstantSpecific.qll

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,7 @@ module CppLangImplConstant implements LangSig<Sem, FloatDelta> {
2222
predicate hasConstantBound(SemExpr e, float bound, boolean upper) { none() }
2323

2424
/**
25-
* Holds if `e >= bound + delta` (if `upper = false`) or `e <= bound + delta` (if `upper = true`).
25+
* Holds if `e2 >= e1 + delta` (if `upper = false`) or `e2 <= e1 + delta` (if `upper = true`).
2626
*/
27-
predicate hasBound(SemExpr e, SemExpr bound, float delta, boolean upper) { none() }
28-
29-
/**
30-
* Gets the type that range analysis should use to track the result of the specified expression,
31-
* if a type other than the original type of the expression is to be used.
32-
*
33-
* This predicate is commonly used in languages that support immutable "boxed" types that are
34-
* actually references but whose values can be tracked as the type contained in the box.
35-
*/
36-
SemType getAlternateType(SemExpr e) { none() }
37-
38-
/**
39-
* Gets the type that range analysis should use to track the result of the specified source
40-
* variable, if a type other than the original type of the expression is to be used.
41-
*
42-
* This predicate is commonly used in languages that support immutable "boxed" types that are
43-
* actually references but whose values can be tracked as the type contained in the box.
44-
*/
45-
SemType getAlternateTypeForSsaVariable(SemSsaVariable var) { none() }
27+
predicate additionalBoundFlowStep(SemExpr e2, SemExpr e1, float delta, boolean upper) { none() }
4628
}

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/RangeAnalysisImpl.qll

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
private import RangeAnalysisConstantSpecific
22
private import RangeAnalysisRelativeSpecific
33
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.FloatDelta
4-
private import RangeUtils
54
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.SemanticExpr
65
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.SemanticCFG
76
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.SemanticGuard
@@ -88,6 +87,10 @@ module Sem implements Semantic {
8887

8988
class AddressType = SemAddressType;
9089

90+
SemType getExprType(SemExpr e) { result = e.getSemType() }
91+
92+
SemType getSsaType(SemSsaVariable var) { result = var.getType() }
93+
9194
class SsaVariable = SemSsaVariable;
9295

9396
class SsaPhiNode = SemSsaPhiNode;
@@ -103,7 +106,7 @@ module Sem implements Semantic {
103106

104107
module SignAnalysis implements SignAnalysisSig<Sem> {
105108
private import SignAnalysisCommon as SA
106-
import SA::SignAnalysis<FloatDelta, Util>
109+
import SA::SignAnalysis<FloatDelta>
107110
}
108111

109112
module ConstantBounds implements BoundSig<SemLocation, Sem, FloatDelta> {
@@ -166,18 +169,16 @@ private module ModulusAnalysisInstantiated implements ModulusAnalysisSig<Sem> {
166169
class ModBound = AllBounds::SemBound;
167170

168171
private import codeql.rangeanalysis.ModulusAnalysis as MA
169-
import MA::ModulusAnalysis<SemLocation, Sem, FloatDelta, AllBounds, Util>
172+
import MA::ModulusAnalysis<SemLocation, Sem, FloatDelta, AllBounds>
170173
}
171174

172-
module Util = RangeUtil<FloatDelta, CppLangImplConstant>;
173-
174175
module ConstantStage =
175176
RangeStage<SemLocation, Sem, FloatDelta, ConstantBounds, FloatOverflow, CppLangImplConstant,
176-
SignAnalysis, ModulusAnalysisInstantiated, Util>;
177+
SignAnalysis, ModulusAnalysisInstantiated>;
177178

178179
module RelativeStage =
179180
RangeStage<SemLocation, Sem, FloatDelta, RelativeBounds, FloatOverflow, CppLangImplRelative,
180-
SignAnalysis, ModulusAnalysisInstantiated, Util>;
181+
SignAnalysis, ModulusAnalysisInstantiated>;
181182

182183
private newtype TSemReason =
183184
TSemNoReason() or

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/RangeAnalysisRelativeSpecific.qll

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,7 @@ module CppLangImplRelative implements LangSig<Sem, FloatDelta> {
5454
predicate hasConstantBound(SemExpr e, float bound, boolean upper) { none() }
5555

5656
/**
57-
* Holds if `e >= bound + delta` (if `upper = false`) or `e <= bound + delta` (if `upper = true`).
57+
* Holds if `e2 >= e1 + delta` (if `upper = false`) or `e2 <= e1 + delta` (if `upper = true`).
5858
*/
59-
predicate hasBound(SemExpr e, SemExpr bound, float delta, boolean upper) { none() }
60-
61-
/**
62-
* Gets the type that range analysis should use to track the result of the specified expression,
63-
* if a type other than the original type of the expression is to be used.
64-
*
65-
* This predicate is commonly used in languages that support immutable "boxed" types that are
66-
* actually references but whose values can be tracked as the type contained in the box.
67-
*/
68-
SemType getAlternateType(SemExpr e) { none() }
69-
70-
/**
71-
* Gets the type that range analysis should use to track the result of the specified source
72-
* variable, if a type other than the original type of the expression is to be used.
73-
*
74-
* This predicate is commonly used in languages that support immutable "boxed" types that are
75-
* actually references but whose values can be tracked as the type contained in the box.
76-
*/
77-
SemType getAlternateTypeForSsaVariable(SemSsaVariable var) { none() }
59+
predicate additionalBoundFlowStep(SemExpr e2, SemExpr e1, float delta, boolean upper) { none() }
7860
}

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/RangeUtils.qll

Lines changed: 0 additions & 35 deletions
This file was deleted.

cpp/ql/lib/semmle/code/cpp/rangeanalysis/new/internal/semantic/analysis/SignAnalysisCommon.qll

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ private import RangeAnalysisImpl
1111
private import SignAnalysisSpecific as Specific
1212
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.Semantic
1313
private import ConstantAnalysis
14-
private import RangeUtils
1514
private import Sign
1615

17-
module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
16+
module SignAnalysis<DeltaSig D> {
1817
private import codeql.rangeanalysis.internal.RangeUtils::MakeUtils<Sem, D>
1918

2019
/**
@@ -39,7 +38,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
3938

4039
/** An SSA definition whose sign is determined by the sign of that definitions source expression. */
4140
private class ExplicitSignDef extends FlowSignDef instanceof SemSsaExplicitUpdate {
42-
final override Sign getSign() { result = semExprSign(super.getSourceExpr()) }
41+
final override Sign getSign() { result = semExprSign(super.getDefiningExpr()) }
4342
}
4443

4544
/** An SSA Phi definition, whose sign is the union of the signs of its inputs. */
@@ -148,7 +147,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
148147
not this instanceof ConstantSignExpr and
149148
(
150149
// Only track numeric types.
151-
Utils::getTrackedType(this) instanceof SemNumericType
150+
Sem::getExprType(this) instanceof SemNumericType
152151
or
153152
// Unless the language says to track this expression anyway.
154153
Specific::trackUnknownNonNumericExpr(this)
@@ -203,7 +202,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
203202

204203
/** An expression of an unsigned type. */
205204
private class UnsignedExpr extends FlowSignExpr {
206-
UnsignedExpr() { Utils::getTrackedType(this) instanceof SemUnsignedIntegerType }
205+
UnsignedExpr() { Sem::getExprType(this) instanceof SemUnsignedIntegerType }
207206

208207
override Sign getSignRestriction() {
209208
result = TPos() or
@@ -276,7 +275,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
276275
override SemUnboxExpr cast;
277276

278277
UnboxSignExpr() {
279-
exists(SemType fromType | fromType = Utils::getTrackedType(cast.getOperand()) |
278+
exists(SemType fromType | fromType = Sem::getExprType(cast.getOperand()) |
280279
// Only numeric source types are handled here.
281280
fromType instanceof SemNumericType
282281
)
@@ -471,7 +470,7 @@ module SignAnalysis<DeltaSig D, UtilSig<Sem, D> Utils> {
471470
Sign semExprSign(SemExpr e) {
472471
exists(Sign s | s = e.(SignExpr).getSign() |
473472
if
474-
Utils::getTrackedType(e) instanceof SemUnsignedIntegerType and
473+
Sem::getExprType(e) instanceof SemUnsignedIntegerType and
475474
s = TNeg() and
476475
not Specific::ignoreTypeRestrictions(e)
477476
then result = TPos()

cpp/ql/test/library-tests/ir/modulus-analysis/ModulusAnalysis.ql

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@ import cpp
22
import codeql.rangeanalysis.ModulusAnalysis
33
import semmle.code.cpp.rangeanalysis.new.internal.semantic.Semantic
44
import semmle.code.cpp.rangeanalysis.new.internal.semantic.SemanticLocation
5-
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.RangeUtils
65
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.FloatDelta
76
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.RangeAnalysisRelativeSpecific
87
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.RangeAnalysisImpl
98
import semmle.code.cpp.rangeanalysis.new.internal.semantic.SemanticExprSpecific
109
import semmle.code.cpp.ir.IR as IR
1110
import TestUtilities.InlineExpectationsTest
1211

13-
module ModulusAnalysisInstantiated =
14-
ModulusAnalysis<SemLocation, Sem, FloatDelta, ConstantBounds,
15-
RangeUtil<FloatDelta, CppLangImplRelative>>;
12+
module ModulusAnalysisInstantiated = ModulusAnalysis<SemLocation, Sem, FloatDelta, ConstantBounds>;
1613

1714
module ModulusAnalysisTest implements TestSig {
1815
string getARelevantTag() { result = "mod" }

cpp/ql/test/library-tests/ir/sign-analysis/SignAnalysis.ql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import cpp
22
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.SignAnalysisCommon
33
import semmle.code.cpp.rangeanalysis.new.internal.semantic.Semantic
4-
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.RangeUtils
54
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.FloatDelta
65
import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.RangeAnalysisRelativeSpecific
76
import semmle.code.cpp.rangeanalysis.new.internal.semantic.SemanticExprSpecific
87
import semmle.code.cpp.ir.IR as IR
98
import TestUtilities.InlineExpectationsTest
109

11-
module SignAnalysisInstantiated =
12-
SignAnalysis<FloatDelta, RangeUtil<FloatDelta, CppLangImplRelative>>;
10+
module SignAnalysisInstantiated = SignAnalysis<FloatDelta>;
1311

1412
module SignAnalysisTest implements TestSig {
1513
string getARelevantTag() { result = "sign" }

java/ql/lib/semmle/code/java/dataflow/NullGuards.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import java
66
import SSA
77
private import semmle.code.java.controlflow.internal.GuardsLogic
88
private import semmle.code.java.frameworks.apache.Collections
9-
private import RangeUtils
109
private import IntegerGuards
1110

1211
/** Gets an expression that is always `null`. */

0 commit comments

Comments
 (0)