Merge pull request #4952 from tamasvajk/feature/type-mention-nullable

C#: Fix type mention extraction of named types with nullability enabled
This commit is contained in:
Tamás Vajk
2021-01-14 11:25:44 +01:00
committed by GitHub
5 changed files with 114 additions and 2 deletions

View File

@@ -497,13 +497,13 @@ namespace Semmle.Extraction.CSharp
/// <summary>
/// Holds if this symbol is a source declaration.
/// </summary>
public static bool IsSourceDeclaration(this ISymbol symbol) => SymbolEqualityComparer.Default.Equals(symbol, symbol.OriginalDefinition);
public static bool IsSourceDeclaration(this ISymbol symbol) => SymbolEqualityComparer.IncludeNullability.Equals(symbol, symbol.OriginalDefinition);
/// <summary>
/// Holds if this method is a source declaration.
/// </summary>
public static bool IsSourceDeclaration(this IMethodSymbol method) =>
IsSourceDeclaration((ISymbol)method) && SymbolEqualityComparer.Default.Equals(method, method.ConstructedFrom) && method.ReducedFrom == null;
IsSourceDeclaration((ISymbol)method) && SymbolEqualityComparer.IncludeNullability.Equals(method, method.ConstructedFrom) && method.ReducedFrom == null;
/// <summary>
/// Holds if this parameter is a source declaration.

View File

@@ -0,0 +1,22 @@
using System;
#nullable disable
public class Base { }
public class Derived : Base { }
public interface I0 { }
public interface I1 : I0 { }
public struct Str : I1 { }
public class C1
{
public void M1()
{
Derived? d0 = new Derived();
Derived d1 = new Derived();
Str? s0 = new Str();
Str s1 = new Str();
I1? i0 = new Str();
I1 i1 = new Str();
}
}

View File

@@ -0,0 +1,24 @@
using System;
#nullable enable
public class Base { }
public class Derived : Base { }
public interface I0 { }
public interface I1 : I0 { }
public struct Str : I1 { }
public class C1
{
public void M1()
{
Derived? d0 = new Derived();
Derived d1 = new Derived();
Str? s0 = new Str();
Str s1 = new Str();
I1? i0 = new Str();
I1 i1 = new Str();
}
}
// semmle-extractor-options: --separate-compilation

View File

@@ -0,0 +1,37 @@
typeMentions
| NullableDisabled.cs:6:24:6:27 | Base |
| NullableDisabled.cs:8:23:8:24 | I0 |
| NullableDisabled.cs:9:21:9:22 | I1 |
| NullableDisabled.cs:13:12:13:15 | Void |
| NullableDisabled.cs:15:9:15:15 | Derived |
| NullableDisabled.cs:15:27:15:33 | Derived |
| NullableDisabled.cs:16:9:16:15 | Derived |
| NullableDisabled.cs:16:26:16:32 | Derived |
| NullableDisabled.cs:17:9:17:11 | Str |
| NullableDisabled.cs:17:9:17:12 | Nullable<Str> |
| NullableDisabled.cs:17:23:17:25 | Str |
| NullableDisabled.cs:18:9:18:11 | Str |
| NullableDisabled.cs:18:22:18:24 | Str |
| NullableDisabled.cs:19:9:19:10 | I1 |
| NullableDisabled.cs:19:22:19:24 | Str |
| NullableDisabled.cs:20:9:20:10 | I1 |
| NullableDisabled.cs:20:21:20:23 | Str |
| NullableEnabled.cs:6:24:6:27 | Base |
| NullableEnabled.cs:8:23:8:24 | I0 |
| NullableEnabled.cs:9:21:9:22 | I1 |
| NullableEnabled.cs:13:12:13:15 | Void |
| NullableEnabled.cs:15:9:15:15 | Derived |
| NullableEnabled.cs:15:27:15:33 | Derived |
| NullableEnabled.cs:16:9:16:15 | Derived |
| NullableEnabled.cs:16:26:16:32 | Derived |
| NullableEnabled.cs:17:9:17:11 | Str |
| NullableEnabled.cs:17:9:17:12 | Nullable<Str> |
| NullableEnabled.cs:17:23:17:25 | Str |
| NullableEnabled.cs:18:9:18:11 | Str |
| NullableEnabled.cs:18:22:18:24 | Str |
| NullableEnabled.cs:19:9:19:10 | I1 |
| NullableEnabled.cs:19:22:19:24 | Str |
| NullableEnabled.cs:20:9:20:10 | I1 |
| NullableEnabled.cs:20:21:20:23 | Str |
duplicates
diff

View File

@@ -0,0 +1,29 @@
import csharp
private predicate matchingLineLoc(TypeMention tm0, TypeMention tm1) {
tm0.getLocation().getStartLine() = tm1.getLocation().getStartLine() and
tm0.getLocation().getEndLine() = tm1.getLocation().getEndLine() and
tm0.getLocation().getStartColumn() = tm1.getLocation().getStartColumn() and
tm0.getLocation().getEndColumn() = tm1.getLocation().getEndColumn()
}
private predicate matchingFile(TypeMention tm0, TypeMention tm1) {
tm0.getLocation().getFile().getStem() = tm1.getLocation().getFile().getStem()
}
query predicate typeMentions(TypeMention tm) { any() }
query predicate duplicates(TypeMention tm) {
exists(TypeMention other |
matchingLineLoc(other, tm) and
matchingFile(other, tm) and
other != tm
)
}
query predicate diff(TypeMention tm) {
not exists(TypeMention other |
matchingLineLoc(other, tm) and
not matchingFile(other, tm)
)
}