/// <summary>
        /// Initializes a new instance of the Violation class.
        /// </summary>
        /// <param name="rule">
        /// The rule that triggered the violation.
        /// </param>
        /// <param name="element">
        /// The element that this violation appears in.
        /// </param>
        /// <param name="location">
        /// The location in the source code where the violation occurs.
        /// </param>
        /// <param name="message">
        /// The context message for the violation.
        /// </param>
        internal Violation(Rule rule, ICodeElement element, CodeLocation location, string message)
        {
            Param.AssertNotNull(rule, "rule");
            Param.Ignore(element);
            Param.AssertNotNull(location, "location");
            Param.AssertNotNull(message, "message");

            this.rule = rule;
            this.element = element;

            // The CodeLocation passed in is zero based everywhere in StyleCop for the column. The line number is already 1 based.
            // We convert is to 1 based here so that are xml reports etc and VisualStudio UI friendly.
            this.location = new CodeLocation(
                location.StartPoint.Index, 
                location.EndPoint.Index, 
                location.StartPoint.IndexOnLine + 1, 
                location.EndPoint.IndexOnLine + 1, 
                location.StartPoint.LineNumber, 
                location.EndPoint.LineNumber);

            // If the location has been passed in we set the linenumber.
            this.line = location.LineNumber;
            this.message = message;

            if (this.element != null && this.element.Document != null)
            {
                this.sourceCode = this.element.Document.SourceCode;
            }

            this.UpdateKey();
        }
 internal static void Violate(this SourceAnalyzer sourceAnalyzer, CsElement element, CodeLocation location, params object[] args)
 {
     var ruleName = sourceAnalyzer.GetType().Name;
     var rule = sourceAnalyzer.GetRule(ruleName);
     if (rule != null)
     {
         sourceAnalyzer.AddViolation(element, location, ruleName, args);
         DebugWrite(element.Violations.Last());
     }
 }
Beispiel #3
0
        /// <summary>
        /// Adds a generic violation.
        /// </summary>
        /// <param name="element">The element to add the violation to.</param>
        /// <param name="type">The type of violation to add.</param>
        /// <param name="location">The location the violation appears on.</param>
        /// <param name="values">The string values to add to the context string.</param>
        internal void AddViolation(ICodeElement element, Rule type, CodeLocation location, params object[] values)
        {
            Param.Ignore(element);
            Param.AssertNotNull(type, "type");
            Param.AssertNotNull(location, "location");
            Param.Ignore(values);

            // Build up the context string.
            StringBuilder message = new StringBuilder();
            message.AppendFormat(CultureInfo.CurrentCulture, type.Context, values);

            // Create the violation object and add it to the list.
            Violation violation = new Violation(type, element, location, message.ToString());

            // Finally, add the violation.
            this.AddViolation(element, violation);
        }
        /// <summary>
        /// Joins the two given locations.
        /// </summary>
        /// <param name="location1">
        /// The first location to join.
        /// </param>
        /// <param name="location2">
        /// The second location to join.
        /// </param>
        /// <returns>
        /// Returns the joined <see cref="CodeLocation"/>.
        /// </returns>
        public static CodeLocation Join(CodeLocation location1, CodeLocation location2)
        {
            Param.Ignore(location1, location2);

            // Figure out which IndexOnLine and EndIndexOnLine to use.
            int indexOnLine;
            int endIndexOnLine;
            if (location1.StartPoint.LineNumber == location2.StartPoint.LineNumber)
            {
                indexOnLine = Math.Min(location1.StartPoint.IndexOnLine, location2.StartPoint.IndexOnLine);
                endIndexOnLine = Math.Max(location1.EndPoint.IndexOnLine, location2.EndPoint.IndexOnLine);
            }
            else if (location1.StartPoint.LineNumber < location2.StartPoint.LineNumber)
            {
                indexOnLine = location1.StartPoint.IndexOnLine;
                endIndexOnLine = location2.EndPoint.IndexOnLine;
            }
            else
            {
                indexOnLine = location2.StartPoint.IndexOnLine;
                endIndexOnLine = location1.EndPoint.IndexOnLine;
            }

            return new CodeLocation(
                Math.Min(location1.StartPoint.Index, location2.StartPoint.Index),
                Math.Max(location1.EndPoint.Index, location2.EndPoint.Index),
                indexOnLine,
                endIndexOnLine,
                Math.Min(location1.StartPoint.LineNumber, location2.StartPoint.LineNumber),
                Math.Max(location2.EndPoint.LineNumber, location2.EndPoint.LineNumber));
        }
        /// <summary>
        /// Joins the two given locations.
        /// </summary>
        /// <param name="location1">
        /// The first location to join.
        /// </param>
        /// <param name="location2">
        /// The second location to join.
        /// </param>
        /// <returns>
        /// Returns the joined <see cref="CodeLocation"/>.
        /// </returns>
        public static CodeLocation? Join(CodeLocation? location1, CodeLocation? location2)
        {
            Param.Ignore(location1, location2);

            if (location1 == null && location2 == null)
            {
                return null;
            }

            if (location1 == null)
            {
                return location2;
            }

            if (location2 == null)
            {
                return location1;
            }

            // Figure out which IndexOnLine and EndIndexOnLine to use.
            int indexOnLine;
            int endIndexOnLine;
            if (location1.Value.StartPoint.LineNumber == location2.Value.StartPoint.LineNumber)
            {
                indexOnLine = Math.Min(location1.Value.StartPoint.IndexOnLine, location2.Value.StartPoint.IndexOnLine);
                endIndexOnLine = Math.Max(location1.Value.EndPoint.IndexOnLine, location2.Value.EndPoint.IndexOnLine);
            }
            else if (location1.Value.StartPoint.LineNumber < location2.Value.StartPoint.LineNumber)
            {
                indexOnLine = location1.Value.StartPoint.IndexOnLine;
                endIndexOnLine = location2.Value.EndPoint.IndexOnLine;
            }
            else
            {
                indexOnLine = location2.Value.StartPoint.IndexOnLine;
                endIndexOnLine = location1.Value.EndPoint.IndexOnLine;
            }

            return new CodeLocation(
                Math.Min(location1.Value.StartPoint.Index, location2.Value.StartPoint.Index),
                Math.Max(location1.Value.EndPoint.Index, location2.Value.EndPoint.Index), 
                indexOnLine, 
                endIndexOnLine,
                Math.Min(location1.Value.StartPoint.LineNumber, location2.Value.StartPoint.LineNumber),
                Math.Max(location2.Value.EndPoint.LineNumber, location2.Value.EndPoint.LineNumber));
        }
        /// <summary>
        /// Adds one violation to the given code element.
        /// </summary>
        /// <param name="element">
        /// The element that the violation appears in.
        /// </param>
        /// <param name="location">
        /// The location in the code where the violation occurs.
        /// </param>
        /// <param name="ruleName">
        /// The name of the rule that triggered the violation.
        /// </param>
        /// <param name="values">
        /// String parameters to insert into the violation string.
        /// </param>
        public void AddViolation(ICodeElement element, CodeLocation location, Enum ruleName, params object[] values)
        {
            Param.Ignore(element);
            Param.RequireNotNull(location, "location");
            Param.RequireNotNull(ruleName, "ruleName");
            Param.Ignore(values);

            this.AddViolation(element, location, ruleName.ToString(), values);
        }
        /// <summary>
        /// Adds one violation to the given code element.
        /// </summary>
        /// <param name="element">
        /// The element that the violation appears in.
        /// </param>
        /// <param name="location">
        /// The location in the code where the violation occurs.
        /// </param>
        /// <param name="ruleName">
        /// The name of the rule that triggered the violation.
        /// </param>
        /// <param name="values">
        /// String parameters to insert into the violation string.
        /// </param>
        public void AddViolation(ICodeElement element, CodeLocation location, string ruleName, params object[] values)
        {
            Param.RequireNotNull(element, "element");
            Param.RequireNotNull(location, "location");
            Param.RequireValidString(ruleName, "ruleName");
            Param.Ignore(values);

            // If the rule is disabled or suppressed, skip it.
            if (this.IsRuleEnabled(element.Document, ruleName))
            {
                Rule rule = this.GetRule(ruleName);
                if (rule == null)
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.RuleDoesNotExist, ruleName), "ruleName");
                }

                if (!this.IsRuleSuppressed(element, rule.CheckId, ruleName, rule.Namespace))
                {
                    // Look up this violation type.
                    this.core.AddViolation(element, rule, location, values);
                }
            }
        }
        /// <summary>
        /// Imports the cached violations under the given node.
        /// </summary>
        /// <param name="sourceCode">
        /// The source code containing the violations.
        /// </param>
        /// <param name="parentNode">
        /// The parent xml node containing the list of violations.
        /// </param>
        /// <returns>
        /// Returns true if all the data was loaded successfully from the file.
        /// </returns>
        internal bool ImportViolations(SourceCode sourceCode, XmlNode parentNode)
        {
            Param.AssertNotNull(sourceCode, "sourceCode");
            Param.AssertNotNull(parentNode, "parentNode");

            bool success = true;

            try
            {
                XmlNodeList violations = parentNode.SelectNodes("violation");
                if (violations != null && violations.Count > 0)
                {
                    foreach (XmlNode violationNode in violations)
                    {
                        // Get the violation data from the xml node.
                        XmlNode nameSpace = violationNode.SelectSingleNode("@namespace");
                        XmlNode ruleName = violationNode.SelectSingleNode("@rule");
                        XmlNode ruleCheckId = violationNode.SelectSingleNode("@ruleCheckId");
                        XmlNode context = violationNode.SelectSingleNode("context");
                        XmlNode lineNumber = violationNode.SelectSingleNode("line");
                        XmlNode warning = violationNode.SelectSingleNode("warning");

                        XmlNode index = violationNode.SelectSingleNode("index");
                        XmlNode endIndex = violationNode.SelectSingleNode("endIndex");
                        XmlNode startLine = violationNode.SelectSingleNode("startLine");
                        XmlNode startColumn = violationNode.SelectSingleNode("startColumn");
                        XmlNode endLine = violationNode.SelectSingleNode("endLine");
                        XmlNode endColumn = violationNode.SelectSingleNode("endColumn");

                        // Create a Rule object representing this data.
                        Rule rule = new Rule(
                            ruleName.InnerText, 
                            nameSpace.InnerText, 
                            ruleCheckId.InnerText, 
                            context.InnerText, 
                            Convert.ToBoolean(warning.InnerText, CultureInfo.InvariantCulture));

                        Violation violation;

                        if (startLine != null && startColumn != null && endLine != null && endColumn != null)
                        {
                            CodeLocation location = new CodeLocation(
                                Convert.ToInt32(index.InnerText, null), 
                                Convert.ToInt32(endIndex.InnerText, null), 
                                Convert.ToInt32(startColumn.InnerText, null), 
                                Convert.ToInt32(endColumn.InnerText, null), 
                                Convert.ToInt32(startLine.InnerText, null), 
                                Convert.ToInt32(endLine.InnerText, null));

                            // Create a Violation object representing this data.
                            violation = new Violation(rule, sourceCode, location, context.InnerText);
                        }
                        else
                        {
                            // Create a Violation object representing this data.
                            violation = new Violation(rule, sourceCode, Convert.ToInt32(lineNumber.InnerText, null), context.InnerText);
                        }

                        this.AddViolation(violation);
                    }
                }
            }
            catch (ArgumentException)
            {
                success = false;
            }
            catch (XmlException)
            {
                success = false;
            }
            catch (FormatException)
            {
                success = false;
            }
            catch (OverflowException)
            {
                success = false;
            }

            return success;
        }
Beispiel #9
0
        /// <summary>
        /// Imports the cached violations under the given node.
        /// </summary>
        /// <param name="sourceCode">
        /// The source code containing the violations.
        /// </param>
        /// <param name="parentNode">
        /// The parent xml node containing the list of violations.
        /// </param>
        /// <returns>
        /// Returns true if all the data was loaded successfully from the file.
        /// </returns>
        internal bool ImportViolations(SourceCode sourceCode, XmlNode parentNode)
        {
            Param.AssertNotNull(sourceCode, "sourceCode");
            Param.AssertNotNull(parentNode, "parentNode");

            bool success = true;

            try
            {
                XmlNodeList violations = parentNode.SelectNodes("violation");
                if (violations != null && violations.Count > 0)
                {
                    foreach (XmlNode violationNode in violations)
                    {
                        // Get the violation data from the xml node.
                        XmlNode nameSpace   = violationNode.SelectSingleNode("@namespace");
                        XmlNode ruleName    = violationNode.SelectSingleNode("@rule");
                        XmlNode ruleCheckId = violationNode.SelectSingleNode("@ruleCheckId");
                        XmlNode context     = violationNode.SelectSingleNode("context");
                        XmlNode lineNumber  = violationNode.SelectSingleNode("line");
                        XmlNode warning     = violationNode.SelectSingleNode("warning");

                        XmlNode index       = violationNode.SelectSingleNode("index");
                        XmlNode endIndex    = violationNode.SelectSingleNode("endIndex");
                        XmlNode startLine   = violationNode.SelectSingleNode("startLine");
                        XmlNode startColumn = violationNode.SelectSingleNode("startColumn");
                        XmlNode endLine     = violationNode.SelectSingleNode("endLine");
                        XmlNode endColumn   = violationNode.SelectSingleNode("endColumn");

                        // Create a Rule object representing this data.
                        Rule rule = new Rule(
                            ruleName.InnerText,
                            nameSpace.InnerText,
                            ruleCheckId.InnerText,
                            context.InnerText,
                            Convert.ToBoolean(warning.InnerText, CultureInfo.InvariantCulture));

                        Violation violation;

                        if (startLine != null && startColumn != null && endLine != null && endColumn != null)
                        {
                            CodeLocation location = new CodeLocation(
                                Convert.ToInt32(index.InnerText, null),
                                Convert.ToInt32(endIndex.InnerText, null),
                                Convert.ToInt32(startColumn.InnerText, null),
                                Convert.ToInt32(endColumn.InnerText, null),
                                Convert.ToInt32(startLine.InnerText, null),
                                Convert.ToInt32(endLine.InnerText, null));

                            // Create a Violation object representing this data.
                            violation = new Violation(rule, sourceCode, location, context.InnerText);
                        }
                        else
                        {
                            // Create a Violation object representing this data.
                            violation = new Violation(rule, sourceCode, Convert.ToInt32(lineNumber.InnerText, null), context.InnerText);
                        }

                        this.AddViolation(violation);
                    }
                }
            }
            catch (ArgumentException)
            {
                success = false;
            }
            catch (XmlException)
            {
                success = false;
            }
            catch (FormatException)
            {
                success = false;
            }
            catch (OverflowException)
            {
                success = false;
            }

            return(success);
        }