|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using Antlr4.Runtime; |
| 4 | +using Rubberduck.Inspections.Abstract; |
| 5 | +using Rubberduck.Inspections.Results; |
| 6 | +using Rubberduck.Parsing; |
| 7 | +using Rubberduck.Parsing.Grammar; |
| 8 | +using Rubberduck.Parsing.Inspections; |
| 9 | +using Rubberduck.Parsing.Inspections.Abstract; |
| 10 | +using Rubberduck.Parsing.Inspections.Resources; |
| 11 | +using Rubberduck.Parsing.VBA; |
| 12 | +using Rubberduck.VBEditor; |
| 13 | + |
| 14 | +namespace Rubberduck.Inspections.Concrete |
| 15 | +{ |
| 16 | + public class UnhandledOnErrorResumeNextInspection : ParseTreeInspectionBase |
| 17 | + { |
| 18 | + private readonly Dictionary<QualifiedContext<ParserRuleContext>, string> _errorHandlerLabelsMap = |
| 19 | + new Dictionary<QualifiedContext<ParserRuleContext>, string>(); |
| 20 | + private readonly Dictionary<QualifiedContext<ParserRuleContext>, VBAParser.ModuleBodyElementContext> _bodyElementContextsMap = |
| 21 | + new Dictionary<QualifiedContext<ParserRuleContext>, VBAParser.ModuleBodyElementContext>(); |
| 22 | + |
| 23 | + public UnhandledOnErrorResumeNextInspection(RubberduckParserState state, |
| 24 | + CodeInspectionSeverity defaultSeverity = CodeInspectionSeverity.Warning) : base(state, defaultSeverity) |
| 25 | + { |
| 26 | + Listener = new OnErrorStatementListener(_errorHandlerLabelsMap, _bodyElementContextsMap); |
| 27 | + } |
| 28 | + |
| 29 | + public override CodeInspectionType InspectionType => CodeInspectionType.CodeQualityIssues; |
| 30 | + |
| 31 | + public override IInspectionListener Listener { get; } |
| 32 | + |
| 33 | + protected override IEnumerable<IInspectionResult> DoGetInspectionResults() |
| 34 | + { |
| 35 | + return Listener.Contexts |
| 36 | + .Where(result => !IsIgnoringInspectionResultFor(result.ModuleName, result.Context.Start.Line)) |
| 37 | + .Select(result => |
| 38 | + { |
| 39 | + dynamic properties = new PropertyBag(); |
| 40 | + properties.Label = _errorHandlerLabelsMap[result]; |
| 41 | + properties.BodyElement = _bodyElementContextsMap[result]; |
| 42 | + |
| 43 | + return new QualifiedContextInspectionResult(this, InspectionsUI.UnhandledOnErrorResumeNextInspectionResultFormat, result, properties); |
| 44 | + }); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public class OnErrorStatementListener : VBAParserBaseListener, IInspectionListener |
| 49 | + { |
| 50 | + private readonly List<QualifiedContext<ParserRuleContext>> _contexts = new List<QualifiedContext<ParserRuleContext>>(); |
| 51 | + private readonly List<QualifiedContext<ParserRuleContext>> _unhandledContexts = new List<QualifiedContext<ParserRuleContext>>(); |
| 52 | + private readonly List<string> _errorHandlerLabels = new List<string>(); |
| 53 | + private readonly Dictionary<QualifiedContext<ParserRuleContext>, string> _errorHandlerLabelsMap; |
| 54 | + private readonly Dictionary<QualifiedContext<ParserRuleContext>, VBAParser.ModuleBodyElementContext> _bodyElementContextsMap; |
| 55 | + |
| 56 | + private const string LabelPrefix = "ErrorHandler"; |
| 57 | + |
| 58 | + public OnErrorStatementListener(Dictionary<QualifiedContext<ParserRuleContext>, string> errorHandlerLabelsMap, |
| 59 | + Dictionary<QualifiedContext<ParserRuleContext>, VBAParser.ModuleBodyElementContext> bodyElementContextsMap) |
| 60 | + { |
| 61 | + _errorHandlerLabelsMap = errorHandlerLabelsMap; |
| 62 | + _bodyElementContextsMap = bodyElementContextsMap; |
| 63 | + } |
| 64 | + |
| 65 | + public IReadOnlyList<QualifiedContext<ParserRuleContext>> Contexts => _contexts; |
| 66 | + |
| 67 | + public void ClearContexts() |
| 68 | + { |
| 69 | + _contexts.Clear(); |
| 70 | + } |
| 71 | + |
| 72 | + public QualifiedModuleName CurrentModuleName { get; set; } |
| 73 | + |
| 74 | + public override void ExitModuleBodyElement(VBAParser.ModuleBodyElementContext context) |
| 75 | + { |
| 76 | + if (_unhandledContexts.Any()) |
| 77 | + { |
| 78 | + var labelIndex = -1; |
| 79 | + |
| 80 | + foreach (var errorContext in _unhandledContexts) |
| 81 | + { |
| 82 | + _bodyElementContextsMap.Add(errorContext, context); |
| 83 | + |
| 84 | + labelIndex++; |
| 85 | + var labelSuffix = labelIndex == 0 ? "" : labelIndex.ToString(); |
| 86 | + |
| 87 | + while (_errorHandlerLabels.Contains($"{LabelPrefix.ToLower()}{labelSuffix}")) |
| 88 | + { |
| 89 | + labelIndex++; |
| 90 | + labelSuffix = labelIndex == 0 ? "" : labelIndex.ToString(); |
| 91 | + } |
| 92 | + |
| 93 | + _errorHandlerLabelsMap.Add(errorContext, $"{LabelPrefix}{labelSuffix}"); |
| 94 | + } |
| 95 | + |
| 96 | + _contexts.AddRange(_unhandledContexts); |
| 97 | + |
| 98 | + _unhandledContexts.Clear(); |
| 99 | + _errorHandlerLabels.Clear(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public override void ExitOnErrorStmt(VBAParser.OnErrorStmtContext context) |
| 104 | + { |
| 105 | + if (context.RESUME() != null) |
| 106 | + { |
| 107 | + _unhandledContexts.Add(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context)); |
| 108 | + } |
| 109 | + else if (context.GOTO() != null) |
| 110 | + { |
| 111 | + _unhandledContexts.Clear(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public override void ExitIdentifierStatementLabel(VBAParser.IdentifierStatementLabelContext context) |
| 116 | + { |
| 117 | + var labelText = context.unrestrictedIdentifier().identifier().untypedIdentifier().identifierValue().IDENTIFIER().GetText(); |
| 118 | + if (labelText.ToLower().StartsWith(LabelPrefix.ToLower())) |
| 119 | + { |
| 120 | + _errorHandlerLabels.Add(labelText.ToLower()); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments