private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e) { if (e.ItemIndex < 0) { return; } StyleCop.ViolationEventArgs ee = Ws[e.ItemIndex] as StyleCop.ViolationEventArgs; ListViewItem v = new ListViewItem(); v.Text = ""; v.SubItems.Add(""); v.SubItems.Add(""); v.SubItems.Add(ee.Message); v.SubItems.Add(Path.GetFileName(ee.SourceCode.Project.ToString())); v.SubItems.Add(ee.SourceCode.Path); v.SubItems.Add(ee.LineNumber.ToString()); v.SubItems.Add("project"); v.Checked = true; v.Tag = ee; e.Item = v; }
private void OnViolationEncountered(object sender, ViolationEventArgs e) { if (e.SourceCode != null) { var violation = new Violation(e.Violation.Rule.CheckId, e.Message, e.LineNumber); this.violations.AddViolationToFile(e.SourceCode.Path, violation); } }
/// <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); } }
private void OnViolationEncountered(object sender, ViolationEventArgs e) { List<Violation> list; if (!this.violations.TryGetValue(e.Violation.Rule.CheckId, out list)) { list = new List<Violation>(); this.violations.Add(e.Violation.Rule.CheckId, list); } list.Add(e.Violation); }
/// <summary> /// Called when a violation is encountered during an analysis. This can be called simultaneously from several threads and so any code must be thread safe. /// </summary> /// <param name="e"> /// The event arguments. /// </param> protected void OnViolationEncountered(ViolationEventArgs e) { Param.RequireNotNull(e, "e"); // Make sure we cache the delegate locally to avoid other threads unsubscribing before we call them. // See http://piers7.blogspot.com/2010/03/3-races-with-net-events.html for info. EventHandler <ViolationEventArgs> handlers = this.ViolationEncountered; if (handlers != null) { handlers(this, e); } }
public Violation(ViolationEventArgs eventArgs) { var csElement = (CsElement)eventArgs.Element; var fullName = csElement.FullNamespaceName; ClassName = GetClassName(csElement); FullName = fullName; Declaration = GetDeclaration(csElement); RuleId = eventArgs.Violation.Rule.CheckId; Kind = csElement.GetType().Name; RuleNamespace = eventArgs.Violation.Rule.Namespace; Rule = eventArgs.Violation.Rule.Name; LineNumber = eventArgs.Violation.Line.ToString(); RuleGroup = eventArgs.Violation.Rule.RuleGroup; Message = eventArgs.Violation.Rule.Context; Namespace = GetNameSpace(csElement); }
/// <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 SA1516 violation. /// </summary> private void Handle1516(ViolationEventArgs e) { CsElement element = (CsElement)e.Element; if (ReadSetting(e, AllowJoinedAccessorsFor1516)) { if (element.ElementType == ElementType.Accessor) return; } m_parent.AddViolation( element, Rules.ElementsMustBeSeparatedByBlankLine); }
/// <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[] { GetExampleSummaryTextForDestructor(m_customDocumentationAnalyzer) }); }
/// <summary> /// Fired when violation encountered in source code files being analyzed. /// </summary> /// <param name="sender"> /// Object representing source of event. /// </param> /// <param name="e"> /// ViolationEventArgs representing violation event data. /// </param> static void OnViolationEncountered(object sender, ViolationEventArgs e) { // Note: To be used for generating custom violation reports. s_numberViolations++; }
/// <summary> /// Handles SA1513 violation. /// </summary> private void Handle1513(ViolationEventArgs e) { CsElement element = (CsElement)e.Element; if (ReadSetting(e, AllowJoinedAccessorsFor1513)) { if (element.ElementType == ElementType.Accessor) return; } m_parent.AddViolation( element, e.LineNumber, Rules.ClosingCurlyBracketMustBeFollowedByBlankLine); }
/// <summary> /// Get rule information for output /// </summary> /// <param name="element">Element to add to</param> /// <param name="e">Violation information</param> private static void GetRuleInfo(XElement element, ViolationEventArgs e) { element.Add(new XAttribute("RuleNamespace", e.Violation.Rule.Namespace)); element.Add(new XAttribute("Rule", e.Violation.Rule.Name)); element.Add(new XAttribute("RuleId", e.Violation.Rule.CheckId)); }
/// <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)); }
/// <summary> /// Called when StyleCop encounters a violation. /// </summary> /// <param name="sender"> /// The event 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; } lock (this) { string trimmedNamespace = e.Violation.Rule.Namespace.SubstringAfter(StyleCop.Constants.ProductName + ".", StringComparison.Ordinal); trimmedNamespace = trimmedNamespace.SubstringBeforeLast("Rules", StringComparison.Ordinal); string description = string.Concat(e.Violation.Rule.CheckId, " : ", trimmedNamespace, " : ", e.Message); if (e.Warning || this.inputTreatErrorsAsWarnings) { if (e.Location == null) { this.Log.LogWarning(MSBuildSubCategory, MSBuildErrorCode, null, path, e.LineNumber, 1, 0, 0, description); } else { this.Log.LogWarning( MSBuildSubCategory, MSBuildErrorCode, null, path, e.Location.Value.StartPoint.LineNumber, e.Location.Value.StartPoint.IndexOnLine, e.Location.Value.EndPoint.LineNumber, e.Location.Value.EndPoint.IndexOnLine, description); } } else { if (e.Location == null) { this.Log.LogError(MSBuildSubCategory, MSBuildErrorCode, null, path, e.LineNumber, 1, 0, 0, description); } else { this.Log.LogError( MSBuildSubCategory, MSBuildErrorCode, null, path, e.Location.Value.StartPoint.LineNumber, e.Location.Value.StartPoint.IndexOnLine, e.Location.Value.EndPoint.LineNumber, e.Location.Value.EndPoint.IndexOnLine, description); } } } } }
/// <summary> /// Handles encountered violations. /// </summary> private static void OnViolationEncountered( object sender, ViolationEventArgs e) { Console.WriteLine ( "{0}: {1}", e.Violation.Rule.CheckId, e.Message); }
/// <summary> /// Handler dell'evento violazione. /// </summary> /// <param name="sender"> /// Chi invoca l'evento. /// </param> /// <param name="e"> /// Parametri dell'evento. /// </param> private static void OnViolationEncountered(object sender, ViolationEventArgs e) { Violations[filePath].Add(new Violation() { LineNumber = e.LineNumber - 1, ErrorMessage = e.Violation.Rule.CheckId + ": " + e.Message }); sfWorkingLines[e.LineNumber - 1].AddViolation(new SAObject(e.Violation.Rule.CheckId, e.LineNumber, e.Message, false)); violationContent.AppendFormat( "{0}:{1}:{2} - {3}{4}", e.Violation.Rule.CheckId, e.Violation.SourceCode.Name, e.LineNumber, e.Message, System.Environment.NewLine); }
protected static void OnViolationEncountered(object sender, ViolationEventArgs e) { Console.WriteLine("violation: " + e.Violation.Message); }
private static void FixStyleCopViolation(List<Tuple<int, string>> sourceCode, ViolationEventArgs e) { switch (e.Violation.Rule.CheckId) { //ElementsMustBeSeparatedByBlankLine case "SA1516": AddEmptyLine(sourceCode, e.LineNumber); break; //CodeMustNotContainMultipleBlankLinesInARow case "SA1507": RemoveEmptyLine(sourceCode, e.LineNumber); break; //ClosingCurlyBracketsMustNotBePrecededByBlankLine case "SA1508": RemoveEmptyLine(sourceCode, e.LineNumber - 1); break; //CodeMustNotContainBlankLinesAtEndOfFile case "SA1518": RemoveEmptyLine(sourceCode, e.LineNumber); break; //OpeningCurlyBracketsMustNotBeFollowedByBlankLine case "SA1505": RemoveEmptyLine(sourceCode, e.LineNumber+1); break; //ClosingCurlyBracketMustBeFollowedByBlankLine case "SA1513": AddEmptyLine(sourceCode, e.LineNumber + 1); break; //SingleLineCommentsMustBePrecededByBlankLine case "SA1515": AddEmptyLine(sourceCode, e.LineNumber); break; //ElementDocumentationHeadersMustBePrecededByBlankLine case "SA1514": AddEmptyLine(sourceCode, e.LineNumber); break; //SingleLineCommentsMustNotBeFollowedByBlankLine case "SA1512": RemoveEmptyLine(sourceCode, e.LineNumber + 1); break; //CodeMustNotContainBlankLinesAtStartOfFile case "SA1517": RemoveEmptyLine(sourceCode, e.LineNumber); break; default: throw new NotImplementedException(); } }
/// <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> /// Handles encountered custom violations. /// </summary> private void OnCustomViolationEncountered(object sender, ViolationEventArgs e) { RemoveCustomViolation(e); switch (e.Violation.Rule.CheckId) { case "SA1502": Handle1502(e); break; case "SA1513": Handle1513(e); break; case "SA1516": Handle1516(e); break; case "SA1642": Handle1642(e); break; case "SA1643": Handle1643(e); break; } }
/// <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); if (e.Location != null) { // Add the detailed location if avail. attrib = this.violations.CreateAttribute("StartLine"); attrib.Value = e.Location.Value.StartPoint.LineNumber.ToString(CultureInfo.InvariantCulture); violation.Attributes.Append(attrib); attrib = this.violations.CreateAttribute("StartColumn"); attrib.Value = e.Location.Value.StartPoint.IndexOnLine.ToString(CultureInfo.InvariantCulture); violation.Attributes.Append(attrib); attrib = this.violations.CreateAttribute("EndLine"); attrib.Value = e.Location.Value.EndPoint.LineNumber.ToString(CultureInfo.InvariantCulture); violation.Attributes.Append(attrib); attrib = this.violations.CreateAttribute("EndColumn"); attrib.Value = e.Location.Value.EndPoint.IndexOnLine.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)); }
/// <summary> /// Get location information for output /// </summary> /// <param name="element">Element to add to</param> /// <param name="e">Violation information</param> private static void GetLocationInfo(XElement element, ViolationEventArgs e) { element.Add(new XAttribute("StartLine", e.Location.Value.StartPoint.LineNumber)); element.Add(new XAttribute("StartColumn", e.Location.Value.StartPoint.IndexOnLine)); element.Add(new XAttribute("EndLine", e.Location.Value.EndPoint.LineNumber)); element.Add(new XAttribute("EndColumn", e.Location.Value.EndPoint.IndexOnLine)); }
/// <summary> /// Called when a violation is encountered while analyzing a code document. This can be called simultaneously from several threads and so any code must be thread safe. /// </summary> /// <param name="e">The event arguments.</param> private void OnViolationEncountered(ViolationEventArgs e) { Param.AssertNotNull(e, "e"); // Make sure we cache the delegate locally to avoid other threads unsubscribing before we call them. // See http://piers7.blogspot.com/2010/03/3-races-with-net-events.html for info. EventHandler<ViolationEventArgs> handlers = this.ViolationEncountered; if (handlers != null) { handlers(this, e); } }
/// <summary> /// Violation encountered /// </summary> /// <param name="sender">Sending object</param> /// <param name="e">Violation data</param> private void Violation(object sender, ViolationEventArgs e) { Param.Ignore(sender, e); lock (this) { if (this.write != null) { if (!this.violationReported) { this.write.WriteLine("<StyleCopViolations>"); this.OnOutputGenerated(new OutputEventArgs("Violations were encountered.")); this.violationReported = true; } var violation = new XElement("Violation"); if (e.Element != null) { var name = e.Element.FullyQualifiedName; if (name != null && this.method != null) { var safe = this.method.Invoke(null, new object[] { name }); name = safe == null ? name : safe.ToString(); } violation.Add(new XAttribute("Section", name)); } violation.Add(new XAttribute("LineNumber", e.LineNumber)); if (e.Location != null) { GetLocationInfo(violation, e); } SourceCode sourceCode = e.SourceCode; if (sourceCode == null && e.Element != null && e.Element.Document != null) { sourceCode = e.Element.Document.SourceCode; } if (sourceCode != null) { violation.Add(new XAttribute("Source", sourceCode.Path)); } GetRuleInfo(violation, e); violation.SetValue(e.Message); this.write.WriteLine(violation.ToString()); } } }
/// <summary> /// Handle Violation reporting from StyleCop /// </summary> /// <param name="sender">Event source</param> /// <param name="e">Arguments supplied</param> private void ViolationEncountered(object sender, ViolationEventArgs e) { mViolations.Add(e.Violation); }
/// <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); } } }
private void OnViolationEncountered(object sender, ViolationEventArgs e) { this.violations.Enqueue(e.Violation); }
/// <summary> /// Called when a violation is encoutered while running under fuzz test mode. /// </summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event arguments.</param> private static void OnFuzzViolationEncountered(object sender, ViolationEventArgs e) { string checkId = e.Violation.Rule.CheckId; if (checkId == "SA0001" || checkId == "SA0101") { issues++; string id = String.Format("{0:0000}", issues); string path = e.Violation.SourceCode.Path; Console.WriteLine("{0} {1}: {2} {3}", id, checkId, path, e.Violation.Message); string saveAs = String.Format("ViolationFiles\\{0}.cs", id); SaveViolationFile(path, saveAs, e.Violation.Line); } }
/// <summary> /// Removes violation got from the custom analyzer. /// </summary> public static void RemoveCustomViolation(ViolationEventArgs e) { Dictionary<int, Violation> violations = (Dictionary<int, Violation>)typeof(CsElement).InvokeMember( "violations", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField, null, e.Element, null); violations.Remove(e.Violation.Key); }
void OnViolationEncountered(object sender, ViolationEventArgs e) { violationCount++; var path = e.SourceCode.Path.Replace(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar, ""); Console.WriteLine("{0}:L{1}: [{2}] {3}", path, e.LineNumber, e.Violation.Rule.CheckId, e.Message); }
/// <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.ElementMustNotBeOnSingleLine, element.FriendlyTypeText); }
/// <summary> /// Fired when violation encountered in source code files being analyzed. /// </summary> /// <param name="sender"> /// Object representing source of event. /// </param> /// <param name="e"> /// ViolationEventArgs representing violation event data. /// </param> static void OnViolationEncountered(object sender, ViolationEventArgs e) { // Note: To be used for generating custom violation reports. CustomReport.AddViolation(new Violation(e)); }
/// <summary> /// Called when a violation is encountered while analyzing a code document. /// </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); } }