Merge pull request #9188 from MathiasVP/fix-GetAPrimaryQlClassConsistency-for-swift

This commit is contained in:
Mathias Vorreiter Pedersen
2022-05-17 20:47:24 +01:00
committed by GitHub

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() + "\""