-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Port single-file analyzer #1665
Merged
+650
−28
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fea122f
Add single-file analyzer
37db018
Add tests
7f80851
Use str constant
3483269
Update Resources
b7fb580
Update tests
b858171
PR feedback
a103592
Remove mysterious filename from comments
b5fd377
Add helper method for single-file diagnostic verification
90ec623
Refactor MSBuild properties
2254bcf
Add test with PublishSingleFile not set
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace ILLink.RoslynAnalyzer | ||
{ | ||
internal static class AnalyzerOptionsExtensions | ||
{ | ||
public static string? GetMSBuildPropertyValue ( | ||
this AnalyzerOptions options, | ||
string optionName, | ||
Compilation compilation) | ||
{ | ||
// MSBuild property values should be set at compilation level, and cannot have different values per-tree. | ||
// So, we default to first syntax tree. | ||
var tree = compilation.SyntaxTrees.FirstOrDefault (); | ||
if (tree is null) { | ||
return null; | ||
} | ||
|
||
return options.AnalyzerConfigOptionsProvider.GlobalOptions.TryGetValue ( | ||
$"build_property.{optionName}", out var value) | ||
? value | ||
: null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace ILLink.RoslynAnalyzer | ||
{ | ||
internal static class DiagnosticCategory | ||
{ | ||
public const string SingleFile = nameof (SingleFile); | ||
public const string Trimming = nameof (Trimming); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Diagnostics; | ||
using System.Linq; | ||
|
||
namespace ILLink.RoslynAnalyzer | ||
{ | ||
public static class MSBuildPropertyOptionNames | ||
{ | ||
public const string PublishSingleFile = nameof (PublishSingleFile); | ||
public const string IncludeAllContentForSelfExtract = nameof (IncludeAllContentForSelfExtract); | ||
} | ||
|
||
internal static class MSBuildPropertyOptionNamesHelpers | ||
{ | ||
public static void VerifySupportedPropertyOptionName (string propertyOptionName) | ||
mateoatr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
#if DEBUG | ||
Debug.Assert (typeof (MSBuildPropertyOptionNames).GetFields ().Single (f => f.Name == propertyOptionName) != null); | ||
#endif | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace ILLink.RoslynAnalyzer | ||
{ | ||
/// <summary> | ||
/// IL3000, IL3001: Do not use Assembly file path in single-file publish | ||
/// </summary> | ||
[DiagnosticAnalyzer (LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
public sealed class AvoidAssemblyLocationInSingleFile : DiagnosticAnalyzer | ||
{ | ||
public const string IL3000 = nameof (IL3000); | ||
public const string IL3001 = nameof (IL3001); | ||
|
||
private static readonly DiagnosticDescriptor LocationRule = new DiagnosticDescriptor ( | ||
IL3000, | ||
new LocalizableResourceString (nameof (Resources.AvoidAssemblyLocationInSingleFileTitle), | ||
Resources.ResourceManager, typeof (Resources)), | ||
new LocalizableResourceString (nameof (Resources.AvoidAssemblyLocationInSingleFileMessage), | ||
Resources.ResourceManager, typeof (Resources)), | ||
DiagnosticCategory.SingleFile, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true); | ||
|
||
private static readonly DiagnosticDescriptor GetFilesRule = new DiagnosticDescriptor ( | ||
IL3001, | ||
new LocalizableResourceString (nameof (Resources.AvoidAssemblyGetFilesInSingleFileTitle), | ||
Resources.ResourceManager, typeof (Resources)), | ||
new LocalizableResourceString (nameof (Resources.AvoidAssemblyGetFilesInSingleFileMessage), | ||
Resources.ResourceManager, typeof (Resources)), | ||
DiagnosticCategory.SingleFile, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create (LocationRule, GetFilesRule); | ||
|
||
public override void Initialize (AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution (); | ||
context.ConfigureGeneratedCodeAnalysis (GeneratedCodeAnalysisFlags.ReportDiagnostics); | ||
|
||
context.RegisterCompilationStartAction (context => { | ||
var compilation = context.Compilation; | ||
|
||
var isSingleFilePublish = context.Options.GetMSBuildPropertyValue (MSBuildPropertyOptionNames.PublishSingleFile, compilation); | ||
if (!string.Equals (isSingleFilePublish?.Trim (), "true", StringComparison.OrdinalIgnoreCase)) { | ||
return; | ||
} | ||
var includesAllContent = context.Options.GetMSBuildPropertyValue (MSBuildPropertyOptionNames.IncludeAllContentForSelfExtract, compilation); | ||
if (string.Equals (includesAllContent?.Trim (), "true", StringComparison.OrdinalIgnoreCase)) { | ||
return; | ||
} | ||
|
||
var propertiesBuilder = ImmutableArray.CreateBuilder<IPropertySymbol> (); | ||
var methodsBuilder = ImmutableArray.CreateBuilder<IMethodSymbol> (); | ||
|
||
var assemblyType = compilation.GetTypeByMetadataName ("System.Reflection.Assembly"); | ||
if (assemblyType != null) { | ||
// properties | ||
AddIfNotNull (propertiesBuilder, TryGetSingleSymbol<IPropertySymbol> (assemblyType.GetMembers ("Location"))); | ||
|
||
// methods | ||
methodsBuilder.AddRange (assemblyType.GetMembers ("GetFile").OfType<IMethodSymbol> ()); | ||
methodsBuilder.AddRange (assemblyType.GetMembers ("GetFiles").OfType<IMethodSymbol> ()); | ||
} | ||
|
||
var assemblyNameType = compilation.GetTypeByMetadataName ("System.Reflection.AssemblyName"); | ||
if (assemblyNameType != null) { | ||
AddIfNotNull (propertiesBuilder, TryGetSingleSymbol<IPropertySymbol> (assemblyNameType.GetMembers ("CodeBase"))); | ||
AddIfNotNull (propertiesBuilder, TryGetSingleSymbol<IPropertySymbol> (assemblyNameType.GetMembers ("EscapedCodeBase"))); | ||
} | ||
|
||
var properties = propertiesBuilder.ToImmutable (); | ||
var methods = methodsBuilder.ToImmutable (); | ||
|
||
context.RegisterOperationAction (operationContext => { | ||
var access = (IPropertyReferenceOperation) operationContext.Operation; | ||
var property = access.Property; | ||
if (!Contains (properties, property, SymbolEqualityComparer.Default)) { | ||
return; | ||
} | ||
|
||
operationContext.ReportDiagnostic (Diagnostic.Create (LocationRule, access.Syntax.GetLocation (), property)); | ||
}, OperationKind.PropertyReference); | ||
|
||
context.RegisterOperationAction (operationContext => { | ||
var invocation = (IInvocationOperation) operationContext.Operation; | ||
var targetMethod = invocation.TargetMethod; | ||
if (!Contains (methods, targetMethod, SymbolEqualityComparer.Default)) { | ||
return; | ||
} | ||
|
||
operationContext.ReportDiagnostic (Diagnostic.Create (GetFilesRule, invocation.Syntax.GetLocation (), targetMethod)); | ||
}, OperationKind.Invocation); | ||
|
||
return; | ||
|
||
static bool Contains<T, TComp> (ImmutableArray<T> list, T elem, TComp comparer) | ||
where TComp : IEqualityComparer<T> | ||
{ | ||
foreach (var e in list) { | ||
if (comparer.Equals (e, elem)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
static TSymbol? TryGetSingleSymbol<TSymbol> (ImmutableArray<ISymbol> members) where TSymbol : class, ISymbol | ||
{ | ||
TSymbol? candidate = null; | ||
foreach (var m in members) { | ||
if (m is TSymbol tsym) { | ||
if (candidate is null) { | ||
candidate = tsym; | ||
} else { | ||
return null; | ||
} | ||
} | ||
} | ||
return candidate; | ||
} | ||
|
||
static void AddIfNotNull<TSymbol> (ImmutableArray<TSymbol>.Builder properties, TSymbol? p) where TSymbol : class, ISymbol | ||
{ | ||
if (p != null) { | ||
properties.Add (p); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably need to add
PublishTrimmed
now since otherwise all the trim warnings will automatically be turned on during single-fileThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't be better to think about the more general area than to hardcode the single narrow scenario? Are we going to add a property for each of remaining scenario which would benefit from the same logic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps there's some broader feature work we could do, but we certainly need to recognize whether trimming or single-file are enabled, so we at least need to recognize the two MSBuild properties that already exist.