Skip to content

Commit f792998

Browse files
authored
Merge pull request github#3831 from MathiasVP/remove-abstract-decl-var-call
C++: Remove abstractness from DeclarationEntry, AccessHolder and Call
2 parents 13cb853 + acee9eb commit f792998

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class Declaration extends Locatable, @declaration {
124124
* To test whether this declaration has a particular name in the global
125125
* namespace, use `hasGlobalName`.
126126
*/
127-
abstract string getName();
127+
string getName() { none() } // overridden in subclasses
128128

129129
/** Holds if this declaration has the given name. */
130130
predicate hasName(string name) { name = this.getName() }
@@ -140,7 +140,7 @@ class Declaration extends Locatable, @declaration {
140140
}
141141

142142
/** Gets a specifier of this declaration. */
143-
abstract Specifier getASpecifier();
143+
Specifier getASpecifier() { none() } // overridden in subclasses
144144

145145
/** Holds if this declaration has a specifier with the given name. */
146146
predicate hasSpecifier(string name) { this.getASpecifier().hasName(name) }
@@ -156,7 +156,7 @@ class Declaration extends Locatable, @declaration {
156156
* Gets the location of a declaration entry corresponding to this
157157
* declaration.
158158
*/
159-
abstract Location getADeclarationLocation();
159+
Location getADeclarationLocation() { none() } // overridden in subclasses
160160

161161
/**
162162
* Gets the declaration entry corresponding to this declaration that is a
@@ -165,7 +165,7 @@ class Declaration extends Locatable, @declaration {
165165
DeclarationEntry getDefinition() { none() }
166166

167167
/** Gets the location of the definition, if any. */
168-
abstract Location getDefinitionLocation();
168+
Location getDefinitionLocation() { none() } // overridden in subclasses
169169

170170
/** Holds if the declaration has a definition. */
171171
predicate hasDefinition() { exists(this.getDefinition()) }
@@ -289,6 +289,8 @@ class Declaration extends Locatable, @declaration {
289289
}
290290
}
291291

292+
private class TDeclarationEntry = @var_decl or @type_decl or @fun_decl;
293+
292294
/**
293295
* A C/C++ declaration entry. For example the following code contains five
294296
* declaration entries:
@@ -304,9 +306,9 @@ class Declaration extends Locatable, @declaration {
304306
* See the comment above `Declaration` for an explanation of the relationship
305307
* between `Declaration` and `DeclarationEntry`.
306308
*/
307-
abstract class DeclarationEntry extends Locatable {
309+
class DeclarationEntry extends Locatable, TDeclarationEntry {
308310
/** Gets a specifier associated with this declaration entry. */
309-
abstract string getASpecifier();
311+
string getASpecifier() { none() } // overridden in subclasses
310312

311313
/**
312314
* Gets the name associated with the corresponding definition (where
@@ -329,10 +331,10 @@ abstract class DeclarationEntry extends Locatable {
329331
* `I.getADeclarationEntry()` returns `D`
330332
* but `D.getDeclaration()` only returns `C`
331333
*/
332-
abstract Declaration getDeclaration();
334+
Declaration getDeclaration() { none() } // overridden in subclasses
333335

334336
/** Gets the name associated with this declaration entry, if any. */
335-
abstract string getName();
337+
string getName() { none() } // overridden in subclasses
336338

337339
/**
338340
* Gets the type associated with this declaration entry.
@@ -341,7 +343,7 @@ abstract class DeclarationEntry extends Locatable {
341343
* For function declarations, get the return type of the function.
342344
* For type declarations, get the type being declared.
343345
*/
344-
abstract Type getType();
346+
Type getType() { none() } // overridden in subclasses
345347

346348
/**
347349
* Gets the type associated with this declaration entry after specifiers
@@ -359,7 +361,7 @@ abstract class DeclarationEntry extends Locatable {
359361
predicate hasSpecifier(string specifier) { getASpecifier() = specifier }
360362

361363
/** Holds if this declaration entry is a definition. */
362-
abstract predicate isDefinition();
364+
predicate isDefinition() { none() } // overridden in subclasses
363365

364366
override string toString() {
365367
if isDefinition()
@@ -371,6 +373,8 @@ abstract class DeclarationEntry extends Locatable {
371373
}
372374
}
373375

376+
private class TAccessHolder = @function or @usertype;
377+
374378
/**
375379
* A declaration that can potentially have more C++ access rights than its
376380
* enclosing element. This comprises `Class` (they have access to their own
@@ -392,7 +396,7 @@ abstract class DeclarationEntry extends Locatable {
392396
* the informal phrase "_R_ occurs in a member or friend of class C", where
393397
* `AccessHolder` corresponds to this _R_.
394398
*/
395-
abstract class AccessHolder extends Declaration {
399+
class AccessHolder extends Declaration, TAccessHolder {
396400
/**
397401
* Holds if `this` can access private members of class `c`.
398402
*
@@ -410,7 +414,7 @@ abstract class AccessHolder extends Declaration {
410414
/**
411415
* Gets the nearest enclosing `AccessHolder`.
412416
*/
413-
abstract AccessHolder getEnclosingAccessHolder();
417+
AccessHolder getEnclosingAccessHolder() { none() } // overridden in subclasses
414418

415419
/**
416420
* Holds if a base class `base` of `derived` _is accessible at_ `this` (N4140

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ class ParameterDeclarationEntry extends VariableDeclarationEntry {
325325
*/
326326
class LocalScopeVariable extends Variable, @localscopevariable {
327327
/** Gets the function to which this variable belongs. */
328-
/*abstract*/ Function getFunction() { none() }
328+
Function getFunction() { none() } // overridden in subclasses
329329
}
330330

331331
/**

cpp/ql/src/semmle/code/cpp/exprs/Call.qll

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@ import semmle.code.cpp.exprs.Expr
22
import semmle.code.cpp.Function
33
private import semmle.code.cpp.dataflow.EscapesTree
44

5+
private class TCall = @funbindexpr or @callexpr;
6+
57
/**
68
* A C/C++ call.
7-
*
8-
* This is the abstract root QL class for all types of calls.
99
*/
10-
abstract class Call extends Expr, NameQualifiableElement {
10+
class Call extends Expr, NameQualifiableElement, TCall {
11+
// `@funbindexpr` (which is the dbscheme type for FunctionCall) is a union type that includes
12+
// `@routineexpr. This dbscheme type includes accesses to functions that are not necessarily calls to
13+
// that function. That's why the charpred for `FunctionCall` requires:
14+
// ```
15+
// iscall(underlyingElement(this), _)
16+
// ```
17+
// So for the charpred for `Call` we include the requirement that if this is an instance of
18+
// `@funbindexpr` it must be a _call_ to the function.
19+
Call() { this instanceof @callexpr or iscall(underlyingElement(this), _) }
20+
1121
/**
1222
* Gets the number of arguments (actual parameters) of this call. The count
1323
* does _not_ include the qualifier of the call, if any.
@@ -74,7 +84,7 @@ abstract class Call extends Expr, NameQualifiableElement {
7484
* method, and it might not exist.
7585
* - For a variable call, it never exists.
7686
*/
77-
abstract Function getTarget();
87+
Function getTarget() { none() } // overridden in subclasses
7888

7989
override int getPrecedence() { result = 17 }
8090

0 commit comments

Comments
 (0)