C++: Merge the location tables

This commit is contained in:
Jeroen Ketema
2024-09-25 13:40:27 +02:00
parent b16e710d3b
commit b4caba7c0e
26 changed files with 66 additions and 103 deletions

View File

@@ -8,7 +8,7 @@ module CryptoInput implements InputSig<Language::Location> {
class LocatableElement = Language::Locatable;
class UnknownLocation = Language::UnknownDefaultLocation;
class UnknownLocation = Language::UnknownLocation;
LocatableElement dfn_to_element(DataFlow::Node node) {
result = node.asExpr() or

View File

@@ -901,7 +901,7 @@ class BuiltInFunction extends Function {
/** Gets a dummy location for the built-in function. */
override Location getLocation() {
suppressUnusedThis(this) and
result instanceof UnknownDefaultLocation
result instanceof UnknownLocation
}
}

View File

@@ -53,9 +53,7 @@ class Location extends @location {
predicate fullLocationInfo(
Container container, int startline, int startcolumn, int endline, int endcolumn
) {
locations_default(this, unresolveElement(container), startline, startcolumn, endline, endcolumn) or
locations_expr(this, unresolveElement(container), startline, startcolumn, endline, endcolumn) or
locations_stmt(this, unresolveElement(container), startline, startcolumn, endline, endcolumn)
locations_default(this, unresolveElement(container), startline, startcolumn, endline, endcolumn)
}
/**
@@ -146,30 +144,32 @@ class Locatable extends Element { }
* expressions, one for statements and one for other program elements.
*/
class UnknownLocation extends Location {
UnknownLocation() { this.getFile().getAbsolutePath() = "" }
UnknownLocation() {
this.getFile().getAbsolutePath() = "" and locations_default(this, _, 0, 0, 0, 0)
}
}
/**
* A dummy location which is used when something doesn't have a location in
* the source code but needs to have a `Location` associated with it.
*
* DEPRECATED: use `UnknownLocation`
*/
class UnknownDefaultLocation extends UnknownLocation {
UnknownDefaultLocation() { locations_default(this, _, 0, 0, 0, 0) }
}
deprecated class UnknownDefaultLocation extends UnknownLocation { }
/**
* A dummy location which is used when an expression doesn't have a
* location in the source code but needs to have a `Location` associated
* with it.
*
* DEPRECATED: use `UnknownLocation`
*/
class UnknownExprLocation extends UnknownLocation {
UnknownExprLocation() { locations_expr(this, _, 0, 0, 0, 0) }
}
deprecated class UnknownExprLocation extends UnknownLocation { }
/**
* A dummy location which is used when a statement doesn't have a location
* in the source code but needs to have a `Location` associated with it.
*
* DEPRECATED: use `UnknownLocation`
*/
class UnknownStmtLocation extends UnknownLocation {
UnknownStmtLocation() { locations_stmt(this, _, 0, 0, 0, 0) }
}
deprecated class UnknownStmtLocation extends UnknownLocation { }

View File

@@ -259,7 +259,8 @@ predicate inMacroExpansion(Locatable element) {
inmacroexpansion(unresolveElement(element), _)
or
macroLocation(element.getLocation()) and
not topLevelMacroAccess(element)
not topLevelMacroAccess(element) and
not element.getLocation() instanceof UnknownLocation
}
/**

View File

@@ -40,7 +40,7 @@ class Namespace extends NameQualifyingElement, @namespace {
override Location getLocation() {
if strictcount(this.getADeclarationEntry()) = 1
then result = this.getADeclarationEntry().getLocation()
else result instanceof UnknownDefaultLocation
else result instanceof UnknownLocation
}
/** Gets the simple name of this namespace. */

View File

@@ -13,7 +13,7 @@ class Specifier extends Element, @specifier {
/** Gets a dummy location for the specifier. */
override Location getLocation() {
exists(this) and
result instanceof UnknownDefaultLocation
result instanceof UnknownLocation
}
override string getAPrimaryQlClass() { result = "Specifier" }

View File

@@ -105,7 +105,7 @@ class AutoType extends TypeTemplateParameter {
override string getAPrimaryQlClass() { result = "AutoType" }
override Location getLocation() { result instanceof UnknownDefaultLocation }
override Location getLocation() { result instanceof UnknownLocation }
}
/**

View File

@@ -290,7 +290,7 @@ class Type extends Locatable, @type {
*/
Type stripType() { result = this }
override Location getLocation() { result instanceof UnknownDefaultLocation }
override Location getLocation() { result instanceof UnknownLocation }
}
/**

View File

@@ -91,13 +91,13 @@ class Expr extends StmtParent, @expr {
*/
private Location getExprLocationOverride() {
// Base case: the parent has a better location than `this`.
this.getDbLocation() instanceof UnknownExprLocation and
this.getDbLocation() instanceof UnknownLocation and
result = this.getParent().(Expr).getDbLocation() and
not result instanceof UnknownLocation
or
// Recursive case: the parent has a location override that's better than
// what `this` has.
this.getDbLocation() instanceof UnknownExprLocation and
this.getDbLocation() instanceof UnknownLocation and
result = this.getParent().(Expr).getExprLocationOverride() and
not result instanceof UnknownLocation
}

View File

@@ -182,7 +182,7 @@ abstract class InstructionNode0 extends Node0Impl {
override Location getLocationImpl() {
if exists(instr.getAst().getLocation())
then result = instr.getAst().getLocation()
else result instanceof UnknownDefaultLocation
else result instanceof UnknownLocation
}
final override predicate isGLValue() { exists(getInstructionType(instr, true)) }
@@ -227,7 +227,7 @@ abstract class OperandNode0 extends Node0Impl {
override Location getLocationImpl() {
if exists(op.getDef().getAst().getLocation())
then result = op.getDef().getAst().getLocation()
else result instanceof UnknownDefaultLocation
else result instanceof UnknownLocation
}
final override predicate isGLValue() { exists(getOperandType(op, true)) }

View File

@@ -847,7 +847,7 @@ class BodyLessParameterNodeImpl extends Node, TBodyLessParameterNodeImpl {
result = unique( | | p.getLocation())
or
count(p.getLocation()) != 1 and
result instanceof UnknownDefaultLocation
result instanceof UnknownLocation
}
final override string toStringImpl() {
@@ -1115,7 +1115,7 @@ private module RawIndirectNodes {
final override Location getLocationImpl() {
if exists(this.getOperand().getLocation())
then result = this.getOperand().getLocation()
else result instanceof UnknownDefaultLocation
else result instanceof UnknownLocation
}
override string toStringImpl() {
@@ -1161,7 +1161,7 @@ private module RawIndirectNodes {
final override Location getLocationImpl() {
if exists(this.getInstruction().getLocation())
then result = this.getInstruction().getLocation()
else result instanceof UnknownDefaultLocation
else result instanceof UnknownLocation
}
override string toStringImpl() {
@@ -1257,7 +1257,7 @@ class FinalParameterNode extends Node, TFinalParameterNode {
result = unique( | | p.getLocation())
or
not exists(unique( | | p.getLocation())) and
result instanceof UnknownDefaultLocation
result instanceof UnknownLocation
}
override string toStringImpl() { result = stars(this) + p.toString() }
@@ -1629,7 +1629,7 @@ class VariableNode extends Node, TGlobalLikeVariableNode {
result = unique( | | v.getLocation())
or
not exists(unique( | | v.getLocation())) and
result instanceof UnknownDefaultLocation
result instanceof UnknownLocation
}
override string toStringImpl() { result = stars(this) + v.toString() }

View File

@@ -516,7 +516,7 @@ class FinalParameterUse extends UseImpl, TFinalParameterUse {
result = unique( | | p.getLocation())
or
not exists(unique( | | p.getLocation())) and
result instanceof UnknownDefaultLocation
result instanceof UnknownLocation
}
override BaseIRVariable getBaseSourceVariable() { result.getIRVariable().getAst() = p }

View File

@@ -45,7 +45,7 @@ module InstructionConsistency {
private class MissingIRFunction extends OptionalIRFunction, TMissingIRFunction {
override string toString() { result = "<Missing IRFunction>" }
override Language::Location getLocation() { result instanceof Language::UnknownDefaultLocation }
override Language::Location getLocation() { result instanceof Language::UnknownLocation }
}
private OptionalIRFunction getInstructionIRFunction(Instruction instr) {

View File

@@ -26,7 +26,7 @@ class ValueNumber extends TValueNumber {
l.getFile().getAbsolutePath(), l.getStartLine(), l.getStartColumn(), l.getEndLine(),
l.getEndColumn()
)
else result instanceof Language::UnknownDefaultLocation
else result instanceof Language::UnknownLocation
}
/**

View File

@@ -45,7 +45,7 @@ module InstructionConsistency {
private class MissingIRFunction extends OptionalIRFunction, TMissingIRFunction {
override string toString() { result = "<Missing IRFunction>" }
override Language::Location getLocation() { result instanceof Language::UnknownDefaultLocation }
override Language::Location getLocation() { result instanceof Language::UnknownLocation }
}
private OptionalIRFunction getInstructionIRFunction(Instruction instr) {

View File

@@ -26,7 +26,7 @@ class ValueNumber extends TValueNumber {
l.getFile().getAbsolutePath(), l.getStartLine(), l.getStartColumn(), l.getEndLine(),
l.getEndColumn()
)
else result instanceof Language::UnknownDefaultLocation
else result instanceof Language::UnknownLocation
}
/**

View File

@@ -45,7 +45,7 @@ module InstructionConsistency {
private class MissingIRFunction extends OptionalIRFunction, TMissingIRFunction {
override string toString() { result = "<Missing IRFunction>" }
override Language::Location getLocation() { result instanceof Language::UnknownDefaultLocation }
override Language::Location getLocation() { result instanceof Language::UnknownLocation }
}
private OptionalIRFunction getInstructionIRFunction(Instruction instr) {

View File

@@ -26,7 +26,7 @@ class ValueNumber extends TValueNumber {
l.getFile().getAbsolutePath(), l.getStartLine(), l.getStartColumn(), l.getEndLine(),
l.getEndColumn()
)
else result instanceof Language::UnknownDefaultLocation
else result instanceof Language::UnknownLocation
}
/**

View File

@@ -76,7 +76,7 @@ class GVN extends TValueNumber {
l.getFile().getAbsolutePath(), l.getStartLine(), l.getStartColumn(), l.getEndLine(),
l.getEndColumn()
)
else result instanceof UnknownDefaultLocation
else result instanceof UnknownLocation
}
final string getKind() {

View File

@@ -22,8 +22,6 @@ class Location = Cpp::Location;
class UnknownLocation = Cpp::UnknownLocation;
class UnknownDefaultLocation = Cpp::UnknownDefaultLocation;
class File = Cpp::File;
class AST = Cpp::Locatable;

View File

@@ -89,7 +89,7 @@ class ZeroBound extends Bound instanceof IRBound::ZeroBound {
result = super.getInstruction(delta).getUnconvertedResultExpression()
}
override Location getLocation() { result instanceof UnknownDefaultLocation }
override Location getLocation() { result instanceof UnknownLocation }
}
/**

View File

@@ -61,7 +61,7 @@ class ZeroBound extends Bound, TBoundZero {
result.(ConstantValueInstruction).getValue().toInt() = delta
}
override Location getLocation() { result instanceof UnknownDefaultLocation }
override Location getLocation() { result instanceof UnknownLocation }
}
/**

View File

@@ -218,10 +218,10 @@ extractor_version(
string frontend_version: string ref
)
@location = @location_stmt | @location_expr | @location_default ;
@location = @location_default ;
/**
* The location of an element that is not an expression or a statement.
* The location of an element.
* The location spans column `startcolumn` of line `startline` to
* column `endcolumn` of line `endline` in file `file`.
* For more information, see
@@ -237,40 +237,6 @@ locations_default(
int endColumn: int ref
);
/**
* The location of a statement.
* The location spans column `startcolumn` of line `startline` to
* column `endcolumn` of line `endline` in file `file`.
* For more information, see
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
*/
locations_stmt(
/** The location of a statement. */
unique int id: @location_stmt,
int container: @container ref,
int startLine: int ref,
int startColumn: int ref,
int endLine: int ref,
int endColumn: int ref
);
/**
* The location of an expression.
* The location spans column `startcolumn` of line `startline` to
* column `endcolumn` of line `endline` in file `file`.
* For more information, see
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
*/
locations_expr(
/** The location of an expression. */
unique int id: @location_expr,
int container: @container ref,
int startLine: int ref,
int startColumn: int ref,
int endLine: int ref,
int endColumn: int ref
);
/** An element for which line-count information is available. */
@sourceline = @file | @function | @variable | @enumconstant | @xmllocatable;
@@ -287,7 +253,7 @@ diagnostics(
string error_tag: string ref,
string error_message: string ref,
string full_error_message: string ref,
int location: @location_default ref
int location: @location ref
);
files(
@@ -332,7 +298,7 @@ case @macroinvocation.kind of
macroinvocations(
unique int id: @macroinvocation,
int macro_id: @ppd_define ref,
int location: @location_default ref,
int location: @location ref,
int kind: int ref
);
@@ -453,7 +419,7 @@ fun_decls(
int function: @function ref,
int type_id: @type ref,
string name: string ref,
int location: @location_default ref
int location: @location ref
);
fun_def(unique int id: @fun_decl ref);
fun_specialized(unique int id: @fun_decl ref);
@@ -505,7 +471,7 @@ var_decls(
int variable: @variable ref,
int type_id: @type ref,
string name: string ref,
int location: @location_default ref
int location: @location ref
);
var_def(unique int id: @var_decl ref);
var_specialized(int id: @var_decl ref);
@@ -522,7 +488,7 @@ var_requires(
type_decls(
unique int id: @type_decl,
int type_id: @type ref,
int location: @location_default ref
int location: @location ref
);
type_def(unique int id: @type_decl ref);
type_decl_top(
@@ -536,8 +502,8 @@ type_requires(
namespace_decls(
unique int id: @namespace_decl,
int namespace_id: @namespace ref,
int location: @location_default ref,
int bodylocation: @location_default ref
int location: @location ref,
int bodylocation: @location ref
);
case @using.kind of
@@ -549,7 +515,7 @@ case @using.kind of
usings(
unique int id: @using,
int element_id: @element ref,
int location: @location_default ref,
int location: @location ref,
int kind: int ref
);
@@ -563,7 +529,7 @@ static_asserts(
unique int id: @static_assert,
int condition : @expr ref,
string message : string ref,
int location: @location_default ref,
int location: @location ref,
int enclosing : @element ref
);
@@ -619,7 +585,7 @@ enumconstants(
int index: int ref,
int type_id: @type ref,
string name: string ref,
int location: @location_default ref
int location: @location ref
);
@variable = @localscopevariable | @globalvariable | @membervariable;
@@ -980,7 +946,7 @@ template_template_argument_value(
concept_templates(
unique int concept_id: @concept_template,
string name: string ref,
int location: @location_default ref
int location: @location ref
);
concept_instantiation(
unique int to: @concept_id ref,
@@ -1084,7 +1050,7 @@ attributes(
int kind: int ref,
string name: string ref,
string name_space: string ref,
int location: @location_default ref
int location: @location ref
);
case @attribute.kind of
@@ -1101,7 +1067,7 @@ attribute_args(
int kind: int ref,
int attribute: @attribute ref,
int index: int ref,
int location: @location_default ref
int location: @location ref
);
case @attribute_arg.kind of
@@ -1190,7 +1156,7 @@ derivations(
int sub: @type ref,
int index: int ref,
int super: @type ref,
int location: @location_default ref
int location: @location ref
);
derspecifiers(
@@ -1224,7 +1190,7 @@ frienddecls(
unique int id: @frienddecl,
int type_id: @type ref,
int decl_id: @declaration ref,
int location: @location_default ref
int location: @location ref
);
@declaredtype = @usertype ;
@@ -1281,7 +1247,7 @@ frienddecls(
comments(
unique int id: @comment,
string contents: string ref,
int location: @location_default ref
int location: @location ref
);
commentbinding(
@@ -1403,7 +1369,7 @@ namequalifiers(
unique int id: @namequalifier,
unique int qualifiableelement: @namequalifiableelement ref,
int qualifyingelement: @namequalifyingelement ref,
int location: @location_default ref
int location: @location ref
);
varbind(
@@ -1672,7 +1638,7 @@ initialisers(
unique int init: @initialiser,
int var: @accessible ref,
unique int expr: @expr ref,
int location: @location_expr ref
int location: @location ref
);
braced_initialisers(
@@ -1691,7 +1657,7 @@ expr_ancestor(
exprs(
unique int id: @expr,
int kind: int ref,
int location: @location_expr ref
int location: @location ref
);
expr_reuse(
@@ -2165,7 +2131,7 @@ lambda_capture(
int field: @membervariable ref,
boolean captured_by_reference: boolean ref,
boolean is_implicit: boolean ref,
int location: @location_default ref
int location: @location ref
);
@funbindexpr = @routineexpr
@@ -2193,7 +2159,7 @@ fold(
stmts(
unique int id: @stmt,
int kind: int ref,
int location: @location_stmt ref
int location: @location ref
);
case @stmt.kind of
@@ -2378,7 +2344,7 @@ jumpinfo(
preprocdirects(
unique int id: @preprocdirect,
int kind: int ref,
int location: @location_default ref
int location: @location ref
);
case @preprocdirect.kind of
0 = @ppd_if

View File

@@ -37,7 +37,7 @@ abstract class MaybePreprocessorDirective extends TMaybePreprocessorDirective {
class NoPreprocessorDirective extends TNoPreprocessorDirective, MaybePreprocessorDirective {
override string toString() { result = "" }
override Location getLocation() { result instanceof UnknownDefaultLocation }
override Location getLocation() { result instanceof UnknownLocation }
}
class SomePreprocessorDirective extends TSomePreprocessorDirective, MaybePreprocessorDirective {

View File

@@ -15,5 +15,5 @@ query predicate instructionBounds(
not valueNumber(b.getInstruction()) = valueNumber(i) and
if reason instanceof CondReason
then reasonLoc = reason.(CondReason).getCond().getLocation()
else reasonLoc instanceof UnknownDefaultLocation
else reasonLoc instanceof UnknownLocation
}

View File

@@ -1,7 +1,5 @@
| bar.h:0:0:0:0 | bar.h:0:0:0:0 |
| file://:0:0:0:0 | file://:0:0:0:0 |
| file://:0:0:0:0 | file://:0:0:0:0 |
| file://:0:0:0:0 | file://:0:0:0:0 |
| includes.c:0:0:0:0 | includes.c:0:0:0:0 |
| includes.c:2:1:2:15 | includes.c:2:1:2:15 |
| includes.c:4:1:4:16 | includes.c:4:1:4:16 |