@@ -321,8 +321,37 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
321321 // default to --check-level=normal from CLI for now
322322 mSettings .setCheckLevel (Settings::CheckLevel::normal);
323323
324+ // read --debug-lookup early so the option is available for the cppcheck.cfg loading
325+ for (int i = 1 ; i < argc; i++) {
326+ // Show debug warnings for lookup for configuration files
327+ if (std::strcmp (argv[i], " --debug-lookup" ) == 0 )
328+ mSettings .debuglookup = true ;
329+
330+ else if (std::strncmp (argv[i], " --debug-lookup=" , 15 ) == 0 ) {
331+ const std::string lookup = argv[i] + 15 ;
332+ if (lookup == " all" )
333+ mSettings .debuglookup = true ;
334+ else if (lookup == " addon" )
335+ mSettings .debuglookupAddon = true ;
336+ else if (lookup == " config" )
337+ mSettings .debuglookupConfig = true ;
338+ else if (lookup == " library" )
339+ mSettings .debuglookupLibrary = true ;
340+ else if (lookup == " platform" )
341+ mSettings .debuglookupPlatform = true ;
342+ else
343+ {
344+ mLogger .printError (" unknown lookup '" + lookup + " '" );
345+ return Result::Fail;
346+ }
347+ }
348+ }
349+
350+ if (!loadCppcheckCfg ())
351+ return Result::Fail;
352+
324353 if (argc <= 1 ) {
325- printHelp ();
354+ printHelp (mSettings . premium );
326355 return Result::Exit;
327356 }
328357
@@ -346,8 +375,6 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
346375
347376 // print all possible error messages..
348377 if (std::strcmp (argv[i], " --errorlist" ) == 0 ) {
349- if (!loadCppcheckCfg ())
350- return Result::Fail;
351378 {
352379 XMLErrorMessagesLogger xmlLogger;
353380 std::cout << ErrorMessage::getXMLHeader (mSettings .cppcheckCfgProductName , 2 );
@@ -359,7 +386,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
359386
360387 // Print help
361388 if (std::strcmp (argv[i], " -h" ) == 0 || std::strcmp (argv[i], " --help" ) == 0 ) {
362- printHelp ();
389+ printHelp (mSettings . premium );
363390 return Result::Exit;
364391 }
365392
@@ -371,8 +398,6 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
371398 }
372399
373400 if (std::strcmp (argv[i], " --version" ) == 0 ) {
374- if (!loadCppcheckCfg ())
375- return Result::Fail;
376401 const std::string version = getVersion ();
377402 mLogger .printRaw (version); // TODO: should not include newline
378403 return Result::Exit;
@@ -611,28 +636,11 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
611636 std::strcmp (argv[i], " --debug-normal" ) == 0 )
612637 debug = true ;
613638
614- // Show debug warnings for lookup for configuration files
615639 else if (std::strcmp (argv[i], " --debug-lookup" ) == 0 )
616- mSettings . debuglookup = true ;
640+ continue ; // already handled above
617641
618- else if (std::strncmp (argv[i], " --debug-lookup=" , 15 ) == 0 ) {
619- const std::string lookup = argv[i] + 15 ;
620- if (lookup == " all" )
621- mSettings .debuglookup = true ;
622- else if (lookup == " addon" )
623- mSettings .debuglookupAddon = true ;
624- else if (lookup == " config" )
625- mSettings .debuglookupConfig = true ;
626- else if (lookup == " library" )
627- mSettings .debuglookupLibrary = true ;
628- else if (lookup == " platform" )
629- mSettings .debuglookupPlatform = true ;
630- else
631- {
632- mLogger .printError (" unknown lookup '" + lookup + " '" );
633- return Result::Fail;
634- }
635- }
642+ else if (std::strncmp (argv[i], " --debug-lookup=" , 15 ) == 0 )
643+ continue ; // already handled above
636644
637645 // Flag used for various purposes during debugging
638646 else if (std::strcmp (argv[i], " --debug-simplified" ) == 0 )
@@ -1084,7 +1092,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
10841092 }
10851093
10861094 // Special Cppcheck Premium options
1087- else if ((std::strncmp (argv[i], " --premium=" , 10 ) == 0 || std::strncmp (argv[i], " --premium-" , 10 ) == 0 ) && isCppcheckPremium () ) {
1095+ else if ((std::strncmp (argv[i], " --premium=" , 10 ) == 0 || std::strncmp (argv[i], " --premium-" , 10 ) == 0 ) && mSettings . premium ) {
10881096 // valid options --premium=..
10891097 const std::set<std::string> valid{
10901098 " autosar" ,
@@ -1145,7 +1153,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11451153
11461154 mSettings .checkAllConfigurations = false ; // Can be overridden with --max-configs or --force
11471155 std::string projectFile = argv[i]+10 ;
1148- projectType = project.import (projectFile, &mSettings , &mSuppressions , isCppcheckPremium () );
1156+ projectType = project.import (projectFile, &mSettings , &mSuppressions , mSettings . premium );
11491157 if (projectType == ImportProject::Type::CPPCHECK_GUI) {
11501158 for (const std::string &lib : project.guiProject .libraries )
11511159 mSettings .libraries .emplace_back (lib);
@@ -1543,9 +1551,6 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
15431551 }
15441552 }
15451553
1546- if (!loadCppcheckCfg ())
1547- return Result::Fail;
1548-
15491554 // TODO: bail out?
15501555 if (!executorAuto && mSettings .useSingleJob ())
15511556 mLogger .printMessage (" '--executor' has no effect as only a single job will be used." );
@@ -1669,13 +1674,15 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
16691674 return Result::Success;
16701675}
16711676
1672- void CmdLineParser::printHelp () const
1677+ void CmdLineParser::printHelp (bool premium ) const
16731678{
1674- const std::string manualUrl (isCppcheckPremium () ?
1675- " https://cppcheck.sourceforge.io/manual.pdf" :
1676- " https://files.cppchecksolutions.com/manual.pdf" );
1679+ // TODO: fetch URL from config like product name?
1680+ const std::string manualUrl (premium ?
1681+ " https://files.cppchecksolutions.com/manual.pdf" :
1682+ " https://cppcheck.sourceforge.io/manual.pdf" );
16771683
16781684 std::ostringstream oss;
1685+ // TODO: display product name
16791686 oss << " Cppcheck - A tool for static C/C++ code analysis\n "
16801687 " \n "
16811688 " Syntax:\n "
@@ -1876,7 +1883,7 @@ void CmdLineParser::printHelp() const
18761883 " --plist-output=<path>\n "
18771884 " Generate Clang-plist output files in folder.\n " ;
18781885
1879- if (isCppcheckPremium () ) {
1886+ if (premium ) {
18801887 oss <<
18811888 " --premium=<option>\n "
18821889 " Coding standards:\n "
@@ -2059,6 +2066,7 @@ void CmdLineParser::printHelp() const
20592066}
20602067
20612068std::string CmdLineParser::getVersion () const {
2069+ // TODO: this should not contain the version - it should set the extraVersion
20622070 if (!mSettings .cppcheckCfgProductName .empty ())
20632071 return mSettings .cppcheckCfgProductName ;
20642072 const char * const extraVersion = CppCheck::extraVersion ();
@@ -2067,12 +2075,6 @@ std::string CmdLineParser::getVersion() const {
20672075 return std::string (" Cppcheck " ) + CppCheck::version ();
20682076}
20692077
2070- bool CmdLineParser::isCppcheckPremium () const {
2071- if (mSettings .cppcheckCfgProductName .empty ())
2072- Settings::loadCppcheckCfg (mSettings , mSuppressions , mSettings .debuglookup || mSettings .debuglookupConfig );
2073- return startsWith (mSettings .cppcheckCfgProductName , " Cppcheck Premium" );
2074- }
2075-
20762078bool CmdLineParser::tryLoadLibrary (Library& destination, const std::string& basepath, const char * filename, bool debug)
20772079{
20782080 const Library::Error err = destination.load (basepath.c_str (), filename, debug);
@@ -2165,13 +2167,19 @@ bool CmdLineParser::loadAddons(Settings& settings)
21652167
21662168bool CmdLineParser::loadCppcheckCfg ()
21672169{
2168- if (!mSettings .cppcheckCfgProductName .empty ())
2169- return true ;
2170+ if (!mSettings .settingsFiles .empty ())
2171+ {
2172+ // should never happen - programming error
2173+ mLogger .printError (" cppcheck.cfg has already been loaded from " + mSettings .settingsFiles [0 ]);
2174+ return false ;
2175+ }
21702176 const std::string cfgErr = Settings::loadCppcheckCfg (mSettings , mSuppressions , mSettings .debuglookup || mSettings .debuglookupConfig );
21712177 if (!cfgErr.empty ()) {
2178+ // TODO: log full path
21722179 mLogger .printError (" could not load cppcheck.cfg - " + cfgErr);
21732180 return false ;
21742181 }
2182+ mSettings .premium = startsWith (mSettings .cppcheckCfgProductName , " Cppcheck Premium" );
21752183 return true ;
21762184}
21772185
0 commit comments