Skip to content

Implement efferent coupling at module level #790

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

Merged
merged 5 commits into from
May 5, 2025
Merged
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
10 changes: 5 additions & 5 deletions plugins/cpp/model/include/model/cppfunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct CppFunction : CppTypedEntity
typedef std::shared_ptr<CppFunction> CppFunctionPtr;

#pragma db view \
object(CppFunction) object(CppVariable = Parameters : CppFunction::parameters)
object(CppFunction) object(CppVariable = Parameters inner : CppFunction::parameters)
struct CppFunctionParamCount
{
#pragma db column("count(" + Parameters::id + ")")
Expand All @@ -45,7 +45,7 @@ struct CppFunctionParamCount

#pragma db view \
object(CppFunction) \
object(CppVariable = Parameters : CppFunction::parameters) \
object(CppVariable = Parameters inner : CppFunction::parameters) \
object(CppAstNode : CppFunction::astNodeId == CppAstNode::id) \
object(File : CppAstNode::location.file) \
query((?) + "GROUP BY" + cc::model::CppEntity::astNodeId + "," + cc::model::File::path)
Expand All @@ -59,7 +59,7 @@ struct CppFunctionParamCountWithId
};

#pragma db view \
object(CppFunction) object(CppVariable = Locals : CppFunction::locals)
object(CppFunction) object(CppVariable = Locals inner : CppFunction::locals)
struct CppFunctionLocalCount
{
#pragma db column("count(" + Locals::id + ")")
Expand Down Expand Up @@ -97,7 +97,7 @@ struct CppFunctionBumpyRoad

#pragma db view \
object(CppFunction) \
object(CppVariable = Parameters : CppFunction::parameters)
object(CppVariable = Parameters inner : CppFunction::parameters)
struct CppFunctionParamTypeView
{
#pragma db column(CppFunction::astNodeId)
Expand All @@ -109,7 +109,7 @@ struct CppFunctionParamTypeView

#pragma db view \
object(CppFunction) \
object(CppVariable = Locals : CppFunction::locals)
object(CppVariable = Locals inner : CppFunction::locals)
struct CppFunctionLocalTypeView
{
#pragma db column(CppFunction::astNodeId)
Expand Down
5 changes: 3 additions & 2 deletions plugins/cpp_metrics/model/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ include_directories(
set(ODB_SOURCES
include/model/cppastnodemetrics.h
include/model/cppcohesionmetrics.h
include/model/cppfilemetrics.h)
include/model/cppfilemetrics.h
include/model/cpptypedependencymetrics.h)

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

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

install_sql()
install_sql()
2 changes: 1 addition & 1 deletion plugins/cpp_metrics/model/include/model/cppfilemetrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct CppFileMetrics
{
enum Type
{
PLACEHOLDER
EFFERENT_MODULE
};

#pragma db id auto
Expand Down
69 changes: 69 additions & 0 deletions plugins/cpp_metrics/model/include/model/cpptypedependencymetrics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#ifndef CC_MODEL_CPPTYPEDEPENDENCYMETRICS_H
#define CC_MODEL_CPPTYPEDEPENDENCYMETRICS_H

#include <cstdint>
#include <string>
#include <model/cppentity.h>
#include <model/cpprecord.h>
#include <model/cppastnode.h>
#include <model/file.h>

namespace cc
{
namespace model
{

#pragma db object
struct CppTypeDependencyMetrics
{
#pragma db id auto
std::uint64_t id;

#pragma db not_null
std::uint64_t entityHash;

#pragma db not_null
std::uint64_t dependencyHash;
};

#pragma db view \
object(CppTypeDependencyMetrics) \
object(CppAstNode = EntityAstNode : CppTypeDependencyMetrics::entityHash == EntityAstNode::entityHash \
&& EntityAstNode::astType == cc::model::CppAstNode::AstType::Definition) \
object(File = EntityFile : EntityAstNode::location.file == EntityFile::id) \
object(CppAstNode = DependencyAstNode : CppTypeDependencyMetrics::dependencyHash == DependencyAstNode::entityHash \
&& DependencyAstNode::astType == cc::model::CppAstNode::AstType::Definition) \
object(File = DependencyFile : DependencyAstNode::location.file == DependencyFile::id)
struct CppTypeDependencyMetricsPathView
{
#pragma db column(CppTypeDependencyMetrics::entityHash)
std::size_t entityHash;

#pragma db column(CppTypeDependencyMetrics::dependencyHash)
std::size_t dependencyHash;

#pragma db column(EntityFile::path)
std::string entityPath;

#pragma db column(DependencyFile::path)
std::string dependencyPath;
};

#pragma db view \
object(CppTypeDependencyMetrics) \
object(CppAstNode = EntityAstNode : CppTypeDependencyMetrics::entityHash == EntityAstNode::entityHash \
&& EntityAstNode::astType == cc::model::CppAstNode::AstType::Definition) \
object(File = EntityFile : EntityAstNode::location.file == EntityFile::id) \
object(CppAstNode = DependencyAstNode : CppTypeDependencyMetrics::dependencyHash == DependencyAstNode::entityHash \
&& DependencyAstNode::astType == cc::model::CppAstNode::AstType::Definition) \
object(File = DependencyFile : DependencyAstNode::location.file == DependencyFile::id)
struct CppTypeDependencyMetricsPathViewDistinctCount
{
#pragma db column("count(distinct" + CppTypeDependencyMetrics::dependencyHash + ")")
std::size_t count;
};

} // model
} // cc

#endif // CC_MODEL_CPPTYPEDEPENDENCYMETRICS_H
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ class CppMetricsParser : public AbstractParser
void efferentTypeLevel();
// Calculate the afferent coupling of types.
void afferentTypeLevel();

// Calculate the efferent coupling at module level.
void efferentModuleLevel();
// Returns module path query based on parser configuration.
odb::query<model::File> getModulePathsQuery();

/// @brief Constructs an ODB query that you can use to filter only
/// the database records of the given parameter type whose path
Expand Down Expand Up @@ -203,6 +206,7 @@ class CppMetricsParser : public AbstractParser
static const int lackOfCohesionPartitionMultiplier = 25;
static const int efferentCouplingTypesPartitionMultiplier = 5;
static const int afferentCouplingTypesPartitionMultiplier = 5;
static const int efferentCouplingModulesPartitionMultiplier = 5;
};

} // parser
Expand Down
71 changes: 68 additions & 3 deletions plugins/cpp_metrics/parser/src/cppmetricsparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
#include <model/cppinheritance-odb.hxx>
#include <model/cpprecord.h>
#include <model/cpprecord-odb.hxx>

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

#include <boost/filesystem.hpp>

Expand Down Expand Up @@ -390,7 +393,9 @@ void CppMetricsParser::efferentTypeLevel()
dependentTypes.clear();

// Count parent types
auto inheritanceView = _ctx.db->query<model::CppInheritanceCount>(
auto inheritanceView = _ctx.db->query<model::CppInheritance>(
InheritanceQuery::derived == type.entityHash);
auto inheritanceCount = _ctx.db->query_value<model::CppInheritanceCount>(
InheritanceQuery::derived == type.entityHash);

// Count unique attribute types
Expand Down Expand Up @@ -423,8 +428,26 @@ void CppMetricsParser::efferentTypeLevel()
model::CppAstNodeMetrics metric;
metric.astNodeId = type.astNodeId;
metric.type = model::CppAstNodeMetrics::Type::EFFERENT_TYPE;
metric.value = inheritanceView.begin()->count + dependentTypes.size();
metric.value = inheritanceCount.count + dependentTypes.size();
_ctx.db->persist(metric);

auto typeRelationInserter = [this](const std::uint64_t& entityHash, const std::uint64_t& dependencyHash)
{
model::CppTypeDependencyMetrics relation;
relation.entityHash = entityHash;
relation.dependencyHash = dependencyHash;
_ctx.db->persist(relation);
};

// Insert type dependency relations
for (const std::uint64_t& d : dependentTypes) {
typeRelationInserter(type.entityHash, d);
}

// Insert inheritance relations
for (const model::CppInheritance& d : inheritanceView) {
typeRelationInserter(type.entityHash, d.base);
}
}
});
});
Expand Down Expand Up @@ -513,6 +536,46 @@ void CppMetricsParser::afferentTypeLevel()
});
}

odb::query<model::File> CppMetricsParser::getModulePathsQuery()
{
if (_ctx.moduleDirectories.empty()) {
// No module directories specified, compute for all directories
return odb::query<model::File>::type == cc::model::File::DIRECTORY_TYPE && getFilterPathsQuery<model::File>();
} else {
// Compute for module directories
return odb::query<model::File>::path.in_range(_ctx.moduleDirectories.begin(), _ctx.moduleDirectories.end());
}
}

void CppMetricsParser::efferentModuleLevel()
{
parallelCalcMetric<model::File>(
"Efferent coupling at module level",
_threadCount * efferentCouplingModulesPartitionMultiplier,// number of jobs; adjust for granularity
getModulePathsQuery(),
[&, this](const MetricsTasks<model::File>& tasks)
{
util::OdbTransaction{_ctx.db}([&, this]
{
typedef odb::query<model::CppTypeDependencyMetricsPathViewDistinctCount> TypeDependencyQuery;
typedef model::CppTypeDependencyMetricsPathViewDistinctCount TypeDependencyResult;

for (const model::File& file : tasks)
{
TypeDependencyResult types = _ctx.db->query_value<model::CppTypeDependencyMetricsPathViewDistinctCount>(
TypeDependencyQuery::EntityFile::path.like(file.path + '%') &&
!TypeDependencyQuery::DependencyFile::path.like(file.path + '%'));

model::CppFileMetrics metric;
metric.file = file.id;
metric.type = model::CppFileMetrics::Type::EFFERENT_MODULE;
metric.value = types.count;
_ctx.db->persist(metric);
}
});
});
}

bool CppMetricsParser::parse()
{
LOG(info) << "[cppmetricsparser] Computing function parameter count metric.";
Expand All @@ -529,6 +592,8 @@ bool CppMetricsParser::parse()
efferentTypeLevel();
LOG(info) << "[cppmetricsparser] Computing afferent coupling metric for types.";
afferentTypeLevel();
LOG(info) << "[cppmetricsparser] Computing efferent coupling metric at module level.";
efferentModuleLevel(); // This metric needs to be calculated after efferentTypeLevel
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion plugins/cpp_metrics/test/sources/parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ add_library(CppMetricsTestProject STATIC
typemccabe.cpp
lackofcohesion.cpp
bumpyroad.cpp
afferentcoupling.cpp)
afferentcoupling.cpp
modulemetrics.cpp)
13 changes: 13 additions & 0 deletions plugins/cpp_metrics/test/sources/parser/module_a/a1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef CC_CPP_MODULE_METRICS_TEST_A1
#define CC_CPP_MODULE_METRICS_TEST_A1

#include "./a2.h"

namespace CC_CPP_MODULE_METRICS_TEST
{
class A1 {
A2 a2;
};
}

#endif
13 changes: 13 additions & 0 deletions plugins/cpp_metrics/test/sources/parser/module_a/a2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef CC_CPP_MODULE_METRICS_TEST_A2
#define CC_CPP_MODULE_METRICS_TEST_A2

#include "../module_b/b1.h"

namespace CC_CPP_MODULE_METRICS_TEST
{
class A2 {
B1 b1;
};
}

#endif
13 changes: 13 additions & 0 deletions plugins/cpp_metrics/test/sources/parser/module_b/b1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef CC_CPP_MODULE_METRICS_TEST_B1
#define CC_CPP_MODULE_METRICS_TEST_B1

#include "./b2.h"

namespace CC_CPP_MODULE_METRICS_TEST
{
class B1 {
B2 b2;
};
}

#endif
9 changes: 9 additions & 0 deletions plugins/cpp_metrics/test/sources/parser/module_b/b2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef CC_CPP_MODULE_METRICS_TEST_B2
#define CC_CPP_MODULE_METRICS_TEST_B2

namespace CC_CPP_MODULE_METRICS_TEST
{
class B2 {};
}

#endif
15 changes: 15 additions & 0 deletions plugins/cpp_metrics/test/sources/parser/module_c/c1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef CC_CPP_MODULE_METRICS_TEST_C1
#define CC_CPP_MODULE_METRICS_TEST_C1

#include "../module_b/b1.h"
#include "./c2.h"

namespace CC_CPP_MODULE_METRICS_TEST
{
class C1 {
B1 b1;
C2 c2;
};
}

#endif
15 changes: 15 additions & 0 deletions plugins/cpp_metrics/test/sources/parser/module_c/c2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef CC_CPP_MODULE_METRICS_TEST_C2
#define CC_CPP_MODULE_METRICS_TEST_C2

#include "../module_a/a2.h"
#include "../module_b/b1.h"

namespace CC_CPP_MODULE_METRICS_TEST
{
class C2 {
A2 a2;
B1 b1;
};
}

#endif
6 changes: 6 additions & 0 deletions plugins/cpp_metrics/test/sources/parser/modulemetrics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "./module_a/a1.h"
#include "./module_a/a2.h"
#include "./module_b/b1.h"
#include "./module_b/b2.h"
#include "./module_c/c1.h"
#include "./module_c/c2.h"
Loading