Compare commits

...

2 Commits

Author SHA1 Message Date
Calum Grant
a6818d2da9 WIP 2024-05-10 10:55:27 +01:00
Calum Grant
4ca11abaa9 Refactoring queries WIP 2024-05-09 16:42:52 +01:00
10 changed files with 154 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
import cpp
class StringVariable extends Variable {
StringVariable() {
this.getType().(PointerType).stripType().getName() = "char"
}
}
class StringField extends StringVariable, Field
{
}
class StringParameter extends StringVariable, Parameter
{
}
from StringVariable f
where f.getFile().getRelativePath().matches("c/extractor/src/%")
and not f.getASpecifier().getName() = "extern"
select f, f.getFile().getRelativePath()

View File

@@ -0,0 +1,38 @@
import relevant
/*
A "class" is an island of globals and functions where all globals and functions are connected
to each other.
*/
class ExtractedClass extends RelevantGlobalVariable
{
ExtractedClass()
{
any()
}
Function getAFunction()
{
result = this.getAnAccess().getEnclosingFunction()
}
}
predicate reaches(GlobalVariable v1, GlobalVariable v2)
{
exists(Function fn | v1.getAnAccess().getEnclosingFunction() = fn and
v2.getAnAccess().getEnclosingFunction() = fn)
}
predicate primary(RelevantGlobalVariable v1)
{
exists(RelevantGlobalVariable v2 | reaches(v1, v2)) and
not exists(RelevantGlobalVariable v2 | reaches*(v1, v2) | v1.getName() < v2.getName())
}
from RelevantGlobalVariable var
where primary(var)
select var, "This is a primary global variable"

View File

@@ -0,0 +1,9 @@
import cpp
import relevant
from Function fn
where
fn.getNamespace() instanceof GlobalNamespace and
not exists(fn.getDeclaringType()) and
is_relevant_result(fn.getFile())
select fn, "This function is not declared in a namespace", fn.getFile().getRelativePath()

View File

@@ -0,0 +1,4 @@
import cpp
from Function fn
where not exists(fn.getDeclaringType()) and is_relevant_result(fn.getFile())

View File

@@ -0,0 +1,10 @@
// Flags use of global variables
import cpp
import relevant
from RelevantGlobalVariable globalVariable, string typeName, Function fn, VariableAccess va
where
typeName = globalVariable.getType().stripType().getName()
and fn = globalVariable.getAnAccess().getEnclosingFunction()
select globalVariable, fn

View File

@@ -0,0 +1,9 @@
// Flags use of global variables
import cpp
import relevant
from RelevantGlobalVariable globalVariable, string typeName
where
typeName = globalVariable.getType().stripType().getName()
select globalVariable, typeName, globalVariable.getFile().getRelativePath()

View File

@@ -0,0 +1,7 @@
import cpp
import relevant
from Call call
where call.getTarget().getName().matches("%printf")
and is_relevant_result(call.getFile())
select call, "Call to a printf formatter", call.getFile().getRelativePath()

View File

@@ -0,0 +1,25 @@
import cpp
predicate is_relevant_result(File file)
{
not file.getRelativePath().matches("c/extractor/edg%")
}
class RelevantGlobalVariable extends GlobalVariable
{
RelevantGlobalVariable() {
not is_valid_global_variable(this) and
exists(this.getFile().getRelativePath()) // From the repo
}
}
predicate is_valid_global_variable(Variable var) {
var.getType().stripType().getName() = "trie_node" or
var.getType().isConst() or
var.getType().isDeeplyConst() or
var.isConstexpr() or
// var.getType() instanceof ArrayType or
var.getASpecifier().getName() = "extern" or
var.getFile().getRelativePath().matches("c/extractor/edg/%") // or
// var.getFile().getRelativePath().matches("c/extractor/edg%") or
}

View File

@@ -0,0 +1,7 @@
import relevant
from Function fn, StaticLocalVariable var, ReturnStmt ret, string path
where ret.getExpr() = var.getAnAccess()
and var.getFunction() = fn
and path = fn.getFile().getRelativePath()
select fn, "This function returns a static local variable"

View File

@@ -0,0 +1,25 @@
import cpp
class ACompressedFileWrite extends Function {
ACompressedFileWrite() {
this.getName() = "operator<<" and
this.getParameter(0).getType().stripType().getName() = "a_compressed_file"
}
}
class LabelDefinition extends Call {
LabelDefinition() {
this.getTarget() instanceof ACompressedFileWrite and
this.getArgument(1).(StringLiteral).getValue().matches("=%")
}
}
predicate is_valid_file_write(Call call) {
call.getFile().getBaseName() = "dbscheme.cpp"
}
from Call call
where
call.getTarget() instanceof ACompressedFileWrite
and not is_valid_file_write(call)
select call