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: 0 additions & 3 deletions plugins/csharp/parser/src/csharpparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ bool CsharpParser::parse()

if (acceptProjectBuildPath(buildPath))
{
LOG(debug) << "C# parser parse path: " << paths[0];
LOG(debug) << "Parsed csharp project build path: " << buildPath;
success = success && parseProjectBuildPath(paths, buildPath);
}
else
Expand Down Expand Up @@ -108,7 +106,6 @@ bool CsharpParser::parseProjectBuildPath(

std::string line;
std::stringstream log_str(log.get());
//LOG(warning) << log_str.str();
int countFull = 0, countPart = 0;

while(std::getline(log_str, line, '\n'))
Expand Down
55 changes: 4 additions & 51 deletions plugins/csharp/parser/src_csharp/AstVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,56 +21,9 @@ public AstVisitor(CsharpDbContext context, SemanticModel model, SyntaxTree tree)
{
this.DbContext = context;
this.Model = model;
this.Tree = tree;
}

private ulong createIdentifier(CsharpAstNode astNode){
string[] properties =
{
astNode.AstValue,":",
astNode.AstType.ToString(),":",
astNode.EntityHash.ToString(),":",
astNode.RawKind.ToString(),":",
astNode.Path,":",
astNode.Location_range_start_line.ToString(),":",
astNode.Location_range_start_column.ToString(),":",
astNode.Location_range_end_line.ToString(),":",
astNode.Location_range_end_column.ToString()
};

string res = string.Concat(properties);

//WriteLine(res);
return fnvHash(res);
this.Tree = tree;
}

private ulong fnvHash(string data_)
{
ulong hash = 14695981039346656037;

int len = data_.Length;
for (int i = 0; i < len; ++i)
{
hash ^= data_[i];
hash *= 1099511628211;
}

return hash;
}

private ulong getAstNodeId(SyntaxNode node){
CsharpAstNode astNode = new CsharpAstNode
{
AstValue = node.ToString(),
RawKind = node.Kind(),
EntityHash = node.GetHashCode(),
AstType = AstTypeEnum.Declaration
};
astNode.SetLocation(node.SyntaxTree.GetLineSpan(node.Span));
var ret = createIdentifier(astNode);
return ret;
}

private CsharpAstNode AstNode(SyntaxNode node, AstSymbolTypeEnum type, AstTypeEnum astType)
{
Accessibility acc = Accessibility.NotApplicable;
Expand All @@ -92,7 +45,7 @@ private CsharpAstNode AstNode(SyntaxNode node, AstSymbolTypeEnum type, AstTypeEn
Accessibility = acc
};
astNode.SetLocation(Tree.GetLineSpan(node.Span));
astNode.Id = createIdentifier(astNode);
astNode.Id = AstVisitorUtility.CreateIdentifier(astNode);

if (DbContext.CsharpAstNodes.Find(astNode.Id) == null)
{
Expand Down Expand Up @@ -933,7 +886,7 @@ public override void VisitInvocationExpression(InvocationExpressionSyntax node)
FullyParsed = false;
}
var info = Model.GetTypeInfo(node).ConvertedType;
var declaratorNodeId = getAstNodeId(declaration.GetSyntax());
var declaratorNodeId = AstVisitorUtility.GetAstNodeId(declaration.GetSyntax());
var astNode = AstNode(node, AstSymbolTypeEnum.EtcEntity, AstTypeEnum.Usage);
CsharpEtcEntity invoc = new CsharpEtcEntity
{
Expand Down Expand Up @@ -981,7 +934,7 @@ public override void VisitIdentifierName(IdentifierNameSyntax node)
EtcEntityTypeEnum.ForeachExpr : EtcEntityTypeEnum.Invocation;
if (node.Parent.Parent.Kind() != SyntaxKind.InvocationExpression)
{
var declaratorNodeId = getAstNodeId(declaration.GetSyntax());
var declaratorNodeId = AstVisitorUtility.GetAstNodeId(declaration.GetSyntax());
var astNode = AstNode(node, AstSymbolTypeEnum.EtcEntity, AstTypeEnum.Usage);
CsharpEtcEntity expr = new CsharpEtcEntity
{
Expand Down
61 changes: 61 additions & 0 deletions plugins/csharp/parser/src_csharp/AstVisitorUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using CSharpParser.model;
using Microsoft.CodeAnalysis;

namespace CSharpParser
{
static class AstVisitorUtility
{
public static ulong CreateIdentifier(CsharpAstNode astNode){
string[] properties =
{
astNode.AstValue,":",
astNode.AstType.ToString(),":",
astNode.EntityHash.ToString(),":",
astNode.RawKind.ToString(),":",
astNode.Path,":",
astNode.Location_range_start_line.ToString(),":",
astNode.Location_range_start_column.ToString(),":",
astNode.Location_range_end_line.ToString(),":",
astNode.Location_range_end_column.ToString()
};

string res = string.Concat(properties);

//WriteLine(res);
return FnvHash(res);
}

private static ulong FnvHash(string data_)
{
ulong hash = 14695981039346656037;

int len = data_.Length;
for (int i = 0; i < len; ++i)
{
hash ^= data_[i];
hash *= 1099511628211;
}

return hash;
}

public static ulong GetAstNodeId(SyntaxNode node){
CsharpAstNode astNode = new CsharpAstNode
{
AstValue = node.ToString(),
RawKind = node.Kind(),
EntityHash = node.GetHashCode(),
AstType = AstTypeEnum.Declaration
};
astNode.SetLocation(node.SyntaxTree.GetLineSpan(node.Span));
var ret = CreateIdentifier(astNode);
return ret;
}
}
}
100 changes: 5 additions & 95 deletions plugins/csharp/parser/src_csharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static int Main(string[] args)
}*/

//Converting the connectionstring into entiy framwork style connectionstring
string csharpConnectionString = transformConnectionString();
string csharpConnectionString = ProgramUtility.TransformConnectionString(_connectionString);

var options = new DbContextOptionsBuilder<CsharpDbContext>()
.UseNpgsql(csharpConnectionString)
Expand All @@ -95,17 +95,17 @@ static int Main(string[] args)
foreach (var p in _rootDir)
{
Console.WriteLine(p);
allFiles.AddRange(GetSourceFilesFromDir(p, ".cs"));
allFiles.AddRange(ProgramUtility.GetSourceFilesFromDir(p, ".cs"));
}

foreach (var f in allFiles)
{
WriteLine(f);
}
IEnumerable<string> assemblies = GetSourceFilesFromDir(_buildDir, ".dll");
IEnumerable<string> assemblies = ProgramUtility.GetSourceFilesFromDir(_buildDir, ".dll");
IEnumerable<string> assemblies_base = assemblies;
if (args.Length == 5)
assemblies_base = GetSourceFilesFromDir(_buildDirBase, ".dll");
assemblies_base = ProgramUtility.GetSourceFilesFromDir(_buildDirBase, ".dll");

List<SyntaxTree> trees = new List<SyntaxTree>();
foreach (string file in allFiles)
Expand Down Expand Up @@ -195,96 +195,6 @@ private static async Task<int> ParseTree(CsharpDbContext context,
return index;
});
return await ParsingTask;
}

public static IEnumerable<string> GetSourceFilesFromDir(string root, string extension)
{
IEnumerable<string> allFiles = new string[]{};
// Data structure to hold names of subfolders.
ArrayList dirs = new ArrayList();

if (!System.IO.Directory.Exists(root))
{
throw new ArgumentException();
}
dirs.Add(root);

while (dirs.Count > 0)
{
string currentDir = dirs[0].ToString();
dirs.RemoveAt(0);
string[] subDirs;
try
{
subDirs = System.IO.Directory.GetDirectories(currentDir);
}
catch (UnauthorizedAccessException e)
{
WriteLine(e.Message);
continue;
}
catch (System.IO.DirectoryNotFoundException e)
{
WriteLine(e.Message);
continue;
}

// Add the subdirectories for traversal.
dirs.AddRange(subDirs);

string[] files = null;
try
{
files = System.IO.Directory.GetFiles(currentDir);
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
continue;
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
continue;
}

foreach (string file in files)
{
try
{
System.IO.FileInfo fi = new System.IO.FileInfo(file);
if (fi.Extension == extension) {
allFiles = allFiles.Append(file);
}
}
catch (System.IO.FileNotFoundException e)
{
// If file was deleted by a separate application
Console.WriteLine(e.Message);
}
}
}

return allFiles;
}

private static string transformConnectionString()
{
_connectionString = _connectionString.Substring(_connectionString.IndexOf(':')+1);
_connectionString = _connectionString.Replace("user", "username");
string [] properties = _connectionString.Split(';');
string csharpConnectionString = "";
for (int i = 0; i < properties.Length; ++i)
{
csharpConnectionString += properties[i].Substring(0,1).ToUpper()
+ properties[i].Substring(1);
if (i < properties.Length-1)
{
csharpConnectionString += ";";
}
}

return csharpConnectionString;
}
}
}
}
Loading
Loading