Skip to content

Commit e14898c

Browse files
committed
Address review comments
1 parent 2343e5e commit e14898c

File tree

4 files changed

+28
-38
lines changed

4 files changed

+28
-38
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyStubGenerator/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Semmle.Extraction.CSharp.StubGenerator;
33
using Semmle.Util.Logging;
44

5-
var logger = new ConsoleLogger(Verbosity.Info);
5+
var logger = new ConsoleLogger(Verbosity.Info, logThreadId: false);
66
using var dependencyManager = new DependencyManager(".", DependencyOptions.Default, logger);
77
StubGenerator.GenerateStubs(logger, dependencyManager.ReferenceFiles, "codeql_csharp_stubs");
88

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public static void GenerateStubs(ILogger logger, IEnumerable<string> referencesP
4141
references.Select(tuple => tuple.Item1),
4242
new CSharpCompilationOptions(OutputKind.ConsoleApplication, allowUnsafe: true));
4343

44-
var referenceStubTasks = references.Select(@ref => (Action)(() => StubReference(compilation, outputPath, @ref.Reference, @ref.Path)));
45-
Parallel.Invoke(
46-
new ParallelOptions { MaxDegreeOfParallelism = threads },
47-
referenceStubTasks.ToArray());
44+
Parallel.ForEach(references, new ParallelOptions { MaxDegreeOfParallelism = threads }, @ref =>
45+
{
46+
StubReference(logger, compilation, outputPath, @ref.Reference, @ref.Path);
47+
});
4848

4949
stopWatch.Stop();
5050
logger.Log(Severity.Info, $"Stub generation took {stopWatch.Elapsed}.");
@@ -59,25 +59,24 @@ private static IEnumerable<Action> GetResolvedReferenceTasks(IEnumerable<string>
5959
});
6060
}
6161

62-
private static void StubReference(CSharpCompilation compilation, string outputPath, MetadataReference reference, string path)
62+
private static void StubReference(ILogger logger, CSharpCompilation compilation, string outputPath, MetadataReference reference, string path)
6363
{
64-
if (compilation.GetAssemblyOrModuleSymbol(reference) is IAssemblySymbol assembly)
65-
{
66-
var logger = new ConsoleLogger(Verbosity.Info);
67-
using var fileStream = new FileStream(FileUtils.NestPaths(logger, outputPath, path.Replace(".dll", ".cs")), FileMode.Create, FileAccess.Write);
68-
using var writer = new StreamWriter(fileStream, new UTF8Encoding(false));
64+
if (compilation.GetAssemblyOrModuleSymbol(reference) is not IAssemblySymbol assembly)
65+
return;
66+
67+
using var fileStream = new FileStream(FileUtils.NestPaths(logger, outputPath, path.Replace(".dll", ".cs")), FileMode.Create, FileAccess.Write);
68+
using var writer = new StreamWriter(fileStream, new UTF8Encoding(false));
6969

70-
writer.WriteLine("// This file contains auto-generated code.");
71-
writer.WriteLine($"// Generated from `{assembly.Identity}`.");
70+
writer.WriteLine("// This file contains auto-generated code.");
71+
writer.WriteLine($"// Generated from `{assembly.Identity}`.");
7272

73-
var visitor = new StubVisitor(assembly, writer);
73+
var visitor = new StubVisitor(assembly, writer);
7474

75-
visitor.StubAttributes(assembly.GetAttributes(), "assembly: ");
75+
visitor.StubAttributes(assembly.GetAttributes(), "assembly: ");
7676

77-
foreach (var module in assembly.Modules)
78-
{
79-
module.GlobalNamespace.Accept(new StubVisitor(assembly, writer));
80-
}
77+
foreach (var module in assembly.Modules)
78+
{
79+
module.GlobalNamespace.Accept(visitor);
8180
}
8281
}
8382
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private bool IsRelevantNamedType(INamedTypeSymbol symbol) =>
3636
IsRelevantBaseType(symbol) &&
3737
SymbolEqualityComparer.Default.Equals(symbol.ContainingAssembly, assembly);
3838

39-
private bool IsRelevantNamespace(INamespaceSymbol symbol) => isRelevantNamespace[symbol];
39+
private bool IsRelevantNamespace(INamespaceSymbol symbol) => isRelevantNamespace.Invoke(symbol);
4040

4141
private void StubExplicitInterface(ISymbol symbol, ISymbol? explicitInterfaceSymbol, bool writeName = true)
4242
{
@@ -109,7 +109,7 @@ private void StubAccessibility(Accessibility accessibility)
109109
case Accessibility.Internal:
110110
stubWriter.Write("internal ");
111111
break;
112-
case Accessibility.ProtectedAndInternal or Accessibility.ProtectedOrInternal:
112+
case Accessibility.ProtectedAndInternal:
113113
stubWriter.Write("protected internal ");
114114
break;
115115
default:
@@ -739,7 +739,7 @@ public override void VisitNamedType(INamedTypeSymbol symbol)
739739
else
740740
{
741741
var seenCtor = false;
742-
foreach (var childSymbol in symbol.GetMembers())
742+
foreach (var childSymbol in symbol.GetMembers().OrderBy(m => m.GetName()))
743743
{
744744
seenCtor |= childSymbol is IMethodSymbol method && method.MethodKind == MethodKind.Constructor;
745745
childSymbol.Accept(this);
@@ -768,7 +768,7 @@ public override void VisitNamespace(INamespaceSymbol symbol)
768768
if (!isGlobal)
769769
stubWriter.WriteLine($"namespace {symbol.Name} {{");
770770

771-
foreach (var childSymbol in symbol.GetMembers())
771+
foreach (var childSymbol in symbol.GetMembers().OrderBy(m => m.GetName()))
772772
{
773773
childSymbol.Accept(this);
774774
}

csharp/extractor/Semmle.Util/MemoizedFunc.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@ public MemoizedFunc(Func<T1, T2> f)
1414
this.f = f;
1515
}
1616

17-
public T2 this[T1 s]
17+
public T2 Invoke(T1 s)
1818
{
19-
get
19+
if (!cache.TryGetValue(s, out var t))
2020
{
21-
if (!cache.TryGetValue(s, out var t))
22-
{
23-
t = f(s);
24-
cache[s] = t;
25-
}
26-
return t;
21+
t = f(s);
22+
cache[s] = t;
2723
}
24+
return t;
2825
}
2926
}
3027

@@ -38,11 +35,5 @@ public ConcurrentMemoizedFunc(Func<T1, T2> f)
3835
this.f = f;
3936
}
4037

41-
public T2 this[T1 s]
42-
{
43-
get
44-
{
45-
return cache.GetOrAdd(s, f);
46-
}
47-
}
38+
public T2 Invoke(T1 s) => cache.GetOrAdd(s, f);
4839
}

0 commit comments

Comments
 (0)