Skip to content

Commit c67e441

Browse files
committed
C#: Implement compilation_referencing_files and update comments for C#.
1 parent 1a99f4f commit c67e441

File tree

6 files changed

+61
-24
lines changed

6 files changed

+61
-24
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Microsoft.CodeAnalysis;
2+
using System;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Linq;
@@ -28,6 +29,13 @@ public Compilation(Context cx, string cwd, string[] args) : base(cx)
2829
cx.Emit(Tuples.compilation_compiling_files(this, index++, file));
2930
}
3031

32+
// References
33+
index = 0;
34+
foreach(var file in cx.Compilation.References.OfType<PortableExecutableReference>().Select(r => Extraction.Entities.File.Create(cx, r.FilePath)))
35+
{
36+
cx.Emit(Tuples.compilation_referencing_files(this, index++, file));
37+
}
38+
3139
// Diagnostics
3240
index = 0;
3341
foreach(var diag in cx.Compilation.GetDiagnostics().Select(d => new Diagnostic(cx, d)))

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ internal static class Tuples
4545

4646
internal static Tuple compilation_compiling_files(Compilation compilation, int index, File file) => new Tuple("compilation_compiling_files", compilation, index, file);
4747

48+
internal static Tuple compilation_referencing_files(Compilation compilation, int index, File file) => new Tuple("compilation_referencing_files", compilation, index, file);
49+
4850
internal static Tuple compilation_finished(Compilation compilation, float cpuSeconds, float elapsedSeconds) => new Tuple("compilation_finished", compilation, cpuSeconds, elapsedSeconds);
4951

5052
internal static Tuple compilation_time(Compilation compilation, int num, int index, float metric) => new Tuple("compilation_time", compilation, num, index, metric);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ class Compilation extends @compilation {
2424
/** Gets a source file compiled in this compilation. */
2525
File getAFileCompiled() { result = getFileCompiled(_) }
2626

27+
/** Gets the `i`th reference in this compilation. */
28+
File getReference(int i) { compilation_referencing_files(this, i, result) }
29+
30+
/** Gets a reference in this compilation. */
31+
File getAReference() { result = getReference(_) }
32+
2733
/** Gets a diagnostic associated with this compilation. */
2834
Diagnostic getADiagnostic() { result.getCompilation() = this }
2935

csharp/ql/src/semmlecode.csharp.dbscheme

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,12 @@
44
* compiled per invocation. For example, this command compiles three
55
* source files:
66
*
7-
* gcc -c f1.c f2.c f3.c
7+
* csc f1.cs f2.cs f3.cs
88
*
99
* The `id` simply identifies the invocation, while `cwd` is the working
1010
* directory from which the compiler was invoked.
1111
*/
1212
compilations(
13-
/**
14-
* An invocation of the compiler. Note that more than one file may
15-
* be compiled per invocation. For example, this command compiles
16-
* three source files:
17-
*
18-
* gcc -c f1.c f2.c f3.c
19-
*/
2013
unique int id : @compilation,
2114
string cwd : string ref
2215
);
@@ -25,19 +18,18 @@ compilations(
2518
* The arguments that were passed to the extractor for a compiler
2619
* invocation. If `id` is for the compiler invocation
2720
*
28-
* gcc -c f1.c f2.c f3.c
21+
* csc f1.cs f2.cs f3.cs
2922
*
3023
* then typically there will be rows for
3124
*
3225
* num | arg
3326
* --- | ---
34-
* 0 | *path to extractor*
35-
* 1 | `--mimic`
36-
* 2 | `/usr/bin/gcc`
37-
* 3 | `-c`
38-
* 4 | f1.c
39-
* 5 | f2.c
40-
* 6 | f3.c
27+
* 0 | --compiler
28+
* 1 | *path to compiler*
29+
* 2 | --cil
30+
* 3 | f1.cs
31+
* 4 | f2.cs
32+
* 5 | f3.cs
4133
*/
4234
#keyset[id, num]
4335
compilation_args(
@@ -50,18 +42,15 @@ compilation_args(
5042
* The source files that are compiled by a compiler invocation.
5143
* If `id` is for the compiler invocation
5244
*
53-
* gcc -c f1.c f2.c f3.c
45+
* csc f1.cs f2.cs f3.cs
5446
*
5547
* then there will be rows for
5648
*
5749
* num | arg
5850
* --- | ---
59-
* 0 | f1.c
60-
* 1 | f2.c
61-
* 2 | f3.c
62-
*
63-
* Note that even if those files `#include` headers, those headers
64-
* do not appear as rows.
51+
* 0 | f1.cs
52+
* 1 | f2.cs
53+
* 2 | f3.cs
6554
*/
6655
#keyset[id, num]
6756
compilation_compiling_files(
@@ -70,6 +59,27 @@ compilation_compiling_files(
7059
int file : @file ref
7160
);
7261

62+
/**
63+
* The references used by a compiler invocation.
64+
* If `id` is for the compiler invocation
65+
*
66+
* csc f1.cs f2.cs f3.cs /r:ref1.dll /r:ref2.dll /r:ref3.dll
67+
*
68+
* then there will be rows for
69+
*
70+
* num | arg
71+
* --- | ---
72+
* 0 | ref1.dll
73+
* 1 | ref2.dll
74+
* 2 | ref3.dll
75+
*/
76+
#keyset[id, num]
77+
compilation_referencing_files(
78+
int id : @compilation ref,
79+
int num : int ref,
80+
int file : @file ref
81+
);
82+
7383
/**
7484
* The time taken by the extractor for a compiler invocation.
7585
*

csharp/ql/test/library-tests/compilations/Compilations.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,10 @@ diagnosticElements
2828
| Program.cs:7:13:7:13 | CS0219: The variable 'x' is assigned but its value is never used | Program.cs:7:13:7:13 | x |
2929
| Program.cs:9:13:9:13 | CS0219: The variable 'y' is assigned but its value is never used | Program.cs:9:13:9:13 | access to local variable y |
3030
| Program.cs:9:13:9:13 | CS0219: The variable 'y' is assigned but its value is never used | Program.cs:9:13:9:13 | y |
31+
references
32+
| compilation | 0 | System.Core.dll |
33+
| compilation | 1 | System.Console.dll |
34+
| compilation | 2 | mscorlib.dll |
35+
| compilation | 3 | System.Runtime.dll |
36+
| compilation | 4 | System.dll |
37+
| compilation | 5 | System.Private.CoreLib.dll |

csharp/ql/test/library-tests/compilations/Compilations.ql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ query predicate compilationFolder(Compilation c, string folder) {
3030
}
3131

3232
query predicate diagnosticElements(Diagnostic d, Element e) { e = d.getElement() }
33+
34+
query predicate references(Compilation c, int i, string reference) {
35+
reference = c.getReference(i).getBaseName()
36+
}

0 commit comments

Comments
 (0)