Make derived 'Context' classes internal and adjust visibility of members in base 'Context'

This commit is contained in:
Tamas Vajk
2021-02-17 10:41:53 +01:00
parent 539fdf952a
commit 841ef9a4ae
43 changed files with 145 additions and 136 deletions

View File

@@ -310,7 +310,7 @@ namespace Semmle.Extraction.CSharp
{
var stopwatch = new Stopwatch();
stopwatch.Start();
CIL.Entities.Assembly.ExtractCIL(layout, r.FilePath, Logger, !options.Cache, options.PDB, options.TrapCompression, out var trapFile, out var extracted);
CIL.Analyser.ExtractCIL(layout, r.FilePath, Logger, !options.Cache, options.PDB, options.TrapCompression, out var trapFile, out var extracted);
stopwatch.Stop();
ReportProgress(r.FilePath, trapFile, stopwatch.Elapsed, extracted ? AnalysisAction.Extracted : AnalysisAction.UpToDate);
}

View File

@@ -1,11 +1,9 @@
using Microsoft.CodeAnalysis;
namespace Semmle.Extraction.CSharp
{
/// <summary>
/// A factory for creating cached entities.
/// </summary>
public abstract class CachedEntityFactory<TInit, TEntity>
internal abstract class CachedEntityFactory<TInit, TEntity>
: Extraction.CachedEntityFactory<TInit, TEntity> where TEntity : CachedEntity
{
/// <summary>

View File

@@ -2,6 +2,7 @@ using Microsoft.CodeAnalysis;
using System;
using System.Diagnostics.CodeAnalysis;
using Semmle.Extraction.Entities;
using System.Collections.Generic;
namespace Semmle.Extraction.CSharp
{
@@ -9,7 +10,7 @@ namespace Semmle.Extraction.CSharp
/// State that needs to be available throughout the extraction process.
/// There is one Context object per trap output file.
/// </summary>
public class Context : Extraction.Context
internal class Context : Extraction.Context
{
/// <summary>
/// The program database provided by Roslyn.
@@ -50,7 +51,7 @@ namespace Semmle.Extraction.CSharp
public bool IsAssemblyScope => scope is AssemblyScope;
public SyntaxTree SourceTree => scope is SourceScope sc ? sc.SourceTree : null;
private SyntaxTree SourceTree => scope is SourceScope sc ? sc.SourceTree : null;
/// <summary>
/// Whether the given symbol needs to be defined in this context.
@@ -116,5 +117,28 @@ namespace Semmle.Extraction.CSharp
loc = null;
return false;
}
private readonly HashSet<Label> extractedGenerics = new HashSet<Label>();
/// <summary>
/// Should the given entity be extracted?
/// A second call to this method will always return false,
/// on the assumption that it would have been extracted on the first call.
///
/// This is used to track the extraction of generics, which cannot be extracted
/// in a top-down manner.
/// </summary>
/// <param name="entity">The entity to extract.</param>
/// <returns>True only on the first call for a particular entity.</returns>
internal bool ExtractGenerics(CachedEntity entity)
{
if (extractedGenerics.Contains(entity.Label))
{
return false;
}
extractedGenerics.Add(entity.Label);
return true;
}
}
}

View File

@@ -4,7 +4,7 @@ using System.IO;
namespace Semmle.Extraction.CSharp.Entities
{
public class Assembly : Extraction.Entities.Location
internal class Assembly : Extraction.Entities.Location
{
// todo: this can be changed to an override after the .NET 5 upgrade
private new Context Context => (Context)base.Context;

View File

@@ -1,6 +1,6 @@
namespace Semmle.Extraction.CSharp.Entities
{
public abstract class CachedEntity<T> : Extraction.CachedEntity<T>
internal abstract class CachedEntity<T> : Extraction.CachedEntity<T>
{
// todo: this can be changed to an override after the .NET 5 upgrade
protected new Context Context => (Context)base.Context;

View File

@@ -9,7 +9,7 @@ using System.Reflection.Metadata.Ecma335;
namespace Semmle.Extraction.CSharp.Entities
{
public abstract class CachedSymbol<T> : CachedEntity<T> where T : ISymbol
internal abstract class CachedSymbol<T> : CachedEntity<T> where T : ISymbol
{
// todo: this can be changed to an override after the .NET 5 upgrade
protected new Context Context => (Context)base.Context;

View File

@@ -6,7 +6,7 @@ using Semmle.Util;
namespace Semmle.Extraction.CSharp.Entities
{
public class Compilation : CachedEntity<object>
internal class Compilation : CachedEntity<object>
{
private static (string Cwd, string[] Args) settings;
private static int hashCode;

View File

@@ -8,7 +8,7 @@ using System.IO;
namespace Semmle.Extraction.CSharp.Entities
{
public class Constructor : Method
internal class Constructor : Method
{
private Constructor(Context cx, IMethodSymbol init)
: base(cx, init) { }

View File

@@ -23,12 +23,13 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
private static ExprKind GetKind(Context cx, BinaryExpressionSyntax node)
{
var k = GetBinaryTokenKind(cx, node.OperatorToken.Kind());
var k = GetBinaryTokenKind(cx, node);
return GetCallType(cx, node).AdjustKind(k);
}
private static ExprKind GetBinaryTokenKind(Context cx, SyntaxKind kind)
private static ExprKind GetBinaryTokenKind(Context cx, BinaryExpressionSyntax node)
{
var kind = node.OperatorToken.Kind();
switch (kind)
{
case SyntaxKind.LessThanToken: return ExprKind.LT;
@@ -54,7 +55,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
case SyntaxKind.QuestionQuestionToken: return ExprKind.NULL_COALESCING;
// !! And the rest
default:
cx.ModelError($"Unhandled operator type {kind}");
cx.ModelError(node, $"Unhandled operator type {kind}");
return ExprKind.UNKNOWN;
}
}

View File

@@ -52,7 +52,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
ObjectInitializer.Create(new ExpressionNodeInfo(Context, Syntax.Initializer, this, -1).SetType(Type));
break;
default:
Context.ModelError("Unhandled initializer in object creation");
Context.ModelError(Syntax.Initializer, "Unhandled initializer in object creation");
break;
}
}

View File

@@ -6,7 +6,7 @@ using System.Linq;
namespace Semmle.Extraction.CSharp.Entities
{
public class File : Extraction.Entities.File
internal class File : Extraction.Entities.File
{
// todo: this can be changed to an override after the .NET 5 upgrade
private new Context Context => (Context)base.Context;

View File

@@ -1,6 +1,6 @@
namespace Semmle.Extraction.CSharp.Entities
{
public abstract class FreshEntity : Extraction.FreshEntity
internal abstract class FreshEntity : Extraction.FreshEntity
{
// todo: this can be changed to an override after the .NET 5 upgrade
protected new Context Context => (Context)base.Context;

View File

@@ -8,7 +8,7 @@ using System.Linq;
namespace Semmle.Extraction.CSharp.Entities
{
public abstract class Method : CachedSymbol<IMethodSymbol>, IExpressionParentEntity, IStatementParentEntity
internal abstract class Method : CachedSymbol<IMethodSymbol>, IExpressionParentEntity, IStatementParentEntity
{
protected Method(Context cx, IMethodSymbol init)
: base(cx, init) { }

View File

@@ -3,7 +3,7 @@ using System.IO;
namespace Semmle.Extraction.CSharp.Entities
{
public class NonGeneratedSourceLocation : Extraction.Entities.SourceLocation
internal class NonGeneratedSourceLocation : Extraction.Entities.SourceLocation
{
// todo: this can be changed to an override after the .NET 5 upgrade
private new Context Context => (Context)base.Context;

View File

@@ -7,7 +7,7 @@ using System.IO;
namespace Semmle.Extraction.CSharp.Entities
{
public class Parameter : CachedSymbol<IParameterSymbol>, IExpressionParentEntity
internal class Parameter : CachedSymbol<IParameterSymbol>, IExpressionParentEntity
{
protected IEntity Parent { get; set; }
protected Parameter Original { get; }

View File

@@ -5,7 +5,7 @@ using System.Linq;
namespace Semmle.Extraction.CSharp.Entities
{
public sealed class Nullability
internal sealed class Nullability
{
public int Annotation { get; }
@@ -96,7 +96,7 @@ namespace Semmle.Extraction.CSharp.Entities
}
}
public class NullabilityEntity : CachedEntity<Nullability>
internal class NullabilityEntity : CachedEntity<Nullability>
{
public NullabilityEntity(Context cx, Nullability init) : base(cx, init)
{

View File

@@ -8,7 +8,7 @@ using System.Linq;
namespace Semmle.Extraction.CSharp.Entities
{
public abstract class Type : CachedSymbol<ITypeSymbol>
internal abstract class Type : CachedSymbol<ITypeSymbol>
{
protected Type(Context cx, ITypeSymbol init)
: base(cx, init) { }

View File

@@ -18,7 +18,7 @@ namespace Semmle.Extraction.CSharp.Entities
var returnType = Type.Create(Context, Symbol.ReturnType);
trapFile.operators(this,
Symbol.Name,
OperatorSymbol(Context, Symbol.Name),
OperatorSymbol(Context, Symbol),
ContainingType,
returnType.TypeRef,
(UserOperator)OriginalDefinition);
@@ -176,10 +176,11 @@ namespace Semmle.Extraction.CSharp.Entities
/// <param name="cx">Extractor context.</param>
/// <param name="methodName">The method name.</param>
/// <returns>The converted name.</returns>
public static string OperatorSymbol(Context cx, string methodName)
private static string OperatorSymbol(Context cx, IMethodSymbol method)
{
var methodName = method.Name;
if (!OperatorSymbol(methodName, out var result))
cx.ModelError($"Unhandled operator name in OperatorSymbol(): '{methodName}'");
cx.ModelError(method, $"Unhandled operator name in OperatorSymbol(): '{methodName}'");
return result;
}

View File

@@ -9,7 +9,7 @@ using System.Linq;
namespace Semmle.Extraction.CSharp.Populators
{
public class TypeContainerVisitor : CSharpSyntaxVisitor
internal class TypeContainerVisitor : CSharpSyntaxVisitor
{
protected Context Cx { get; }
protected IEntity Parent { get; }