Skip to content

Commit 2ecf0d3

Browse files
authored
Merge pull request #7550 from michaelnebel/csharp/global-using
C#: Support for identifying whether a using directive is "global".
2 parents bce2a81 + 6c20585 commit 2ecf0d3

File tree

18 files changed

+54320
-47235
lines changed

18 files changed

+54320
-47235
lines changed

csharp/downgrades/193c35b0e3776f8ee9a49e984e393974b4c670f6/old.dbscheme

Lines changed: 2088 additions & 0 deletions
Large diffs are not rendered by default.

csharp/downgrades/193c35b0e3776f8ee9a49e984e393974b4c670f6/semmlecode.csharp.dbscheme

Lines changed: 2084 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
description: Remove 'using_global' relation.
2+
compatibility: backwards
3+
using_global.rel: delete

csharp/downgrades/qlpack.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: codeql/csharp-downgrades
2+
groups: csharp
3+
version: 0.0.6-dev
4+
downgrades: .
5+
library: true

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ protected override void Populate(TextWriter trapFile)
4747
trapFile.using_directive_location(this, Context.CreateLocation(ReportingLocation));
4848
}
4949

50+
if (node.GlobalKeyword.Kind() == SyntaxKind.GlobalKeyword)
51+
{
52+
trapFile.using_global(this);
53+
}
54+
5055
if (parent is not null)
5156
{
5257
trapFile.parent_namespace_declaration(this, parent);

csharp/extractor/Semmle.Extraction.CSharp/SymbolExtensions.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ public static string GetName(this ISymbol symbol, bool useMetadataName = false)
6868
return symbol.CanBeReferencedByName ? name : name.Substring(symbol.Name.LastIndexOf('.') + 1);
6969
}
7070

71+
private static IEnumerable<SyntaxToken> GetModifiers<T>(this ISymbol symbol, Func<T, IEnumerable<SyntaxToken>> getModifierTokens) =>
72+
symbol.DeclaringSyntaxReferences
73+
.Select(r => r.GetSyntax())
74+
.OfType<T>()
75+
.SelectMany(getModifierTokens);
76+
7177
/// <summary>
7278
/// Gets the source-level modifiers belonging to this symbol, if any.
7379
/// </summary>
7480
public static IEnumerable<string> GetSourceLevelModifiers(this ISymbol symbol)
7581
{
76-
var methodModifiers = symbol.DeclaringSyntaxReferences
77-
.Select(r => r.GetSyntax())
78-
.OfType<Microsoft.CodeAnalysis.CSharp.Syntax.BaseMethodDeclarationSyntax>()
79-
.SelectMany(md => md.Modifiers);
80-
var typeModifers = symbol.DeclaringSyntaxReferences
81-
.Select(r => r.GetSyntax())
82-
.OfType<Microsoft.CodeAnalysis.CSharp.Syntax.TypeDeclarationSyntax>()
83-
.SelectMany(cd => cd.Modifiers);
82+
var methodModifiers = symbol.GetModifiers<Microsoft.CodeAnalysis.CSharp.Syntax.BaseMethodDeclarationSyntax>(md => md.Modifiers);
83+
var typeModifers = symbol.GetModifiers<Microsoft.CodeAnalysis.CSharp.Syntax.TypeDeclarationSyntax>(cd => cd.Modifiers);
8484
return methodModifiers.Concat(typeModifers).Select(m => m.Text);
8585
}
8686

@@ -277,10 +277,8 @@ private static void BuildAssembly(IAssemblySymbol asm, EscapingTextWriter trapFi
277277
trapFile.Write("::");
278278
}
279279

280-
private static void BuildFunctionPointerTypeId(this IFunctionPointerTypeSymbol funptr, Context cx, EscapingTextWriter trapFile, ISymbol symbolBeingDefined)
281-
{
280+
private static void BuildFunctionPointerTypeId(this IFunctionPointerTypeSymbol funptr, Context cx, EscapingTextWriter trapFile, ISymbol symbolBeingDefined) =>
282281
BuildFunctionPointerSignature(funptr, trapFile, s => s.BuildOrWriteId(cx, trapFile, symbolBeingDefined));
283-
}
284282

285283
private static void BuildNamedTypeId(this INamedTypeSymbol named, Context cx, EscapingTextWriter trapFile, ISymbol symbolBeingDefined, bool constructUnderlyingTupleType)
286284
{
@@ -456,10 +454,8 @@ public static void BuildFunctionPointerSignature(IFunctionPointerTypeSymbol funp
456454
trapFile.Write('>');
457455
}
458456

459-
private static void BuildFunctionPointerTypeDisplayName(this IFunctionPointerTypeSymbol funptr, Context cx, TextWriter trapFile)
460-
{
457+
private static void BuildFunctionPointerTypeDisplayName(this IFunctionPointerTypeSymbol funptr, Context cx, TextWriter trapFile) =>
461458
BuildFunctionPointerSignature(funptr, trapFile, s => s.BuildDisplayName(cx, trapFile));
462-
}
463459

464460
private static void BuildNamedTypeDisplayName(this INamedTypeSymbol namedType, Context cx, TextWriter trapFile, bool constructUnderlyingTupleType)
465461
{

csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs

Lines changed: 145 additions & 427 deletions
Large diffs are not rendered by default.

csharp/ql/lib/semmle/code/csharp/Stmt.qll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -946,17 +946,13 @@ class TryStmt extends Stmt, @try_stmt {
946946
exists(ControlFlowElement mid |
947947
mid = this.getATriedElement() and
948948
not mid instanceof TryStmt and
949-
result = getAChild(mid, mid.getEnclosingCallable())
949+
result = mid.getAChild() and
950+
pragma[only_bind_into](mid.getEnclosingCallable()) =
951+
pragma[only_bind_into](result.getEnclosingCallable())
950952
)
951953
}
952954
}
953955

954-
pragma[noinline]
955-
private ControlFlowElement getAChild(ControlFlowElement cfe, Callable c) {
956-
result = cfe.getAChild() and
957-
c = result.getEnclosingCallable()
958-
}
959-
960956
/**
961957
* A `catch` clause within a `try` statement.
962958
*

csharp/ql/lib/semmle/code/csharp/Using.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class UsingDirective extends Element, @using_directive {
3636
}
3737

3838
override Location getALocation() { using_directive_location(this, result) }
39+
40+
/** Holds if this directive is `global`. */
41+
predicate isGlobal() { using_global(this) }
3942
}
4043

4144
/**

csharp/ql/lib/semmlecode.csharp.dbscheme

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ parent_namespace_declaration(
324324

325325
@using_directive = @using_namespace_directive | @using_static_directive;
326326

327+
using_global(
328+
unique int id: @using_directive ref
329+
);
330+
327331
using_namespace_directives(
328332
unique int id: @using_namespace_directive,
329333
int namespace_id: @namespace ref);

0 commit comments

Comments
 (0)