Skip to content

Commit 42be9e9

Browse files
authored
Merge pull request #16938 from tamasvajk/feature/extract-files-multiple
C#: Do not skip extraction of already seen source files
2 parents 62c2fe6 + a4e357e commit 42be9e9

File tree

19 files changed

+8514
-36
lines changed

19 files changed

+8514
-36
lines changed

csharp/downgrades/15b989afd2bfc4743536fdb0958c1d8177a32600/old.dbscheme

Lines changed: 2099 additions & 0 deletions
Large diffs are not rendered by default.

csharp/downgrades/15b989afd2bfc4743536fdb0958c1d8177a32600/semmlecode.csharp.dbscheme

Lines changed: 2099 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
description: Add unique constraint on preprocessor directive and compilation pairs
2+
compatibility: backwards

csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/ElifDirective.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ private ElifDirective(Context cx, ElifDirectiveTriviaSyntax trivia, IfDirective
2020
public override void WriteId(EscapingTextWriter trapFile)
2121
{
2222
trapFile.WriteSubId(Context.CreateLocation(ReportingLocation));
23+
trapFile.WriteSubId(start);
2324
trapFile.Write(Symbol.IsActive);
2425
trapFile.Write(',');
2526
trapFile.Write(Symbol.BranchTaken);

csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/ElseDirective.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ private ElseDirective(Context cx, ElseDirectiveTriviaSyntax trivia, IfDirective
1818
public override void WriteId(EscapingTextWriter trapFile)
1919
{
2020
trapFile.WriteSubId(Context.CreateLocation(ReportingLocation));
21+
trapFile.WriteSubId(start);
2122
trapFile.Write(Symbol.IsActive);
2223
trapFile.Write(',');
2324
trapFile.Write(Symbol.BranchTaken);

csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/EndIfDirective.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ private EndIfDirective(Context cx, EndIfDirectiveTriviaSyntax trivia, IfDirectiv
1313
this.start = start;
1414
}
1515

16+
public override void WriteId(EscapingTextWriter trapFile)
17+
{
18+
trapFile.WriteSubId(Context.CreateLocation(ReportingLocation));
19+
trapFile.WriteSubId(start);
20+
trapFile.Write(Symbol.IsActive);
21+
trapFile.Write(";trivia");
22+
}
23+
1624
protected override void PopulatePreprocessor(TextWriter trapFile)
1725
{
1826
trapFile.directive_endifs(this, start);

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

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -190,37 +190,29 @@ private void DoExtractTree(SyntaxTree tree)
190190
var transformedSourcePath = PathTransformer.Transform(sourcePath);
191191

192192
var trapPath = transformedSourcePath.GetTrapPath(Logger, options.TrapCompression);
193-
var upToDate = false;
194-
195-
// compilation.Clone() is used to allow symbols to be garbage collected.
196193
using var trapWriter = transformedSourcePath.CreateTrapWriter(Logger, options.TrapCompression, discardDuplicates: false);
197194

198-
upToDate = FileIsUpToDate(sourcePath, trapWriter.TrapFile);
199-
200195
var currentTaskId = IncrementTaskCount();
201196
ReportProgressTaskStarted(currentTaskId, sourcePath);
202197

203-
if (!upToDate)
198+
var cx = new Context(ExtractionContext, compilation, trapWriter, new SourceScope(tree), addAssemblyTrapPrefix);
199+
// Ensure that the file itself is populated in case the source file is totally empty
200+
var root = tree.GetRoot();
201+
Entities.File.Create(cx, root.SyntaxTree.FilePath);
202+
203+
var csNode = (CSharpSyntaxNode)root;
204+
var directiveVisitor = new DirectiveVisitor(cx);
205+
csNode.Accept(directiveVisitor);
206+
foreach (var branch in directiveVisitor.BranchesTaken)
204207
{
205-
var cx = new Context(ExtractionContext, compilation, trapWriter, new SourceScope(tree), addAssemblyTrapPrefix);
206-
// Ensure that the file itself is populated in case the source file is totally empty
207-
var root = tree.GetRoot();
208-
Entities.File.Create(cx, root.SyntaxTree.FilePath);
209-
210-
var csNode = (CSharpSyntaxNode)root;
211-
var directiveVisitor = new DirectiveVisitor(cx);
212-
csNode.Accept(directiveVisitor);
213-
foreach (var branch in directiveVisitor.BranchesTaken)
214-
{
215-
cx.TrapStackSuffix.Add(branch);
216-
}
217-
csNode.Accept(new CompilationUnitVisitor(cx));
218-
cx.PopulateAll();
219-
CommentPopulator.ExtractCommentBlocks(cx, cx.CommentGenerator);
220-
cx.PopulateAll();
208+
cx.TrapStackSuffix.Add(branch);
221209
}
210+
csNode.Accept(new CompilationUnitVisitor(cx));
211+
cx.PopulateAll();
212+
CommentPopulator.ExtractCommentBlocks(cx, cx.CommentGenerator);
213+
cx.PopulateAll();
222214

223-
ReportProgressTaskDone(currentTaskId, sourcePath, trapPath, stopwatch.Elapsed, upToDate ? AnalysisAction.UpToDate : AnalysisAction.Extracted);
215+
ReportProgressTaskDone(currentTaskId, sourcePath, trapPath, stopwatch.Elapsed, AnalysisAction.Extracted);
224216
}
225217
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
226218
{
@@ -268,12 +260,6 @@ public void AnalyseCompilation()
268260
extractionTasks.Add(() => DoAnalyseCompilation());
269261
}
270262

271-
private static bool FileIsUpToDate(string src, string dest)
272-
{
273-
return File.Exists(dest) &&
274-
File.GetLastWriteTime(dest) >= File.GetLastWriteTime(src);
275-
}
276-
277263
private static void AnalyseNamespace(Context cx, INamespaceSymbol ns)
278264
{
279265
foreach (var memberNamespace in ns.GetNamespaceMembers())

csharp/extractor/Semmle.Extraction/TrapWriter.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,11 @@ public void Dispose()
194194
var hash = FileUtils.ComputeFileHash(tmpFile);
195195
if (existingHash != hash)
196196
{
197-
var root = TrapFile.Substring(0, TrapFile.Length - 8); // Remove trailing ".trap.gz"
198-
if (TryMove(tmpFile, $"{root}-{hash}.trap{TrapExtension(trapCompression)}"))
197+
var extension = TrapExtension(trapCompression);
198+
var root = TrapFile[..^extension.Length]; // Remove trailing ".trap", ".trap.gz", or ".trap.br"
199+
var newTrapName = $"{root}-{hash}{extension}";
200+
logger.LogInfo($"Identical trap file for {TrapFile} already exists, renaming to {newTrapName}");
201+
if (TryMove(tmpFile, $"{newTrapName}"))
199202
return;
200203
}
201204
logger.LogInfo($"Identical trap file for {TrapFile} already exists");
@@ -217,16 +220,16 @@ private static string TrapExtension(CompressionMode compression)
217220
{
218221
switch (compression)
219222
{
220-
case CompressionMode.None: return "";
221-
case CompressionMode.Gzip: return ".gz";
222-
case CompressionMode.Brotli: return ".br";
223+
case CompressionMode.None: return ".trap";
224+
case CompressionMode.Gzip: return ".trap.gz";
225+
case CompressionMode.Brotli: return ".trap.br";
223226
default: throw new ArgumentOutOfRangeException(nameof(compression), compression, "Unsupported compression type");
224227
}
225228
}
226229

227230
public static string TrapPath(ILogger logger, string? folder, PathTransformer.ITransformedPath path, TrapWriter.CompressionMode trapCompression)
228231
{
229-
var filename = $"{path.Value}.trap{TrapExtension(trapCompression)}";
232+
var filename = $"{path.Value}{TrapExtension(trapCompression)}";
230233
if (string.IsNullOrEmpty(folder))
231234
folder = Directory.GetCurrentDirectory();
232235

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| Program.cs:3:24:3:27 | Main | 2 |
2+
| Program.cs:10:17:10:33 | GreetConditional1 | 2 |
3+
| Program.cs:19:17:19:21 | Greet | 2 |
4+
| Program.cs:25:17:25:33 | GreetConditional2 | 2 |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import csharp
2+
3+
from Method m
4+
where m.fromSource()
5+
select m, count(m.getBody())

0 commit comments

Comments
 (0)