/
Program.cs
616 lines (510 loc) · 25.7 KB
/
Program.cs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Analyzer.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
namespace GenerateAnalyzerRulesets
{
public static class Program
{
public static int Main(string[] args)
{
const int expectedArguments = 13;
if (args.Length != expectedArguments)
{
Console.Error.WriteLine($"Excepted {expectedArguments} arguments, found {args.Length}: {string.Join(';', args)}");
return 1;
}
string analyzerRulesetsDir = args[0];
string binDirectory = args[1];
string configuration = args[2];
string tfm = args[3];
var assemblyList = args[4].Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
string propsFileDir = args[5];
string propsFileName = args[6];
string analyzerDocumentationFileDir = args[7];
string analyzerDocumentationFileName = args[8];
string analyzerSarifFileDir = args[9];
string analyzerSarifFileName = args[10];
var analyzerVersion = args[11];
if (!bool.TryParse(args[12], out var containsPortedFxCopRules))
{
containsPortedFxCopRules = false;
}
var allRulesByAssembly = new SortedList<string, SortedList<string, DiagnosticDescriptor>>();
var allRulesById = new SortedList<string, DiagnosticDescriptor>();
var fixableDiagnosticIds = new HashSet<string>();
var categories = new HashSet<string>();
var rulesMetadata = new SortedList<string, (string path, SortedList<string, (DiagnosticDescriptor rule, string typeName, string[] languages)> rules)>();
foreach (string assembly in assemblyList)
{
var assemblyName = Path.GetFileNameWithoutExtension(assembly);
string path = Path.Combine(binDirectory, assemblyName, configuration, tfm, assembly);
if (!File.Exists(path))
{
Console.Error.WriteLine($"'{path}' does not exist");
return 1;
}
var analyzerFileReference = new AnalyzerFileReference(path, AnalyzerAssemblyLoader.Instance);
var analyzers = analyzerFileReference.GetAnalyzersForAllLanguages();
var rulesById = new SortedList<string, DiagnosticDescriptor>();
var assemblyRulesMetadata = (path, rules: new SortedList<string, (DiagnosticDescriptor rule, string typeName, string[] languages)>());
foreach (var analyzer in analyzers)
{
var analyzerType = analyzer.GetType();
foreach (var rule in analyzer.SupportedDiagnostics)
{
rulesById[rule.Id] = rule;
allRulesById[rule.Id] = rule;
categories.Add(rule.Category);
assemblyRulesMetadata.rules[rule.Id] = (rule, analyzerType.Name, analyzerType.GetCustomAttribute<DiagnosticAnalyzerAttribute>(true)?.Languages);
}
}
allRulesByAssembly.Add(assemblyName, rulesById);
rulesMetadata.Add(assemblyName, assemblyRulesMetadata);
foreach (var id in analyzerFileReference.GetFixers().SelectMany(fixer => fixer.FixableDiagnosticIds))
{
fixableDiagnosticIds.Add(id);
}
}
createRuleset(
"AllRulesDefault.ruleset",
"All Rules with default action",
@"All Rules with default action. Rules with IsEnabledByDefault = false are disabled.",
RulesetKind.AllDefault);
createRuleset(
"AllRulesEnabled.ruleset",
"All Rules Enabled with default action",
"All Rules are enabled with default action. Rules with IsEnabledByDefault = false are force enabled with default action.",
RulesetKind.AllEnabled);
createRuleset(
"AllRulesDisabled.ruleset",
"All Rules Disabled",
@"All Rules are disabled.",
RulesetKind.AllDisabled);
foreach (var category in categories)
{
createRuleset(
$"{category}RulesDefault.ruleset",
$"{category} Rules with default action",
$@"All {category} Rules with default action. Rules with IsEnabledByDefault = false or from a different category are disabled.",
RulesetKind.CategoryDefault,
categoryOpt: category);
createRuleset(
$"{category}RulesEnabled.ruleset",
$"{category} Rules Enabled with default action",
$@"All {category} Rules are enabled with default action. {category} Rules with IsEnabledByDefault = false are force enabled with default action. Rules from a different category are disabled.",
RulesetKind.CategoryEnabled,
categoryOpt: category);
}
// We generate custom tag based rulesets only for select custom tags.
var customTagsToGenerateRulesets = ImmutableArray.Create(
WellKnownDiagnosticTagsExtensions.Dataflow,
FxCopWellKnownDiagnosticTags.PortedFromFxCop);
foreach (var customTag in customTagsToGenerateRulesets)
{
createRuleset(
$"{customTag}RulesDefault.ruleset",
$"{customTag} Rules with default action",
$@"All {customTag} Rules with default action. Rules with IsEnabledByDefault = false and non-{customTag} rules are disabled.",
RulesetKind.CustomTagDefault,
customTagOpt: customTag);
createRuleset(
$"{customTag}RulesEnabled.ruleset",
$"{customTag} Rules Enabled with default action",
$@"All {customTag} Rules are enabled with default action. {customTag} Rules with IsEnabledByDefault = false are force enabled with default action. Non-{customTag} Rules are disabled.",
RulesetKind.CustomTagEnabled,
customTagOpt: customTag);
}
createPropsFile();
createAnalyzerDocumentationFile();
createAnalyzerSarifFile();
return 0;
// Local functions.
void createRuleset(
string rulesetFileName,
string rulesetName,
string rulesetDescription,
RulesetKind rulesetKind,
string categoryOpt = null,
string customTagOpt = null)
{
Debug.Assert(categoryOpt == null || customTagOpt == null);
Debug.Assert((categoryOpt != null) == (rulesetKind == RulesetKind.CategoryDefault || rulesetKind == RulesetKind.CategoryEnabled));
Debug.Assert((customTagOpt != null) == (rulesetKind == RulesetKind.CustomTagDefault || rulesetKind == RulesetKind.CustomTagEnabled));
var result = new StringBuilder();
startRuleset();
if (categoryOpt == null && customTagOpt == null)
{
addRules(categoryPass: false, customTagPass: false);
}
else
{
result.AppendLine($@" <!-- {categoryOpt ?? customTagOpt} Rules -->");
addRules(categoryPass: categoryOpt != null, customTagPass: customTagOpt != null);
result.AppendLine();
result.AppendLine($@" <!-- Other Rules -->");
addRules(categoryPass: false, customTagPass: false);
}
endRuleset();
var directory = Directory.CreateDirectory(analyzerRulesetsDir);
var rulesetFilePath = Path.Combine(directory.FullName, rulesetFileName);
File.WriteAllText(rulesetFilePath, result.ToString());
return;
void startRuleset()
{
result.AppendLine(@"<?xml version=""1.0""?>");
result.AppendLine($@"<RuleSet Name=""{rulesetName}"" Description=""{rulesetDescription}"" ToolsVersion=""15.0"">");
}
void endRuleset()
{
result.AppendLine("</RuleSet>");
}
void addRules(bool categoryPass, bool customTagPass)
{
foreach (var rulesByAssembly in allRulesByAssembly)
{
string assemblyName = rulesByAssembly.Key;
SortedList<string, DiagnosticDescriptor> sortedRulesById = rulesByAssembly.Value;
if (!sortedRulesById.Any(r => !shouldSkipRule(r.Value)))
{
// Bail out if we don't have any rule to be added for this assembly.
continue;
}
startRules(assemblyName);
foreach (var rule in sortedRulesById)
{
addRule(rule.Value);
}
endRules();
}
return;
void startRules(string assemblyName)
{
result.AppendLine($@" <Rules AnalyzerId=""{assemblyName}"" RuleNamespace=""{assemblyName}"">");
}
void endRules()
{
result.AppendLine(" </Rules>");
}
void addRule(DiagnosticDescriptor rule)
{
if (shouldSkipRule(rule))
{
return;
}
string ruleAction = getRuleAction(rule);
var spacing = new string(' ', count: 15 - ruleAction.Length);
result.AppendLine($@" <Rule Id=""{rule.Id}"" Action=""{ruleAction}"" /> {spacing} <!-- {rule.Title} -->");
}
bool shouldSkipRule(DiagnosticDescriptor rule)
{
switch (rulesetKind)
{
case RulesetKind.CategoryDefault:
case RulesetKind.CategoryEnabled:
if (categoryPass)
{
return rule.Category != categoryOpt;
}
else
{
return rule.Category == categoryOpt;
}
case RulesetKind.CustomTagDefault:
case RulesetKind.CustomTagEnabled:
if (customTagPass)
{
return !rule.CustomTags.Contains(customTagOpt);
}
else
{
return rule.CustomTags.Contains(customTagOpt);
}
default:
return false;
}
}
string getRuleAction(DiagnosticDescriptor rule)
{
switch (rulesetKind)
{
case RulesetKind.CategoryDefault:
return getRuleActionCore(enable: categoryPass && rule.IsEnabledByDefault);
case RulesetKind.CategoryEnabled:
return getRuleActionCore(enable: categoryPass);
case RulesetKind.CustomTagDefault:
return getRuleActionCore(enable: customTagPass && rule.IsEnabledByDefault);
case RulesetKind.CustomTagEnabled:
return getRuleActionCore(enable: customTagPass);
case RulesetKind.AllDefault:
return getRuleActionCore(enable: rule.IsEnabledByDefault);
case RulesetKind.AllEnabled:
return getRuleActionCore(enable: true);
case RulesetKind.AllDisabled:
return getRuleActionCore(enable: false);
default:
throw new InvalidProgramException();
}
string getRuleActionCore(bool enable)
{
if (enable)
{
return rule.DefaultSeverity.ToString();
}
else
{
return "None";
}
}
}
}
}
void createPropsFile()
{
if (string.IsNullOrEmpty(propsFileDir) || string.IsNullOrEmpty(propsFileName))
{
Debug.Assert(!containsPortedFxCopRules);
return;
}
var fileContents =
$@"<Project DefaultTargets=""Build"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
{getEditorConfigAsAdditionalFile()}{getCodeAnalysisTreatWarningsNotAsErrors()}{getRulesetOverrides()}{getFlowAnalysisFeatureFlag()}
</Project>";
var directory = Directory.CreateDirectory(propsFileDir);
var fileWithPath = Path.Combine(directory.FullName, propsFileName);
File.WriteAllText(fileWithPath, fileContents);
}
static string getFlowAnalysisFeatureFlag()
{
return @"
<PropertyGroup>
<Features>$(Features);flow-analysis</Features>
</PropertyGroup>";
}
string getCodeAnalysisTreatWarningsNotAsErrors()
{
var allRuleIds = string.Join(';', allRulesByAssembly.Values.SelectMany(l => l.Keys).Distinct());
return $@"
<!--
This property group prevents the rule ids implemented in this package to be bumped to errors when
the 'CodeAnalysisTreatWarningsAsErrors' = 'false'.
-->
<PropertyGroup Condition=""'$(CodeAnalysisTreatWarningsAsErrors)' == 'false'"">
<WarningsNotAsErrors>$(WarningsNotAsErrors);{allRuleIds}</WarningsNotAsErrors>
</PropertyGroup>";
}
string getRulesetOverrides()
{
if (containsPortedFxCopRules)
{
var rulesetOverridesBuilder = new StringBuilder();
foreach (var category in categories.OrderBy(k => k))
{
// Each rule entry format is: -[Category]#[ID];
// For example, -Microsoft.Design#CA1001;
var categoryPrefix = $" -Microsoft.{category}#";
var entries = allRulesByAssembly.Values
.SelectMany(l => l)
.Where(ruleIdAndDescriptor => ruleIdAndDescriptor.Value.Category == category &&
FxCopWellKnownDiagnosticTags.IsPortedFxCopRule(ruleIdAndDescriptor.Value))
.OrderBy(ruleIdAndDescriptor => ruleIdAndDescriptor.Key)
.Select(ruleIdAndDescriptor => $"{categoryPrefix}{ruleIdAndDescriptor.Key};")
.Distinct();
if (entries.Any())
{
rulesetOverridesBuilder.AppendLine();
rulesetOverridesBuilder.Append(string.Join(Environment.NewLine, entries));
rulesetOverridesBuilder.AppendLine();
}
}
if (rulesetOverridesBuilder.Length > 0)
{
return $@"
<!--
This property group contains the rules that have been implemented in this package and therefore should be disabled for the binary FxCop.
The format is -[Category]#[ID], e.g., -Microsoft.Design#CA1001;
-->
<PropertyGroup>
<CodeAnalysisRuleSetOverrides>
$(CodeAnalysisRuleSetOverrides);{rulesetOverridesBuilder.ToString()}
</CodeAnalysisRuleSetOverrides>
</PropertyGroup>";
}
}
return string.Empty;
}
static string getEditorConfigAsAdditionalFile()
{
return $@"
<!--
This item group adds any .editorconfig file present at the project root directory
as an additional file.
-->
<ItemGroup Condition=""'$(SkipDefaultEditorConfigAsAdditionalFile)' != 'true' And Exists('$(MSBuildProjectDirectory)\.editorconfig')"" >
<AdditionalFiles Include=""$(MSBuildProjectDirectory)\.editorconfig"" />
</ItemGroup>
";
}
void createAnalyzerDocumentationFile()
{
if (string.IsNullOrEmpty(analyzerDocumentationFileDir) || string.IsNullOrEmpty(analyzerDocumentationFileName) || allRulesById.Count == 0)
{
Debug.Assert(!containsPortedFxCopRules);
return;
}
var directory = Directory.CreateDirectory(analyzerDocumentationFileDir);
var fileWithPath = Path.Combine(directory.FullName, analyzerDocumentationFileName);
var builder = new StringBuilder();
builder.Append(@"
Sr. No. | Rule ID | Title | Category | Enabled | CodeFix | Description |
--------|---------|-------|----------|---------|---------|--------------------------------------------------------------------------------------------------------------|
");
var index = 1;
foreach (var ruleById in allRulesById)
{
string ruleId = ruleById.Key;
DiagnosticDescriptor descriptor = ruleById.Value;
var ruleIdWithHyperLink = descriptor.Id;
if (!string.IsNullOrWhiteSpace(descriptor.HelpLinkUri))
{
ruleIdWithHyperLink = $"[{ruleIdWithHyperLink}]({descriptor.HelpLinkUri})";
}
var hasCodeFix = fixableDiagnosticIds.Contains(descriptor.Id);
var description = descriptor.Description.ToString();
if (string.IsNullOrWhiteSpace(description))
{
description = descriptor.MessageFormat.ToString();
}
builder.AppendLine($"{index} | {ruleIdWithHyperLink} | {descriptor.Title} | {descriptor.Category} | {descriptor.IsEnabledByDefault} | {hasCodeFix} | {description} |");
index++;
}
File.WriteAllText(fileWithPath, builder.ToString());
}
// based on https://github.com/dotnet/roslyn/blob/master/src/Compilers/Core/Portable/CommandLine/ErrorLogger.cs
void createAnalyzerSarifFile()
{
if (string.IsNullOrEmpty(analyzerSarifFileDir) || string.IsNullOrEmpty(analyzerSarifFileName) || allRulesById.Count == 0)
{
Debug.Assert(!containsPortedFxCopRules);
return;
}
var culture = new CultureInfo("en-us");
var directory = Directory.CreateDirectory(analyzerSarifFileDir);
var fileWithPath = Path.Combine(directory.FullName, analyzerSarifFileName);
using var textWriter = new StreamWriter(fileWithPath, false, Encoding.UTF8);
using var writer = new Roslyn.Utilities.JsonWriter(textWriter);
writer.WriteObjectStart(); // root
writer.Write("$schema", "http://json.schemastore.org/sarif-1.0.0");
writer.Write("version", "1.0.0");
writer.WriteArrayStart("runs");
foreach (var assemblymetadata in rulesMetadata)
{
writer.WriteObjectStart(); // run
writer.WriteObjectStart("tool");
writer.Write("name", assemblymetadata.Key);
if (!string.IsNullOrWhiteSpace(analyzerVersion))
{
writer.Write("version", analyzerVersion);
}
writer.Write("language", culture.Name);
writer.WriteObjectEnd(); // tool
writer.WriteObjectStart("rules"); // rules
foreach (var rule in assemblymetadata.Value.rules)
{
var ruleId = rule.Key;
var descriptor = rule.Value.rule;
writer.WriteObjectStart(descriptor.Id); // rule
writer.Write("id", descriptor.Id);
writer.Write("shortDescription", descriptor.Title.ToString(culture));
string fullDescription = descriptor.Description.ToString(culture);
writer.Write("fullDescription", !string.IsNullOrEmpty(fullDescription) ? fullDescription : descriptor.MessageFormat.ToString());
writer.Write("defaultLevel", getLevel(descriptor.DefaultSeverity));
if (!string.IsNullOrEmpty(descriptor.HelpLinkUri))
{
writer.Write("helpUri", descriptor.HelpLinkUri);
}
writer.WriteObjectStart("properties");
writer.Write("category", descriptor.Category);
writer.Write("isEnabledByDefault", descriptor.IsEnabledByDefault);
writer.Write("typeName", rule.Value.typeName);
if ((rule.Value.languages?.Length ?? 0) > 0)
{
writer.WriteArrayStart("languages");
foreach (var language in rule.Value.languages.OrderBy(l => l, StringComparer.InvariantCultureIgnoreCase))
{
writer.Write(language);
}
writer.WriteArrayEnd(); // languages
}
if (descriptor.CustomTags.Any())
{
writer.WriteArrayStart("tags");
foreach (string tag in descriptor.CustomTags)
{
writer.Write(tag);
}
writer.WriteArrayEnd(); // tags
}
writer.WriteObjectEnd(); // properties
writer.WriteObjectEnd(); // rule
}
writer.WriteObjectEnd(); // rules
writer.WriteObjectEnd(); // run
}
writer.WriteArrayEnd(); // runs
writer.WriteObjectEnd(); // root
return;
static string getLevel(DiagnosticSeverity severity)
{
switch (severity)
{
case DiagnosticSeverity.Info:
return "note";
case DiagnosticSeverity.Error:
return "error";
case DiagnosticSeverity.Warning:
return "warning";
case DiagnosticSeverity.Hidden:
default:
// hidden diagnostics are not reported on the command line and therefore not currently given to
// the error logger. We could represent it with a custom property in the SARIF log if that changes.
Debug.Assert(false);
goto case DiagnosticSeverity.Warning;
}
}
}
}
private enum RulesetKind
{
AllDefault,
CategoryDefault,
CustomTagDefault,
AllEnabled,
CategoryEnabled,
CustomTagEnabled,
AllDisabled,
}
private sealed class AnalyzerAssemblyLoader : IAnalyzerAssemblyLoader
{
public static IAnalyzerAssemblyLoader Instance = new AnalyzerAssemblyLoader();
private AnalyzerAssemblyLoader() { }
public void AddDependencyLocation(string fullPath)
{
}
public Assembly LoadFromPath(string fullPath)
{
return Assembly.LoadFrom(fullPath);
}
}
}
}