1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
//
// MonoTODODiagnosticAnalyzer.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
//
// 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.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using ICSharpCode.NRefactory6.CSharp;
using System.Threading;
namespace MonoDevelop.CSharp.Diagnostics.MonoTODODiagnostic
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
sealed class MonoTODODiagnosticAnalyzer : DiagnosticAnalyzer
{
static readonly ImmutableArray<SyntaxKind> syntaxKindsOfInterest = ImmutableArray.Create(
SyntaxKind.IdentifierName, // foo
SyntaxKind.SimpleMemberAccessExpression, // foo.bar
SyntaxKind.PointerMemberAccessExpression, // foo->bar
SyntaxKind.ConditionalAccessExpression // foo?.bar
);
static readonly DiagnosticDescriptor descriptor = new DiagnosticDescriptor(
IDEDiagnosticIds.MonoTODODiagnosticDiagnosticId,
"Find usages of mono todo items",
"{0}",
DiagnosticAnalyzerCategories.Notifications,
DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics {
get {
return ImmutableArray.Create(descriptor);
}
}
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(
(nodeContext) => {
Diagnostic diagnostic;
if (TryFindMonoTODO(nodeContext.SemanticModel, nodeContext.Node, out diagnostic, nodeContext.CancellationToken))
nodeContext.ReportDiagnostic (diagnostic);
},
syntaxKindsOfInterest);
}
static readonly Dictionary<string, string> attributes = new Dictionary<string, string> {
{ "MonoTODOAttribute", "Mono TODO" },
{ "MonoNotSupportedAttribute", "Mono NOT SUPPORTED" },
{ "MonoLimitationAttribute", "Mono LIMITATION" }
};
bool TryFindMonoTODO (SemanticModel semanticModel, SyntaxNode node, out Diagnostic diagnostic, CancellationToken cancellationToken)
{
var info = semanticModel.GetSymbolInfo (node);
diagnostic = default(Diagnostic);
if (info.Symbol == null || semanticModel.IsFromGeneratedCode (cancellationToken))
return false;
foreach (var attr in info.Symbol.GetAttributes ()) {
if (attr.AttributeClass.ContainingNamespace.GetFullName () != "System")
continue;
string val;
if (attributes.TryGetValue (attr.AttributeClass.Name, out val)) {
string msg = null;
if (attr.ConstructorArguments.Length > 0) {
var arg = attr.ConstructorArguments [0];
msg = arg.Value != null ? arg.Value.ToString () : null;
}
var tree = semanticModel.SyntaxTree;
diagnostic = Diagnostic.Create(descriptor, tree.GetLocation(node.Span), string.IsNullOrEmpty (msg) ? val : val + ": " + msg);
return true;
}
}
return false;
}
}
}
|