Skip to content

Commit 6d95ad3

Browse files
committed
C#: Add file instead of generated location for extraction errors when possible
1 parent 8619422 commit 6d95ad3

File tree

7 files changed

+111
-82
lines changed

7 files changed

+111
-82
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.CodeAnalysis;
2+
3+
namespace Semmle.Extraction
4+
{
5+
/// <summary>
6+
/// The scope of symbols in an assembly.
7+
/// </summary>
8+
public class AssemblyScope : IExtractionScope
9+
{
10+
private readonly IAssemblySymbol assembly;
11+
private readonly string filepath;
12+
13+
public AssemblyScope(IAssemblySymbol symbol, string path, bool isOutput)
14+
{
15+
assembly = symbol;
16+
filepath = path;
17+
IsGlobalScope = isOutput;
18+
}
19+
20+
public bool IsGlobalScope { get; }
21+
22+
public bool InFileScope(string path) => path == filepath;
23+
24+
public bool InScope(ISymbol symbol) =>
25+
SymbolEqualityComparer.Default.Equals(symbol.ContainingAssembly, assembly) ||
26+
SymbolEqualityComparer.Default.Equals(symbol, assembly);
27+
28+
public bool FromSource => false;
29+
}
30+
}

csharp/extractor/Semmle.Extraction/Context.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public void PopulateAll()
215215
}
216216
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
217217
{
218-
ExtractionError("Uncaught exception", ex.Message, GeneratedLocation.Create(this), ex.StackTrace);
218+
ExtractionError("Uncaught exception", ex.Message, Entities.Location.Create(this), ex.StackTrace);
219219
}
220220
}
221221
}
@@ -250,6 +250,8 @@ public Context(IExtractor e, Compilation c, TrapWriter trapWriter, IExtractionSc
250250

251251
private IExtractionScope scope { get; }
252252

253+
public SyntaxTree? SourceTree => scope is SourceScope sc ? sc.SourceTree : null;
254+
253255
/// <summary>
254256
/// Whether the given symbol needs to be defined in this context.
255257
/// This is the case if the symbol is contained in the source/assembly, or
@@ -451,7 +453,7 @@ public void ExtractionError(string message, ISymbol? optionalSymbol, IEntity opt
451453
}
452454
else
453455
{
454-
ExtractionError(message, "", GeneratedLocation.Create(this));
456+
ExtractionError(message, "", Entities.Location.Create(this));
455457
}
456458
}
457459

@@ -522,13 +524,21 @@ public static void Try(this Context context, SyntaxNode? node, ISymbol? symbol,
522524
Message message;
523525

524526
if (node != null)
527+
{
525528
message = Message.Create(context, ex.Message, node, ex.StackTrace);
529+
}
526530
else if (symbol != null)
531+
{
527532
message = Message.Create(context, ex.Message, symbol, ex.StackTrace);
533+
}
528534
else if (ex is InternalError ie)
535+
{
529536
message = new Message(ie.Text, ie.EntityText, Entities.Location.Create(context, ie.Location), ex.StackTrace);
537+
}
530538
else
531-
message = new Message("Uncaught exception", ex.Message, GeneratedLocation.Create(context), ex.StackTrace);
539+
{
540+
message = new Message("Uncaught exception", ex.Message, Entities.Location.Create(context), ex.StackTrace);
541+
}
532542

533543
context.ExtractionError(message);
534544
}

csharp/extractor/Semmle.Extraction/Entities/ExtractionError.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public ExtractionMessage(Context cx, Message msg) : base(cx)
1414

1515
protected override void Populate(TextWriter trapFile)
1616
{
17-
trapFile.extractor_messages(this, msg.Severity, "C# extractor", msg.Text, msg.EntityText, msg.Location ?? GeneratedLocation.Create(cx), msg.StackTrace);
17+
trapFile.extractor_messages(this, msg.Severity, "C# extractor", msg.Text, msg.EntityText, msg.Location ?? Location.Create(cx), msg.StackTrace);
1818
}
1919

2020
public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel;

csharp/extractor/Semmle.Extraction/Entities/Location.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
using Microsoft.CodeAnalysis.Text;
3+
24
namespace Semmle.Extraction.Entities
35
{
46
public abstract class Location : CachedEntity<Microsoft.CodeAnalysis.Location?>
@@ -13,6 +15,14 @@ public static Location Create(Context cx, Microsoft.CodeAnalysis.Location? loc)
1315
? NonGeneratedSourceLocation.Create(cx, loc)
1416
: Assembly.Create(cx, loc);
1517

18+
public static Location Create(Context cx)
19+
{
20+
return cx.SourceTree == null
21+
? GeneratedLocation.Create(cx)
22+
: Create(cx, Microsoft.CodeAnalysis.Location.Create(cx.SourceTree, TextSpan.FromBounds(0, 0)));
23+
24+
}
25+
1626
public override Microsoft.CodeAnalysis.Location? ReportingLocation => symbol;
1727

1828
public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.OptionalLabel;

csharp/extractor/Semmle.Extraction/ExtractionScope.cs

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.CodeAnalysis;
2+
3+
namespace Semmle.Extraction
4+
{
5+
/// <summary>
6+
/// Defines which entities belong in the trap file
7+
/// for the currently extracted entity. This is used to ensure that
8+
/// trap files do not contain redundant information. Generally a symbol
9+
/// should have an affinity with exactly one trap file, except for constructed
10+
/// symbols.
11+
/// </summary>
12+
public interface IExtractionScope
13+
{
14+
/// <summary>
15+
/// Whether the given symbol belongs in the trap file.
16+
/// </summary>
17+
/// <param name="symbol">The symbol to populate.</param>
18+
bool InScope(ISymbol symbol);
19+
20+
/// <summary>
21+
/// Whether the given file belongs in the trap file.
22+
/// </summary>
23+
/// <param name="path">The path to populate.</param>
24+
bool InFileScope(string path);
25+
26+
bool IsGlobalScope { get; }
27+
28+
bool FromSource { get; }
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.CodeAnalysis;
2+
using System.Linq;
3+
4+
namespace Semmle.Extraction
5+
{
6+
7+
/// <summary>
8+
/// The scope of symbols in a source file.
9+
/// </summary>
10+
public class SourceScope : IExtractionScope
11+
{
12+
public SyntaxTree SourceTree { get; }
13+
14+
public SourceScope(SyntaxTree tree)
15+
{
16+
SourceTree = tree;
17+
}
18+
19+
public bool IsGlobalScope => false;
20+
21+
public bool InFileScope(string path) => path == SourceTree.FilePath;
22+
23+
public bool InScope(ISymbol symbol) => symbol.Locations.Any(loc => loc.SourceTree == SourceTree);
24+
25+
public bool FromSource => true;
26+
}
27+
}

0 commit comments

Comments
 (0)