@@ -1014,16 +1014,37 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
10141014
10151015 // Get configurations..
10161016 std::set<std::string> configurations;
1017- if ((mSettings .checkAllConfigurations && mSettings .userDefines .empty ()) || mSettings .force ) {
1018- Timer::run (" Preprocessor::getConfigs" , mSettings .showtime , &s_timerResults, [&]() {
1019- configurations = preprocessor.getConfigs (tokens1);
1020- });
1017+ Timer::run (" Preprocessor::getConfigs" , mSettings .showtime , &s_timerResults, [&]() {
1018+ configurations = preprocessor.getConfigs (tokens1);
1019+ });
1020+
1021+ std::list<std::string> configs_total;
1022+ if (!mSettings .userDefines .empty ()) {
1023+ configs_total.push_back (mSettings .userDefines );
1024+ }
1025+
1026+ int max_config_tmp;
1027+ if (mSettings .force && mSettings .maxConfigs <= 0 ) {
1028+ max_config_tmp = configurations.size ();
1029+ } else if (mSettings .maxConfigs > 0 ) {
1030+ if (mSettings .preprocessOnly ) {
1031+ max_config_tmp = 1 ;
1032+ } else {
1033+ max_config_tmp = mSettings .maxConfigs ;
1034+ }
1035+ max_config_tmp = max_config_tmp - configs_total.size ();
1036+ } else if (!mSettings .checkAllConfigurations ) {
1037+ max_config_tmp = 1 ;
10211038 } else {
1022- configurations.insert (mSettings .userDefines );
1039+ max_config_tmp = mSettings .maxConfigsDefault - configs_total.size ();
1040+ }
1041+
1042+ for (auto it = configurations.begin (); it != configurations.end () && max_config_tmp > 0 ; ++it, max_config_tmp--) {
1043+ configs_total.push_back (*it);
10231044 }
10241045
10251046 if (mSettings .checkConfiguration ) {
1026- for (const std::string &config : configurations )
1047+ for (const std::string &config : configs_total )
10271048 (void )preprocessor.getcode (tokens1, config, files, true );
10281049
10291050 if (analyzerInformation)
@@ -1046,10 +1067,10 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
10461067 executeRules (" define" , tokenlist);
10471068 }
10481069#endif
1049-
1050- if (!mSettings .force && configurations. size () > mSettings .maxConfigs ) {
1070+ int max_total = configurations. size () + ( mSettings . userDefines . empty () ? 0 : 1 );
1071+ if (!mSettings .force && max_total > mSettings .maxConfigsDefault ) {
10511072 if (mSettings .severity .isEnabled (Severity::information)) {
1052- tooManyConfigsError (Path::toNativeSeparators (file.spath ()),configurations. size () );
1073+ tooManyConfigsError (Path::toNativeSeparators (file.spath ()), max_total );
10531074 } else {
10541075 mTooManyConfigs = true ;
10551076 }
@@ -1069,30 +1090,14 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
10691090 }
10701091
10711092 std::set<unsigned long long > hashes;
1072- int checkCount = 0 ;
10731093 bool hasValidConfig = false ;
10741094 std::list<std::string> configurationError;
1075- for (const std::string &currCfg : configurations ) {
1095+ for (const std::string &currCfg : configs_total ) {
10761096 // bail out if terminated
10771097 if (Settings::terminated ())
10781098 break ;
10791099
1080- // Check only a few configurations (default 12), after that bail out, unless --force
1081- // was used.
1082- if (!mSettings .force && ++checkCount > mSettings .maxConfigs )
1083- break ;
1084-
1085- if (!mSettings .userDefines .empty ()) {
1086- mCurrentConfig = mSettings .userDefines ;
1087- const std::vector<std::string> v1 (split (mSettings .userDefines , " ;" ));
1088- for (const std::string &cfg: split (currCfg, " ;" )) {
1089- if (std::find (v1.cbegin (), v1.cend (), cfg) == v1.cend ()) {
1090- mCurrentConfig += " ;" + cfg;
1091- }
1092- }
1093- } else {
1094- mCurrentConfig = currCfg;
1095- }
1100+ mCurrentConfig = currCfg;
10961101
10971102 if (mSettings .preprocessOnly ) {
10981103 std::string codeWithoutCfg;
@@ -1132,7 +1137,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
11321137 mLogger ->setLocationMacros (tokenizer.tokens (), files);
11331138
11341139 // If only errors are printed, print filename after the check
1135- if (!mSettings .quiet && (!mCurrentConfig .empty () || checkCount > 1 )) {
1140+ if (!mSettings .quiet && (!mCurrentConfig .empty ())) {
11361141 std::string fixedpath = Path::toNativeSeparators (file.spath ());
11371142 mErrorLogger .reportOut (" Checking " + fixedpath + " : " + mCurrentConfig + " ..." , Color::FgGreen);
11381143 }
@@ -1187,9 +1192,8 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
11871192 } catch (const simplecpp::Output &o) {
11881193 // #error etc during preprocessing
11891194 configurationError.push_back ((mCurrentConfig .empty () ? " \'\' " : mCurrentConfig ) + " : [" + o.location .file () + ' :' + std::to_string (o.location .line ) + " ] " + o.msg );
1190- --checkCount; // don't count invalid configurations
11911195
1192- if (!hasValidConfig && currCfg == *configurations .rbegin ()) {
1196+ if (!hasValidConfig && currCfg == *configs_total .rbegin ()) {
11931197 // If there is no valid configuration then report error..
11941198 std::string locfile = Path::fromNativeSeparators (o.location .file ());
11951199 if (mSettings .relativePaths )
@@ -1218,7 +1222,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
12181222 }
12191223 }
12201224
1221- if (!hasValidConfig && configurations .size () > 1 && mSettings .severity .isEnabled (Severity::information)) {
1225+ if (!hasValidConfig && configs_total .size () > 1 && mSettings .severity .isEnabled (Severity::information)) {
12221226 std::string msg;
12231227 msg = " This file is not analyzed. Cppcheck failed to extract a valid configuration. Use -v for more details." ;
12241228 msg += " \n This file is not analyzed. Cppcheck failed to extract a valid configuration. The tested configurations have these preprocessor errors:" ;
0 commit comments