public override void AnalyzeDocument(CodeDocument document) { Param.RequireNotNull(document, "document"); CsDocument document2 = (CsDocument) document; if ((document2.RootElement != null) && !document2.RootElement.Generated) { TopLevelElements context = new TopLevelElements(); document2.WalkDocument<TopLevelElements>(new CodeWalkerElementVisitor<TopLevelElements>(this.ProcessElement), new CodeWalkerStatementVisitor<TopLevelElements>(this.ProcessStatement), new CodeWalkerExpressionVisitor<TopLevelElements>(this.ProcessExpression), context); if (context.Classes.Count > 1) { string fullNamespaceName = string.Empty; foreach (Class class2 in context.Classes) { if (!class2.Declaration.ContainsModifier(new CsTokenType[] { CsTokenType.Partial }) || (!string.IsNullOrEmpty(fullNamespaceName) && (string.Compare(fullNamespaceName, class2.FullNamespaceName, StringComparison.Ordinal) != 0))) { base.AddViolation(class2, 1, Microsoft.StyleCop.CSharp.Rules.FileMayOnlyContainASingleClass, new object[0]); break; } fullNamespaceName = class2.FullNamespaceName; } } if (context.Namespaces.Count > 1) { base.AddViolation(document2.RootElement, Microsoft.StyleCop.CSharp.Rules.FileMayOnlyContainASingleNamespace, new object[0]); } } }
/// <summary> /// Visits one code unit in the document. /// </summary> /// <param name="codeUnit">The item being visited.</param> /// <param name="parentElement">The parent element, if any.</param> /// <param name="parentStatement">The parent statement, if any.</param> /// <param name="parentExpression">The parent expression, if any.</param> /// <param name="parentClause">The parent query clause, if any.</param> /// <param name="parentToken">The parent token, if any.</param> /// <param name="topLevelElements">The number of classes and namespaces seen in the document.</param> /// <returns>Returns true to continue, or false to stop the walker.</returns> private bool VisitCodeUnit( CodeUnit codeUnit, Element parentElement, Statement parentStatement, Expression parentExpression, QueryClause parentClause, Token parentToken, TopLevelElements topLevelElements) { Param.AssertNotNull(codeUnit, "codeUnit"); Param.Ignore(parentElement, parentStatement, parentExpression, parentClause, parentToken); Param.AssertNotNull(topLevelElements, "topLevelElements"); if (codeUnit.CodeUnitType == CodeUnitType.Element) { return(this.VisitElement((Element)codeUnit, parentElement, topLevelElements)); } else if (codeUnit.CodeUnitType == CodeUnitType.Statement) { return(this.VisitStatement((Statement)codeUnit, parentExpression, parentStatement, parentElement)); } else if (codeUnit.CodeUnitType == CodeUnitType.Expression) { return(this.VisitExpression((Expression)codeUnit, parentExpression, parentStatement, parentElement)); } return(!this.Cancel); }
public TSLScript(IEnumerable <ITSLTopLevelElement> topLevelElements) { TopLevelElements = topLevelElements.ToImmutableArray(); Cells = TopLevelElements.OfType <TSLCell>().ToImmutableArray(); Structs = TopLevelElements.Where(e => e.GetType() == typeof(TSLStruct)).Cast <TSLStruct>().ToImmutableArray(); Enums = TopLevelElements.OfType <TSLEnum>().ToImmutableArray(); Protocols = TopLevelElements.OfType <TSLProtocol>().ToImmutableArray(); Servers = TopLevelElements.OfType <TSLServer>().ToImmutableArray(); Proxies = TopLevelElements.OfType <TSLProxy>().ToImmutableArray(); Modules = TopLevelElements.OfType <TSLModule>().ToImmutableArray(); }
/// <summary> /// Called when an expression is visited. /// </summary> /// <param name="expression">The expression being visited.</param> /// <param name="parentExpression">The parent expression, if any.</param> /// <param name="parentStatement">The parent statement, if any.</param> /// <param name="parentElement">The parent element, if any.</param> /// <param name="context">The optional visitor context data.</param> /// <returns>Returns true to continue, or false to stop the walker.</returns> private bool ProcessExpression( Expression expression, Expression parentExpression, Statement parentStatement, CsElement parentElement, TopLevelElements context) { Param.AssertNotNull(expression, "expression"); Param.Ignore(parentExpression); Param.Ignore(parentStatement); Param.AssertNotNull(parentElement, "parentElement"); Param.Ignore(context); if (!parentElement.Generated) { // Determine whether this expression is a method invocation which contains call to Debug.Fail or Debug.Assert. if (expression.ExpressionType == ExpressionType.MethodInvocation) { MethodInvocationExpression methodInvocation = (MethodInvocationExpression)expression; if (methodInvocation.Name.Tokens.MatchTokens("Debug", ".", "Assert") || methodInvocation.Name.Tokens.MatchTokens("System", ".", "Diagnostics", ".", "Debug", ".", "Assert")) { this.CheckDebugAssertMessage(parentElement, methodInvocation); } else if (methodInvocation.Name.Tokens.MatchTokens("Debug", ".", "Fail") || methodInvocation.Name.Tokens.MatchTokens("System", ".", "Diagnostics", ".", "Debug", ".", "Fail")) { this.CheckDebugFailMessage(parentElement, methodInvocation); } } else if (expression.ExpressionType == ExpressionType.Parenthesized) { this.CheckParenthesizedExpression(parentElement, (ParenthesizedExpression)expression); } else if (expression.ExpressionType == ExpressionType.Arithmetic) { this.CheckArithmeticExpressionParenthesis(parentElement, (ArithmeticExpression)expression); } else if (expression.ExpressionType == ExpressionType.ConditionalLogical) { this.CheckConditionalLogicalExpressionParenthesis(parentElement, (ConditionalLogicalExpression)expression); } else if (expression.ExpressionType == ExpressionType.AnonymousMethod) { this.CheckAnonymousMethodParenthesis(parentElement, (AnonymousMethodExpression)expression); } } return(true); }
private static void CheckFileContents(CsElement element, CsElement parentElement, TopLevelElements topLevelElements) { if (element.ElementType == ElementType.Class) { if (((parentElement == null) || (parentElement.ElementType == ElementType.Root)) || (parentElement.ElementType == ElementType.Namespace)) { topLevelElements.Classes.Add((Class) element); } } else if (element.ElementType == ElementType.Namespace) { topLevelElements.Namespaces.Add((Namespace) element); } }
/// <summary> /// Processes the given element. /// </summary> /// <param name="element">The element being visited.</param> /// <param name="parentElement">The parent element, if any.</param> /// <param name="topLevelElements">The number of classes and namespaces seen in the document.</param> /// <returns>Returns true to continue, or false to stop the walker.</returns> private bool VisitElement( Element element, Element parentElement, TopLevelElements topLevelElements) { Param.AssertNotNull(element, "element"); Param.Ignore(parentElement); Param.AssertNotNull(topLevelElements, "topLevelElements"); this.CheckAccessModifierRulesForElement(element); this.CheckCodeAnalysisAttributeJustifications(element); this.CheckForEmptyElements(element); CheckFileContents(element, parentElement, topLevelElements); return(true); }
/// <summary> /// Called when a statement is visited. /// </summary> /// <param name="statement">The statement being visited.</param> /// <param name="parentExpression">The parent expression, if any.</param> /// <param name="parentStatement">The parent statement, if any.</param> /// <param name="parentElement">The parent element, if any.</param> /// <param name="context">The optional visitor context data.</param> /// <returns>Returns true to continue, or false to stop the walker.</returns> private bool ProcessStatement( Statement statement, Expression parentExpression, Statement parentStatement, CsElement parentElement, TopLevelElements context) { Param.AssertNotNull(statement, "statement"); Param.Ignore(parentExpression); Param.Ignore(parentStatement); Param.AssertNotNull(parentElement, "parentElement"); Param.Ignore(context); this.CheckForUnnecessaryStatements(statement, parentElement); return(true); }
/// <summary> /// Processes the given element. /// </summary> /// <param name="element">The element being visited.</param> /// <param name="parentElement">The parent of the element.</param> /// <param name="topLevelElements">The number of classes and namespaces seen in the document.</param> private static void CheckFileContents( CsElement element, CsElement parentElement, TopLevelElements topLevelElements) { Param.AssertNotNull(element, "element"); Param.Ignore(parentElement); Param.AssertNotNull(topLevelElements, "topLevelElements"); if (element.ElementType == ElementType.Class) { if (parentElement == null || parentElement.ElementType == ElementType.Root || parentElement.ElementType == ElementType.Namespace) { topLevelElements.Classes.Add((Class)element); } } else if (element.ElementType == ElementType.Namespace) { topLevelElements.Namespaces.Add((Namespace)element); } }
/// <summary> /// Checks the methods within the given document. /// </summary> /// <param name="document"> /// The document to check. /// </param> public override void AnalyzeDocument(CodeDocument document) { Param.RequireNotNull(document, "document"); CsDocument csdocument = (CsDocument)document; if (csdocument.RootElement != null && !csdocument.RootElement.Generated) { // Check the access modifier rules. TopLevelElements topLevelElements = new TopLevelElements(); csdocument.WalkDocument(this.ProcessElement, this.ProcessStatement, this.ProcessExpression, topLevelElements); // If there is more than one top-level class in the file, make sure they are all // partial classes and are all of the same type. if (topLevelElements.Classes.Count > 1) { string name = string.Empty; foreach (Class classElement in topLevelElements.Classes) { if (!classElement.Declaration.ContainsModifier(CsTokenType.Partial) || (!string.IsNullOrEmpty(name) && string.Compare(name, classElement.FullNamespaceName, StringComparison.Ordinal) != 0)) { // Set the violation line number to the second class in the file. int count = 0; foreach (Class c in topLevelElements.Classes) { if (count == 1) { this.AddViolation(c, c.LineNumber, Rules.FileMayOnlyContainASingleClass); break; } ++count; } break; } name = classElement.FullNamespaceName; } } // If there is more than one namespace in the file, this is a violation. if (topLevelElements.Namespaces.Count > 1) { // Set the violation line number to the second namespace in the file. int count = 0; foreach (Namespace n in topLevelElements.Namespaces) { if (count == 1) { this.AddViolation(n, n.LineNumber, Rules.FileMayOnlyContainASingleNamespace); break; } ++count; } } } }
/// <summary> /// Called when a statement is visited. /// </summary> /// <param name="statement"> /// The statement being visited. /// </param> /// <param name="parentExpression"> /// The parent expression, if any. /// </param> /// <param name="parentStatement"> /// The parent statement, if any. /// </param> /// <param name="parentElement"> /// The parent element, if any. /// </param> /// <param name="context"> /// The optional visitor context data. /// </param> /// <returns> /// Returns true to continue, or false to stop the walker. /// </returns> private bool ProcessStatement(Statement statement, Expression parentExpression, Statement parentStatement, CsElement parentElement, TopLevelElements context) { Param.AssertNotNull(statement, "statement"); Param.Ignore(parentExpression); Param.Ignore(parentStatement); Param.AssertNotNull(parentElement, "parentElement"); Param.Ignore(context); this.CheckForUnnecessaryStatements(statement, parentElement); return true; }
/// <summary> /// Processes the given element. /// </summary> /// <param name="element"> /// The element being visited. /// </param> /// <param name="parentElement"> /// The parent of the element. /// </param> /// <param name="topLevelElements"> /// The number of classes and namespaces seen in the document. /// </param> private static void CheckFileContents(CsElement element, CsElement parentElement, TopLevelElements topLevelElements) { Param.AssertNotNull(element, "element"); Param.Ignore(parentElement); Param.AssertNotNull(topLevelElements, "topLevelElements"); if (element.ElementType == ElementType.Class) { if (parentElement == null || parentElement.ElementType == ElementType.Root || parentElement.ElementType == ElementType.Namespace) { topLevelElements.Classes.Add((Class)element); } } else if (element.ElementType == ElementType.Namespace) { topLevelElements.Namespaces.Add((Namespace)element); } }
/// <summary> /// Processes the given element. /// </summary> /// <param name="element"> /// The element being visited. /// </param> /// <param name="parentElement"> /// The parent element, if any. /// </param> /// <param name="topLevelElements"> /// The number of classes and namespaces seen in the document. /// </param> /// <returns> /// Returns true to continue, or false to stop the walker. /// </returns> private bool ProcessElement(CsElement element, CsElement parentElement, TopLevelElements topLevelElements) { Param.AssertNotNull(element, "element"); Param.Ignore(parentElement); Param.AssertNotNull(topLevelElements, "topLevelElements"); this.CheckAccessModifierRulesForElement(element); this.CheckCodeAnalysisAttributeJustifications(element); this.CheckForEmptyElements(element); CheckFileContents(element, parentElement, topLevelElements); this.CheckParenthesisForAttributeConstructors(element); return true; }
/// <summary> /// Called when an expression is visited. /// </summary> /// <param name="expression"> /// The expression being visited. /// </param> /// <param name="parentExpression"> /// The parent expression, if any. /// </param> /// <param name="parentStatement"> /// The parent statement, if any. /// </param> /// <param name="parentElement"> /// The parent element, if any. /// </param> /// <param name="context"> /// The optional visitor context data. /// </param> /// <returns> /// Returns true to continue, or false to stop the walker. /// </returns> private bool ProcessExpression(Expression expression, Expression parentExpression, Statement parentStatement, CsElement parentElement, TopLevelElements context) { Param.AssertNotNull(expression, "expression"); Param.Ignore(parentExpression); Param.Ignore(parentStatement); Param.AssertNotNull(parentElement, "parentElement"); Param.Ignore(context); if (!parentElement.Generated) { // Determine whether this expression is a method invocation which contains call to Debug.Fail or Debug.Assert. if (expression.ExpressionType == ExpressionType.MethodInvocation) { MethodInvocationExpression methodInvocation = (MethodInvocationExpression)expression; if (methodInvocation.Name.Tokens.MatchTokens("Debug", ".", "Assert") || methodInvocation.Name.Tokens.MatchTokens("System", ".", "Diagnostics", ".", "Debug", ".", "Assert")) { this.CheckDebugAssertMessage(parentElement, methodInvocation); } else if (methodInvocation.Name.Tokens.MatchTokens("Debug", ".", "Fail") || methodInvocation.Name.Tokens.MatchTokens("System", ".", "Diagnostics", ".", "Debug", ".", "Fail")) { this.CheckDebugFailMessage(parentElement, methodInvocation); } } else if (expression.ExpressionType == ExpressionType.Parenthesized) { this.CheckParenthesizedExpression(parentElement, (ParenthesizedExpression)expression); } else if (expression.ExpressionType == ExpressionType.Arithmetic) { this.CheckArithmeticExpressionParenthesis(parentElement, (ArithmeticExpression)expression); } else if (expression.ExpressionType == ExpressionType.ConditionalLogical) { this.CheckConditionalLogicalExpressionParenthesis(parentElement, (ConditionalLogicalExpression)expression); } else if (expression.ExpressionType == ExpressionType.AnonymousMethod) { this.CheckAnonymousMethodParenthesis(parentElement, (AnonymousMethodExpression)expression); } } return true; }
/// <summary> /// Visits one code unit in the document. /// </summary> /// <param name="codeUnit">The item being visited.</param> /// <param name="parentElement">The parent element, if any.</param> /// <param name="parentStatement">The parent statement, if any.</param> /// <param name="parentExpression">The parent expression, if any.</param> /// <param name="parentClause">The parent query clause, if any.</param> /// <param name="parentToken">The parent token, if any.</param> /// <param name="topLevelElements">The number of classes and namespaces seen in the document.</param> /// <returns>Returns true to continue, or false to stop the walker.</returns> private bool VisitCodeUnit( CodeUnit codeUnit, Element parentElement, Statement parentStatement, Expression parentExpression, QueryClause parentClause, Token parentToken, TopLevelElements topLevelElements) { Param.AssertNotNull(codeUnit, "codeUnit"); Param.Ignore(parentElement, parentStatement, parentExpression, parentClause, parentToken); Param.AssertNotNull(topLevelElements, "topLevelElements"); if (codeUnit.CodeUnitType == CodeUnitType.Element) { return this.VisitElement((Element)codeUnit, parentElement, topLevelElements); } else if (codeUnit.CodeUnitType == CodeUnitType.Statement) { return this.VisitStatement((Statement)codeUnit, parentExpression, parentStatement, parentElement); } else if (codeUnit.CodeUnitType == CodeUnitType.Expression) { return this.VisitExpression((Expression)codeUnit, parentExpression, parentStatement, parentElement); } return !this.Cancel; }
private bool ProcessElement(CsElement element, CsElement parentElement, TopLevelElements topLevelElements) { this.CheckAccessModifierRulesForElement(element); this.CheckCodeAnalysisAttributeJustifications(element); this.CheckForEmptyElements(element); CheckFileContents(element, parentElement, topLevelElements); return true; }
/// <summary> /// Checks the methods within the given document. /// </summary> /// <param name="document">The document to check.</param> public override void AnalyzeDocument(CodeDocument document) { Param.RequireNotNull(document, "document"); CsDocument csdocument = (CsDocument)document; if (csdocument.RootElement != null && !csdocument.RootElement.Generated) { // Check the access modifier rules. TopLevelElements topLevelElements = new TopLevelElements(); csdocument.WalkDocument <TopLevelElements>( new CodeWalkerElementVisitor <TopLevelElements>(this.ProcessElement), new CodeWalkerStatementVisitor <TopLevelElements>(this.ProcessStatement), new CodeWalkerExpressionVisitor <TopLevelElements>(this.ProcessExpression), topLevelElements); // If there is more than one top-level class in the file, make sure they are all // partial classes and are all of the same type. if (topLevelElements.Classes.Count > 1) { string name = string.Empty; foreach (Class classElement in topLevelElements.Classes) { if (!classElement.Declaration.ContainsModifier(CsTokenType.Partial) || (!string.IsNullOrEmpty(name) && string.Compare(name, classElement.FullNamespaceName, StringComparison.Ordinal) != 0)) { // Set the violation line number to the second class in the file. int count = 0; foreach (Class c in topLevelElements.Classes) { if (count == 1) { this.AddViolation(c, c.LineNumber, Rules.FileMayOnlyContainASingleClass); break; } ++count; } break; } name = classElement.FullNamespaceName; } } // If there is more than one namespace in the file, this is a violation. if (topLevelElements.Namespaces.Count > 1) { // Set the violation line number to the second namespace in the file. int count = 0; foreach (Namespace n in topLevelElements.Namespaces) { if (count == 1) { this.AddViolation(n, n.LineNumber, Rules.FileMayOnlyContainASingleNamespace); break; } ++count; } } } }
private bool ProcessExpression(Expression expression, Expression parentExpression, Statement parentStatement, CsElement parentElement, TopLevelElements context) { if (!parentElement.Generated) { if (expression.ExpressionType == ExpressionType.MethodInvocation) { MethodInvocationExpression debugAssertMethodCall = (MethodInvocationExpression) expression; if (debugAssertMethodCall.Name.Tokens.MatchTokens(new string[] { "Debug", ".", "Assert" }) || debugAssertMethodCall.Name.Tokens.MatchTokens(new string[] { "System", ".", "Diagnostics", ".", "Debug", ".", "Assert" })) { this.CheckDebugAssertMessage(parentElement, debugAssertMethodCall); } else if (debugAssertMethodCall.Name.Tokens.MatchTokens(new string[] { "Debug", ".", "Fail" }) || debugAssertMethodCall.Name.Tokens.MatchTokens(new string[] { "System", ".", "Diagnostics", ".", "Debug", ".", "Fail" })) { this.CheckDebugFailMessage(parentElement, debugAssertMethodCall); } } else if (expression.ExpressionType == ExpressionType.Parenthesized) { this.CheckParenthesizedExpression(parentElement, (ParenthesizedExpression) expression); } else if (expression.ExpressionType == ExpressionType.Arithmetic) { this.CheckArithmeticExpressionParenthesis(parentElement, (ArithmeticExpression) expression); } else if (expression.ExpressionType == ExpressionType.ConditionalLogical) { this.CheckConditionalLogicalExpressionParenthesis(parentElement, (ConditionalLogicalExpression) expression); } else if (expression.ExpressionType == ExpressionType.AnonymousMethod) { this.CheckAnonymousMethodParenthesis(parentElement, (AnonymousMethodExpression) expression); } } return true; }
private bool ProcessStatement(Statement statement, Expression parentExpression, Statement parentStatement, CsElement parentElement, TopLevelElements context) { this.CheckForUnnecessaryStatements(statement, parentElement); return true; }