Merge pull request #12370 from michaelnebel/csharp/unsafemember

C#: Improve the `unsafe` predicate on Modifiable.
This commit is contained in:
Michael Nebel
2023-03-08 13:47:59 +01:00
committed by GitHub
5 changed files with 33 additions and 10 deletions

View File

@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The `unsafe` predicate for `Modifiable` has been extended to cover delegate return types and identify pointer like types at any nest level. This is relevant for `unsafe` declarations extracted from assemblies.

View File

@@ -98,10 +98,21 @@ class Modifiable extends Declaration, @modifiable {
/** Holds if this declaration is `unsafe`. */
predicate isUnsafe() {
this.hasModifier("unsafe") or
this.(Parameterizable).getAParameter().getType() instanceof PointerType or
this.(Property).getType() instanceof PointerType or
this.(Callable).getReturnType() instanceof PointerType
this.hasModifier("unsafe")
or
exists(Type t, Type child |
t = this.(Parameterizable).getAParameter().getType() or
t = this.(Property).getType() or
t = this.(Callable).getReturnType() or
t = this.(DelegateType).getReturnType()
|
child = t.getAChild*() and
(
child instanceof PointerType
or
child instanceof FunctionPointerType
)
)
}
/** Holds if this declaration is `async`. */

View File

@@ -132,11 +132,11 @@ abstract private class GeneratedType extends Type, GeneratedElement {
else (
not this instanceof DelegateType and
result =
this.stubAttributes() + stubAccessibility(this) + this.stubAbstractModifier() +
this.stubStaticModifier() + this.stubPartialModifier() + this.stubKeyword() + " " +
this.getUndecoratedName() + stubGenericArguments(this) + this.stubBaseTypesString() +
stubTypeParametersConstraints(this) + "\n{\n" + this.stubPrivateConstructor() +
this.stubMembers(assembly) + "}\n\n"
this.stubAttributes() + stubUnsafe(this) + stubAccessibility(this) +
this.stubAbstractModifier() + this.stubStaticModifier() + this.stubPartialModifier() +
this.stubKeyword() + " " + this.getUndecoratedName() + stubGenericArguments(this) +
this.stubBaseTypesString() + stubTypeParametersConstraints(this) + "\n{\n" +
this.stubPrivateConstructor() + this.stubMembers(assembly) + "}\n\n"
or
result =
this.stubAttributes() + stubUnsafe(this) + stubAccessibility(this) + this.stubKeyword() +

File diff suppressed because one or more lines are too long

View File

@@ -172,6 +172,14 @@ namespace Test
static explicit IInterface2<Class11>.operator int(Class11 n) => 0;
}
public unsafe class MyUnsafeClass
{
public static void M1(delegate*<void> f) => throw null;
public static void M2(int*[] x) => throw null;
public static char* M3() => throw null;
public static void M4(int x) => throw null;
}
public enum Enum1
{
None1,