Skip to content

Commit f365477

Browse files
committed
C#: Address review comments and update test output.
1 parent 32756cd commit f365477

File tree

9 files changed

+151
-148
lines changed

9 files changed

+151
-148
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Attribute.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,20 @@ private void ExtractArguments(TextWriter trapFile)
137137

138138
public override bool NeedsPopulation => true;
139139

140-
private static void ExtractAttributes<T>(Context cx, T symbol, Func<T, IEnumerable<AttributeData>> getAttributes, IEntity entity, AttributeKind kind) where T : ISymbol
140+
private static void ExtractAttributes(Context cx, IEnumerable<AttributeData> attributes, IEntity entity, AttributeKind kind)
141141
{
142-
foreach (var attribute in getAttributes(symbol))
142+
foreach (var attribute in attributes)
143143
{
144144
Create(cx, attribute, entity, kind);
145145
}
146146
}
147147

148148
public static void ExtractAttributes(Context cx, ISymbol symbol, IEntity entity)
149149
{
150-
ExtractAttributes(cx, symbol, s => s.GetAttributes(), entity, AttributeKind.Default);
150+
ExtractAttributes(cx, symbol.GetAttributes(), entity, AttributeKind.Default);
151151
if (symbol is IMethodSymbol method)
152152
{
153-
ExtractAttributes(cx, method, s => s.GetReturnTypeAttributes(), entity, AttributeKind.Return);
153+
ExtractAttributes(cx, method.GetReturnTypeAttributes(), entity, AttributeKind.Return);
154154
}
155155
}
156156

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,18 @@ private void ExtractTypeDeclaration(BaseTypeDeclarationSyntax node)
8080
Entities.Type.Create(Cx, Cx.GetModel(node).GetDeclaredSymbol(node)).ExtractRecursive(TrapFile, Parent);
8181
}
8282

83-
private static Entities.AttributeKind ExtractGlobalTarget(AttributeListSyntax node) =>
84-
node.Target?.Identifier.Kind() switch
85-
{
86-
SyntaxKind.AssemblyKeyword => Entities.AttributeKind.Assembly,
87-
SyntaxKind.ModuleKeyword => Entities.AttributeKind.Module,
88-
_ => throw new InternalError(node, "Unhandled global target")
89-
};
90-
9183
public override void VisitAttributeList(AttributeListSyntax node)
9284
{
9385
if (Cx.Extractor.Mode.HasFlag(ExtractorMode.Standalone))
9486
return;
9587

9688
var outputAssembly = Assembly.CreateOutputAssembly(Cx);
97-
var kind = ExtractGlobalTarget(node);
89+
var kind = node.Target?.Identifier.Kind() switch
90+
{
91+
SyntaxKind.AssemblyKeyword => Entities.AttributeKind.Assembly,
92+
SyntaxKind.ModuleKeyword => Entities.AttributeKind.Module,
93+
_ => throw new InternalError(node, "Unhandled global target")
94+
};
9895
foreach (var attribute in node.Attributes)
9996
{
10097
if (attributeLookup.Value(attribute) is AttributeData attributeData)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal static void array_element_type(this TextWriter trapFile, ArrayType arra
3333
trapFile.WriteTuple("array_element_type", array, dimension, rank, elementType);
3434

3535
internal static void attributes(this TextWriter trapFile, Attribute attribute, AttributeKind kind, Type attributeType, IEntity entity) =>
36-
trapFile.WriteTuple("attributes", attribute, (int)kind, attributeType, entity);
36+
trapFile.WriteTuple("attributes", attribute, kind, attributeType, entity);
3737

3838
internal static void attribute_location(this TextWriter trapFile, Attribute attribute, Location location) =>
3939
trapFile.WriteTuple("attribute_location", attribute, location);

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ class Attributable extends @attributable {
3636
}
3737
}
3838

39+
private string getAttributeName(Attribute a) {
40+
exists(string type | type = a.getType().getName() |
41+
if type.matches("%Attribute") then result = type.prefix(type.length() - 9) else result = type
42+
)
43+
}
44+
3945
/**
4046
* An attribute, for example `[...]` on line 1 in
4147
*
@@ -88,12 +94,7 @@ class Attribute extends TopLevelExprParent, @attribute {
8894

8995
override Location getALocation() { attribute_location(this, result) }
9096

91-
override string toString() {
92-
exists(string type, string name | type = this.getType().getName() |
93-
(if type.matches("%Attribute") then name = type.prefix(type.length() - 9) else name = type) and
94-
result = "[" + name + "(...)]"
95-
)
96-
}
97+
override string toString() { result = "[" + getAttributeName(this) + "(...)]" }
9798

9899
override string getAPrimaryQlClass() { result = "Attribute" }
99100
}
@@ -117,6 +118,8 @@ class DefaultAttribute extends Attribute, @attribute_default {
117118
* ```
118119
*/
119120
class ReturnAttribute extends Attribute, @attribute_return {
121+
override string toString() { result = "[return: " + getAttributeName(this) + "(...)]" }
122+
120123
override string getAPrimaryQlClass() { result = "ReturnAttribute" }
121124
}
122125

@@ -127,6 +130,8 @@ class ReturnAttribute extends Attribute, @attribute_return {
127130
* ```
128131
*/
129132
class AssemblyAttribute extends Attribute, @attribute_assembly {
133+
override string toString() { result = "[assembly: " + getAttributeName(this) + "(...)]" }
134+
130135
override string getAPrimaryQlClass() { result = "AssemblyAttribute" }
131136
}
132137

@@ -137,5 +142,7 @@ class AssemblyAttribute extends Attribute, @attribute_assembly {
137142
* ```
138143
*/
139144
class ModuleAttribute extends Attribute, @attribute_module {
145+
override string toString() { result = "[module: " + getAttributeName(this) + "(...)]" }
146+
140147
override string getAPrimaryQlClass() { result = "ModuleAttribute" }
141148
}

csharp/ql/lib/upgrades/ba2201248071b2bf0bb52909b35014091d2e18a6/attribute_kind.ql

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class TypeOrRef extends @type_or_ref {
1010
string toString() { none() }
1111
}
1212

13-
query predicate add_default_kind(Attribute id, int kind, TypeOrRef type_id, Attributable target) {
14-
kind = 0 and
15-
attributes(id, type_id, target)
16-
}
13+
from Attribute id, TypeOrRef type_id, Attributable target
14+
where attributes(id, type_id, target)
15+
select id, 0, type_id, target

0 commit comments

Comments
 (0)