Skip to content

Commit

Permalink
Add signature to method data in SummaryAnalyzer. Pretty-print methods…
Browse files Browse the repository at this point in the history
… in console.
  • Loading branch information
allisterb committed Jul 25, 2023
1 parent cff61af commit 1aa0aee
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 99 deletions.
132 changes: 33 additions & 99 deletions src/Stratis.CodeAnalysis.Cs/Stratis.CodeAnalysis.Cs/SummaryAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static void Analyze(string cfgFile, Configuration config, SemanticModel m
var methodCreatedObjects = new List<Dictionary<string, object>>();//Objects created by methods

var builder = new StringBuilder();
var paramBuilder = new StringBuilder();
var classNames = new List<string>();
builder.AppendLine("classDiagram");
foreach (var c in classdecls)
Expand Down Expand Up @@ -68,78 +69,43 @@ public static void Analyze(string cfgFile, Configuration config, SemanticModel m
inheritsList.Add(inheritInfo);
}
}
/*
var methods = c.SyntaxTree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>();
foreach (var method in methods)
{
var symbol = model.GetDeclaredSymbol(method);
//Collect Method Information
var methoddata = new Dictionary<string, object>();
methoddata["name"] = symbol.MetadataName;
if (symbol.ContainingNamespace != null && !string.IsNullOrEmpty(symbol.ContainingNamespace.Name))
methoddata["name"] = symbol.ContainingNamespace.Name + "." + symbol.MetadataName;
methoddata["location"] = c.GetLocation().ToString();
methoddata["class"] = classinfo["name"];
implementsList.Add(methoddata);
var invocations = method.SyntaxTree.GetRoot().DescendantNodes().OfType<InvocationExpressionSyntax>();
//For each invocation within our method, collect information
foreach (var invocation in invocations)
{
var invokedSymbol = model.GetSymbolInfo(invocation).Symbol;
if (invokedSymbol == null)
continue;
var invocationInfo = new Dictionary<string, object>();
invocationInfo["name"] = invokedSymbol.MetadataName;
if (symbol.ContainingNamespace != null && !string.IsNullOrEmpty(symbol.ContainingNamespace.Name))
invocationInfo["name"] = invokedSymbol.ContainingNamespace.Name + "." + invokedSymbol.MetadataName;
if (invokedSymbol.Locations.Length == 1)
invocationInfo["location"] = invocation.GetLocation().ToString();
invocationInfo["method"] = methoddata["name"];
invocationList.Add(invocationInfo);
}
//For each object creation within our method, collect information
var methodCreates = method.SyntaxTree.GetRoot().DescendantNodes().OfType<ObjectCreationExpressionSyntax>();
foreach (var creation in methodCreates)
{
var typeInfo = model.GetTypeInfo(creation);
var createInfo = new Dictionary<string, object>();
var typeName = typeInfo.Type.Name;
if (typeInfo.Type.ContainingNamespace != null && !string.IsNullOrEmpty(typeInfo.Type.ContainingNamespace.Name))
typeName = typeInfo.Type.ContainingNamespace.Name + "." + typeInfo.Type.Name;
createInfo["method"] = methoddata["name"];
createInfo["creates"] = typeName;
createInfo["location"] = creation.GetLocation().ToString();
methodCreatedObjects.Add(createInfo);
}
}
*/

var t = model.GetDeclaredSymbol(c);
var className = t.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
builder.AppendLineFormat("class {0}", className);
classNames.Add(className);
Debug("{cls}", builder.Last());
if (t.IsSmartContract()) builder.AppendLineFormat("<<contract>> {0}".Replace("<", "&lt;").Replace(">", "&gt;"), className);

foreach (var method in t.GetMembers().Where(m => m.Kind == SymbolKind.Method && m.DeclaredAccessibility == Accessibility.Public).Cast<IMethodSymbol>())
foreach (var method in t.GetMembers().Where(m => m.Kind == SymbolKind.Method).Cast<IMethodSymbol>())
{
// Collect Method Information
var methoddata = new Dictionary<string, object>();
methoddata["name"] = method.MetadataName;
if (method.ContainingNamespace != null && !string.IsNullOrEmpty(method.ContainingNamespace.Name))
methoddata["name"] = method.ContainingNamespace.Name + "." + method.MetadataName;
methoddata["name"] = className + "::" + (string)methoddata["name"];

paramBuilder.Clear();
paramBuilder.Append("(");
foreach (var p in method.Parameters)
{
paramBuilder.AppendFormat("{0} {1}", p.Type.Name, p.Name);
paramBuilder.Append(",");
}
if (method.Parameters.Count() > 0)
{
paramBuilder.Remove(paramBuilder.Length - 1, 1);
}
paramBuilder.Append(")");
methoddata["signature"] = paramBuilder.ToString();

methoddata["location"] = c.GetLocation().ToString();
methoddata["class"] = classinfo["name"];


implementsList.Add(methoddata);

var invocations = method.DeclaringSyntaxReferences.First().GetSyntax().DescendantNodes().OfType<InvocationExpressionSyntax>();
//For each invocation within our method, collect information
foreach (var invocation in invocations)
Expand All @@ -150,9 +116,10 @@ public static void Analyze(string cfgFile, Configuration config, SemanticModel m
continue;

var invocationInfo = new Dictionary<string, object>();
invocationInfo["name"] = invokedSymbol.MetadataName;
if (method.ContainingNamespace != null && !string.IsNullOrEmpty(method.ContainingNamespace.Name))
invocationInfo["name"] = invokedSymbol.ContainingNamespace.Name + "." + invokedSymbol.MetadataName;
invocationInfo["name"] = invokedSymbol.ContainingSymbol.Name + "::" + invokedSymbol.MetadataName;

//if (method.ContainingNamespace != null && !string.IsNullOrEmpty(method.ContainingNamespace.Name))
// invocationInfo["name"] = invokedSymbol.ContainingNamespace.Name + "." + invokedSymbol.MetadataName;
if (invokedSymbol.Locations.Length == 1)
invocationInfo["location"] = invocation.GetLocation().ToString();
invocationInfo["method"] = methoddata["name"];
Expand All @@ -178,36 +145,14 @@ public static void Analyze(string cfgFile, Configuration config, SemanticModel m
methodCreatedObjects.Add(createInfo);
}


builder.AppendFormat("{0} : ", className);
builder.AppendFormat("+");
builder.AppendFormat(method.Name);
builder.Append("(");

foreach (var p in method.Parameters)
{
builder.AppendFormat("{0} {1}", p.Type.Name.Replace("<", "~").Replace(">", "~"), p.Name);
builder.Append(",");
}
if (method.Parameters.Count() > 0)
{
builder.Remove(builder.Length - 1, 1);
}
builder.Append(")");
builder.AppendFormat(Environment.NewLine);
Debug("{m}", builder.Last());
}

foreach (var method in t.GetMembers().Where(m => m.Kind == SymbolKind.Method && m.DeclaredAccessibility != Accessibility.Public).Cast<IMethodSymbol>())
{
builder.AppendFormat("{0} : ", className);
switch (method.DeclaredAccessibility)
{
case Accessibility.Public:
builder.AppendFormat("+");
builder.Append("+");
break;
case Accessibility.Private:
builder.AppendFormat("-");
builder.Append("-");
break;
case Accessibility.ProtectedAndInternal:
builder.AppendFormat("#");
Expand All @@ -216,24 +161,13 @@ public static void Analyze(string cfgFile, Configuration config, SemanticModel m
// builder.AppendFormat("~");
// break;
default:
builder.AppendFormat("-");
builder.Append("-");
break;
}


builder.AppendFormat(method.Name);
builder.Append("(");
foreach (var p in method.Parameters)
{
builder.AppendFormat("{0} {1}", p.Type.Name, p.Name);
builder.Append(",");
}
if (method.Parameters.Count() > 0)
{
builder.Remove(builder.Length - 1, 1);
}
builder.Append(")");
builder.AppendFormat(Environment.NewLine);
builder.Append(method.Name);
builder.Append(methoddata["signature"]);
builder.Append(Environment.NewLine);
Debug("{m}", builder.Last());
}

Expand Down
1 change: 1 addition & 0 deletions src/Stratis.DevEx.Gui.Service/Pipes/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public static string PrettyPrint(SummaryMessage m)
return $"{{{n}\tCompilation ID: {m.CompilationId}{n}\tEditor Entry Assembly: {m.EditorEntryAssembly}{n}" +
$"\tAssemblyName: {m.AssemblyName}{n}\tDocument: {m.Document}{n}\tSummary: {m.Summary}{n}\tClasses: {m.ClassNames.JoinWithSpaces()}{n}" +
$"\tInherits: {"{" + m.Inherits.Select(d => (string)d["class"] + "<:" + (string)d["base"]).JoinWith(", ") + "}"}{n}" +
$"\tMethods: {"{" + m.Implements.Select(d => (string)d["name"]+ (string)d["signature"]).JoinWith(", ") + "}"}{n}" +
$"\tInvocations: {"{" + m.Invocations.Select(d =>(string) d["name"] + " in " + (string)d["method"]).JoinWith(", ") + "}"}{n}" +
$"\tClass Created Objects: {"{" + m.ClassCreatedObjects.Select(d => (string)d["class"] + " creates " + (string)d["creates"]).JoinWith(", ") + "}"}{n}" +
$"\tMethod Created Objects: {"{" + m.MethodCreatedObjects.Select(d => (string)d["method"] + " creates " + (string)d["creates"]).JoinWith(", ") + "}"}{n}" +
Expand Down

0 comments on commit 1aa0aee

Please sign in to comment.