Skip to content

Commit a5a5b30

Browse files
Fix cppmetrics-ignore-lambdas flag
Previously, we calculated metrics for functions inside anonymous lambda objects. This has now been fixed.
1 parent 7e69ea2 commit a5a5b30

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

plugins/cpp/model/include/model/cppfunction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct CppFunction : CppTypedEntity
2323
unsigned int mccabe;
2424
unsigned int bumpiness;
2525
unsigned int statementCount;
26+
bool inLambdaObject = false;
2627

2728
// Hash of the record (e.g. class or struct) in which this function is declared
2829
// recordHash = 0 means this is a global function

plugins/cpp/parser/src/clangastvisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>
940940
if (const clang::CXXRecordDecl* parent = md->getParent())
941941
{
942942
cppFunction->recordHash = util::fnvHash(getUSR(parent));
943+
cppFunction->inLambdaObject = parent->isLambda();
943944
}
944945

945946
if (md->isVirtual())

plugins/cpp_metrics/parser/include/cppmetricsparser/cppmetricsparser.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ class CppMetricsParser : public AbstractParser
9494
// Returns cohesion record query based on parser configuration.
9595
odb::query<model::CohesionCppRecordView> getCohesionRecordQuery();
9696

97+
// Returns metric function query based on parser configuration.
98+
template<typename TQueryParam>
99+
odb::query<TQueryParam> getFunctionQuery() const
100+
{
101+
odb::query<TQueryParam> query = getFilterPathsQuery<TQueryParam>();
102+
103+
if (_ctx.options.count("cppmetrics-ignore-lambdas")) {
104+
query = query && odb::query<model::CppFunction>::inLambdaObject == false;
105+
}
106+
107+
return query;
108+
}
109+
97110
/// @brief Constructs an ODB query that you can use to filter only
98111
/// the database records of the given parameter type whose path
99112
/// is rooted under any of this parser's input paths.

plugins/cpp_metrics/parser/src/cppmetricsparser.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void CppMetricsParser::functionMcCabe()
132132
parallelCalcMetric<model::CppFunctionMcCabe>(
133133
"Function-level McCabe",
134134
_threadCount * functionMcCabePartitionMultiplier,// number of jobs; adjust for granularity
135-
getFilterPathsQuery<model::CppFunctionMcCabe>(),
135+
getFunctionQuery<model::CppFunctionMcCabe>(),
136136
[&, this](const MetricsTasks<model::CppFunctionMcCabe>& tasks)
137137
{
138138
util::OdbTransaction {_ctx.db} ([&, this]
@@ -155,7 +155,7 @@ void CppMetricsParser::functionBumpyRoad()
155155
parallelCalcMetric<model::CppFunctionBumpyRoad>(
156156
"Bumpy road complexity",
157157
_threadCount * functionBumpyRoadPartitionMultiplier,// number of jobs; adjust for granularity
158-
getFilterPathsQuery<model::CppFunctionBumpyRoad>(),
158+
getFunctionQuery<model::CppFunctionBumpyRoad>(),
159159
[&, this](const MetricsTasks<model::CppFunctionBumpyRoad>& tasks)
160160
{
161161
util::OdbTransaction {_ctx.db} ([&, this]
@@ -184,27 +184,19 @@ void CppMetricsParser::typeMcCabe()
184184
using AstNodeMet = model::CppAstNodeMetrics;
185185

186186
// Calculate type level McCabe metric for all types on parallel threads.
187-
parallelCalcMetric<AstNode>(
187+
parallelCalcMetric<model::CohesionCppRecordView>(
188188
"Type-level McCabe",
189189
_threadCount * typeMcCabePartitionMultiplier,// number of jobs; adjust for granularity
190-
odb::query<AstNode>::symbolType == AstNode::SymbolType::Type &&
191-
odb::query<AstNode>::astType == AstNode::AstType::Definition,
192-
[&, this](const MetricsTasks<AstNode>& tasks)
190+
getCohesionRecordQuery(),
191+
[&, this](const MetricsTasks<model::CohesionCppRecordView>& tasks)
193192
{
194193
util::OdbTransaction {_ctx.db} ([&, this]
195194
{
196-
for (const AstNode& type : tasks)
195+
for (const model::CohesionCppRecordView& type : tasks)
197196
{
198-
// Skip if class is included from external library
199-
type.location.file.load();
200-
const auto typeFile = _ctx.db->query_one<model::File>(
201-
odb::query<model::File>::id == type.location.file->id);
202-
if (!typeFile || !cc::util::isRootedUnderAnyOf(_inputPaths, typeFile->path))
203-
continue;
204-
205197
// Skip if its a template instantiation
206198
const auto typeEntity = _ctx.db->query_one<Entity>(
207-
odb::query<Entity>::astNodeId == type.id);
199+
odb::query<Entity>::astNodeId == type.astNodeId);
208200
if (typeEntity && typeEntity->tags.find(model::Tag::TemplateInstantiation)
209201
!= typeEntity->tags.cend())
210202
continue;
@@ -256,7 +248,7 @@ void CppMetricsParser::typeMcCabe()
256248
}
257249

258250
model::CppAstNodeMetrics typeMcMetric;
259-
typeMcMetric.astNodeId = type.id;
251+
typeMcMetric.astNodeId = type.astNodeId;
260252
typeMcMetric.type = model::CppAstNodeMetrics::Type::MCCABE_TYPE;
261253
typeMcMetric.value = value;
262254
_ctx.db->persist(typeMcMetric);

0 commit comments

Comments
 (0)