Use `SpecialType` where possible in BizHawk.Analyzer

This commit is contained in:
YoshiRulz 2025-01-31 17:52:20 +10:00
parent ae97d1f49d
commit de1a7b1e30
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
5 changed files with 14 additions and 21 deletions

View File

@ -26,27 +26,24 @@ public sealed class AmbiguousMoneyToFloatConversionAnalyzer : DiagnosticAnalyzer
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(initContext =>
{
var decimalSym = initContext.Compilation.GetTypeByMetadataName("System.Decimal")!;
var doubleSym = initContext.Compilation.GetTypeByMetadataName("System.Double")!;
var floatSym = initContext.Compilation.GetTypeByMetadataName("System.Single")!;
initContext.RegisterOperationAction(oac =>
{
var conversionOp = (IConversionOperation) oac.Operation;
var typeOutput = conversionOp.Type;
var typeInput = conversionOp.Operand.Type;
var typeOutput = conversionOp.Type?.SpecialType ?? SpecialType.None;
var typeInput = conversionOp.Operand.Type?.SpecialType ?? SpecialType.None;
bool isToDecimal;
bool isDoublePrecision;
if (decimalSym.Matches(typeOutput))
if (typeOutput is SpecialType.System_Decimal)
{
if (doubleSym.Matches(typeInput)) isDoublePrecision = true;
else if (floatSym.Matches(typeInput)) isDoublePrecision = false;
if (typeInput is SpecialType.System_Double) isDoublePrecision = true;
else if (typeInput is SpecialType.System_Single) isDoublePrecision = false;
else return;
isToDecimal = true;
}
else if (decimalSym.Matches(typeInput))
else if (typeInput is SpecialType.System_Decimal)
{
if (doubleSym.Matches(typeOutput)) isDoublePrecision = true;
else if (floatSym.Matches(typeOutput)) isDoublePrecision = false;
if (typeOutput is SpecialType.System_Double) isDoublePrecision = true;
else if (typeOutput is SpecialType.System_Single) isDoublePrecision = false;
else return;
isToDecimal = false;
}

View File

@ -25,8 +25,6 @@ public sealed class TernaryInferredTypeMismatchAnalyzer : DiagnosticAnalyzer
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(initContext =>
{
var objectSym = initContext.Compilation.GetTypeByMetadataName("System.Object")!;
var stringSym = initContext.Compilation.GetTypeByMetadataName("System.String")!;
initContext.RegisterOperationAction(oac =>
{
var ifelseOrTernaryOp = (IConditionalOperation) oac.Operation;
@ -37,7 +35,7 @@ public sealed class TernaryInferredTypeMismatchAnalyzer : DiagnosticAnalyzer
var ternaryOp = ifelseOrTernaryOp;
var typeTernary = ternaryOp.Type!;
#if false // never hit; either both branches are string and there are no conversions, or conversions are necessary
if (stringSym.Matches(typeTernary)) return;
if (typeTernary.SpecialType is SpecialType.System_String) return;
#endif
var lhs = ternaryOp.WhenTrue;
var rhs = ternaryOp.WhenFalse;
@ -52,17 +50,17 @@ public sealed class TernaryInferredTypeMismatchAnalyzer : DiagnosticAnalyzer
var fatal = false;
IOperation flaggedOp = ternaryOp;
string message;
if (stringSym.Matches(typeLHS))
if (typeLHS.SpecialType is SpecialType.System_String)
{
flaggedOp = rhs;
message = ERR_MSG_OBJECT;
}
else if (stringSym.Matches(typeRHS))
else if (typeRHS.SpecialType is SpecialType.System_String)
{
flaggedOp = lhs;
message = ERR_MSG_OBJECT;
}
else if (objectSym.Matches(typeTernary))
else if (typeTernary.SpecialType is SpecialType.System_Object)
{
fatal = true;
message = "ternary branches are upcast to object! add ToString calls, or convert one to the other's type";

View File

@ -28,7 +28,6 @@ public sealed class UseSimplerBoolFlipAnalyzer : DiagnosticAnalyzer
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
ISymbol? boolSym = null;
context.RegisterOperationAction(
oac =>
{
@ -43,8 +42,7 @@ public sealed class UseSimplerBoolFlipAnalyzer : DiagnosticAnalyzer
}
var operation = (ICompoundAssignmentOperation) oac.Operation;
if (operation.OperatorKind is not BinaryOperatorKind.ExclusiveOr) return;
boolSym ??= oac.Compilation.GetTypeByMetadataName("System.Boolean")!;
if (!boolSym.Matches(operation.Type)) return;
if (operation.Type?.SpecialType is not SpecialType.System_Boolean) return;
if (operation.Value.Kind is not OperationKind.Literal) return;
var lhsOp = operation.Target;
bool lhsIsSimpleExpr;

View File

@ -39,7 +39,7 @@ public sealed class UseTypeofOperatorAnalyzer : DiagnosticAnalyzer
{
var operation = (IInvocationOperation) oac.Operation;
if (operation.IsImplicit || operation.Instance is null) return;
objectDotGetTypeSym ??= oac.Compilation.GetTypeByMetadataName("System.Object")!.GetMembers("GetType")[0];
objectDotGetTypeSym ??= oac.Compilation.GetSpecialType(SpecialType.System_Object).GetMembers("GetType")[0];
if (!objectDotGetTypeSym.Matches(operation.TargetMethod)) return;
if (operation.Instance.Syntax is not ThisExpressionSyntax and not IdentifierNameSyntax { Identifier.Text: "GetType" }) return; // called on something that isn't `this`
var enclosingType = operation.SemanticModel!.GetDeclaredSymbol(

Binary file not shown.