diff --git a/plugins/cpp/model/include/model/cppfunction.h b/plugins/cpp/model/include/model/cppfunction.h index c136278a9..a30ca5a51 100644 --- a/plugins/cpp/model/include/model/cppfunction.h +++ b/plugins/cpp/model/include/model/cppfunction.h @@ -23,6 +23,7 @@ struct CppFunction : CppTypedEntity unsigned int mccabe; unsigned int bumpiness; unsigned int statementCount; + bool inLambdaObject = false; // Hash of the record (e.g. class or struct) in which this function is declared // recordHash = 0 means this is a global function diff --git a/plugins/cpp/parser/src/clangastvisitor.h b/plugins/cpp/parser/src/clangastvisitor.h index c0951f2d3..53a5dc1cf 100644 --- a/plugins/cpp/parser/src/clangastvisitor.h +++ b/plugins/cpp/parser/src/clangastvisitor.h @@ -940,6 +940,7 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor if (const clang::CXXRecordDecl* parent = md->getParent()) { cppFunction->recordHash = util::fnvHash(getUSR(parent)); + cppFunction->inLambdaObject = parent->isLambda(); } if (md->isVirtual()) diff --git a/plugins/cpp_metrics/parser/include/cppmetricsparser/cppmetricsparser.h b/plugins/cpp_metrics/parser/include/cppmetricsparser/cppmetricsparser.h index 63e1550c3..a43e27daf 100644 --- a/plugins/cpp_metrics/parser/include/cppmetricsparser/cppmetricsparser.h +++ b/plugins/cpp_metrics/parser/include/cppmetricsparser/cppmetricsparser.h @@ -94,6 +94,19 @@ class CppMetricsParser : public AbstractParser // Returns cohesion record query based on parser configuration. odb::query getCohesionRecordQuery(); + // Returns metric function query based on parser configuration. + template + odb::query getFunctionQuery() const + { + odb::query query = getFilterPathsQuery(); + + if (_ctx.options.count("cppmetrics-ignore-lambdas")) { + query = query && odb::query::inLambdaObject == false; + } + + return query; + } + /// @brief Constructs an ODB query that you can use to filter only /// the database records of the given parameter type whose path /// is rooted under any of this parser's input paths. diff --git a/plugins/cpp_metrics/parser/src/cppmetricsparser.cpp b/plugins/cpp_metrics/parser/src/cppmetricsparser.cpp index 35944c256..965709fde 100644 --- a/plugins/cpp_metrics/parser/src/cppmetricsparser.cpp +++ b/plugins/cpp_metrics/parser/src/cppmetricsparser.cpp @@ -132,7 +132,7 @@ void CppMetricsParser::functionMcCabe() parallelCalcMetric( "Function-level McCabe", _threadCount * functionMcCabePartitionMultiplier,// number of jobs; adjust for granularity - getFilterPathsQuery(), + getFunctionQuery(), [&, this](const MetricsTasks& tasks) { util::OdbTransaction {_ctx.db} ([&, this] @@ -155,7 +155,7 @@ void CppMetricsParser::functionBumpyRoad() parallelCalcMetric( "Bumpy road complexity", _threadCount * functionBumpyRoadPartitionMultiplier,// number of jobs; adjust for granularity - getFilterPathsQuery(), + getFunctionQuery(), [&, this](const MetricsTasks& tasks) { util::OdbTransaction {_ctx.db} ([&, this] @@ -184,27 +184,19 @@ void CppMetricsParser::typeMcCabe() using AstNodeMet = model::CppAstNodeMetrics; // Calculate type level McCabe metric for all types on parallel threads. - parallelCalcMetric( + parallelCalcMetric( "Type-level McCabe", _threadCount * typeMcCabePartitionMultiplier,// number of jobs; adjust for granularity - odb::query::symbolType == AstNode::SymbolType::Type && - odb::query::astType == AstNode::AstType::Definition, - [&, this](const MetricsTasks& tasks) + getCohesionRecordQuery(), + [&, this](const MetricsTasks& tasks) { util::OdbTransaction {_ctx.db} ([&, this] { - for (const AstNode& type : tasks) + for (const model::CohesionCppRecordView& type : tasks) { - // Skip if class is included from external library - 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; - // Skip if its a template instantiation const auto typeEntity = _ctx.db->query_one( - odb::query::astNodeId == type.id); + odb::query::astNodeId == type.astNodeId); if (typeEntity && typeEntity->tags.find(model::Tag::TemplateInstantiation) != typeEntity->tags.cend()) continue; @@ -256,7 +248,7 @@ void CppMetricsParser::typeMcCabe() } model::CppAstNodeMetrics typeMcMetric; - typeMcMetric.astNodeId = type.id; + typeMcMetric.astNodeId = type.astNodeId; typeMcMetric.type = model::CppAstNodeMetrics::Type::MCCABE_TYPE; typeMcMetric.value = value; _ctx.db->persist(typeMcMetric);