Skip to content

Commit 842ed62

Browse files
authored
Merge pull request github#4927 from tamasvajk/feature/comp-assembly
C#: Add output assembly to compilation
2 parents 4229f55 + ec669c8 commit 842ed62

File tree

19 files changed

+6454
-2606
lines changed

19 files changed

+6454
-2606
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private void DoAnalyseCompilation(string cwd, string[] args)
232232
var projectLayout = layout.LookupProjectOrDefault(transformedAssemblyPath);
233233
var trapWriter = projectLayout.CreateTrapWriter(Logger, transformedAssemblyPath, options.TrapCompression, discardDuplicates: false);
234234
compilationTrapFile = trapWriter; // Dispose later
235-
var cx = extractor.CreateContext(compilation.Clone(), trapWriter, new AssemblyScope(assembly, assemblyPath, true), AddAssemblyTrapPrefix);
235+
var cx = extractor.CreateContext(compilation.Clone(), trapWriter, new AssemblyScope(assembly, assemblyPath), AddAssemblyTrapPrefix);
236236

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

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

292292
foreach (var module in assembly.Modules)
293293
{

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ public Compilation(Context cx, string cwd, string[] args) : base(cx)
2121

2222
protected override void Populate(TextWriter trapFile)
2323
{
24-
Extraction.Entities.Assembly.CreateOutputAssembly(cx);
24+
var assembly = Extraction.Entities.Assembly.CreateOutputAssembly(cx);
2525

2626
trapFile.compilations(this, FileUtils.ConvertToUnix(cwd));
27+
trapFile.compilation_assembly(this, assembly);
2728

2829
// Arguments
2930
var index = 0;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ internal static void compilations(this TextWriter trapFile, Compilation compilat
111111
trapFile.WriteTuple("compilations", compilation, cwd);
112112
}
113113

114+
internal static void compilation_assembly(this TextWriter trapFile, Compilation compilation, Assembly assembly)
115+
{
116+
trapFile.WriteTuple("compilation_assembly", compilation, assembly);
117+
}
118+
114119
internal static void compiler_generated(this TextWriter trapFile, IEntity entity)
115120
{
116121
trapFile.WriteTuple("compiler_generated", entity);

csharp/extractor/Semmle.Extraction/AssemblyScope.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@ public class AssemblyScope : IExtractionScope
1010
private readonly IAssemblySymbol assembly;
1111
private readonly string filepath;
1212

13-
public AssemblyScope(IAssemblySymbol symbol, string path, bool isOutput)
13+
public AssemblyScope(IAssemblySymbol symbol, string path)
1414
{
1515
assembly = symbol;
1616
filepath = path;
17-
IsGlobalScope = isOutput;
1817
}
1918

20-
public bool IsGlobalScope { get; }
21-
2219
public bool InFileScope(string path) => path == filepath;
2320

2421
public bool InScope(ISymbol symbol) =>

csharp/extractor/Semmle.Extraction/Context.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,6 @@ public Context(IExtractor e, Compilation c, TrapWriter trapWriter, IExtractionSc
244244

245245
public bool FromSource => scope.FromSource;
246246

247-
public bool IsGlobalContext => scope.IsGlobalScope;
248-
249247
public ICommentGenerator CommentGenerator { get; } = new CommentProcessor();
250248

251249
private IExtractionScope scope { get; }

csharp/extractor/Semmle.Extraction/Entities/Assembly.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public override void Populate(TextWriter trapFile)
3535
}
3636
}
3737

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

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

6160
private static readonly object outputAssemblyCacheKey = new object();
62-
public static Location CreateOutputAssembly(Context cx)
61+
62+
public static Assembly CreateOutputAssembly(Context cx)
6363
{
6464
if (cx.Extractor.OutputPath == null)
6565
throw new InternalError("Attempting to create the output assembly in standalone extraction mode");

csharp/extractor/Semmle.Extraction/IExtractionScope.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ public interface IExtractionScope
2323
/// <param name="path">The path to populate.</param>
2424
bool InFileScope(string path);
2525

26-
bool IsGlobalScope { get; }
27-
2826
bool FromSource { get; }
2927
}
3028
}

csharp/extractor/Semmle.Extraction/SourceScope.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public SourceScope(SyntaxTree tree)
1616
SourceTree = tree;
1717
}
1818

19-
public bool IsGlobalScope => false;
20-
2119
public bool InFileScope(string path) => path == SourceTree.FilePath;
2220

2321
public bool InScope(ISymbol symbol) => symbol.Locations.Any(loc => loc.SourceTree == SourceTree);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import File
1616
private import Attribute
17+
private import semmle.code.csharp.commons.Compilation
1718

1819
/**
1920
* A ___location of a program element.
@@ -171,6 +172,9 @@ class Assembly extends Location, Attributable, @assembly {
171172
/** Gets the version of this assembly. */
172173
Version getVersion() { assemblies(this, _, _, _, result) }
173174

175+
/** Gets the compilation producing this assembly, if any. */
176+
Compilation getCompilation() { compilation_assembly(result, this) }
177+
174178
override File getFile() { assemblies(this, result, _, _, _) }
175179

176180
override string toString() { result = this.getFullName() }

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class Compilation extends @compilation {
99
/** Gets the directory in which this compilation was run, as a string. */
1010
string getDirectoryString() { compilations(this, result) }
1111

12+
/** Gets the output assembly. */
13+
Assembly getOutputAssembly() { compilation_assembly(this, result) }
14+
1215
/** Gets the folder in which this compilation was run. */
1316
Folder getFolder() { result.getAbsolutePath() = getDirectoryString() }
1417

0 commit comments

Comments
 (0)