/// <summary>
        /// Removes violation got from the custom analyzer.
        /// </summary>
        public static void RemoveCustomViolation(ViolationEventArgs e)
        {
            string key = (string)typeof(Violation).InvokeMember(
                "Key",
                BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty,
                null,
                e.Violation,
                null);

            Dictionary<string, Violation> violations = (Dictionary<string, Violation>)typeof(CodeElement).InvokeMember(
                "violations",
                BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField,
                null,
                e.Element,
                null);

            violations.Remove(key);
        }
Example #2
0
 /// <summary>
 /// Handles encountered violations.
 /// </summary>
 private static void OnViolationEncountered(object sender, ViolationEventArgs e)
 {
     Console.WriteLine("{0} ({1}): {2}", e.Violation.Rule.CheckId, e.LineNumber, e.Message);
 }
 /// <summary>
 /// Handles encountered violations.
 /// </summary>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="ViolationEventArgs"/> instance containing the event data.</param>
 private static void OnViolationEncountered(
     object sender,
     ViolationEventArgs e)
 {
     string elementName = (null != e.Element) ? e.Element.FullyQualifiedName : string.Empty;
     Console.WriteLine(
         "{0},{1},{2},{3},{4},{5}",
         e.Violation.Rule.CheckId,
         e.Message.Replace(',', ' '),
         e.SourceCode.Path.Replace(',', ' '),
         e.Violation.Rule.Namespace.Replace(',', ' '),
         elementName.Replace(',', ' '),
         e.LineNumber);
 }
        /// <summary>
        /// Reports violations encountered by stylecop to the error pad
        /// </summary>
        /// <param name="sender">
        /// A <see cref="System.Object"/> determining which object sends this violation
        /// </param>
        /// <param name="args">
        /// A <see cref="ViolationEventArgs"/> determining elements composing the violation
        /// </param>
        private void OnViolationEncountered(object sender, ViolationEventArgs args)
        {
            try
            {
            MonoDevelop.Projects.BuildError b = new MonoDevelop.Projects.BuildError(args.Element.Document.SourceCode.Path, args.LineNumber, 0, args.Violation.Rule.CheckId, args.Message);
            b.IsWarning = args.Warning;
            Task errorTask = new Task(b, RunStyleCopHandler.styleCopAddinOwner);

            this.errorPad.AddTask(errorTask);
            }
            catch (Exception e)
            {
                this.logger.WriteLine("Error: {0}\nStackTrace:{1}", e.Message, e.StackTrace);
            }
        }
        /// <summary>
        /// Called when a violation is encountered during an analysis.
        /// </summary>
        /// <param name="e">The event arguments.</param>
        protected void OnViolationEncountered(ViolationEventArgs e)
        {
            Param.RequireNotNull(e, "e");

            if (this.ViolationEncountered != null)
            {
                this.ViolationEncountered(this, e);
            }
        }
        /// <summary>
        /// Removes violation got from the custom analyzer.
        /// </summary>
        public static void RemoveCustomViolation(ViolationEventArgs e)
        {
            Dictionary<string, Violation> violations = (Dictionary<string, Violation>)typeof(CsElement).InvokeMember(
                "violations",
                BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField,
                null,
                e.Element,
                null);

            violations.Remove(e.Violation.Key);
        }
 /// <summary>
 /// Reads the value of custom setting.
 /// </summary>
 private bool ReadSetting(ViolationEventArgs e, string settingName)
 {
     return SettingsManager.GetValue<bool>(
         m_parent,
         e.Element.Document.Settings,
         settingName);
 }
        /// <summary>
        /// Callback method for when a violation is encountered.
        /// </summary>
        /// <param name="sender">
        /// The object that caused this event.
        /// </param>
        /// <param name="e">
        /// The ViolationEventArgs object that describes this violation.
        /// </param>
        private void ViolationEncountered(
            object sender,
            ViolationEventArgs e)
        {
            // Add the violation.
            var sfiles =
                this.Report.SourceCodeFiles.Where(
                    r => r.Path == e.SourceCode.Path);

            foreach (var sf in sfiles)
            {
                var lines = File.ReadAllLines(sf.Path);

                lock (this.Report)
                {
                    var violation = this.Report.Violations.AddViolationsRow(
                        e.LineNumber,
                        e.Message,
                        sf,
                        lines[e.LineNumber - 1]);

                    // Add the rule.
                    this.Report.Rules.AddRulesRow(
                        e.Violation.Rule.CheckId,
                        e.Violation.Rule.Description,
                        e.Violation.Rule.EnabledByDefault,
                        e.Violation.Rule.Name,
                        e.Violation.Rule.Namespace,
                        e.Violation.Rule.RuleGroup,
                        e.Violation.Rule.Warning,
                        violation);
                }
            }
        }
        /// <summary>
        /// Handles SA1643 violation.
        /// </summary>
        private void Handle1643(ViolationEventArgs e)
        {
            CsElement element = (CsElement)e.Element;

            string text = CodeHelper.GetSummaryText(element);
            if (text == Resources.StandardDestructorSummaryText)
                return;

            m_parent.AddViolation(
                element,
                Rules.DestructorSummaryDocumentationMustBeginWithStandardText,
                new object[] { StyleCop43Compatibility.GetExampleSummaryTextForDestructor(m_customDocumentationAnalyzer) });
        }
        /// <summary>
        /// Handles encountered custom violations.
        /// </summary>
        private void OnCustomViolationEncountered(object sender, ViolationEventArgs e)
        {
            StyleCop43Compatibility.RemoveCustomViolation(e);

            switch (e.Violation.Rule.CheckId)
            {
                case "SA1502":
                    Handle1502(e);
                    break;

                case "SA1509":
                    Handle1509(e);
                    break;

                case "SA1513":
                    Handle1513(e);
                    break;

                case "SA1516":
                    Handle1516(e);
                    break;

                case "SA1642":
                    Handle1642(e);
                    break;

                case "SA1643":
                    Handle1643(e);
                    break;
            }
        }
        /// <summary>
        /// Handles SA1516 violation.
        /// </summary>
        private void Handle1516(ViolationEventArgs e)
        {
            CsElement element = (CsElement)e.Element;

            if (ReadSetting(e, AllowJoinedAccessorsFor1516))
            {
                if (element.ElementType == ElementType.Accessor)
                    return;

                if (StyleCop43Compatibility.IsStyleCop43())
                {
                    Node<CsToken> node = CodeHelper.GetNodeByLine((CsDocument)element.Document, e.LineNumber);
                    if (node != null)
                    {
                        Node<CsToken> next1 = CodeHelper.FindNextValueableNode(node);
                        if (next1.Value.CsTokenType == CsTokenType.CloseCurlyBracket)
                        {
                            Node<CsToken> next2 = CodeHelper.FindNextValueableNode(next1);
                            if (next2.Value.CsTokenType == CsTokenType.Get
                                || next2.Value.CsTokenType == CsTokenType.Set)
                                return;
                        }
                    }
                }
            }

            m_parent.AddViolation(
                element,
                Rules.ElementsMustBeSeparatedByBlankLine);
        }
        /// <summary>
        /// Handles SA1509 violation.
        /// </summary>
        private void Handle1509(ViolationEventArgs e)
        {
            CsElement element = (CsElement)e.Element;

            if (ReadSetting(e, AllowNestedCodeBlocksFor1509))
            {
                Node<CsToken> node = CodeHelper.GetNodeByLine((CsDocument)element.Document, e.LineNumber);
                if (node != null)
                {
                    Node<CsToken> prev = CodeHelper.FindPreviousValueableNode(node);
                    if (prev.Value.CsTokenType == CsTokenType.Semicolon
                        || prev.Value.CsTokenType == CsTokenType.CloseCurlyBracket)
                        return;
                }
            }

            m_parent.AddViolation(
                element,
                e.LineNumber,
                Rules.OpeningCurlyBracketsMustNotBePrecededByBlankLine,
                new object[0]);
        }
        /// <summary>
        /// Handles SA1502 violation.
        /// </summary>
        private void Handle1502(ViolationEventArgs e)
        {
            CsElement element = (CsElement)e.Element;

            if (ReadSetting(e, AllowConstructorsFor1502))
            {
                if (element.ElementType == ElementType.Constructor)
                    return;
            }

            m_parent.AddViolation(
                element,
                e.LineNumber,
                Rules.OpeningCurlyBracketsMustNotBePrecededByBlankLine,
                element.FriendlyTypeText);
        }
        /// <summary>
        /// Called when a violation is found.
        /// </summary> 
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event arguments.</param>
        private void CoreViolationEncountered(object sender, ViolationEventArgs e)
        {
            Param.Ignore(sender, e);

            lock (this)
            {
                // Create the violation element.
                XmlElement violation = this.violations.CreateElement("Violation");
                XmlAttribute attrib = null;

                // Add the element section if it's not empty.
                if (e.Element != null)
                {
                    attrib = this.violations.CreateAttribute("Section");
                    attrib.Value = CreateSafeSectionName(e.Element.FullyQualifiedName);
                    violation.Attributes.Append(attrib);
                }

                // Add the line number.
                attrib = this.violations.CreateAttribute("LineNumber");
                attrib.Value = e.LineNumber.ToString(CultureInfo.InvariantCulture);
                violation.Attributes.Append(attrib);

                // Get the source code that this element is in.
                SourceCode sourceCode = e.SourceCode;
                if (sourceCode == null && e.Element != null && e.Element.Document != null)
                {
                    sourceCode = e.Element.Document.SourceCode;
                }

                // Add the source code path.
                if (sourceCode != null)
                {
                    attrib = this.violations.CreateAttribute("Source");
                    attrib.Value = sourceCode.Path;
                    violation.Attributes.Append(attrib);
                }

                // Add the rule namespace.
                attrib = this.violations.CreateAttribute("RuleNamespace");
                attrib.Value = e.Violation.Rule.Namespace;
                violation.Attributes.Append(attrib);

                // Add the rule name.
                attrib = this.violations.CreateAttribute("Rule");
                attrib.Value = e.Violation.Rule.Name;
                violation.Attributes.Append(attrib);

                // Add the rule ID.
                attrib = this.violations.CreateAttribute("RuleId");
                attrib.Value = e.Violation.Rule.CheckId;
                violation.Attributes.Append(attrib);

                violation.InnerText = e.Message;

                this.violations.DocumentElement.AppendChild(violation);
                this.violationCount++;
            }

            // Forward event
            this.OnViolationEncountered(new ViolationEventArgs(e.Violation));
        }
 private void CoreViolationEncountered(object sender, ViolationEventArgs e)
 {
     lock (this)
     {
         XmlElement newChild = this.violations.CreateElement("Violation");
         XmlAttribute node = null;
         if (e.Element != null)
         {
             node = this.violations.CreateAttribute("Section");
             node.Value = CreateSafeSectionName(e.Element.FullyQualifiedName);
             newChild.Attributes.Append(node);
         }
         node = this.violations.CreateAttribute("LineNumber");
         node.Value = e.LineNumber.ToString(CultureInfo.InvariantCulture);
         newChild.Attributes.Append(node);
         SourceCode sourceCode = e.SourceCode;
         if (((sourceCode == null) && (e.Element != null)) && (e.Element.Document != null))
         {
             sourceCode = e.Element.Document.SourceCode;
         }
         if (sourceCode != null)
         {
             node = this.violations.CreateAttribute("Source");
             node.Value = sourceCode.Path;
             newChild.Attributes.Append(node);
         }
         node = this.violations.CreateAttribute("RuleNamespace");
         node.Value = e.Violation.Rule.Namespace;
         newChild.Attributes.Append(node);
         node = this.violations.CreateAttribute("Rule");
         node.Value = e.Violation.Rule.Name;
         newChild.Attributes.Append(node);
         node = this.violations.CreateAttribute("RuleId");
         node.Value = e.Violation.Rule.CheckId;
         newChild.Attributes.Append(node);
         newChild.InnerText = e.Message;
         this.violations.DocumentElement.AppendChild(newChild);
         this.violationCount++;
     }
     this.OnViolationEncountered(new ViolationEventArgs(e.Violation));
 }
        /// <summary>
        /// Called when a violation is encountered during an analysis.
        /// </summary>
        /// <param name="e">The event arguments.</param>
        private void OnViolationEncountered(ViolationEventArgs e)
        {
            Param.AssertNotNull(e, "e");

            if (this.ViolationEncountered != null)
            {
                this.ViolationEncountered(this, e);
            }
        }
 private void OnViolationEncountered(ViolationEventArgs e)
 {
     if (this.ViolationEncountered != null)
     {
         this.ViolationEncountered(this, e);
     }
 }
Example #18
0
        /// <summary>
        /// Called when StyleCop encounters a violation.
        /// </summary>
        /// <param name="sender">The eventt sender.</param>
        /// <param name="e">The event arguments.</param>
        private void OnViolationEncountered(object sender, ViolationEventArgs e)
        {
            Param.Ignore(sender);
            Param.AssertNotNull(e, "e");

            if (this.violationLimit < 0 || this.violationCount < this.violationLimit)
            {
                this.violationCount++;

                // Does the violation qualify for breaking the build?
                if (!(e.Warning || this.inputTreatErrorsAsWarnings))
                {
                    this.succeeded = false;
                }

                string path = string.Empty;
                if (e.SourceCode != null && e.SourceCode.Path != null && e.SourceCode.Path.Length > 0)
                {
                    path = e.SourceCode.Path;
                }
                else if (e.Element != null &&
                    e.Element.Document != null &&
                    e.Element.Document.SourceCode != null &&
                    e.Element.Document.SourceCode.Path != null)
                {
                    path = e.Element.Document.SourceCode.Path;
                }

                // Prepend the rule check-id to the message.
                string message = string.Format(CultureInfo.CurrentCulture, Strings.ViolationFormatter, e.Violation.Rule.CheckId, e.Message);

                lock (this)
                {
                    if (e.Warning || this.inputTreatErrorsAsWarnings)
                    {
                        Log.LogWarning(MSBuildSubCategory, MSBuildErrorCode, null, path, e.LineNumber, 1, 0, 0, message);
                    }
                    else
                    {
                        Log.LogError(MSBuildSubCategory, MSBuildErrorCode, null, path, e.LineNumber, 1, 0, 0, message);
                    }
                }
            }
        }
 /// <summary>
 /// Handles encountered violations.
 /// </summary>
 private void OnViolationEncountered(object sender, ViolationEventArgs e)
 {
     Violations.Add(e.Violation.Rule.Name);
 }