Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 599a5b1

Browse files
committed
C#: Make @local_function @modifiable, make LocalFunction extend Modifiable, and extract modifiers for local functions.
1 parent 813e1e7 commit 599a5b1

5 files changed

Lines changed: 34 additions & 1 deletion

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp.Syntax;
3+
using System.Linq;
24

35
namespace Semmle.Extraction.CSharp.Entities
46
{
@@ -39,6 +41,18 @@ class LocalFunctionFactory : ICachedEntityFactory<IMethodSymbol, LocalFunction>
3941
public override void Populate()
4042
{
4143
PopulateMethod();
44+
45+
// There is a "bug" in Roslyn whereby the IMethodSymbol associated with the local function symbol
46+
// is always static, so we need to go to the syntax reference of the local function to see whether
47+
// the "static" modifier is present.
48+
if (symbol.DeclaringSyntaxReferences.SingleOrDefault().GetSyntax() is LocalFunctionStatementSyntax fn)
49+
{
50+
foreach(var modifier in fn.Modifiers)
51+
{
52+
Modifier.HasModifier(Context, this, modifier.Text);
53+
}
54+
}
55+
4256
var originalDefinition = IsSourceDeclaration ? this : Create(Context, symbol.OriginalDefinition);
4357
var returnType = Type.Create(Context, symbol.ReturnType);
4458
Context.Emit(Tuples.local_functions(this, symbol.Name, returnType, originalDefinition));

csharp/ql/src/semmle/code/csharp/Callable.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ class ExplicitConversionOperator extends ConversionOperator {
903903
* }
904904
* ```
905905
*/
906-
class LocalFunction extends Callable, @local_function {
906+
class LocalFunction extends Callable, Modifiable, @local_function {
907907
override string getName() { local_functions(this, result, _, _) }
908908

909909
override LocalFunction getSourceDeclaration() { local_functions(this, _, _, result) }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// semmle-extractor-options: /langversion:8.0
2+
3+
using System;
4+
5+
class StaticLocalFunctions
6+
{
7+
int Fn(int x)
8+
{
9+
static int I(int y) => y;
10+
int J(int y) => x+y;
11+
return I(x) + J(x);
12+
}
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| StaticLocalFunctions.cs:9:9:9:33 | I | static |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import csharp
2+
3+
from LocalFunction fn, string modifier
4+
where fn.hasModifier(modifier)
5+
select fn, modifier

0 commit comments

Comments
 (0)