Extract pragma warning directives

This commit is contained in:
Tamas Vajk
2021-01-19 15:52:12 +01:00
parent 40186db768
commit 8b9c6712d1
10 changed files with 157 additions and 1 deletions

View File

@@ -380,6 +380,7 @@ namespace Semmle.Extraction.CSharp
var csNode = (CSharpSyntaxNode)root;
csNode.Accept(new CompilationUnitVisitor(cx));
csNode.Accept(new DirectiveVisitor(cx));
cx.PopulateAll();
CommentPopulator.ExtractCommentBlocks(cx, cx.CommentGenerator);
cx.PopulateAll();

View File

@@ -0,0 +1,4 @@
namespace Semmle.Extraction.CSharp.Entities
{
internal interface IPreprocessorDirective { }
}

View File

@@ -0,0 +1,43 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Semmle.Extraction.Entities;
using System.IO;
namespace Semmle.Extraction.CSharp.Entities
{
internal class PragmaWarningDirective : FreshEntity, IPreprocessorDirective
{
private readonly PragmaWarningDirectiveTriviaSyntax trivia;
public PragmaWarningDirective(Context cx, PragmaWarningDirectiveTriviaSyntax trivia)
: base(cx)
{
this.trivia = trivia;
TryPopulate();
}
protected override void Populate(TextWriter trapFile)
{
trapFile.pragma_warnings(this, trivia.DisableOrRestoreKeyword.IsKind(SyntaxKind.DisableKeyword) ? 0 : 1);
var childIndex = 0;
foreach (var code in trivia.ErrorCodes)
{
trapFile.pragma_warning_error_codes(this, code.ToString(), childIndex++);
}
trapFile.preprocessor_directive_location(this, cx.Create(ReportingLocation));
if (!cx.Extractor.Standalone)
{
var assembly = Assembly.CreateOutputAssembly(cx);
trapFile.preprocessor_directive_assembly(this, assembly);
}
}
public sealed override Microsoft.CodeAnalysis.Location ReportingLocation => trivia.GetLocation();
public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.OptionalLabel;
}
}

View File

@@ -0,0 +1,21 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Semmle.Extraction.CSharp.Populators
{
internal class DirectiveVisitor : CSharpSyntaxWalker
{
private readonly Context cx;
public DirectiveVisitor(Context cx) : base(SyntaxWalkerDepth.StructuredTrivia)
{
this.cx = cx;
}
public override void VisitPragmaWarningDirectiveTrivia(PragmaWarningDirectiveTriviaSyntax node)
{
new Entities.PragmaWarningDirective(cx, node);
}
}
}

View File

@@ -590,5 +590,25 @@ namespace Semmle.Extraction.CSharp
{
trapFile.WriteTuple("using_static_directives", @using, type);
}
internal static void preprocessor_directive_location(this TextWriter trapFile, IPreprocessorDirective directive, Location location)
{
trapFile.WriteTuple("preprocessor_directive_location", directive, location);
}
internal static void preprocessor_directive_assembly(this TextWriter trapFile, IPreprocessorDirective directive, Assembly assembly)
{
trapFile.WriteTuple("preprocessor_directive_assembly", directive, assembly);
}
internal static void pragma_warnings(this TextWriter trapFile, PragmaWarningDirective pragma, int kind)
{
trapFile.WriteTuple("pragma_warnings", pragma, kind);
}
internal static void pragma_warning_error_codes(this TextWriter trapFile, PragmaWarningDirective pragma, string errorCode, int child)
{
trapFile.WriteTuple("pragma_warning_error_codes", pragma, errorCode, child);
}
}
}