Skip to content

Commit d73b28e

Browse files
committed
C#: Address review comments.
Add more tests for duplicated entities, and fix some duplicated entities. Update the TupleTypes output - some extraneous results gone so it's probably better.
1 parent 6a54a6d commit d73b28e

File tree

6 files changed

+31
-31
lines changed

6 files changed

+31
-31
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public override IId Id
3131
}
3232
}
3333

34-
public static Namespace Create(Context cx, INamespaceSymbol ns) => NamespaceFactory.Instance.CreateEntity(cx, ns);
34+
public static Namespace Create(Context cx, INamespaceSymbol ns) => NamespaceFactory.Instance.CreateEntity2(cx, ns);
3535

3636
class NamespaceFactory : ICachedEntityFactory<INamespaceSymbol, Namespace>
3737
{

csharp/extractor/Semmle.Extraction/Context.cs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,47 @@ public Entity CreateEntity<Type, Entity>(ICachedEntityFactory<Type, Entity> fact
6161
/// <param name="factory">The entity factory.</param>
6262
/// <param name="init">The initializer for the entity.</param>
6363
/// <returns>The new/existing entity.</returns>
64-
public Entity CreateEntity2<Type, Entity>(ICachedEntityFactory<Type, Entity> factory, Type type) where Entity : ICachedEntity
64+
public Entity CreateEntity2<Type, Entity>(ICachedEntityFactory<Type, Entity> factory, Type init) where Entity : ICachedEntity
6565
{
6666
using (StackGuard)
6767
{
68-
var entity = factory.Create(this, type);
68+
var entity = factory.Create(this, init);
6969

70-
if(entityLabelCache.TryGetValue(entity, out var label))
70+
if (entityLabelCache.TryGetValue(entity, out var label))
7171
{
7272
entity.Label = label;
73-
return entity;
7473
}
7574
else
7675
{
7776
var id = entity.Id;
77+
#if DEBUG_LABELS
78+
CheckEntityHasUniqueLabel(id, entity);
79+
#endif
7880
label = new Label(NewId());
7981
entity.Label = label;
8082
entityLabelCache[entity] = label;
8183
DefineLabel(label, id);
8284
if (entity.NeedsPopulation)
83-
Populate(null, entity);
84-
return entity;
85+
Populate(init as ISymbol, entity);
8586
}
87+
return entity;
8688
}
8789
}
8890

91+
#if DEBUG_LABELS
92+
private void CheckEntityHasUniqueLabel(IId id, ICachedEntity entity)
93+
{
94+
if (idLabelCache.TryGetValue(id, out var originalEntity))
95+
{
96+
Extractor.Message(new Message { message = "Label collision for " + id.ToString(), severity = Severity.Warning });
97+
}
98+
else
99+
{
100+
idLabelCache[id] = entity;
101+
}
102+
}
103+
#endif
104+
89105
private Entity CreateNonNullEntity<Type, Entity>(ICachedEntityFactory<Type, Entity> factory, Type init) where Entity : ICachedEntity
90106
{
91107
if (objectEntityCache.TryGetValue(init, out var cached))
@@ -103,14 +119,7 @@ private Entity CreateNonNullEntity<Type, Entity>(ICachedEntityFactory<Type, Enti
103119
DefineLabel(label, id);
104120

105121
#if DEBUG_LABELS
106-
if (idLabelCache.TryGetValue(id, out var originalEntity))
107-
{
108-
Extractor.Message(new Message { message = "Label collision for " + id.ToString(), severity = Severity.Warning });
109-
}
110-
else
111-
{
112-
idLabelCache[id] = entity;
113-
}
122+
CheckEntityHasUniqueLabel(id, entity);
114123
#endif
115124

116125
if (entity.NeedsPopulation)
@@ -154,7 +163,9 @@ public void AddFreshLabel(IEntity entity)
154163
entity.Label = label;
155164
}
156165

166+
#if DEBUG_LABELS
157167
readonly Dictionary<IId, ICachedEntity> idLabelCache = new Dictionary<IId, ICachedEntity>();
168+
#endif
158169
readonly Dictionary<object, ICachedEntity> objectEntityCache = new Dictionary<object, ICachedEntity>();
159170
readonly Dictionary<ICachedEntity, Label> entityLabelCache = new Dictionary<ICachedEntity, Label>();
160171
readonly HashSet<Label> extractedGenerics = new HashSet<Label>();

csharp/extractor/Semmle.Extraction/Entities/Folder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public override void Populate()
3939
public override IId Id => new Key(DatabasePath, ";folder");
4040

4141
public static Folder Create(Context cx, DirectoryInfo folder) =>
42-
FolderFactory.Instance.CreateEntity(cx, folder);
42+
FolderFactory.Instance.CreateEntity2(cx, folder);
4343

4444
public override Microsoft.CodeAnalysis.Location ReportingLocation => null;
4545

csharp/extractor/Semmle.Extraction/Semmle.Extraction.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
88
</PropertyGroup>
99

10+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
11+
<DefineConstants>TRACE;DEBUG;DEBUG_LABELS</DefineConstants>
12+
</PropertyGroup>
13+
1014
<ItemGroup>
1115
<PackageReference Include="Microsoft.CodeAnalysis" Version="2.8.0" />
1216
<PackageReference Include="GitInfo" Version="2.0.18" />

csharp/ql/test/library-tests/assignables/Assignables.expected

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@
5858
| Assignables.cs:87:10:87:17 | Item1 |
5959
| Assignables.cs:87:10:87:17 | Property |
6060
| Assignables.cs:87:20:87:26 | Item2 |
61-
| Assignables.cs:87:32:87:32 | Item1 |
62-
| Assignables.cs:87:35:87:35 | Item2 |
6361
| Assignables.cs:88:10:88:10 | Item1 |
6462
| Assignables.cs:88:10:88:10 | x |
6563
| Assignables.cs:88:13:88:18 | Item2 |
@@ -69,13 +67,9 @@
6967
| Assignables.cs:88:17:88:17 | x |
7068
| Assignables.cs:88:24:88:24 | Item1 |
7169
| Assignables.cs:88:27:88:36 | Item2 |
72-
| Assignables.cs:88:28:88:32 | Item1 |
73-
| Assignables.cs:88:35:88:35 | Item2 |
7470
| Assignables.cs:89:10:89:10 | Item1 |
7571
| Assignables.cs:89:10:89:10 | b |
7672
| Assignables.cs:89:13:89:18 | Item2 |
77-
| Assignables.cs:89:24:89:27 | Item1 |
78-
| Assignables.cs:89:30:89:35 | Item2 |
7973
| Assignables.cs:92:6:92:8 | Item1 |
8074
| Assignables.cs:92:11:92:24 | Item2 |
8175
| Assignables.cs:92:12:92:15 | Item1 |

csharp/ql/test/library-tests/csharp7/TupleTypes.expected

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,24 @@
2525
| (Int32,Int32) | (int, int) | ValueTuple<int, int> | 2 | 1 | CSharp7.cs:112:19:112:20 | m5 |
2626
| (Int32,Int32) | (int, int) | ValueTuple<int, int> | 2 | 1 | CSharp7.cs:114:27:114:32 | m9 |
2727
| (Int32,Int32,Int32) | (int, int, int) | ValueTuple<int, int, int> | 3 | 0 | CSharp7.cs:75:10:75:10 | x |
28-
| (Int32,Int32,Int32) | (int, int, int) | ValueTuple<int, int, int> | 3 | 0 | CSharp7.cs:75:28:75:28 | Item1 |
2928
| (Int32,Int32,Int32) | (int, int, int) | ValueTuple<int, int, int> | 3 | 1 | CSharp7.cs:75:13:75:13 | y |
30-
| (Int32,Int32,Int32) | (int, int, int) | ValueTuple<int, int, int> | 3 | 1 | CSharp7.cs:75:31:75:31 | Item2 |
3129
| (Int32,Int32,Int32) | (int, int, int) | ValueTuple<int, int, int> | 3 | 2 | CSharp7.cs:75:16:75:22 | Item3 |
32-
| (Int32,Int32,Int32) | (int, int, int) | ValueTuple<int, int, int> | 3 | 2 | CSharp7.cs:75:34:75:34 | Item3 |
33-
| (Int32,String) | (int, string) | ValueTuple<int, string> | 2 | 0 | CSharp7.cs:97:19:97:19 | Item1 |
3430
| (Int32,String) | (int, string) | ValueTuple<int, string> | 2 | 0 | CSharp7.cs:284:41:284:48 | Key |
3531
| (Int32,String) | (int, string) | ValueTuple<int, string> | 2 | 0 | CSharp7.cs:286:19:286:23 | a |
36-
| (Int32,String) | (int, string) | ValueTuple<int, string> | 2 | 1 | CSharp7.cs:97:22:97:37 | Item2 |
3732
| (Int32,String) | (int, string) | ValueTuple<int, string> | 2 | 1 | CSharp7.cs:284:51:284:60 | Value |
3833
| (Int32,String) | (int, string) | ValueTuple<int, string> | 2 | 1 | CSharp7.cs:286:26:286:33 | b |
3934
| (String,(Int32,Int32)) | (string, (int, int)) | ValueTuple<string, (int, int)> | 2 | 0 | CSharp7.cs:109:10:109:15 | m1 |
40-
| (String,(Int32,Int32)) | (string, (int, int)) | ValueTuple<string, (int, int)> | 2 | 0 | CSharp7.cs:109:29:109:37 | Item1 |
4135
| (String,(Int32,Int32)) | (string, (int, int)) | ValueTuple<string, (int, int)> | 2 | 0 | CSharp7.cs:112:10:112:11 | m3 |
4236
| (String,(Int32,Int32)) | (string, (int, int)) | ValueTuple<string, (int, int)> | 2 | 0 | CSharp7.cs:114:10:114:15 | m7 |
4337
| (String,(Int32,Int32)) | (string, (int, int)) | ValueTuple<string, (int, int)> | 2 | 1 | CSharp7.cs:109:18:109:23 | m2 |
44-
| (String,(Int32,Int32)) | (string, (int, int)) | ValueTuple<string, (int, int)> | 2 | 1 | CSharp7.cs:109:40:109:45 | Item2 |
4538
| (String,(Int32,Int32)) | (string, (int, int)) | ValueTuple<string, (int, int)> | 2 | 1 | CSharp7.cs:112:14:112:21 | Item2 |
4639
| (String,(Int32,Int32)) | (string, (int, int)) | ValueTuple<string, (int, int)> | 2 | 1 | CSharp7.cs:114:18:114:33 | Item2 |
4740
| (String,Int32) | (string, int) | ValueTuple<string, int> | 2 | 0 | CSharp7.cs:79:14:79:14 | i |
4841
| (String,Int32) | (string, int) | ValueTuple<string, int> | 2 | 0 | CSharp7.cs:79:23:79:24 | Item1 |
4942
| (String,Int32) | (string, int) | ValueTuple<string, int> | 2 | 0 | CSharp7.cs:84:17:84:17 | a |
50-
| (String,Int32) | (string, int) | ValueTuple<string, int> | 2 | 0 | CSharp7.cs:98:23:98:38 | Item1 |
5143
| (String,Int32) | (string, int) | ValueTuple<string, int> | 2 | 1 | CSharp7.cs:79:17:79:17 | j |
5244
| (String,Int32) | (string, int) | ValueTuple<string, int> | 2 | 1 | CSharp7.cs:79:27:79:27 | x |
5345
| (String,Int32) | (string, int) | ValueTuple<string, int> | 2 | 1 | CSharp7.cs:84:23:84:23 | Item2 |
54-
| (String,Int32) | (string, int) | ValueTuple<string, int> | 2 | 1 | CSharp7.cs:98:41:98:41 | Item2 |
5546
| (String,String) | (string, string) | ValueTuple<string, string> | 2 | 0 | CSharp7.cs:89:19:89:27 | Item1 |
5647
| (String,String) | (string, string) | ValueTuple<string, string> | 2 | 0 | CSharp7.cs:90:10:90:15 | t2 |
5748
| (String,String) | (string, string) | ValueTuple<string, string> | 2 | 1 | CSharp7.cs:89:30:89:33 | Item2 |

0 commit comments

Comments
 (0)