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

Added calculation for afferent coupling of types #757

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 1 deletion plugins/cpp_metrics/model/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ include_directories(
set(ODB_SOURCES
include/model/cppastnodemetrics.h
include/model/cppcohesionmetrics.h
include/model/cppfilemetrics.h)
include/model/cppfilemetrics.h
include/model/cppafferentmetrics.h)

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

Expand Down
38 changes: 38 additions & 0 deletions plugins/cpp_metrics/model/include/model/cppafferentmetrics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef CC_MODEL_AFFERENTMETRICS_H
#define CC_MODEL_AFFERENTMETRICS_H

#include <model/cppentity.h>
#include <model/cpprecord.h>
#include <model/cppastnode.h>

namespace cc
{
namespace model
{
#pragma db view \
object(CppRecord) \
object(CppAstNode : CppRecord::astNodeId == CppAstNode::id) \
object(File : CppAstNode::location.file) \
object(CppMemberType : CppMemberType::memberAstNode)
struct AfferentRecordView
{
#pragma db column(CppEntity::entityHash)
std::size_t entityHash;

#pragma db column(CppMemberType::typeHash)
std::size_t typeHash;

#pragma db column(CppEntity::qualifiedName)
std::string qualifiedName;

#pragma db column(CppEntity::astNodeId)
CppAstNodeId astNodeId;

#pragma db column(File::path)
std::string filePath;
};

} //model
} //cc

#endif //CC_MODEL_AFFERENTMETRICS_H
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct CppAstNodeMetrics
BUMPY_ROAD = 4,
LACK_OF_COHESION = 5,
LACK_OF_COHESION_HS = 6,
AFFERENT_COUPLING = 7
};

#pragma db id auto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class CppMetricsParser : public AbstractParser
// Calculate the lack of cohesion between member variables
// and member functions for every type.
void lackOfCohesion();

// Calculate afferent coupling metric bewteen types
void afferentCouplingTypeLevel();

/// @brief Constructs an ODB query that you can use to filter only
/// the database records of the given parameter type whose path
Expand Down
50 changes: 50 additions & 0 deletions plugins/cpp_metrics/parser/src/cppmetricsparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <model/cppcohesionmetrics-odb.hxx>
#include <model/cppfilemetrics.h>
#include <model/cppfilemetrics-odb.hxx>
#include <model/cppafferentmetrics.h>
#include <model/cppafferentmetrics-odb.hxx>

#include <model/cppastnode.h>
#include <model/cppastnode-odb.hxx>
Expand Down Expand Up @@ -98,6 +100,52 @@ bool CppMetricsParser::cleanupDatabase()
return true;
}


void CppMetricsParser::afferentCouplingTypeLevel()
{
util::OdbTransaction{_ctx.db}([&,this]
{
std::set<std::uint64_t> typesFound;
std::unordered_map<std::uint64_t, int> typeFoundCnt;
std::unordered_map<std::uint64_t,std::uint64_t> astNodeIdOfType;

for (const model::AfferentRecordView& type
: _ctx.db->query<model::AfferentRecordView>())
{
if (!cc::util::isRootedUnderAnyOf(_inputPaths, type.filePath))
{
continue;
}

typesFound.clear();
for (const model::CppMemberType& member : _ctx.db->query<model::CppMemberType>(
odb::query<cc::model::CppMemberType>::typeHash == type.entityHash &&
odb::query<cc::model::CppMemberType>::kind == model::CppMemberType::Kind::Field))
{
typesFound.insert(member.memberTypeHash);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

My current solution is not complete. This coupling is not only defined by member fields but also by function parameters, inheritance relations etc. Please complement this calculation accordingly.


astNodeIdOfType[type.typeHash] = type.astNodeId;

for (const auto& t : typesFound)
{
typeFoundCnt[t]++;
}
}

for (const auto& pair : typeFoundCnt)
{
model::CppAstNodeMetrics metric;
metric.astNodeId = astNodeIdOfType[pair.first];
metric.type = model::CppAstNodeMetrics::Type::AFFERENT_COUPLING;
metric.value = pair.second;
_ctx.db->persist(metric);
}


});
}

void CppMetricsParser::functionParameters()
{
parallelCalcMetric<model::CppFunctionParamCountWithId>(
Expand Down Expand Up @@ -376,6 +424,8 @@ bool CppMetricsParser::parse()
typeMcCabe();
LOG(info) << "[cppmetricsparser] Computing Lack of Cohesion metric for types.";
lackOfCohesion();
LOG(info) << "[cppmetricsparser] Computing Afferent Coupling metric for types.";
afferentCouplingTypeLevel();
return true;
}

Expand Down
Loading