@@ -428,12 +428,15 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::set<s
428428 }
429429
430430 std::set<std::string> configset;
431+ bool isNotDefinedMacro = false ;
431432 for (; sameline (iftok,cond); cond = cond->next ) {
432433 if (cond->op == ' !' ) {
433434 if (!sameline (iftok,cond->next ) || !cond->next ->name )
434435 break ;
435- if (cond->next ->str () == " defined" )
436+ if (cond->next ->str () == " defined" ) {
437+ isNotDefinedMacro = true ;
436438 continue ;
439+ }
437440 configset.insert (cond->next ->str () + " =0" );
438441 continue ;
439442 }
@@ -444,8 +447,15 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::set<s
444447 break ;
445448 if (dtok->op == ' (' )
446449 dtok = dtok->next ;
447- if (sameline (iftok,dtok) && dtok->name && defined .find (dtok->str ()) == defined .end () && undefined.find (dtok->str ()) == undefined.end ())
448- configset.insert (dtok->str ());
450+
451+ if (sameline (iftok,dtok) && dtok->name && defined .find (dtok->str ()) == defined .end () && undefined.find (dtok->str ()) == undefined.end ()) {
452+ if (!isNotDefinedMacro) {
453+ configset.insert (dtok->str () + " =" + dtok->str ()); // if defined is set to itself.
454+ } else {
455+ configset.insert (dtok->str ());
456+ }
457+ }
458+ isNotDefinedMacro = false ;
449459 }
450460 std::string cfgStr;
451461 for (const std::string &s : configset) {
@@ -464,10 +474,11 @@ static bool hasDefine(const std::string &userDefines, const std::string &cfg)
464474
465475 std::string::size_type pos = 0 ;
466476 while (pos < userDefines.size ()) {
467- pos = userDefines.find (cfg, pos);
477+ const std::string::size_type eq = cfg.find (' =' );
478+ pos = (eq == std::string::npos) ? userDefines.find (cfg, pos) : userDefines.find (cfg.substr (0 , eq), pos);
468479 if (pos == std::string::npos)
469480 break ;
470- const std::string::size_type pos2 = pos + cfg.size ();
481+ const std::string::size_type pos2 = (eq == std::string::npos) ? pos + cfg.size () : pos + eq ;
471482 if ((pos == 0 || userDefines[pos-1U ] == ' ;' ) && (pos2 == userDefines.size () || userDefines[pos2] == ' =' ))
472483 return true ;
473484 pos = pos2;
@@ -553,8 +564,11 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set<std::string>
553564 const simplecpp::Token *expr1 = cmdtok->next ;
554565 if (sameline (tok,expr1) && expr1->name && !sameline (tok,expr1->next ))
555566 config = expr1->str ();
556- if (defined .find (config) != defined .end ())
567+ if (defined .find (config) != defined .end ()) {
557568 config.clear ();
569+ } else if ((cmdtok->str () == " ifdef" ) && sameline (cmdtok,expr1) && !config.empty ()) {
570+ config.append (" =" + expr1->str ()); // Set equal to itself if ifdef.
571+ }
558572 } else if (cmdtok->str () == " if" ) {
559573 config = readcondition (cmdtok, defined , undefined);
560574 }
0 commit comments