Skip to content

Commit

Permalink
isInInputPath is generalized and moved to cc::util::isRootedUnderAnyOf.
Browse files Browse the repository at this point in the history
Fixed the casing and naming of debug utility variables in the LoC parser.
  • Loading branch information
dbukki committed Dec 11, 2023
1 parent b0dcc41 commit adf2c44
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ class CppMetricsParser : public AbstractParser
// and member functions for every type.
void lackOfCohesion();

// Checks if the given (canonical) path begins with any of the input paths
// specified for the metrics parser via command line arguments on startup.
bool isInInputPath(const std::string& path_) const;
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
39 changes: 16 additions & 23 deletions plugins/cpp_metrics/parser/src/cppmetricsparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <boost/filesystem.hpp>

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

#include <memory>
Expand Down Expand Up @@ -141,33 +142,33 @@ void CppMetricsParser::lackOfCohesion()
const auto& QNodeRange = QNode::CppAstNode::location.range;

#ifdef DEBUG_COHESION_VERBOSE
std::size_t typecount =
std::size_t typeCount =
_ctx.db->query_value<model::CppRecordCount>().count;
std::size_t typeindex = 0;
std::size_t checkedcount = 0;
std::size_t typeIndex = 0;
std::size_t checkedCount = 0;

LOG(debug) << "=== Lack of Cohesion (LoC) metrics parser ===";
int colwidth = static_cast<int>(ceil(log10(typecount)));
auto start = std::chrono::steady_clock::now();
int colWidth = static_cast<int>(ceil(log10(typeCount)));
auto startTime = std::chrono::steady_clock::now();
#endif

// Calculate the cohesion metric for all types.
for (const model::CohesionCppRecordView& type
: _ctx.db->query<model::CohesionCppRecordView>())
{
#ifdef DEBUG_COHESION_VERBOSE
++typeindex;
++typeIndex;
#endif

// Skip types that were included from external libraries.
if (!isInInputPath(type.filePath))
if (!cc::util::isRootedUnderAnyOf(_inputPaths, type.filePath))
continue;

#ifdef DEBUG_COHESION_VERBOSE
++checkedcount;
++checkedCount;
std::ostringstream logLine;
logLine << std::right << std::setw(colwidth) << typeindex << '/';
logLine << std::left << std::setw(colwidth) << typecount << '\t';
logLine << std::right << std::setw(colWidth) << typeIndex << '/';
logLine << std::left << std::setw(colWidth) << typeCount << '\t';
logLine << std::left << std::setw(32) << type.qualifiedName;
#endif

Expand Down Expand Up @@ -254,23 +255,15 @@ void CppMetricsParser::lackOfCohesion()
}

#ifdef DEBUG_COHESION_VERBOSE
auto finish = std::chrono::steady_clock::now();
auto duration = finish - start;
auto durs = std::chrono::duration_cast<std::chrono::seconds>(duration);
LOG(debug) << "=== Checked types: " << checkedcount
<< ", Total runtime: " << durs.count() << "s ===";
auto finishTime = std::chrono::steady_clock::now();
auto durTime = finishTime - startTime;
auto durSecs = std::chrono::duration_cast<std::chrono::seconds>(durTime);
LOG(debug) << "=== Checked types: " << checkedCount
<< ", Total runtime: " << durSecs.count() << "s ===";
#endif
});
}

bool CppMetricsParser::isInInputPath(const std::string& path_) const
{
std::size_t i = 0;
while (i < _inputPaths.size() && path_.rfind(_inputPaths[i], 0) != 0)
++i;
return i < _inputPaths.size();
}

bool CppMetricsParser::parse()
{
functionParameters();
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

0 comments on commit adf2c44

Please sign in to comment.