本文整理汇总了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));
}
示例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);
}
}
示例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) });
}
}
}
示例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;
}
示例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();
}
示例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");
}
示例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) }));
}
示例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) }));
}
示例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) });
}
}
示例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);
}
示例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);
}
}
示例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);
}
示例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);
}
示例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);
}
}
}
示例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)))});
}