Skip to content

Commit d8fdba0

Browse files
authored
Merge pull request #15119 from michaelnebel/csharp/stubgenimprovements
C#: Escape method names in stub generation.
2 parents 8ed9fbb + b10137c commit d8fdba0

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

csharp/extractor/Semmle.Extraction.CSharp.StubGenerator/StubVisitor.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ t2 is IPointerTypeSymbol pointer2 &&
8080
stubWriter.Write(explicitInterfaceType.GetQualifiedName());
8181
stubWriter.Write('.');
8282
if (writeName)
83-
stubWriter.Write(explicitInterfaceSymbol.GetName());
83+
stubWriter.Write(EscapeIdentifier(explicitInterfaceSymbol.GetName()));
8484
}
8585
else if (writeName)
8686
{
87-
stubWriter.Write(symbol.GetName());
87+
stubWriter.Write(EscapeIdentifier(symbol.GetName()));
8888
}
8989
}
9090

@@ -557,6 +557,9 @@ private void StubParameters(ICollection<IParameterSymbol> parameters)
557557
});
558558
}
559559

560+
private static bool ExcludeMethod(IMethodSymbol symbol) =>
561+
symbol.Name == "<Clone>$";
562+
560563
private void StubMethod(IMethodSymbol symbol, IMethodSymbol? explicitInterfaceSymbol, IMethodSymbol? baseCtor)
561564
{
562565
var methodKind = explicitInterfaceSymbol is null ? symbol.MethodKind : explicitInterfaceSymbol.MethodKind;
@@ -568,7 +571,7 @@ private void StubMethod(IMethodSymbol symbol, IMethodSymbol? explicitInterfaceSy
568571
MethodKind.Ordinary
569572
};
570573

571-
if (!relevantMethods.Contains(methodKind))
574+
if (!relevantMethods.Contains(methodKind) || ExcludeMethod(symbol))
572575
return;
573576

574577
StubAttributes(symbol.GetAttributes());

csharp/extractor/Semmle.Extraction.Tests/StubGenerator.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ public class MyTest {
7676
Assert.Equal(expected, stub);
7777
}
7878

79+
[Fact]
80+
public void StubGeneratorEscapeMethodName()
81+
{
82+
// Setup
83+
const string source = @"
84+
public class MyTest {
85+
public int @default() { return 0; }
86+
}";
87+
88+
// Execute
89+
var stub = GenerateStub(source);
90+
91+
// Verify
92+
const string expected = @"public class MyTest {
93+
public int @default() => throw null;
94+
}
95+
";
96+
Assert.Equal(expected, stub);
97+
}
98+
7999
private static string GenerateStub(string source)
80100
{
81101
var st = CSharpSyntaxTree.ParseText(source);

csharp/ql/test/resources/stubs/ServiceStack.Common/8.0.0/ServiceStack.Common.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,10 +1436,14 @@ public class DefaultScripts : ServiceStack.Script.ScriptMethods, ServiceStack.Sc
14361436
public long decrBy(long value, long by) => throw null;
14371437
public long decrement(long value) => throw null;
14381438
public long decrementBy(long value, long by) => throw null;
1439+
public object @default(object returnTarget, object elseReturn) => throw null;
14391440
public string dirPath(string filePath) => throw null;
14401441
public System.Collections.Generic.IEnumerable<object> distinct(System.Collections.Generic.IEnumerable<object> items) => throw null;
14411442
public double div(double lhs, double rhs) => throw null;
14421443
public double divide(double lhs, double rhs) => throw null;
1444+
public object @do(ServiceStack.Script.ScriptScopeContext scope, object expression) => throw null;
1445+
public System.Threading.Tasks.Task @do(ServiceStack.Script.ScriptScopeContext scope, object target, object expression) => throw null;
1446+
public System.Threading.Tasks.Task @do(ServiceStack.Script.ScriptScopeContext scope, object target, object expression, object scopeOptions) => throw null;
14431447
public object doIf(object test) => throw null;
14441448
public object doIf(object ignoreTarget, object test) => throw null;
14451449
public double doubleAdd(double lhs, double rhs) => throw null;
@@ -1558,6 +1562,8 @@ public class DefaultScripts : ServiceStack.Script.ScriptMethods, ServiceStack.Sc
15581562
public string httpPathInfo(ServiceStack.Script.ScriptScopeContext scope) => throw null;
15591563
public string httpRequestUrl(ServiceStack.Script.ScriptScopeContext scope) => throw null;
15601564
public string humanize(string text) => throw null;
1565+
public object @if(object test) => throw null;
1566+
public object @if(object returnTarget, object test) => throw null;
15611567
public object ifDebug(ServiceStack.Script.ScriptScopeContext scope, object ignoreTarget) => throw null;
15621568
public object ifDebug(ServiceStack.Script.ScriptScopeContext scope) => throw null;
15631569
public object ifDo(object test) => throw null;
@@ -1840,6 +1846,9 @@ public class DefaultScripts : ServiceStack.Script.ScriptMethods, ServiceStack.Sc
18401846
public object resolveContextArg(ServiceStack.Script.ScriptScopeContext scope, string name) => throw null;
18411847
public object resolveGlobal(ServiceStack.Script.ScriptScopeContext scope, string name) => throw null;
18421848
public object resolvePageArg(ServiceStack.Script.ScriptScopeContext scope, string name) => throw null;
1849+
public ServiceStack.Script.StopExecution @return(ServiceStack.Script.ScriptScopeContext scope) => throw null;
1850+
public ServiceStack.Script.StopExecution @return(ServiceStack.Script.ScriptScopeContext scope, object returnValue) => throw null;
1851+
public ServiceStack.Script.StopExecution @return(ServiceStack.Script.ScriptScopeContext scope, object returnValue, System.Collections.Generic.Dictionary<string, object> returnArgs) => throw null;
18431852
public System.Collections.Generic.IEnumerable<object> reverse(ServiceStack.Script.ScriptScopeContext scope, System.Collections.Generic.IEnumerable<object> original) => throw null;
18441853
public string rightPart(string text, string needle) => throw null;
18451854
public double round(double value) => throw null;
@@ -1930,6 +1939,8 @@ public class DefaultScripts : ServiceStack.Script.ScriptMethods, ServiceStack.Sc
19301939
public System.Collections.Generic.IEnumerable<object> thenByDescending(ServiceStack.Script.ScriptScopeContext scope, object target, object expression) => throw null;
19311940
public System.Collections.Generic.IEnumerable<object> thenByDescending(ServiceStack.Script.ScriptScopeContext scope, object target, object expression, object scopeOptions) => throw null;
19321941
public static System.Collections.Generic.IEnumerable<object> thenByInternal(string filterName, ServiceStack.Script.ScriptScopeContext scope, object target, object expression, object scopeOptions) => throw null;
1942+
public object @throw(ServiceStack.Script.ScriptScopeContext scope, string message) => throw null;
1943+
public object @throw(ServiceStack.Script.ScriptScopeContext scope, string message, object options) => throw null;
19331944
public object throwArgumentException(ServiceStack.Script.ScriptScopeContext scope, string message) => throw null;
19341945
public object throwArgumentException(ServiceStack.Script.ScriptScopeContext scope, string message, string options) => throw null;
19351946
public object throwArgumentNullException(ServiceStack.Script.ScriptScopeContext scope, string paramName) => throw null;
@@ -3163,6 +3174,7 @@ public class ProtectedScripts : ServiceStack.Script.ScriptMethods
31633174
public ProtectedScripts() => throw null;
31643175
public ServiceStack.Script.IgnoreResult Decrypt(string path) => throw null;
31653176
public ServiceStack.Script.IgnoreResult Decrypt(ServiceStack.Script.FileScripts fs, string path) => throw null;
3177+
public object @default(string typeName) => throw null;
31663178
public ServiceStack.Script.IgnoreResult Delete(string path) => throw null;
31673179
public ServiceStack.Script.IgnoreResult Delete(ServiceStack.Script.IOScript os, string path) => throw null;
31683180
public string deleteDirectory(string virtualPath) => throw null;
@@ -3253,6 +3265,8 @@ public class ProtectedScripts : ServiceStack.Script.ScriptMethods
32533265
public ServiceStack.Script.IgnoreResult Move(string from, string to) => throw null;
32543266
public ServiceStack.Script.IgnoreResult Move(ServiceStack.Script.IOScript os, string from, string to) => throw null;
32553267
public string mv(ServiceStack.Script.ScriptScopeContext scope, string from, string to) => throw null;
3268+
public object @new(string typeName) => throw null;
3269+
public object @new(string typeName, System.Collections.Generic.List<object> constructorArgs) => throw null;
32563270
public string osPaths(string path) => throw null;
32573271
public string proc(ServiceStack.Script.ScriptScopeContext scope, string fileName) => throw null;
32583272
public string proc(ServiceStack.Script.ScriptScopeContext scope, string fileName, System.Collections.Generic.Dictionary<string, object> options) => throw null;
@@ -3284,6 +3298,7 @@ public class ProtectedScripts : ServiceStack.Script.ScriptMethods
32843298
public string textContents(ServiceStack.IO.IVirtualFile file) => throw null;
32853299
public string touch(ServiceStack.Script.ScriptScopeContext scope, string target) => throw null;
32863300
public static string TypeNotFoundErrorMessage(string typeName) => throw null;
3301+
public System.Type @typeof(string typeName) => throw null;
32873302
public System.Type typeofProgId(string name) => throw null;
32883303
public string typeQualifiedName(System.Type type) => throw null;
32893304
public System.ReadOnlyMemory<byte> urlBytesContents(ServiceStack.Script.ScriptScopeContext scope, string url, object options) => throw null;

csharp/scripts/stubs/helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import subprocess
44
import json
55
import shutil
6+
import re
67

78
def run_cmd(cmd, msg="Failed to run command"):
89
print('Running ' + ' '.join(cmd))
@@ -191,7 +192,8 @@ def make_stubs(self):
191192

192193
if 'dependencies' in data['targets'][target][package]:
193194
for dependency in data['targets'][target][package]['dependencies'].keys():
194-
depVersion = data['targets'][target][package]['dependencies'][dependency]
195+
depString = data['targets'][target][package]['dependencies'][dependency]
196+
depVersion = re.search("(\d+\.\d+\.\d+(-[a-z]+)?)", depString).group(0)
195197
pf.write(' <ProjectReference Include="../../' +
196198
dependency + '/' + depVersion + '/' + dependency + '.csproj" />\n')
197199

0 commit comments

Comments
 (0)