Esempio n. 1
0
 public override void VisitInvocationExpression(InvocationExpressionSyntax node)
 {
     if (node.Expression is MemberAccessExpressionSyntax member &&
         member.Expression is IdentifierNameSyntax className &&
         className.Identifier.Text == nameof(Monitor) &&
         node.ArgumentList.Arguments.Count > 0)
     {
         var symbol = SymbolHelper.GetSymbol(node.ArgumentList.Arguments[0].Expression, SemanticModel);
         if (member.Name.Identifier.Text == nameof(Monitor.Enter))
         {
             CurrentLock.Push(symbol);
         }
         else if (member.Name.Identifier.Text == nameof(Monitor.Exit))
         {
             if (CurrentLock.Count > 0)
             {
                 var value = CurrentLock.Pop();
                 if (!value.Equals(symbol))
                 {
                     throw new InvalidOperationException("Wrong locking");
                 }
             }
         }
     }
     base.VisitInvocationExpression(node);
 }
Esempio n. 2
0
        public override void VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
        {
            var identifierText = (node.Expression as IdentifierNameSyntax)?.Identifier.Text;

            if (identifierText != nameof(Type) ||
                identifierText != nameof(TypeCode))
            {
                var symbol = SymbolHelper.GetSymbol(node.Expression, SemanticModel);

                if (symbol != null && _newObjectsAndLocks.Keys.Contains(symbol))
                {
                    var objectLocks = _newObjectsAndLocks[symbol];

                    if (!objectLocks.All(x => CurrentLock.Contains(x)))
                    {
                        if (!PotentialIssuesFound.Contains(symbol))
                        {
                            if (GloablOptions.Verbose)
                            {
                                Console.WriteLine($"For node {node}");
                                Console.WriteLine($"With parent {node.Parent}");
                                Console.WriteLine($"In lock(s) {String.Join(", ", objectLocks)}");
                                Console.WriteLine();
                            }
                            PotentialIssuesFound.Add(symbol);
                        }
                    }
                }
            }
            base.VisitMemberAccessExpression(node);
        }
Esempio n. 3
0
        public override void VisitLockStatement(LockStatementSyntax node)
        {
            var symbol = SymbolHelper.GetSymbol(node, SemanticModel);

            CurrentLock.Push(symbol);
            base.VisitLockStatement(node);
            CurrentLock.Pop();
        }
Esempio n. 4
0
        private static string MapParameter(VisualBasicSyntax.ParameterSyntax parameter, bool useLongNames, SemanticModel semanticModel)
        {
            if (semanticModel == null)
            {
                return(string.Empty);
            }
            var symbol = SymbolHelper.GetSymbol <IParameterSymbol>(semanticModel, parameter);

            return(TypeMapper.Map(symbol?.Type, useLongNames));
        }
Esempio n. 5
0
        public override void VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
        {
            if (InMethod && CurrentLock.Count > 0)
            {
                ISymbol symbol = null;
                if (node.Parent is AssignmentExpressionSyntax assignmentExpressionSyntax)
                {
                    symbol = SymbolHelper.GetSymbol(assignmentExpressionSyntax.Left, SemanticModel);
                }
                else if (node.Parent is ThrowStatementSyntax ||
                         node.Parent is YieldStatementSyntax ||
                         node.Parent is ArgumentSyntax ||
                         node.Parent is ReturnStatementSyntax)
                {
                    // do nothing
                }
                else if (node.Parent.Parent is VariableDeclaratorSyntax variableDeclaratorSyntax)
                {
                    symbol = SemanticModel.GetDeclaredSymbol(variableDeclaratorSyntax);
                }

                if (symbol != null)
                {
                    var blockSyntax = node.Parent.Ancestors().OfType <BlockSyntax>().First();

                    var analyzeBlockWalker = new AnalyzeBlockWalker(SemanticModel, symbol);
                    analyzeBlockWalker.Visit(blockSyntax);

                    var nonVolatileFieldsAndProperties = analyzeBlockWalker.NonVolatileFieldsAndProperties.ToList();

                    if (nonVolatileFieldsAndProperties.Any())
                    {
                        var locks = CurrentLock.ToList();
                        foreach (var nonVolatileFieldsAndProperty in nonVolatileFieldsAndProperties)
                        {
                            if (!NewObjectsAndLocks.ContainsKey(nonVolatileFieldsAndProperty))
                            {
                                if (GloablOptions.Verbose)
                                {
                                    Console.WriteLine($"For node {node}");
                                    Console.WriteLine($"With parent {node.Parent}");
                                    Console.WriteLine($"In lock(s) {String.Join(", ", locks)}");
                                    Console.WriteLine();
                                }
                                NewObjectsAndLocks.Add(nonVolatileFieldsAndProperty, locks);
                            }
                        }
                    }
                }
            }

            base.VisitObjectCreationExpression(node);
        }
Esempio n. 6
0
        public static CodePropertyItem MapProperty(VisualBasicSyntax.PropertyBlockSyntax member,
                                                   ICodeViewUserControl control, SemanticModel semanticModel)
        {
            if (member == null)
            {
                return(null);
            }

            var item = BaseMapper.MapBase <CodePropertyItem>(member, member.PropertyStatement.Identifier,
                                                             member.PropertyStatement.Modifiers, control, semanticModel);

            var symbol = SymbolHelper.GetSymbol <IPropertySymbol>(semanticModel, member);

            item.Type = TypeMapper.Map(symbol?.Type);

            if (member.Accessors != null)
            {
                if (member.Accessors.Any(a => a.Kind() == VisualBasic.SyntaxKind.GetAccessorBlock))
                {
                    item.Parameters += "get";
                }

                if (member.Accessors.Any(a => a.Kind() == VisualBasic.SyntaxKind.SetAccessorBlock))
                {
                    item.Parameters += string.IsNullOrEmpty(item.Parameters) ? "set" : ",set";
                }

                if (!string.IsNullOrEmpty(item.Parameters))
                {
                    item.Parameters = $" {{{item.Parameters}}}";
                }
            }

            item.Tooltip = TooltipMapper.Map(item.Access, item.Type, item.Name, item.Parameters);
            item.Kind    = CodeItemKindEnum.Property;
            item.Moniker = IconMapper.MapMoniker(item.Kind, item.Access);

            if (TriviaSummaryMapper.HasSummary(member) && SettingsHelper.UseXMLComments)
            {
                item.Tooltip = TriviaSummaryMapper.Map(member);
            }

            return(item);
        }
Esempio n. 7
0
        public static CodeItem MapMethod(VisualBasicSyntax.MethodBlockSyntax member, ICodeViewUserControl control, SemanticModel semanticModel)
        {
            if (member == null)
            {
                return(null);
            }

            CodeItem item;

            var statementsCodeItems = StatementMapper.MapStatement(member.Statements, control, semanticModel);

            if (VisibilityHelper.ShouldBeVisible(CodeItemKindEnum.Switch) && statementsCodeItems.Any())
            {
                // Map method as item containing statements
                item = BaseMapper.MapBase <CodeClassItem>(member, member.SubOrFunctionStatement.Identifier,
                                                          member.SubOrFunctionStatement.Modifiers, control, semanticModel);
                ((CodeClassItem)item).Members.AddRange(statementsCodeItems);
                ((CodeClassItem)item).BorderColor = Colors.DarkGray;
            }
            else
            {
                // Map method as single item
                item = BaseMapper.MapBase <CodeFunctionItem>(member, member.SubOrFunctionStatement.Identifier,
                                                             member.SubOrFunctionStatement.Modifiers, control, semanticModel);

                var symbol = SymbolHelper.GetSymbol <IMethodSymbol>(semanticModel, member);
                ((CodeFunctionItem)item).Type       = TypeMapper.Map(symbol?.ReturnType);
                ((CodeFunctionItem)item).Parameters = ParameterMapper.MapParameters(member.SubOrFunctionStatement.ParameterList, semanticModel);
                item.Tooltip = TooltipMapper.Map(item.Access, ((CodeFunctionItem)item).Type, item.Name,
                                                 member.SubOrFunctionStatement.ParameterList, semanticModel);
            }

            item.Id      = IdMapper.MapId(item.FullName, member.SubOrFunctionStatement.ParameterList, semanticModel);
            item.Kind    = CodeItemKindEnum.Method;
            item.Moniker = IconMapper.MapMoniker(item.Kind, item.Access);

            if (TriviaSummaryMapper.HasSummary(member) && SettingsHelper.UseXMLComments)
            {
                item.Tooltip = TriviaSummaryMapper.Map(member);
            }

            return(item);
        }