Skip to content

C#: Add output assembly to compilation #4927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private void DoAnalyseCompilation(string cwd, string[] args)
var projectLayout = layout.LookupProjectOrDefault(transformedAssemblyPath);
var trapWriter = projectLayout.CreateTrapWriter(Logger, transformedAssemblyPath, options.TrapCompression, discardDuplicates: false);
compilationTrapFile = trapWriter; // Dispose later
var cx = extractor.CreateContext(compilation.Clone(), trapWriter, new AssemblyScope(assembly, assemblyPath, true), AddAssemblyTrapPrefix);
var cx = extractor.CreateContext(compilation.Clone(), trapWriter, new AssemblyScope(assembly, assemblyPath), AddAssemblyTrapPrefix);

compilationEntity = new Entities.Compilation(cx, cwd, args);
}
Expand Down Expand Up @@ -287,7 +287,7 @@ private void DoAnalyseReferenceAssembly(PortableExecutableReference r)

if (c.GetAssemblyOrModuleSymbol(r) is IAssemblySymbol assembly)
{
var cx = extractor.CreateContext(c, trapWriter, new AssemblyScope(assembly, assemblyPath, false), AddAssemblyTrapPrefix);
var cx = extractor.CreateContext(c, trapWriter, new AssemblyScope(assembly, assemblyPath), AddAssemblyTrapPrefix);

foreach (var module in assembly.Modules)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public Compilation(Context cx, string cwd, string[] args) : base(cx)

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.compilation_assembly(this, assembly);

// Arguments
var index = 0;
Expand Down
5 changes: 5 additions & 0 deletions csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ internal static void compilations(this TextWriter trapFile, Compilation compilat
trapFile.WriteTuple("compilations", compilation, cwd);
}

internal static void compilation_assembly(this TextWriter trapFile, Compilation compilation, Assembly assembly)
{
trapFile.WriteTuple("compilation_assembly", compilation, assembly);
}

internal static void compiler_generated(this TextWriter trapFile, IEntity entity)
{
trapFile.WriteTuple("compiler_generated", entity);
Expand Down
5 changes: 1 addition & 4 deletions csharp/extractor/Semmle.Extraction/AssemblyScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ public class AssemblyScope : IExtractionScope
private readonly IAssemblySymbol assembly;
private readonly string filepath;

public AssemblyScope(IAssemblySymbol symbol, string path, bool isOutput)
public AssemblyScope(IAssemblySymbol symbol, string path)
{
assembly = symbol;
filepath = path;
IsGlobalScope = isOutput;
}

public bool IsGlobalScope { get; }

public bool InFileScope(string path) => path == filepath;

public bool InScope(ISymbol symbol) =>
Expand Down
2 changes: 0 additions & 2 deletions csharp/extractor/Semmle.Extraction/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,6 @@ public Context(IExtractor e, Compilation c, TrapWriter trapWriter, IExtractionSc

public bool FromSource => scope.FromSource;

public bool IsGlobalContext => scope.IsGlobalScope;

public ICommentGenerator CommentGenerator { get; } = new CommentProcessor();

private IExtractionScope scope { get; }
Expand Down
6 changes: 3 additions & 3 deletions csharp/extractor/Semmle.Extraction/Entities/Assembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public override void Populate(TextWriter trapFile)
}
}

public override bool NeedsPopulation =>
!SymbolEqualityComparer.Default.Equals(assembly, Context.Compilation.Assembly) || !Context.IsGlobalContext;
public override bool NeedsPopulation => true;

public override int GetHashCode() =>
symbol == null ? 91187354 : symbol.GetHashCode();
Expand All @@ -59,7 +58,8 @@ private class AssemblyConstructorFactory : ICachedEntityFactory<Microsoft.CodeAn
}

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");
Expand Down
2 changes: 0 additions & 2 deletions csharp/extractor/Semmle.Extraction/IExtractionScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public interface IExtractionScope
/// <param name="path">The path to populate.</param>
bool InFileScope(string path);

bool IsGlobalScope { get; }

bool FromSource { get; }
}
}
2 changes: 0 additions & 2 deletions csharp/extractor/Semmle.Extraction/SourceScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public SourceScope(SyntaxTree tree)
SourceTree = tree;
}

public bool IsGlobalScope => false;

public bool InFileScope(string path) => path == SourceTree.FilePath;

public bool InScope(ISymbol symbol) => symbol.Locations.Any(loc => loc.SourceTree == SourceTree);
Expand Down
4 changes: 4 additions & 0 deletions csharp/ql/src/semmle/code/csharp/Location.qll
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import File
private import Attribute
private import semmle.code.csharp.commons.Compilation

/**
* A location of a program element.
Expand Down Expand Up @@ -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() { compilation_assembly(result, this) }

override File getFile() { assemblies(this, result, _, _, _) }

override string toString() { result = this.getFullName() }
Expand Down
3 changes: 3 additions & 0 deletions csharp/ql/src/semmle/code/csharp/commons/Compilation.qll
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class Compilation extends @compilation {
/** Gets the directory in which this compilation was run, as a string. */
string getDirectoryString() { compilations(this, result) }

/** Gets the output assembly. */
Assembly getOutputAssembly() { compilation_assembly(this, result) }

/** Gets the folder in which this compilation was run. */
Folder getFolder() { result.getAbsolutePath() = getDirectoryString() }

Expand Down
5 changes: 5 additions & 0 deletions csharp/ql/src/semmlecode.csharp.dbscheme
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ compilation_finished(
float elapsed_seconds : float ref
);

compilation_assembly(
unique int id : @compilation ref,
int assembly: @assembly ref
)

/*
* External artifacts
*/
Expand Down
Loading