From 530748bcad263fc2638d67b590c39607c45453b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20Domozi?= Date: Mon, 10 Mar 2025 17:14:20 +0100 Subject: [PATCH 1/2] Added --modules/-m flag. --- parser/include/parser/parsercontext.h | 3 ++- parser/src/parser.cpp | 6 +++++- parser/src/parsercontext.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/parser/include/parser/parsercontext.h b/parser/include/parser/parsercontext.h index 1b7928526..d576d1809 100644 --- a/parser/include/parser/parsercontext.h +++ b/parser/include/parser/parsercontext.h @@ -3,9 +3,9 @@ #include #include +#include #include - #include namespace po = boost::program_options; @@ -43,6 +43,7 @@ struct ParserContext std::string& compassRoot; po::variables_map& options; std::unordered_map fileStatus; + std::vector moduleDirectories; }; } // parser diff --git a/parser/src/parser.cpp b/parser/src/parser.cpp index 93cb0362b..50ae1997d 100644 --- a/parser/src/parser.cpp +++ b/parser/src/parser.cpp @@ -85,7 +85,11 @@ po::options_description commandLineArguments() "further actions modifying the state of the database.") ("incremental-threshold", po::value()->default_value(10), "This is a threshold percentage. If the total ratio of changed files " - "is greater than this value, full parse is forced instead of incremental parsing."); + "is greater than this value, full parse is forced instead of incremental parsing.") + ("modules,m", po::value(), + "For metrics calculations, you can specify the project's (sub)module structure." + "Provide the path of a text file for this setting." + "The file should contain directory paths, each on a separate line, which will be considered modules."); return desc; } diff --git a/parser/src/parsercontext.cpp b/parser/src/parsercontext.cpp index 937cbfa5e..51f2d1b3d 100644 --- a/parser/src/parsercontext.cpp +++ b/parser/src/parsercontext.cpp @@ -12,6 +12,7 @@ #include namespace po = boost::program_options; +namespace fs = boost::filesystem; namespace cc { @@ -76,6 +77,31 @@ ParserContext::ParserContext( // TODO: detect ADDED files }); + + // Fill moduleDirectories vector + if (options.count("modules")) { + const std::string& modulesFilePath = options["modules"].as(); + + std::ifstream fileStream(modulesFilePath); + + if (!fileStream.good()) { + LOG(error) << "Failed to open modules file: " << modulesFilePath; + exit(1); + } + + LOG(info) << "Processing modules file: " << modulesFilePath; + + std::string line; + while (std::getline(fileStream, line)) { + try { + const fs::path p = fs::canonical(line); + moduleDirectories.push_back(p.string()); + } catch (...) { + LOG(error) << "Failed to process path from modules file: " << line; + exit(1); + } + } + } } } } From d0c2e9497c8e2a3164da4d58b98b7d72322ba511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20Domozi?= Date: Tue, 8 Apr 2025 14:44:48 +0200 Subject: [PATCH 2/2] Do not exit if it fails to process a path from modules file --- parser/src/parsercontext.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/parser/src/parsercontext.cpp b/parser/src/parsercontext.cpp index 51f2d1b3d..3746e9b93 100644 --- a/parser/src/parsercontext.cpp +++ b/parser/src/parsercontext.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -86,7 +87,7 @@ ParserContext::ParserContext( if (!fileStream.good()) { LOG(error) << "Failed to open modules file: " << modulesFilePath; - exit(1); + return; } LOG(info) << "Processing modules file: " << modulesFilePath; @@ -96,9 +97,9 @@ ParserContext::ParserContext( try { const fs::path p = fs::canonical(line); moduleDirectories.push_back(p.string()); - } catch (...) { + } catch (fs::filesystem_error& err) { LOG(error) << "Failed to process path from modules file: " << line; - exit(1); + LOG(error) << err.what(); } } }