2
// FormatStringIssue.cs
5
// Simon Lindgren <simon.n.lindgren@gmail.com>
7
// Copyright (c) 2012 Simon Lindgren
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using System.Collections.Generic;
28
using ICSharpCode.NRefactory.CSharp.Resolver;
30
using ICSharpCode.NRefactory.Utils;
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
34
[IssueDescription("Format string syntax error",
35
Description = "Finds issues with format strings.",
36
Category = IssueCategories.ConstraintViolations,
37
Severity = Severity.Error)]
38
public class FormatStringIssue : ICodeIssueProvider
40
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
42
return new GatherVisitor(context).GetIssues();
45
class GatherVisitor : GatherVisitorBase<FormatStringIssue>
47
readonly BaseRefactoringContext context;
49
public GatherVisitor(BaseRefactoringContext context) : base (context)
51
this.context = context;
54
public override void VisitInvocationExpression(InvocationExpression invocationExpression)
56
base.VisitInvocationExpression(invocationExpression);
58
// Speed up the inspector by discarding some invocations early
59
var hasEligibleArgument = invocationExpression.Arguments.Any(argument => {
60
var primitiveArg = argument as PrimitiveExpression;
61
return primitiveArg != null && primitiveArg.Value is string;
63
if (!hasEligibleArgument)
66
var invocationResolveResult = context.Resolve(invocationExpression) as CSharpInvocationResolveResult;
67
if (invocationResolveResult == null)
69
Expression formatArgument;
70
IList<Expression> formatArguments;
71
TextLocation formatStart;
72
if (!FormatStringHelper.TryGetFormattingParameters(invocationResolveResult, invocationExpression,
73
out formatArgument, out formatStart, out formatArguments, null)) {
76
var primitiveArgument = formatArgument as PrimitiveExpression;
77
if (primitiveArgument == null || !(primitiveArgument.Value is string))
79
var format = (string)primitiveArgument.Value;
80
var parsingResult = context.ParseFormatString(format);
81
CheckSegments(parsingResult.Segments, formatStart, formatArguments, invocationExpression);
84
void CheckSegments(IList<IFormatStringSegment> segments, TextLocation formatStart, IList<Expression> formatArguments, AstNode anchor)
86
int argumentCount = formatArguments.Count;
87
foreach (var segment in segments) {
88
var errors = segment.Errors.ToList();
89
var formatItem = segment as FormatItem;
90
if (formatItem != null) {
91
var segmentEnd = new TextLocation(formatStart.Line, formatStart.Column + segment.EndLocation + 1);
92
var segmentStart = new TextLocation(formatStart.Line, formatStart.Column + segment.StartLocation + 1);
93
if (formatItem.Index >= argumentCount) {
94
var outOfBounds = context.TranslateString("The index '{0}' is out of bounds of the passed arguments");
95
AddIssue(segmentStart, segmentEnd, string.Format(outOfBounds, formatItem.Index));
97
if (formatItem.HasErrors) {
98
var errorMessage = string.Join(Environment.NewLine, errors.Select(error => error.Message).ToArray());
100
if (errors.Count > 1) {
101
messageFormat = context.TranslateString("Multiple:\n{0}");
103
messageFormat = context.TranslateString("{0}");
105
AddIssue(segmentStart, segmentEnd, string.Format(messageFormat, errorMessage));
107
} else if (segment.HasErrors) {
108
foreach (var error in errors) {
109
var errorStart = new TextLocation(formatStart.Line, formatStart.Column + error.StartLocation + 1);
110
var errorEnd = new TextLocation(formatStart.Line, formatStart.Column + error.EndLocation + 1);
111
AddIssue(errorStart, errorEnd, error.Message);