Skip to content

Commit d957c15

Browse files
authored
Merge pull request #110 from jbj/fewer-dbtypes
Approved by ian-semmle
2 parents 261cfe9 + 4cc2745 commit d957c15

File tree

37 files changed

+3064
-3046
lines changed

37 files changed

+3064
-3046
lines changed

cpp/ql/src/AlertSuppression.ql

+4-14
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,16 @@ class SuppressionComment extends CppStyleComment {
4747

4848
/** Gets the scope of this suppression. */
4949
SuppressionScope getScope() {
50-
this = result.getSuppressionComment()
50+
result = this
5151
}
5252
}
5353

5454
/**
5555
* The scope of an alert suppression comment.
5656
*/
57-
class SuppressionScope extends @comment {
57+
class SuppressionScope extends ElementBase {
5858
SuppressionScope() {
59-
mkElement(this) instanceof SuppressionComment
60-
}
61-
62-
/** Gets a suppression comment with this scope. */
63-
SuppressionComment getSuppressionComment() {
64-
result = mkElement(this)
59+
this instanceof SuppressionComment
6560
}
6661

6762
/**
@@ -72,12 +67,7 @@ class SuppressionScope extends @comment {
7267
* [LGTM locations](https://lgtm.com/help/ql/locations).
7368
*/
7469
predicate hasLocationInfo(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
75-
mkElement(this).(SuppressionComment).covers(filepath, startline, startcolumn, endline, endcolumn)
76-
}
77-
78-
/** Gets a textual representation of this element. */
79-
string toString() {
80-
result = "suppression range"
70+
this.(SuppressionComment).covers(filepath, startline, startcolumn, endline, endcolumn)
8171
}
8272
}
8373

cpp/ql/src/Architecture/Refactoring Opportunities/ClassesWithManyFields.ql

+12-12
Original file line numberDiff line numberDiff line change
@@ -46,40 +46,40 @@ predicate masterVde(VariableDeclarationEntry master, VariableDeclarationEntry vd
4646
exists(VariableDeclarationEntry previous | previousVde(previous, vde) and masterVde(master, previous))
4747
}
4848

49-
class VariableDeclarationGroup extends @var_decl {
49+
class VariableDeclarationGroup extends ElementBase {
5050
VariableDeclarationGroup() {
51-
not previousVde(_, mkElement(this))
51+
this instanceof VariableDeclarationEntry and
52+
not previousVde(_, this)
5253
}
5354
Class getClass() {
54-
vdeInfo(mkElement(this), result, _, _)
55+
vdeInfo(this, result, _, _)
5556
}
5657

5758
// pragma[noopt] since otherwise the two locationInfo relations get join-ordered
5859
// after each other
5960
pragma[noopt]
6061
predicate hasLocationInfo(string path, int startline, int startcol, int endline, int endcol) {
61-
exists(Element thisElement, VariableDeclarationEntry last, Location lstart, Location lend |
62-
thisElement = mkElement(this) and
63-
masterVde(thisElement, last) and
62+
exists(VariableDeclarationEntry last, Location lstart, Location lend |
63+
masterVde(this, last) and
6464
this instanceof VariableDeclarationGroup and
6565
not previousVde(last, _) and
66-
exists(VariableDeclarationEntry vde | vde=mkElement(this) and vde instanceof VariableDeclarationEntry and vde.getLocation() = lstart) and
66+
exists(VariableDeclarationEntry vde | vde=this and vde instanceof VariableDeclarationEntry and vde.getLocation() = lstart) and
6767
last.getLocation() = lend and
6868
lstart.hasLocationInfo(path, startline, startcol, _, _) and
6969
lend.hasLocationInfo(path, _, _, endline, endcol)
7070
)
7171
}
7272

73-
string toString() {
74-
if previousVde(mkElement(this), _) then
73+
string describeGroup() {
74+
if previousVde(this, _) then
7575
result = "group of "
7676
+ strictcount(string name
7777
| exists(VariableDeclarationEntry vde
78-
| masterVde(mkElement(this), vde) and
78+
| masterVde(this, vde) and
7979
name = vde.getName()))
8080
+ " fields here"
8181
else
82-
result = "declaration of " + mkElement(this).(VariableDeclarationEntry).getVariable().getName()
82+
result = "declaration of " + this.(VariableDeclarationEntry).getVariable().getName()
8383
}
8484
}
8585

@@ -111,4 +111,4 @@ where n = strictcount(string fieldName
111111
c = vdg.getClass() and
112112
if c.hasOneVariableGroup() then suffix = "" else suffix = " - see $@"
113113
select c, kindstr(c) + " " + c.getName() + " has " + n + " fields, which is too many" + suffix + ".",
114-
vdg, vdg.toString()
114+
vdg, vdg.describeGroup()

cpp/ql/src/CPython/Extensions.qll

-11
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ class PythonClass extends Variable, CObject {
7777
/* This needs to be kept in sync with extractor-python/semmle/passes/type.py */
7878
result = "C_type$" + this.getTpName()
7979
}
80-
81-
/** Gets a textual representation of this element. */
82-
override string toString() {
83-
result = Variable.super.toString()
84-
}
8580
}
8681

8782
/**
@@ -518,12 +513,6 @@ class PythonExtensionFunction extends Function {
518513
}
519514

520515
class TypedPythonExtensionProperty extends PythonGetSetTableEntry, CObject {
521-
522-
override
523-
string toString() {
524-
result = PythonGetSetTableEntry.super.toString()
525-
}
526-
527516
PythonClass getPropertyType() {
528517
result = py_return_type(this.getGetter())
529518
}

cpp/ql/src/Likely Bugs/Format/SnprintfOverflow.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ predicate flowsToDefImpl(
9898
or
9999
// `x++`
100100
exists (CrementOperation crem
101-
| mkElement(def) = crem and
101+
| def = crem and
102102
crem.getOperand() = v.getAnAccess() and
103103
flowsToExpr(source, crem.getOperand(), pathMightOverflow))
104104
or

cpp/ql/src/Security/CWE/CWE-764/LockFlow.qll

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ predicate tryLockCondition(VariableAccess access,
2727
(cond = call.getParent*() and
2828
cond.isCondition() and
2929
failNode = cond.getASuccessor() and
30-
unresolveElement(failNode) instanceof BasicBlockWithReturn))
30+
failNode instanceof BasicBlockWithReturn))
3131
}
3232

3333
/**

cpp/ql/src/Security/CWE/CWE-764/UnreleasedLock.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ predicate failedLock(MutexType t, BasicBlock lockblock, BasicBlock failblock) {
2929
exists (ControlFlowNode lock |
3030
lock = lockblock.getEnd() and
3131
lock = t.getLockAccess() and
32-
lock.getAFalseSuccessor() = mkElement(failblock)
32+
lock.getAFalseSuccessor() = failblock
3333
)
3434
}
3535

cpp/ql/src/semmle/code/cpp/Declaration.qll

+8-10
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ abstract class AccessHolder extends Declaration {
309309
isDirectPublicBaseOf*(base, derived)
310310
or
311311
exists(DirectAccessHolder n |
312-
this.getEnclosingAccessHolder*() = mkElement(n) and
312+
this.getEnclosingAccessHolder*() = n and
313313
// Derivations using (4.2) or (4.3) at least once.
314314
n.thisCanAccessClassTrans(base, derived)
315315
)
@@ -379,7 +379,7 @@ abstract class AccessHolder extends Declaration {
379379
everyoneCouldAccessMember(memberClass, memberAccess, derived)
380380
or
381381
exists(DirectAccessHolder n |
382-
this.getEnclosingAccessHolder*() = mkElement(n) and
382+
this.getEnclosingAccessHolder*() = n and
383383
// Any other derivation.
384384
n.thisCouldAccessMember(memberClass, memberAccess, derived)
385385
)
@@ -396,11 +396,11 @@ abstract class AccessHolder extends Declaration {
396396
* `DirectAccessHolder`s. If a `DirectAccessHolder` contains an `AccessHolder`,
397397
* then the contained `AccessHolder` inherits its access rights.
398398
*/
399-
private class DirectAccessHolder extends @declaration {
399+
private class DirectAccessHolder extends Element {
400400
DirectAccessHolder() {
401-
mkElement(this) instanceof Class
401+
this instanceof Class
402402
or
403-
exists(FriendDecl fd | fd.getFriend() = mkElement(this))
403+
exists(FriendDecl fd | fd.getFriend() = this)
404404
}
405405

406406
/**
@@ -486,7 +486,7 @@ private class DirectAccessHolder extends @declaration {
486486
)
487487
or
488488
// Rule (5.4) followed by Rule (5.2)
489-
exists(Class between | mkElement(this).(AccessHolder).canAccessClass(between, derived) |
489+
exists(Class between | this.(AccessHolder).canAccessClass(between, derived) |
490490
between.accessOfBaseMember(memberClass, memberAccess)
491491
.hasName("private") and
492492
this.isFriendOfOrEqualTo(between)
@@ -539,12 +539,10 @@ private class DirectAccessHolder extends @declaration {
539539
}
540540

541541
private predicate isFriendOfOrEqualTo(Class c) {
542-
exists(FriendDecl fd | fd.getDeclaringClass() = c | mkElement(this) = fd.getFriend())
542+
exists(FriendDecl fd | fd.getDeclaringClass() = c | this = fd.getFriend())
543543
or
544-
mkElement(this) = c
544+
this = c
545545
}
546-
547-
string toString() { result = mkElement(this).(Declaration).toString() }
548546
}
549547

550548
/**

cpp/ql/src/semmle/code/cpp/Element.qll

+13-4
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,26 @@ Element mkElement(@element e) {
4646
}
4747

4848
/**
49-
* A C/C++ element. This class is the base class for all C/C++
50-
* elements, such as functions, classes, expressions, and so on.
49+
* A C/C++ element with no member predicates other than `toString`. Not for
50+
* general use. This class does not define a location, so classes wanting to
51+
* change their location without affecting other classes can extend
52+
* `ElementBase` instead of `Element` to create a new rootdef for `getURL`,
53+
* `getLocation`, or `hasLocationInfo`.
5154
*/
52-
class Element extends @element {
53-
Element() {
55+
class ElementBase extends @element {
56+
ElementBase() {
5457
this = resolveElement(_)
5558
}
5659

5760
/** Gets a textual representation of this element. */
5861
string toString() { none() }
62+
}
5963

64+
/**
65+
* A C/C++ element. This class is the base class for all C/C++
66+
* elements, such as functions, classes, expressions, and so on.
67+
*/
68+
class Element extends ElementBase {
6069
/** Gets the primary file where this element occurs. */
6170
File getFile() { result = this.getLocation().getFile() }
6271

cpp/ql/src/semmle/code/cpp/controlflow/BasicBlocks.qll

+12-17
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private cached module Cached {
6060
private predicate non_primitive_basic_block_entry_node(ControlFlowNode node) {
6161
not primitive_basic_block_entry_node(node) and
6262
not exists(node.getAPredecessor()) and
63-
successors_extended(unresolveElement(node), _)
63+
successors_extended(node, _)
6464
}
6565

6666
/**
@@ -80,7 +80,7 @@ private cached module Cached {
8080
* reuse predicates already computed for `PrimitiveBasicBlocks`.
8181
*/
8282
private predicate equalsPrimitiveBasicBlock(BasicBlock bb) {
83-
primitive_basic_block_entry_node(mkElement(bb))
83+
primitive_basic_block_entry_node(bb)
8484
and
8585
not exists(int i |
8686
i > 0 and
@@ -96,11 +96,11 @@ private cached module Cached {
9696
}
9797

9898
private predicate non_primitive_basic_block_member(ControlFlowNode node, BasicBlock bb, int pos) {
99-
(not equalsPrimitiveBasicBlock(bb) and node = mkElement(bb) and pos = 0)
99+
(not equalsPrimitiveBasicBlock(bb) and node = bb and pos = 0)
100100
or
101-
(not (unresolveElement(node) instanceof BasicBlock) and
101+
(not (node instanceof BasicBlock) and
102102
exists (ControlFlowNode pred
103-
| successors_extended(unresolveElement(pred),unresolveElement(node))
103+
| successors_extended(pred, node)
104104
| non_primitive_basic_block_member(pred, bb, pos - 1)))
105105
}
106106

@@ -117,7 +117,7 @@ private cached module Cached {
117117
predicate bb_successor_cached(BasicBlock pred, BasicBlock succ) {
118118
exists(ControlFlowNode last |
119119
basic_block_member(last, pred, bb_length(pred)-1) and
120-
last.getASuccessor() = mkElement(succ)
120+
last.getASuccessor() = succ
121121
)
122122
}
123123
}
@@ -143,15 +143,10 @@ predicate bb_successor = bb_successor_cached/2;
143143
* A - B < C - D AB is a basic block and CD is a basic block (B has two outgoing edges)
144144
* ```
145145
*/
146-
class BasicBlock extends @cfgnode {
146+
class BasicBlock extends ControlFlowNodeBase {
147147

148148
BasicBlock() {
149-
basic_block_entry_node(mkElement(this))
150-
}
151-
152-
/** Gets a textual representation of this element. */
153-
string toString() {
154-
result = "BasicBlock"
149+
basic_block_entry_node(this)
155150
}
156151

157152
predicate contains(ControlFlowNode node) {
@@ -187,7 +182,7 @@ class BasicBlock extends @cfgnode {
187182
}
188183

189184
ControlFlowNode getStart() {
190-
result = mkElement(this)
185+
result = this
191186
}
192187

193188
/** Gets the number of `ControlFlowNode`s in this basic block. */
@@ -248,9 +243,9 @@ class BasicBlock extends @cfgnode {
248243
* point or a `catch` clause of a reachable `try` statement.
249244
*/
250245
predicate isReachable() {
251-
exists(Function f | f.getBlock() = mkElement(this))
246+
exists(Function f | f.getBlock() = this)
252247
or
253-
exists(TryStmt t, BasicBlock tryblock | mkElement(this) = t.getACatchClause() and tryblock.isReachable() and tryblock.contains(t))
248+
exists(TryStmt t, BasicBlock tryblock | this = t.getACatchClause() and tryblock.isReachable() and tryblock.contains(t))
254249
or
255250
exists(BasicBlock pred | pred.getASuccessor() = this and pred.isReachable())
256251
}
@@ -272,7 +267,7 @@ predicate unreachable(ControlFlowNode n) {
272267
*/
273268
class EntryBasicBlock extends BasicBlock {
274269
EntryBasicBlock() {
275-
exists (Function f | mkElement(this) = f.getEntryPoint())
270+
exists (Function f | this = f.getEntryPoint())
276271
}
277272
}
278273

0 commit comments

Comments
 (0)