@@ -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}
0 commit comments