Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2026 Piero Viano
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Composition;

using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.ILSpyX.Dependencies;

namespace ICSharpCode.ILSpyX.Analyzers.Builtin
{
/// <summary>
/// For an assembly, lists the code in the other assemblies of the list that depends on it: every
/// type or member whose signature, base types, attributes or body uses a type or member of the
/// analyzed assembly. Results are grouped by referencing assembly (assemblies in list order).
/// </summary>
[ExportAnalyzer(Header = ModuleAnalyzerHeaders.DependentCode, Order = 20)]
[Shared]
class ModuleDependentCodeAnalyzer : IAnalyzer
{
public bool Show(ISymbol? symbol) => symbol is IModule { MetadataFile.IsAssembly: true };

public IEnumerable<ISymbol> Analyze(ISymbol analyzedSymbol, AnalyzerContext context)
{
ArgumentNullException.ThrowIfNull(analyzedSymbol);
ArgumentNullException.ThrowIfNull(context);
if (analyzedSymbol is not IModule { MetadataFile: { } target })
throw new ArgumentException("A module with metadata is required.", nameof(analyzedSymbol));
return AnalyzeCore(target, context);
}

static IEnumerable<ISymbol> AnalyzeCore(MetadataFile target, AnalyzerContext context)
{
var ct = context.CancellationToken;
var assemblies = context.AssemblyList.GetAllAssemblies().GetAwaiter().GetResult();
foreach (var assembly in assemblies)
{
ct.ThrowIfCancellationRequested();
var module = assembly.GetMetadataFileOrNull();
if (module == null || module.IsMetadataOnly || ReferenceEquals(module, target))
continue;
var references = DependentCodeScanner.FindReferencesTo(module, target);
if (references.Count == 0)
continue;
var typeSystem = context.GetOrCreateTypeSystem(module);
foreach (var symbol in DependentCodeScanner.FindDependentSymbols(module, typeSystem, references, ct))
yield return symbol;
}
}
}
}
59 changes: 59 additions & 0 deletions ICSharpCode.ILSpyX/Analyzers/Builtin/ModuleReferencedByAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2026 Piero Viano
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Composition;

using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.ILSpyX.Dependencies;

namespace ICSharpCode.ILSpyX.Analyzers.Builtin
{
/// <summary>
/// For an assembly, lists the assemblies of the list that reference it (by name and public key
/// token; the referenced version may differ). Each result is itself a module, so it can be
/// expanded to follow the chain of referencing assemblies further.
/// </summary>
[ExportAnalyzer(Header = ModuleAnalyzerHeaders.ReferencedBy, Order = 10)]
[Shared]
class ModuleReferencedByAnalyzer : IAnalyzer
{
public bool Show(ISymbol? symbol) => symbol is IModule { MetadataFile.IsAssembly: true };

public IEnumerable<ISymbol> Analyze(ISymbol analyzedSymbol, AnalyzerContext context)
{
ArgumentNullException.ThrowIfNull(analyzedSymbol);
ArgumentNullException.ThrowIfNull(context);
if (analyzedSymbol is not IModule { MetadataFile: { } target })
throw new ArgumentException("A module with metadata is required.", nameof(analyzedSymbol));
return AnalyzeCore(target, context);
}

static IEnumerable<ISymbol> AnalyzeCore(MetadataFile target, AnalyzerContext context)
{
var assemblies = context.AssemblyList.GetAllAssemblies().GetAwaiter().GetResult();
foreach (var referencing in ReferencedByFinder.FindReferencingAssemblies(assemblies, target, context.CancellationToken))
{
context.CancellationToken.ThrowIfCancellationRequested();
yield return context.GetOrCreateTypeSystem(referencing.Module).MainModule;
}
}
}
}
30 changes: 30 additions & 0 deletions ICSharpCode.ILSpyX/Analyzers/ModuleAnalyzerHeaders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2026 Piero Viano
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

namespace ICSharpCode.ILSpyX.Analyzers
{
/// <summary>
/// Headers of the built-in analyzers that apply to whole modules, so hosts can locate the
/// matching analyzer row of an analyzed module.
/// </summary>
public static class ModuleAnalyzerHeaders

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary bloat, please remove, if you need constants you can add them closer to the actual use sites.

{
public const string ReferencedBy = "Referenced By";
public const string DependentCode = "Dependent Code";
}
}
Loading