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

Cpp metrics plugin #638

Merged
merged 14 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5.1)
cmake_minimum_required(VERSION 3.16.3)
intjftw marked this conversation as resolved.
Show resolved Hide resolved
project(CodeCompass)

# Common config variables and settings
Expand Down
5 changes: 5 additions & 0 deletions Functions.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Generate ODB files from sources
# @return ODB_CXX_SOURCES - odb cxx source files
function(generate_odb_files _src)
set(DEPENDENCY_PLUGIN_INCLUDE_DIRS ${ARGN})
list(TRANSFORM DEPENDENCY_PLUGIN_INCLUDE_DIRS PREPEND "-I${CMAKE_SOURCE_DIR}/plugins/")
list(TRANSFORM DEPENDENCY_PLUGIN_INCLUDE_DIRS APPEND "/model/include")

foreach(_file ${_src})
get_filename_component(_dir ${_file} DIRECTORY)
get_filename_component(_name ${_file} NAME)
Expand All @@ -25,6 +29,7 @@ function(generate_odb_files _src)
-I ${CMAKE_SOURCE_DIR}/model/include
-I ${CMAKE_SOURCE_DIR}/util/include
-I ${ODB_INCLUDE_DIRS}
${DEPENDENCY_PLUGIN_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}/${_file}
COMMAND
mv ${CMAKE_CURRENT_BINARY_DIR}/include/model/${_cxx}
Expand Down
15 changes: 15 additions & 0 deletions plugins/cpp_metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# The C++ metrics plugin depends on the C++ plugin's components to be built.

if ("${cpp_PLUGIN_DIR}" STREQUAL "")
# Use SEND_ERROR here so a build file is not generated at the end.
# CodeCompass might use a lot of plugins and produce a lengthy build, so
# a warning at configure time would easily be missed by the users.
message(SEND_ERROR
"C++ Metrics plugin found without C++ plugin in the plugins directory.")
endif()

add_subdirectory(model)
add_subdirectory(parser)
add_subdirectory(service)

install_webplugin(webgui)
mcserep marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 16 additions & 0 deletions plugins/cpp_metrics/model/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
include_directories(
include
${cpp_PLUGIN_DIR}/model/include)

message(WARNING "${cpp_PLUGIN_DIR}/model/include")

set(ODB_SOURCES
include/model/cppastnodemetrics.h
include/model/cppfilemetrics.h)

generate_odb_files("${ODB_SOURCES}" "cpp")

add_odb_library(cppmetricsmodel ${ODB_CXX_SOURCES})
target_link_libraries(cppmetricsmodel cppmodel)

install_sql()
36 changes: 36 additions & 0 deletions plugins/cpp_metrics/model/include/model/cppastnodemetrics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef CC_MODEL_CPPASTNODEMETRICS_H
#define CC_MODEL_CPPASTNODEMETRICS_H

//#include <../../../../plugins/cpp/model/include/model/cppastnode.h>
mcserep marked this conversation as resolved.
Show resolved Hide resolved
#include <model/cppastnode.h>

namespace cc
{
namespace model
{

#pragma db object
struct CppAstNodeMetrics
{
enum Type
{
PARAMETER_COUNT
};

#pragma db id auto
std::uint64_t id;

#pragma db not_null
CppAstNodeId astNodeId;

#pragma db not_null
Type type;

#pragma db not_null
unsigned value;
};

} //model
} //cc

#endif //CC_MODEL_CPPASTNODEMETRICS_H
35 changes: 35 additions & 0 deletions plugins/cpp_metrics/model/include/model/cppfilemetrics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef CC_MODEL_CPPDIRECTORYMETRICS_H
#define CC_MODEL_CPPDIRECTORYMETRICS_H

#include <model/file.h>

namespace cc
{
namespace model
{

#pragma db object
struct CppDirectoryMetrics
{
enum Type
{
PLACEHOLDER
};

#pragma db id auto
std::uint64_t id;

#pragma db not_null
FileId file;

#pragma db not_null
Type type;

#pragma db not_null
unsigned value;
};

} //model
} //cc

#endif //CC_MODEL_CPPDIRECTORYMETRICS_H
21 changes: 21 additions & 0 deletions plugins/cpp_metrics/parser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
include_directories(
include
${PROJECT_SOURCE_DIR}/model/include
${PROJECT_SOURCE_DIR}/util/include
${PROJECT_SOURCE_DIR}/parser/include
${PROJECT_SOURCE_DIR}/plugins/cpp/model/include
${PROJECT_BINARY_DIR}/plugins/cpp/model/include
${PLUGIN_DIR}/model/include)

add_library(cxxmetricsparser SHARED
src/cppmetricsparser.cpp)

target_link_libraries(cxxmetricsparser
cppmetricsmodel
model
util
${Boost_LIBRARIES})

install(TARGETS cxxmetricsparser
LIBRARY DESTINATION ${INSTALL_LIB_DIR}
DESTINATION ${INSTALL_PARSER_DIR})
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef CC_PARSER_DUMMYPARSER_H
#define CC_PARSER_DUMMYPARSER_H
mcserep marked this conversation as resolved.
Show resolved Hide resolved

#include <parser/abstractparser.h>
#include <parser/parsercontext.h>

#include <model/cppastnodemetrics.h>
#include <model/cppastnodemetrics-odb.hxx>

#include <model/cppfunction.h>
#include <model/cppfunction-odb.hxx>

#include <util/parserutil.h>
#include <util/threadpool.h>

namespace cc
{
namespace parser
{

class CppMetricsParser : public AbstractParser
{
public:
CppMetricsParser(ParserContext& ctx_);
virtual ~CppMetricsParser();
virtual bool parse() override;

private:
bool accept(const std::string& path_);

void functionParameters();

std::unique_ptr<util::JobQueueThreadPool<std::string>> _pool;
};

} // parser
} // cc

#endif // CC_PLUGINS_PARSER_CPPMETRICSPARSER_H
89 changes: 89 additions & 0 deletions plugins/cpp_metrics/parser/src/cppmetricsparser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <cppmetricsparser/cppmetricsparser.h>

#include <model/cppastnodemetrics.h>
#include <model/cppastnodemetrics-odb.hxx>
#include <model/cppfilemetrics.h>
#include <model/cppfilemetrics-odb.hxx>

#include <model/cppastnode.h>
#include <model/cppastnode-odb.hxx>

#include <boost/filesystem.hpp>

#include <util/logutil.h>
#include <util/odbtransaction.h>

#include <memory>

namespace cc
{
namespace parser
{

CppMetricsParser::CppMetricsParser(ParserContext& ctx_): AbstractParser(ctx_)
{
}

bool CppMetricsParser::accept(const std::string& path_)
{
std::string ext = boost::filesystem::extension(path_);
return ext == ".dummy";
}

void CppMetricsParser::functionParameters()
{
util::OdbTransaction {_ctx.db} ([&, this] {
for (const model::CppFunction func
: _ctx.db->query<model::CppFunction>())
{
model::CppAstNodeMetrics funcParams;
funcParams.astNodeId = func.astNodeId;
funcParams.type = model::CppAstNodeMetrics::Type::PARAMETER_COUNT;
funcParams.value = func.parameters.size();
mcserep marked this conversation as resolved.
Show resolved Hide resolved
_ctx.db->persist(funcParams);
}
});
}

bool CppMetricsParser::parse()
{
// Function parameter number metric.
functionParameters();

return true;
}

CppMetricsParser::~CppMetricsParser()
{
}

/* These two methods are used by the plugin manager to allow dynamic loading
of CodeCompass Parser plugins. Clang (>= version 6.0) gives a warning that
these C-linkage specified methods return types that are not proper from a
C code.

These codes are NOT to be called from any C code. The C linkage is used to
turn off the name mangling so that the dynamic loader can easily find the
symbol table needed to set the plugin up.
*/
// When writing a plugin, please do NOT copy this notice to your code.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
extern "C"
{
boost::program_options::options_description getOptions()
{
boost::program_options::options_description description("C++ Metrics Plugin");

return description;
}

std::shared_ptr<CppMetricsParser> make(ParserContext& ctx_)
{
return std::make_shared<CppMetricsParser>(ctx_);
}
}
#pragma clang diagnostic pop

} // parser
} // cc
49 changes: 49 additions & 0 deletions plugins/cpp_metrics/service/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
include_directories(
include
${CMAKE_CURRENT_BINARY_DIR}/gen-cpp
${PROJECT_SOURCE_DIR}/util/include
${PROJECT_SOURCE_DIR}/webserver/include)

include_directories(SYSTEM
${THRIFT_LIBTHRIFT_INCLUDE_DIRS})

add_custom_command(
OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/gen-cpp/cppmetrics_constants.cpp
${CMAKE_CURRENT_BINARY_DIR}/gen-cpp/cppmetrics_constants.h
${CMAKE_CURRENT_BINARY_DIR}/gen-cpp/cppmetrics_types.cpp
${CMAKE_CURRENT_BINARY_DIR}/gen-cpp/cppmetrics_types.h
${CMAKE_CURRENT_BINARY_DIR}/gen-cpp/CppMetricsService.cpp
${CMAKE_CURRENT_BINARY_DIR}/gen-cpp/CppMetricsService.h
${CMAKE_CURRENT_BINARY_DIR}/gen-cpp
COMMAND
${THRIFT_EXECUTABLE} --gen cpp
-o ${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/cppmetrics.thrift
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/cppmetrics.thrift
COMMENT
"Generating Thrift for cppmetrics.thrift")

add_library(cppmetricsthrift STATIC
${CMAKE_CURRENT_BINARY_DIR}/gen-cpp/cppmetrics_constants.cpp
${CMAKE_CURRENT_BINARY_DIR}/gen-cpp/cppmetrics_types.cpp
${CMAKE_CURRENT_BINARY_DIR}/gen-cpp/CppMetricsService.cpp)

target_compile_options(cppmetricsthrift PUBLIC -fPIC)

add_library(cppmetricsservice SHARED
src/cppmetricsservice.cpp
src/plugin.cpp)

target_compile_options(cppmetricsservice PUBLIC -Wno-unknown-pragmas)

target_link_libraries(cppmetricsservice
cppmetricsmodel
model
util
${THRIFT_LIBTHRIFT_LIBRARIES}
${ODB_LIBRARIES}
cppmetricsthrift)

install(TARGETS cppmetricsservice DESTINATION ${INSTALL_SERVICE_DIR})
7 changes: 7 additions & 0 deletions plugins/cpp_metrics/service/cppmetrics.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace cpp cc.service.cppmetrics
namespace java cc.service.cppmetrics

service CppMetricsService
{
string getDummyString()
}
43 changes: 43 additions & 0 deletions plugins/cpp_metrics/service/include/service/cppmetricsservice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef CC_SERVICE_DUMMY_DUMMYSSERVICE_H
#define CC_SERVICE_DUMMY_DUMMYSSERVICE_H

#include <memory>
#include <vector>

#include <boost/program_options/variables_map.hpp>

#include <odb/database.hxx>
#include <util/odbtransaction.h>
#include <webserver/servercontext.h>

#include <CppMetricsService.h>

namespace cc
{
namespace service
{
namespace cppmetrics
{

class CppMetricsServiceHandler : virtual public CppMetricsServiceIf
mcserep marked this conversation as resolved.
Show resolved Hide resolved
{
public:
CppMetricsServiceHandler(
std::shared_ptr<odb::database> db_,
std::shared_ptr<std::string> datadir_,
const cc::webserver::ServerContext& context_);

void getDummyString(std::string& str_);

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

const boost::program_options::variables_map& _config;
};

} // cppmetrics
} // service
} // cc

#endif // CC_SERVICE_DUMMY_CPPMETRICSSSERVICE_H
Loading