Skip to content

Commit d398c8c

Browse files
committed
C++: Share some code by introducing a parameterized module to construct reasons.
1 parent 7270b50 commit d398c8c

File tree

3 files changed

+91
-91
lines changed

3 files changed

+91
-91
lines changed

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

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,23 @@
33
*/
44

55
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.Semantic
6+
private import codeql.util.Unit
7+
private import Reason as Reason
68
private import RangeAnalysisStage
79
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.FloatDelta
810

911
module CppLangImplConstant implements LangSig<FloatDelta> {
10-
private newtype TSemReason =
11-
TSemNoReason() or
12-
TSemCondReason(SemGuard guard) or
13-
TSemTypeReason()
14-
15-
/**
16-
* A reason for an inferred bound. This can either be `CondReason` if the bound
17-
* is due to a specific condition, or `NoReason` if the bound is inferred
18-
* without going through a bounding condition.
19-
*/
20-
abstract class SemReason extends TSemReason {
21-
/** Gets a textual representation of this reason. */
22-
abstract string toString();
23-
24-
bindingset[this, reason]
25-
abstract SemReason combineWith(SemReason reason);
26-
}
27-
28-
/**
29-
* A reason for an inferred bound that indicates that the bound is inferred
30-
* without going through a bounding condition.
31-
*/
32-
class SemNoReason extends SemReason, TSemNoReason {
33-
override string toString() { result = "NoReason" }
34-
35-
override SemReason combineWith(SemReason reason) { result = reason }
12+
private module Param implements Reason::ParamSig {
13+
class TypeReasonImpl = Unit;
3614
}
3715

38-
/** A reason for an inferred bound pointing to a condition. */
39-
class SemCondReason extends SemReason, TSemCondReason {
40-
/** Gets the condition that is the reason for the bound. */
41-
SemGuard getCond() { this = TSemCondReason(result) }
16+
class SemReason = Reason::Make<Param>::SemReason;
4217

43-
override string toString() { result = this.getCond().toString() }
18+
class SemNoReason = Reason::Make<Param>::SemNoReason;
4419

45-
bindingset[this, reason]
46-
override SemReason combineWith(SemReason reason) {
47-
if reason instanceof SemTypeReason then result instanceof SemTypeReason else result = this
48-
}
49-
}
20+
class SemCondReason = Reason::Make<Param>::SemCondReason;
5021

51-
/**
52-
* A reason for an inferred bound that indicates that the bound is inferred
53-
* based on type-information.
54-
*/
55-
class SemTypeReason extends SemReason, TSemTypeReason {
56-
override string toString() { result = "TypeReason" }
57-
58-
override SemReason combineWith(SemReason reason) { result = this and exists(reason) }
59-
}
22+
class SemTypeReason = Reason::Make<Param>::SemTypeReason;
6023

6124
/**
6225
* Holds if the specified expression should be excluded from the result of `ssaRead()`.

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

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,24 @@ private import RangeAnalysisStage
77
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.FloatDelta
88
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.analysis.IntDelta
99
private import RangeAnalysisImpl
10+
private import codeql.util.Unit
11+
private import Reason as Reason
1012
private import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
1113

1214
module CppLangImplRelative implements LangSig<FloatDelta> {
13-
private newtype TSemReason =
14-
TSemNoReason() or
15-
TSemCondReason(SemGuard guard)
16-
17-
/**
18-
* A reason for an inferred bound. This can either be `CondReason` if the bound
19-
* is due to a specific condition, or `NoReason` if the bound is inferred
20-
* without going through a bounding condition.
21-
*/
22-
abstract class SemReason extends TSemReason {
23-
/** Gets a textual representation of this reason. */
24-
abstract string toString();
25-
26-
bindingset[this, reason]
27-
abstract SemReason combineWith(SemReason reason);
28-
}
29-
30-
/**
31-
* A reason for an inferred boudn that indicates that the bound is inferred
32-
* based on type-information.
33-
*
34-
* NOTE: The relative stage does not infer any bounds based on type-information.
35-
*/
36-
class SemTypeReason extends SemReason {
37-
SemTypeReason() { none() }
38-
39-
override string toString() { result = "TypeReason" }
40-
41-
override SemReason combineWith(SemReason reason) { none() }
15+
private module Param implements Reason::ParamSig {
16+
class TypeReasonImpl extends Unit {
17+
TypeReasonImpl() { none() }
18+
}
4219
}
4320

44-
/**
45-
* A reason for an inferred bound that indicates that the bound is inferred
46-
* without going through a bounding condition.
47-
*/
48-
class SemNoReason extends SemReason, TSemNoReason {
49-
override string toString() { result = "NoReason" }
50-
51-
override SemReason combineWith(SemReason reason) { result = reason }
52-
}
21+
class SemReason = Reason::Make<Param>::SemReason;
5322

54-
/** A reason for an inferred bound pointing to a condition. */
55-
class SemCondReason extends SemReason, TSemCondReason {
56-
/** Gets the condition that is the reason for the bound. */
57-
SemGuard getCond() { this = TSemCondReason(result) }
23+
class SemNoReason = Reason::Make<Param>::SemNoReason;
5824

59-
override string toString() { result = this.getCond().toString() }
25+
class SemCondReason = Reason::Make<Param>::SemCondReason;
6026

61-
bindingset[this, reason]
62-
override SemReason combineWith(SemReason reason) { result = reason }
63-
}
27+
class SemTypeReason = Reason::Make<Param>::SemTypeReason;
6428

6529
/**
6630
* Holds if the specified expression should be excluded from the result of `ssaRead()`.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Provides a `Make` parameterized module for constructing a `Reason` type that is used
3+
* when implementing the `LangSig` module.
4+
*/
5+
6+
private import semmle.code.cpp.rangeanalysis.new.internal.semantic.Semantic
7+
8+
/** The necessary parameters that must be implemented to instantiate `Make`. */
9+
signature module ParamSig {
10+
class TypeReasonImpl;
11+
}
12+
13+
/**
14+
* The module that constructs a `Reason` type when provided with an implementation
15+
* of `ParamSig`.
16+
*/
17+
module Make<ParamSig Param> {
18+
private import Param
19+
20+
private newtype TSemReason =
21+
TSemNoReason() or
22+
TSemCondReason(SemGuard guard) or
23+
TSemTypeReason(TypeReasonImpl trc)
24+
25+
/**
26+
* A reason for an inferred bound. This can either be `CondReason` if the bound
27+
* is due to a specific condition, or `NoReason` if the bound is inferred
28+
* without going through a bounding condition.
29+
*/
30+
abstract class SemReason extends TSemReason {
31+
/** Gets a textual representation of this reason. */
32+
abstract string toString();
33+
34+
bindingset[this, reason]
35+
abstract SemReason combineWith(SemReason reason);
36+
}
37+
38+
/**
39+
* A reason for an inferred bound that indicates that the bound is inferred
40+
* without going through a bounding condition.
41+
*/
42+
class SemNoReason extends SemReason, TSemNoReason {
43+
override string toString() { result = "NoReason" }
44+
45+
override SemReason combineWith(SemReason reason) { result = reason }
46+
}
47+
48+
/** A reason for an inferred bound pointing to a condition. */
49+
class SemCondReason extends SemReason, TSemCondReason {
50+
/** Gets the condition that is the reason for the bound. */
51+
SemGuard getCond() { this = TSemCondReason(result) }
52+
53+
override string toString() { result = this.getCond().toString() }
54+
55+
bindingset[this, reason]
56+
override SemReason combineWith(SemReason reason) {
57+
if reason instanceof SemTypeReason then result instanceof SemTypeReason else result = this
58+
}
59+
}
60+
61+
/**
62+
* A reason for an inferred bound that indicates that the bound is inferred
63+
* based on type-information.
64+
*/
65+
class SemTypeReason extends SemReason, TSemTypeReason {
66+
TypeReasonImpl impl;
67+
68+
override string toString() { result = "TypeReason" }
69+
70+
bindingset[this, reason]
71+
override SemReason combineWith(SemReason reason) { result = this and exists(reason) }
72+
}
73+
}

0 commit comments

Comments
 (0)