本文整理汇总了C#中InvocationExpressionSyntax.Ancestors方法的典型用法代码示例。如果您正苦于以下问题:C# InvocationExpressionSyntax.Ancestors方法的具体用法?C# InvocationExpressionSyntax.Ancestors怎么用?C# InvocationExpressionSyntax.Ancestors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类InvocationExpressionSyntax
的用法示例。
在下文中一共展示了InvocationExpressionSyntax.Ancestors方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitInvocationExpression
public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)
{
// Replace the module method name with a qualified one if this is a module ctor
bool nodeChanged = IsInvocationAModuleCtor(node);
ExpressionSyntax nodeExpression = nodeChanged
? ((IdentifierNameSyntax)node.Expression).WithIdentifier(
SyntaxFactory.Identifier("ConfigScript." + ((IdentifierNameSyntax)node.Expression).Identifier.Text)
.WithTriviaFrom(((IdentifierNameSyntax)node.Expression).Identifier))
: node.Expression;
// Get a hash of all the previous @doc and @ctx lambda parameters and don't replace the same
HashSet<string> currentScopeLambdaParameters = new HashSet<string>(
node.Ancestors().OfType<LambdaExpressionSyntax>().SelectMany(
x => x.DescendantNodes().OfType<ParameterSyntax>()).Select(x => x.Identifier.Text));
// Only do the replacement if this is a module ctor or a module fluent method
ArgumentListSyntax argumentList = node.ArgumentList;
if(nodeChanged || IsInvocationAFluentMethod(node))
{
// Replace @doc and @ctx argument expressions with the appropriate lambda expressions, and stop descending if we hit another module ctor
foreach (ArgumentSyntax argument in node.ArgumentList.Arguments)
{
// Don't replace existing lambda expressions
if (!(argument.Expression is LambdaExpressionSyntax))
{
List<IdentifierNameSyntax> identifierNames = argument
.DescendantNodes(x => !(x is InvocationExpressionSyntax) || !IsInvocationAModuleCtorOrFluentMethod((InvocationExpressionSyntax)x))
.OfType<IdentifierNameSyntax>()
.Where(x => x != null && !currentScopeLambdaParameters.Contains(x.Identifier.Text))
.ToList();
IdentifierNameSyntax docReplacementName = identifierNames.FirstOrDefault(x => x.Identifier.Text.StartsWith("@doc"));
IdentifierNameSyntax ctxReplacementName = identifierNames.FirstOrDefault(x => x.Identifier.Text.StartsWith("@ctx"));
if (docReplacementName != null)
{
argumentList = argumentList.ReplaceNode(argument, SyntaxFactory.Argument(
SyntaxFactory.ParenthesizedLambdaExpression(
SyntaxFactory.ParameterList(
new SeparatedSyntaxList<ParameterSyntax>()
.Add(SyntaxFactory.Parameter(SyntaxFactory.Identifier(docReplacementName.Identifier.Text)))
.Add(SyntaxFactory.Parameter(SyntaxFactory.Identifier(ctxReplacementName == null ? "_" : ctxReplacementName.Identifier.Text)))),
argument.Expression))
.WithTriviaFrom(argument));
nodeChanged = true;
}
else if (ctxReplacementName != null)
{
argumentList = argumentList.ReplaceNode(argument, SyntaxFactory.Argument(
SyntaxFactory.SimpleLambdaExpression(
SyntaxFactory.Parameter(SyntaxFactory.Identifier(ctxReplacementName.Identifier.Text)),
argument.Expression))
.WithTriviaFrom(argument));
nodeChanged = true;
}
}
}
}
// Build and return the result node (or just return the original node)
return base.VisitInvocationExpression(nodeChanged ? node.WithExpression(nodeExpression).WithArgumentList(argumentList) : node);
}
示例2: VisitInvocationExpression
public override void VisitInvocationExpression(InvocationExpressionSyntax node)
{
var symbol = (IMethodSymbol)SemanticModel.GetSymbolInfo(node).Symbol;
if (symbol != null)
{
if(symbol.ToString().Contains("System.Threading.Tasks.Task.WaitAll"))
{
var block = node.Ancestors().OfType<BlockSyntax>().First();
if (block.DescendantNodes().OfType<ForEachStatementSyntax>().Any() || block.DescendantNodes().OfType<ForStatementSyntax>().Any())
return;
foreach (var invocation in block.DescendantNodes().OfType<InvocationExpressionSyntax>())
{
var symbol2 = (IMethodSymbol)SemanticModel.GetSymbolInfo(invocation).Symbol;
if (symbol2!=null && symbol2.IsTaskCreationMethod())
{
Logs.TempLog3.Info("{0}{1}--------------------------", Document.FilePath, block.ToLog());
break;
}
}
}
}
base.VisitInvocationExpression(node);
}
示例3: VisitInvocationExpression
public override void VisitInvocationExpression(InvocationExpressionSyntax node)
{
var symbol = (IMethodSymbol)SemanticModel.GetSymbolInfo(node).Symbol;
if (symbol != null)
{
var synctype = DetectSynchronousUsages((IMethodSymbol)symbol.OriginalDefinition);
Result.StoreDetectedSyncUsage(synctype);
Result.WriteDetectedSyncUsage(synctype, Document.FilePath, (IMethodSymbol)symbol.OriginalDefinition);
if (synctype != Utilities.Enums.SyncDetected.None
&& node.Ancestors().OfType<MethodDeclarationSyntax>().Any(method => method.HasAsyncModifier()))
{
Result.syncUsageResults.NumGUIBlockingSyncUsages++;
Logs.TempLog.Info(@"GUIBLOCKING {0}", node.Ancestors().OfType<MethodDeclarationSyntax>().First().ToString());
}
}
}
示例4: ValidateInvocation
static bool ValidateInvocation(InvocationExpressionSyntax expr)
{
bool allAncestorsIsParenthes = true;
foreach (var x in expr.Ancestors())
{
// scope is in lambda, method
if (x.IsKind(SyntaxKind.SimpleLambdaExpression) || x.IsKind(SyntaxKind.ParenthesizedLambdaExpression) || x.IsKind(SyntaxKind.ArrowExpressionClause))
{
// () => M()
if (allAncestorsIsParenthes) return true;
break;
}
if (x.IsKind(SyntaxKind.MethodDeclaration)) break;
if (x.IsKind(SyntaxKind.PropertyDeclaration)) break;
if (x.IsKind(SyntaxKind.ConstructorDeclaration)) break;
// x = M()
if (x.IsKind(SyntaxKind.SimpleAssignmentExpression)) return true;
// var x = M()
if (x.IsKind(SyntaxKind.VariableDeclarator)) return true;
// return M()
if (x.IsKind(SyntaxKind.ReturnStatement)) return true;
// from x in M()
if (x.IsKind(SyntaxKind.FromClause)) return true;
// (bool) ? M() : M()
if (x.IsKind(SyntaxKind.ConditionalExpression)) return true;
// M(M())
if (x.IsKind(SyntaxKind.InvocationExpression)) return true;
// new C(M())
if (x.IsKind(SyntaxKind.ObjectCreationExpression)) return true;
// (((((M()))))
if (!x.IsKind(SyntaxKind.ParenthesizedExpression))
{
allAncestorsIsParenthes = false;
}
}
// Okay => M().M()
if (expr.DescendantNodes().OfType<InvocationExpressionSyntax>().Any()) return true;
return false;
}
示例5: IsInsideANullCheck
private static bool IsInsideANullCheck(InvocationExpressionSyntax invocation, SemanticModel semanticModel, ISymbol symbol) =>
invocation.Ancestors().OfType<IfStatementSyntax>().Any(@if => IsConditionThatChecksForNotEqualsNull(@if.Condition, semanticModel, symbol));
示例6: CorrectArgumentsAsync
// puts the correct arguments in the register statement
private async Task<Document> CorrectArgumentsAsync(Document document, InvocationExpressionSyntax declaration, CancellationToken c)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
string methodName = CodeFixHelper.GetRegisterMethodName(declaration);
ClassDeclarationSyntax classDeclaration = declaration.AncestorsAndSelf().OfType<ClassDeclarationSyntax>().First();
methodName = CodeFixHelper.GetExistingAnalysisMethodName(classDeclaration);
if (methodName == null)
{
methodName = "AnalyzeIfStatement";
}
SyntaxNode statement = CodeFixHelper.CreateRegister(generator, declaration.Ancestors().OfType<MethodDeclarationSyntax>().First(), methodName);
SyntaxNode expression = generator.ExpressionStatement(statement);
return await ReplaceNode(declaration.Parent, expression.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ParseLeadingTrivia("// Calls the method (first argument) to perform analysis whenever a SyntaxNode of kind IfStatement is found").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document);
}
示例7: IsInsideANullCheck
private static bool IsInsideANullCheck(InvocationExpressionSyntax invocation, SemanticModel semanticModel, ISymbol symbol)
{
var ifs = invocation.Ancestors().OfType<IfStatementSyntax>();
foreach (IfStatementSyntax @if in ifs)
{
if ([email protected]?.IsKind(SyntaxKind.NotEqualsExpression) ?? true) continue;
var equals = (BinaryExpressionSyntax)@if.Condition;
if (equals.Left == null || equals.Right == null) continue;
ISymbol identifierSymbol;
if (equals.Right.IsKind(SyntaxKind.NullLiteralExpression) && equals.Left.IsKind(SyntaxKind.IdentifierName))
identifierSymbol = semanticModel.GetSymbolInfo(equals.Left).Symbol;
else if (equals.Left.IsKind(SyntaxKind.NullLiteralExpression) && equals.Right.IsKind(SyntaxKind.IdentifierName))
identifierSymbol = semanticModel.GetSymbolInfo(equals.Right).Symbol;
else continue;
if (symbol.Equals(identifierSymbol)) return true;
}
return false;
}
示例8: APMDiagnosisDetection
private void APMDiagnosisDetection(IMethodSymbol symbol, InvocationExpressionSyntax node)
{
int c = GetIndexCallbackArgument(symbol);
var callbackArg = node.ArgumentList.Arguments.ElementAt(c);
if (callbackArg.Expression.CSharpKind().ToString().Contains("IdentifierName"))
Logs.TempLog.Info("{0} {1}", callbackArg.Expression.CSharpKind(), SemanticModel.GetSymbolInfo(callbackArg.Expression).Symbol.Kind);
else
Logs.TempLog.Info("{0}", callbackArg.Expression.CSharpKind());
//PRINT ALL APM BEGIN METHODS
Logs.APMDiagnosisLog.Info(@"Document: {0}", Document.FilePath);
Logs.APMDiagnosisLog.Info(@"Symbol: {0}", symbol);
Logs.APMDiagnosisLog.Info(@"Invocation: {0}", node);
Logs.APMDiagnosisLog.Info(@"CallbackType: {0}", callbackArg.Expression.CSharpKind());
if (callbackArg.Expression.CSharpKind().ToString().Contains("IdentifierName"))
Logs.APMDiagnosisLog.Info("{0}", SemanticModel.GetSymbolInfo(callbackArg.Expression).Symbol.Kind);
Logs.APMDiagnosisLog.Info("---------------------------------------------------");
Result.apmDiagnosisResults.NumAPMBeginMethods++;
if (node.Ancestors().Where(a => (a is BinaryExpressionSyntax) || (a is VariableDeclaratorSyntax) || (a is VariableDeclarationSyntax) || (a is ReturnStatementSyntax)).Any() )
{
Logs.TempLog2.Info("UnderStatement {0}\r\n{1}\r\n--------------------", Document.FilePath, node.Ancestors().OfType<BlockSyntax>().First());
}
//var statement = node.Ancestors().OfType<StatementSyntax>().First();
//var ancestors = node.Ancestors().OfType<MethodDeclarationSyntax>();
//if (ancestors.Any())
//{
// var method = ancestors.First();
// bool isFound = false;
// bool isAPMFollowed = false;
// foreach (var tmp in method.Body.ChildNodes())
// {
// if (isFound)
// isAPMFollowed = true;
// if (statement == tmp)
// isFound = true;
// }
// if (isAPMFollowed)
// {
// Result.apmDiagnosisResults.NumAPMBeginFollowed++;
// Logs.TempLog2.Info("APMFOLLOWED:\r\n{0}\r\n---------------------------------------------------", method);
// }
//}
//SyntaxNode callbackBody = null;
//if (callbackArg.Expression.Kind.ToString().Contains("IdentifierName"))
//{
// var methodSymbol = SemanticModel.GetSymbolInfo(callbackArg.Expression).Symbol;
// if (methodSymbol.Kind.ToString().Equals("Method"))
// callbackBody = (MethodDeclarationSyntax)methodSymbol.DeclaringSyntaxReferences.First().GetSyntax();
//}
//else if (callbackArg.Expression.Kind.ToString().Contains("LambdaExpression"))
// callbackBody = node;
//if (callbackBody != null)
//{
// if (callbackBody.DescendantNodes().OfType<MemberAccessExpressionSyntax>().Any(a => a.Name.ToString().StartsWith("End")))
// Logs.TempLog3.Info("APMEND in Callback:\r\n{0}\r\nCaller: {1}\r\nDeclaringSyntaxNodes:\r\n{2}\r\n--------------------------------------------------", Document.FilePath, node, callbackBody);
// else
// Logs.TempLog3.Info("NO APMEND in Callback:\r\n{0}\r\nCaller: {1}\r\nDeclaringSyntaxNodes:\r\n{2}\r\n--------------------------------------------------", Document.FilePath, node, callbackBody);
//}
}
示例9: VisitInvocationExpression
public override void VisitInvocationExpression(InvocationExpressionSyntax node)
{
var symbol = (IMethodSymbol)SemanticModel.GetSymbolInfo(node).Symbol;
if (symbol != null)
{
if (symbol.IsAPMBeginMethod())
APMDiagnosisDetection(symbol, node);
if (symbol.IsAPMEndMethod())
{
Result.apmDiagnosisResults.NumAPMEndMethods++;
var tmp = node.ArgumentList.Arguments.First().DescendantNodes().OfType<IdentifierNameSyntax>();
if(tmp.Any())
{
var id = tmp.First();
if(node.Ancestors().OfType<BlockSyntax>().First().DescendantNodes().OfType<IdentifierNameSyntax>().Where(a=> (a.Identifier.ToString()== id.Identifier.ToString())).Count()>2)
Logs.TempLog3.Info("IAsyncResultSomewhereElse {0}\r\n{1}\r\n--------------------", Document.FilePath, node.Ancestors().OfType<BlockSyntax>().First());
}
//var ancestors2 = node.Ancestors().OfType<TryStatementSyntax>();
//if (ancestors2.Any())
//{
// Logs.TempLog4.Info("TRYCATCHED ENDXXX:\r\n{0}\r\n---------------------------------------------------", ancestors2.First());
// Result.apmDiagnosisResults.NumAPMEndTryCatchedMethods++;
//}
//SyntaxNode block = null;
//var lambdas = node.Ancestors().OfType<SimpleLambdaExpressionSyntax>();
//if (lambdas.Any())
//{
// block = lambdas.First();
//}
//if (block == null)
//{
// var lambdas2 = node.Ancestors().OfType<ParenthesizedLambdaExpressionSyntax>();
// if (lambdas2.Any())
// block = lambdas2.First();
//}
//if (block == null)
//{
// var ancestors3 = node.Ancestors().OfType<MethodDeclarationSyntax>();
// if (ancestors3.Any())
// block = ancestors3.First();
//}
//if (block != null)
//{
// if (block.DescendantNodes().OfType<MemberAccessExpressionSyntax>().Any(a => a.Name.ToString().StartsWith("Begin") && !a.Name.ToString().Equals("BeginInvoke")))
// {
// Logs.TempLog5.Info("NESTED ENDXXX:\r\n{0}\r\n---------------------------------------------------", block);
// Result.apmDiagnosisResults.NumAPMEndNestedMethods++;
// }
//}
}
}
base.VisitInvocationExpression(node);
}