mirror of
https://github.com/github/codeql.git
synced 2026-04-27 09:45:15 +02:00
Extract region directives
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using System.IO;
|
||||
|
||||
namespace Semmle.Extraction.CSharp.Entities
|
||||
{
|
||||
internal class EndRegionDirective : PreprocessorDirective<EndRegionDirectiveTriviaSyntax>
|
||||
{
|
||||
public EndRegionDirective(Context cx, EndRegionDirectiveTriviaSyntax trivia)
|
||||
: base(cx, trivia)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void PopulatePreprocessor(TextWriter trapFile)
|
||||
{
|
||||
trapFile.directive_endregions(this);
|
||||
}
|
||||
|
||||
internal static void WriteRegionBlock(Context cx, RegionDirective region, EndRegionDirective endregion)
|
||||
{
|
||||
cx.TrapWriter.Writer.regions(region, endregion);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using System.IO;
|
||||
|
||||
namespace Semmle.Extraction.CSharp.Entities
|
||||
{
|
||||
internal class RegionDirective : PreprocessorDirective<RegionDirectiveTriviaSyntax>
|
||||
{
|
||||
public RegionDirective(Context cx, RegionDirectiveTriviaSyntax trivia)
|
||||
: base(cx, trivia)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void PopulatePreprocessor(TextWriter trapFile)
|
||||
{
|
||||
trapFile.directive_regions(this, trivia.EndOfDirectiveToken.LeadingTrivia.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
@@ -52,5 +54,26 @@ namespace Semmle.Extraction.CSharp.Populators
|
||||
{
|
||||
new Entities.LineDirective(cx, node);
|
||||
}
|
||||
|
||||
private readonly Stack<Entities.RegionDirective> regionStarts = new Stack<Entities.RegionDirective>();
|
||||
|
||||
public override void VisitRegionDirectiveTrivia(RegionDirectiveTriviaSyntax node)
|
||||
{
|
||||
var region = new Entities.RegionDirective(cx, node);
|
||||
regionStarts.Push(region);
|
||||
}
|
||||
|
||||
public override void VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node)
|
||||
{
|
||||
var endregion = new Entities.EndRegionDirective(cx, node);
|
||||
if (regionStarts.Count == 0)
|
||||
{
|
||||
cx.ExtractionError("Couldn't find start region", null,
|
||||
Extraction.Entities.Location.Create(cx, node.GetLocation()), null, Util.Logging.Severity.Warning);
|
||||
return;
|
||||
}
|
||||
var start = regionStarts.Pop();
|
||||
Entities.EndRegionDirective.WriteRegionBlock(cx, start, endregion);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -655,5 +655,20 @@ namespace Semmle.Extraction.CSharp
|
||||
{
|
||||
trapFile.WriteTuple("directive_line_values", directive, line, file);
|
||||
}
|
||||
|
||||
internal static void directive_regions(this TextWriter trapFile, RegionDirective directive, string name)
|
||||
{
|
||||
trapFile.WriteTuple("directive_regions", directive, name);
|
||||
}
|
||||
|
||||
internal static void directive_endregions(this TextWriter trapFile, EndRegionDirective directive)
|
||||
{
|
||||
trapFile.WriteTuple("directive_endregions", directive);
|
||||
}
|
||||
|
||||
internal static void regions(this TextWriter trapFile, RegionDirective start, EndRegionDirective end)
|
||||
{
|
||||
trapFile.WriteTuple("regions", start, end);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user