Reformat PrintAST.qll ONLY.

This commit is contained in:
Ziemowit Laski
2019-07-10 18:42:08 -07:00
parent 49b0fc0a57
commit 2459d2ab92

View File

@@ -7,28 +7,26 @@ private newtype TPrintASTConfiguration = MkPrintASTConfiguration()
* The query can extend this class to control which functions are printed.
*/
class PrintASTConfiguration extends TPrintASTConfiguration {
string toString() {
result = "PrintASTConfiguration"
}
string toString() { result = "PrintASTConfiguration" }
/**
* Holds if the AST for `func` should be printed. By default, holds for all
* functions.
*/
predicate shouldPrintFunction(Function func) {
any()
}
predicate shouldPrintFunction(Function func) { any() }
}
private predicate shouldPrintFunction(Function func) {
exists(PrintASTConfiguration config |
config.shouldPrintFunction(func)
)
exists(PrintASTConfiguration config | config.shouldPrintFunction(func))
}
bindingset[s]
private string escapeString(string s) {
result = s.replaceAll("\\", "\\\\").replaceAll("\n", "\\n").replaceAll("\r", "\\r").replaceAll("\t", "\\t")
result = s
.replaceAll("\\", "\\\\")
.replaceAll("\n", "\\n")
.replaceAll("\r", "\\r")
.replaceAll("\t", "\\t")
}
/**
@@ -44,16 +42,16 @@ private Location getRepresentativeLocation(Locatable ast) {
* Computes the sort keys to sort the given AST node by location. An AST without
* a location gets an empty file name and a zero line and column number.
*/
private predicate locationSortKeys(Locatable ast, string file, int line,
int column) {
if exists(getRepresentativeLocation(ast)) then (
private predicate locationSortKeys(Locatable ast, string file, int line, int column) {
if exists(getRepresentativeLocation(ast))
then
exists(Location loc |
loc = getRepresentativeLocation(ast) and
file = loc.getFile().toString() and
line = loc.getStartLine() and
column = loc.getStartColumn()
)
) else (
else (
file = "" and
line = 0 and
column = 0
@@ -61,14 +59,19 @@ private predicate locationSortKeys(Locatable ast, string file, int line,
}
private Function getEnclosingFunction(Locatable ast) {
result = ast.(Expr).getEnclosingFunction() or
result = ast.(Stmt).getEnclosingFunction() or
result = ast.(Initializer).getExpr().getEnclosingFunction() or
result = ast.(Parameter).getFunction() or
result = ast.(Expr).getEnclosingFunction()
or
result = ast.(Stmt).getEnclosingFunction()
or
result = ast.(Initializer).getExpr().getEnclosingFunction()
or
result = ast.(Parameter).getFunction()
or
exists(DeclStmt stmt |
stmt.getADeclarationEntry() = ast and
result = stmt.getEnclosingFunction()
) or
)
or
result = ast
}
@@ -77,12 +80,8 @@ private Function getEnclosingFunction(Locatable ast) {
* nodes for things like parameter lists and constructor init lists.
*/
private newtype TPrintASTNode =
TASTNode(Locatable ast) {
shouldPrintFunction(getEnclosingFunction(ast))
} or
TParametersNode(Function func) {
shouldPrintFunction(func)
} or
TASTNode(Locatable ast) { shouldPrintFunction(getEnclosingFunction(ast)) } or
TParametersNode(Function func) { shouldPrintFunction(func) } or
TConstructorInitializersNode(Constructor ctor) {
ctor.hasEntryPoint() and
shouldPrintFunction(ctor)
@@ -109,23 +108,17 @@ class PrintASTNode extends TPrintASTNode {
* within a function are printed, but the query can override
* `PrintASTConfiguration.shouldPrintFunction` to filter the output.
*/
final predicate shouldPrint() {
shouldPrintFunction(getEnclosingFunction())
}
final predicate shouldPrint() { shouldPrintFunction(getEnclosingFunction()) }
/**
* Gets the children of this node.
*/
final PrintASTNode getAChild() {
result = getChild(_)
}
final PrintASTNode getAChild() { result = getChild(_) }
/**
* Gets the parent of this node, if any.
*/
final PrintASTNode getParent() {
result.getAChild() = this
}
final PrintASTNode getParent() { result.getAChild() = this }
/**
* Gets the location of this node in the source code.
@@ -154,43 +147,32 @@ class PrintASTNode extends TPrintASTNode {
/**
* Gets the `Function` that contains this node.
*/
private Function getEnclosingFunction() {
result = getParent*().(FunctionNode).getFunction()
}
private Function getEnclosingFunction() { result = getParent*().(FunctionNode).getFunction() }
}
/**
* Retrieves the caonical QL class for entity `el`
* Retrieves the canonical QL class for entity `el`
*/
private string qlClass(ElementBase el) { result = "["+ el.getCanonicalQLClass() + "]: " }
private string qlClass(ElementBase el) { result = "[" + el.getCanonicalQLClass() + "]: " }
// Do not delete this - it is useful for QL class discovery
//private string qlClass(ElementBase el) { result = "["+ concat(el.getAQlClass(), ",") + "]: " }
/**
* A node representing an AST node.
*/
abstract class ASTNode extends PrintASTNode, TASTNode {
Locatable ast;
ASTNode() {
this = TASTNode(ast)
}
ASTNode() { this = TASTNode(ast) }
override string toString() {
result = qlClass(ast) + ast.toString()
}
override string toString() { result = qlClass(ast) + ast.toString() }
override final Location getLocation() {
result = getRepresentativeLocation(ast)
}
final override Location getLocation() { result = getRepresentativeLocation(ast) }
/**
* Gets the AST represented by this node.
*/
final Locatable getAST() {
result = ast
}
final Locatable getAST() { result = ast }
}
/**
@@ -199,50 +181,37 @@ abstract class ASTNode extends PrintASTNode, TASTNode {
class ExprNode extends ASTNode {
Expr expr;
ExprNode() {
expr = ast
}
ExprNode() { expr = ast }
override ASTNode getChild(int childIndex) {
result.getAST() = expr.getChild(childIndex).getFullyConverted()
}
override string getProperty(string key) {
result = super.getProperty(key) or
(
key = "Value" and
result = qlClass(expr) + getValue()
) or
(
key = "Type" and
result = qlClass(expr.getType()) + expr.getType().toString()
) or
(
key = "ValueCategory" and
result = expr.getValueCategoryString()
)
result = super.getProperty(key)
or
key = "Value" and
result = qlClass(expr) + getValue()
or
key = "Type" and
result = qlClass(expr.getType()) + expr.getType().toString()
or
key = "ValueCategory" and
result = expr.getValueCategoryString()
}
string getValue() {
result = expr.getValue()
}
string getValue() { result = expr.getValue() }
}
/**
* A node representing a `StringLiteral`.
*/
class StringLiteralNode extends ExprNode {
StringLiteralNode() {
expr instanceof StringLiteral
}
StringLiteralNode() { expr instanceof StringLiteral }
override string toString() {
result = escapeString(expr.getValue())
}
override string toString() { result = escapeString(expr.getValue()) }
override string getValue() {
result = "\"" + escapeString(expr.getValue()) + "\""
}
override string getValue() { result = "\"" + escapeString(expr.getValue()) + "\"" }
}
/**
@@ -251,18 +220,14 @@ class StringLiteralNode extends ExprNode {
class ConversionNode extends ExprNode {
Conversion conv;
ConversionNode() {
conv = expr
}
ConversionNode() { conv = expr }
override ASTNode getChild(int childIndex) {
childIndex = 0 and
result.getAST() = conv.getExpr()
}
override string getChildEdgeLabel(int childIndex) {
childIndex = 0 and result = "expr"
}
override string getChildEdgeLabel(int childIndex) { childIndex = 0 and result = "expr" }
}
/**
@@ -271,16 +236,13 @@ class ConversionNode extends ExprNode {
class CastNode extends ConversionNode {
Cast cast;
CastNode() {
cast = conv
}
CastNode() { cast = conv }
override string getProperty(string key) {
result = super.getProperty(key) or
(
key = "Conversion" and
result = cast.getSemanticConversionString()
)
result = super.getProperty(key)
or
key = "Conversion" and
result = cast.getSemanticConversionString()
}
}
@@ -290,20 +252,15 @@ class CastNode extends ConversionNode {
class DeclarationEntryNode extends ASTNode {
DeclarationEntry entry;
DeclarationEntryNode() {
entry = ast
}
DeclarationEntryNode() { entry = ast }
override PrintASTNode getChild(int childIndex) {
none()
}
override PrintASTNode getChild(int childIndex) { none() }
override string getProperty(string key) {
result = super.getProperty(key) or
(
key = "Type" and
result = qlClass(entry.getType()) + entry.getType().toString()
)
result = super.getProperty(key)
or
key = "Type" and
result = qlClass(entry.getType()) + entry.getType().toString()
}
}
@@ -313,18 +270,14 @@ class DeclarationEntryNode extends ASTNode {
class VariableDeclarationEntryNode extends DeclarationEntryNode {
VariableDeclarationEntry varEntry;
VariableDeclarationEntryNode() {
varEntry = entry
}
VariableDeclarationEntryNode() { varEntry = entry }
override ASTNode getChild(int childIndex) {
childIndex = 0 and
result.getAST() = varEntry.getVariable().getInitializer()
}
override string getChildEdgeLabel(int childIndex) {
childIndex = 0 and result = "init"
}
override string getChildEdgeLabel(int childIndex) { childIndex = 0 and result = "init" }
}
/**
@@ -333,9 +286,7 @@ class VariableDeclarationEntryNode extends DeclarationEntryNode {
class StmtNode extends ASTNode {
Stmt stmt;
StmtNode() {
stmt = ast
}
StmtNode() { stmt = ast }
override ASTNode getChild(int childIndex) {
exists(Locatable child |
@@ -354,9 +305,7 @@ class StmtNode extends ASTNode {
class DeclStmtNode extends StmtNode {
DeclStmt declStmt;
DeclStmtNode() {
declStmt = stmt
}
DeclStmtNode() { declStmt = stmt }
override ASTNode getChild(int childIndex) {
result.getAST() = declStmt.getDeclarationEntry(childIndex)
@@ -369,20 +318,15 @@ class DeclStmtNode extends StmtNode {
class ParameterNode extends ASTNode {
Parameter param;
ParameterNode() {
param = ast
}
ParameterNode() { param = ast }
override final PrintASTNode getChild(int childIndex) {
none()
}
final override PrintASTNode getChild(int childIndex) { none() }
override final string getProperty(string key) {
result = super.getProperty(key) or
(
key = "Type" and
result = qlClass(param.getType()) + param.getType().toString()
)
final override string getProperty(string key) {
result = super.getProperty(key)
or
key = "Type" and
result = qlClass(param.getType()) + param.getType().toString()
}
}
@@ -392,9 +336,7 @@ class ParameterNode extends ASTNode {
class InitializerNode extends ASTNode {
Initializer init;
InitializerNode() {
init = ast
}
InitializerNode() { init = ast }
override ASTNode getChild(int childIndex) {
childIndex = 0 and
@@ -413,25 +355,15 @@ class InitializerNode extends ASTNode {
class ParametersNode extends PrintASTNode, TParametersNode {
Function func;
ParametersNode() {
this = TParametersNode(func)
}
ParametersNode() { this = TParametersNode(func) }
override final string toString() {
result = ""
}
final override string toString() { result = "" }
override final Location getLocation() {
result = getRepresentativeLocation(func)
}
final override Location getLocation() { result = getRepresentativeLocation(func) }
override ASTNode getChild(int childIndex) {
result.getAST() = func.getParameter(childIndex)
}
override ASTNode getChild(int childIndex) { result.getAST() = func.getParameter(childIndex) }
final Function getFunction() {
result = func
}
final Function getFunction() { result = func }
}
/**
@@ -440,25 +372,17 @@ class ParametersNode extends PrintASTNode, TParametersNode {
class ConstructorInitializersNode extends PrintASTNode, TConstructorInitializersNode {
Constructor ctor;
ConstructorInitializersNode() {
this = TConstructorInitializersNode(ctor)
}
ConstructorInitializersNode() { this = TConstructorInitializersNode(ctor) }
override final string toString() {
result = ""
}
final override string toString() { result = "" }
override final Location getLocation() {
result = getRepresentativeLocation(ctor)
}
final override Location getLocation() { result = getRepresentativeLocation(ctor) }
override final ASTNode getChild(int childIndex) {
final override ASTNode getChild(int childIndex) {
result.getAST() = ctor.getInitializer(childIndex)
}
final Constructor getConstructor() {
result = ctor
}
final Constructor getConstructor() { result = ctor }
}
/**
@@ -467,25 +391,17 @@ class ConstructorInitializersNode extends PrintASTNode, TConstructorInitializers
class DestructorDestructionsNode extends PrintASTNode, TDestructorDestructionsNode {
Destructor dtor;
DestructorDestructionsNode() {
this = TDestructorDestructionsNode(dtor)
}
DestructorDestructionsNode() { this = TDestructorDestructionsNode(dtor) }
override final string toString() {
result = ""
}
final override string toString() { result = "" }
override final Location getLocation() {
result = getRepresentativeLocation(dtor)
}
final override Location getLocation() { result = getRepresentativeLocation(dtor) }
override final ASTNode getChild(int childIndex) {
final override ASTNode getChild(int childIndex) {
result.getAST() = dtor.getDestruction(childIndex)
}
final Destructor getDestructor() {
result = dtor
}
final Destructor getDestructor() { result = dtor }
}
/**
@@ -494,61 +410,50 @@ class DestructorDestructionsNode extends PrintASTNode, TDestructorDestructionsNo
class FunctionNode extends ASTNode {
Function func;
FunctionNode() {
func = ast
}
FunctionNode() { func = ast }
override string toString() {
result = qlClass(func) + getIdentityString(func)
}
override string toString() { result = qlClass(func) + getIdentityString(func) }
override PrintASTNode getChild(int childIndex) {
(
childIndex = 0 and
result.(ParametersNode).getFunction() = func
) or
(
childIndex = 1 and
result.(ConstructorInitializersNode).getConstructor() = func
) or
(
childIndex = 2 and
result.(ASTNode).getAST() = func.getEntryPoint()
) or
(
childIndex = 3 and
result.(DestructorDestructionsNode).getDestructor() = func
)
childIndex = 0 and
result.(ParametersNode).getFunction() = func
or
childIndex = 1 and
result.(ConstructorInitializersNode).getConstructor() = func
or
childIndex = 2 and
result.(ASTNode).getAST() = func.getEntryPoint()
or
childIndex = 3 and
result.(DestructorDestructionsNode).getDestructor() = func
}
override string getChildEdgeLabel(int childIndex) {
childIndex = 0 and result = "params" or
childIndex = 1 and result = "initializations" or
childIndex = 2 and result = "body" or
childIndex = 0 and result = "params"
or
childIndex = 1 and result = "initializations"
or
childIndex = 2 and result = "body"
or
childIndex = 3 and result = "destructions"
}
private int getOrder() {
this = rank[result](FunctionNode node, Function function, string file,
int line, int column |
node.getAST() = function and
locationSortKeys(function, file, line, column) |
node order by
file,
line,
column,
getIdentityString(function)
)
this = rank[result](FunctionNode node, Function function, string file, int line, int column |
node.getAST() = function and
locationSortKeys(function, file, line, column)
|
node order by file, line, column, getIdentityString(function)
)
}
override string getProperty(string key) {
result = super.getProperty(key) or
result = super.getProperty(key)
or
key = "semmle.order" and result = getOrder().toString()
}
final Function getFunction() {
result = func
}
final Function getFunction() { result = func }
}
/**
@@ -557,9 +462,7 @@ class FunctionNode extends ASTNode {
class ClassAggregateLiteralNode extends ExprNode {
ClassAggregateLiteral list;
ClassAggregateLiteralNode() {
list = ast
}
ClassAggregateLiteralNode() { list = ast }
override string getChildEdgeLabel(int childIndex) {
exists(Field field |
@@ -575,9 +478,7 @@ class ClassAggregateLiteralNode extends ExprNode {
class ArrayAggregateLiteralNode extends ExprNode {
ArrayAggregateLiteral list;
ArrayAggregateLiteralNode() {
list = ast
}
ArrayAggregateLiteralNode() { list = ast }
override string getChildEdgeLabel(int childIndex) {
exists(int elementIndex |
@@ -598,7 +499,8 @@ query predicate edges(PrintASTNode source, PrintASTNode target, string key, stri
target.shouldPrint() and
target = source.getChild(childIndex) and
(
key = "semmle.label" and value = source.getChildEdgeLabel(childIndex) or
key = "semmle.label" and value = source.getChildEdgeLabel(childIndex)
or
key = "semmle.order" and value = childIndex.toString()
)
)