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

Persist C++ metrics only for files of the analyzed project #709

Merged
merged 1 commit into from
Feb 4, 2024
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
25 changes: 20 additions & 5 deletions plugins/cpp/model/include/model/cppfunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,21 @@ struct CppFunctionParamCount
};

#pragma db view \
object(CppFunction) object(CppVariable = Parameters : CppFunction::parameters) \
query((?) + "GROUP BY" + cc::model::CppEntity::astNodeId)
object(CppFunction) \
object(CppVariable = Parameters : CppFunction::parameters) \
object(CppAstNode : CppFunction::astNodeId == CppAstNode::id) \
object(File : CppAstNode::location.file) \
query((?) + "GROUP BY" + cc::model::CppEntity::astNodeId + "," + cc::model::File::path)
struct CppFunctionParamCountWithId
{
#pragma db column("CppEntity.astNodeId")
#pragma db column(CppEntity::astNodeId)
CppAstNodeId id;

#pragma db column("count(" + Parameters::id + ")")
std::size_t count;

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

#pragma db view \
Expand All @@ -60,11 +66,20 @@ struct CppFunctionLocalCount
std::size_t count;
};

#pragma db view object(CppFunction)
struct CppFunctionMcCabeWithId
#pragma db view \
object(CppFunction) \
object(CppAstNode : CppFunction::astNodeId == CppAstNode::id) \
object(File : CppAstNode::location.file)
struct CppFunctionMcCabe
{
#pragma db column(CppEntity::astNodeId)
CppAstNodeId astNodeId;

#pragma db column(CppFunction::mccabe)
unsigned int mccabe;

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

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CppMetricsParser : public AbstractParser
void functionParameters();
void functionMcCabe();

std::vector<std::string> _inputPaths;
std::unordered_set<model::FileId> _fileIdCache;
std::unordered_map<model::CppAstNodeId, model::FileId> _astNodeIdCache;
std::unique_ptr<util::JobQueueThreadPool<std::string>> _pool;
Expand Down
19 changes: 17 additions & 2 deletions plugins/cpp_metrics/parser/src/cppmetricsparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <boost/filesystem.hpp>

#include <util/filesystem.h>
#include <util/logutil.h>
#include <util/odbtransaction.h>

Expand All @@ -20,8 +21,14 @@ namespace cc
namespace parser
{

namespace fs = boost::filesystem;

CppMetricsParser::CppMetricsParser(ParserContext& ctx_): AbstractParser(ctx_)
{
for (const std::string& path :
_ctx.options["input"].as<std::vector<std::string>>())
_inputPaths.push_back(fs::canonical(path).string());

util::OdbTransaction {_ctx.db} ([&, this] {
for (const model::CppFileMetrics& fm
: _ctx.db->query<model::CppFileMetrics>())
Expand Down Expand Up @@ -98,6 +105,10 @@ void CppMetricsParser::functionParameters()
for (const model::CppFunctionParamCountWithId& paramCount
: _ctx.db->query<model::CppFunctionParamCountWithId>())
{
// Skip functions that were included from external libraries.
if (!cc::util::isRootedUnderAnyOf(_inputPaths, paramCount.filePath))
continue;

model::CppAstNodeMetrics funcParams;
funcParams.astNodeId = paramCount.id;
funcParams.type = model::CppAstNodeMetrics::Type::PARAMETER_COUNT;
Expand All @@ -111,9 +122,13 @@ void CppMetricsParser::functionMcCabe()
{
util::OdbTransaction {_ctx.db} ([&, this]
{
for (const model::CppFunctionMcCabeWithId& function
: _ctx.db->query<model::CppFunctionMcCabeWithId>())
for (const model::CppFunctionMcCabe& function
: _ctx.db->query<model::CppFunctionMcCabe>())
{
// Skip functions that were included from external libraries.
if (!cc::util::isRootedUnderAnyOf(_inputPaths, function.filePath))
continue;

model::CppAstNodeMetrics funcMcCabe;
funcMcCabe.astNodeId = function.astNodeId;
funcMcCabe.type = model::CppAstNodeMetrics::Type::MCCABE;
Expand Down
13 changes: 13 additions & 0 deletions util/include/util/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ std::string binaryPathToInstallDir(const char* path);
*/
std::string findCurrentExecutableDir();

/**
* @brief Determines if the given path is rooted under
* any of the given other paths.
*
* @param paths_ A list of canonical paths.
* @param path_ A canonical path to match against the given paths.
* @return True if any of the paths in paths_ is a prefix of path_,
* otherwise false.
*/
bool isRootedUnderAnyOf(
const std::vector<std::string>& paths_,
const std::string& path_);

} // namespace util
} // namespace cc

Expand Down
11 changes: 11 additions & 0 deletions util/src/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,16 @@ std::string findCurrentExecutableDir()
return fs::path(exePath).parent_path().string();
}

bool isRootedUnderAnyOf(
const std::vector<std::string>& paths_,
const std::string& path_)
{
auto it = paths_.begin();
const auto end = paths_.end();
while (it != end && path_.rfind(*it, 0) != 0)
++it;
return it != end;
}

} // namespace util
} // namespace cc
Loading