@@ -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;
@@ -614,28 +639,11 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
614639 std::strcmp (argv[i], " --debug-normal" ) == 0 )
615640 debug = true ;
616641
617- // Show debug warnings for lookup for configuration files
618642 else if (std::strcmp (argv[i], " --debug-lookup" ) == 0 )
619- mSettings . debuglookup = true ;
643+ continue ; // already handled above
620644
621- else if (std::strncmp (argv[i], " --debug-lookup=" , 15 ) == 0 ) {
622- const std::string lookup = argv[i] + 15 ;
623- if (lookup == " all" )
624- mSettings .debuglookup = true ;
625- else if (lookup == " addon" )
626- mSettings .debuglookupAddon = true ;
627- else if (lookup == " config" )
628- mSettings .debuglookupConfig = true ;
629- else if (lookup == " library" )
630- mSettings .debuglookupLibrary = true ;
631- else if (lookup == " platform" )
632- mSettings .debuglookupPlatform = true ;
633- else
634- {
635- mLogger .printError (" unknown lookup '" + lookup + " '" );
636- return Result::Fail;
637- }
638- }
645+ else if (std::strncmp (argv[i], " --debug-lookup=" , 15 ) == 0 )
646+ continue ; // already handled above
639647
640648 // Flag used for various purposes during debugging
641649 else if (std::strcmp (argv[i], " --debug-simplified" ) == 0 )
@@ -1087,7 +1095,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
10871095 }
10881096
10891097 // Special Cppcheck Premium options
1090- else if ((std::strncmp (argv[i], " --premium=" , 10 ) == 0 || std::strncmp (argv[i], " --premium-" , 10 ) == 0 ) && isCppcheckPremium () ) {
1098+ else if ((std::strncmp (argv[i], " --premium=" , 10 ) == 0 || std::strncmp (argv[i], " --premium-" , 10 ) == 0 ) && mSettings . premium ) {
10911099 // valid options --premium=..
10921100 const std::set<std::string> valid{
10931101 " autosar" ,
@@ -1148,7 +1156,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11481156
11491157 mSettings .checkAllConfigurations = false ; // Can be overridden with --max-configs or --force
11501158 std::string projectFile = argv[i]+10 ;
1151- projectType = project.import (projectFile, &mSettings , &mSuppressions , isCppcheckPremium () );
1159+ projectType = project.import (projectFile, &mSettings , &mSuppressions , mSettings . premium );
11521160 if (projectType == ImportProject::Type::CPPCHECK_GUI) {
11531161 for (const std::string &lib : project.guiProject .libraries )
11541162 mSettings .libraries .emplace_back (lib);
@@ -1561,9 +1569,6 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
15611569 }
15621570 }
15631571
1564- if (!loadCppcheckCfg ())
1565- return Result::Fail;
1566-
15671572 // TODO: bail out?
15681573 if (!executorAuto && mSettings .useSingleJob ())
15691574 mLogger .printMessage (" '--executor' has no effect as only a single job will be used." );
@@ -1687,13 +1692,15 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
16871692 return Result::Success;
16881693}
16891694
1690- void CmdLineParser::printHelp () const
1695+ void CmdLineParser::printHelp (bool premium ) const
16911696{
1692- const std::string manualUrl (isCppcheckPremium () ?
1693- " https://cppcheck.sourceforge.io/manual.pdf" :
1694- " https://files.cppchecksolutions.com/manual.pdf" );
1697+ // TODO: fetch URL from config like product name?
1698+ const std::string manualUrl (premium ?
1699+ " https://files.cppchecksolutions.com/manual.pdf" :
1700+ " https://cppcheck.sourceforge.io/manual.pdf" );
16951701
16961702 std::ostringstream oss;
1703+ // TODO: display product name
16971704 oss << " Cppcheck - A tool for static C/C++ code analysis\n "
16981705 " \n "
16991706 " Syntax:\n "
@@ -1894,7 +1901,7 @@ void CmdLineParser::printHelp() const
18941901 " --plist-output=<path>\n "
18951902 " Generate Clang-plist output files in folder.\n " ;
18961903
1897- if (isCppcheckPremium () ) {
1904+ if (premium ) {
18981905 oss <<
18991906 " --premium=<option>\n "
19001907 " Coding standards:\n "
@@ -2077,6 +2084,7 @@ void CmdLineParser::printHelp() const
20772084}
20782085
20792086std::string CmdLineParser::getVersion () const {
2087+ // TODO: this should not contain the version - it should set the extraVersion
20802088 if (!mSettings .cppcheckCfgProductName .empty ())
20812089 return mSettings .cppcheckCfgProductName ;
20822090 const char * const extraVersion = CppCheck::extraVersion ();
@@ -2085,12 +2093,6 @@ std::string CmdLineParser::getVersion() const {
20852093 return std::string (" Cppcheck " ) + CppCheck::version ();
20862094}
20872095
2088- bool CmdLineParser::isCppcheckPremium () const {
2089- if (mSettings .cppcheckCfgProductName .empty ())
2090- Settings::loadCppcheckCfg (mSettings , mSuppressions , mSettings .debuglookup || mSettings .debuglookupConfig );
2091- return startsWith (mSettings .cppcheckCfgProductName , " Cppcheck Premium" );
2092- }
2093-
20942096bool CmdLineParser::tryLoadLibrary (Library& destination, const std::string& basepath, const char * filename, bool debug)
20952097{
20962098 const Library::Error err = destination.load (basepath.c_str (), filename, debug);
@@ -2183,13 +2185,19 @@ bool CmdLineParser::loadAddons(Settings& settings)
21832185
21842186bool CmdLineParser::loadCppcheckCfg ()
21852187{
2186- if (!mSettings .cppcheckCfgProductName .empty ())
2187- return true ;
2188+ if (!mSettings .settingsFiles .empty ())
2189+ {
2190+ // should never happen - programming error
2191+ mLogger .printError (" cppcheck.cfg has already been loaded from " + mSettings .settingsFiles [0 ]);
2192+ return false ;
2193+ }
21882194 const std::string cfgErr = Settings::loadCppcheckCfg (mSettings , mSuppressions , mSettings .debuglookup || mSettings .debuglookupConfig );
21892195 if (!cfgErr.empty ()) {
2196+ // TODO: log full path
21902197 mLogger .printError (" could not load cppcheck.cfg - " + cfgErr);
21912198 return false ;
21922199 }
2200+ mSettings .premium = startsWith (mSettings .cppcheckCfgProductName , " Cppcheck Premium" );
21932201 return true ;
21942202}
21952203
0 commit comments