@@ -912,6 +912,10 @@ simplecpp::TokenList Preprocessor::preprocess(const std::string &cfg, std::vecto
912912
913913 tokens2.removeComments ();
914914
915+ // ensure that guessed define macros without value are not used in the code
916+ if (!validateCfg (cfg, macroUsage))
917+ return simplecpp::TokenList (files);
918+
915919 return tokens2;
916920}
917921
@@ -1059,6 +1063,44 @@ void Preprocessor::invalidSuppression(const simplecpp::Location& loc, const std:
10591063 error (loc, msg, " invalidSuppression" );
10601064}
10611065
1066+ bool Preprocessor::validateCfg (const std::string &cfg, const std::list<simplecpp::MacroUsage> ¯oUsageList)
1067+ {
1068+ bool ret = true ;
1069+ std::list<std::string> defines;
1070+ splitcfg (cfg, defines, emptyString);
1071+ for (const std::string &define : defines) {
1072+ if (define.find (' =' ) != std::string::npos)
1073+ continue ;
1074+ const std::string macroName (define.substr (0 , define.find (' (' )));
1075+ for (const simplecpp::MacroUsage &mu : macroUsageList) {
1076+ if (mu.macroValueKnown )
1077+ continue ;
1078+ if (mu.macroName != macroName)
1079+ continue ;
1080+ const bool directiveLocation = std::any_of (mDirectives .cbegin (), mDirectives .cend (),
1081+ [=](const Directive &dir) {
1082+ return mu.useLocation .file () == dir.file && mu.useLocation .line == dir.linenr ;
1083+ });
1084+
1085+ if (!directiveLocation) {
1086+ if (mSettings .severity .isEnabled (Severity::information))
1087+ validateCfgError (mu.useLocation .file (), mu.useLocation .line , cfg, macroName);
1088+ ret = false ;
1089+ }
1090+ }
1091+ }
1092+
1093+ return ret;
1094+ }
1095+
1096+ void Preprocessor::validateCfgError (const std::string &file, const unsigned int line, const std::string &cfg, const std::string ¯o)
1097+ {
1098+ const std::string id = " ConfigurationNotChecked" ;
1099+ ErrorMessage::FileLocation loc (file, line, 0 );
1100+ const ErrorMessage errmsg ({std::move (loc)}, mFile0 , Severity::information, " Skipping configuration '" + cfg + " ' since the value of '" + macro + " ' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly." , id, Certainty::normal);
1101+ mErrorLogger ->reportInfo (errmsg);
1102+ }
1103+
10621104void Preprocessor::getErrorMessages (ErrorLogger &errorLogger, const Settings &settings)
10631105{
10641106 std::vector<std::string> files;
@@ -1069,6 +1111,7 @@ void Preprocessor::getErrorMessages(ErrorLogger &errorLogger, const Settings &se
10691111 loc.col = 2 ;
10701112 preprocessor.missingInclude (loc, " " , UserHeader);
10711113 preprocessor.missingInclude (loc, " " , SystemHeader);
1114+ preprocessor.validateCfgError (" " , 1 , " X" , " X" );
10721115 preprocessor.error (loc, " message" , simplecpp::Output::ERROR);
10731116 preprocessor.error (loc, " message" , simplecpp::Output::SYNTAX_ERROR);
10741117 preprocessor.error (loc, " message" , simplecpp::Output::UNHANDLED_CHAR_ERROR);
0 commit comments