From 63481da35f1c45629762064c56ba644d120c0f36 Mon Sep 17 00:00:00 2001 From: Anett Fekete Date: Tue, 2 Jul 2024 00:38:37 +0200 Subject: [PATCH] Adding inheritance to the efferent coupling of types. --- .../model/include/model/cppastnodemetrics.h | 1 + plugins/cpp_metrics/parser/CMakeLists.txt | 1 - .../cppmetricsparser/cppmetricsparser.h | 2 + .../parser/src/cppmetricsparser.cpp | 60 +++++++++++++++++-- plugins/cpp_metrics/parser/src/efferent.cpp | 57 ------------------ plugins/cpp_metrics/parser/src/efferent.h | 28 --------- 6 files changed, 59 insertions(+), 90 deletions(-) delete mode 100644 plugins/cpp_metrics/parser/src/efferent.cpp delete mode 100644 plugins/cpp_metrics/parser/src/efferent.h diff --git a/plugins/cpp_metrics/model/include/model/cppastnodemetrics.h b/plugins/cpp_metrics/model/include/model/cppastnodemetrics.h index e4c039e8c..2c6b3cd28 100644 --- a/plugins/cpp_metrics/model/include/model/cppastnodemetrics.h +++ b/plugins/cpp_metrics/model/include/model/cppastnodemetrics.h @@ -21,6 +21,7 @@ struct CppAstNodeMetrics BUMPY_ROAD = 4, LACK_OF_COHESION = 5, LACK_OF_COHESION_HS = 6, + EFFERENT_TYPE = 7 }; #pragma db id auto diff --git a/plugins/cpp_metrics/parser/CMakeLists.txt b/plugins/cpp_metrics/parser/CMakeLists.txt index c2bd3f193..08a32f62b 100644 --- a/plugins/cpp_metrics/parser/CMakeLists.txt +++ b/plugins/cpp_metrics/parser/CMakeLists.txt @@ -8,7 +8,6 @@ include_directories( ${PLUGIN_DIR}/model/include) add_library(cxxmetricsparser SHARED - src/efferent.cpp src/cppmetricsparser.cpp) target_link_libraries(cxxmetricsparser diff --git a/plugins/cpp_metrics/parser/include/cppmetricsparser/cppmetricsparser.h b/plugins/cpp_metrics/parser/include/cppmetricsparser/cppmetricsparser.h index 0991b96ad..92facba29 100644 --- a/plugins/cpp_metrics/parser/include/cppmetricsparser/cppmetricsparser.h +++ b/plugins/cpp_metrics/parser/include/cppmetricsparser/cppmetricsparser.h @@ -76,6 +76,8 @@ class CppMetricsParser : public AbstractParser // Calculate the lack of cohesion between member variables // and member functions for every type. void lackOfCohesion(); + // Calculate the efferent coupling of types. + void efferentTypeLevel(); /// @brief Constructs an ODB query that you can use to filter only diff --git a/plugins/cpp_metrics/parser/src/cppmetricsparser.cpp b/plugins/cpp_metrics/parser/src/cppmetricsparser.cpp index cd5fd33df..b216e51be 100644 --- a/plugins/cpp_metrics/parser/src/cppmetricsparser.cpp +++ b/plugins/cpp_metrics/parser/src/cppmetricsparser.cpp @@ -1,5 +1,4 @@ #include -#include "efferent.h" #include #include @@ -7,6 +6,8 @@ #include #include #include +#include +#include #include #include @@ -365,6 +366,58 @@ void CppMetricsParser::lackOfCohesion() }); } +void CppMetricsParser::efferentTypeLevel() +{ + typedef odb::query MemTypeQuery; + typedef odb::query InheritanceQuery; + + util::OdbTransaction{_ctx.db}([&, this] + { + std::set dependentTypes; + for (const model::CohesionCppRecordView& type + : _ctx.db->query()) + { + // Skip types that were included from external libraries. + /*type.location.file.load(); + const auto typeFile = _ctx.db->query_one( + odb::query::id == type.location.file->id); + if (!typeFile || !cc::util::isRootedUnderAnyOf(_inputPaths, typeFile->path)) + continue;*/ + //if (!cc::util::isRootedUnderAnyOf(_inputPaths, type.filePath)) + //continue; + + dependentTypes.clear(); + for (const model::CppMemberType& mem : _ctx.db->query( + MemTypeQuery::typeHash == type.entityHash && + MemTypeQuery::kind == model::CppMemberType::Kind::Field)) + { + LOG(warning) << "step 1"; + dependentTypes.insert(mem.memberAstNode.load()->id); + } + + for (const model::CppInheritance& inherit : _ctx.db->query( + InheritanceQuery::derived == type.astNodeId)) + { + LOG(warning) << "step 2"; + dependentTypes.insert(inherit.base); + } + + /*for (const model::CppMemberType& mem : _ctx.db->query( + MemTypeQuery::typeHash == type.entityHash && + MemTypeQuery::kind == model::CppMemberType::Kind::Method)) + { + dependentTypes.insert(mem.memberAstNode->id); + }*/ + + model::CppAstNodeMetrics metric; + metric.astNodeId = type.astNodeId; + metric.type = model::CppAstNodeMetrics::Type::EFFERENT_TYPE; + metric.value = dependentTypes.size(); + _ctx.db->persist(metric); + } + }); +} + bool CppMetricsParser::parse() { LOG(info) << "[cppmetricsparser] Computing function parameter count metric."; @@ -377,9 +430,8 @@ bool CppMetricsParser::parse() typeMcCabe(); LOG(info) << "[cppmetricsparser] Computing Lack of Cohesion metric for types."; lackOfCohesion(); - - EfferentCoupling efferent(_ctx, _inputPaths); - efferent.efferentTypeLevel(); + LOG(info) << "[cppmetricsparser] Computing efferent coupling metric for types."; + efferentTypeLevel(); return true; } diff --git a/plugins/cpp_metrics/parser/src/efferent.cpp b/plugins/cpp_metrics/parser/src/efferent.cpp deleted file mode 100644 index 3e041979e..000000000 --- a/plugins/cpp_metrics/parser/src/efferent.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "efferent.h" - -namespace cc -{ -namespace parser -{ - -EfferentCoupling::EfferentCoupling( - ParserContext& ctx_, - std::vector& inputPaths_) - : _ctx(ctx_), _inputPaths(inputPaths_) -{ -} - -void EfferentCoupling::efferentTypeLevel() -{ - typedef odb::query MemTypeQuery; - - util::OdbTransaction{_ctx.db}([&, this] - { - std::set memberTypes; - for (const model::CohesionCppRecordView& type - : _ctx.db->query()) - { - // Skip types that were included from external libraries. - if (!cc::util::isRootedUnderAnyOf(_inputPaths, type.filePath)) - continue; - - memberTypes.clear(); - for (const model::CppMemberType& mem : _ctx.db->query( - MemTypeQuery::typeHash == type.entityHash && - MemTypeQuery::kind == model::CppMemberType::Kind::Field)) - { - memberTypes.insert(mem.memberTypeHash); - } - - model::CppAstNodeMetrics metric; - metric.astNodeId = type.astNodeId; - metric.type = model::CppAstNodeMetrics::Type::EFFERENT_TYPE; - metric.value = memberTypes.size(); - _ctx.db->persist(metric); - } - }); -} - -} // parser -} // cc diff --git a/plugins/cpp_metrics/parser/src/efferent.h b/plugins/cpp_metrics/parser/src/efferent.h deleted file mode 100644 index 9a5e6d359..000000000 --- a/plugins/cpp_metrics/parser/src/efferent.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef CC_PARSER_EFFERENT_H -#define CC_PARSER_EFFERENT_H - -#include "parser/parsercontext.h" - -namespace cc -{ -namespace parser -{ - -class EfferentCoupling -{ -public: - EfferentCoupling( - ParserContext& ctx_, - std::vector& inputPaths_); - - void efferentTypeLevel(); - -private: - ParserContext _ctx; - std::vector _inputPaths; -}; - -} // parser -} // cc - -#endif // CC_PARSER_EFFERENT_H