1- // <copyright file="PublicApiAnalyzer .cs" company="Datadog">
1+ // <copyright file="InternalForTestingAnalyzer .cs" company="Datadog">
22// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
33// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
44// </copyright>
1414using Microsoft . CodeAnalysis . Diagnostics ;
1515using Microsoft . CodeAnalysis . Operations ;
1616
17- namespace Datadog . Trace . Tools . Analyzers . PublicApiAnalyzer
17+ namespace Datadog . Trace . Tools . Analyzers . InternalForTestingAnalyzer
1818{
1919 /// <summary>
20- /// DD002: Incorrect usage of public API
20+ /// DD002: Incorrect usage of internal API
2121 ///
22- /// Finds internal usages of APIs specifically marked with the [PublicApi ] flag.
23- /// These methods should not be called directly by our library code, only users should invoke them .
24- /// The analyzer enforces that. requirement
22+ /// Finds internal usages of APIs specifically marked with the [InternalForTesting ] flag.
23+ /// These methods should not be called directly by our library code, only from test code .
24+ /// The analyzer enforces that requirement
2525 ///
2626 /// </summary>
2727 [ DiagnosticAnalyzer ( LanguageNames . CSharp ) ]
28- public sealed class PublicApiAnalyzer : DiagnosticAnalyzer
28+ public sealed class InternalForTestingAnalyzer : DiagnosticAnalyzer
2929 {
3030 /// <summary>
3131 /// The diagnostic ID displayed in error messages
3232 /// </summary>
3333 public const string DiagnosticId = "DD0002" ;
3434
35- private const string PublicApiAttribute = nameof ( PublicApiAttribute ) ;
35+ private const string InternalForTestingAttribute = nameof ( InternalForTestingAttribute ) ;
3636
37- private static readonly ImmutableArray < string > PublicApiAttributeNames = ImmutableArray . Create ( PublicApiAttribute ) ;
37+ private static readonly ImmutableArray < string > InternalForTestingAttributeNames
38+ = ImmutableArray . Create ( InternalForTestingAttribute ) ;
3839
3940#pragma warning disable RS2008 // Enable analyzer release tracking for the analyzer project
4041 private static readonly DiagnosticDescriptor Rule = new (
4142 DiagnosticId ,
42- title : "Incorrect usage of public API" ,
43- messageFormat : "This API is only for public usage and should not be called internally" ,
43+ title : "Incorrect usage of internal API" ,
44+ messageFormat : "This API is only for use in tests and should not be called internally" ,
4445 category : "CodeQuality" ,
4546 defaultSeverity : DiagnosticSeverity . Error ,
4647 isEnabledByDefault : true ,
47- description : "This API is only for public usage and should not be called internally. Use an alternative method." ) ;
48+ description : "This API is only for internal testing and should not be called internally. Use an alternative method." ) ;
4849#pragma warning restore RS2008
4950
5051 /// <inheritdoc />
@@ -58,23 +59,23 @@ public override void Initialize(AnalysisContext context)
5859
5960 context . RegisterCompilationStartAction ( context =>
6061 {
61- var publicApiMembers = new ConcurrentDictionary < ISymbol , PublicApiStatus > ( SymbolEqualityComparer . Default ) ;
62+ var internalApiMembers = new ConcurrentDictionary < ISymbol , InternalApiStatus > ( SymbolEqualityComparer . Default ) ;
6263
6364 context . RegisterOperationBlockStartAction (
64- context => AnalyzeOperationBlock ( context , publicApiMembers ) ) ;
65+ ctx => AnalyzeOperationBlock ( ctx , internalApiMembers ) ) ;
6566 } ) ;
6667 }
6768
68- private void AnalyzeOperationBlock (
69+ private static void AnalyzeOperationBlock (
6970 OperationBlockStartAnalysisContext context ,
70- ConcurrentDictionary < ISymbol , PublicApiStatus > publicApiMembers )
71+ ConcurrentDictionary < ISymbol , InternalApiStatus > internalApiMembers )
7172 {
72- var publicApiOperations = PooledConcurrentDictionary < KeyValuePair < IOperation , ISymbol > , bool > . GetInstance ( ) ;
73+ var internalApiOperations = PooledConcurrentDictionary < KeyValuePair < IOperation , ISymbol > , bool > . GetInstance ( ) ;
7374
7475 context . RegisterOperationAction (
75- context =>
76+ ctx =>
7677 {
77- Helpers . AnalyzeOperation ( context . Operation , publicApiOperations , publicApiMembers ) ;
78+ Helpers . AnalyzeOperation ( ctx . Operation , internalApiOperations , internalApiMembers ) ;
7879 } ,
7980 OperationKind . MethodReference ,
8081 OperationKind . EventReference ,
@@ -83,23 +84,23 @@ private void AnalyzeOperationBlock(
8384 OperationKind . ObjectCreation ,
8485 OperationKind . PropertyReference ) ;
8586
86- context . RegisterOperationBlockEndAction ( context =>
87+ context . RegisterOperationBlockEndAction ( ctx =>
8788 {
8889 try
8990 {
90- if ( publicApiOperations . IsEmpty )
91+ if ( internalApiOperations . IsEmpty )
9192 {
9293 return ;
9394 }
9495
95- foreach ( var kvp in publicApiOperations )
96+ foreach ( var kvp in internalApiOperations )
9697 {
97- context . ReportDiagnostic ( Diagnostic . Create ( Rule , kvp . Key . Key . Syntax . GetLocation ( ) ) ) ;
98+ ctx . ReportDiagnostic ( Diagnostic . Create ( Rule , kvp . Key . Key . Syntax . GetLocation ( ) ) ) ;
9899 }
99100 }
100101 finally
101102 {
102- publicApiOperations . Free ( context . CancellationToken ) ;
103+ internalApiOperations . Free ( ctx . CancellationToken ) ;
103104 }
104105 } ) ;
105106 }
@@ -108,8 +109,8 @@ private static class Helpers
108109 {
109110 internal static void AnalyzeOperation (
110111 IOperation operation ,
111- PooledConcurrentDictionary < KeyValuePair < IOperation , ISymbol > , bool > publicApiOperations ,
112- ConcurrentDictionary < ISymbol , PublicApiStatus > publicApiMembers )
112+ PooledConcurrentDictionary < KeyValuePair < IOperation , ISymbol > , bool > internalApiOperations ,
113+ ConcurrentDictionary < ISymbol , InternalApiStatus > internalApiMembers )
113114 {
114115 var symbol = GetOperationSymbol ( operation ) ;
115116
@@ -184,9 +185,9 @@ void CheckTypeArgumentsCore(ImmutableArray<ITypeSymbol> typeArguments, PooledHas
184185
185186 void CheckOperationAttributes ( ISymbol symbol , bool checkParents )
186187 {
187- if ( TryGetOrCreatePublicApiAttributes ( symbol , checkParents , publicApiMembers , out _ ) )
188+ if ( TryGetOrCreatePublicApiAttributes ( symbol , checkParents , internalApiMembers , out _ ) )
188189 {
189- publicApiOperations . TryAdd ( new KeyValuePair < IOperation , ISymbol > ( operation , symbol ) , true ) ;
190+ internalApiOperations . TryAdd ( new KeyValuePair < IOperation , ISymbol > ( operation , symbol ) , true ) ;
190191 }
191192 }
192193 }
@@ -238,7 +239,7 @@ void CheckOperationAttributes(ISymbol symbol, bool checkParents)
238239 return iEvent ;
239240 }
240241
241- private static PublicApiStatus CopyAttributes ( PublicApiStatus copyAttributes ) =>
242+ private static InternalApiStatus CopyAttributes ( InternalApiStatus copyAttributes ) =>
242243 new ( )
243244 {
244245 IsAssemblyAttribute = copyAttributes . IsAssemblyAttribute ,
@@ -260,8 +261,8 @@ or BinaryOperatorKind.LessThanOrEqual
260261 private static bool TryGetOrCreatePublicApiAttributes (
261262 ISymbol symbol ,
262263 bool checkParents ,
263- ConcurrentDictionary < ISymbol , PublicApiStatus > publicApiMembers ,
264- out PublicApiStatus attributes )
264+ ConcurrentDictionary < ISymbol , InternalApiStatus > publicApiMembers ,
265+ out InternalApiStatus attributes )
265266 {
266267 if ( ! publicApiMembers . TryGetValue ( symbol , out attributes ) )
267268 {
@@ -281,7 +282,7 @@ private static bool TryGetOrCreatePublicApiAttributes(
281282 }
282283 }
283284
284- attributes ??= new PublicApiStatus ( ) { IsAssemblyAttribute = symbol is IAssemblySymbol } ;
285+ attributes ??= new InternalApiStatus ( ) { IsAssemblyAttribute = symbol is IAssemblySymbol } ;
285286 MergePlatformAttributes ( symbol . GetAttributes ( ) , ref attributes ) ;
286287 attributes = publicApiMembers . GetOrAdd ( symbol , attributes ) ;
287288 }
@@ -290,11 +291,11 @@ private static bool TryGetOrCreatePublicApiAttributes(
290291
291292 static void MergePlatformAttributes (
292293 ImmutableArray < AttributeData > immediateAttributes ,
293- ref PublicApiStatus parentAttributes )
294+ ref InternalApiStatus parentAttributes )
294295 {
295296 foreach ( AttributeData attribute in immediateAttributes )
296297 {
297- if ( PublicApiAttributeNames . Contains ( attribute . AttributeClass ! . Name ) )
298+ if ( InternalForTestingAttributeNames . Contains ( attribute . AttributeClass ! . Name ) )
298299 {
299300 parentAttributes . IsPublicApi = true ;
300301 return ;
@@ -304,7 +305,7 @@ static void MergePlatformAttributes(
304305 }
305306 }
306307
307- private sealed class PublicApiStatus
308+ private sealed class InternalApiStatus
308309 {
309310 public bool IsAssemblyAttribute { get ; set ; }
310311
0 commit comments