Skip to content

Commit 4967664

Browse files
committed
Rework global statement extraction without DB scheme change
1 parent a14db7a commit 4967664

File tree

8 files changed

+17
-32
lines changed

8 files changed

+17
-32
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ public override void Populate(TextWriter trapFile)
5151
Overrides(trapFile);
5252
ExtractRefReturn(trapFile, symbol, this);
5353
ExtractCompilerGenerated(trapFile);
54-
55-
if (SymbolEqualityComparer.Default.Equals(symbol, Context.Compilation.GetEntryPoint(System.Threading.CancellationToken.None)))
56-
{
57-
trapFile.entry_methods(this);
58-
}
5954
}
6055

6156
public static new OrdinaryMethod Create(Context cx, IMethodSymbol method) => OrdinaryMethodFactory.Instance.CreateEntityFromSymbol(cx, method);

csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/GlobalStatementsBlock.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ namespace Semmle.Extraction.CSharp.Entities.Statements
77
{
88
internal class GlobalStatementsBlock : Statement
99
{
10+
private readonly Method parent;
11+
1012
private GlobalStatementsBlock(Context cx, Method parent)
11-
: base(cx, StmtKind.BLOCK, parent, 0) { }
13+
: base(cx, StmtKind.BLOCK, parent, 0)
14+
{
15+
this.parent = parent;
16+
}
1217

1318
public override Microsoft.CodeAnalysis.Location ReportingLocation
1419
{
1520
get
1621
{
17-
// We only create a `GlobalStatementsBlock` if there are global statements. This also means that the
18-
// entry point is going to be the generated method around those global statements
19-
return cx.Compilation.GetEntryPoint(System.Threading.CancellationToken.None)
22+
return parent.symbol
2023
?.DeclaringSyntaxReferences
2124
.FirstOrDefault()
2225
?.GetSyntax()
@@ -33,8 +36,6 @@ public static GlobalStatementsBlock Create(Context cx, Method parent)
3336

3437
protected override void PopulateStatement(TextWriter trapFile)
3538
{
36-
trapFile.global_stmt_block(this);
37-
3839
trapFile.stmt_location(this, cx.CreateLocation(ReportingLocation));
3940
}
4041
}

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,6 @@ internal static void methods(this TextWriter trapFile, Method method, string nam
379379
trapFile.WriteTuple("methods", method, name, declType, retType, originalDefinition);
380380
}
381381

382-
internal static void entry_methods(this TextWriter trapFile, Method method)
383-
{
384-
trapFile.WriteTuple("entry_methods", method);
385-
}
386-
387382
internal static void modifiers(this TextWriter trapFile, Label entity, string modifier)
388383
{
389384
trapFile.WriteTuple("modifiers", entity, modifier);
@@ -514,11 +509,6 @@ internal static void stackalloc_array_creation(this TextWriter trapFile, Express
514509
trapFile.WriteTuple("stackalloc_array_creation", array);
515510
}
516511

517-
internal static void global_stmt_block(this TextWriter trapFile, Entities.Statements.GlobalStatementsBlock block)
518-
{
519-
trapFile.WriteTuple("global_stmt_block", block);
520-
}
521-
522512
internal static void stmt_location(this TextWriter trapFile, Statement stmt, Location location)
523513
{
524514
trapFile.WriteTuple("stmt_location", stmt, location);

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,6 @@ class Method extends Callable, Virtualizable, Attributable, @method {
292292
}
293293

294294
override string getAPrimaryQlClass() { result = "Method" }
295-
296-
/** Holds if this method is the entry method of the compilation. */
297-
predicate isEntry() { entry_methods(this) }
298295
}
299296

300297
/**

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ class BlockStmt extends Stmt, @block_stmt {
7474
predicate isEmpty() { not exists(this.getAStmt()) }
7575

7676
/** Holds if this block is the container of the global statements. */
77-
predicate isGlobalStatementContainer() { global_stmt_block(this) }
77+
predicate isGlobalStatementContainer() {
78+
this.getEnclosingCallable().hasQualifiedName("<Program>$.<Main>$")
79+
}
7880

7981
override Stmt stripSingletonBlocks() {
8082
if getNumberOfStmts() = 1

csharp/ql/src/semmle/code/csharp/commons/Util.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import csharp
55
/** A `Main` method. */
66
class MainMethod extends Method {
77
MainMethod() {
8-
this.hasName("Main") and
8+
(
9+
this.hasName("Main")
10+
or
11+
this.hasQualifiedName("<Program>$.<Main>$")
12+
) and
913
this.isStatic() and
1014
(this.getReturnType() instanceof VoidType or this.getReturnType() instanceof IntType) and
1115
if this.getNumberOfParameters() = 1

csharp/ql/src/semmlecode.csharp.dbscheme

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -843,9 +843,6 @@ local_function_stmts(
843843
unique int fn: @local_function_stmt ref,
844844
int stmt: @local_function ref);
845845

846-
entry_methods(
847-
unique int id: @method ref);
848-
849846
/** VARIABLES **/
850847

851848
@variable = @local_scope_variable | @field;
@@ -975,8 +972,6 @@ case @stmt.kind of
975972

976973
@goto_any_stmt = @goto_default_stmt | @goto_case_stmt | @goto_stmt;
977974

978-
global_stmt_block(
979-
unique int id: @block_stmt ref);
980975

981976
stmt_location(
982977
unique int id: @stmt ref,

csharp/ql/test/library-tests/csharp9/globalStmt.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import csharp
2+
private import semmle.code.csharp.commons.Util
23

34
query predicate global_stmt(Stmt stmt) { stmt.isGlobal() }
45

@@ -11,5 +12,5 @@ query predicate globalBlock(BlockStmt block, Method m, Parameter p, Type t) {
1112

1213
query predicate methods(Method m, string entry) {
1314
m.getFile().getStem() = "GlobalStmt" and
14-
if m.isEntry() then entry = "entry" else entry = "non-entry"
15+
if m instanceof MainMethod then entry = "entry" else entry = "non-entry"
1516
}

0 commit comments

Comments
 (0)