Skip to content

Commit 7df90f1

Browse files
committed
C#: Address review comments. Tidy up tuple generation, consolidating code and use run-time type information instead of FirstParam/NextParam.
1 parent 40f56ff commit 7df90f1

File tree

10 files changed

+160
-328
lines changed

10 files changed

+160
-328
lines changed

csharp/extractor/Semmle.Extraction.CIL/Entities/Type.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public bool TryGetPrimitiveType(out PrimitiveType t)
247247
}
248248
}
249249

250-
public bool TryGetPrimitiveTypeCode(out PrimitiveTypeCode code)
250+
private bool TryGetPrimitiveTypeCode(out PrimitiveTypeCode code)
251251
{
252252
if (ContainingType == null && Namespace.Name == cx.SystemNamespace.Name)
253253
{
@@ -314,7 +314,7 @@ public bool TryGetPrimitiveTypeCode(out PrimitiveTypeCode code)
314314
return false;
315315
}
316316

317-
public bool IsPrimitiveType => TryGetPrimitiveTypeCode(out _);
317+
protected bool IsPrimitiveType => TryGetPrimitiveTypeCode(out _);
318318

319319
public static Type DecodeType(GenericContext gc, TypeSpecificationHandle handle) =>
320320
gc.cx.mdReader.GetTypeSpecification(handle).DecodeSignature(gc.cx.TypeSignatureDecoder, gc);

csharp/extractor/Semmle.Extraction.CIL/Factories.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public partial class Context
1515

1616
public T Populate<T>(T e) where T : IExtractedEntity
1717
{
18-
if(e.Label.Valid)
18+
if (e.Label.Valid)
1919
{
2020
return e; // Already populated
2121
}
@@ -72,7 +72,7 @@ public PrimitiveType Create(PrimitiveTypeCode code)
7272
{
7373
PrimitiveType e = primitiveTypes[(int)code];
7474

75-
if(e is null)
75+
if (e is null)
7676
{
7777
e = new PrimitiveType(this, code);
7878
e.Label = cx.GetNewLabel();
@@ -157,7 +157,7 @@ IExtractedEntity Create(GenericContext gc, MemberReferenceHandle handle)
157157
/// <returns>The string.</returns>
158158
public string GetString(StringHandle h) => mdReader.GetString(h);
159159

160-
#region Namespaces
160+
#region Namespaces
161161

162162
readonly CachedFunction<StringHandle, Namespace> namespaceFactory;
163163

@@ -197,9 +197,9 @@ Namespace CreateNamespace(NamespaceDefinitionHandle handle)
197197
NamespaceDefinition nd = mdReader.GetNamespaceDefinition(handle);
198198
return Populate(new Namespace(this, GetString(nd.Name), Create(nd.Parent)));
199199
}
200-
#endregion
200+
#endregion
201201

202-
#region Locations
202+
#region Locations
203203
readonly CachedFunction<PDB.ISourceFile, PdbSourceFile> sourceFiles;
204204
readonly CachedFunction<string, Folder> folders;
205205
readonly CachedFunction<PDB.Location, PdbSourceLocation> sourceLocations;
@@ -225,7 +225,7 @@ Namespace CreateNamespace(NamespaceDefinitionHandle handle)
225225
/// <returns>A source location entity.</returns>
226226
public PdbSourceLocation CreateSourceLocation(PDB.Location loc) => sourceLocations[loc];
227227

228-
#endregion
228+
#endregion
229229

230230
readonly CachedFunction<GenericContext, Handle, IExtractedEntity> genericHandleFactory;
231231

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ internal static void accessor_location(this TextWriter trapFile, Accessor access
2323

2424
internal static void accessors(this TextWriter trapFile, Accessor accessorKey, int kind, string name, Property propKey, Accessor unboundAccessor)
2525
{
26-
trapFile.BeginTuple("accessors").Param(accessorKey).Param(kind).Param(name).Param(propKey).Param(unboundAccessor).EndTuple();
26+
trapFile.WriteTuple("accessors", accessorKey, kind, name, propKey, unboundAccessor);
2727
}
2828

2929
internal static void array_element_type(this TextWriter trapFile, ArrayType array, int dimension, int rank, Type elementType)
3030
{
31-
trapFile.BeginTuple("array_element_type").Param(array).Param(dimension).Param(rank).Param(elementType).EndTuple();
31+
trapFile.WriteTuple("array_element_type", array, dimension, rank, elementType);
3232
}
3333

3434
internal static void attributes(this TextWriter trapFile, Attribute attribute, Type attributeType, IEntity entity)
@@ -43,7 +43,7 @@ internal static void attribute_location(this TextWriter trapFile, Attribute attr
4343

4444
internal static void catch_type(this TextWriter trapFile, Entities.Statements.Catch @catch, Type type, bool explicityCaught)
4545
{
46-
trapFile.BeginTuple("catch_type").Param(@catch).Param(type).Param(explicityCaught ? 1 : 2).EndTuple();
46+
trapFile.WriteTuple("catch_type", @catch, type, explicityCaught ? 1 : 2);
4747
}
4848

4949
internal static void commentblock(this TextWriter trapFile, CommentBlock k)
@@ -53,12 +53,12 @@ internal static void commentblock(this TextWriter trapFile, CommentBlock k)
5353

5454
internal static void commentblock_binding(this TextWriter trapFile, CommentBlock commentBlock, Label entity, CommentBinding binding)
5555
{
56-
trapFile.BeginTuple("commentblock_binding").Param(commentBlock).Param(entity).Param((int)binding).EndTuple();
56+
trapFile.WriteTuple("commentblock_binding", commentBlock, entity, (int)binding);
5757
}
5858

5959
internal static void commentblock_child(this TextWriter trapFile, CommentBlock commentBlock, CommentLine commentLine, int child)
6060
{
61-
trapFile.BeginTuple("commentblock_child").Param(commentBlock).Param(commentLine).Param(child).EndTuple();
61+
trapFile.WriteTuple("commentblock_child", commentBlock, commentLine, child);
6262
}
6363

6464
internal static void commentblock_location(this TextWriter trapFile, CommentBlock k, Location l)
@@ -68,7 +68,7 @@ internal static void commentblock_location(this TextWriter trapFile, CommentBloc
6868

6969
internal static void commentline(this TextWriter trapFile, CommentLine commentLine, CommentLineType type, string text, string rawtext)
7070
{
71-
trapFile.BeginTuple("commentline").Param(commentLine).Param((int)type).Param(text).Param(rawtext).EndTuple();
71+
trapFile.WriteTuple("commentline", commentLine, (int)type, text, rawtext);
7272
}
7373

7474
internal static void commentline_location(this TextWriter trapFile, CommentLine commentLine, Location location)
@@ -78,32 +78,32 @@ internal static void commentline_location(this TextWriter trapFile, CommentLine
7878

7979
internal static void compilation_args(this TextWriter trapFile, Compilation compilation, int index, string arg)
8080
{
81-
trapFile.BeginTuple("compilation_args").Param(compilation).Param(index).Param(arg).EndTuple();
81+
trapFile.WriteTuple("compilation_args", compilation, index, arg);
8282
}
8383

8484
internal static void compilation_compiling_files(this TextWriter trapFile, Compilation compilation, int index, Extraction.Entities.File file)
8585
{
86-
trapFile.BeginTuple("compilation_compiling_files").Param(compilation).Param(index).Param(file).EndTuple();
86+
trapFile.WriteTuple("compilation_compiling_files", compilation, index, file);
8787
}
8888

8989
internal static void compilation_referencing_files(this TextWriter trapFile, Compilation compilation, int index, Extraction.Entities.File file)
9090
{
91-
trapFile.BeginTuple("compilation_referencing_files").Param(compilation).Param(index).Param(file).EndTuple();
91+
trapFile.WriteTuple("compilation_referencing_files", compilation, index, file);
9292
}
9393

9494
internal static void compilation_finished(this TextWriter trapFile, Compilation compilation, float cpuSeconds, float elapsedSeconds)
9595
{
96-
trapFile.BeginTuple("compilation_finished").Param(compilation).Param(cpuSeconds).Param(elapsedSeconds).EndTuple();
96+
trapFile.WriteTuple("compilation_finished", compilation, cpuSeconds, elapsedSeconds);
9797
}
9898

9999
internal static void compilation_time(this TextWriter trapFile, Compilation compilation, int num, int index, float metric)
100100
{
101-
trapFile.BeginTuple("compilation_time").Param(compilation).Param(num).Param(index).Param(metric).EndTuple();
101+
trapFile.WriteTuple("compilation_time", compilation, num, index, metric);
102102
}
103103

104104
internal static void compilations(this TextWriter trapFile, Compilation compilation, string cwd)
105105
{
106-
trapFile.BeginTuple("compilations").Param(compilation).Param(cwd).EndTuple();
106+
trapFile.WriteTuple("compilations", compilation, cwd);
107107
}
108108

109109
internal static void compiler_generated(this TextWriter trapFile, IEntity entity)
@@ -118,7 +118,7 @@ internal static void conditional_access(this TextWriter trapFile, Expression acc
118118

119119
internal static void constant_value(this TextWriter trapFile, IEntity field, string value)
120120
{
121-
trapFile.BeginTuple("constant_value").Param(field).Param(value).EndTuple();
121+
trapFile.WriteTuple("constant_value", field, value);
122122
}
123123

124124
internal static void constructed_generic(this TextWriter trapFile, IEntity constructedTypeOrMethod, IEntity unboundTypeOrMethod)
@@ -153,17 +153,17 @@ internal static void destructors(this TextWriter trapFile, Destructor destructor
153153

154154
internal static void diagnostic_for(this TextWriter trapFile, Diagnostic diag, Compilation comp, int fileNo, int index)
155155
{
156-
trapFile.BeginTuple("diagnostic_for").Param(diag).Param(comp).Param(fileNo).Param(index).EndTuple();
156+
trapFile.WriteTuple("diagnostic_for", diag, comp, fileNo, index);
157157
}
158158

159159
internal static void diagnostics(this TextWriter trapFile, Diagnostic diag, int severity, string errorTag, string errorMessage, string fullErrorMessage, Location location)
160160
{
161-
trapFile.BeginTuple("diagnostics").Param(diag).Param(severity).Param(errorTag).Param(errorMessage).Param(fullErrorMessage).Param(location).EndTuple();
161+
trapFile.WriteTuple("diagnostics", diag, severity, errorTag, errorMessage, fullErrorMessage, location);
162162
}
163163

164164
internal static void dynamic_member_name(this TextWriter trapFile, Expression e, string name)
165165
{
166-
trapFile.BeginTuple("dynamic_member_name").Param(e).Param(name).EndTuple();
166+
trapFile.WriteTuple("dynamic_member_name", e, name);
167167
}
168168

169169
internal static void enum_underlying_type(this TextWriter trapFile, Type @enum, Type type)
@@ -178,7 +178,7 @@ internal static void event_accessor_location(this TextWriter trapFile, EventAcce
178178

179179
internal static void event_accessors(this TextWriter trapFile, EventAccessor accessorKey, int type, string name, Event eventKey, EventAccessor unboundAccessor)
180180
{
181-
trapFile.BeginTuple("event_accessors").Param(accessorKey).Param(type).Param(name).Param(eventKey).Param(unboundAccessor).EndTuple();
181+
trapFile.WriteTuple("event_accessors", accessorKey, type, name, eventKey, unboundAccessor);
182182
}
183183

184184
internal static void event_location(this TextWriter trapFile, Event eventKey, Location locationKey)
@@ -268,7 +268,7 @@ internal static void field_location(this TextWriter trapFile, Field field, Locat
268268

269269
internal static void fields(this TextWriter trapFile, Field field, int @const, string name, Type declaringType, Type fieldType, Field unboundKey)
270270
{
271-
trapFile.BeginTuple("fields").Param(field).Param(@const).Param(name).Param(declaringType).Param(fieldType).Param(unboundKey).EndTuple();
271+
trapFile.WriteTuple("fields", field, @const, name, declaringType, fieldType, unboundKey);
272272
}
273273

274274
internal static void general_type_parameter_constraints(this TextWriter trapFile, TypeParameterConstraints constraints, int hasKind)
@@ -328,7 +328,7 @@ internal static void localvar_location(this TextWriter trapFile, LocalVariable v
328328

329329
internal static void localvars(this TextWriter trapFile, LocalVariable key, int @const, string name, int @var, Type type, Expression expr)
330330
{
331-
trapFile.BeginTuple("localvars").Param(key).Param(@const).Param(name).Param(@var).Param(type).Param(expr).EndTuple();
331+
trapFile.WriteTuple("localvars", key, @const, name, @var, type, expr);
332332
}
333333

334334
public static void metadata_handle(this TextWriter trapFile, IEntity entity, Location assembly, int handleValue)
@@ -348,7 +348,7 @@ internal static void methods(this TextWriter trapFile, Method method, string nam
348348

349349
internal static void modifiers(this TextWriter trapFile, Label entity, string modifier)
350350
{
351-
trapFile.BeginTuple("modifiers").Param(entity).Param(modifier).EndTuple();
351+
trapFile.WriteTuple("modifiers", entity, modifier);
352352
}
353353

354354
internal static void mutator_invocation_mode(this TextWriter trapFile, Expression expr, int mode)
@@ -383,7 +383,7 @@ internal static void nullable_underlying_type(this TextWriter trapFile, Type nul
383383

384384
internal static void numlines(this TextWriter trapFile, IEntity label, LineCounts lineCounts)
385385
{
386-
trapFile.BeginTuple("numlines").Param(label).Param(lineCounts.Total).Param(lineCounts.Code).Param(lineCounts.Comment).EndTuple();
386+
trapFile.WriteTuple("numlines", label, lineCounts.Total, lineCounts.Code, lineCounts.Comment);
387387
}
388388

389389
internal static void operator_location(this TextWriter trapFile, UserOperator @operator, Location location)
@@ -393,7 +393,7 @@ internal static void operator_location(this TextWriter trapFile, UserOperator @o
393393

394394
internal static void operators(this TextWriter trapFile, UserOperator method, string methodName, string symbol, Type classKey, Type returnType, UserOperator originalDefinition)
395395
{
396-
trapFile.BeginTuple("operators").Param(method).Param(methodName).Param(symbol).Param(classKey).Param(returnType).Param(originalDefinition).EndTuple();
396+
trapFile.WriteTuple("operators", method, methodName, symbol, classKey, returnType, originalDefinition);
397397
}
398398

399399
internal static void overrides(this TextWriter trapFile, Method overriding, Method overridden)
@@ -408,7 +408,7 @@ internal static void param_location(this TextWriter trapFile, Parameter param, L
408408

409409
internal static void @params(this TextWriter trapFile, Parameter param, string name, Type type, int child, Parameter.Kind mode, IEntity method, Parameter originalDefinition)
410410
{
411-
trapFile.BeginTuple("params").Param(param).Param(name).Param(type).Param(child).Param((int)mode).Param(method).Param(originalDefinition).EndTuple();
411+
trapFile.WriteTuple("params", param, name, type, child, (int)mode, method, originalDefinition);
412412
}
413413

414414
internal static void parent_namespace(this TextWriter trapFile, IEntity type, Namespace parent)
@@ -513,7 +513,7 @@ internal static void type_parameter_constraints(this TextWriter trapFile, TypePa
513513

514514
internal static void type_parameters(this TextWriter trapFile, TypeParameter param, int child, IEntity typeOrMethod)
515515
{
516-
trapFile.BeginTuple("type_parameters").Param(param).Param(child).Param(typeOrMethod).Param((int)param.Variance).EndTuple();
516+
trapFile.WriteTuple("type_parameters", param, child, typeOrMethod, (int)param.Variance);
517517
}
518518

519519
internal static void typeref_type(this TextWriter trapFile, NamedTypeRef typeref, Type type)
@@ -528,7 +528,7 @@ internal static void typerefs(this TextWriter trapFile, NamedTypeRef type, strin
528528

529529
internal static void types(this TextWriter trapFile, Type type, TypeKind kind, string name)
530530
{
531-
trapFile.BeginTuple("types").Param(type).Param((int)kind).Param(name).EndTuple();
531+
trapFile.WriteTuple("types", type, (int)kind, name);
532532
}
533533

534534
internal static void using_namespace_directives(this TextWriter trapFile, UsingDirective @using, Namespace ns)

csharp/extractor/Semmle.Extraction.Tests/Layout.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public StringTrapEmitter(string content)
191191
Content = content;
192192
}
193193

194-
public void EmitToTrapBuilder(TextWriter trapFile)
194+
public void EmitTrap(TextWriter trapFile)
195195
{
196196
trapFile.Write(Content);
197197
}

csharp/extractor/Semmle.Extraction/Context.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public PushEmitter(Key key)
332332
Key = key;
333333
}
334334

335-
public void EmitToTrapBuilder(TextWriter trapFile)
335+
public void EmitTrap(TextWriter trapFile)
336336
{
337337
trapFile.Write(".push ");
338338
Key.AppendTo(trapFile);
@@ -342,7 +342,7 @@ public void EmitToTrapBuilder(TextWriter trapFile)
342342

343343
class PopEmitter : ITrapEmitter
344344
{
345-
public void EmitToTrapBuilder(TextWriter trapFile)
345+
public void EmitTrap(TextWriter trapFile)
346346
{
347347
trapFile.WriteLine(".pop");
348348
}

csharp/extractor/Semmle.Extraction/TrapBuilder.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)