QL: Allow class + 'Base' in 'ql/primary-ql-class-consistency'.

This commit is contained in:
Mathias Vorreiter Pedersen
2022-05-17 10:43:54 +01:00
parent 1280d43e36
commit a6ac14f4de

View File

@@ -10,18 +10,31 @@
import ql
from ClassPredicate pred, String constant
/**
* Gets a string that is allowed to be returned from the `getAPrimaryQlClass` implementation
* in class `c`.
*
* For most languages this is just the name of `c`, but QL for Swift implements
* the `getAPrimaryQlClass` predicate for a class `Foo` in another class `FooBase`.
*/
string getAnAllowedResultForClass(Class c) {
result = c.getName()
or
c.getName() = result + "Base"
}
from ClassPredicate pred, String constant, Class c
where
exists(string className, string constantName |
pred.getParent().getName() = className and
pred.getName() = "getAPrimaryQlClass" and
c = pred.getParent() and
pred.getName() = "getAPrimaryQlClass" and
exists(string constantName |
constant = pred.getBody().(ComparisonFormula).getRightOperand() and
constant.getValue() = constantName and
// might be "Foo::classname", detect by matching with a regexp
not constantName.regexpMatch(".*\\b" + className + "$") and
not constantName.regexpMatch(".*\\b" + getAnAllowedResultForClass(c) + "$") and
// ignore constants with "?" in them
not constantName.regexpMatch(".*\\?.*")
)
select pred,
"The getAPrimaryQlClass predicate $@ instead of the class name \"" + pred.getParent().getName() +
"\".", constant, "results in \"" + constant.getValue() + "\""
"The getAPrimaryQlClass predicate $@ instead of the class name \"" + c.getName() + "\".",
constant, "results in \"" + constant.getValue() + "\""