Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 72547b8

Browse files
committed
Rework endregion extraction
1 parent a5dec5b commit 72547b8

6 files changed

Lines changed: 20 additions & 21 deletions

File tree

csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/EndRegionDirective.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@ namespace Semmle.Extraction.CSharp.Entities
55
{
66
internal class EndRegionDirective : PreprocessorDirective<EndRegionDirectiveTriviaSyntax>
77
{
8-
public EndRegionDirective(Context cx, EndRegionDirectiveTriviaSyntax trivia)
9-
: base(cx, trivia)
10-
{
11-
}
8+
private readonly RegionDirective region;
129

13-
protected override void PopulatePreprocessor(TextWriter trapFile)
10+
public EndRegionDirective(Context cx, EndRegionDirectiveTriviaSyntax trivia, RegionDirective region)
11+
: base(cx, trivia, populateFromBase: false)
1412
{
15-
trapFile.directive_endregions(this);
13+
this.region = region;
14+
TryPopulate();
1615
}
1716

18-
internal static void WriteRegionBlock(Context cx, RegionDirective region, EndRegionDirective endregion)
17+
protected override void PopulatePreprocessor(TextWriter trapFile)
1918
{
20-
cx.TrapWriter.Writer.regions(region, endregion);
19+
trapFile.directive_endregions(this, region);
2120
}
2221
}
2322
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/PreprocessorDirective.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ internal abstract class PreprocessorDirective<TDirective> : FreshEntity where TD
88
{
99
protected readonly TDirective trivia;
1010

11-
protected PreprocessorDirective(Context cx, TDirective trivia)
11+
protected PreprocessorDirective(Context cx, TDirective trivia, bool populateFromBase = true)
1212
: base(cx)
1313
{
1414
this.trivia = trivia;
15-
TryPopulate();
15+
if (populateFromBase)
16+
{
17+
TryPopulate();
18+
}
1619
}
1720

1821
protected sealed override void Populate(TextWriter trapFile)

csharp/extractor/Semmle.Extraction.CSharp/Populators/DirectiveVisitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ public override void VisitRegionDirectiveTrivia(RegionDirectiveTriviaSyntax node
6565

6666
public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
6767
{
68-
var endregion = new Entities.EndRegionDirective(cx, node);
6968
if (regionStarts.Count == 0)
7069
{
7170
cx.ExtractionError("Couldn't find start region", null,
7271
Extraction.Entities.Location.Create(cx, node.GetLocation()), null, Util.Logging.Severity.Warning);
7372
return;
7473
}
74+
7575
var start = regionStarts.Pop();
76-
Entities.EndRegionDirective.WriteRegionBlock(cx, start, endregion);
76+
new Entities.EndRegionDirective(cx, node, start);
7777
}
7878

7979
private readonly Stack<Entities.IfDirective> ifStarts = new Stack<Entities.IfDirective>();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,9 @@ internal static void directive_regions(this TextWriter trapFile, RegionDirective
669669
trapFile.WriteTuple("directive_regions", directive, name);
670670
}
671671

672-
internal static void directive_endregions(this TextWriter trapFile, EndRegionDirective directive)
672+
internal static void directive_endregions(this TextWriter trapFile, EndRegionDirective directive, RegionDirective start)
673673
{
674-
trapFile.WriteTuple("directive_endregions", directive);
674+
trapFile.WriteTuple("directive_endregions", directive, start);
675675
}
676676

677677
internal static void regions(this TextWriter trapFile, RegionDirective start, EndRegionDirective end)

csharp/ql/src/semmle/code/csharp/Preprocessor.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class RegionDirective extends PreprocessorDirective, @directive_region {
232232
string getName() { directive_regions(this, result) }
233233

234234
/** Gets the closing `#endregion` directive. */
235-
EndRegionDirective getEnd() { regions(this, result) }
235+
EndRegionDirective getEnd() { directive_endregions(result, this) }
236236

237237
override string toString() { result = "#region ..." }
238238

csharp/ql/src/semmlecode.csharp.dbscheme

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,10 @@ directive_regions(
376376
unique int id: @directive_region,
377377
string name: string ref);
378378

379+
#keyset[id, start]
379380
directive_endregions(
380-
unique int id: @directive_endregion);
381-
382-
#keyset[start, end]
383-
regions(
384-
unique int start: @directive_region ref,
385-
unique int end: @directive_endregion ref);
381+
unique int id: @directive_endregion,
382+
unique int start: @directive_region ref);
386383

387384
directive_lines(
388385
unique int id: @directive_line,

0 commit comments

Comments
 (0)