Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aeb38a1

Browse files
committed
C#: Refactoring tuple writing to remove Tuple intermediate object and write straight to trap file.
1 parent bd1b001 commit aeb38a1

8 files changed

Lines changed: 57 additions & 69 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public override void Populate()
2929
{
3030
if (assemblyPath != null)
3131
{
32-
Context.Emit(Tuples.assemblies(this, File.Create(Context, assemblyPath), assembly.ToString(),
33-
assembly.Identity.Name, assembly.Identity.Version.ToString()));
32+
Context.TrapWriter.assemblies(this, File.Create(Context, assemblyPath), assembly.ToString(),
33+
assembly.Identity.Name, assembly.Identity.Version.ToString());
3434
}
3535
}
3636

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class ExtractionMessage : FreshEntity
44
{
55
public ExtractionMessage(Context cx, Message msg) : base(cx)
66
{
7-
cx.Emit(Tuples.extractor_messages(this, msg.Severity, "C# extractor", msg.Text, msg.EntityText, msg.Location, msg.StackTrace));
7+
cx.TrapWriter.extractor_messages(this, msg.Severity, "C# extractor", msg.Text, msg.EntityText, msg.Location, msg.StackTrace);
88
}
99

1010
public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override void Populate()
2727
{
2828
if (Path == null)
2929
{
30-
Context.Emit(Tuples.files(this, "", "", ""));
30+
Context.TrapWriter.files(this, "", "", "");
3131
}
3232
else
3333
{
@@ -41,9 +41,9 @@ public override void Populate()
4141
// remove the dot from the extension
4242
if (extension.Length > 0)
4343
extension = extension.Substring(1);
44-
Context.Emit(Tuples.files(this, PathAsDatabaseString(Path), name, extension));
44+
Context.TrapWriter.files(this, PathAsDatabaseString(Path), name, extension);
4545

46-
Context.Emit(Tuples.containerparent(Entities.Folder.Create(Context, fi.Directory), this));
46+
Context.TrapWriter.containerparent(Folder.Create(Context, fi.Directory), this);
4747
if (fromSource == 1)
4848
{
4949
foreach (var text in Context.Compilation.SyntaxTrees.
@@ -54,12 +54,12 @@ public override void Populate()
5454
var lineCounts = LineCounter.ComputeLineCounts(rawText);
5555
if (rawText.Length > 0 && rawText[rawText.Length - 1] != '\n') lineCounts.Total++;
5656

57-
Context.Emit(Tuples.numlines(this, lineCounts));
57+
Context.TrapWriter.numlines(this, lineCounts);
5858
Context.TrapWriter.Archive(fi.FullName, text.Encoding);
5959
}
6060
}
6161

62-
Context.Emit(Tuples.file_extraction_mode(this, Context.Extractor.Standalone ? 1 : 0));
62+
Context.TrapWriter.file_extraction_mode(this, Context.Extractor.Standalone ? 1 : 0);
6363
}
6464
}
6565

@@ -101,7 +101,7 @@ class GeneratedFile : File
101101

102102
public override void Populate()
103103
{
104-
Context.Emit(Tuples.files(this, "", "", ""));
104+
Context.TrapWriter.files(this, "", "", "");
105105
}
106106

107107
public override IId Id => new Key("GENERATED;sourcefile");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public override void Populate()
2727
// On Windows: System.IO.DirectoryInfo.Name returns "L:\"
2828
string shortName = symbol.Parent == null ? "" : symbol.Name;
2929

30-
Context.Emit(Tuples.folders(this, File.PathAsDatabaseString(Path), shortName));
30+
Context.TrapWriter.folders(this, File.PathAsDatabaseString(Path), shortName);
3131
if (symbol.Parent != null)
3232
{
33-
Context.Emit(Tuples.containerparent(Create(Context, symbol.Parent), this));
33+
Context.TrapWriter.containerparent(Create(Context, symbol.Parent), this);
3434
}
3535
}
3636

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class GeneratedLocation : SourceLocation
1212

1313
public override void Populate()
1414
{
15-
Context.Emit(Tuples.locations_default(this, GeneratedFile, 0, 0, 0, 0));
15+
Context.TrapWriter.locations_default(this, GeneratedFile, 0, 0, 0, 0);
1616
}
1717

1818
public override IId Id => new Key("loc,", GeneratedFile, ",0,0,0,0");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public override void Populate()
1313
{
1414
Position = symbol.GetLineSpan();
1515
FileEntity = File.Create(Context, Position.Path);
16-
Context.Emit(Tuples.locations_default(this, FileEntity, Position.Span.Start.Line + 1, Position.Span.Start.Character + 1,
17-
Position.Span.End.Line + 1, Position.Span.End.Character));
16+
Context.TrapWriter.locations_default(this, FileEntity, Position.Span.Start.Line + 1, Position.Span.Start.Character + 1,
17+
Position.Span.End.Line + 1, Position.Span.End.Character);
1818
}
1919

2020
public override bool NeedsPopulation => true;

csharp/extractor/Semmle.Extraction/TrapWriter.cs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
using Semmle.Util;
2+
using Semmle.Util.Logging;
13
using System;
24
using System.IO;
35
using System.IO.Compression;
46
using System.Security.Cryptography;
57
using System.Text;
6-
using Semmle.Util;
7-
using Semmle.Util.Logging;
88

99
namespace Semmle.Extraction
1010
{
@@ -15,20 +15,12 @@ public interface ITrapEmitter
1515

1616
public sealed class TrapWriter : IDisposable
1717
{
18-
//#################### ENUMERATIONS ####################
19-
#region
20-
2118
public enum InnerPathComputation
2219
{
2320
ABSOLUTE,
2421
RELATIVE
2522
}
2623

27-
#endregion
28-
29-
//#################### PRIVATE VARIABLES ####################
30-
#region
31-
3224
/// <summary>
3325
/// The location of the src_archive directory.
3426
/// </summary>
@@ -37,25 +29,17 @@ public enum InnerPathComputation
3729

3830
private readonly bool discardDuplicates;
3931

40-
#endregion
41-
42-
//#################### PROPERTIES ####################
43-
#region
44-
4532
public int IdCounter { get; set; } = 1;
4633

4734
readonly Lazy<StreamWriter> WriterLazy;
4835

36+
public StreamWriter Writer => WriterLazy.Value;
37+
4938
readonly Lazy<TrapBuilder> BuilderLazy;
5039
TrapBuilder Builder => BuilderLazy.Value;
5140

5241
readonly ILogger Logger;
5342

54-
#endregion
55-
56-
//#################### CONSTRUCTORS ####################
57-
#region
58-
5943
public TrapWriter(ILogger logger, string outputfile, string trap, string archive, bool discardDuplicates)
6044
{
6145
Logger = logger;
@@ -95,11 +79,6 @@ public TrapWriter(ILogger logger, string outputfile, string trap, string archive
9579
public readonly string TrapFile;
9680
string tmpFile; // The temporary file which is moved to trapFile once written.
9781

98-
#endregion
99-
100-
//#################### PUBLIC METHODS ####################
101-
#region
102-
10382
/// <summary>
10483
/// Adds the specified input file to the source archive. It may end up in either the normal or long path area
10584
/// of the source archive, depending on the length of its full path.
@@ -201,11 +180,6 @@ public void Emit(ITrapEmitter emitter)
201180
emitter.EmitToTrapBuilder(Builder);
202181
}
203182

204-
#endregion
205-
206-
//#################### PRIVATE METHODS ####################
207-
#region
208-
209183
/// <summary>
210184
/// Computes the hash of <paramref name="filePath"/>.
211185
/// </summary>
@@ -319,7 +293,5 @@ public static string TrapPath(ILogger logger, string folder, string filename)
319293

320294
return NestPaths(logger, folder, filename, InnerPathComputation.ABSOLUTE); ;
321295
}
322-
323-
#endregion
324296
}
325297
}

csharp/extractor/Semmle.Extraction/Tuples.cs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,44 @@ namespace Semmle.Extraction
88
/// </summary>
99
static class Tuples
1010
{
11-
internal static Tuple assemblies(Assembly assembly, File file, string identifier, string name, string version) =>
12-
new Tuple("assemblies", assembly, file, identifier, name, version);
13-
14-
internal static Tuple containerparent(Folder parent, IEntity child) =>
15-
new Tuple("containerparent", parent, child);
16-
17-
internal static Tuple extractor_messages(ExtractionMessage error, Semmle.Util.Logging.Severity severity, string origin, string errorMessage, string entityText, Location location, string stackTrace) =>
18-
new Tuple("extractor_messages", error, severity, origin, errorMessage, entityText, location, stackTrace);
19-
20-
internal static Tuple file_extraction_mode(File file, int mode) =>
21-
new Tuple("file_extraction_mode", file, mode);
22-
23-
internal static Tuple files(File file, string fullName, string name, string extension) =>
24-
new Tuple("files", file, fullName, name, extension, 0);
25-
26-
internal static Tuple folders(Folder folder, string path, string name) =>
27-
new Tuple("folders", folder, path, name);
28-
29-
internal static Tuple locations_default(SourceLocation label, File file, int startLine, int startCol, int endLine, int endCol) =>
30-
new Tuple("locations_default", label, file, startLine, startCol, endLine, endCol);
31-
32-
internal static Tuple numlines(IEntity label, LineCounts lineCounts) =>
33-
new Tuple("numlines", label, lineCounts.Total, lineCounts.Code, lineCounts.Comment);
11+
public static void assemblies(this TrapWriter writer, Assembly assembly, File file, string identifier, string name, string version)
12+
{
13+
writer.Writer.BeginTuple("assemblies").Param(assembly).Param(file).Param(identifier).Param(name).Param(version).EndTuple();
14+
}
15+
16+
public static void containerparent(this TrapWriter writer, Folder parent, IEntity child)
17+
{
18+
writer.Writer.BeginTuple("containerparent").Param(parent).Param(child).EndTuple();
19+
}
20+
21+
public static void extractor_messages(this TrapWriter writer, ExtractionMessage error, Semmle.Util.Logging.Severity severity, string origin, string errorMessage, string entityText, Location location, string stackTrace)
22+
{
23+
writer.Writer.BeginTuple("extractor_messages").Param(error).Param((int)severity).Param(origin).Param(errorMessage).Param(entityText).Param(location).Param(stackTrace).EndTuple();
24+
}
25+
26+
internal static void file_extraction_mode(this TrapWriter writer, Entities.File file, int mode)
27+
{
28+
writer.Writer.BeginTuple("file_extraction_mode").Param(file).Param(mode).EndTuple();
29+
}
30+
31+
public static void files(this TrapWriter writer, Entities.File file, string fullName, string name, string extension)
32+
{
33+
writer.Writer.BeginTuple("files").Param(file).Param(fullName).Param(name).Param(extension).Param(0).EndTuple();
34+
}
35+
36+
public static void folders(this TrapWriter writer, Folder folder, string path, string name)
37+
{
38+
writer.Writer.BeginTuple("folders").Param(folder).Param(path).Param(name).EndTuple();
39+
}
40+
41+
public static void locations_default(this TrapWriter writer, SourceLocation label, Entities.File file, int startLine, int startCol, int endLine, int endCol)
42+
{
43+
writer.Writer.BeginTuple("locations_default").Param(label).Param(file).Param(startLine).Param(startCol).Param(endLine).Param(endCol).EndTuple();
44+
}
45+
46+
public static void numlines(this TrapWriter writer, IEntity label, LineCounts lineCounts)
47+
{
48+
writer.Writer.BeginTuple("numlines").Param(label).Param(lineCounts.Total).Param(lineCounts.Code).Param(lineCounts.Comment).EndTuple();
49+
}
3450
}
3551
}

0 commit comments

Comments
 (0)