Skip to content

Commit 2970240

Browse files
committed
Remove remaining customized handling for generated code detection
1 parent 2ce8bb2 commit 2970240

17 files changed

Lines changed: 100 additions & 367 deletions

StyleCop.Analyzers/StyleCop.Analyzers/AnalyzerExtensions.cs

Lines changed: 38 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace StyleCop.Analyzers
66
using System;
77
using System.Collections.Concurrent;
88
using System.Collections.Immutable;
9-
using System.Threading;
109
using Microsoft.CodeAnalysis;
1110
using Microsoft.CodeAnalysis.Diagnostics;
1211
using Settings.ObjectModel;
@@ -16,16 +15,6 @@ namespace StyleCop.Analyzers
1615
/// </summary>
1716
internal static class AnalyzerExtensions
1817
{
19-
/// <summary>
20-
/// A cache of the result of computing whether a document has an auto-generated header.
21-
/// </summary>
22-
/// <remarks>
23-
/// This allows many analyzers that run on every token in the file to avoid checking
24-
/// the same state in the document repeatedly.
25-
/// </remarks>
26-
private static Tuple<WeakReference<Compilation>, ConcurrentDictionary<SyntaxTree, bool>> generatedHeaderCache
27-
= Tuple.Create(new WeakReference<Compilation>(null), default(ConcurrentDictionary<SyntaxTree, bool>));
28-
2918
/// <summary>
3019
/// Register an action to be executed at completion of parsing of a code document. A syntax tree action reports
3120
/// diagnostics about the <see cref="SyntaxTree"/> of a document.
@@ -59,89 +48,79 @@ public static void RegisterSyntaxTreeAction(this CompilationStartAnalysisContext
5948
}
6049

6150
/// <summary>
62-
/// Gets or creates a cache which can be used with <see cref="GeneratedCodeAnalysisExtensions"/> methods to
63-
/// efficiently determine whether or not a source file is considered generated.
51+
/// Register an action to be executed at completion of semantic analysis of a <see cref="SyntaxNode"/> with an
52+
/// appropriate kind. A syntax node action can report diagnostics about a <see cref="SyntaxNode"/>, and can also
53+
/// collect state information to be used by other syntax node actions or code block end actions.
6454
/// </summary>
65-
/// <param name="compilation">The compilation which the cache applies to.</param>
66-
/// <returns>A cache which tracks the syntax trees in a compilation which are considered generated.</returns>
67-
public static ConcurrentDictionary<SyntaxTree, bool> GetOrCreateGeneratedDocumentCache(this Compilation compilation)
55+
/// <param name="context">The analysis context.</param>
56+
/// <param name="action">Action to be executed at completion of semantic analysis of a
57+
/// <see cref="SyntaxNode"/>.</param>
58+
/// <param name="syntaxKind">The kind of syntax that should be analyzed.</param>
59+
/// <typeparam name="TLanguageKindEnum">Enum type giving the syntax node kinds of the source language for which
60+
/// the action applies.</typeparam>
61+
public static void RegisterSyntaxNodeAction<TLanguageKindEnum>(this AnalysisContext context, Action<SyntaxNodeAnalysisContext, StyleCopSettings> action, TLanguageKindEnum syntaxKind)
62+
where TLanguageKindEnum : struct
6863
{
69-
var headerCache = generatedHeaderCache;
64+
context.RegisterSyntaxNodeAction(action, LanguageKindArrays<TLanguageKindEnum>.GetOrCreateArray(syntaxKind));
65+
}
7066

71-
Compilation cachedCompilation;
72-
if (!headerCache.Item1.TryGetTarget(out cachedCompilation) || cachedCompilation != compilation)
73-
{
74-
var replacementCache = Tuple.Create(new WeakReference<Compilation>(compilation), new ConcurrentDictionary<SyntaxTree, bool>());
75-
while (true)
67+
/// <summary>
68+
/// Register an action to be executed at completion of semantic analysis of a <see cref="SyntaxNode"/> with an
69+
/// appropriate kind. A syntax node action can report diagnostics about a <see cref="SyntaxNode"/>, and can also
70+
/// collect state information to be used by other syntax node actions or code block end actions.
71+
/// </summary>
72+
/// <param name="context">The analysis context.</param>
73+
/// <param name="action">Action to be executed at completion of semantic analysis of a
74+
/// <see cref="SyntaxNode"/>.</param>
75+
/// <param name="syntaxKinds">The kinds of syntax that should be analyzed.</param>
76+
/// <typeparam name="TLanguageKindEnum">Enum type giving the syntax node kinds of the source language for which
77+
/// the action applies.</typeparam>
78+
public static void RegisterSyntaxNodeAction<TLanguageKindEnum>(this AnalysisContext context, Action<SyntaxNodeAnalysisContext, StyleCopSettings> action, ImmutableArray<TLanguageKindEnum> syntaxKinds)
79+
where TLanguageKindEnum : struct
80+
{
81+
context.RegisterSyntaxNodeAction(
82+
c =>
7683
{
77-
var prior = Interlocked.CompareExchange(ref generatedHeaderCache, replacementCache, headerCache);
78-
if (prior == headerCache)
79-
{
80-
headerCache = replacementCache;
81-
break;
82-
}
83-
84-
headerCache = prior;
85-
if (headerCache.Item1.TryGetTarget(out cachedCompilation) && cachedCompilation == compilation)
86-
{
87-
break;
88-
}
89-
}
90-
}
91-
92-
return headerCache.Item2;
84+
StyleCopSettings settings = context.GetStyleCopSettings(c.Options, c.CancellationToken);
85+
action(c, settings);
86+
},
87+
syntaxKinds);
9388
}
9489

9590
/// <summary>
9691
/// Register an action to be executed at completion of semantic analysis of a <see cref="SyntaxNode"/> with an
9792
/// appropriate kind. A syntax node action can report diagnostics about a <see cref="SyntaxNode"/>, and can also
9893
/// collect state information to be used by other syntax node actions or code block end actions.
9994
/// </summary>
100-
/// <remarks>This method honors exclusions.</remarks>
101-
/// <param name="context">Action will be executed only if the kind of a <see cref="SyntaxNode"/> matches
102-
/// <paramref name="syntaxKind"/>.</param>
95+
/// <param name="context">The analysis context.</param>
10396
/// <param name="action">Action to be executed at completion of semantic analysis of a
10497
/// <see cref="SyntaxNode"/>.</param>
10598
/// <param name="syntaxKind">The kind of syntax that should be analyzed.</param>
10699
/// <typeparam name="TLanguageKindEnum">Enum type giving the syntax node kinds of the source language for which
107100
/// the action applies.</typeparam>
108-
public static void RegisterSyntaxNodeActionHonorExclusions<TLanguageKindEnum>(this CompilationStartAnalysisContext context, Action<SyntaxNodeAnalysisContext, StyleCopSettings> action, TLanguageKindEnum syntaxKind)
101+
public static void RegisterSyntaxNodeAction<TLanguageKindEnum>(this CompilationStartAnalysisContext context, Action<SyntaxNodeAnalysisContext, StyleCopSettings> action, TLanguageKindEnum syntaxKind)
109102
where TLanguageKindEnum : struct
110103
{
111-
context.RegisterSyntaxNodeActionHonorExclusions(action, LanguageKindArrays<TLanguageKindEnum>.GetOrCreateArray(syntaxKind));
104+
context.RegisterSyntaxNodeAction(action, LanguageKindArrays<TLanguageKindEnum>.GetOrCreateArray(syntaxKind));
112105
}
113106

114107
/// <summary>
115108
/// Register an action to be executed at completion of semantic analysis of a <see cref="SyntaxNode"/> with an
116109
/// appropriate kind. A syntax node action can report diagnostics about a <see cref="SyntaxNode"/>, and can also
117110
/// collect state information to be used by other syntax node actions or code block end actions.
118111
/// </summary>
119-
/// <remarks>This method honors exclusions.</remarks>
120-
/// <param name="context">Action will be executed only if the kind of a <see cref="SyntaxNode"/> matches one of
121-
/// the <paramref name="syntaxKinds"/> values.</param>
112+
/// <param name="context">The analysis context.</param>
122113
/// <param name="action">Action to be executed at completion of semantic analysis of a
123114
/// <see cref="SyntaxNode"/>.</param>
124115
/// <param name="syntaxKinds">The kinds of syntax that should be analyzed.</param>
125116
/// <typeparam name="TLanguageKindEnum">Enum type giving the syntax node kinds of the source language for which
126117
/// the action applies.</typeparam>
127-
public static void RegisterSyntaxNodeActionHonorExclusions<TLanguageKindEnum>(this CompilationStartAnalysisContext context, Action<SyntaxNodeAnalysisContext, StyleCopSettings> action, ImmutableArray<TLanguageKindEnum> syntaxKinds)
118+
public static void RegisterSyntaxNodeAction<TLanguageKindEnum>(this CompilationStartAnalysisContext context, Action<SyntaxNodeAnalysisContext, StyleCopSettings> action, ImmutableArray<TLanguageKindEnum> syntaxKinds)
128119
where TLanguageKindEnum : struct
129120
{
130-
Compilation compilation = context.Compilation;
131-
ConcurrentDictionary<SyntaxTree, bool> cache = GetOrCreateGeneratedDocumentCache(compilation);
132-
133121
context.RegisterSyntaxNodeAction(
134122
c =>
135123
{
136-
if (c.IsGenerated(cache))
137-
{
138-
return;
139-
}
140-
141-
// Honor the containing document item's ExcludeFromStylecop=True
142-
// MSBuild metadata, if analyzers have access to it.
143-
//// TODO: code here
144-
145124
StyleCopSettings settings = context.GetStyleCopSettings(c.Options, c.CancellationToken);
146125
action(c, settings);
147126
},

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1600ElementsMustBeDocumented.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ internal class SA1600ElementsMustBeDocumented : DiagnosticAnalyzer
4444
private static readonly ImmutableArray<SyntaxKind> BaseTypeDeclarationKinds =
4545
ImmutableArray.Create(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.EnumDeclaration);
4646

47-
private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
4847
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> BaseTypeDeclarationAction = Analyzer.HandleBaseTypeDeclaration;
4948
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> MethodDeclarationAction = Analyzer.HandleMethodDeclaration;
5049
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> ConstructorDeclarationAction = Analyzer.HandleConstructorDeclaration;
@@ -63,21 +62,18 @@ internal class SA1600ElementsMustBeDocumented : DiagnosticAnalyzer
6362
/// <inheritdoc/>
6463
public override void Initialize(AnalysisContext context)
6564
{
66-
context.RegisterCompilationStartAction(CompilationStartAction);
67-
}
68-
69-
private static void HandleCompilationStart(CompilationStartAnalysisContext context)
70-
{
71-
context.RegisterSyntaxNodeActionHonorExclusions(BaseTypeDeclarationAction, BaseTypeDeclarationKinds);
72-
context.RegisterSyntaxNodeActionHonorExclusions(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
73-
context.RegisterSyntaxNodeActionHonorExclusions(ConstructorDeclarationAction, SyntaxKind.ConstructorDeclaration);
74-
context.RegisterSyntaxNodeActionHonorExclusions(DestructorDeclarationAction, SyntaxKind.DestructorDeclaration);
75-
context.RegisterSyntaxNodeActionHonorExclusions(PropertyDeclarationAction, SyntaxKind.PropertyDeclaration);
76-
context.RegisterSyntaxNodeActionHonorExclusions(IndexerDeclarationAction, SyntaxKind.IndexerDeclaration);
77-
context.RegisterSyntaxNodeActionHonorExclusions(FieldDeclarationAction, SyntaxKind.FieldDeclaration);
78-
context.RegisterSyntaxNodeActionHonorExclusions(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
79-
context.RegisterSyntaxNodeActionHonorExclusions(EventDeclarationAction, SyntaxKind.EventDeclaration);
80-
context.RegisterSyntaxNodeActionHonorExclusions(EventFieldDeclarationAction, SyntaxKind.EventFieldDeclaration);
65+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
66+
67+
context.RegisterSyntaxNodeAction(BaseTypeDeclarationAction, BaseTypeDeclarationKinds);
68+
context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
69+
context.RegisterSyntaxNodeAction(ConstructorDeclarationAction, SyntaxKind.ConstructorDeclaration);
70+
context.RegisterSyntaxNodeAction(DestructorDeclarationAction, SyntaxKind.DestructorDeclaration);
71+
context.RegisterSyntaxNodeAction(PropertyDeclarationAction, SyntaxKind.PropertyDeclaration);
72+
context.RegisterSyntaxNodeAction(IndexerDeclarationAction, SyntaxKind.IndexerDeclaration);
73+
context.RegisterSyntaxNodeAction(FieldDeclarationAction, SyntaxKind.FieldDeclaration);
74+
context.RegisterSyntaxNodeAction(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
75+
context.RegisterSyntaxNodeAction(EventDeclarationAction, SyntaxKind.EventDeclaration);
76+
context.RegisterSyntaxNodeAction(EventFieldDeclarationAction, SyntaxKind.EventFieldDeclaration);
8177
}
8278

8379
private static class Analyzer

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1601PartialElementsMustBeDocumented.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ internal class SA1601PartialElementsMustBeDocumented : DiagnosticAnalyzer
8585
private static readonly ImmutableArray<SyntaxKind> BaseTypeDeclarationKinds =
8686
ImmutableArray.Create(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.InterfaceDeclaration);
8787

88-
private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
8988
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> BaseTypeDeclarationAction = Analyzer.HandleBaseTypeDeclaration;
9089
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> MethodDeclarationAction = Analyzer.HandleMethodDeclaration;
9190

@@ -96,13 +95,10 @@ internal class SA1601PartialElementsMustBeDocumented : DiagnosticAnalyzer
9695
/// <inheritdoc/>
9796
public override void Initialize(AnalysisContext context)
9897
{
99-
context.RegisterCompilationStartAction(CompilationStartAction);
100-
}
98+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
10199

102-
private static void HandleCompilationStart(CompilationStartAnalysisContext context)
103-
{
104-
context.RegisterSyntaxNodeActionHonorExclusions(BaseTypeDeclarationAction, BaseTypeDeclarationKinds);
105-
context.RegisterSyntaxNodeActionHonorExclusions(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
100+
context.RegisterSyntaxNodeAction(BaseTypeDeclarationAction, BaseTypeDeclarationKinds);
101+
context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
106102
}
107103

108104
private static class Analyzer

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1602EnumerationItemsMustBeDocumented.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ internal class SA1602EnumerationItemsMustBeDocumented : DiagnosticAnalyzer
5252
private static readonly DiagnosticDescriptor Descriptor =
5353
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.DocumentationRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
5454

55-
private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
5655
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> EnumMemberDeclarationAction = Analyzer.HandleEnumMemberDeclaration;
5756

5857
/// <inheritdoc/>
@@ -62,12 +61,9 @@ internal class SA1602EnumerationItemsMustBeDocumented : DiagnosticAnalyzer
6261
/// <inheritdoc/>
6362
public override void Initialize(AnalysisContext context)
6463
{
65-
context.RegisterCompilationStartAction(CompilationStartAction);
66-
}
64+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
6765

68-
private static void HandleCompilationStart(CompilationStartAnalysisContext context)
69-
{
70-
context.RegisterSyntaxNodeActionHonorExclusions(EnumMemberDeclarationAction, SyntaxKind.EnumMemberDeclaration);
66+
context.RegisterSyntaxNodeAction(EnumMemberDeclarationAction, SyntaxKind.EnumMemberDeclaration);
7167
}
7268

7369
private static class Analyzer

0 commit comments

Comments
 (0)