Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plugins/csharp/model/CsharpEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ enum EtcEntityTypeEnum
{
Event,
Invocation,
ForeachExpr
ForeachExpr,
Inheritance
}
class CsharpEtcEntity : CsharpTypedEntity
{
Expand Down
150 changes: 136 additions & 14 deletions plugins/csharp/parser/src_csharp/AstVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,35 @@ private ulong fnvHash(string data_)
}

return hash;
}
}

// Deterministic replacement for GetHashCode(), which is reference based
// for syntax nodes and randomized per process for strings. Both would
// give different ids for the same entity on every parse.
private long stableHash(string data_)
{
return unchecked((long)fnvHash(data_));
}

private long getEntityHash(SyntaxNode node)
{
string[] properties =
{
node.SyntaxTree.FilePath,":",
node.Kind().ToString(),":",
node.Span.Start.ToString(),":",
node.Span.End.ToString()
};

return stableHash(string.Concat(properties));
}

private ulong getAstNodeId(SyntaxNode node){
CsharpAstNode astNode = new CsharpAstNode
{
AstValue = node.ToString(),
RawKind = node.Kind(),
EntityHash = node.GetHashCode(),
EntityHash = getEntityHash(node),
AstType = AstTypeEnum.Declaration
};
astNode.SetLocation(node.SyntaxTree.GetLineSpan(node.Span));
Expand All @@ -86,7 +107,7 @@ private CsharpAstNode AstNode(SyntaxNode node, AstSymbolTypeEnum type, AstTypeEn
{
AstValue = node.ToString(),
RawKind = node.Kind(),
EntityHash = node.GetHashCode(),
EntityHash = getEntityHash(node),
AstSymbolType = type,
AstType = astType,
Accessibility = acc
Expand Down Expand Up @@ -175,7 +196,9 @@ public override void VisitInterfaceDeclaration(InterfaceDeclarationSyntax node)
QualifiedName = qName,
DocumentationCommentXML = Model.GetDeclaredSymbol(node).GetDocumentationCommentXml(),
EntityHash = astNode.EntityHash
};
};

VisitBaseList(node.BaseList, astNode);

foreach (PropertyDeclarationSyntax propertyDeclaration in node.Members.OfType<PropertyDeclarationSyntax>())
{
Expand Down Expand Up @@ -226,9 +249,11 @@ public override void VisitStructDeclaration(StructDeclarationSyntax node)
EntityHash = astNode.EntityHash
};

foreach (VariableDeclarationSyntax variableDeclaration in node.Members.OfType<VariableDeclarationSyntax>())
VisitBaseList(node.BaseList, astNode);

foreach (var variableDeclaration in node.Members.OfType<FieldDeclarationSyntax>())
{
VisitVariableDecl(variableDeclaration, astNode);
VisitVariableDecl(variableDeclaration.Declaration, astNode);
}

foreach (PropertyDeclarationSyntax propertyDeclaration in node.Members.OfType<PropertyDeclarationSyntax>())
Expand Down Expand Up @@ -321,6 +346,8 @@ public override void VisitClassDeclaration(ClassDeclarationSyntax node)
EntityHash = astNode.EntityHash
};

VisitBaseList(node.BaseList, astNode);

foreach (var variableDeclaration in node.Members.OfType<FieldDeclarationSyntax>())
{
VisitVariableDecl(variableDeclaration.Declaration, astNode);
Expand Down Expand Up @@ -416,6 +443,8 @@ public override void VisitRecordDeclaration(RecordDeclarationSyntax node) {
EntityHash = astNode.EntityHash
};

VisitBaseList(node.BaseList, astNode);

foreach (var variableDeclaration in node.Members.OfType<FieldDeclarationSyntax>())
{
VisitVariableDecl(variableDeclaration.Declaration, astNode);
Expand Down Expand Up @@ -480,6 +509,41 @@ public override void VisitRecordDeclaration(RecordDeclarationSyntax node) {
DbContext.CsharpClasses.Add(csharpRecord);
}

private void VisitBaseList(BaseListSyntax baseList, CsharpAstNode parent)
{
if (baseList == null)
return;

foreach (BaseTypeSyntax baseType in baseList.Types)
{
var symbol = Model.GetTypeInfo(baseType.Type).Type as INamedTypeSymbol;
if (symbol == null)
continue;

symbol = symbol.OriginalDefinition;

var declaration = symbol.DeclaringSyntaxReferences
.Select(r => r.GetSyntax())
.FirstOrDefault(s => s is BaseTypeDeclarationSyntax);
Comment on lines +525 to +527
if (declaration == null)
continue;

var astNode = AstNode(baseType, AstSymbolTypeEnum.EtcEntity, AstTypeEnum.Usage);
CsharpEtcEntity inheritance = new CsharpEtcEntity
{
AstNode = astNode,
EntityHash = astNode.EntityHash,
ParentNode = parent,
EtcEntityType = EtcEntityTypeEnum.Inheritance,
DeclaratorNodeId = getAstNodeId(declaration),
Name = symbol.Name,
QualifiedName = symbol.ToString(),
QualifiedType = symbol.TypeKind.ToString()
};
DbContext.CsharpEtcEntitys.Add(inheritance);
}
}

private void VisitDelegateDecl(DelegateDeclarationSyntax node, CsharpAstNode parent)
{
CsharpAstNode astNode = AstNode(node,AstSymbolTypeEnum.Method);
Expand Down Expand Up @@ -616,7 +680,7 @@ private void VisitMethodDecl(MethodDeclarationSyntax node, CsharpAstNode parent)
QualifiedName = qName,
QualifiedType = qType,
DocumentationCommentXML = Model.GetDeclaredSymbol(node).GetDocumentationCommentXml(),
TypeHash = qType.GetHashCode(),
TypeHash = stableHash(qType),
EntityHash = astNode.EntityHash,
ParentNode = parent,
MethodType = MethodTypeEnum.Method
Expand Down Expand Up @@ -666,7 +730,7 @@ private void VisitOperatorDecl(OperatorDeclarationSyntax node, CsharpAstNode par
QualifiedName = qName,
QualifiedType = qType,
DocumentationCommentXML = Model.GetDeclaredSymbol(node).GetDocumentationCommentXml(),
TypeHash = qType.GetHashCode(),
TypeHash = stableHash(qType),
EntityHash = astNode.EntityHash,
ParentNode = parent,
MethodType = MethodTypeEnum.Operator
Expand Down Expand Up @@ -704,7 +768,7 @@ private void VisitMethodParameters(SeparatedSyntaxList<ParameterSyntax> paramete
AstNode = astNode,
Name = param.Identifier.Text,
QualifiedType = paramQType,
TypeHash = paramQType.GetHashCode(),
TypeHash = stableHash(paramQType),
EntityHash = astNode.EntityHash,
ParentNode = parent,
VariableType = VariableTypeEnum.Parameter
Expand Down Expand Up @@ -752,7 +816,7 @@ private void VisitVariableDecl(VariableDeclarationSyntax node, CsharpAstNode par
AstNode = astNode,
Name = variable.Identifier.Text,
QualifiedType = varQType,
TypeHash = varQType.GetHashCode(),
TypeHash = stableHash(varQType),
DocumentationCommentXML = Model.GetDeclaredSymbol(variable).GetDocumentationCommentXml(),
EntityHash = astNode.EntityHash,
VariableType = isLINQvar ? VariableTypeEnum.LINQ : VariableTypeEnum.Variable,
Expand All @@ -779,7 +843,7 @@ private void VisitPropertyDecl(PropertyDeclarationSyntax node, CsharpAstNode par
AstNode = astNode,
Name = node.Identifier.Text,
QualifiedType = varQType,
TypeHash = varQType.GetHashCode(),
TypeHash = stableHash(varQType),
VariableType = VariableTypeEnum.Property,
DocumentationCommentXML = Model.GetDeclaredSymbol(node).GetDocumentationCommentXml(),
EntityHash = astNode.EntityHash,
Expand Down Expand Up @@ -951,11 +1015,69 @@ public override void VisitInvocationExpression(InvocationExpressionSyntax node)
DbContext.CsharpEtcEntitys.Add(invoc);
}
}
}
}
}
}

VisitMethodInvocation(node);

base.VisitInvocationExpression(node);
}
}

// Records an invocation whose DeclaratorNodeId points to the declaration
// of the called method. This is what the Callee / Caller references and
// the function call diagram rely on.
private void VisitMethodInvocation(InvocationExpressionSyntax node)
{
SimpleNameSyntax name = node.Expression switch
{
MemberAccessExpressionSyntax memberAccess => memberAccess.Name,
MemberBindingExpressionSyntax memberBinding => memberBinding.Name,
SimpleNameSyntax simpleName => simpleName,
_ => null
};

// Unqualified Foo() calls are already recorded by VisitIdentifierName.
if (name == null || (name == node.Expression && name is IdentifierNameSyntax))
return;

var method = Model.GetSymbolInfo(node).Symbol as IMethodSymbol;
if (method == null)
return;

method = (method.ReducedFrom ?? method).OriginalDefinition;
method = method.PartialImplementationPart ?? method;

var declaration = method.DeclaringSyntaxReferences
.Select(r => r.GetSyntax())
.FirstOrDefault(s => s is BaseMethodDeclarationSyntax);
if (declaration == null)
return;

var type = "";
try
{
var info = Model.GetTypeInfo(node).ConvertedType;
if (info != null) type = info.Name;
}
catch (Exception)
{
FullyParsed = false;
}

var declaratorNodeId = getAstNodeId(declaration);
var astNode = AstNode(name, AstSymbolTypeEnum.EtcEntity, AstTypeEnum.Usage);
CsharpEtcEntity invoc = new CsharpEtcEntity
{
AstNode = astNode,
EntityHash = astNode.EntityHash,
EtcEntityType = EtcEntityTypeEnum.Invocation,
DeclaratorNodeId = declaratorNodeId,
Name = name.Identifier.Text,
QualifiedName = method.ToString(),
QualifiedType = type
};
DbContext.CsharpEtcEntitys.Add(invoc);
}

public override void VisitIdentifierName(IdentifierNameSyntax node)
{
Expand Down
19 changes: 18 additions & 1 deletion plugins/csharp/parser/src_csharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ static async Task<int> Main(string[] args)
}
}

private static void RecreateCsharpTables(CsharpDbContext context)
{
var tables = context.Model.GetEntityTypes()
.Select(e => e.GetTableName())
.Where(t => t != null)
.Distinct()
.Append("__EFMigrationsHistory");

foreach (var table in tables)
{
string sql = "DROP TABLE IF EXISTS \"" + table + "\" CASCADE";
context.Database.ExecuteSqlRaw(sql);
}

context.Database.Migrate();
}

private static async Task<int> RunParserAsync(string primaryInput, int threadNum)
{
// Converting the connectionstring into entity framework style connectionstring
Expand All @@ -82,7 +99,7 @@ private static async Task<int> RunParserAsync(string primaryInput, int threadNum
.Options;

await using var _context = new CsharpDbContext(options);
_context.Database.Migrate();
RecreateCsharpTables(_context);

if (IsSolutionInput(primaryInput))
{
Expand Down
1 change: 1 addition & 0 deletions plugins/csharp/service/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ add_subdirectory(src_csharp)

add_library(csharpservice SHARED
src/csharpservice.cpp
src/csharpdiagram.cpp
src/plugin.cpp)

target_compile_options(csharpservice PUBLIC -Wno-unknown-pragmas)
Expand Down
3 changes: 0 additions & 3 deletions plugins/csharp/service/csharpservice.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ service CsharpService
map<string, i32> getDiagramTypes(1:common.AstNodeId astNodeId)
throws (1:common.InvalidId ex)

string getDiagram(1:common.AstNodeId astNodeId, 2:i32 diagramId)
throws (1:common.InvalidId exId, 2:common.Timeout exLong)

list<language.SyntaxHighlight> getSyntaxHighlight(
1:common.FileRange range,
2:list<string> content)
Expand Down
32 changes: 23 additions & 9 deletions plugins/csharp/service/include/service/csharpservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,6 @@ class CSharpQueryHandler : public CsharpServiceIf
_service -> getDiagramTypes(return_, astNodeId_);
}

void getDiagram(
std::string& return_,
const core::AstNodeId& astNodeId_,
const std::int32_t diagramId_) override
{
_service -> getDiagram(return_, astNodeId_, diagramId_);
}

void getSyntaxHighlight(
std::vector<language::SyntaxHighlight>& return_,
const core::FileRange& range_,
Expand Down Expand Up @@ -219,7 +211,7 @@ using TransportException = apache::thrift::transport::TTransportException;

class CsharpServiceHandler : virtual public LanguageServiceIf
{
friend class Diagram;
friend class CsharpDiagram;

public:
CsharpServiceHandler(
Expand Down Expand Up @@ -323,6 +315,28 @@ class CsharpServiceHandler : virtual public LanguageServiceIf
const core::FileRange& range_) override;

private:
// Must be kept in sync with CSharpServiceEnums.cs.
enum DiagramType
{
FUNCTION_CALL,
DETAILED_CLASS
};

enum ReferenceType
{
DEFINITION = 0,
CALLEE = 5,
CALLER = 6,
LOCAL_VAR = 10,
INHERIT_FROM = 19,
INHERIT_BY = 20,
DATA_MEMBER = 21,
METHOD = 22
};


core::AstNodeId getDefinitionId(const core::AstNodeId& astNodeId_);

std::shared_ptr<odb::database> _db;
util::OdbTransaction _transaction;

Expand Down
Loading
Loading