@@ -321,8 +321,25 @@ 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+ // TODO: this is duplicated in gui/mainwindows.cpp
325+ {
326+ // only read cppcheck.cfg if the product name has not been set yet
327+ // this is hack so we can unit test this - should never happen in production
328+ if (mSettings .cppcheckCfgProductName .empty ())
329+ {
330+ // load config to determine if we are premium - do not apply any other settings
331+ Settings s;
332+ s.exename = mSettings .exename ;
333+ Suppressions supprs;
334+ // TODO: errorhandling
335+ Settings::loadCppcheckCfg (s, supprs, mSettings .debuglookup || mSettings .debuglookupConfig );
336+ mSettings .cppcheckCfgProductName = s.cppcheckCfgProductName ;
337+ }
338+ mSettings .premium = startsWith (mSettings .cppcheckCfgProductName , " Cppcheck Premium" );
339+ }
340+
324341 if (argc <= 1 ) {
325- printHelp ();
342+ printHelp (mSettings . premium );
326343 return Result::Exit;
327344 }
328345
@@ -359,7 +376,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
359376
360377 // Print help
361378 if (std::strcmp (argv[i], " -h" ) == 0 || std::strcmp (argv[i], " --help" ) == 0 ) {
362- printHelp ();
379+ printHelp (mSettings . premium );
363380 return Result::Exit;
364381 }
365382
@@ -1084,7 +1101,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
10841101 }
10851102
10861103 // Special Cppcheck Premium options
1087- 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 ) {
10881105 // valid options --premium=..
10891106 const std::set<std::string> valid{
10901107 " autosar" ,
@@ -1145,7 +1162,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11451162
11461163 mSettings .checkAllConfigurations = false ; // Can be overridden with --max-configs or --force
11471164 std::string projectFile = argv[i]+10 ;
1148- projectType = project.import (projectFile, &mSettings , &mSuppressions , isCppcheckPremium () );
1165+ projectType = project.import (projectFile, &mSettings , &mSuppressions , mSettings . premium );
11491166 if (projectType == ImportProject::Type::CPPCHECK_GUI) {
11501167 for (const std::string &lib : project.guiProject .libraries )
11511168 mSettings .libraries .emplace_back (lib);
@@ -1669,13 +1686,15 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
16691686 return Result::Success;
16701687}
16711688
1672- void CmdLineParser::printHelp () const
1689+ void CmdLineParser::printHelp (bool premium ) const
16731690{
1674- const std::string manualUrl (isCppcheckPremium () ?
1675- " https://cppcheck.sourceforge.io/manual.pdf" :
1676- " https://files.cppchecksolutions.com/manual.pdf" );
1691+ // TODO: fetch URL from config like product name?
1692+ const std::string manualUrl (premium ?
1693+ " https://files.cppchecksolutions.com/manual.pdf" :
1694+ " https://cppcheck.sourceforge.io/manual.pdf" );
16771695
16781696 std::ostringstream oss;
1697+ // TODO: display product name
16791698 oss << " Cppcheck - A tool for static C/C++ code analysis\n "
16801699 " \n "
16811700 " Syntax:\n "
@@ -1876,7 +1895,7 @@ void CmdLineParser::printHelp() const
18761895 " --plist-output=<path>\n "
18771896 " Generate Clang-plist output files in folder.\n " ;
18781897
1879- if (isCppcheckPremium () ) {
1898+ if (premium ) {
18801899 oss <<
18811900 " --premium=<option>\n "
18821901 " Coding standards:\n "
@@ -2059,6 +2078,7 @@ void CmdLineParser::printHelp() const
20592078}
20602079
20612080std::string CmdLineParser::getVersion () const {
2081+ // TODO: this should not contain the version - it should set the extraVersion
20622082 if (!mSettings .cppcheckCfgProductName .empty ())
20632083 return mSettings .cppcheckCfgProductName ;
20642084 const char * const extraVersion = CppCheck::extraVersion ();
@@ -2067,12 +2087,6 @@ std::string CmdLineParser::getVersion() const {
20672087 return std::string (" Cppcheck " ) + CppCheck::version ();
20682088}
20692089
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-
20762090bool CmdLineParser::tryLoadLibrary (Library& destination, const std::string& basepath, const char * filename, bool debug)
20772091{
20782092 const Library::Error err = destination.load (basepath.c_str (), filename, debug);
@@ -2165,10 +2179,15 @@ bool CmdLineParser::loadAddons(Settings& settings)
21652179
21662180bool CmdLineParser::loadCppcheckCfg ()
21672181{
2168- if (!mSettings .cppcheckCfgProductName .empty ())
2169- return true ;
2182+ if (!mSettings .settingsFiles .empty ())
2183+ {
2184+ // should never happen - programming error
2185+ mLogger .printError (" cppcheck.cfg has already been loaded from " + mSettings .settingsFiles [0 ]);
2186+ return false ;
2187+ }
21702188 const std::string cfgErr = Settings::loadCppcheckCfg (mSettings , mSuppressions , mSettings .debuglookup || mSettings .debuglookupConfig );
21712189 if (!cfgErr.empty ()) {
2190+ // TODO: log full path
21722191 mLogger .printError (" could not load cppcheck.cfg - " + cfgErr);
21732192 return false ;
21742193 }
0 commit comments