当前位置: 首页>>代码示例>>C#>>正文


C# IDocument.GetSemanticModel方法代码示例

本文整理汇总了C#中IDocument.GetSemanticModel方法的典型用法代码示例。如果您正苦于以下问题:C# IDocument.GetSemanticModel方法的具体用法?C# IDocument.GetSemanticModel怎么用?C# IDocument.GetSemanticModel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IDocument的用法示例。


在下文中一共展示了IDocument.GetSemanticModel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetIssues

        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
        {
            var parameterSyntax = (ParameterSyntax)node;
            var parentMethodBody = parameterSyntax.GetParentMethodBody();
            
            if (parentMethodBody == null) // abstract/interface/extern
                yield break;

            var model = document.GetSemanticModel(cancellationToken);
            var parentMethod = parameterSyntax.Parent.Parent as BaseMethodDeclarationSyntax;

            if (parentMethod != null &&
                parentMethod.SignatureIsRequiredToSatisfyExternalDependency(document, cancellationToken))
                yield break;

            var dataFlow = model.AnalyzeDataFlow(parentMethodBody);

            if (!dataFlow.Succeeded)
                yield break;

            var parameterSymbol = model.GetDeclaredSymbol(node, cancellationToken);

            bool isUsed = dataFlow.ReadInside.Contains(parameterSymbol) || dataFlow.WrittenInside.Contains(parameterSymbol);

            if (!isUsed)
                yield return new CodeIssue(CodeIssueKind.Unnecessary, node.Span,
                    string.Format("{0} is not used.", parameterSymbol.Name));
        }
开发者ID:PashaPash,项目名称:Refactorings,代码行数:28,代码来源:UnusedParameterCodeIssueProvider.cs

示例2: GetIssues

        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
        {
            LoadKnownPure();
            var methodCall = node as InvocationExpressionSyntax;//Must be method call
            Debug.Assert(methodCall != null);

            if (!(methodCall.Parent is ExpressionStatementSyntax))//Method call must be the outermost in a statement
                yield break;

            var semanticModel = document.GetSemanticModel();
            var symbol = semanticModel.GetSemanticInfo(methodCall.Expression).Symbol as IMethodSymbol;

            if (symbol == null)//Symbol for method unavailable
                yield break;

            if (knownPureFunctions.Contains(GetMethodNameCecilStyle(symbol)) ||
                symbol.GetAttributes().Any(attr => attr.AttributeClass.Name == "PureAttribute"))
            {
                var issueDescription = string.Format("Call to pure method '{0}' used as a statement.", symbol);
                yield return new CodeIssue(CodeIssue.Severity.Warning, methodCall.Span, issueDescription);
            }

            var typeSymbol = symbol.ContainingType;

            if (typeSymbol.GetAttributes().Any(attr => attr.AttributeClass.Name == "PureAttribute"))
            {
                var issueDescription = string.Format("Call to method '{0}' on pure type '{1}' used as a statement.", symbol.Name, typeSymbol);
                yield return new CodeIssue(CodeIssue.Severity.Warning, methodCall.Span, issueDescription);
            }
        }
开发者ID:jorik041,项目名称:Roslyn,代码行数:30,代码来源:CodeIssueProvider.cs

示例3: GetIssues

        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
        {
            var declaration = node as VariableDeclarationSyntax;
            var loop = node as ForEachStatementSyntax;
            var model = document.GetSemanticModel();
            var assume = Assumptions.All;

            if (declaration != null) {
                foreach (var r in GetSimplifications(declaration, model, assume, cancellationToken)) {
                    yield return new CodeIssue(
                        CodeIssue.Severity.Warning,
                        r.SuggestedSpan ?? declaration.Type.Span,
                        "Type of local variable can be infered.",
                        new[] { r.AsCodeAction(document) });
                }
            } else if (loop != null) {
                foreach (var r in GetSimplifications(loop, model, assume, cancellationToken)) {
                    yield return new CodeIssue(
                        CodeIssue.Severity.Warning,
                        r.SuggestedSpan ?? loop.Type.Span,
                        "Type of loop variable can be infered.",
                        new[] { r.AsCodeAction(document) });
                }
            }
        }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:25,代码来源:InferableType.cs

示例4: ClassifyNode

        public IEnumerable<SyntaxClassification> ClassifyNode(IDocument document, CommonSyntaxNode syntax, CancellationToken cancellationToken)
        {
            // If this node is a field declaration, return syntax classifications for the
            // identifier token of each field name.
            //
            // For example, "x" and "y" would be classified in the following code:
            // 
            // class C
            // {
            //     int x, y;
            // }
            if (syntax is FieldDeclarationSyntax)
            {
                var field = (FieldDeclarationSyntax)syntax;

                return from v in field.Declaration.Variables
                       select new SyntaxClassification(v.Identifier.Span, fieldClassification);
            }

            // If this node is an identifier, use the binding API to retrieve its symbol and return a 
            // syntax classification for the node if that symbol is a field.
            if (syntax is IdentifierNameSyntax)
            {
                var semanticModel = document.GetSemanticModel(cancellationToken);
                var symbol = semanticModel.GetSemanticInfo(syntax).Symbol;

                if (symbol != null && symbol.Kind == CommonSymbolKind.Field)
                {
                    return new[] { new SyntaxClassification(syntax.Span, fieldClassification) };
                }
            }

            return null;
        }
开发者ID:jjrdk,项目名称:CqrsMessagingTools,代码行数:34,代码来源:FieldSyntaxClassifier.cs

示例5: TypeHierarchyAnalyzerTests

 public TypeHierarchyAnalyzerTests()
 {
     var source = FileUtil.ReadAllText(TestUtil.GetFakeSourceFolder() + "/TypeHierarchyFakeSource.cs");
     var converter = new String2IDocumentConverter();
     document = (IDocument)converter.Convert(source, null, null, null);
     analyzer = AnalyzerFactory.GetTypeHierarchyAnalyzer();
     analyzer.SetSemanticModel(document.GetSemanticModel());
     root = (SyntaxNode) document.GetSyntaxRoot();
 }
开发者ID:nkcsgexi,项目名称:GhostFactor2,代码行数:9,代码来源:TypeHierarchyAnalyzerTests.cs

示例6: GetIssues

        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
        {
            var statement = (IfStatementSyntax)node;
            var model = document.GetSemanticModel();
            var newStatement = statement.DropEmptyBranchesIfApplicable(Assumptions.All, model);
            if (newStatement == statement) return null;

            var r = new ReadyCodeAction("Omit useless code", document, statement, () => newStatement);
            return r.CodeIssues1(CodeIssue.Severity.Warning, statement.IfKeyword.Span, "Useless branch");
        }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:10,代码来源:UselessBranch.cs

示例7: GetIssues

 public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
 {
     var forLoop = (ForEachStatementSyntax)node;
     var model = document.GetSemanticModel();
     var simplifications = GetSimplifications(forLoop, model, Assumptions.All, cancellationToken);
     return simplifications.Select(e => new CodeIssue(
         CodeIssue.Severity.Warning,
         forLoop.ForEachKeyword.Span,
         "For each loop body can be simplified by projecting.",
         new[] { e.AsCodeAction(document) }));
 }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:11,代码来源:ForEachProject.cs

示例8: GetIssues

        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
        {
            var invocation = (InvocationExpressionSyntax)node;
            var model = document.GetSemanticModel();
            var simplifications = GetSimplifications(invocation, model, cancellationToken);

            return simplifications.Select(e => new CodeIssue(
                CodeIssue.Severity.Warning,
                invocation.ArgumentList.Span,
                "Filter can be inlined into fold.",
                new[] { e.AsCodeAction(document) }));
        }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:12,代码来源:FilterSpecialize.cs

示例9: GetIssues

        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
        {
            var declaration = (LocalDeclarationStatementSyntax)node;
            var model = document.GetSemanticModel();
            var assume = Assumptions.All;

            foreach (var r in GetSimplifications(declaration, model, assume, cancellationToken)) {
                yield return new CodeIssue(
                    CodeIssue.Severity.Warning,
                    r.SuggestedSpan ?? declaration.Declaration.Type.Span,
                    "Scope of local variable can be reduced.",
                    new[] { r.AsCodeAction(document) });
            }
        }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:14,代码来源:OverexposedLocal.cs

示例10: ReplaceVarByConcreteAction

        public ReplaceVarByConcreteAction(ICodeActionEditFactory editFactory, IDocument document, TypeSyntax typeSyntax)
        {
            this.editFactory = editFactory;
            this.document = document;
            this.typeSyntax = typeSyntax;

            var semanticModel = (SemanticModel)document.GetSemanticModel();
            var syntaxTree = (SyntaxTree)document.GetSyntaxTree();

            ILocation location = syntaxTree.GetLocation(typeSyntax);

            ITypeSymbol variableType = semanticModel.GetSemanticInfo(typeSyntax).Type;
            this.typeName = variableType.ToMinimalDisplayString((Location)location, semanticModel);
        }
开发者ID:jorik041,项目名称:Roslyn,代码行数:14,代码来源:ReplaceVarByConcreteAction.cs

示例11: HandleDocument

        private static void HandleDocument(IDocument document)
        {
            Console.WriteLine(document.DisplayName);
            var tree = document.GetSyntaxTree();
            var sema = document.GetSemanticModel();

            //HandleCompilation(sema.Compilation);

            var methods = tree.Root.DescendentNodes().OfType<MethodDeclarationSyntax>();
            foreach (var method in methods)
            {
                HandleMethod(sema, method);
            }
        }
开发者ID:halllo,项目名称:MTSS12,代码行数:14,代码来源:SolutionHandler.cs

示例12: GetIssues

        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
        {
            var model = document.GetSemanticModel();
            var b = (BinaryExpressionSyntax)node;
            var actions = GetSimplifications(b, model, Assumptions.All, cancellationToken)
                          .Select(e => e.AsCodeAction(document))
                          .ToArray();
            if (actions.Length == 0) yield break;

            yield return new CodeIssue(
                CodeIssue.Severity.Warning,
                b.OperatorToken.Span,
                "Boolean operation can be simplified.",
                actions);
        }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:15,代码来源:ReducibleBooleanExpression.cs

示例13: GetIssues

        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
        {
            var model = document.GetSemanticModel();
            var ternaryNode = (ConditionalExpressionSyntax)node;
            var actions = GetSimplifications(ternaryNode, model, Assumptions.All, cancellationToken)
                          .Select(e => e.AsCodeAction(document))
                          .ToArray();
            if (actions.Length == 0) yield break;

            yield return new CodeIssue(
                CodeIssue.Severity.Warning,
                ternaryNode.QuestionToken.Span,
                "Conditional expression simplifies into boolean expression.",
                actions);
        }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:15,代码来源:ReducibleConditionalExpression.cs

示例14: GetIssues

        public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
        {
            CommonSyntaxTree syntaxTree = document.GetSyntaxTree();
            SemanticModel semanticModel = (SemanticModel) document.GetSemanticModel();

            AnalysisResult result = _engine.Analyze(syntaxTree, semanticModel);

            if (!result.Success)
            {
                foreach (Issue issue in result.Issues)
                {
                    yield return new CodeIssue(CodeIssueKind.Error, issue.SyntaxNode.Span, issue.Description);
                }
            }
        }
开发者ID:flashcurd,项目名称:ThreadSafetyAnnotations,代码行数:15,代码来源:CodeIssueProvider.cs

示例15: GetRefactoring

        public CodeRefactoring GetRefactoring(IDocument document, TextSpan textSpan, CancellationToken cancellationToken)
        {
            var assume = Assumptions.All;
            var model = document.GetSemanticModel();
            var tree = (SyntaxTree)document.GetSyntaxTree(cancellationToken);
            var token = tree.GetRoot().FindToken(textSpan.Start);

            var desc = token.Parent.TryGetCodeBlockOrAreaDescription();
            if (desc == null) return null;

            return new CodeRefactoring(new[] { new ReadyCodeAction(
                "Remove Unused Operations in " + desc,
                document,
                token.Parent,
                () => token.Parent.ReplaceNodes(token.Parent.DescendantNodes().OfType<IfStatementSyntax>(), (e,a) => a.DropEmptyBranchesIfApplicable(assume, model)))});
        }
开发者ID:Strilanc,项目名称:Croslyn,代码行数:16,代码来源:RemoveUnusedOperations.cs


注:本文中的IDocument.GetSemanticModel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。