@@ -453,46 +453,15 @@ static std::string detectPython(const CppCheck::ExecuteCmdFn &executeCommand)
453453/* *
454454 * @throws InternalError thrown when execution fails
455455 */
456- static std::vector<picojson::value> executeAddon (const AddonInfo &addonInfo,
457- const std::string &defaultPythonExe,
458- const std::string &file,
459- const std::string &premiumArgs,
460- const CppCheck::ExecuteCmdFn &executeCommand)
456+ static std::vector<picojson::value> runAddonCommand (const std::string &addonName,
457+ const std::string &exe,
458+ const std::string &args,
459+ const CppCheck::ExecuteCmdFn &executeCommand)
461460{
462- if (!executeCommand)
463- throw InternalError (nullptr , " Failed to execute addon - no command callback provided" );
464-
465- std::string pythonExe;
466-
467- if (!addonInfo.executable .empty ())
468- pythonExe = addonInfo.executable ;
469- else if (!addonInfo.python .empty ())
470- pythonExe = cmdFileName (addonInfo.python );
471- else if (!defaultPythonExe.empty ())
472- pythonExe = cmdFileName (defaultPythonExe);
473- else {
474- // store in static variable so we only look this up once - TODO: do not cache globally
475- static const std::string detectedPythonExe = detectPython (executeCommand);
476- if (detectedPythonExe.empty ())
477- throw InternalError (nullptr , " Failed to auto detect python" );
478- pythonExe = detectedPythonExe;
479- }
480-
481- std::string args;
482- if (addonInfo.executable .empty ())
483- args = cmdFileName (addonInfo.runScript ) + " " + cmdFileName (addonInfo.scriptFile );
484- args += std::string (args.empty () ? " " : " " ) + " --cli" + addonInfo.args ;
485- if (!premiumArgs.empty () && !addonInfo.executable .empty ())
486- args += " " + premiumArgs;
487-
488- const bool is_file_list = (file.find (FILELIST ) != std::string::npos);
489- const std::string fileArg = (is_file_list ? " --file-list " : " " ) + cmdFileName (file);
490- args += fileArg;
491-
492461 std::string result;
493- if (const int exitcode = executeCommand (pythonExe , split (args), " 2>&1" , result)) {
494- std::string message (" Failed to execute addon '" + addonInfo. name + " ' - exitcode is " + std::to_string (exitcode));
495- std::string details = pythonExe + " " + args;
462+ if (const int exitcode = executeCommand (exe , split (args), " 2>&1" , result)) {
463+ std::string message (" Failed to execute addon '" + addonName + " ' - exitcode is " + std::to_string (exitcode));
464+ std::string details = exe + " " + args;
496465 if (result.size () > 2 ) {
497466 details += " \n Output:\n " ;
498467 details += result;
@@ -511,34 +480,34 @@ static std::vector<picojson::value> executeAddon(const AddonInfo &addonInfo,
511480 while (std::getline (istr, line)) {
512481 // TODO: also bail out?
513482 if (line.empty ()) {
514- // std::cout << "addon '" << addonInfo.name << "' result contains empty line" << std::endl;
483+ // std::cout << "addon '" << addonName << "' result contains empty line" << std::endl;
515484 continue ;
516485 }
517486
518487 // TODO: get rid of this
519488 if (startsWith (line," Checking " )) {
520- // std::cout << "addon '" << addonInfo.name << "' result contains 'Checking ' line" << std::endl;
489+ // std::cout << "addon '" << addonName << "' result contains 'Checking ' line" << std::endl;
521490 continue ;
522491 }
523492
524493 if (line[0 ] != ' {' ) {
525- // std::cout << "addon '" << addonInfo.name << "' result is not a JSON" << std::endl;
494+ // std::cout << "addon '" << addonName << "' result is not a JSON" << std::endl;
526495
527496 result.erase (result.find_last_not_of (' \n ' ) + 1 , std::string::npos); // Remove trailing newlines
528- throw InternalError (nullptr , " Failed to execute '" + pythonExe + " " + args + " '. " + result);
497+ throw InternalError (nullptr , " Failed to execute '" + exe + " " + args + " '. " + result);
529498 }
530499
531- // std::cout << "addon '" << addonInfo.name << "' result is " << line << std::endl;
500+ // std::cout << "addon '" << addonName << "' result is " << line << std::endl;
532501
533502 // TODO: make these failures?
534503 picojson::value res;
535504 const std::string err = picojson::parse (res, line);
536505 if (!err.empty ()) {
537- // std::cout << "addon '" << addonInfo.name << "' result is not a valid JSON (" << err << ")" << std::endl;
506+ // std::cout << "addon '" << addonName << "' result is not a valid JSON (" << err << ")" << std::endl;
538507 continue ;
539508 }
540509 if (!res.is <picojson::object>()) {
541- // std::cout << "addon '" << addonInfo.name << "' result is not a JSON object" << std::endl;
510+ // std::cout << "addon '" << addonName << "' result is not a JSON object" << std::endl;
542511 continue ;
543512 }
544513 addonResult.emplace_back (std::move (res));
@@ -548,6 +517,48 @@ static std::vector<picojson::value> executeAddon(const AddonInfo &addonInfo,
548517 return addonResult;
549518}
550519
520+ /* *
521+ * @throws InternalError thrown when execution fails
522+ */
523+ static std::vector<picojson::value> executeAddon (const AddonInfo &addonInfo,
524+ const std::string &defaultPythonExe,
525+ const std::string &file,
526+ const std::string &premiumArgs,
527+ const CppCheck::ExecuteCmdFn &executeCommand)
528+ {
529+ if (!executeCommand)
530+ throw InternalError (nullptr , " Failed to execute addon - no command callback provided" );
531+
532+ std::string pythonExe;
533+
534+ if (!addonInfo.executable .empty ())
535+ pythonExe = addonInfo.executable ;
536+ else if (!addonInfo.python .empty ())
537+ pythonExe = cmdFileName (addonInfo.python );
538+ else if (!defaultPythonExe.empty ())
539+ pythonExe = cmdFileName (defaultPythonExe);
540+ else {
541+ // store in static variable so we only look this up once - TODO: do not cache globally
542+ static const std::string detectedPythonExe = detectPython (executeCommand);
543+ if (detectedPythonExe.empty ())
544+ throw InternalError (nullptr , " Failed to auto detect python" );
545+ pythonExe = detectedPythonExe;
546+ }
547+
548+ std::string args;
549+ if (addonInfo.executable .empty ())
550+ args = cmdFileName (addonInfo.runScript ) + " " + cmdFileName (addonInfo.scriptFile );
551+ args += std::string (args.empty () ? " " : " " ) + " --cli" + addonInfo.args ;
552+ if (!premiumArgs.empty () && !addonInfo.executable .empty ())
553+ args += " " + premiumArgs;
554+
555+ const bool is_file_list = (file.find (FILELIST ) != std::string::npos);
556+ const std::string fileArg = (is_file_list ? " --file-list " : " " ) + cmdFileName (file);
557+ args += fileArg;
558+
559+ return runAddonCommand (addonInfo.name , pythonExe, args, executeCommand);
560+ }
561+
551562static std::string getDefinesFlags (const std::string &semicolonSeparatedString)
552563{
553564 std::string flags;
@@ -1805,6 +1816,25 @@ void CppCheck::getErrorMessages(ErrorLogger &errorlogger)
18051816 SymbolDatabase::getErrorMessages (errorlogger);
18061817}
18071818
1819+ void CppCheck::getAddonErrorMessages (const AddonInfo &addonInfo, ErrorLogger &errorlogger, const ExecuteCmdFn &executeCommand)
1820+ {
1821+ if (!executeCommand)
1822+ throw InternalError (nullptr , " Failed to execute addon - no command callback provided" );
1823+
1824+ const std::vector<picojson::value> results = runAddonCommand (addonInfo.name , addonInfo.executable , " --cli --errorlist" , executeCommand);
1825+
1826+ for (const picojson::value& res : results) {
1827+ // this is a copy so we can access missing fields and get a default value
1828+ picojson::object obj = res.get <picojson::object>();
1829+
1830+ ErrorMessage errmsg;
1831+ errmsg.id = obj[" addon" ].get <std::string>() + " -" + obj[" errorId" ].get <std::string>();
1832+ errmsg.setmsg (obj[" message" ].get <std::string>());
1833+ errmsg.severity = severityFromString (obj[" severity" ].get <std::string>());
1834+ errorlogger.reportErr (errmsg);
1835+ }
1836+ }
1837+
18081838void CppCheck::analyseClangTidy (const FileSettings &fileSettings)
18091839{
18101840 std::string allIncludes;
0 commit comments