@@ -2570,42 +2570,59 @@ namespace simplecpp {
25702570 return recursiveExpandToken (output, temp, loc, tok2, macros, expandedmacros, parametertokens, outputList);
25712571 }
25722572
2573- if (tok->str () == DEFINED ) {
2574- const Token * const tok2 = tok->next ;
2575- const Token * const tok3 = tok2 ? tok2->next : nullptr ;
2576- const Token * const tok4 = tok3 ? tok3->next : nullptr ;
2577- const Token *defToken = nullptr ;
2578- const Token *lastToken = nullptr ;
2579- if (sameline (tok, tok4) && tok2->op == ' (' && tok3->name && tok4->op == ' )' ) {
2580- defToken = tok3;
2581- lastToken = tok4;
2582- } else if (sameline (tok,tok2) && tok2->name ) {
2583- defToken = lastToken = tok2;
2584- }
2585- if (defToken) {
2586- std::string macroName = defToken->str ();
2587- if (defToken->next && defToken->next ->op == ' #' && defToken->next ->next && defToken->next ->next ->op == ' #' && defToken->next ->next ->next && defToken->next ->next ->next ->name && sameline (defToken,defToken->next ->next ->next )) {
2588- TokenList temp (files);
2589- if (expandArg (temp, defToken, parametertokens))
2590- macroName = temp.cback ()->str ();
2591- if (expandArg (temp, defToken->next ->next ->next , parametertokens))
2592- macroName += temp.cback () ? temp.cback ()->str () : " " ;
2593- else
2594- macroName += defToken->next ->next ->next ->str ();
2595- lastToken = defToken->next ->next ->next ;
2596- }
2597- const bool def = (macros.find (macroName) != macros.end ());
2598- output.push_back (newMacroToken (def ? " 1" : " 0" , loc, true ));
2599- return lastToken->next ;
2600- }
2601- }
2573+ const Token *next = nullptr ;
2574+ if (tok->str () == DEFINED && expandDefined (output, loc, tok, macros, parametertokens, next))
2575+ return next;
26022576
26032577 output.push_back (newMacroToken (tok->str (), loc, true , tok));
26042578 if (it != macros.end ())
26052579 output.back ()->markExpandedFrom (&it->second );
26062580 return tok->next ;
26072581 }
26082582
2583+ /* *
2584+ * Expand "defined X" or "defined(X)" in the replacement list to 1 or 0.
2585+ * The operand can be a parameter, or two names pasted with ##.
2586+ * @param output destination tokenlist
2587+ * @param loc location for the result
2588+ * @param tok the "defined" token
2589+ * @param macros all macros
2590+ * @param parametertokens arguments of this macro call
2591+ * @param next output: the token after the operand
2592+ * @return false if there is no operand, then nothing is added to output
2593+ */
2594+ bool expandDefined (TokenList &output, const Location &loc, const Token *tok, const MacroMap ¯os, const std::vector<const Token*> ¶metertokens, const Token *&next) const {
2595+ const Token * const tok2 = tok->next ;
2596+ const Token * const tok3 = tok2 ? tok2->next : nullptr ;
2597+ const Token * const tok4 = tok3 ? tok3->next : nullptr ;
2598+ const Token *defToken = nullptr ;
2599+ const Token *lastToken = nullptr ;
2600+ if (sameline (tok, tok4) && tok2->op == ' (' && tok3->name && tok4->op == ' )' ) {
2601+ defToken = tok3;
2602+ lastToken = tok4;
2603+ } else if (sameline (tok,tok2) && tok2->name ) {
2604+ defToken = lastToken = tok2;
2605+ }
2606+ if (defToken) {
2607+ std::string macroName = defToken->str ();
2608+ if (defToken->next && defToken->next ->op == ' #' && defToken->next ->next && defToken->next ->next ->op == ' #' && defToken->next ->next ->next && defToken->next ->next ->next ->name && sameline (defToken,defToken->next ->next ->next )) {
2609+ TokenList temp (files);
2610+ if (expandArg (temp, defToken, parametertokens))
2611+ macroName = temp.cback ()->str ();
2612+ if (expandArg (temp, defToken->next ->next ->next , parametertokens))
2613+ macroName += temp.cback () ? temp.cback ()->str () : " " ;
2614+ else
2615+ macroName += defToken->next ->next ->next ->str ();
2616+ lastToken = defToken->next ->next ->next ;
2617+ }
2618+ const bool def = (macros.find (macroName) != macros.end ());
2619+ output.push_back (newMacroToken (def ? " 1" : " 0" , loc, true ));
2620+ next = lastToken->next ;
2621+ return true ;
2622+ }
2623+ return false ;
2624+ }
2625+
26092626 bool expandArg (TokenList &output, const Token *tok, const std::vector<const Token*> ¶metertokens) const {
26102627 if (!tok->name )
26112628 return false ;
0 commit comments