Skip to content

Commit a2f6f52

Browse files
committed
test: add regression test for ## PAR-style indirection (hashhash_funclike_par_indirection)
Covers the fix in expandHashHash(): when ## concatenation produces a function-like macro name but '(' is not immediately adjacent in the replacement text (hidden behind a comma/parameter), the forward scan must locate '(' and complete the expansion. #define PAR(a, ...) a __VA_ARGS__ #define PREFIX_SCALAR(T, N) T N #define DISPATCH(kind, ...) PAR(PREFIX_ ## kind, (__VA_ARGS__)) DISPATCH(SCALAR, int, x) // expected: int x
1 parent a328c4d commit a2f6f52

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,26 @@ static void hashhash_universal_character_2()
20382038
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'A', Invalid ## usage when expanding 'A': Combining '\\U0104' and '0104' yields universal character '\\U01040104'. This is undefined behavior according to C standard chapter 5.1.1.2, paragraph 4.\n", toString(outputList));
20392039
}
20402040

2041+
// Regression test: ## result is a function-like macro whose '(' is not adjacent.
2042+
// PAR-style indirection hides '(' behind a comma/parameter in the replacement text.
2043+
// Previously caused [unknownMacro] and aborted TU expansion.
2044+
static void hashhash_funclike_par_indirection()
2045+
{
2046+
// Single dispatch: PREFIX_ ## kind → PREFIX_SCALAR, '(' separated by ','
2047+
ASSERT_EQUALS("\n\n\nint x",
2048+
preprocess("#define PAR(a, ...) a __VA_ARGS__\n"
2049+
"#define PREFIX_SCALAR(T, N) T N\n"
2050+
"#define DISPATCH(kind, ...) PAR(PREFIX_ ## kind, (__VA_ARGS__))\n"
2051+
"DISPATCH(SCALAR, int, x)\n"));
2052+
2053+
// Chained: two DISPATCH calls with different PREFIX_ specialisations
2054+
ASSERT_EQUALS("\n\n\n\nint x float arr [ 10 ]",
2055+
preprocess("#define PAR(a, ...) a __VA_ARGS__\n"
2056+
"#define PREFIX_SCALAR(T, N) T N\n"
2057+
"#define PREFIX_ARRAY(T, N, S) T N[S]\n"
2058+
"#define DISPATCH(kind, ...) PAR(PREFIX_ ## kind, (__VA_ARGS__))\n"
2059+
"DISPATCH(SCALAR, int, x) DISPATCH(ARRAY, float, arr, 10)\n"));
2060+
}
20412061

20422062
static void has_include_1()
20432063
{
@@ -4669,6 +4689,7 @@ static void runTests(int argc, char **argv, Input input)
46694689
// the behavior is undefined."
46704690
TEST_CASE(hashhash_universal_character);
46714691
TEST_CASE(hashhash_universal_character_2);
4692+
TEST_CASE(hashhash_funclike_par_indirection); // PAR-style ## indirection: '(' not adjacent to ## result
46724693

46734694
// c++17 __has_include
46744695
TEST_CASE(has_include_1);

0 commit comments

Comments
 (0)