Skip to content

Commit

Permalink
C#: Fixup of extration.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelnebel committed Nov 28, 2024
1 parent d8bcd79 commit 1e2405e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -648,5 +648,17 @@ public static IEnumerable<AnnotatedTypeSymbol> GetAnnotatedTypeArguments(this IN
/// </summary>
public static bool IsPublicOrProtected(this ISymbol symbol) =>
symbol.DeclaredAccessibility == Accessibility.Public || symbol.DeclaredAccessibility == Accessibility.Protected;

/// <summary>
/// Returns true if the given symbol should be extracted in this context.
/// </summary>
public static bool ShouldExtractSymbol(this ISymbol symbol) =>
symbol.Locations.Any(x => !x.IsInMetadata) || symbol.IsPublicOrProtected();

/// <summary>
/// Returns the symbols that should be extracted in the given context.
/// </summary>
public static IEnumerable<T> ToExtract<T>(this IEnumerable<T> symbols) where T : ISymbol =>
symbols.Where(symbol => symbol.ShouldExtractSymbol());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void Overrides(TextWriter trapFile)
}
}

if (Symbol.OverriddenMethod is not null)
if (Symbol.OverriddenMethod is not null && Symbol.OverriddenMethod.ShouldExtractSymbol())
{
trapFile.overrides(this, Method.Create(Context, Symbol.OverriddenMethod));
}
Expand Down
20 changes: 8 additions & 12 deletions csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,24 +228,20 @@ private void ExtractParametersForDelegateLikeType(TextWriter trapFile, IMethodSy
/// Called to extract members and nested types.
/// This is called on each member of a namespace,
/// in either source code or an assembly.
/// <param name="onlyEffectivelyPublic">If true, only extract members that are effectively public otherwise extract all members.</param>
/// </summary>
public void ExtractRecursive(bool onlyEffectivelyPublic = false)
public void ExtractRecursive()
{
foreach (var l in Symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax().GetLocation()))
{
Context.BindComments(this, l);
}

foreach (var member in Symbol.GetMembers())
foreach (var member in Symbol.GetMembers().ToExtract())
{
if (onlyEffectivelyPublic && !member.IsPublicOrProtected())
continue;

switch (member.Kind)
{
case SymbolKind.NamedType:
Create(Context, (ITypeSymbol)member).ExtractRecursive(onlyEffectivelyPublic);
Create(Context, (ITypeSymbol)member).ExtractRecursive();
break;
default:
Context.CreateEntity(member);
Expand All @@ -266,16 +262,16 @@ public void PopulateGenerics()

var members = new List<ISymbol>();

foreach (var member in Symbol.GetMembers())
foreach (var member in Symbol.GetMembers().ToExtract())
members.Add(member);
foreach (var member in Symbol.GetTypeMembers())
foreach (var member in Symbol.GetTypeMembers().ToExtract())
members.Add(member);

// Mono extractor puts all BASE interface members as members of the current interface.

if (Symbol.TypeKind == TypeKind.Interface)
{
foreach (var baseInterface in Symbol.Interfaces)
foreach (var baseInterface in Symbol.Interfaces.ToExtract())
{
foreach (var member in baseInterface.GetMembers())
members.Add(member);
Expand All @@ -289,10 +285,10 @@ public void PopulateGenerics()
Context.CreateEntity(member);
}

if (Symbol.BaseType is not null)
if (Symbol.BaseType is not null && Symbol.BaseType.ShouldExtractSymbol())
Create(Context, Symbol.BaseType).PopulateGenerics();

foreach (var i in Symbol.Interfaces)
foreach (var i in Symbol.Interfaces.ToExtract())
{
Create(Context, i).PopulateGenerics();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ private static void AnalyseNamespace(Context cx, INamespaceSymbol ns)

foreach (var memberType in ns.GetTypeMembers())
{
if (memberType.IsPublicOrProtected())
if (memberType.ShouldExtractSymbol())
{
Entities.Type.Create(cx, memberType).ExtractRecursive(onlyEffectivelyPublic: true);
Entities.Type.Create(cx, memberType).ExtractRecursive();
}
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ public bool Defines(ISymbol symbol) =>
!SymbolEqualityComparer.Default.Equals(symbol, symbol.OriginalDefinition) ||
scope.InScope(symbol);


/// <summary>
/// Runs the given action <paramref name="a"/>, guarding for trap duplication
/// based on key <paramref name="key"/>.
Expand Down

0 comments on commit 1e2405e

Please sign in to comment.