refactor(pprocessor): rename macro table type and update function names
- Change `scc_macro_table_t` to `scc_pp_macro_table_t` for consistency - Rename `scc_pp_macro_create` to `scc_pp_macro_new` for naming convention - Remove unused `scc_pp_compress_whitespace` function - Update macro table function names: `scc_pp_find_macro` → `scc_pp_macro_table_get`, `scc_pp_remove_macro` → `scc_pp_macro_table_remove` - Add new `scc_pp_macro_table_set` function for setting macros - Update all function signatures to use new type name - Remove commented-out whitespace compression code from implementation
This commit is contained in:
@@ -22,7 +22,7 @@ static scc_probe_stream_t *from_file_stream(FILE *fp) {
|
||||
|
||||
scc_mem_probe_stream_t *mem_stream = malloc(sizeof(scc_mem_probe_stream_t));
|
||||
scc_probe_stream_t *stream =
|
||||
scc_mem_probe_stream_init(mem_stream, buffer, fsize, true);
|
||||
scc_mem_probe_stream_init(mem_stream, buffer, read_ret, true);
|
||||
return stream;
|
||||
}
|
||||
|
||||
@@ -38,32 +38,50 @@ static void test_file(const char *name) {
|
||||
assert(fexpect != NULL);
|
||||
|
||||
scc_pproc_t pp;
|
||||
scc_mem_probe_stream_t stream;
|
||||
scc_probe_stream_t *output_stream =
|
||||
scc_pproc_init(&pp, from_file_stream(fsrc));
|
||||
scc_probe_stream_t *expect_stream = from_file_stream(fexpect);
|
||||
|
||||
fclose(fsrc);
|
||||
fclose(fexpect);
|
||||
|
||||
TEST_CASE(src_fname);
|
||||
while (1) {
|
||||
int output_ch = scc_probe_stream_consume(output_stream);
|
||||
int expect_ch = scc_probe_stream_consume(expect_stream);
|
||||
TEST_CHECK(output_ch == expect_ch);
|
||||
TEST_MSG("output: %c %x, expect: %c %x", output_ch, output_ch,
|
||||
expect_ch, expect_ch);
|
||||
if (output_ch != expect_ch) {
|
||||
break;
|
||||
}
|
||||
if (output_ch == scc_stream_eof) {
|
||||
#define BUFFER_LEN 4096
|
||||
int ch;
|
||||
char expect_buffer[BUFFER_LEN];
|
||||
char output_buffer[BUFFER_LEN];
|
||||
usize size_produced = 0, size_expected = 0;
|
||||
for (usize i = 0; i < BUFFER_LEN; ++i) {
|
||||
ch = scc_probe_stream_consume(expect_stream);
|
||||
if (ch != scc_stream_eof) {
|
||||
expect_buffer[i] = (char)ch;
|
||||
} else {
|
||||
size_expected = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (usize i = 0; i < BUFFER_LEN; ++i) {
|
||||
ch = scc_probe_stream_consume(output_stream);
|
||||
if (ch != scc_stream_eof) {
|
||||
output_buffer[i] = (char)ch;
|
||||
} else {
|
||||
size_produced = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
TEST_CHECK(size_produced == size_expected &&
|
||||
memcmp(output_buffer, expect_buffer, size_produced) == 0);
|
||||
TEST_DUMP("Expected:", expect_buffer, size_expected);
|
||||
TEST_DUMP("Produced:", output_buffer, size_produced);
|
||||
|
||||
scc_pproc_drop(&pp);
|
||||
}
|
||||
|
||||
static void test_basic(void) {
|
||||
char name[32];
|
||||
for (int i = 1; i <= 22; ++i) {
|
||||
// snprintf(name, sizeof(name), "%02d", i);
|
||||
// test_file(name);
|
||||
snprintf(name, sizeof(name), "%02d", i);
|
||||
test_file(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -104,6 +104,43 @@ static void test_define_nested_macros(void) {
|
||||
"((1 + 1) + 1)\n");
|
||||
}
|
||||
|
||||
static void hard_test_define_func_macros(void) {
|
||||
TEST_CASE("func_macros_hard with pp_01");
|
||||
CHECK_PP_OUTPUT_EXACT("#define hash_hash # ## #\n"
|
||||
"#define mkstr(a) # a\n"
|
||||
"#define in_between(a) mkstr(a)\n"
|
||||
"#define join(c, d) in_between(c hash_hash d)\n"
|
||||
"char p[] = join(x, y);\n",
|
||||
"char p[] = \"x ## y\";\n");
|
||||
|
||||
TEST_CASE("func_macros_hard with recursive define");
|
||||
CHECK_PP_OUTPUT_EXACT("#define M1(x) M2(x + 1)\n"
|
||||
"#define M2(x) M1(x * 2)\n"
|
||||
"M1(5)\n",
|
||||
"M1(5 + 1 * 2)\n");
|
||||
CHECK_PP_OUTPUT_EXACT("#define A B\n"
|
||||
"#define B C\n"
|
||||
"#define C 1\n"
|
||||
"A\n",
|
||||
"1\n");
|
||||
|
||||
TEST_CASE("func_macros_hard with self recursive call");
|
||||
CHECK_PP_OUTPUT_EXACT("#define M(x) x\n"
|
||||
"M(M(10))\n",
|
||||
"10\n");
|
||||
CHECK_PP_OUTPUT_EXACT("#define M(x) M(x)\n"
|
||||
"#define N(x) x\n"
|
||||
"N(M(1))\n",
|
||||
"M(1)\n");
|
||||
|
||||
TEST_CASE("func_macros_hard with define by macro");
|
||||
CHECK_PP_OUTPUT_EXACT("#define M1(x) M1(x + 1)\n"
|
||||
"#define M2 M1\n"
|
||||
"#define M3(x) x\n"
|
||||
"M3(M3(M2)(0))\n",
|
||||
"M1(0 + 1)\n");
|
||||
}
|
||||
|
||||
static void test_conditional_compilation(void) {
|
||||
TEST_CASE("conditional compilation");
|
||||
CHECK_PP_OUTPUT_EXACT("#if 1\ntrue\n#endif\n", "true\n");
|
||||
@@ -140,5 +177,6 @@ TEST_LIST = {
|
||||
TEST_LIST_CASE(test_define_stringify_operator),
|
||||
TEST_LIST_CASE(test_define_concat_operator),
|
||||
TEST_LIST_CASE(test_define_nested_macros),
|
||||
TEST_LIST_CASE(hard_test_define_func_macros),
|
||||
{NULL, NULL},
|
||||
};
|
||||
Reference in New Issue
Block a user