Skip to content

Commit 1dab159

Browse files
committed
C#: Adjust 'fromSource' to hold only on files passed to the compiler as a source file
1 parent a7cc9f9 commit 1dab159

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ protected File(Context cx, string path)
1717

1818
public override void Populate(TextWriter trapFile)
1919
{
20-
trapFile.files(this, TransformedPath.Value, TransformedPath.NameWithoutExtension, TransformedPath.Extension);
20+
var trees = Context.Compilation.SyntaxTrees.Where(t => t.FilePath == originalPath);
21+
var isSource = trees.Any();
22+
23+
trapFile.files(this, TransformedPath.Value, TransformedPath.NameWithoutExtension, TransformedPath.Extension, isSource ? FileSourceKind.FromSource : FileSourceKind.Unknown);
2124

2225
if (TransformedPath.ParentDirectory is PathTransformer.ITransformedPath dir)
2326
trapFile.containerparent(Extraction.Entities.Folder.Create(Context, dir), this);
2427

25-
var trees = Context.Compilation.SyntaxTrees.Where(t => t.FilePath == originalPath);
26-
27-
if (trees.Any())
28+
if (isSource)
2829
{
2930
foreach (var text in trees.Select(tree => tree.GetText()))
3031
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ private GeneratedFile(Context cx) : base(cx, "") { }
1010

1111
public override void Populate(TextWriter trapFile)
1212
{
13-
trapFile.files(this, "", "", "");
13+
trapFile.files(this, "", "", "", FileSourceKind.Unknown);
1414
}
1515

1616
public override void WriteId(TextWriter trapFile)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Semmle.Extraction
2+
{
3+
public enum FileSourceKind
4+
{
5+
Unknown = 0,
6+
FromSource = 1,
7+
FromLibrary = 2
8+
}
9+
}

csharp/extractor/Semmle.Extraction/Tuples.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ internal static void extractor_messages(this System.IO.TextWriter trapFile, Extr
1818
trapFile.WriteTuple("extractor_messages", error, (int)severity, origin, errorMessage, entityText, location, stackTrace);
1919
}
2020

21-
public static void files(this System.IO.TextWriter trapFile, File file, string fullName, string name, string extension)
21+
public static void files(this System.IO.TextWriter trapFile, File file, string fullName, string name, string extension, FileSourceKind kind)
2222
{
23-
trapFile.WriteTuple("files", file, fullName, name, extension, 0);
23+
trapFile.WriteTuple("files", file, fullName, name, extension, (int)kind);
2424
}
2525

2626
internal static void folders(this System.IO.TextWriter trapFile, Folder folder, string path, string name)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class File extends Container, @file {
192192
override string getURL() { result = "file://" + this.getAbsolutePath() + ":0:0:0:0" }
193193

194194
/** Holds if this file contains source code. */
195-
predicate fromSource() { this.getNumberOfLinesOfCode() > 0 }
195+
predicate fromSource() { files(this, _, _, _, 1) }
196196

197197
/** Holds if this file is a library. */
198198
predicate fromLibrary() {

0 commit comments

Comments
 (0)