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

Skip to content

Commit a5d18f9

Browse files
committed
Extract region directives
1 parent fe0a494 commit a5d18f9

8 files changed

Lines changed: 133 additions & 1 deletion

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.CodeAnalysis.CSharp.Syntax;
2+
using System.IO;
3+
4+
namespace Semmle.Extraction.CSharp.Entities
5+
{
6+
internal class EndRegionDirective : PreprocessorDirective<EndRegionDirectiveTriviaSyntax>
7+
{
8+
public EndRegionDirective(Context cx, EndRegionDirectiveTriviaSyntax trivia)
9+
: base(cx, trivia)
10+
{
11+
}
12+
13+
protected override void PopulatePreprocessor(TextWriter trapFile)
14+
{
15+
trapFile.directive_endregions(this);
16+
}
17+
18+
internal static void WriteRegionBlock(Context cx, RegionDirective region, EndRegionDirective endregion)
19+
{
20+
cx.TrapWriter.Writer.regions(region, endregion);
21+
}
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.CodeAnalysis.CSharp.Syntax;
2+
using System.IO;
3+
4+
namespace Semmle.Extraction.CSharp.Entities
5+
{
6+
internal class RegionDirective : PreprocessorDirective<RegionDirectiveTriviaSyntax>
7+
{
8+
public RegionDirective(Context cx, RegionDirectiveTriviaSyntax trivia)
9+
: base(cx, trivia)
10+
{
11+
}
12+
13+
protected override void PopulatePreprocessor(TextWriter trapFile)
14+
{
15+
trapFile.directive_regions(this, trivia.EndOfDirectiveToken.LeadingTrivia.ToString());
16+
}
17+
}
18+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
13
using Microsoft.CodeAnalysis;
24
using Microsoft.CodeAnalysis.CSharp;
35
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -52,5 +54,26 @@ public override void VisitLineDirectiveTrivia(LineDirectiveTriviaSyntax node)
5254
{
5355
new Entities.LineDirective(cx, node);
5456
}
57+
58+
private readonly Stack<Entities.RegionDirective> regionStarts = new Stack<Entities.RegionDirective>();
59+
60+
public override void VisitRegionDirectiveTrivia(RegionDirectiveTriviaSyntax node)
61+
{
62+
var region = new Entities.RegionDirective(cx, node);
63+
regionStarts.Push(region);
64+
}
65+
66+
public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
67+
{
68+
var endregion = new Entities.EndRegionDirective(cx, node);
69+
if (regionStarts.Count == 0)
70+
{
71+
cx.ExtractionError("Couldn't find start region", null,
72+
Extraction.Entities.Location.Create(cx, node.GetLocation()), null, Util.Logging.Severity.Warning);
73+
return;
74+
}
75+
var start = regionStarts.Pop();
76+
Entities.EndRegionDirective.WriteRegionBlock(cx, start, endregion);
77+
}
5578
}
5679
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,5 +655,20 @@ internal static void directive_line_values(this TextWriter trapFile, LineDirecti
655655
{
656656
trapFile.WriteTuple("directive_line_values", directive, line, file);
657657
}
658+
659+
internal static void directive_regions(this TextWriter trapFile, RegionDirective directive, string name)
660+
{
661+
trapFile.WriteTuple("directive_regions", directive, name);
662+
}
663+
664+
internal static void directive_endregions(this TextWriter trapFile, EndRegionDirective directive)
665+
{
666+
trapFile.WriteTuple("directive_endregions", directive);
667+
}
668+
669+
internal static void regions(this TextWriter trapFile, RegionDirective start, EndRegionDirective end)
670+
{
671+
trapFile.WriteTuple("regions", start, end);
672+
}
658673
}
659674
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,30 @@ class NumericLineDirective extends LineDirective {
209209

210210
override string getAPrimaryQlClass() { result = "NumericLineDirective" }
211211
}
212+
213+
/**
214+
* A `#region` directive.
215+
*/
216+
class RegionDirective extends PreprocessorDirective, @directive_region {
217+
/** Gets the name of this region. */
218+
string getName() { directive_regions(this, result) }
219+
220+
/** Gets the closing `#endregion` directive. */
221+
EndRegionDirective getEnd() { regions(this, result) }
222+
223+
override string toString() { result = "#region ..." }
224+
225+
override string getAPrimaryQlClass() { result = "RegionDirective" }
226+
}
227+
228+
/**
229+
* A `#endregion` directive.
230+
*/
231+
class EndRegionDirective extends PreprocessorDirective, @directive_endregion {
232+
/** Gets the opening `#region` directive. */
233+
RegionDirective getStart() { regions(result, this) }
234+
235+
override string toString() { result = "#endregion" }
236+
237+
override string getAPrimaryQlClass() { result = "EndRegionDirective" }
238+
}

csharp/ql/src/semmlecode.csharp.dbscheme

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,19 @@ using_directive_location(
332332
int loc: @location ref);
333333

334334
@preprocessor_directive = @pragma_warning | @pragma_checksum | @directive_define | @directive_undefine | @directive_warning
335-
| @directive_error | @directive_nullable | @directive_line;
335+
| @directive_error | @directive_nullable | @directive_line | @directive_region | @directive_endregion;
336+
337+
directive_regions(
338+
unique int id: @directive_region,
339+
string name: string ref);
340+
341+
directive_endregions(
342+
unique int id: @directive_endregion);
343+
344+
#keyset[start, end]
345+
regions(
346+
unique int start: @directive_region ref,
347+
unique int end: @directive_endregion ref);
336348

337349
directive_lines(
338350
unique int id: @directive_line,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
regionDirectives
2+
| trivia.cs:36:9:36:22 | #region ... | fields | trivia.cs:41:9:41:18 | #endregion |
3+
| trivia.cs:38:9:38:22 | #region ... | nested | trivia.cs:40:9:40:18 | #endregion |
4+
endregions
5+
| trivia.cs:40:9:40:18 | #endregion | trivia.cs:38:9:38:22 | #region ... |
6+
| trivia.cs:41:9:41:18 | #endregion | trivia.cs:36:9:36:22 | #region ... |
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import csharp
2+
3+
query predicate regionDirectives(RegionDirective d, string name, EndRegionDirective end) {
4+
d.getName() = name and
5+
d.getEnd() = end
6+
}
7+
8+
query predicate endregions(EndRegionDirective d, RegionDirective start) { d.getStart() = start }

0 commit comments

Comments
 (0)