Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ddb0fd9

Browse files
committed
[CPP-386] Provide getCanonicalQLClass() predicate for many AST elements.
1 parent a4affbe commit ddb0fd9

21 files changed

Lines changed: 304 additions & 7 deletions

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ElementBase extends @element {
5353
/** Gets a textual representation of this element. */
5454
string toString() { none() }
5555
/** Retrieves canonical QL class(es) corresponding to this element. */
56-
string getCanonicalQLClass() { none() }
56+
string getCanonicalQLClass() { result = "ElementBase" }
5757
}
5858

5959
/**
@@ -64,6 +64,9 @@ class Element extends ElementBase {
6464
/** Gets the primary file where this element occurs. */
6565
File getFile() { result = this.getLocation().getFile() }
6666

67+
/** Retrieves canonical QL class(es) corresponding to this element. */
68+
string getCanonicalQLClass() { result = "Element" }
69+
6770
/**
6871
* Holds if this element may be from source.
6972
*

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,8 @@ class TopLevelFunction extends Function {
707707
TopLevelFunction() {
708708
not this.isMember()
709709
}
710+
/** Retrieves canonical QL class(es) corresponding to this element. */
711+
string getCanonicalQLClass() { result = "TopLevelFunction" }
710712
}
711713

712714
/**
@@ -718,6 +720,9 @@ class MemberFunction extends Function {
718720
this.isMember()
719721
}
720722

723+
/** Retrieves canonical QL class(es) corresponding to this element. */
724+
string getCanonicalQLClass() { result = "MemberFunction" }
725+
721726
/**
722727
* Gets the number of parameters of this function, including any implicit
723728
* `this` parameter.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import semmle.code.cpp.controlflow.ControlFlowGraph
66
class Initializer extends ControlFlowNode, @initialiser {
77
override Location getLocation() { initialisers(underlyingElement(this),_,_,result) }
88

9+
/** Retrieves canonical QL class(es) corresponding to this element. */
10+
string getCanonicalQLClass() { result = "Initializer" }
11+
912
/** Holds if this initializer is explicit in the source. */
1013
override predicate fromSource() {
1114
not (this.getLocation() instanceof UnknownLocation)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class Parameter extends LocalScopeVariable, @parameter {
3333
result = "p#" + this.getIndex().toString())
3434
}
3535

36+
/** Retrieves canonical QL class(es) corresponding to this element. */
37+
string getCanonicalQLClass() { result = "Parameter" }
38+
3639
/**
3740
* Gets the name of this parameter, including it's type.
3841
*

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ class PrintASTNode extends TPrintASTNode {
159159
}
160160
}
161161

162+
/**
163+
* A concatenation of all the leaf QL types of `el`
164+
*/
165+
private string qlClass(ElementBase el) { result = "["+ el.getCanonicalQLClass() + "]: " }
166+
162167
/**
163168
* A node representing an AST node.
164169
*/
@@ -170,7 +175,7 @@ abstract class ASTNode extends PrintASTNode, TASTNode {
170175
}
171176

172177
override string toString() {
173-
result = ast.toString()
178+
result = qlClass(ast) + ast.toString()
174179
}
175180

176181
override final Location getLocation() {
@@ -203,11 +208,11 @@ class ExprNode extends ASTNode {
203208
result = super.getProperty(key) or
204209
(
205210
key = "Value" and
206-
result = getValue()
211+
result = qlClass(expr) + getValue()
207212
) or
208213
(
209214
key = "Type" and
210-
result = expr.getType().toString()
215+
result = qlClass(expr.getType()) + expr.getType().toString()
211216
) or
212217
(
213218
key = "ValueCategory" and
@@ -294,7 +299,7 @@ class DeclarationEntryNode extends ASTNode {
294299
result = super.getProperty(key) or
295300
(
296301
key = "Type" and
297-
result = entry.getType().toString()
302+
result = qlClass(entry.getType()) + entry.getType().toString()
298303
)
299304
}
300305
}
@@ -373,7 +378,7 @@ class ParameterNode extends ASTNode {
373378
result = super.getProperty(key) or
374379
(
375380
key = "Type" and
376-
result = param.getType().toString()
381+
result = qlClass(param.getType()) + param.getType().toString()
377382
)
378383
}
379384
}
@@ -491,7 +496,7 @@ class FunctionNode extends ASTNode {
491496
}
492497

493498
override string toString() {
494-
result = getIdentityString(func)
499+
result = qlClass(func) + getIdentityString(func)
495500
}
496501

497502
override PrintASTNode getChild(int childIndex) {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,8 @@ class BoolType extends IntegralType {
485485

486486
BoolType() { builtintypes(underlyingElement(this),_,4,_,_,_) }
487487

488+
/** Retrieves canonical QL class(es) corresponding to this element. */
489+
string getCanonicalQLClass() { result = "BoolType" }
488490
}
489491

490492
/**
@@ -499,6 +501,8 @@ class PlainCharType extends CharType {
499501
PlainCharType() {
500502
builtintypes(underlyingElement(this),_,5,_,_,_)
501503
}
504+
/** Retrieves canonical QL class(es) corresponding to this element. */
505+
string getCanonicalQLClass() { result = "PlainCharType" }
502506
}
503507

504508
/**
@@ -508,6 +512,8 @@ class UnsignedCharType extends CharType {
508512
UnsignedCharType() {
509513
builtintypes(underlyingElement(this),_,6,_,_,_)
510514
}
515+
/** Retrieves canonical QL class(es) corresponding to this element. */
516+
string getCanonicalQLClass() { result = "UnsignedCharType" }
511517
}
512518

513519
/**
@@ -517,6 +523,8 @@ class SignedCharType extends CharType {
517523
SignedCharType() {
518524
builtintypes(underlyingElement(this),_,7,_,_,_)
519525
}
526+
/** Retrieves canonical QL class(es) corresponding to this element. */
527+
string getCanonicalQLClass() { result = "SignedCharType" }
520528
}
521529

522530
/**
@@ -528,6 +536,8 @@ class ShortType extends IntegralType {
528536
builtintypes(underlyingElement(this),_,8,_,_,_) or builtintypes(underlyingElement(this),_,9,_,_,_) or builtintypes(underlyingElement(this),_,10,_,_,_)
529537
}
530538

539+
/** Retrieves canonical QL class(es) corresponding to this element. */
540+
string getCanonicalQLClass() { result = "ShortType" }
531541
}
532542

533543
/**
@@ -539,6 +549,8 @@ class IntType extends IntegralType {
539549
builtintypes(underlyingElement(this),_,11,_,_,_) or builtintypes(underlyingElement(this),_,12,_,_,_) or builtintypes(underlyingElement(this),_,13,_,_,_)
540550
}
541551

552+
/** Retrieves canonical QL class(es) corresponding to this element. */
553+
string getCanonicalQLClass() { result = "IntType" }
542554
}
543555

544556
/**
@@ -550,6 +562,8 @@ class LongType extends IntegralType {
550562
builtintypes(underlyingElement(this),_,14,_,_,_) or builtintypes(underlyingElement(this),_,15,_,_,_) or builtintypes(underlyingElement(this),_,16,_,_,_)
551563
}
552564

565+
/** Retrieves canonical QL class(es) corresponding to this element. */
566+
string getCanonicalQLClass() { result = "LongType" }
553567
}
554568

555569
/**
@@ -561,6 +575,8 @@ class LongLongType extends IntegralType {
561575
builtintypes(underlyingElement(this),_,17,_,_,_) or builtintypes(underlyingElement(this),_,18,_,_,_) or builtintypes(underlyingElement(this),_,19,_,_,_)
562576
}
563577

578+
/** Retrieves canonical QL class(es) corresponding to this element. */
579+
string getCanonicalQLClass() { result = "LongLongType" }
564580
}
565581

566582
/**
@@ -592,6 +608,8 @@ class FloatType extends FloatingPointType {
592608

593609
FloatType() { builtintypes(underlyingElement(this),_,24,_,_,_) }
594610

611+
/** Retrieves canonical QL class(es) corresponding to this element. */
612+
string getCanonicalQLClass() { result = "FloatType" }
595613
}
596614

597615
/**
@@ -601,6 +619,8 @@ class DoubleType extends FloatingPointType {
601619

602620
DoubleType() { builtintypes(underlyingElement(this),_,25,_,_,_) }
603621

622+
/** Retrieves canonical QL class(es) corresponding to this element. */
623+
string getCanonicalQLClass() { result = "DoubleType" }
604624
}
605625

606626
/**
@@ -610,6 +630,8 @@ class LongDoubleType extends FloatingPointType {
610630

611631
LongDoubleType() { builtintypes(underlyingElement(this),_,26,_,_,_) }
612632

633+
/** Retrieves canonical QL class(es) corresponding to this element. */
634+
string getCanonicalQLClass() { result = "LongDoubleType" }
613635
}
614636

615637
/**
@@ -655,6 +677,8 @@ class VoidType extends BuiltInType {
655677

656678
VoidType() { builtintypes(underlyingElement(this),_,3,_,_,_) }
657679

680+
/** Retrieves canonical QL class(es) corresponding to this element. */
681+
string getCanonicalQLClass() { result = "VoidType" }
658682
}
659683

660684
/**
@@ -967,6 +991,9 @@ class ArrayType extends DerivedType {
967991

968992
ArrayType() { derivedtypes(underlyingElement(this),_,4,_) }
969993

994+
/** Retrieves canonical QL class(es) corresponding to this element. */
995+
string getCanonicalQLClass() { result = "ArrayType" }
996+
970997
predicate hasArraySize() { arraysizes(underlyingElement(this),_,_,_) }
971998

972999
/**

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ class Variable extends Declaration, @variable {
174174
class VariableDeclarationEntry extends DeclarationEntry, @var_decl {
175175
override Variable getDeclaration() { result = getVariable() }
176176

177+
/** Retrieves canonical QL class(es) corresponding to this element. */
178+
string getCanonicalQLClass() { result = "VariableDeclarationEntry" }
179+
177180
/**
178181
* Gets the variable which is being declared or defined.
179182
*/

cpp/ql/src/semmle/code/cpp/commons/CommonType.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class CharPointerType extends PointerType {
77

88
CharPointerType() { this.getBaseType() instanceof CharType }
99

10+
/** Retrieves canonical QL class(es) corresponding to this element. */
11+
string getCanonicalQLClass() { result = "CharPointerType" }
1012
}
1113

1214
/**
@@ -16,6 +18,8 @@ class IntPointerType extends PointerType {
1618

1719
IntPointerType() { this.getBaseType() instanceof IntType }
1820

21+
/** Retrieves canonical QL class(es) corresponding to this element. */
22+
string getCanonicalQLClass() { result = "IntPointerType" }
1923
}
2024

2125

@@ -26,6 +30,8 @@ class VoidPointerType extends PointerType {
2630

2731
VoidPointerType() { this.getBaseType() instanceof VoidType }
2832

33+
/** Retrieves canonical QL class(es) corresponding to this element. */
34+
string getCanonicalQLClass() { result = "VoidPointerType" }
2935
}
3036

3137
/**
@@ -36,6 +42,8 @@ class Size_t extends Type {
3642
this.getUnderlyingType() instanceof IntegralType and
3743
this.hasName("size_t")
3844
}
45+
/** Retrieves canonical QL class(es) corresponding to this element. */
46+
string getCanonicalQLClass() { result = "Size_t" }
3947
}
4048

4149
/**
@@ -46,6 +54,8 @@ class Ssize_t extends Type {
4654
this.getUnderlyingType() instanceof IntegralType and
4755
this.hasName("ssize_t")
4856
}
57+
/** Retrieves canonical QL class(es) corresponding to this element. */
58+
string getCanonicalQLClass() { result = "Ssize_t" }
4959
}
5060

5161
/**
@@ -56,6 +66,8 @@ class Ptrdiff_t extends Type {
5666
this.getUnderlyingType() instanceof IntegralType and
5767
this.hasName("ptrdiff_t")
5868
}
69+
/** Retrieves canonical QL class(es) corresponding to this element. */
70+
string getCanonicalQLClass() { result = "Ptrdiff_t" }
5971
}
6072

6173
/**
@@ -90,6 +102,8 @@ class Wchar_t extends Type {
90102
this.getUnderlyingType() instanceof IntegralType and
91103
this.hasName("wchar_t")
92104
}
105+
/** Retrieves canonical QL class(es) corresponding to this element. */
106+
string getCanonicalQLClass() { result = "Wchar_t" }
93107
}
94108

95109
/**

cpp/ql/src/semmle/code/cpp/commons/Printf.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ class UserDefinedFormattingFunction extends FormattingFunction {
7575
class FormattingFunctionCall extends Expr {
7676
FormattingFunctionCall() { this.(Call).getTarget() instanceof FormattingFunction }
7777

78+
/** Retrieves canonical QL class(es) corresponding to this element. */
79+
string getCanonicalQLClass() { result = "FormattingFunctionCall" }
80+
7881
/**
7982
* Gets the formatting function being called.
8083
*/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ abstract class NullValue extends Expr
1414
class Zero extends NullValue
1515
{
1616
Zero() { this.(Literal).getValue() = "0" }
17+
/** Retrieves canonical QL class(es) corresponding to this element. */
18+
string getCanonicalQLClass() { result = "Zero" }
19+
1720
}
1821

1922
/**

0 commit comments

Comments
 (0)