Skip to content

Commit abf6be6

Browse files
committed
C#: Avoid qualifying explicit interface implementations.
1 parent 9ec0c8f commit abf6be6

File tree

6 files changed

+43
-20
lines changed

6 files changed

+43
-20
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,21 +204,21 @@ protected static void AddSignatureTypeToId(Context cx, TextWriter trapFile, IMet
204204
type.BuildTypeId(cx, trapFile, false, symbolBeingDefined, (cx0, tb0, type0, g) => AddSignatureTypeToId(cx, tb0, method, type0, g));
205205
}
206206

207-
protected static void AddParametersToId(Context cx, TextWriter trapFile, IMethodSymbol method, ISymbol generic)
207+
protected static void AddParametersToId(Context cx, TextWriter trapFile, IMethodSymbol method, ISymbol symbolBeingDefined)
208208
{
209209
trapFile.Write('(');
210210
int index = 0;
211211

212212
if (method.MethodKind == MethodKind.ReducedExtension)
213213
{
214214
trapFile.WriteSeparator(",", ref index);
215-
AddSignatureTypeToId(cx, trapFile, method, method.ReceiverType, generic);
215+
AddSignatureTypeToId(cx, trapFile, method, method.ReceiverType, symbolBeingDefined);
216216
}
217217

218218
foreach (var param in method.Parameters)
219219
{
220220
trapFile.WriteSeparator(",", ref index);
221-
AddSignatureTypeToId(cx, trapFile, method, param.Type, generic);
221+
AddSignatureTypeToId(cx, trapFile, method, param.Type, symbolBeingDefined);
222222
switch (param.RefKind)
223223
{
224224
case RefKind.Out:
@@ -241,9 +241,10 @@ protected static void AddParametersToId(Context cx, TextWriter trapFile, IMethod
241241

242242
public static void AddExplicitInterfaceQualifierToId(Context cx, System.IO.TextWriter trapFile, IEnumerable<ISymbol> explicitInterfaceImplementations)
243243
{
244-
if (explicitInterfaceImplementations.Any())
244+
foreach (var i in explicitInterfaceImplementations)
245245
{
246-
trapFile.AppendList(",", explicitInterfaceImplementations.Select(impl => cx.CreateEntity(impl.ContainingType)));
246+
trapFile.Write(';');
247+
i.ContainingType.BuildNestedTypeId(cx, trapFile, null);
247248
}
248249
}
249250

csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/NamedType.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public override IEnumerable<Extraction.Entities.Location> Locations
111111

112112
public override void WriteId(TextWriter trapFile)
113113
{
114-
symbol.BuildTypeId(Context, trapFile, true, symbol, (cx0, tb0, sub, g) => tb0.WriteSubId(Create(cx0, sub)));
114+
symbol.BuildTypeId(Context, trapFile, true, symbol, (cx0, tb0, sub, _) => tb0.WriteSubId(Create(cx0, sub)));
115115
trapFile.Write(";type");
116116
}
117117

@@ -177,11 +177,7 @@ class NamedTypeRefFactory : ICachedEntityFactory<INamedTypeSymbol, NamedTypeRef>
177177

178178
public override void WriteId(TextWriter trapFile)
179179
{
180-
void WriteType(Context cx, TextWriter trapFile, ITypeSymbol symbol, ISymbol symbolBeingDefined)
181-
{
182-
symbol.BuildTypeId(cx, trapFile, false, symbolBeingDefined, WriteType);
183-
}
184-
WriteType(Context, trapFile, referencedType.symbol, referencedType.symbol);
180+
referencedType.symbol.BuildNestedTypeId(Context, trapFile, referencedType.symbol);
185181
trapFile.Write(";typeRef");
186182
}
187183

csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TupleType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TupleTypeFactory : ICachedEntityFactory<INamedTypeSymbol, TupleType>
3232

3333
public override void WriteId(TextWriter trapFile)
3434
{
35-
symbol.BuildTypeId(Context, trapFile, false, symbol, (cx0, tb0, sub, g) => tb0.WriteSubId(Create(cx0, sub)));
35+
symbol.BuildTypeId(Context, trapFile, false, symbol, (cx0, tb0, sub, _) => tb0.WriteSubId(Create(cx0, sub)));
3636
trapFile.Write(";tuple");
3737
}
3838

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ public static bool ContainsTypeParameters(this ITypeSymbol type, Context cx, ISy
122122
}
123123
}
124124

125+
/// <summary>
126+
/// Write the identifier for the symbol <paramref name="type"/> to the trapfile <paramref name="trapFile"/>.
127+
/// If any nested types are found in the identifier, then they are written out explicitly, without
128+
/// prefixing the assembly ID.
129+
/// </summary>
130+
/// <param name="type">The type to write.</param>
131+
/// <param name="cx">The extraction context.</param>
132+
/// <param name="trapFile">The trap file to write to.</param>
133+
/// <param name="symbolBeingDefined">The outer symbol being defined (to avoid recursive ids).</param>
134+
public static void BuildNestedTypeId(this ITypeSymbol type, Context cx, TextWriter trapFile, ISymbol symbolBeingDefined)
135+
{
136+
void WriteType(Context cx, TextWriter trapFile, ITypeSymbol symbol, ISymbol symbolBeingDefined)
137+
{
138+
symbol.BuildTypeId(cx, trapFile, false, symbolBeingDefined, WriteType);
139+
}
140+
WriteType(cx, trapFile, type, symbolBeingDefined);
141+
}
142+
125143
/// <summary>
126144
/// Constructs a unique string for this type symbol.
127145
///
@@ -130,8 +148,10 @@ public static bool ContainsTypeParameters(this ITypeSymbol type, Context cx, ISy
130148
/// </summary>
131149
/// <param name="cx">The extraction context.</param>
132150
/// <param name="trapFile">The trap builder used to store the result.</param>
151+
/// <param name="prefix">Whether to prefix the type ID with the assembly ID.</param>
152+
/// <param name="symbolBeingDefined">The outer symbol being defined (to avoid recursive ids).</param>
133153
/// <param name="subTermAction">The action to apply to syntactic sub terms of this type.</param>
134-
public static void BuildTypeId(this ITypeSymbol type, Context cx, TextWriter trapFile, bool prefix, ISymbol SymbolBeingDefined, Action<Context, TextWriter, ITypeSymbol, ISymbol> subTermAction)
154+
public static void BuildTypeId(this ITypeSymbol type, Context cx, TextWriter trapFile, bool prefix, ISymbol symbolBeingDefined, Action<Context, TextWriter, ITypeSymbol, ISymbol> subTermAction)
135155
{
136156
if (type.SpecialType != SpecialType.None && !(type is INamedTypeSymbol n && n.IsGenericType))
137157
{
@@ -150,7 +170,7 @@ public static void BuildTypeId(this ITypeSymbol type, Context cx, TextWriter tra
150170
{
151171
case TypeKind.Array:
152172
var array = (IArrayTypeSymbol)type;
153-
subTermAction(cx, trapFile, array.ElementType, SymbolBeingDefined);
173+
subTermAction(cx, trapFile, array.ElementType, symbolBeingDefined);
154174
array.BuildArraySuffix(trapFile);
155175
return;
156176
case TypeKind.Class:
@@ -160,16 +180,16 @@ public static void BuildTypeId(this ITypeSymbol type, Context cx, TextWriter tra
160180
case TypeKind.Delegate:
161181
case TypeKind.Error:
162182
var named = (INamedTypeSymbol)type;
163-
named.BuildNamedTypeId(cx, trapFile, prefix, SymbolBeingDefined, subTermAction);
183+
named.BuildNamedTypeId(cx, trapFile, prefix, symbolBeingDefined, subTermAction);
164184
return;
165185
case TypeKind.Pointer:
166186
var ptr = (IPointerTypeSymbol)type;
167-
subTermAction(cx, trapFile, ptr.PointedAtType, SymbolBeingDefined);
187+
subTermAction(cx, trapFile, ptr.PointedAtType, symbolBeingDefined);
168188
trapFile.Write('*');
169189
return;
170190
case TypeKind.TypeParameter:
171191
var tp = (ITypeParameterSymbol)type;
172-
if (!SymbolEqualityComparer.Default.Equals(tp.ContainingSymbol, SymbolBeingDefined))
192+
if (!SymbolEqualityComparer.Default.Equals(tp.ContainingSymbol, symbolBeingDefined))
173193
{
174194
switch (tp.TypeParameterKind)
175195
{
@@ -179,7 +199,7 @@ public static void BuildTypeId(this ITypeSymbol type, Context cx, TextWriter tra
179199
trapFile.Write('_');
180200
break;
181201
case TypeParameterKind.Type:
182-
subTermAction(cx, trapFile, tp.ContainingType, SymbolBeingDefined);
202+
subTermAction(cx, trapFile, tp.ContainingType, symbolBeingDefined);
183203
trapFile.Write('_');
184204
break;
185205
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
| Program.cs:18:21:18:22 | c1 | Assembly1.dll:0:0:0:0 | Class |
2+
| Program.cs:18:21:18:22 | c1 | Assembly2.dll:0:0:0:0 | Class |
3+
| Program.cs:18:21:18:22 | c1 | Program.cs:10:7:10:11 | Class |
4+
| Program.cs:19:21:19:22 | c2 | Assembly1.dll:0:0:0:0 | Class |
25
| Program.cs:19:21:19:22 | c2 | Assembly2.dll:0:0:0:0 | Class |
6+
| Program.cs:19:21:19:22 | c2 | Program.cs:10:7:10:11 | Class |
7+
| Program.cs:20:15:20:16 | c3 | Assembly1.dll:0:0:0:0 | Class |
8+
| Program.cs:20:15:20:16 | c3 | Assembly2.dll:0:0:0:0 | Class |
39
| Program.cs:20:15:20:16 | c3 | Program.cs:10:7:10:11 | Class |

csharp/ql/test/library-tests/csharp8/NullableRefTypes.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ expressionTypes
241241
| NullableRefTypes.cs:17:29:17:32 | null | null |
242242
| NullableRefTypes.cs:18:31:18:34 | this access | MyClass! |
243243
| NullableRefTypes.cs:19:33:19:36 | this access | MyClass! |
244-
| NullableRefTypes.cs:26:44:26:53 | throw ... | MyClass![]! |
244+
| NullableRefTypes.cs:26:44:26:53 | throw ... | MyClass?[]! |
245245
| NullableRefTypes.cs:26:50:26:53 | null | null |
246-
| NullableRefTypes.cs:27:44:27:53 | throw ... | MyClass![]! |
246+
| NullableRefTypes.cs:27:44:27:53 | throw ... | MyClass?[]! |
247247
| NullableRefTypes.cs:27:50:27:53 | null | null |
248248
| NullableRefTypes.cs:30:21:30:24 | null | null |
249249
| NullableRefTypes.cs:31:20:31:23 | this access | MyClass! |

0 commit comments

Comments
 (0)