@@ -2988,7 +2988,7 @@ namespace simplecpp {
29882988 * The result of an expansion is pushed back onto the input and scanned again, so a
29892989 * function-like macro whose arguments follow the expansion in the source is found by
29902990 * normal rescanning.
2991- * Work in progress: __VA_OPT__ and "defined" in a replacement list are not handled yet.
2991+ * Work in progress: __VA_OPT__ is not handled yet.
29922992 */
29932993 class HideSetExpander {
29942994 public:
@@ -3243,14 +3243,30 @@ namespace simplecpp {
32433243
32443244 /* *
32453245 * The tokens that a token in the replacement list is replaced by: a parameter by its
3246- * argument, # and a parameter by a string literal, and other tokens by a copy.
3246+ * argument, # and a parameter by a string literal, "defined X" by 1 or 0, and other tokens by a copy.
32473247 * @param operand destination tokenlist
32483248 * @param macro the macro
32493249 * @param tok the token in the replacement list; for # it is moved to the parameter
32503250 * @param call the macro call
32513251 * @param expand use the expanded argument for a parameter
32523252 */
32533253 void getOperand (TokenList &operand, const Macro ¯o, const Token *&tok, const MacroCall &call, bool expand) const {
3254+ // "defined X" is replaced by 1 or 0
3255+ const Token *first;
3256+ const Token *second;
3257+ const Token *last;
3258+ if (tok->str () == DEFINED && Macro::findDefinedOperand (tok, first, second, last)) {
3259+ const std::string macroName = Macro::definedMacroName (first, second, [&](const Token *argtok, std::string &text) {
3260+ const unsigned int argnr = getParameter (macro, argtok);
3261+ if (argnr >= call.arguments .size ())
3262+ return false ;
3263+ text = call.arguments [argnr].cback () ? call.arguments [argnr].cback ()->str () : " " ;
3264+ return true ;
3265+ });
3266+ operand.push_back (newToken ((macros.find (macroName) != macros.end ()) ? " 1" : " 0" , macro, last->whitespaceahead ));
3267+ tok = last;
3268+ return ;
3269+ }
32543270 const Token * const next = (tok->next != macro.endToken ) ? tok->next : nullptr ;
32553271 // # is an operator in a function-like macro when it is followed by a parameter
32563272 const unsigned int hashArg = (tok->op == ' #' ) ? getParameter (macro, next) : ~0U ;
@@ -3288,8 +3304,6 @@ namespace simplecpp {
32883304 tok = tok->next ;
32893305 continue ;
32903306 }
3291- if (tok->str () == DEFINED )
3292- throw unsupported (loc, " defined" );
32933307 const Token * const operandTok = tok;
32943308 const bool parameter = getParameter (macro, tok) < call.arguments .size ();
32953309 // an argument is not expanded when it is an operand of ##
0 commit comments