@@ -324,8 +324,37 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
324324 // default to --check-level=normal from CLI for now
325325 mSettings .setCheckLevel (Settings::CheckLevel::normal);
326326
327+ // read --debug-lookup early so the option is available for the cppcheck.cfg loading
328+ for (int i = 1 ; i < argc; i++) {
329+ // Show debug warnings for lookup for configuration files
330+ if (std::strcmp (argv[i], " --debug-lookup" ) == 0 )
331+ mSettings .debuglookup = true ;
332+
333+ else if (std::strncmp (argv[i], " --debug-lookup=" , 15 ) == 0 ) {
334+ const std::string lookup = argv[i] + 15 ;
335+ if (lookup == " all" )
336+ mSettings .debuglookup = true ;
337+ else if (lookup == " addon" )
338+ mSettings .debuglookupAddon = true ;
339+ else if (lookup == " config" )
340+ mSettings .debuglookupConfig = true ;
341+ else if (lookup == " library" )
342+ mSettings .debuglookupLibrary = true ;
343+ else if (lookup == " platform" )
344+ mSettings .debuglookupPlatform = true ;
345+ else
346+ {
347+ mLogger .printError (" unknown lookup '" + lookup + " '" );
348+ return Result::Fail;
349+ }
350+ }
351+ }
352+
353+ if (!loadCppcheckCfg ())
354+ return Result::Fail;
355+
327356 if (argc <= 1 ) {
328- printHelp ();
357+ printHelp (mSettings . premium );
329358 return Result::Exit;
330359 }
331360
@@ -349,8 +378,6 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
349378
350379 // print all possible error messages..
351380 if (std::strcmp (argv[i], " --errorlist" ) == 0 ) {
352- if (!loadCppcheckCfg ())
353- return Result::Fail;
354381 {
355382 XMLErrorMessagesLogger xmlLogger;
356383 std::cout << ErrorMessage::getXMLHeader (mSettings .cppcheckCfgProductName , 2 );
@@ -362,7 +389,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
362389
363390 // Print help
364391 if (std::strcmp (argv[i], " -h" ) == 0 || std::strcmp (argv[i], " --help" ) == 0 ) {
365- printHelp ();
392+ printHelp (mSettings . premium );
366393 return Result::Exit;
367394 }
368395
@@ -374,8 +401,6 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
374401 }
375402
376403 if (std::strcmp (argv[i], " --version" ) == 0 ) {
377- if (!loadCppcheckCfg ())
378- return Result::Fail;
379404 const std::string version = getVersion ();
380405 mLogger .printRaw (version); // TODO: should not include newline
381406 return Result::Exit;
@@ -617,28 +642,11 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
617642 std::strcmp (argv[i], " --debug-normal" ) == 0 )
618643 debug = true ;
619644
620- // Show debug warnings for lookup for configuration files
621645 else if (std::strcmp (argv[i], " --debug-lookup" ) == 0 )
622- mSettings . debuglookup = true ;
646+ continue ; // already handled above
623647
624- else if (std::strncmp (argv[i], " --debug-lookup=" , 15 ) == 0 ) {
625- const std::string lookup = argv[i] + 15 ;
626- if (lookup == " all" )
627- mSettings .debuglookup = true ;
628- else if (lookup == " addon" )
629- mSettings .debuglookupAddon = true ;
630- else if (lookup == " config" )
631- mSettings .debuglookupConfig = true ;
632- else if (lookup == " library" )
633- mSettings .debuglookupLibrary = true ;
634- else if (lookup == " platform" )
635- mSettings .debuglookupPlatform = true ;
636- else
637- {
638- mLogger .printError (" unknown lookup '" + lookup + " '" );
639- return Result::Fail;
640- }
641- }
648+ else if (std::strncmp (argv[i], " --debug-lookup=" , 15 ) == 0 )
649+ continue ; // already handled above
642650
643651 // Flag used for various purposes during debugging
644652 else if (std::strcmp (argv[i], " --debug-simplified" ) == 0 )
@@ -1008,6 +1016,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
10081016 mSettings .cppHeaderProbe = false ;
10091017 }
10101018
1019+ else if (std::strcmp (argv[i], " --no-safety" ) == 0 )
1020+ mSettings .safety = false ;
1021+
10111022 // Write results in file
10121023 else if (std::strncmp (argv[i], " --output-file=" , 14 ) == 0 )
10131024 mSettings .outputFile = Path::simplifyPath (argv[i] + 14 );
@@ -1090,7 +1101,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
10901101 }
10911102
10921103 // Special Cppcheck Premium options
1093- else if ((std::strncmp (argv[i], " --premium=" , 10 ) == 0 || std::strncmp (argv[i], " --premium-" , 10 ) == 0 ) && isCppcheckPremium () ) {
1104+ else if ((std::strncmp (argv[i], " --premium=" , 10 ) == 0 || std::strncmp (argv[i], " --premium-" , 10 ) == 0 ) && mSettings . premium ) {
10941105 // valid options --premium=..
10951106 const std::set<std::string> valid{
10961107 " autosar" ,
@@ -1108,7 +1119,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11081119 " misra-c++-2023" ,
11091120 " misra-cpp-2023" ,
11101121 " bughunting" ,
1111- " safety" ,
1122+ " safety" , // TODO: deprecate in favor of the regular --saftey/--no-safety
11121123 " debug-progress" };
11131124 // valid options --premium-..=
11141125 const std::set<std::string> valid2{
@@ -1151,7 +1162,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11511162
11521163 mSettings .checkAllConfigurations = false ; // Can be overridden with --max-configs or --force
11531164 std::string projectFile = argv[i]+10 ;
1154- projectType = project.import (projectFile, &mSettings , &mSuppressions , isCppcheckPremium () );
1165+ projectType = project.import (projectFile, &mSettings , &mSuppressions );
11551166 if (projectType == ImportProject::Type::CPPCHECK_GUI) {
11561167 for (const std::string &lib : project.guiProject .libraries )
11571168 mSettings .libraries .emplace_back (lib);
@@ -1564,9 +1575,6 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
15641575 }
15651576 }
15661577
1567- if (!loadCppcheckCfg ())
1568- return Result::Fail;
1569-
15701578 // TODO: bail out?
15711579 if (!executorAuto && mSettings .useSingleJob ())
15721580 mLogger .printMessage (" '--executor' has no effect as only a single job will be used." );
@@ -1690,13 +1698,15 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
16901698 return Result::Success;
16911699}
16921700
1693- void CmdLineParser::printHelp () const
1701+ void CmdLineParser::printHelp (bool premium ) const
16941702{
1695- const std::string manualUrl (isCppcheckPremium () ?
1696- " https://cppcheck.sourceforge.io/manual.pdf" :
1697- " https://files.cppchecksolutions.com/manual.pdf" );
1703+ // TODO: fetch URL from config like product name?
1704+ const std::string manualUrl (premium ?
1705+ " https://files.cppchecksolutions.com/manual.pdf" :
1706+ " https://cppcheck.sourceforge.io/manual.pdf" );
16981707
16991708 std::ostringstream oss;
1709+ // TODO: display product name
17001710 oss << " Cppcheck - A tool for static C/C++ code analysis\n "
17011711 " \n "
17021712 " Syntax:\n "
@@ -1890,7 +1900,7 @@ void CmdLineParser::printHelp() const
18901900 " --plist-output=<path>\n "
18911901 " Generate Clang-plist output files in folder.\n " ;
18921902
1893- if (isCppcheckPremium () ) {
1903+ if (premium ) {
18941904 oss <<
18951905 " --premium=<option>\n "
18961906 " Coding standards:\n "
@@ -2073,6 +2083,7 @@ void CmdLineParser::printHelp() const
20732083}
20742084
20752085std::string CmdLineParser::getVersion () const {
2086+ // TODO: this should not contain the version - it should set the extraVersion
20762087 if (!mSettings .cppcheckCfgProductName .empty ())
20772088 return mSettings .cppcheckCfgProductName ;
20782089 const char * const extraVersion = CppCheck::extraVersion ();
@@ -2081,12 +2092,6 @@ std::string CmdLineParser::getVersion() const {
20812092 return std::string (" Cppcheck " ) + CppCheck::version ();
20822093}
20832094
2084- bool CmdLineParser::isCppcheckPremium () const {
2085- if (mSettings .cppcheckCfgProductName .empty ())
2086- Settings::loadCppcheckCfg (mSettings , mSuppressions , mSettings .debuglookup || mSettings .debuglookupConfig );
2087- return startsWith (mSettings .cppcheckCfgProductName , " Cppcheck Premium" );
2088- }
2089-
20902095bool CmdLineParser::tryLoadLibrary (Library& destination, const std::string& basepath, const char * filename, bool debug)
20912096{
20922097 const Library::Error err = destination.load (basepath.c_str (), filename, debug);
@@ -2179,13 +2184,19 @@ bool CmdLineParser::loadAddons(Settings& settings)
21792184
21802185bool CmdLineParser::loadCppcheckCfg ()
21812186{
2182- if (!mSettings .cppcheckCfgProductName .empty ())
2183- return true ;
2187+ if (!mSettings .settingsFiles .empty ())
2188+ {
2189+ // should never happen - programming error
2190+ mLogger .printError (" cppcheck.cfg has already been loaded from " + mSettings .settingsFiles [0 ]);
2191+ return false ;
2192+ }
21842193 const std::string cfgErr = Settings::loadCppcheckCfg (mSettings , mSuppressions , mSettings .debuglookup || mSettings .debuglookupConfig );
21852194 if (!cfgErr.empty ()) {
2195+ // TODO: log full path
21862196 mLogger .printError (" could not load cppcheck.cfg - " + cfgErr);
21872197 return false ;
21882198 }
2199+ mSettings .premium = startsWith (mSettings .cppcheckCfgProductName , " Cppcheck Premium" );
21892200 return true ;
21902201}
21912202
0 commit comments