Ejemplo n.º 1
0
        private static bool IsSystemException(CatchDeclarationSyntax catchDeclaration, SemanticModel semanticModel)
        {
            var caughtTypeSyntax = catchDeclaration?.Type;

            return(caughtTypeSyntax == null ||
                   semanticModel.GetTypeInfo(caughtTypeSyntax).Type.Is(KnownType.System_Exception));
        }
Ejemplo n.º 2
0
        public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
        {
            var type       = node.Type.ToString();
            var identifier = node.Identifier.ValueText;

            _currentNode = new CatchDeclaration(type, identifier);
        }
            protected override ISymbol GetExceptionTypeSymbolFromCatchClause(CatchClauseSyntax catchNode, SemanticModel model)
            {
                Debug.Assert(catchNode != null);
                CatchDeclarationSyntax typeDeclNode = catchNode.Declaration;

                return(typeDeclNode == null ? TypesOfInterest.SystemObject : typeDeclNode.Type.GetDeclaredOrReferencedSymbol(model));
            }
Ejemplo n.º 4
0
        public static void Analyze(SyntaxNodeAnalysisContext context, CatchClauseSyntax catchClause)
        {
            CatchDeclarationSyntax declaration = catchClause.Declaration;

            if (declaration != null)
            {
                BlockSyntax block = catchClause.Block;

                if (block != null &&
                    declaration.Type != null &&
                    !block.Statements.Any())
                {
                    ITypeSymbol typeSymbol = context
                                             .SemanticModel
                                             .GetTypeSymbol(declaration.Type, context.CancellationToken);

                    if (typeSymbol?.IsErrorType() == false)
                    {
                        INamedTypeSymbol exceptionTypeSymbol = context.GetTypeByMetadataName(MetadataNames.System_Exception);

                        if (typeSymbol.Equals(exceptionTypeSymbol))
                        {
                            context.ReportDiagnostic(
                                DiagnosticDescriptors.AvoidEmptyCatchClauseThatCatchesSystemException,
                                catchClause.CatchKeyword.GetLocation());
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
 public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
 {
     if (node.Identifier != null)
     {
         map.Add(node.Identifier.ValueText, node.Identifier);
     }
     base.VisitCatchDeclaration(node);
 }
Ejemplo n.º 6
0
        private async static Task <Document> RemoveVariableAsync(Document document, CatchDeclarationSyntax variableUnused, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken);

            var newRoot = root.ReplaceNode(variableUnused, variableUnused.Update(variableUnused.OpenParenToken, variableUnused.Type.WithoutTrailingTrivia(), new SyntaxToken(), variableUnused.CloseParenToken));

            return(document.WithSyntaxRoot(newRoot));
        }
        private async static Task<Document> RemoveVariableAsync(Document document, CatchDeclarationSyntax variableUnused, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken);

            var newRoot = root.ReplaceNode(variableUnused, variableUnused.Update(variableUnused.OpenParenToken, variableUnused.Type.WithoutTrailingTrivia(), new SyntaxToken(), variableUnused.CloseParenToken));

            return document.WithSyntaxRoot(newRoot);
        }
Ejemplo n.º 8
0
        public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
        {
            Information.Add(InfoExtractor.Info.NAME, node.Identifier.Value);

            node.Type?.Accept(this);

            base.VisitCatchDeclaration(node);
        }
        private static bool IsCatchingNullReferenceException(CatchDeclarationSyntax catchDeclaration,
                                                             SemanticModel semanticModel)
        {
            var caughtTypeSyntax = catchDeclaration?.Type;

            return(caughtTypeSyntax != null &&
                   semanticModel.GetTypeInfo(caughtTypeSyntax).Type.Is(KnownType.System_NullReferenceException));
        }
Ejemplo n.º 10
0
 public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
 {
     if (node.Identifier != null)
     {
         map.Add(node.Identifier.ValueText, node.Identifier);
     }
     base.VisitCatchDeclaration(node);
 }
Ejemplo n.º 11
0
        public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
        {
            if (node.Identifier != default)
            {
                VisitLocal(node);
            }

            base.VisitCatchDeclaration(node);
        }
Ejemplo n.º 12
0
        public static void AnalyzeCatchClause(SyntaxNodeAnalysisContext context)
        {
            var catchClause = (CatchClauseSyntax)context.Node;

            BlockSyntax block = catchClause.Block;

            if (block == null)
            {
                return;
            }

            CatchDeclarationSyntax declaration = catchClause.Declaration;

            if (declaration == null)
            {
                return;
            }

            SemanticModel     semanticModel     = context.SemanticModel;
            CancellationToken cancellationToken = context.CancellationToken;

            ILocalSymbol symbol = semanticModel.GetDeclaredSymbol(declaration, cancellationToken);

            if (symbol?.IsErrorType() != false)
            {
                return;
            }

            //TODO: SyntaxWalker
            foreach (SyntaxNode node in block.DescendantNodes(descendIntoChildren: f => f.Kind() != SyntaxKind.CatchClause))
            {
                if (node.Kind() != SyntaxKind.ThrowStatement)
                {
                    continue;
                }

                var throwStatement          = (ThrowStatementSyntax)node;
                ExpressionSyntax expression = throwStatement.Expression;

                if (expression == null)
                {
                    continue;
                }

                ISymbol expressionSymbol = semanticModel.GetSymbol(expression, cancellationToken);

                if (!symbol.Equals(expressionSymbol))
                {
                    continue;
                }

                context.ReportDiagnostic(
                    DiagnosticDescriptors.RemoveOriginalExceptionFromThrowStatement,
                    expression);
            }
        }
            public override SyntaxNode VisitCatchDeclaration(CatchDeclarationSyntax node)
            {
                if (node.Identifier.ValueText != null)
                {
                    var newIdName = RegisterNewReanmedVariable(node.Identifier.ValueText);
                    node = node.WithIdentifier(SyntaxFactory.Identifier(newIdName));
                }

                return(base.VisitCatchDeclaration(node));
            }
Ejemplo n.º 14
0
        public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
        {
            var exceptionType = this.semanticModel.GetTypeInfo(node.Type).Type;

            if (exceptionType != null)
            {
                addDependency(this.currentClass, Dependency.DECLARE, exceptionType.ToString());
            }
            base.VisitCatchDeclaration(node);
        }
Ejemplo n.º 15
0
    public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
    {
        ISymbol typeSymbol     = semanticModel.GetTypeInfo(node.Type).Type;
        var     exceptionClass = (FAMIX.Class)importer.EnsureType(typeSymbol, typeof(FAMIX.Class));

        FAMIX.CaughtException caughtException = importer.New <FAMIX.CaughtException>();
        caughtException.definingMethod = currentMethod;
        caughtException.exceptionClass = exceptionClass;
        base.VisitCatchDeclaration(node);
    }
        private static bool CatchIsTooGeneric(SyntaxNodeAnalysisContext context, CatchDeclarationSyntax declaration)
        {
            var symbol = context.SemanticModel.GetSymbolInfo(declaration.Type);
            if (symbol.Symbol == null)
            {
                return false;
            }

            var exception = context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(Exception).FullName);
            return symbol.Symbol == exception;
        }
Ejemplo n.º 17
0
        public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
        {
            var identifier = node.Identifier;

            if (identifier.Span.Length != 0)
            {
                FindSpellingMistakesForIdentifier(identifier);
            }

            base.VisitCatchDeclaration(node);
        }
        public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
        {
            ISymbol exceptionSymbol = _semanticModel.GetSymbolInfo(node.Type).Symbol;

            if (exceptionSymbol.ToString() != "System.Exception")
            {
                throw new NotSupportedException("Catch block for inherited exception types is not supported");
            }

            base.VisitCatchDeclaration(node);
        }
        private CatchClauseSyntax ParseCatchClause(bool hasCatchAll)
        {
            Debug.Assert(this.CurrentToken.Kind == SyntaxKind.CatchKeyword);

            var @catch = this.EatToken();

            // Check for the error of catch clause following empty catch here.
            if (hasCatchAll)
            {
                @catch = this.AddError(@catch, ErrorCode.ERR_TooManyCatches);
            }

            CatchDeclarationSyntax decl = null;
            var saveTerm = this._termState;

            if (this.CurrentToken.Kind == SyntaxKind.OpenParenToken)
            {
                var openParen = this.EatToken();
                this._termState |= TerminatorState.IsEndOfCatchClause;
                var         type = this.ParseClassType();
                SyntaxToken name = null;

                if (this.IsTrueIdentifier())
                {
                    name = this.ParseIdentifierToken();
                }

                this._termState = saveTerm;
                var closeParen = this.EatToken(SyntaxKind.CloseParenToken);
                decl = _syntaxFactory.CatchDeclaration(openParen, type, name, closeParen);
            }

            CatchFilterClauseSyntax filter = null;

            if (this.CurrentToken.Kind == SyntaxKind.IfKeyword)
            {
                var ifKeyword = CheckFeatureAvailability(this.EatToken(), MessageID.IDS_FeatureExceptionFilter);
                this._termState |= TerminatorState.IsEndOfilterClause;
                var openParen        = this.EatToken(SyntaxKind.OpenParenToken);
                var filterExpression = this.ParseExpression();

                this._termState = saveTerm;
                var closeParen = this.EatToken(SyntaxKind.CloseParenToken);
                filter = _syntaxFactory.CatchFilterClause(ifKeyword, openParen, filterExpression, closeParen);
            }

            this._termState |= TerminatorState.IsEndOfCatchBlock;
            var block = this.ParseBlock();

            this._termState = saveTerm;

            return(_syntaxFactory.CatchClause(@catch, decl, filter, block));
        }
Ejemplo n.º 20
0
 public override void AddChildren()
 {
     Kind                   = Node.Kind();
     _catchKeyword          = ((CatchClauseSyntax)Node).CatchKeyword;
     _catchKeywordIsChanged = false;
     _declaration           = ((CatchClauseSyntax)Node).Declaration;
     _declarationIsChanged  = false;
     _filter                = ((CatchClauseSyntax)Node).Filter;
     _filterIsChanged       = false;
     _block                 = ((CatchClauseSyntax)Node).Block;
     _blockIsChanged        = false;
 }
Ejemplo n.º 21
0
        public static void Write(this CatchDeclarationSyntax syntax, IIndentedTextWriterWrapper textWriter, IContext context)
        {
            if (syntax.Type != null)
            {
                var symbol = (ITypeSymbol)context.SemanticModel.GetSymbolInfo(syntax.Type).Symbol;
                textWriter.Write("type = ");
                context.TypeReferenceWriter.WriteTypeReference(symbol, textWriter);
                textWriter.WriteLine(",");
            }

            textWriter.WriteLine("func = function({0})", syntax.Identifier.Text);
        }
Ejemplo n.º 22
0
        private static void AnalyzeCatchClause(SyntaxNodeAnalysisContext context)
        {
            var catchClause = (CatchClauseSyntax)context.Node;

            CatchDeclarationSyntax declaration = catchClause.Declaration;

            if (declaration == null)
            {
                return;
            }

            SemanticModel     semanticModel     = context.SemanticModel;
            CancellationToken cancellationToken = context.CancellationToken;

            ILocalSymbol symbol = semanticModel.GetDeclaredSymbol(declaration, cancellationToken);

            if (symbol?.IsErrorType() != false)
            {
                return;
            }

            ExpressionSyntax expression = null;
            Walker           walker     = null;

            try
            {
                walker = Walker.GetInstance();

                walker.Symbol            = symbol;
                walker.SemanticModel     = semanticModel;
                walker.CancellationToken = cancellationToken;

                walker.VisitBlock(catchClause.Block);

                expression = walker.ThrowStatement?.Expression;
            }
            finally
            {
                if (walker != null)
                {
                    Walker.Free(walker);
                }
            }

            if (expression != null)
            {
                DiagnosticHelpers.ReportDiagnostic(
                    context,
                    DiagnosticRules.RemoveOriginalExceptionFromThrowStatement,
                    expression);
            }
        }
Ejemplo n.º 23
0
        public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
        {
            if (!PreVisit(node))
            {
                return;
            }

            node.Type?.Accept(this);

            base.VisitCatchDeclaration(node);

            PostVisit(node);
        }
        // TODO: copy-paste! Refactor if needed!
        private static bool CatchIsTooGeneric(SyntaxNodeAnalysisContext context, CatchDeclarationSyntax declaration)
        {
            var symbol = context.SemanticModel.GetSymbolInfo(declaration.Type);

            if (symbol.Symbol == null)
            {
                return(false);
            }

            var exception = context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(Exception).FullName);

            return(symbol.Symbol == exception);
        }
Ejemplo n.º 25
0
        public static VariableDeclaration Create(Context cx, CatchDeclarationSyntax d, bool isVar, IExpressionParentEntity parent, int child)
        {
            var symbol = cx.GetModel(d).GetDeclaredSymbol(d) !;
            var type   = symbol.GetAnnotatedType();
            var ret    = Create(cx, d, type, parent, child);

            cx.Try(d, null, () =>
            {
                var declSymbol = cx.GetModel(d).GetDeclaredSymbol(d) !;
                var l          = LocalVariable.Create(cx, declSymbol);
                l.PopulateManual(ret, isVar);
                TypeMention.Create(cx, d.Type, ret, type);
            });
            return(ret);
        }
Ejemplo n.º 26
0
        public static VariableDeclaration Create(Context cx, CatchDeclarationSyntax d, bool isVar, IExpressionParentEntity parent, int child)
        {
            var type = Type.Create(cx, cx.Model(d).GetDeclaredSymbol(d).Type);
            var ret  = Create(cx, d, type, isVar, parent, child);

            cx.Try(d, null, () =>
            {
                var id         = d.Identifier;
                var declSymbol = cx.Model(d).GetDeclaredSymbol(d);
                var location   = cx.Create(id.GetLocation());
                LocalVariable.Create(cx, declSymbol, ret, isVar, location);
                TypeMention.Create(cx, d.Type, ret, type);
            });
            return(ret);
        }
Ejemplo n.º 27
0
        public CSharpSyntaxNode Convert(CatchClause node)
        {
            BlockSyntax csCatchBlock = node.Block.ToCsNode <BlockSyntax>();

            if (node.VariableDeclaration == null)
            {
                return(SyntaxFactory.CatchClause().WithBlock(csCatchBlock));
            }
            else
            {
                CatchDeclarationSyntax csCatchDeclaration = SyntaxFactory.CatchDeclaration(
                    SyntaxFactory.IdentifierName(node.VariableDeclaration.Type.Text),
                    SyntaxFactory.Identifier(node.VariableDeclaration.Name.Text));

                return(SyntaxFactory.CatchClause().WithDeclaration(csCatchDeclaration).WithBlock(csCatchBlock));
            }
        }
Ejemplo n.º 28
0
        public static bool CatchIsTooGeneric(this CatchDeclarationSyntax declaration, SemanticModel semanticModel)
        {
            if (declaration == null)
            {
                return(true);
            }

            var symbol = semanticModel.GetSymbolInfo(declaration.Type).Symbol;

            if (symbol == null)
            {
                return(false);
            }

            var compilation = semanticModel.Compilation;

            return(IsGenericException(symbol, compilation));
        }
Ejemplo n.º 29
0
 /// <summary>
 /// Traverse AST node that represents catch clause parameter declaration
 /// </summary>
 /// <param name="node">AST node.</param>
 public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
 {
     try
     {
         var symbol = _sm.GetDeclaredSymbol(node);
         if (symbol != null)
         {
             var def = Def.For(symbol: symbol, type: "local", name: symbol.Name).At(_path, node.Identifier.Span);
             def.Local    = true;
             def.Exported = false;
             AddDef(def);
         }
         base.VisitCatchDeclaration(node);
     }
     catch (Exception e)
     {
     }
 }
Ejemplo n.º 30
0
        public static bool CatchIsTooGeneric(this CatchDeclarationSyntax declaration, SemanticModel semanticModel)
        {
            if (declaration == null)
            {
                return(true);
            }

            var symbol = semanticModel.GetSymbolInfo(declaration.Type);

            if (symbol.Symbol == null)
            {
                return(false);
            }

            var exception = semanticModel.Compilation.GetTypeByMetadataName(typeof(Exception).FullName);

            return(symbol.Symbol.Equals(exception));
        }
Ejemplo n.º 31
0
        public override SyntaxNode VisitCatchDeclaration(CatchDeclarationSyntax node)
        {
            string newCatch = null;

            foreach (var c in catchReplacementList)
            {
                if (node.Type.ToString().Contains(c.Key))
                {
                    newCatch = node.Type.ToString().Replace(c.Key, c.Value);
                }
            }
            if (newCatch != null)
            {
                return(CatchDeclaration(ParseTypeName(newCatch), node.Identifier));
            }
            else
            {
                return(base.VisitCatchDeclaration(node));
            }
        }
        public static void Analyze(SyntaxNodeAnalysisContext context, CatchClauseSyntax catchClause)
        {
            CatchDeclarationSyntax declaration = catchClause.Declaration;

            if (declaration != null)
            {
                BlockSyntax block = catchClause.Block;

                if (block != null)
                {
                    ILocalSymbol symbol = context
                                          .SemanticModel
                                          .GetDeclaredSymbol(catchClause.Declaration, context.CancellationToken);

                    if (symbol != null)
                    {
                        foreach (SyntaxNode node in block.DescendantNodes(f => !f.IsKind(SyntaxKind.CatchClause)))
                        {
                            if (node.IsKind(SyntaxKind.ThrowStatement))
                            {
                                var throwStatement = (ThrowStatementSyntax)node;
                                if (throwStatement.Expression != null)
                                {
                                    ISymbol expressionSymbol = context
                                                               .SemanticModel
                                                               .GetSymbol(throwStatement.Expression, context.CancellationToken);

                                    if (expressionSymbol != null &&
                                        symbol.Equals(expressionSymbol))
                                    {
                                        context.ReportDiagnostic(
                                            DiagnosticDescriptors.RemoveOriginalExceptionFromThrowStatement,
                                            throwStatement.Expression);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 33
0
        private Diagnostic AnalyzeCatchDeclaration(CatchDeclarationSyntax node)
        {
            if (node is null)
            {
                return(null); // we don't have an exception
            }

            var identifier = node.Identifier;
            var name       = identifier.ValueText;

            switch (name)
            {
            case null:         // we don't have one
            case "":           // we don't have one
            case ExpectedName: // correct identifier
                return(null);

            default:
                return(Issue(name, identifier, ExpectedName));
            }
        }
Ejemplo n.º 34
0
        /// <summary>
        /// Given a catch declaration, get the symbol for the exception variable
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="catchDeclaration"></param>
        public ILocalSymbol GetDeclaredSymbol(CatchDeclarationSyntax catchDeclaration, CancellationToken cancellationToken = default(CancellationToken))
        {
            CSharpSyntaxNode catchClause = catchDeclaration.Parent; //Syntax->Binder map is keyed on clause, not decl
            Debug.Assert(catchClause.Kind() == SyntaxKind.CatchClause);
            Binder enclosingBinder = this.GetEnclosingBinder(GetAdjustedNodePosition(catchClause));

            if (enclosingBinder == null)
            {
                return null;
            }

            Binder catchBinder = enclosingBinder.GetBinder(catchClause);

            // Binder.GetBinder can fail in presence of syntax errors. 
            if (catchBinder == null)
            {
                return null;
            }

            catchBinder = enclosingBinder.GetBinder(catchClause);
            LocalSymbol local = catchBinder.GetDeclaredLocalsForScope(catchClause).FirstOrDefault();
            return ((object)local != null && local.DeclarationKind == LocalDeclarationKind.CatchVariable)
                ? local
                : null;
        }
        public CatchDeclarationTranslation(CatchDeclarationSyntax syntax, SyntaxTranslation parent) : base(syntax, parent)
        {

        }
 public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
 {
     _builder.Add(node);
     base.VisitCatchDeclaration(node);
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="node"></param>
 public override sealed void VisitCatchDeclaration(CatchDeclarationSyntax node)
 {
     this.OnNodeVisited(node, this.type.IsInstanceOfType(node));
     base.VisitCatchDeclaration(node);
 }
Ejemplo n.º 38
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="node"></param>
 public override sealed void VisitCatchDeclaration(CatchDeclarationSyntax node)
 {
     this.OnNodeVisited(node);
     if (!this.traverseRootOnly) base.VisitCatchDeclaration(node);
 }
Ejemplo n.º 39
0
        public void VisitCatchDeclaration(CatchDeclarationSyntax node)
        {
            if (node == null)
                throw new ArgumentNullException("node");

            node.Validate();

            _writer.WriteSyntax(Syntax.OpenParen);

            if (_writer.Configuration.Spaces.WithinParentheses.CatchParentheses)
                _writer.WriteSpace();

            node.Type.Accept(this);

            if (node.Identifier != null)
            {
                _writer.WriteSpace();
                _writer.WriteIdentifier(node.Identifier);
            }

            if (_writer.Configuration.Spaces.WithinParentheses.CatchParentheses)
                _writer.WriteSpace();

            _writer.WriteSyntax(Syntax.CloseParen);
        }
Ejemplo n.º 40
0
 public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
 {
     this.Found |= node.Identifier.ValueText == this.name;
     base.VisitCatchDeclaration(node);
 }
 public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
 {
     base.VisitCatchDeclaration(node);
     Add(node.Identifier);
 }
            private IEnumerable<ITypeSymbol> InferTypeInCatchDeclaration(CatchDeclarationSyntax catchDeclaration, SyntaxToken? previousToken = null)
            {
                // If we have a position, it has to be after "catch("
                if (previousToken.HasValue && previousToken.Value != catchDeclaration.OpenParenToken)
                {
                    return SpecializedCollections.EmptyEnumerable<ITypeSymbol>();
                }

                return SpecializedCollections.SingletonEnumerable(this.Compilation.ExceptionType());
            }