|
| 1 | +/** |
| 2 | + * This query generates type models for PowerShell from a C# database. |
| 3 | + * |
| 4 | + * It is not meant to be run manually. Instead, it is executed |
| 5 | + * by `typemodelgenFromDB.py`. |
| 6 | + */ |
| 7 | +import csharp |
| 8 | +private import semmle.code.csharp.commons.QualifiedName |
| 9 | + |
| 10 | +private module FullyQualifiedNameInput implements QualifiedNameInputSig { |
| 11 | + string getUnboundGenericSuffix(UnboundGeneric ug) { exists(ug) and result = "" } |
| 12 | +} |
| 13 | + |
| 14 | +predicate hasFullyQualifiedNameImpl(Declaration d, string namespace, string type, string name) { |
| 15 | + QualifiedName<FullyQualifiedNameInput>::hasQualifiedName(d, namespace, type, name) |
| 16 | +} |
| 17 | + |
| 18 | +predicate hasFullyQualifiedNameImpl(Declaration d, string qualifier, string name) { |
| 19 | + QualifiedName<FullyQualifiedNameInput>::hasQualifiedName(d, qualifier, name) |
| 20 | +} |
| 21 | + |
| 22 | +predicate hasFullyQualifiedName(Callable callable, string namespace, string type, string name) { |
| 23 | + hasFullyQualifiedNameImpl(callable.(Method).getUnboundDeclaration(), namespace, type, name) |
| 24 | +} |
| 25 | + |
| 26 | +Type getReturnType(Callable c) { |
| 27 | + result = c.(Method).getReturnType() |
| 28 | +} |
| 29 | + |
| 30 | +bindingset[t] |
| 31 | +Type remap(Type t) { |
| 32 | + if hasFullyQualifiedNameImpl(t, "System", "ReadOnlySpan") |
| 33 | + then hasFullyQualifiedNameImpl(result, "System", "String") |
| 34 | + else ( |
| 35 | + if t instanceof TupleType |
| 36 | + then hasFullyQualifiedNameImpl(result, "System", "ValueTuple") |
| 37 | + else result = t |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +from Callable m, Type t, string namespace, string type, string name, string qualifier, string return |
| 42 | +where |
| 43 | + hasFullyQualifiedName(m, namespace, type, name) and |
| 44 | + not type.matches("%+%") and |
| 45 | + not return.matches("%+%") and |
| 46 | + getReturnType(m) = t and |
| 47 | + not t instanceof VoidType and |
| 48 | + hasFullyQualifiedNameImpl(remap(t.getUnboundDeclaration()), qualifier, return) and |
| 49 | + qualifier != "" and |
| 50 | + m.fromSource() and |
| 51 | + exists(string relative | |
| 52 | + relative = m.getLocation().getFile().getRelativePath() and |
| 53 | + not relative.toLowerCase().matches("%/tests/%") |
| 54 | + ) |
| 55 | +select qualifier.toLowerCase() + "." + return.toLowerCase(), |
| 56 | + namespace.toLowerCase() + "." + type.toLowerCase(), name.toLowerCase() |
0 commit comments