~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/FormatStringIssues/FormatStringIssue.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// FormatStringIssue.cs
 
3
//
 
4
// Author:
 
5
//       Simon Lindgren <simon.n.lindgren@gmail.com>
 
6
//
 
7
// Copyright (c) 2012 Simon Lindgren
 
8
//
 
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:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
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
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using System.Collections.Generic;
 
28
using ICSharpCode.NRefactory.CSharp.Resolver;
 
29
using System.Linq;
 
30
using ICSharpCode.NRefactory.Utils;
 
31
 
 
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
33
{
 
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
 
39
        {
 
40
                public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
 
41
                {
 
42
                        return new GatherVisitor(context).GetIssues();
 
43
                }
 
44
                
 
45
                class GatherVisitor : GatherVisitorBase<FormatStringIssue>
 
46
                {
 
47
                        readonly BaseRefactoringContext context;
 
48
                        
 
49
                        public GatherVisitor(BaseRefactoringContext context) : base (context)
 
50
                        {
 
51
                                this.context = context;
 
52
                        }
 
53
 
 
54
                        public override void VisitInvocationExpression(InvocationExpression invocationExpression)
 
55
                        {
 
56
                                base.VisitInvocationExpression(invocationExpression);
 
57
 
 
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;
 
62
                                });
 
63
                                if (!hasEligibleArgument)
 
64
                                        return;
 
65
 
 
66
                                var invocationResolveResult = context.Resolve(invocationExpression) as CSharpInvocationResolveResult;
 
67
                                if (invocationResolveResult == null)
 
68
                                        return;
 
69
                                Expression formatArgument;
 
70
                                IList<Expression> formatArguments;
 
71
                                TextLocation formatStart;
 
72
                                if (!FormatStringHelper.TryGetFormattingParameters(invocationResolveResult, invocationExpression,
 
73
                                                                                   out formatArgument, out formatStart, out formatArguments, null)) {
 
74
                                        return;
 
75
                                }
 
76
                                var primitiveArgument = formatArgument as PrimitiveExpression;
 
77
                                if (primitiveArgument == null || !(primitiveArgument.Value is string))
 
78
                                        return;
 
79
                                var format = (string)primitiveArgument.Value;
 
80
                                var parsingResult = context.ParseFormatString(format);
 
81
                                CheckSegments(parsingResult.Segments, formatStart, formatArguments, invocationExpression);
 
82
                        }
 
83
 
 
84
                        void CheckSegments(IList<IFormatStringSegment> segments, TextLocation formatStart, IList<Expression> formatArguments, AstNode anchor)
 
85
                        {
 
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));
 
96
                                                }
 
97
                                                if (formatItem.HasErrors) {
 
98
                                                        var errorMessage = string.Join(Environment.NewLine, errors.Select(error => error.Message).ToArray());
 
99
                                                        string messageFormat;
 
100
                                                        if (errors.Count > 1) {
 
101
                                                                messageFormat = context.TranslateString("Multiple:\n{0}");
 
102
                                                        } else {
 
103
                                                                messageFormat = context.TranslateString("{0}");
 
104
                                                        }
 
105
                                                        AddIssue(segmentStart, segmentEnd, string.Format(messageFormat, errorMessage));
 
106
                                                }
 
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);
 
112
                                                }
 
113
                                        }
 
114
                                }
 
115
                        }
 
116
                }
 
117
        }
 
118
}
 
119