Skip to content

Commit 4b412e7

Browse files
authored
search for config file in parent directories of input file (#2)
1 parent 0b1bd10 commit 4b412e7

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

config.cpp

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ std::string Config::load(const std::filesystem::path &path)
2424
if (ifs.fail())
2525
return std::strerror(errno);
2626

27-
std::string text = buffer.data();
27+
const std::string text = buffer.data();
2828

2929
// Parse JSON
3030
picojson::value data;
31-
std::string err = picojson::parse(data, text);
31+
const std::string err = picojson::parse(data, text);
3232
if (!err.empty()) {
3333
return err;
3434
}
@@ -39,7 +39,7 @@ std::string Config::load(const std::filesystem::path &path)
3939
const picojson::object &obj = data.get<picojson::object>();
4040

4141
// Read settings
42-
for (auto [key, value] : obj) {
42+
for (const auto &[key, value] : obj) {
4343

4444
if (key == "project_file") {
4545
if (!value.is<std::string>()) {
@@ -81,7 +81,7 @@ std::string Config::command() const
8181

8282
cmd += m_cppcheck;
8383

84-
for (auto arg : m_args)
84+
for (const auto &arg : m_args)
8585
cmd += " " + arg;
8686

8787
if (!m_projectFilePath.empty()) {
@@ -93,7 +93,7 @@ std::string Config::command() const
9393
cmd += " --project=" + m_projectFilePath.string() + " --file-filter=" + filter;
9494

9595
} else {
96-
cmd += " " + m_filename;
96+
cmd += " " + m_filename.string();
9797
}
9898

9999
cmd += " 2>&1";
@@ -113,14 +113,14 @@ std::string Config::parseArgs(int argc, char **argv)
113113

114114
++argv;
115115

116+
std::filesystem::path configPath = "";
117+
116118
for (; *argv; ++argv) {
117119
const char *arg = *argv;
118120
const char *value;
119121

120122
if ((value = startsWith(arg, "--config="))) {
121-
std::string err = load(value);
122-
if (!err.empty())
123-
return "Failed to load config file '" + std::string(value) + "': " + err;
123+
configPath = value;
124124
continue;
125125
}
126126

@@ -136,5 +136,34 @@ std::string Config::parseArgs(int argc, char **argv)
136136
if (m_filename.empty())
137137
return "Missing filename";
138138

139+
if (configPath.empty())
140+
configPath = findConfig(m_filename);
141+
142+
if (configPath.empty())
143+
return "Failed to find config file";
144+
145+
const std::string err = load(configPath);
146+
if (err.empty())
147+
return "";
148+
return "Failed to load '" + configPath.string() + "': " + err;
149+
}
150+
151+
// Find config file by recursively searching parent directories of input file
152+
std::filesystem::path Config::findConfig(const std::filesystem::path &input_path)
153+
{
154+
auto path = input_path;
155+
156+
if (path.is_relative())
157+
path = std::filesystem::current_path() / path;
158+
159+
do {
160+
path = path.parent_path();
161+
const auto config_path = path / "run-cppcheck-config.json";
162+
163+
if (std::filesystem::exists(config_path))
164+
return config_path;
165+
166+
} while (path != path.root_path());
167+
139168
return "";
140169
}

config.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class Config {
99
public:
1010
Config()
1111
: m_projectFilePath("")
12-
, m_cppcheck("cppcheck")
1312
, m_filename("")
13+
, m_cppcheck("cppcheck")
1414
, m_args({})
1515
{
1616
}
@@ -29,9 +29,11 @@ class Config {
2929
std::string parseArgs(int argc, char **argv);
3030

3131
private:
32+
static std::filesystem::path findConfig(const std::filesystem::path &input_path);
33+
3234
std::filesystem::path m_projectFilePath = "";
35+
std::filesystem::path m_filename;
3336
std::string m_cppcheck;
34-
std::string m_filename;
3537
std::vector<std::string> m_args;
3638
};
3739

0 commit comments

Comments
 (0)