Skip to content
Draft
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
11 changes: 5 additions & 6 deletions csharp/extractor/Semmle.Extraction.CSharp/Entities/Assembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,14 @@ public static Assembly CreateOutputAssembly(Context cx)

public override void WriteId(EscapingTextWriter trapFile)
{
if (isOutputAssembly && Context.ExtractionContext.IsStandalone)
if (Context.ExtractionContext.IsStandalone)
{
trapFile.Write("buildlessOutputAssembly");
}
else
{
trapFile.Write(assembly.ToString());
WriteStarId(trapFile);
return;
}

trapFile.Write(assembly.ToString());

if (assemblyPath is not null)
{
trapFile.Write("#file:///");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.IO;

namespace Semmle.Extraction.CSharp.Entities
{
/// <summary>
/// Used to create a single canonical empty location entity.
/// </summary>
public class EmptyLocation : GeneratedLocation
{
private EmptyLocation(Context cx) : base(cx) { }

public override void Populate(TextWriter trapFile)
{
trapFile.locations_default(this, GenFile, -1, -1, -1, -1);
}

public override void WriteId(EscapingTextWriter trapFile)
{
if (Context.ExtractionContext.IsStandalone)
{
WriteStarId(trapFile);
return;
}

trapFile.Write("loc,");
trapFile.WriteSubId(GenFile);
trapFile.Write(",-1,-1,-1,-1");
}

public static new EmptyLocation Create(Context cx) => EmptyLocationFactory.Instance.CreateEntity(cx, typeof(EmptyLocation), null);

private class EmptyLocationFactory : CachedEntityFactory<string?, EmptyLocation>
{
public static EmptyLocationFactory Instance { get; } = new EmptyLocationFactory();

/// <summary>
/// The QL library assumes the presence of a single empty location element.
/// </summary>
public override EmptyLocation Create(Context cx, string? init) => new EmptyLocation(cx);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,29 @@ namespace Semmle.Extraction.CSharp.Entities
{
public class GeneratedLocation : SourceLocation
{
private readonly File generatedFile;
protected File GenFile { get; init; }

private GeneratedLocation(Context cx)
protected GeneratedLocation(Context cx)
: base(cx, null)
{
generatedFile = GeneratedFile.Create(cx);
GenFile = GeneratedFile.Create(cx);
}

public override void Populate(TextWriter trapFile)
{
trapFile.locations_default(this, generatedFile, 0, 0, 0, 0);
trapFile.locations_default(this, GenFile, 0, 0, 0, 0);
}

public override void WriteId(EscapingTextWriter trapFile)
{
if (Context.ExtractionContext.IsStandalone)
{
WriteStarId(trapFile);
return;
}

trapFile.Write("loc,");
trapFile.WriteSubId(generatedFile);
trapFile.WriteSubId(GenFile);
trapFile.Write(",0,0,0,0");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,24 @@ public abstract class Location : CachedEntity<Microsoft.CodeAnalysis.Location?>
{
#nullable restore warnings
protected Location(Context cx, Microsoft.CodeAnalysis.Location? init)
: base(cx, init) { }
: base(cx, init)
{ }

protected static void WriteStarId(EscapingTextWriter writer)
{
writer.Write('*');
}

public sealed override void WriteQuotedId(EscapingTextWriter writer)
{
if (Context.ExtractionContext.IsStandalone)
{
WriteStarId(writer);
return;
}

base.WriteQuotedId(writer);
}

public override Microsoft.CodeAnalysis.Location? ReportingLocation => Symbol;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public File FileEntity

public override void WriteId(EscapingTextWriter trapFile)
{
if (Context.ExtractionContext.IsStandalone)
{
WriteStarId(trapFile);
return;
}

trapFile.Write("loc,");
trapFile.WriteSubId(FileEntity);
trapFile.Write(',');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ private void DoAnalyseCompilation()

compilationEntity = Entities.Compilation.Create(cx);

// Add a single empty location
Entities.EmptyLocation.Create(cx);

ExtractionContext.CompilationInfos.ForEach(ci => trapWriter.Writer.compilation_info(compilationEntity, ci.key, ci.value));

ReportProgressTaskDone(currentTaskId, assemblyPath, trapWriter.TrapFile, stopwatch.Elapsed, AnalysisAction.Extracted);
Expand Down
18 changes: 16 additions & 2 deletions csharp/ql/lib/semmle/code/csharp/Location.qll
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,29 @@ class Location extends @location {
}

/** An empty location. */
class EmptyLocation extends Location {
EmptyLocation() { this.hasLocationInfo("", 0, 0, 0, 0) }
class EmptyLocation extends Location, @location_default {
EmptyLocation() { locations_default(this, _, -1, -1, -1, -1) }

override predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
filepath = "" and
startline = 0 and
startcolumn = 0 and
endline = 0 and
endcolumn = 0
}

override string toString() { result = "empty location" }
}

/**
* A location in source code, comprising of a source file and a segment of text
* within the file.
*/
class SourceLocation extends Location, @location_default {
SourceLocation() { not this instanceof EmptyLocation }

/** Gets the location that takes into account `#line` directives, if any. */
SourceLocation getMappedLocation() {
locations_mapped(this, result) and
Expand Down
Loading