Skip to content

Commit 88734f1

Browse files
committed
C#: Fix label conflicts.
C#: Remove unnecessary code from Property.
1 parent a7cdf52 commit 88734f1

File tree

13 files changed

+68
-65
lines changed

13 files changed

+68
-65
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public override void Populate()
5858
}
5959
else
6060
{
61-
Context.ModelError(symbol, "Undhandled accessor kind");
61+
Context.ModelError(symbol, "Unhandled accessor kind");
6262
return;
6363
}
6464

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public override IId Id
118118
if (symbol.IsStatic) tb.Append("static");
119119
tb.Append(ContainingType);
120120
AddParametersToId(Context, tb, symbol);
121-
tb.Append("; constructor");
121+
tb.Append(";constructor");
122122
});
123123
}
124124
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ public override void Populate()
3030
Context.Emit(Tuples.events(this, symbol.GetName(), ContainingType, type.TypeRef, Create(Context, symbol.OriginalDefinition)));
3131

3232
var adder = symbol.AddMethod;
33-
if (adder != null)
33+
var remover = symbol.RemoveMethod;
34+
35+
if (!(adder is null))
3436
EventAccessor.Create(Context, adder);
3537

36-
var remover = symbol.RemoveMethod;
37-
if (remover != null)
38+
if (!(remover is null))
3839
EventAccessor.Create(Context, remover);
3940

4041
ExtractModifiers();

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

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,21 @@ public override void Populate()
2121
var getter = symbol.GetMethod;
2222
var setter = symbol.SetMethod;
2323

24-
if (getter == null && setter == null)
24+
if (getter is null && setter is null)
2525
Context.ModelError(symbol, "No indexer accessor defined");
2626

27-
if (getter != null)
28-
{
29-
Getter = Accessor.Create(Context, getter);
30-
}
27+
if (!(getter is null))
28+
Method.Create(Context, getter);
3129

32-
if (setter != null)
33-
{
34-
Setter = Accessor.Create(Context, setter);
35-
}
30+
if (!(setter is null))
31+
Method.Create(Context, setter);
3632

3733
for (var i = 0; i < symbol.Parameters.Length; ++i)
3834
{
3935
var original = Parameter.Create(Context, symbol.OriginalDefinition.Parameters[i], OriginalDefinition);
4036
Parameter.Create(Context, symbol.Parameters[i], this, original);
4137
}
4238

43-
if (getter != null)
44-
{
45-
Getter = Accessor.Create(Context, getter);
46-
Context.Emit(Tuples.accessors(Getter, 1, getter.Name, this, Getter.OriginalDefinition));
47-
48-
Context.Emit(Tuples.accessor_location(Getter, Getter.Location));
49-
Getter.Overrides();
50-
Getter.ExtractModifiers();
51-
}
52-
53-
if (setter != null)
54-
{
55-
Setter = Accessor.Create(Context, setter);
56-
Context.Emit(Tuples.accessors(Setter, 2, setter.Name, this, Setter.OriginalDefinition));
57-
58-
Context.Emit(Tuples.accessor_location(Setter, Setter.Location));
59-
Setter.Overrides();
60-
Setter.ExtractModifiers();
61-
}
62-
6339
if (IsSourceDeclaration)
6440
{
6541
var expressionBody = ExpressionBody;

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,21 +256,28 @@ public static Method Create(Context cx, IMethodSymbol methodDecl)
256256
{
257257
if (methodDecl == null) return null;
258258

259-
switch (methodDecl.MethodKind)
259+
var methodKind = methodDecl.MethodKind;
260+
261+
if(methodKind == MethodKind.ExplicitInterfaceImplementation)
262+
{
263+
// Retrieve the original method kind
264+
methodKind = methodDecl.ExplicitInterfaceImplementations.Select(m => m.MethodKind).FirstOrDefault();
265+
}
266+
267+
switch (methodKind)
260268
{
261269
case MethodKind.StaticConstructor:
262270
case MethodKind.Constructor:
263271
return Constructor.Create(cx, methodDecl);
264272
case MethodKind.ReducedExtension:
265273
case MethodKind.Ordinary:
266274
case MethodKind.DelegateInvoke:
267-
case MethodKind.ExplicitInterfaceImplementation:
268275
return OrdinaryMethod.Create(cx, methodDecl);
269276
case MethodKind.Destructor:
270277
return Destructor.Create(cx, methodDecl);
271278
case MethodKind.PropertyGet:
272279
case MethodKind.PropertySet:
273-
return Accessor.Create(cx, methodDecl);
280+
return methodDecl.AssociatedSymbol is null ? OrdinaryMethod.Create(cx, methodDecl) : (Method)Accessor.Create(cx, methodDecl);
274281
case MethodKind.EventAdd:
275282
case MethodKind.EventRemove:
276283
return EventAccessor.Create(cx, methodDecl);

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Semmle.Extraction.CSharp.Entities
44
{
5-
class Namespace : CachedEntity<INamespaceSymbol>
5+
sealed class Namespace : CachedEntity<INamespaceSymbol>
66
{
77
Namespace(Context cx, INamespaceSymbol init)
88
: base(cx, init) { }
@@ -41,5 +41,14 @@ class NamespaceFactory : ICachedEntityFactory<INamespaceSymbol, Namespace>
4141
}
4242

4343
public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel;
44+
45+
public override int GetHashCode() => QualifiedName.GetHashCode();
46+
47+
string QualifiedName => symbol.ToDisplayString();
48+
49+
public override bool Equals(object obj)
50+
{
51+
return obj is Namespace ns && QualifiedName == ns.QualifiedName;
52+
}
4453
}
4554
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ public override IId Id
2424
}
2525
}
2626

27-
protected Accessor Getter { get; set; }
28-
protected Accessor Setter { get; set; }
29-
3027
public override void Populate()
3128
{
3229
ExtractAttributes();
@@ -37,12 +34,13 @@ public override void Populate()
3734
Context.Emit(Tuples.properties(this, symbol.GetName(), ContainingType, type.TypeRef, Create(Context, symbol.OriginalDefinition)));
3835

3936
var getter = symbol.GetMethod;
40-
if (getter != null)
41-
Getter = Accessor.Create(Context, getter);
42-
4337
var setter = symbol.SetMethod;
44-
if (setter != null)
45-
Setter = Accessor.Create(Context, setter);
38+
39+
if (!(getter is null))
40+
Method.Create(Context, getter);
41+
42+
if (!(setter is null))
43+
Method.Create(Context, setter);
4644

4745
var declSyntaxReferences = IsSourceDeclaration ?
4846
symbol.DeclaringSyntaxReferences.
@@ -100,7 +98,9 @@ public override Microsoft.CodeAnalysis.Location FullLocation
10098

10199
public static Property Create(Context cx, IPropertySymbol prop)
102100
{
103-
return prop.IsIndexer ? Indexer.Create(cx, prop) : PropertyFactory.Instance.CreateEntity(cx, prop);
101+
bool isIndexer = prop.IsIndexer || prop.ExplicitInterfaceImplementations.Any(e => e.IsIndexer);
102+
103+
return isIndexer ? Indexer.Create(cx, prop) : PropertyFactory.Instance.CreateEntity(cx, prop);
104104
}
105105

106106
public void VisitDeclaration(Context cx, PropertyDeclarationSyntax p)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ protected void ExtractType()
157157
var param = invokeMethod.Parameters[i];
158158
var originalParam = invokeMethod.OriginalDefinition.Parameters[i];
159159
var originalParamEntity = Equals(param, originalParam) ? null :
160-
DelegateTypeParameter.Create(Context, originalParam, Create(Context, ((INamedTypeSymbol)symbol).ConstructedFrom));
160+
DelegateTypeParameter.Create(Context, originalParam, Create(Context, ((INamedTypeSymbol)symbol).OriginalDefinition));
161161
DelegateTypeParameter.Create(Context, param, this, originalParamEntity);
162162
}
163163

csharp/extractor/Semmle.Extraction/Context.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,22 @@ public bool GetOrAddCachedLabel(ICachedEntity entity)
5757
return true;
5858
}
5959

60-
var id = entity.Id;
60+
entity.Label = label = new Label(NewId());
61+
entityLabelCache[entity] = label;
6162

62-
if (id == null)
63-
throw new InternalError("Attempt to create a null entity for {0}", entity.GetType());
63+
var id = entity.Id;
64+
DefineLabel(label, id);
6465

65-
// Look up the Id in the idLabelCache
66-
if (idLabelCache.TryGetValue(id, out label))
66+
#if DEBUG_LABELS
67+
if (idLabelCache.TryGetValue(id, out var originalEntity))
6768
{
68-
entity.Label = label;
69-
entityLabelCache[entity] = label;
70-
return true;
69+
Extractor.Message(new Message { message = "Label collision for " + id.ToString(), severity = Severity.Warning });
7170
}
72-
73-
entity.Label = label = new Label(NewId());
74-
DefineLabel(label, id);
75-
entityLabelCache[entity] = label;
76-
idLabelCache[id] = label;
71+
else
72+
{
73+
idLabelCache[id] = entity;
74+
}
75+
#endif
7776
return false;
7877
}
7978

@@ -111,7 +110,7 @@ public void AddFreshLabel(IEntity entity)
111110
entity.Label = label;
112111
}
113112

114-
readonly Dictionary<IId, Label> idLabelCache = new Dictionary<IId, Label>();
113+
readonly Dictionary<IId, ICachedEntity> idLabelCache = new Dictionary<IId, ICachedEntity>();
115114
readonly Dictionary<ICachedEntity, Label> entityLabelCache = new Dictionary<ICachedEntity, Label>();
116115
readonly HashSet<Label> extractedGenerics = new HashSet<Label>();
117116

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public override IId Id
6969
get
7070
{
7171
return assemblyPath == null
72-
? new Key(assembly, ";assembly")
73-
: new Key(assembly, "#file:///", assemblyPath.Replace("\\", "/"), ";assembly");
72+
? new Key(assembly, ";sourcefile")
73+
: new Key(assembly, "#file:///", assemblyPath.Replace("\\", "/"), ";sourcefile");
7474
}
7575
}
7676
}

0 commit comments

Comments
 (0)