Skip to content

Commit 7d1d2e7

Browse files
committed
C#: Add specialized ql classes for each attribute kind and update AST printing.
1 parent 5a6667e commit 7d1d2e7

File tree

5 files changed

+70
-28
lines changed

5 files changed

+70
-28
lines changed

csharp/ql/lib/semmle/code/csharp/Attribute.qll

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,45 @@ class Attribute extends TopLevelExprParent, @attribute {
9797

9898
override string getAPrimaryQlClass() { result = "Attribute" }
9999
}
100+
101+
/**
102+
* An attribute with default kind, for example `[...]` on line 1 in
103+
* ```csharp
104+
* [MyAttribute(0)]
105+
* int SomeMethod() { return 1; }
106+
* ```
107+
*/
108+
class DefaultAttribute extends Attribute, @attribute_default {
109+
override string getAPrimaryQlClass() { result = "DefaultAttribute" }
110+
}
111+
112+
/**
113+
* An attribute with return kind, for example `[...]` on line 1 in
114+
* ```csharp
115+
* [return: MyAttribute(0)]
116+
* int SomeMethod() { return 1; }
117+
* ```
118+
*/
119+
class ReturnAttribute extends Attribute, @attribute_return {
120+
override string getAPrimaryQlClass() { result = "ReturnAttribute" }
121+
}
122+
123+
/**
124+
* An attribute with assembly kind, for example `[...]` on line 1 in
125+
* ```csharp
126+
* [assembly: MyAttribute(0)]
127+
* ```
128+
*/
129+
class AssemblyAttribute extends Attribute, @attribute_assembly {
130+
override string getAPrimaryQlClass() { result = "AssemblyAttribute" }
131+
}
132+
133+
/**
134+
* An attribute with module kind, for example `[...]` on line 1 in
135+
* ```csharp
136+
* [module: MyAttribute(0)]
137+
* ```
138+
*/
139+
class ModuleAttribute extends Attribute, @attribute_module {
140+
override string getAPrimaryQlClass() { result = "ModuleAttribute" }
141+
}

csharp/ql/test/library-tests/arguments/PrintAst.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,14 @@ arguments.cs:
222222
# 66| 14: [Method] f6
223223
# 66| -1: [TypeMention] Void
224224
#-----| 0: (Attributes)
225-
# 65| 1: [Attribute] [My(...)]
225+
# 65| 1: [DefaultAttribute] [My(...)]
226226
# 65| -1: [TypeMention] MyAttribute
227227
# 65| 0: [BoolLiteral] false
228228
# 66| 4: [BlockStmt] {...}
229229
# 69| 15: [Method] f7
230230
# 69| -1: [TypeMention] Void
231231
#-----| 0: (Attributes)
232-
# 68| 1: [Attribute] [My(...)]
232+
# 68| 1: [DefaultAttribute] [My(...)]
233233
# 68| -1: [TypeMention] MyAttribute
234234
# 68| 0: [BoolLiteral] true
235235
# 68| 1: [StringLiteral] ""

csharp/ql/test/library-tests/attributes/PrintAst.expected

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
attributes.cs:
2-
# 10| [Attribute] [AssemblyTitle(...)]
2+
# 10| [AssemblyAttribute] [AssemblyTitle(...)]
33
# 10| -1: [TypeMention] AssemblyTitleAttribute
44
# 10| 0: [StringLiteral] "C# attributes test"
5-
# 11| [Attribute] [AssemblyDescription(...)]
5+
# 11| [AssemblyAttribute] [AssemblyDescription(...)]
66
# 11| -1: [TypeMention] AssemblyDescriptionAttribute
77
# 11| 0: [StringLiteral] "A test of C# attributes"
8-
# 12| [Attribute] [AssemblyConfiguration(...)]
8+
# 12| [AssemblyAttribute] [AssemblyConfiguration(...)]
99
# 12| -1: [TypeMention] AssemblyConfigurationAttribute
1010
# 12| 0: [StringLiteral] ""
11-
# 13| [Attribute] [AssemblyCompany(...)]
11+
# 13| [AssemblyAttribute] [AssemblyCompany(...)]
1212
# 13| -1: [TypeMention] AssemblyCompanyAttribute
1313
# 13| 0: [StringLiteral] "Semmle Plc"
14-
# 14| [Attribute] [AssemblyProduct(...)]
14+
# 14| [AssemblyAttribute] [AssemblyProduct(...)]
1515
# 14| -1: [TypeMention] AssemblyProductAttribute
1616
# 14| 0: [StringLiteral] "Odasa"
17-
# 15| [Attribute] [AssemblyCopyright(...)]
17+
# 15| [AssemblyAttribute] [AssemblyCopyright(...)]
1818
# 15| -1: [TypeMention] AssemblyCopyrightAttribute
1919
# 15| 0: [StringLiteral] "Copyright © Semmle 2018"
20-
# 16| [Attribute] [AssemblyTrademark(...)]
20+
# 16| [AssemblyAttribute] [AssemblyTrademark(...)]
2121
# 16| -1: [TypeMention] AssemblyTrademarkAttribute
2222
# 16| 0: [StringLiteral] ""
23-
# 17| [Attribute] [AssemblyCulture(...)]
23+
# 17| [AssemblyAttribute] [AssemblyCulture(...)]
2424
# 17| -1: [TypeMention] AssemblyCultureAttribute
2525
# 17| 0: [StringLiteral] ""
26-
# 22| [Attribute] [ComVisible(...)]
26+
# 22| [AssemblyAttribute] [ComVisible(...)]
2727
# 22| -1: [TypeMention] ComVisibleAttribute
2828
# 22| 0: [BoolLiteral] false
29-
# 25| [Attribute] [Guid(...)]
29+
# 25| [AssemblyAttribute] [Guid(...)]
3030
# 25| -1: [TypeMention] GuidAttribute
3131
# 25| 0: [StringLiteral] "2f70fdd6-14aa-4850-b053-d547adb1f476"
32-
# 37| [Attribute] [AssemblyVersion(...)]
32+
# 37| [AssemblyAttribute] [AssemblyVersion(...)]
3333
# 37| -1: [TypeMention] AssemblyVersionAttribute
3434
# 37| 0: [StringLiteral] "1.0.0.0"
35-
# 38| [Attribute] [AssemblyFileVersion(...)]
35+
# 38| [AssemblyAttribute] [AssemblyFileVersion(...)]
3636
# 38| -1: [TypeMention] AssemblyFileVersionAttribute
3737
# 38| 0: [StringLiteral] "1.0.0.0"
38-
# 40| [Attribute] [Args(...)]
38+
# 40| [AssemblyAttribute] [Args(...)]
3939
# 40| -1: [TypeMention] ArgsAttribute
4040
# 40| 0: [IntLiteral] 0
4141
# 40| 1: [ArrayCreation] array creation of type Object[]
@@ -64,7 +64,7 @@ attributes.cs:
6464
# 40| 1: [TypeofExpr] typeof(...)
6565
# 40| 0: [TypeAccess] access to type Int32
6666
# 40| 0: [TypeMention] int
67-
# 41| [Attribute] [Args(...)]
67+
# 41| [ModuleAttribute] [Args(...)]
6868
# 41| -1: [TypeMention] ArgsAttribute
6969
# 41| 0: [IntLiteral] 0
7070
# 41| 1: [ArrayCreation] array creation of type Object[]
@@ -95,7 +95,7 @@ attributes.cs:
9595
# 41| 0: [TypeMention] int
9696
# 44| [Class] Foo
9797
#-----| 0: (Attributes)
98-
# 43| 1: [Attribute] [AttributeUsage(...)]
98+
# 43| 1: [DefaultAttribute] [AttributeUsage(...)]
9999
# 43| -1: [TypeMention] AttributeUsageAttribute
100100
# 43| 0: [MemberConstantAccess] access to constant All
101101
# 43| -1: [TypeAccess] access to type AttributeTargets
@@ -105,7 +105,7 @@ attributes.cs:
105105
# 47| 5: [Method] foo
106106
# 47| -1: [TypeMention] Void
107107
#-----| 0: (Attributes)
108-
# 46| 1: [Attribute] [Conditional(...)]
108+
# 46| 1: [DefaultAttribute] [Conditional(...)]
109109
# 46| -1: [TypeMention] ConditionalAttribute
110110
# 46| 0: [StringLiteral] "DEBUG2"
111111
# 47| 4: [BlockStmt] {...}
@@ -116,7 +116,7 @@ attributes.cs:
116116
# 52| 0: [Parameter] x
117117
# 52| -1: [TypeMention] int
118118
#-----| 0: (Attributes)
119-
# 52| 1: [Attribute] [Foo(...)]
119+
# 52| 1: [DefaultAttribute] [Foo(...)]
120120
# 52| 0: [TypeMention] Foo
121121
# 52| 4: [BlockStmt] {...}
122122
# 52| 0: [ReturnStmt] return ...;
@@ -126,19 +126,19 @@ attributes.cs:
126126
# 55| 6: [Method] M1
127127
# 55| -1: [TypeMention] Void
128128
#-----| 0: (Attributes)
129-
# 54| 1: [Attribute] [My(...)]
129+
# 54| 1: [DefaultAttribute] [My(...)]
130130
# 54| -1: [TypeMention] MyAttribute
131131
# 54| 0: [BoolLiteral] false
132132
# 55| 4: [BlockStmt] {...}
133133
# 59| 7: [Method] M2
134134
# 59| -1: [TypeMention] Void
135135
#-----| 0: (Attributes)
136-
# 57| 1: [Attribute] [My(...)]
136+
# 57| 1: [DefaultAttribute] [My(...)]
137137
# 57| -1: [TypeMention] MyAttribute
138138
# 57| 0: [BoolLiteral] true
139139
# 57| 1: [StringLiteral] ""
140140
# 57| 2: [IntLiteral] 0
141-
# 58| 2: [Attribute] [My2(...)]
141+
# 58| 2: [DefaultAttribute] [My2(...)]
142142
# 58| -1: [TypeMention] My2Attribute
143143
# 58| 0: [BoolLiteral] false
144144
# 58| 1: [BoolLiteral] true
@@ -192,7 +192,7 @@ attributes.cs:
192192
# 74| 4: [BlockStmt] {...}
193193
# 78| [Class] X
194194
#-----| 0: (Attributes)
195-
# 77| 1: [Attribute] [Args(...)]
195+
# 77| 1: [DefaultAttribute] [Args(...)]
196196
# 77| -1: [TypeMention] ArgsAttribute
197197
# 77| 0: [IntLiteral] 42
198198
# 77| 1: [NullLiteral] null
@@ -221,7 +221,7 @@ attributes.cs:
221221
# 82| 5: [Method] SomeMethod
222222
# 82| -1: [TypeMention] int
223223
#-----| 0: (Attributes)
224-
# 80| 1: [Attribute] [Args(...)]
224+
# 80| 1: [DefaultAttribute] [Args(...)]
225225
# 80| -1: [TypeMention] ArgsAttribute
226226
# 80| 0: [AddExpr] ... + ...
227227
# 80| 0: [IntLiteral] 42
@@ -248,7 +248,7 @@ attributes.cs:
248248
# 80| 1: [TypeofExpr] typeof(...)
249249
# 80| 0: [TypeAccess] access to type Int32
250250
# 80| 0: [TypeMention] int
251-
# 81| 2: [Attribute] [Args(...)]
251+
# 81| 2: [ReturnAttribute] [Args(...)]
252252
# 81| -1: [TypeMention] ArgsAttribute
253253
# 81| 0: [AddExpr] ... + ...
254254
# 81| 0: [IntLiteral] 42

csharp/ql/test/library-tests/csharp9/PrintAst.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,13 +583,13 @@ LocalFunction.cs:
583583
# 21| 0: [LocalFunctionStmt] dup(...)
584584
# 21| 0: [LocalFunction] dup
585585
#-----| 0: (Attributes)
586-
# 21| 1: [Attribute] [Obsolete(...)]
586+
# 21| 1: [DefaultAttribute] [Obsolete(...)]
587587
# 21| 0: [TypeMention] ObsoleteAttribute
588588
#-----| 2: (Parameters)
589589
# 22| 0: [Parameter] b
590590
# 22| -1: [TypeMention] bool
591591
#-----| 0: (Attributes)
592-
# 22| 1: [Attribute] [NotNullWhen(...)]
592+
# 22| 1: [DefaultAttribute] [NotNullWhen(...)]
593593
# 22| -1: [TypeMention] NotNullWhenAttribute
594594
# 22| 0: [BoolLiteral] true
595595
# 22| 1: [Parameter] i

csharp/ql/test/library-tests/definitions/PrintAst.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ definitions.cs:
462462
# 181| 0: [Parameter] c
463463
# 181| -1: [TypeMention] C6
464464
#-----| 0: (Attributes)
465-
# 181| 1: [Attribute] [My(...)]
465+
# 181| 1: [DefaultAttribute] [My(...)]
466466
# 181| 0: [TypeMention] MyAttribute
467467
# 182| 4: [BlockStmt] {...}
468468
# 183| 0: [ReturnStmt] return ...;

0 commit comments

Comments
 (0)