public override void VisitMethodDeclaration(MethodDeclarationSyntax node) { if (!node.HasAsyncModifier()) { base.VisitMethodDeclaration(node); return; } if (node.ToString().Contains("await")) { Result.asyncAwaitResults.NumAsyncMethods++; Logs.TempLog.Info(@"{0}{1}**********************************************", Document.FilePath, node.ToLog()); if (!node.ReturnType.ToString().Equals("void")) { Result.asyncAwaitResults.NumAsyncTaskMethods++; } if (IsFireForget(node)) Result.StoreDetectedAsyncMisuse(1, Document, node); if (IsUnnecessaryAsyncAwait(node)) Result.StoreDetectedAsyncMisuse(2, Document, node); if (IsThereLongRunning(node)) Result.StoreDetectedAsyncMisuse(3, Document, node); if (IsUnnecessarilyCaptureContext(node, 0)) Result.StoreDetectedAsyncMisuse(4, Document, node); //var symbol = SemanticModel.GetDeclaredSymbol(node); //if (symbol != null) //{ // foreach (var refs in SymbolFinder.FindReferencesAsync(symbol, Document.Project.Solution).Result) // { // foreach (var loc in refs.Locations) // { // var caller = loc.Document.GetTextAsync().Result.Lines.ElementAt(loc.Location.GetLineSpan().StartLinePosition.Line).ToString(); // if (caller.Contains(".Result") || caller.Contains(".Wait")) // Logs.TempLog5.Info("{0}{1}{2}{3}************************************", // System.Environment.NewLine + "Blocking Caller: '" + caller.Trim() + "' from this file: " + loc.Document.FilePath + System.Environment.NewLine, // System.Environment.NewLine + "Async method Callee: " + System.Environment.NewLine, // node.ToLog(), // "From this file: " + Document.FilePath + System.Environment.NewLine + System.Environment.NewLine); // } // } //} } else Logs.AsyncMisuse.Info(@"{0} - {1}{2}**********************************************", Document.FilePath, "No Await", node.ToLog()); base.VisitMethodDeclaration(node); }
public override void VisitMethodDeclaration(MethodDeclarationSyntax node) { if (node.HasAsyncModifier() && node.ToString().Contains("await")) { if(Result.CurrentAnalyzedProjectType == Enums.ProjectType.WP7) Result.asyncUsageResults_WP7.NumAsyncAwaitMethods++; else Result.asyncUsageResults_WP8.NumAsyncAwaitMethods++; } base.VisitMethodDeclaration(node); }
private void ProcessMethodCallsInMethod(MethodDeclarationSyntax node, int n, string topAncestor) { var hashcode = node.Identifier.ToString() + node.ParameterList.ToString(); bool asyncFlag = false; if (node.HasAsyncModifier()) asyncFlag = true; if (!AnalyzedMethods.Contains(hashcode)) { AnalyzedMethods.Add(hashcode); var newMethods = new List<MethodDeclarationSyntax>(); try { var semanticModel = Document.Project.Solution.GetDocument(node.SyntaxTree).GetSemanticModelAsync().Result; foreach (var blocking in node.DescendantNodes().OfType<MemberAccessExpressionSyntax>().Where(a => BlockingMethodCalls.Any(b => b.Equals(a.Name.ToString())))) { Logs.TempLog2.Info("BLOCKING {0} {1} {2}\r\n{3} \r\n{4}\r\n{5}\r\n--------------------------", asyncFlag, n, blocking, Document.FilePath, topAncestor, node); } foreach (var blocking in node.DescendantNodes().OfType<MemberAccessExpressionSyntax>().Where(a => a.Name.ToString().Equals("Result"))) { var s = semanticModel.GetSymbolInfo(blocking).Symbol; if (s != null && s.ToString().Contains("System.Threading.Tasks")) Logs.TempLog2.Info("BLOCKING {0} {1} {2}\r\n{3} \r\n{4}\r\n{5}\r\n--------------------------", asyncFlag, n, blocking, Document.FilePath, topAncestor, node); } foreach (var methodCall in node.DescendantNodes().OfType<InvocationExpressionSyntax>()) { var methodCallSymbol = (IMethodSymbol)semanticModel.GetSymbolInfo(methodCall).Symbol; if (methodCallSymbol != null) { var replacement = ((IMethodSymbol)methodCallSymbol.OriginalDefinition).DetectSynchronousUsages(SemanticModel); if (replacement != "None") { if (!methodCallSymbol.Name.ToString().Equals("Invoke")) Logs.TempLog2.Info("LONGRUNNING {0} {1} {2} {3}\r\n{4} {5}\r\n{6}\r\n--------------------------", asyncFlag, n, methodCallSymbol, Document.FilePath, replacement, topAncestor, node); Logs.TempLog3.Info("{0} {1}", methodCallSymbol.ContainingType, methodCallSymbol, replacement); } var methodDeclarationNode = methodCallSymbol.FindMethodDeclarationNode(); if (methodDeclarationNode != null) newMethods.Add(methodDeclarationNode); } } foreach (var newMethod in newMethods) ProcessMethodCallsInMethod(newMethod, n + 1, topAncestor); } catch (Exception ex) { Logs.Console.Warn("Caught exception while processing method call node: {0} @ {1}", node, ex.Message); if (!( ex is FormatException || ex is ArgumentException || ex is PathTooLongException)) throw; } } }
public override void VisitMethodDeclaration(MethodDeclarationSyntax node) { //if (node.HasAsyncModifier() && !node.ToString().Contains("await")) //{ // Logs.TempLog5.Info("NotHavingAwait {0}\r\n{1}\r\n------------------------------", Document.FilePath, node); //} if (node.HasAsyncModifier() && node.ToString().Contains("await")) { if (Result.CurrentAnalyzedProjectType == Enums.ProjectType.WP7) Result.asyncAwaitResults.NumAsyncAwaitMethods_WP7++; else Result.asyncAwaitResults.NumAsyncAwaitMethods_WP8++; if (node.ReturnType.ToString().Equals("void")) { if (node.HasEventArgsParameter()) Result.asyncAwaitResults.NumAsyncVoidEventHandlerMethods++; else Result.asyncAwaitResults.NumAsyncVoidNonEventHandlerMethods++; } else Result.asyncAwaitResults.NumAsyncTaskMethods++; if (IsFireForget(node)) Result.WriteDetectedMisuseAsyncUsageToTable(1, Document, node); if (IsUnnecessaryAsyncAwait(node)) Result.WriteDetectedMisuseAsyncUsageToTable(2, Document, node); if(IsThereLongRunning(node)) Result.WriteDetectedMisuseAsyncUsageToTable(3, Document, node); if (IsUnnecessarilyCaptureContext(node, 0)) Result.WriteDetectedMisuseAsyncUsageToTable(4,Document,node); //Logs.TempLog.Info("ConfigureAwaitUse {0}\r\n{1}\r\n------------------------------",Document.FilePath,node); //var endTime = DateTime.UtcNow; //Logs.TempLog5.Info(endTime.Subtract(startTime).Milliseconds); //if (node.Body.ToString().Contains("ConfigureAwait")) //{ // Result.asyncAwaitResults.NumAsyncMethodsHavingConfigureAwait++; // Logs.TempLog5.Info("ConfigureAwait {0}\r\n{1}\r\n------------------------------", Document.FilePath, node); //} //foreach (var loop in node.DescendantNodes().Where(a => a is ForEachStatementSyntax || a is ForStatementSyntax || a is WhileStatementSyntax)) //{ // foreach (var tap in loop.DescendantNodes().OfType<InvocationExpressionSyntax>()) // { // MethodSymbol sym = (MethodSymbol)SemanticModel.GetSymbolInfo(tap).Symbol; // if (sym != null && sym.IsTAPMethod()) // Logs.TempLog2.Info("ExpensiveAwaits {0} {1}\r\n{2}\r\n-----------------------", sym, Document.FilePath, node); // } //} //var symbol = SemanticModel.GetDeclaredSymbol(node); //bool isThereAnyCaller = false; //if (symbol != null) //{ // foreach (var refs in SymbolFinder.FindReferencesAsync(symbol, Document.Project.Solution).Result) // { // foreach (var locs in refs.Locations) // { // isThereAnyCaller = true; // var caller = locs.Document.GetTextAsync().Result.Lines.ElementAt(locs.Location.GetLineSpan(false).StartLinePosition.Line).ToString(); // if (caller.Contains(".Result") || caller.Contains(".Wait")) // Logs.TempLog5.Info("BlockingCaller {0}\r\n{1}\r\n{2}\r\n-----------------------", Document.FilePath, node, caller); // } // } //} } //if (node.HasEventArgsParameter()) //{ // var arg = node.ParameterList.Parameters.Where(param => param.Type.ToString().EndsWith("EventArgs")).First(); // var type = SemanticModel.GetTypeInfo(arg.Type).Type; // if (type.ToString().StartsWith("System.Windows")) // { // ProcessMethodCallsInMethod(node, 0, node.Identifier.ToString() + node.ParameterList.ToString()); // } //} base.VisitMethodDeclaration(node); }