Skip to content

Commit 97936bc

Browse files
authored
Merge pull request #1838 from TypeCobolTeam/1837_AddRulesViolations
WI #1837 Add Violation class
2 parents 9d80790 + 0b9241a commit 97936bc

File tree

7 files changed

+47
-34
lines changed

7 files changed

+47
-34
lines changed

TypeCobol/Analysis/Violation.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using TypeCobol.Compiler.Diagnostics;
2+
3+
namespace TypeCobol.Analysis
4+
{
5+
/// <summary>
6+
/// Custom diagnostic to represent a Quality Rule Violation
7+
/// </summary>
8+
public class Violation : Diagnostic
9+
{
10+
private const int DIAGNOSTIC_CODE = (int) MessageCode.QualityRuleViolation;
11+
12+
private static DiagnosticMessage ToDiagnosticMessage(Severity severity, string message, string ruleId)
13+
{
14+
//RuleId is stored in ReferenceText, it will then be transferred through LSP as 'source'.
15+
//Diagnostic code is fixed, it means that all diagnostics with code '47' are in fact quality rule violations created by an analyzer.
16+
return new DiagnosticMessage(Category.CodeAnalysis, DIAGNOSTIC_CODE, severity, message, null, 0, ruleId);
17+
}
18+
19+
public string RuleId => Info.ReferenceText;
20+
21+
public Violation(string ruleId, Severity severity, int lineNumber, int columnStart, int columnEnd, string message)
22+
: base(ToDiagnosticMessage(severity, message, ruleId), columnStart, columnEnd, lineNumber)
23+
{
24+
25+
}
26+
}
27+
}

TypeCobol/Compiler/CodeElements/ASTAnalyzerBase.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using JetBrains.Annotations;
34
using TypeCobol.Analysis;
45
using TypeCobol.Compiler.Diagnostics;
@@ -26,19 +27,7 @@ protected ASTAnalyzerBase([NotNull] string identifier)
2627
/// Returns the diagnostics produced by this analyzer.
2728
/// May be empty, but not null.
2829
/// </summary>
29-
public IEnumerable<Diagnostic> Diagnostics
30-
{
31-
get
32-
{
33-
if (_diagnostics != null)
34-
{
35-
foreach (var diagnostic in _diagnostics)
36-
{
37-
yield return diagnostic;
38-
}
39-
}
40-
}
41-
}
30+
public IEnumerable<Diagnostic> Diagnostics => _diagnostics ?? Enumerable.Empty<Diagnostic>();
4231

4332
/// <summary>
4433
/// Returns the result produced by this analyzer.

TypeCobol/Compiler/CupParser/NodeBuilder/SyntaxDrivenAnalyzerBase.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using JetBrains.Annotations;
34
using TypeCobol.Analysis;
45
using TypeCobol.Compiler.Diagnostics;
@@ -28,19 +29,7 @@ protected SyntaxDrivenAnalyzerBase([NotNull] string identifier)
2829
/// Returns the diagnostics produced by this analyzer.
2930
/// May be empty, but not null.
3031
/// </summary>
31-
public IEnumerable<Diagnostic> Diagnostics
32-
{
33-
get
34-
{
35-
if (_diagnostics != null)
36-
{
37-
foreach (var diagnostic in _diagnostics)
38-
{
39-
yield return diagnostic;
40-
}
41-
}
42-
}
43-
}
32+
public IEnumerable<Diagnostic> Diagnostics => _diagnostics ?? Enumerable.Empty<Diagnostic>();
4433

4534
/// <summary>
4635
/// Returns the result produced by this analyzer.

TypeCobol/Compiler/Diagnostics/Diagnostic.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,23 @@ namespace TypeCobol.Compiler.Diagnostics
1212
public class Diagnostic
1313
{
1414
public Diagnostic(MessageCode messageCode, int columnStart, int columnEnd, int lineNumber, params object[] messageArgs)
15+
: this(DiagnosticMessage.GetFromCode[(int)messageCode], columnStart, columnEnd, lineNumber, messageArgs)
1516
{
16-
Info = DiagnosticMessage.GetFromCode[(int)messageCode];
17+
18+
}
19+
20+
protected Diagnostic(DiagnosticMessage info, int columnStart, int columnEnd, int lineNumber, params object[] messageArgs)
21+
{
22+
Info = info;
1723

1824
ColumnStart = Math.Max(columnStart, 0);
19-
ColumnEnd = Math.Max(columnEnd, 0);
25+
ColumnEnd = Math.Max(columnEnd, 0);
2026

2127
Line = lineNumber;
22-
Message = String.Format(Info.MessageTemplate, messageArgs ?? new object[0]);
28+
Message = string.Format(Info.MessageTemplate, messageArgs ?? new object[0]);
2329
if (messageArgs != null)
2430
{
25-
CatchedException = messageArgs.FirstOrDefault(x => x is Exception) as Exception;
31+
CatchedException = messageArgs.OfType<Exception>().FirstOrDefault();
2632
MessageArgs = messageArgs;
2733
}
2834
}

TypeCobol/Compiler/Diagnostics/DiagnosticMessage.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ public enum Category
2222
Syntax = 4,
2323
Semantics = 5,
2424
Generation = 6,
25-
General = 7
25+
General = 7,
26+
CodeAnalysis = 8
2627
}
2728

2829
public class DiagnosticMessage
2930
{
30-
private DiagnosticMessage(Category category, int code, Severity severity, string messageTemplate, ReferenceDocument document, int pageNumber, string referenceText)
31+
internal DiagnosticMessage(Category category, int code, Severity severity, string messageTemplate, ReferenceDocument document, int pageNumber, string referenceText)
3132
{
3233
Category = category;
3334
Code = code;

TypeCobol/Compiler/Diagnostics/MessageCode.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public enum MessageCode
5353
ErrorFormalizedCommentMissplaced = 43,
5454
MultiFormalizedCommentIndicatorMisused = 44,
5555
Info = 45,
56-
AnalyzerFailure = 46
56+
AnalyzerFailure = 46,
57+
QualityRuleViolation = 47 //MessageCode 47 is reserved for violations coming from any analyzer
5758
}
5859
}

TypeCobol/Compiler/Diagnostics/Resources/DiagnosticMessages.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ Category;Code;Severity;MessageTemplate;Document;PageNumber;ReferenceText
4444
5;43;1;Formalized Comment misplaced;0;0;Page
4545
5;44;1;Invalid use of '%'. Should be used with "%<< ... (linebreak) %>>" for MultiLine Comment or "%<<< ... (linebreak) %>>>" for Formalized Comment;0;0;Page
4646
7;45;2;Info: {0};0;0;
47-
7;46;1;CodeAnalysis: analyzer '{0}' failed. Exception message is '{1}';0;0;
47+
8;46;1;CodeAnalysis: analyzer '{0}' failed. Exception message is '{1}';0;0;

0 commit comments

Comments
 (0)