Generate stubs per assembly

This commit is contained in:
Tamas Vajk
2021-04-28 16:56:19 +02:00
parent ba238578d1
commit 88c97bd34e
7 changed files with 70 additions and 16 deletions

View File

@@ -17,4 +17,5 @@ class NonTargetAssembly extends ExcludedAssembly {
}
}
select generatedCode()
from Assembly a
select a, generatedCode(a)

View File

@@ -10,4 +10,4 @@ class AllDeclarations extends GeneratedDeclaration {
AllDeclarations() { not this.fromLibrary() }
}
select generatedCode()
select concat(generatedCode(_) + "\n\n")

View File

@@ -29,4 +29,4 @@ class UsedInSource extends GeneratedDeclaration {
}
}
select generatedCode()
select concat(generatedCode(_) + "\n\n")

View File

@@ -48,6 +48,8 @@ abstract private class GeneratedType extends ValueOrRefType, GeneratedElement {
)
}
predicate isInAssembly(Assembly assembly) { this.getALocation() = assembly }
private string stubKeyword() {
this instanceof Interface and result = "interface"
or
@@ -87,8 +89,8 @@ abstract private class GeneratedType extends ValueOrRefType, GeneratedElement {
else (
not this instanceof DelegateType and
result =
this.stubComment() + this.stubAttributes() + this.stubAbstractModifier() +
this.stubStaticModifier() + stubAccessibility(this) + this.stubKeyword() + " " +
this.stubComment() + this.stubAttributes() + stubAccessibility(this) +
this.stubAbstractModifier() + this.stubStaticModifier() + this.stubKeyword() + " " +
this.getUndecoratedName() + stubGenericArguments(this) + stubBaseTypesString() +
stubTypeParametersConstraints(this) + "\n{\n" + stubMembers() + "}\n\n"
or
@@ -238,8 +240,9 @@ private class GeneratedNamespace extends Namespace, GeneratedElement {
private string getPostAmble() { if this.isGlobalNamespace() then result = "" else result = "}\n" }
final string getStubs() {
result = getPreamble() + getTypeStubs() + getSubNamespaces() + getPostAmble()
final string getStubs(Assembly assembly) {
result =
getPreamble() + getTypeStubs(assembly) + getSubNamespaceStubs(assembly) + getPostAmble()
}
/** Gets the `n`th generated child namespace, indexed from 0. */
@@ -254,14 +257,31 @@ private class GeneratedNamespace extends Namespace, GeneratedElement {
result = count(GeneratedNamespace g | g.getParentNamespace() = this)
}
language[monotonicAggregates]
private string getSubNamespaces() {
result = concat(int i | exists(getChildNamespace(i)) | getChildNamespace(i).getStubs())
private predicate isInAssembly(Assembly assembly) {
any(GeneratedType gt | gt.getDeclaringNamespace() = this).isInAssembly(assembly)
or
this.getChildNamespace(_).isInAssembly(assembly)
}
private string getTypeStubs() {
language[monotonicAggregates]
string getSubNamespaceStubs(Assembly assembly) {
this.isInAssembly(assembly) and
result =
concat(string s | s = any(GeneratedType gt | gt.getDeclaringNamespace() = this).getStub())
concat(GeneratedNamespace child, int i |
child = getChildNamespace(i) and child.isInAssembly(assembly)
|
child.getStubs(assembly) order by i
)
}
string getTypeStubs(Assembly assembly) {
this.isInAssembly(assembly) and
result =
concat(GeneratedType gt |
gt.getDeclaringNamespace() = this and gt.isInAssembly(assembly)
|
gt.getStub() order by gt.getName()
)
}
}
@@ -783,8 +803,8 @@ private string stubSemmleExtractorOptions() {
}
/** Gets the generated C# code. */
string generatedCode() {
string generatedCode(Assembly assembly) {
result =
"// This file contains auto-generated code.\n" + stubSemmleExtractorOptions() + "\n" +
any(GeneratedNamespace ns | ns.isGlobalNamespace()).getStubs()
any(GeneratedNamespace ns | ns.isGlobalNamespace()).getStubs(assembly)
}

File diff suppressed because one or more lines are too long

View File

@@ -129,4 +129,37 @@ namespace Test
public Class9.Nested NestedInstance { get; } = new Class9.Nested(1);
}
}
namespace A1
{
namespace B1
{
}
public class C1 { }
}
namespace A2
{
namespace B2
{
public class C2 { }
}
}
namespace A3
{
public class C3 { }
}
namespace A4
{
namespace B4
{
public class D4 { }
}
public class C4 { }
}

File diff suppressed because one or more lines are too long