BizHawk/ExternalProjects/AnalyzersCommon/RoslynUtils.cs

30 lines
1.1 KiB
C#

namespace BizHawk.Analyzers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
public static class RoslynUtils
{
public static SyntaxNode? EnclosingTypeDeclarationSyntax(this CSharpSyntaxNode node)
{
var parent = node.Parent;
while (parent is not (null or TypeDeclarationSyntax)) parent = parent.Parent;
return parent;
}
private static ITypeSymbol? GetThrownExceptionType(this SemanticModel model, ExpressionSyntax exprSyn)
=> exprSyn is ObjectCreationExpressionSyntax
? model.GetTypeInfo(exprSyn).Type
: null; // code reads `throw <something weird>`
public static ITypeSymbol? GetThrownExceptionType(this SemanticModel model, ThrowExpressionSyntax tes)
=> model.GetThrownExceptionType(tes.Expression);
public static ITypeSymbol? GetThrownExceptionType(this SemanticModel model, ThrowStatementSyntax tss)
=> model.GetThrownExceptionType(tss.Expression!);
public static bool Matches(this ISymbol expected, ISymbol? actual)
=> SymbolEqualityComparer.Default.Equals(expected, actual);
}