@@ -95,23 +95,27 @@ std::string Config::command() const
95
95
96
96
cmd += " \" " + m_cppcheck + " \" " ;
97
97
98
- for (const auto &arg : m_args) {
99
- // If arg contains double quotes, escape them
100
- std::string escapedArg = arg;
101
- size_t pos = 0 ;
102
- while ((pos = escapedArg.find (" \" " , pos)) != std::string::npos) {
103
- escapedArg.replace (pos, 1 , " \\\" " );
104
- pos += 2 ;
105
- }
106
- cmd += " \" " + escapedArg + " \" " ;
98
+ if (m_printVersion) {
99
+ cmd += " \" --version\" " ;
100
+ } else {
101
+ for (const auto &arg : m_args) {
102
+ // If arg contains double quotes, escape them
103
+ std::string escapedArg = arg;
104
+ size_t pos = 0 ;
105
+ while ((pos = escapedArg.find (" \" " , pos)) != std::string::npos) {
106
+ escapedArg.replace (pos, 1 , " \\\" " );
107
+ pos += 2 ;
108
+ }
109
+ cmd += " \" " + escapedArg + " \" " ;
107
110
108
- }
111
+ }
109
112
110
113
111
- if (!m_projectFilePath.empty ()) {
112
- cmd += " \" --project=" + m_projectFilePath.string () + " \" \" --file-filter=" + m_filename.string () + " \" " ;
113
- } else {
114
- cmd += " \" " + m_filename.string () + " \" " ;
114
+ if (!m_projectFilePath.empty ()) {
115
+ cmd += " \" --project=" + m_projectFilePath.string () + " \" \" --file-filter=" + m_filename.string () + " \" " ;
116
+ } else {
117
+ cmd += " \" " + m_filename.string () + " \" " ;
118
+ }
115
119
}
116
120
117
121
cmd += " 2>&1" ;
@@ -185,6 +189,15 @@ std::string Config::matchFilenameFromCompileCommand()
185
189
return " " ;
186
190
}
187
191
192
+ static bool fileExists (const std::filesystem::path &path)
193
+ {
194
+ #ifdef _WIN32
195
+ return !_access (path.string ().c_str (), 0 );
196
+ #else
197
+ return !access (path.string ().c_str (), F_OK);
198
+ #endif
199
+ }
200
+
188
201
std::string Config::parseArgs (int argc, char **argv)
189
202
{
190
203
(void ) argc;
@@ -204,6 +217,11 @@ std::string Config::parseArgs(int argc, char **argv)
204
217
continue ;
205
218
}
206
219
220
+ if (std::string (arg) == " --version" ) {
221
+ m_printVersion = true ;
222
+ continue ;
223
+ }
224
+
207
225
if (arg[0 ] == ' -' ) {
208
226
m_args.push_back (arg);
209
227
continue ;
@@ -215,35 +233,51 @@ std::string Config::parseArgs(int argc, char **argv)
215
233
m_filename = arg;
216
234
}
217
235
218
- if (m_filename.empty ())
219
- return " Missing filename" ;
236
+ if (!m_printVersion) {
237
+ if (m_filename.empty ())
238
+ return " Missing filename" ;
220
239
221
- if (m_logFilePath.empty ()) {
222
- const std::string error = getDefaultLogFilePath (m_logFilePath);
223
- if (!error.empty ())
224
- return error;
225
- }
226
- if (m_configPath.empty ())
227
- m_configPath = findFile (m_filename, " run-cppcheck-config.json" );
240
+ if (m_logFilePath.empty ()) {
241
+ const std::string error = getDefaultLogFilePath (m_logFilePath);
242
+ if (!error.empty ())
243
+ return error;
244
+ }
228
245
229
- if (m_configPath.empty ())
230
- return " Failed to find ' run-cppcheck-config.json' in any parent directory of analyzed file " ;
246
+ if (m_configPath.empty ())
247
+ m_configPath = findFile (m_filename, " run-cppcheck-config.json" ) ;
231
248
232
- std::string err = load (m_configPath);
233
- if (!err.empty ())
234
- return " Failed to load '" + m_configPath.string () + " ': " + err;
249
+ if (m_configPath.empty ())
250
+ return " Failed to find 'run-cppcheck-config.json' in any parent directory of analyzed file" ;
235
251
236
- if (!m_projectFilePath.empty () && m_projectFilePath.is_relative ())
237
- m_projectFilePath = m_configPath.parent_path () / m_projectFilePath;
252
+ std::string err = load (m_configPath);
253
+ if (!err.empty ())
254
+ return " Failed to load '" + m_configPath.string () + " ': " + err;
238
255
239
- if (m_filename .is_relative ())
240
- m_filename = normalizePath ( std::filesystem::current_path ( ) / m_filename) ;
256
+ if (!m_projectFilePath. empty () && m_projectFilePath .is_relative ())
257
+ m_projectFilePath = m_configPath. parent_path ( ) / m_projectFilePath ;
241
258
242
- err = matchFilenameFromCompileCommand ();
259
+ if (m_filename.is_relative ())
260
+ m_filename = normalizePath (std::filesystem::current_path () / m_filename);
243
261
244
- // Only warn if compile_commands.json is corrupted
245
- if (!err.empty ())
246
- return " Failed to process compile_commands.json: " + err;
262
+ err = matchFilenameFromCompileCommand ();
263
+
264
+ // Only warn if compile_commands.json is corrupted
265
+ if (!err.empty ())
266
+ return " Failed to process compile_commands.json: " + err;
267
+ } else {
268
+
269
+ // If --version is used there is no input file, so check for config
270
+ // in current working directory
271
+ if (m_configPath.empty ())
272
+ m_configPath = std::filesystem::current_path () / " run-cppcheck-config.json" ;
273
+
274
+ if (!fileExists (m_configPath))
275
+ return " " ;
276
+
277
+ std::string err = load (m_configPath);
278
+ if (!err.empty ())
279
+ return " Failed to load '" + m_configPath.string () + " ': " + err;
280
+ }
247
281
248
282
return " " ;
249
283
}
0 commit comments