--- /dev/null
+/* PR preprocessor/92319 */
+/* { dg-do preprocess } */
+/* { dg-options "-std=gnu99" { target c } } */
+/* { dg-options "-std=c++2a" { target c++ } } */
+
+#define f1(...) b##__VA_OPT__(c)
+#define e
+#define e2 e
+#define e3 1
+#define e5 e3
+t1 f1 (e);
+/* { dg-final { scan-file va-opt-4.i "t1 b;" } } */
+t2 f1 (e2);
+/* { dg-final { scan-file va-opt-4.i "t2 b;" } } */
+t3 f1 (e3);
+/* { dg-final { scan-file va-opt-4.i "t3 bc;" } } */
+t4 f1 (e4);
+/* { dg-final { scan-file va-opt-4.i "t4 bc;" } } */
+t5 f1 (e5);
+/* { dg-final { scan-file va-opt-4.i "t5 bc;" } } */
+2020-02-14 Jakub Jelinek <jakub@redhat.com>
+
+ Partially implement P1042R1: __VA_OPT__ wording clarifications
+ PR preprocessor/92319
+ * macro.c (expand_arg): Move declarations before vaopt_state
+ definition.
+ (class vaopt_state): Move enum update_type definition earlier. Remove
+ m_allowed member, add m_arg and m_update members.
+ (vaopt_state::vaopt_state): Change last argument from bool any_args
+ to macro_arg *arg, initialize m_arg and m_update instead of m_allowed.
+ (vaopt_state::update): When bumping m_state from 1 to 2 and m_update
+ is ERROR, determine if __VA_ARGS__ expansion has any non-CPP_PADDING
+ tokens and set m_update to INCLUDE if it has any, DROP otherwise.
+ Return m_update instead of m_allowed ? INCLUDE : DROP in m_state >= 2.
+ (replace_args, create_iso_definition): Adjust last argument to
+ vaopt_state ctor.
+
2020-02-05 Martin Sebor <msebor@redhat.com>
* include/cpplib.h (cpp_builtin_type): Remove trailing comma to
static const char *vaopt_paste_error =
N_("'##' cannot appear at either end of __VA_OPT__");
+static void expand_arg (cpp_reader *, macro_arg *);
+
/* A class for tracking __VA_OPT__ state while iterating over a
sequence of tokens. This is used during both macro definition and
expansion. */
public:
+ enum update_type
+ {
+ ERROR,
+ DROP,
+ INCLUDE,
+ BEGIN,
+ END
+ };
+
/* Initialize the state tracker. ANY_ARGS is true if variable
arguments were provided to the macro invocation. */
- vaopt_state (cpp_reader *pfile, bool is_variadic, bool any_args)
+ vaopt_state (cpp_reader *pfile, bool is_variadic, macro_arg *arg)
: m_pfile (pfile),
- m_allowed (any_args),
+ m_arg (arg),
m_variadic (is_variadic),
m_last_was_paste (false),
m_state (0),
m_paste_location (0),
- m_location (0)
+ m_location (0),
+ m_update (ERROR)
{
}
- enum update_type
- {
- ERROR,
- DROP,
- INCLUDE,
- BEGIN,
- END
- };
-
/* Given a token, update the state of this tracker and return a
boolean indicating whether the token should be be included in the
expansion. */
return ERROR;
}
++m_state;
+ if (m_update == ERROR)
+ {
+ if (m_arg == NULL)
+ m_update = INCLUDE;
+ else
+ {
+ m_update = DROP;
+ if (!m_arg->expanded)
+ expand_arg (m_pfile, m_arg);
+ for (unsigned idx = 0; idx < m_arg->expanded_count; ++idx)
+ if (m_arg->expanded[idx]->type != CPP_PADDING)
+ {
+ m_update = INCLUDE;
+ break;
+ }
+ }
+ }
return DROP;
}
else if (m_state >= 2)
return END;
}
}
- return m_allowed ? INCLUDE : DROP;
+ return m_update;
}
/* Nothing to do with __VA_OPT__. */
/* The cpp_reader. */
cpp_reader *m_pfile;
- /* True if there were varargs. */
- bool m_allowed;
+ /* The __VA_ARGS__ argument. */
+ macro_arg *m_arg;
+
/* True if the macro is variadic. */
bool m_variadic;
/* If true, the previous token was ##. This is used to detect when
/* Location of the __VA_OPT__ token. */
location_t m_location;
+
+ /* If __VA_ARGS__ substitutes to no preprocessing tokens,
+ INCLUDE, otherwise DROP. ERROR when unknown yet. */
+ update_type m_update;
};
/* Macro expansion. */
_cpp_buff **, unsigned *);
static cpp_context *next_context (cpp_reader *);
static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
-static void expand_arg (cpp_reader *, macro_arg *);
static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
static void paste_all_tokens (cpp_reader *, const cpp_token *);
num_macro_tokens);
}
i = 0;
- vaopt_state vaopt_tracker (pfile, macro->variadic,
- args[macro->paramc - 1].count > 0);
+ vaopt_state vaopt_tracker (pfile, macro->variadic, &args[macro->paramc - 1]);
const cpp_token **vaopt_start = NULL;
for (src = macro->exp.tokens; src < limit; src++)
{
macro->count = 1;
}
- for (vaopt_state vaopt_tracker (pfile, macro->variadic, true);; token = NULL)
+ for (vaopt_state vaopt_tracker (pfile, macro->variadic, NULL);; token = NULL)
{
if (!token)
{