@@ -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 } ,
0 commit comments