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

Skip to content

Commit 3740aba

Browse files
committed
Extract undef directives
1 parent 9b40514 commit 3740aba

7 files changed

Lines changed: 50 additions & 1 deletion

File tree

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 UndefineDirective : PreprocessorDirective<UndefDirectiveTriviaSyntax>
7+
{
8+
public UndefineDirective(Context cx, UndefDirectiveTriviaSyntax trivia)
9+
: base(cx, trivia)
10+
{
11+
}
12+
13+
protected override void PopulatePreprocessor(TextWriter trapFile)
14+
{
15+
trapFile.directive_undefines(this, trivia.Name.ToString());
16+
}
17+
}
18+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,10 @@ public override void VisitDefineDirectiveTrivia(DefineDirectiveTriviaSyntax node
2727
{
2828
new Entities.DefineDirective(cx, node);
2929
}
30+
31+
public override void VisitUndefDirectiveTrivia(UndefDirectiveTriviaSyntax node)
32+
{
33+
new Entities.UndefineDirective(cx, node);
34+
}
3035
}
3136
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,5 +625,10 @@ internal static void directive_defines(this TextWriter trapFile, DefineDirective
625625
{
626626
trapFile.WriteTuple("directive_defines", directive, name);
627627
}
628+
629+
internal static void directive_undefines(this TextWriter trapFile, UndefineDirective directive, string name)
630+
{
631+
trapFile.WriteTuple("directive_undefines", directive, name);
632+
}
628633
}
629634
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,15 @@ class DefineDirective extends PreprocessorDirective, @directive_define {
5858

5959
override string getAPrimaryQlClass() { result = "DefineDirective" }
6060
}
61+
62+
/**
63+
* An `#undef` directive.
64+
*/
65+
class UndefineDirective extends PreprocessorDirective, @directive_undefine {
66+
/** Gets the name of the preprocessor symbol that is being unset by this directive. */
67+
string getName() { directive_undefines(this, result) }
68+
69+
override string toString() { result = "#undef ..." }
70+
71+
override string getAPrimaryQlClass() { result = "UndefineDirective" }
72+
}

csharp/ql/src/semmlecode.csharp.dbscheme

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,11 @@ using_directive_location(
331331
unique int id: @using_directive ref,
332332
int loc: @location ref);
333333

334-
@preprocessor_directive = @pragma_warning | @pragma_checksum | @directive_define;
334+
@preprocessor_directive = @pragma_warning | @pragma_checksum | @directive_define | @directive_undefine;
335+
336+
directive_undefines(
337+
unique int id: @directive_undefine,
338+
string name: string ref);
335339

336340
directive_defines(
337341
unique int id: @directive_define,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| trivia.cs:6:1:6:12 | #undef ... | DEBUG |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import csharp
2+
3+
from UndefineDirective d
4+
select d, d.getName()

0 commit comments

Comments
 (0)