C#: Sprinkle uses of OnlyScaffold to extract less when in overlay mode.

This commit is contained in:
Michael Nebel
2025-09-17 14:30:19 +02:00
parent cab9d81376
commit 61f6512c74
17 changed files with 43 additions and 18 deletions

View File

@@ -59,6 +59,12 @@ namespace Semmle.Extraction.CSharp.Entities
{
var type = Type.Create(Context, Symbol.AttributeClass);
trapFile.attributes(this, kind, type.TypeRef, entity);
if (Context.OnlyScaffold)
{
return;
}
WriteLocationToTrap(trapFile.attribute_location, this, Location);
if (attributeSyntax is not null)

View File

@@ -109,7 +109,7 @@ namespace Semmle.Extraction.CSharp.Entities
/// </summary>
protected void BindComments()
{
if (!Symbol.IsImplicitlyDeclared && IsSourceDeclaration && Symbol.FromSource())
if (!Symbol.IsImplicitlyDeclared && IsSourceDeclaration && Symbol.FromSource() && !Context.OnlyScaffold)
Context.BindComments(this, FullLocation);
}

View File

@@ -11,7 +11,10 @@ namespace Semmle.Extraction.CSharp.Entities
public override void Populate(TextWriter trapFile)
{
trapFile.commentblock(this);
WriteLocationToTrap(trapFile.commentblock_location, this, Context.CreateLocation(Symbol.Location));
if (!Context.OnlyScaffold)
{
WriteLocationToTrap(trapFile.commentblock_location, this, Context.CreateLocation(Symbol.Location));
}
Symbol.CommentLines.ForEach((l, child) => trapFile.commentblock_child(this, l, child));
}

View File

@@ -23,7 +23,10 @@ namespace Semmle.Extraction.CSharp.Entities
{
location = Context.CreateLocation(Location);
trapFile.commentline(this, Type == CommentLineType.MultilineContinuation ? CommentLineType.Multiline : Type, Text, RawText);
WriteLocationToTrap(trapFile.commentline_location, this, location);
if (!Context.OnlyScaffold)
{
WriteLocationToTrap(trapFile.commentline_location, this, location);
}
}
public override Microsoft.CodeAnalysis.Location? ReportingLocation => location?.Symbol;

View File

@@ -179,7 +179,7 @@ namespace Semmle.Extraction.CSharp.Entities
/// </summary>
private bool IsBestSourceLocation => ReportingLocation is not null && Context.IsLocationInContext(ReportingLocation);
private bool MakeSynthetic => IsPrimary || (IsDefault && IsBestSourceLocation);
private bool MakeSynthetic => (IsPrimary || (IsDefault && IsBestSourceLocation)) && !Context.OnlyScaffold;
[return: NotNullIfNotNull(nameof(constructor))]
public static new Constructor? Create(Context cx, IMethodSymbol? constructor)

View File

@@ -54,7 +54,7 @@ namespace Semmle.Extraction.CSharp.Entities
WriteLocationsToTrap(trapFile.field_location, this, Locations);
}
if (!IsSourceDeclaration || !Symbol.FromSource())
if (!IsSourceDeclaration || !Symbol.FromSource() || Context.OnlyScaffold)
return;
Context.BindComments(this, Location.Symbol);

View File

@@ -42,7 +42,7 @@ namespace Semmle.Extraction.CSharp.Entities
Parameter.Create(Context, Symbol.Parameters[i], this, original);
}
if (IsSourceDeclaration)
if (IsSourceDeclaration && !Context.OnlyScaffold)
{
var expressionBody = ExpressionBody;
if (expressionBody is not null)
@@ -55,6 +55,12 @@ namespace Semmle.Extraction.CSharp.Entities
PopulateAttributes();
PopulateModifiers(trapFile);
if (Context.OnlyScaffold)
{
return;
}
BindComments();
var declSyntaxReferences = IsSourceDeclaration

View File

@@ -48,7 +48,7 @@ namespace Semmle.Extraction.CSharp.Entities
protected virtual void PopulateMethodBody(TextWriter trapFile)
{
if (!IsSourceDeclaration)
if (!IsSourceDeclaration || Context.OnlyScaffold)
return;
var block = Block;
@@ -94,7 +94,7 @@ namespace Semmle.Extraction.CSharp.Entities
{
trapFile.explicitly_implements(this, explicitInterface.TypeRef);
if (IsSourceDeclaration)
if (IsSourceDeclaration && !Context.OnlyScaffold)
{
foreach (var syntax in Symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()).OfType<MethodDeclarationSyntax>())
TypeMention.Create(Context, syntax.ExplicitInterfaceSpecifier!.Name, this, explicitInterface);

View File

@@ -34,7 +34,7 @@ namespace Semmle.Extraction.CSharp.Entities
var returnType = Type.Create(Context, Symbol.ReturnType);
trapFile.methods(this, Name, ContainingType, returnType.TypeRef, OriginalDefinition);
if (IsSourceDeclaration)
if (IsSourceDeclaration && !Context.OnlyScaffold)
{
foreach (var declaration in Symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax()).OfType<MethodDeclarationSyntax>())
{

View File

@@ -140,7 +140,7 @@ namespace Semmle.Extraction.CSharp.Entities
Context.PopulateLater(defaultValueExpressionCreation);
}
if (!IsSourceDeclaration || !Symbol.FromSource())
if (!IsSourceDeclaration || !Symbol.FromSource() || Context.OnlyScaffold)
return;
BindComments();

View File

@@ -74,7 +74,7 @@ namespace Semmle.Extraction.CSharp.Entities
WriteLocationsToTrap(trapFile.property_location, this, Locations);
}
if (IsSourceDeclaration && Symbol.FromSource())
if (IsSourceDeclaration && Symbol.FromSource() && !Context.OnlyScaffold)
{
var expressionBody = ExpressionBody;
if (expressionBody is not null)

View File

@@ -81,7 +81,7 @@ namespace Semmle.Extraction.CSharp.Entities
}
// Class location
if (!Symbol.IsGenericType || Symbol.IsReallyUnbound())
if ((!Symbol.IsGenericType || Symbol.IsReallyUnbound()) && !Context.OnlyScaffold)
{
WriteLocationsToTrap(trapFile.type_location, this, Locations);
}

View File

@@ -222,7 +222,7 @@ namespace Semmle.Extraction.CSharp.Entities
private IEnumerable<BaseTypeSyntax> GetBaseTypeDeclarations()
{
if (!IsSourceDeclaration || !Symbol.FromSource())
if (!IsSourceDeclaration || !Symbol.FromSource() || Context.OnlyScaffold)
{
return Enumerable.Empty<BaseTypeSyntax>();
}

View File

@@ -32,7 +32,7 @@ namespace Semmle.Extraction.CSharp.Entities
WriteLocationsToTrap(trapFile.type_location, this, locations);
}
if (IsSourceDeclaration)
if (IsSourceDeclaration && !Context.OnlyScaffold)
{
var declSyntaxReferences = Symbol.DeclaringSyntaxReferences
.Select(d => d.GetSyntax())

View File

@@ -31,7 +31,7 @@ namespace Semmle.Extraction.CSharp.Entities
WriteLocationsToTrap(trapFile.operator_location, this, Locations);
}
if (IsSourceDeclaration)
if (IsSourceDeclaration && !Context.OnlyScaffold)
{
var declSyntaxReferences = Symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax()).ToArray();
foreach (var declaration in declSyntaxReferences.OfType<OperatorDeclarationSyntax>())

View File

@@ -32,7 +32,10 @@ namespace Semmle.Extraction.CSharp.Entities
{
var ns = Namespace.Create(Context, namespaceSymbol);
trapFile.using_namespace_directives(this, ns);
trapFile.using_directive_location(this, Context.CreateLocation(ReportingLocation));
if (!Context.OnlyScaffold)
{
trapFile.using_directive_location(this, Context.CreateLocation(ReportingLocation));
}
}
else
{
@@ -46,7 +49,10 @@ namespace Semmle.Extraction.CSharp.Entities
// A "using static"
var m = Type.Create(Context, (ITypeSymbol?)info.Symbol);
trapFile.using_static_directives(this, m.TypeRef);
trapFile.using_directive_location(this, Context.CreateLocation(ReportingLocation));
if (!Context.OnlyScaffold)
{
trapFile.using_directive_location(this, Context.CreateLocation(ReportingLocation));
}
}
if (node.GlobalKeyword.IsKind(SyntaxKind.GlobalKeyword))

View File

@@ -559,7 +559,8 @@ namespace Semmle.Extraction.CSharp
public bool ExtractLocation(ISymbol symbol) =>
SymbolEqualityComparer.Default.Equals(symbol, symbol.OriginalDefinition) &&
scope.InScope(symbol);
scope.InScope(symbol) &&
!OnlyScaffold;
/// <summary>
/// Gets the locations of the symbol that are either