C#: Add output assembly to compilation

This commit is contained in:
Tamas Vajk
2021-01-08 09:35:57 +01:00
parent 7418c05594
commit 5cfa900828
10 changed files with 39 additions and 8 deletions

View File

@@ -21,9 +21,9 @@ namespace Semmle.Extraction.CSharp.Entities
protected override void Populate(TextWriter trapFile)
{
Extraction.Entities.Assembly.CreateOutputAssembly(cx);
var assembly = Extraction.Entities.Assembly.CreateOutputAssembly(cx);
trapFile.compilations(this, FileUtils.ConvertToUnix(cwd));
trapFile.compilations(this, FileUtils.ConvertToUnix(cwd), assembly);
// Arguments
var index = 0;

View File

@@ -106,9 +106,9 @@ namespace Semmle.Extraction.CSharp
trapFile.WriteTuple("compilation_time", compilation, num, index, metric);
}
internal static void compilations(this TextWriter trapFile, Compilation compilation, string cwd)
internal static void compilations(this TextWriter trapFile, Compilation compilation, string cwd, Assembly assembly)
{
trapFile.WriteTuple("compilations", compilation, cwd);
trapFile.WriteTuple("compilations", compilation, cwd, assembly);
}
internal static void compiler_generated(this TextWriter trapFile, IEntity entity)

View File

@@ -58,7 +58,8 @@ namespace Semmle.Extraction.Entities
}
private static readonly object outputAssemblyCacheKey = new object();
public static Location CreateOutputAssembly(Context cx)
public static Assembly CreateOutputAssembly(Context cx)
{
if (cx.Extractor.OutputPath == null)
throw new InternalError("Attempting to create the output assembly in standalone extraction mode");

View File

@@ -14,6 +14,7 @@
import File
private import Attribute
private import semmle.code.csharp.commons.Compilation
/**
* A location of a program element.
@@ -171,6 +172,9 @@ class Assembly extends Location, Attributable, @assembly {
/** Gets the version of this assembly. */
Version getVersion() { assemblies(this, _, _, _, result) }
/** Gets the compilation producing this assembly if any. */
Compilation getCompilation() { compilations(result, _, this) }
override File getFile() { assemblies(this, result, _, _, _) }
override string toString() { result = this.getFullName() }

View File

@@ -7,7 +7,10 @@ class Compilation extends @compilation {
string toString() { result = "compilation" }
/** Gets the directory in which this compilation was run, as a string. */
string getDirectoryString() { compilations(this, result) }
string getDirectoryString() { compilations(this, result, _) }
/** Gets the output assembly. */
Assembly getOutputAssembly() { compilations(this, _, result) }
/** Gets the folder in which this compilation was run. */
Folder getFolder() { result.getAbsolutePath() = getDirectoryString() }

View File

@@ -7,11 +7,13 @@
* csc f1.cs f2.cs f3.cs
*
* The `id` simply identifies the invocation, while `cwd` is the working
* directory from which the compiler was invoked.
* directory from which the compiler was invoked. The `assembly` identifies
* the output assembly of the compilation.
*/
compilations(
unique int id : @compilation,
string cwd : string ref
string cwd : string ref,
int assembly: @assembly ref
);
/**

View File

@@ -0,0 +1,9 @@
| Assembly1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | no compilation |
| Locations, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | has compilation |
| System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 | no compilation |
| System.Console, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a | no compilation |
| System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 | no compilation |
| System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e | no compilation |
| System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a | no compilation |
| System.Runtime.Extensions, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a | no compilation |
| mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 | no compilation |

View File

@@ -0,0 +1,8 @@
import csharp
private string hasCompilation(Assembly a) {
if exists(a.getCompilation()) then result = "has compilation" else result = "no compilation"
}
from Assembly a
select a.getFullName(), hasCompilation(a)

View File

@@ -35,3 +35,5 @@ references
| compilation | mscorlib.dll |
timings
| compilation |
assembly
| compilation | Program.dll:0:0:0:0 | Program, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null |

View File

@@ -39,3 +39,5 @@ query predicate timings(Compilation c) {
c.getCpuSeconds() > 0 and
c.getElapsedSeconds() > 0
}
query predicate assembly(Compilation c, Assembly a) { c.getOutputAssembly() = a }