Skip to content

Commit 93cc594

Browse files
Add module configuration support for C++ metrics (#782)
Added --modules/-m flag.
1 parent be4893b commit 93cc594

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

parser/include/parser/parsercontext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
#include <memory>
55
#include <unordered_map>
6+
#include <vector>
67

78
#include <boost/program_options.hpp>
8-
99
#include <odb/database.hxx>
1010

1111
namespace po = boost::program_options;
@@ -43,6 +43,7 @@ struct ParserContext
4343
std::string& compassRoot;
4444
po::variables_map& options;
4545
std::unordered_map<std::string, IncrementalStatus> fileStatus;
46+
std::vector<std::string> moduleDirectories;
4647
};
4748

4849
} // parser

parser/src/parser.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ po::options_description commandLineArguments()
8585
"further actions modifying the state of the database.")
8686
("incremental-threshold", po::value<int>()->default_value(10),
8787
"This is a threshold percentage. If the total ratio of changed files "
88-
"is greater than this value, full parse is forced instead of incremental parsing.");
88+
"is greater than this value, full parse is forced instead of incremental parsing.")
89+
("modules,m", po::value<std::string>(),
90+
"For metrics calculations, you can specify the project's (sub)module structure."
91+
"Provide the path of a text file for this setting."
92+
"The file should contain directory paths, each on a separate line, which will be considered modules.");
8993

9094
return desc;
9195
}

parser/src/parsercontext.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <boost/filesystem/exception.hpp>
12
#include <fstream>
23

34
#include <boost/filesystem.hpp>
@@ -12,6 +13,7 @@
1213
#include <parser/sourcemanager.h>
1314

1415
namespace po = boost::program_options;
16+
namespace fs = boost::filesystem;
1517

1618
namespace cc
1719
{
@@ -76,6 +78,31 @@ ParserContext::ParserContext(
7678

7779
// TODO: detect ADDED files
7880
});
81+
82+
// Fill moduleDirectories vector
83+
if (options.count("modules")) {
84+
const std::string& modulesFilePath = options["modules"].as<std::string>();
85+
86+
std::ifstream fileStream(modulesFilePath);
87+
88+
if (!fileStream.good()) {
89+
LOG(error) << "Failed to open modules file: " << modulesFilePath;
90+
return;
91+
}
92+
93+
LOG(info) << "Processing modules file: " << modulesFilePath;
94+
95+
std::string line;
96+
while (std::getline(fileStream, line)) {
97+
try {
98+
const fs::path p = fs::canonical(line);
99+
moduleDirectories.push_back(p.string());
100+
} catch (fs::filesystem_error& err) {
101+
LOG(error) << "Failed to process path from modules file: " << line;
102+
LOG(error) << err.what();
103+
}
104+
}
105+
}
79106
}
80107
}
81108
}

0 commit comments

Comments
 (0)