Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for C# static analysis #584

Open
wants to merge 62 commits into
base: master
Choose a base branch
from

Conversation

intjftw
Copy link
Collaborator

@intjftw intjftw commented Oct 4, 2022

The official support for C# static analysis. Uses the Roslyn Semantic API to parse source code.

This plugin has been made by @Borisz42 as his TDK thesis. Thanks Dávid for your work!

@intjftw intjftw requested review from mcserep and bruntib October 4, 2022 15:20
@intjftw intjftw changed the title C# plugin Support for C# static analysis Oct 4, 2022
@intjftw intjftw added Kind: Enhancement 🌟 Plugin: C# Issues related to the parsing and presentation of C# projects. labels Oct 4, 2022
Copy link
Collaborator Author

@intjftw intjftw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plugin generally works fine, however, there is much space for improvement. We should run some tests on larger projects to find critical errors.

Comment on lines +12 to +15
<ItemGroup>
<ProjectReference Include="..\..\..\..\lib\csharp\thrift_netstd\Thrift\Thrift.csproj" />
<ProjectReference Include="..\..\..\..\..\..\..\codecompass\intjftw-build\plugins\csharp\service\src_csharp\gen-netstd\gen-netstd.csproj" />
</ItemGroup>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are written into the file by CMake in order to include two csproj files. The Thrift.csproj file reference can be put in as a default reference since it is part of the source code with a fixed path. However, the other one (gen-netstd.csproj) is generated by CMake in a previous step inside the build directory, so this reference cannot be guaranteed to have the same relative path all the time, hence the ugly path to my build directory. This reference is added during build time and it requires the committer to manually delete this line everytime they want to make a git commit. My question is, @mcserep, what do you think how we can solve this problem? Adding some kind of git hook or git filter that automatically deletes this line from the csproj file is a valid solution?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tough question, I see the following options:

  1. Generated, language-specific Thrift interfaces should be generated into the source folder and version controlled. These files rarely change and it is also an issue for the web frontend that the required JS/TS interfaces are not available in the source folder of CodeCompass. As a first attempt we could do this for the C# Thrift interfaces to see how this work. However, if shall refactor this later to the complete project. (Preferably in another PR.)
  2. Copy the complete C# source code into the build folder, edit the .csproj file there, and do the compilation there. Basically that is what we do for the current and the new (New web frontend #590) webgui, in the installation folder. Kinda ugly to be honest.
  3. During building copy the csharpservice.csproj inside the source directory, e.g. csharpservice_generated.csproj and edit the copied version. Use that for building the C# project. Place csharpservice_generated.csproj in the gitignore list.

In my opinion, option 2 is what we currently do, but it is not really nice. Option 1 should be the proper solution, but it requires refactoring for the complete project to be a proper solution. Option 3 requires minimal extra effort. Although it creates a new generated file in the source directory, that could easily be ignored from VC. It is not really the proper and general solution, but can be done easily.

So maybe do Option 3 now and later refactor as suggested in Option 1 in a separate PR?

Comment on lines 31 to 34
bool CsharpParser::acceptProjectBuildPath(const std::vector<std::string>& path_)
{
return path_.size() >= 2 && fs::is_directory(path_[0]) && fs::is_directory(path_[1]);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parser requires the source code root directory and the build directory to work properly. Right now the solution is to have a fix order of input parameters (the source root first and the build directory second). This is a very inflexible solution, especially if we want to use multiple language plugins at the same time.

plugins/csharp/CMakeLists.txt Outdated Show resolved Hide resolved
Comment on lines +26 to +31
add_custom_target(dotnetaddref2
COMMAND dotnet add reference ${CMAKE_SOURCE_DIR}/lib/csharp/thrift_netstd/Thrift/Thrift.csproj
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/gen-netstd/"
)

add_dependencies(dotnetaddref2 dotnetaddref dotnetaddclasslib dotnetbuildservice)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this reference can be written in csproj file permanently.

plugins/csharp/test/src/csharpparserboosttest.cpp Outdated Show resolved Hide resolved
std::string& return_,
const core::AstNodeId& astNodeId_)
{
LOG(info) << "getSourceText";
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All unnecessary debug comments should be deleted.

namespace bp = boost::process;
namespace pt = boost::property_tree;

int CsharpServiceHandler::_thriftServerPort = 9091;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us make sure that the port is free and if it's not, search for a free one.

Comment on lines +27 to +59
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);
}

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;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper functions like these might be placed in a separate file / class.

plugins/csharp/parser/src_csharp/AstVisitor.cs Outdated Show resolved Hide resolved
{
private static readonly ILogger Logger = LoggingHelper.CreateLogger<CSharpQueryServer>();
private static readonly TConfiguration Configuration = new TConfiguration();
private static int port = 9091;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I rewrote this part to not clash with the cpp service, and to work with multiple parsed C# projects, port numbering should really be given some thought. The plugin should look for the next free port if the current one is taken.

@mcserep mcserep added this to the Release Gershwin milestone Jan 8, 2023
@@ -0,0 +1,79 @@
// Licensed to the Apache Software Foundation(ASF) under one
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should investigate whether this large pack of Thrift files is really needed as source in the plugin. It seems unncessary.

intjftw added 2 commits March 29, 2023 14:58
…irectories and a separate build path. Also corrected the parsing command to be able to run CodeCompass_parser from anywhere.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Kind: Enhancement 🌟 Plugin: C# Issues related to the parsing and presentation of C# projects.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants