Skip to content

Commit

Permalink
chore: rename & cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sator-imaging committed Dec 1, 2024
1 parent 7c17a4f commit ca08e33
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 92 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

<!-- CMD: dotnet restore -p:DontReferenceItself=true other-options... -->
<ItemGroup Condition=" '$(DontReferenceItself)' != 'true' and '$(MSBuildProjectFile)' != 'AnalyzerCheck.csproj' ">
<PackageReference Include="SatorImaging.StaticMemberAnalyzer" Version="*">
<PackageReference Include="SatorImaging.StaticMemberAnalyzer" Version="2.0.0-beta.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ INamedTypeSymbol disposableSymbol

if (!disposableSymbol.IsRefLikeType)
{
// Task or Task<T> in System.Threading.Tasks
if (disposableSymbol.Name.StartsWith(nameof(Task), StringComparison.Ordinal)
&& disposableSymbol.ContainingNamespace.Name == nameof(System.Threading.Tasks)
&& disposableSymbol.ContainingNamespace.ContainingNamespace.Name == nameof(System.Threading)
Expand Down Expand Up @@ -272,7 +273,7 @@ INamedTypeSymbol disposableSymbol

private static bool IsTypeIgnoredByAssemblyAttribute(OperationAnalysisContext context, INamedTypeSymbol disposableSymbol)
{
const string ATTR_NAME = nameof(DisposableAnalyzer);
const string ATTR_NAME = "DisposableAnalyzer";

foreach (var attr in context.Compilation.Assembly.GetAttributes())
{
Expand Down Expand Up @@ -365,7 +366,7 @@ or IDefaultValueOperation
}


var memberRefOrInvokeOp = Core.UnpackNullCoalesceOperation(op);
var memberRefOrInvokeOp = Core.UnwrapNullCoalesceOperation(op);

// member reference!!
// --> disposable.Property;
Expand All @@ -381,7 +382,7 @@ or IDefaultValueOperation
}
else
{
var parentOp = Core.UnpackNullCoalesceOperation(memberRefOrInvokeOp.Parent);
var parentOp = Core.UnwrapNullCoalesceOperation(memberRefOrInvokeOp.Parent);
if (parentOp != null)
{
if (parentOp is IMemberReferenceOperation or ILocalReferenceOperation)
Expand Down Expand Up @@ -451,7 +452,7 @@ or IDefaultValueOperation
// NOTE: remove parenthesizes and null warning suppressor!!
// --> (((new Disposable()))) --> new Disposable()
// --> (new Disposable())! --> new Disposable()
syntax = Core.UnpackParenthesizeAndNullCoalesceNodes(syntax);
syntax = Core.UnwrapParenthesizeAndNullSuppressorNodes(syntax);


// return statement?
Expand Down Expand Up @@ -529,7 +530,7 @@ or IDefaultValueOperation
}
}
// --> if (disposable == ...)
// --> switch (disposable) ...
// --> while (disposable == ...)
else if (op.Parent is IBinaryOperation)
{
// don't allow creation operation pass the warning
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
/* Core ================================================================ */
#define STMG_DEBUG_MESSAGE // some try-catch will be enabled
#define STMG_DEBUG_MESSAGE
#if DEBUG == false
#undef STMG_DEBUG_MESSAGE
#endif

#if STMG_DEBUG_MESSAGE
//#define STMG_DEBUG_MESSAGE_VERBOSE // for debugging. many of additional debug diagnostics will be emitted
#endif
/* /Core ================================================================ */

#define STMG_USE_ATTRIBUTE_CACHE
#define STMG_USE_DESCRIPTION_CACHE
//#define STMG_ENABLE_LINE_FILL

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -206,23 +199,12 @@ public override void Initialize(AnalysisContext context)

//https://github.com/dotnet/roslyn/blob/main/docs/analyzers/Analyzer%20Actions%20Semantics.md

/* = symbol = */

context.RegisterSymbolAction(DrawUnderlineOnSymbols, cache_descriptionTargetSymbols);


/* = syntax = */

context.RegisterSyntaxNodeAction(DrawUnderlineOnSyntaxNodes, cache_descriptionTargetSyntaxes);


/* = operation = */

context.RegisterOperationAction(DrawUnderlineOnOperators, cache_descriptionTargetOperations);


/* = cache = */

context.RegisterCompilationStartAction(InitializeAndRegisterCallbacks);
}

Expand Down Expand Up @@ -323,14 +305,6 @@ private static void DrawUnderlineOnSymbols(SymbolAnalysisContext context)
var symbolToDescription = (ts_symbolToDescription ??= new());
var descAttrToMessage = (ts_descAttrToMessage ??= new());

#if STMG_DEBUG_MESSAGE_VERBOSE
Core.ReportDebugMessage(context.ReportDiagnostic,
"DEBUG SYMBOL",
GetAndUpdateDescriptionCache(symbol, symbolToDescription, /*filePathToModel,*/ descAttrToMessage, compilation, token)
?? "=== NO DESCRIPTION === " + symbol.Name,
symbol.Locations);
#endif

Underlining(symbol.Locations, symbol, Rule_DeclarationDesc,
reportAction, symbolToDescription, /*filePathToModel,*/ descAttrToMessage, compilation, token, 0);

Expand Down Expand Up @@ -407,13 +381,6 @@ private static void DrawUnderlineOnSyntaxNodes(SyntaxNodeAnalysisContext context
goto RERUN_SIMPLE_BASE_TYPE;
}

#if STMG_DEBUG_MESSAGE_VERBOSE
Core.ReportDebugMessage(context.ReportDiagnostic,
"DEBUG NODE (NOT FOUND)",
"=== SYMBOL NOT FOUND ===",
singleLocation);
#endif

symbol = symbolCandidate.CandidateSymbols.FirstOrDefault();
if (symbol == null)
return;
Expand All @@ -425,14 +392,6 @@ private static void DrawUnderlineOnSyntaxNodes(SyntaxNodeAnalysisContext context
}
else if (symbol is IParameterSymbol paramSymbol)
{
#if STMG_DEBUG_MESSAGE_VERBOSE
Core.ReportDebugMessage(context.ReportDiagnostic,
"DEBUG NODE (PARAM)",
GetAndUpdateDescriptionCache(symbol, symbolToDescription, /*filePathToModel,*/ descAttrToMessage, compilation, token)
?? "=== NO DESCRIPTION === " + symbol.Name,
singleLocation);
#endif

// NOTE: draw underline on methodParameter --> void Method([DescriptionAttribute] T methodParameter);
Underlining(singleLocation, symbol, rule,
reportAction, symbolToDescription, /*filePathToModel,*/ descAttrToMessage, compilation, token,
Expand All @@ -444,14 +403,6 @@ private static void DrawUnderlineOnSyntaxNodes(SyntaxNodeAnalysisContext context
}


#if STMG_DEBUG_MESSAGE_VERBOSE
Core.ReportDebugMessage(context.ReportDiagnostic,
"DEBUG NODE: " + syntax.Kind(),
GetAndUpdateDescriptionCache(symbol, symbolToDescription, /*filePathToModel,*/ descAttrToMessage, compilation, token)
?? "=== NO DESCRIPTION === " + symbol.Name,
singleLocation);
#endif

if (symbol.IsImplicitlyDeclared)
return;

Expand Down Expand Up @@ -504,10 +455,6 @@ CancellationToken token

if (lambdaType == null)
{
#if STMG_DEBUG_MESSAGE_VERBOSE
Core.ReportDebugMessage(reportAction, "LAMBDA PARAM TYPE NOT FOUND", syntax.Kind().ToString(), syntax.GetLocation());
#endif

return;
}

Expand Down Expand Up @@ -757,21 +704,10 @@ int dotTokenCount

if (description == null)
{
#if STMG_DEBUG_MESSAGE_VERBOSE
Core.ReportDebugMessage(reportAction,
targetSymbol.GetType().Name,
"=== NO DESCRIPTION === " + targetSymbol,
locations);
#endif

goto NEXT;
}


#if STMG_DEBUG_MESSAGE_VERBOSE
description += $" cache:{new Random().Next()}";
#endif

var args = GetMessageFormatArgs(targetSymbol, description);
foreach (var location in locations)
{
Expand Down Expand Up @@ -1121,10 +1057,6 @@ CancellationToken token
description = "Take care to use (no details provided)"; //SpanConcat(span, " has Description attribute w/o args".AsSpan());
}

#if STMG_DEBUG_MESSAGE_VERBOSE
description += $" (from:{symbol.Kind})";
#endif


#if STMG_USE_DESCRIPTION_CACHE
// NOTE: existence checked is done in caller!!
Expand Down Expand Up @@ -1195,17 +1127,6 @@ object[] messageArgs
Rule_LineLeadingDesc, tree.GetLocation(new(start, length)), messageArgs));
}
}

#if STMG_ENABLE_LINE_FILL
start += offset;
int linefillEnd = locStart - 1;

// add line fill
if (start < linefillEnd)
{
__DrawUnderlinePerChar(reportAction, tree, start, linefillEnd, Rule_LineFillDesc, messageArgs);
}
#endif
}

//lineEnd!!
Expand Down
12 changes: 6 additions & 6 deletions src/StaticMemberAnalyzer.Analysis/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ internal static void ReportDebugMessage(Action<Diagnostic> reportMethod, IOperat
[CallerLineNumber] int lineNumber = -1
)
{
op = UnpackNullCoalesceOperation(op);
op = UnwrapNullCoalesceOperation(op);

ReportDebugMessage(reportMethod, $"{callerMember}\n#{lineNumber}", ImmutableArray.Create(location),
$"Op: {op.Kind} ({op.Type?.Name})",
$"Parent: {op.Parent?.UnpackNullCoalesceOperation().Kind} ({op.Parent?.Type?.Name})",
$"Grand Parent: {op.Parent?.Parent?.UnpackNullCoalesceOperation().Kind} ({op.Parent?.Parent?.Type?.Name})",
$"Parent: {op.Parent?.UnwrapNullCoalesceOperation().Kind} ({op.Parent?.Type?.Name})",
$"Grand Parent: {op.Parent?.Parent?.UnwrapNullCoalesceOperation().Kind} ({op.Parent?.Parent?.Type?.Name})",
"> " + new string(op.Syntax?.ToString().Take(72).ToArray()),
$"Child: {op.Children?.FirstOrDefault()?.UnpackNullCoalesceOperation().Kind} ({op.Children?.FirstOrDefault().Type?.Name})"
$"Child: {op.Children?.FirstOrDefault()?.UnwrapNullCoalesceOperation().Kind} ({op.Children?.FirstOrDefault()?.Type?.Name})"
);
}

Expand Down Expand Up @@ -214,7 +214,7 @@ internal static void ReportDebugMessage<T>(Action<Diagnostic> reportMethod, stri

/* node & operation ================================================================ */

internal static SyntaxNode UnpackParenthesizeAndNullCoalesceNodes(this SyntaxNode syntax)
internal static SyntaxNode UnwrapParenthesizeAndNullSuppressorNodes(this SyntaxNode syntax)
{
while (syntax.Parent is ParenthesizedExpressionSyntax || syntax.Parent.IsKind(SyntaxKind.SuppressNullableWarningExpression))
{
Expand All @@ -224,7 +224,7 @@ internal static SyntaxNode UnpackParenthesizeAndNullCoalesceNodes(this SyntaxNod
}


internal static IOperation UnpackNullCoalesceOperation(this IOperation op)
internal static IOperation UnwrapNullCoalesceOperation(this IOperation op)
{
return (op as IConditionalAccessOperation)?.Operation ?? op;
}
Expand Down

0 comments on commit ca08e33

Please sign in to comment.