authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-29 01:31:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-29 01:31:40-07:00
logc1691afdd90b744200f2238afe52d7fd9e6471b6
treead13a4543728df614bb7f1dbe296348a72dcbff5
parent9b2ed1fac53319cbddb2752409e166334bb339bf

parseh understands number literal defines


2 files changed, 91 insertions(+), 36 deletions(-)

src/parseh.cpp+38-5
......@@ -31,7 +31,7 @@ struct Context {
3131 HashMap<Buf *, bool, buf_hash, buf_eql_buf> struct_type_table;
3232 HashMap<Buf *, bool, buf_hash, buf_eql_buf> enum_type_table;
3333 HashMap<Buf *, bool, buf_hash, buf_eql_buf> fn_table;
34 HashMap<Buf *, bool, buf_hash, buf_eql_buf> macro_table;
34 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> macro_table;
3535 SourceManager *source_manager;
3636 ZigList<AstNode *> aliases;
3737};
......@@ -715,10 +715,25 @@ static void render_aliases(Context *c) {
715715 if (c->fn_table.maybe_get(name)) {
716716 continue;
717717 }
718 if (c->macro_table.maybe_get(name)) {
719 continue;
720 }
718721 c->root->data.root.top_level_decls.append(alias_node);
719722 }
720723}
721724
725static void render_macros(Context *c) {
726 auto it = c->macro_table.entry_iterator();
727 for (;;) {
728 auto *entry = it.next();
729 if (!entry)
730 break;
731
732 AstNode *var_node = entry->value;
733 c->root->data.root.top_level_decls.append(var_node);
734 }
735}
736
722737static int parse_c_char_lit(Buf *value, uint8_t *out_c) {
723738 enum State {
724739 StateExpectStartQuot,
......@@ -765,19 +780,36 @@ static int parse_c_char_lit(Buf *value, uint8_t *out_c) {
765780 return (state == StateExpectEnd) ? 0 : -1;
766781}
767782
783static int parse_c_num_lit_unsigned(Buf *buf, uint64_t *out_val) {
784 char *temp;
785 *out_val = strtoull(buf_ptr(buf), &temp, 0);
786
787 if (temp == buf_ptr(buf) || *temp != 0 || *out_val == ULLONG_MAX) {
788 return -1;
789 }
790
791 return 0;
792}
793
768794static void process_macro(Context *c, Buf *name, Buf *value) {
769795 // maybe it's a character literal
770796 uint8_t ch;
771797 if (!parse_c_char_lit(value, &ch)) {
772 c->macro_table.put(name, true);
773798 AstNode *var_node = create_var_decl_node(c, buf_ptr(name), create_char_lit_node(c, ch));
774 c->root->data.root.top_level_decls.append(var_node);
799 c->macro_table.put(name, var_node);
775800 return;
776801 }
777802 // maybe it's a string literal
778803 // TODO
779 // maybe it's a number literal
780 // TODO
804
805 // maybe it's an unsigned integer
806 uint64_t uint;
807 if (!parse_c_num_lit_unsigned(value, &uint)) {
808 AstNode *var_node = create_var_decl_node(c, buf_ptr(name), create_num_lit_unsigned(c, uint));
809 c->macro_table.put(name, var_node);
810 return;
811 }
812
781813 // maybe it's a symbol
782814 // TODO
783815}
......@@ -959,6 +991,7 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors,
959991
960992 process_preprocessor_entities(c, *ast_unit);
961993
994 render_macros(c);
962995 render_aliases(c);
963996
964997 normalize_parent_ptrs(c->root);
test/run_tests.cpp+53-31
......@@ -96,21 +96,30 @@ static TestCase *add_compile_fail_case(const char *case_name, const char *source
9696 return test_case;
9797}
9898
99static TestCase *add_parseh_case(const char *case_name, const char *source, const char *output) {
99static TestCase *add_parseh_case(const char *case_name, const char *source, int count, ...) {
100 va_list ap;
101 va_start(ap, count);
102
100103 TestCase *test_case = allocate<TestCase>(1);
101104 test_case->case_name = case_name;
102 test_case->output = output;
103105 test_case->is_parseh = true;
104106
105107 test_case->source_files.resize(1);
106108 test_case->source_files.at(0).relative_path = tmp_h_path;
107109 test_case->source_files.at(0).source_code = source;
108110
111 for (int i = 0; i < count; i += 1) {
112 const char *arg = va_arg(ap, const char *);
113 test_case->compile_errors.append(arg);
114 }
115
109116 test_case->compiler_args.append("parseh");
110117 test_case->compiler_args.append(tmp_h_path);
111118 test_case->compiler_args.append("--c-import-warnings");
112119
113120 test_cases.append(test_case);
121
122 va_end(ap);
114123 return test_case;
115124}
116125
......@@ -1894,13 +1903,13 @@ int foo(char a, unsigned char b, signed char c);
18941903int foo(char a, unsigned char b, signed char c); // test a duplicate prototype
18951904void bar(uint8_t a, uint16_t b, uint32_t c, uint64_t d);
18961905void baz(int8_t a, int16_t b, int32_t c, int64_t d);
1897 )SOURCE", R"OUTPUT(pub extern fn foo(a: u8, b: u8, c: i8) -> c_int;
1906 )SOURCE", 1, R"OUTPUT(pub extern fn foo(a: u8, b: u8, c: i8) -> c_int;
18981907pub extern fn bar(a: u8, b: u16, c: u32, d: u64);
18991908pub extern fn baz(a: i8, b: i16, c: i32, d: i64);)OUTPUT");
19001909
19011910 add_parseh_case("noreturn attribute", R"SOURCE(
19021911void foo(void) __attribute__((noreturn));
1903 )SOURCE", R"OUTPUT(pub extern fn foo() -> unreachable;)OUTPUT");
1912 )SOURCE", 1, R"OUTPUT(pub extern fn foo() -> unreachable;)OUTPUT");
19041913
19051914 add_parseh_case("enums", R"SOURCE(
19061915enum Foo {
......@@ -1908,19 +1917,19 @@ enum Foo {
19081917 FooB,
19091918 Foo1,
19101919};
1911 )SOURCE", R"OUTPUT(export enum enum_Foo {
1920 )SOURCE", 1, R"OUTPUT(export enum enum_Foo {
19121921 A,
19131922 B,
19141923 _1,
19151924}
19161925pub const FooA = enum_Foo.A;
19171926pub const FooB = enum_Foo.B;
1918pub const Foo1 = enum_Foo._1;
1919pub const Foo = enum_Foo;)OUTPUT");
1927pub const Foo1 = enum_Foo._1;)OUTPUT",
1928 R"OUTPUT(pub const Foo = enum_Foo;)OUTPUT");
19201929
19211930 add_parseh_case("restrict -> noalias", R"SOURCE(
19221931void foo(void *restrict bar, void *restrict);
1923 )SOURCE", R"OUTPUT(pub const c_void = u8;
1932 )SOURCE", 1, R"OUTPUT(pub const c_void = u8;
19241933pub extern fn foo(noalias bar: ?&c_void, noalias arg1: ?&c_void);)OUTPUT");
19251934
19261935 add_parseh_case("simple struct", R"SOURCE(
......@@ -1928,11 +1937,11 @@ struct Foo {
19281937 int x;
19291938 char *y;
19301939};
1931 )SOURCE", R"OUTPUT(export struct struct_Foo {
1940 )SOURCE", 2,
1941 R"OUTPUT(export struct struct_Foo {
19321942 x: c_int,
19331943 y: ?&u8,
1934}
1935pub const Foo = struct_Foo;)OUTPUT");
1944})OUTPUT", R"OUTPUT(pub const Foo = struct_Foo;)OUTPUT");
19361945
19371946 add_parseh_case("qualified struct and enum", R"SOURCE(
19381947struct Foo {
......@@ -1944,7 +1953,7 @@ enum Bar {
19441953 BarB,
19451954};
19461955void func(struct Foo *a, enum Bar **b);
1947 )SOURCE", R"OUTPUT(export struct struct_Foo {
1956 )SOURCE", 2, R"OUTPUT(export struct struct_Foo {
19481957 x: c_int,
19491958 y: c_int,
19501959}
......@@ -1954,36 +1963,45 @@ export enum enum_Bar {
19541963}
19551964pub const BarA = enum_Bar.A;
19561965pub const BarB = enum_Bar.B;
1957pub extern fn func(a: ?&struct_Foo, b: ?&?&enum_Bar);
1958pub const Foo = struct_Foo;
1966pub extern fn func(a: ?&struct_Foo, b: ?&?&enum_Bar);)OUTPUT",
1967 R"OUTPUT(pub const Foo = struct_Foo;
19591968pub const Bar = enum_Bar;)OUTPUT");
19601969
19611970 add_parseh_case("constant size array", R"SOURCE(
19621971void func(int array[20]);
1963 )SOURCE", R"OUTPUT(pub extern fn func(array: [20]c_int);)OUTPUT");
1972 )SOURCE", 1, R"OUTPUT(pub extern fn func(array: [20]c_int);)OUTPUT");
19641973
19651974
19661975 add_parseh_case("self referential struct with function pointer", R"SOURCE(
19671976struct Foo {
19681977 void (*derp)(struct Foo *foo);
19691978};
1970 )SOURCE", R"OUTPUT(export struct struct_Foo {
1979 )SOURCE", 2, R"OUTPUT(export struct struct_Foo {
19711980 derp: ?extern fn (?&struct_Foo),
1972}
1973pub const Foo = struct_Foo;)OUTPUT");
1981})OUTPUT", R"OUTPUT(pub const Foo = struct_Foo;)OUTPUT");
19741982
19751983
19761984 add_parseh_case("struct prototype used in func", R"SOURCE(
19771985struct Foo;
19781986struct Foo *some_func(struct Foo *foo, int x);
1979 )SOURCE", R"OUTPUT(pub const struct_Foo = u8;
1980pub extern fn some_func(foo: ?&struct_Foo, x: c_int) -> ?&struct_Foo;
1981pub const Foo = struct_Foo;)OUTPUT");
1987 )SOURCE", 2, R"OUTPUT(pub const struct_Foo = u8;
1988pub extern fn some_func(foo: ?&struct_Foo, x: c_int) -> ?&struct_Foo;)OUTPUT",
1989 R"OUTPUT(pub const Foo = struct_Foo;)OUTPUT");
19821990
19831991
19841992 add_parseh_case("#define a char literal", R"SOURCE(
19851993#define A_CHAR 'a'
1986 )SOURCE", R"OUTPUT(pub const A_CHAR = 'a';)OUTPUT");
1994 )SOURCE", 1, R"OUTPUT(pub const A_CHAR = 'a';)OUTPUT");
1995
1996
1997 add_parseh_case("#define an unsigned integer literal", R"SOURCE(
1998#define CHANNEL_COUNT 24
1999 )SOURCE", 1, R"OUTPUT(pub const CHANNEL_COUNT = 24;)OUTPUT");
2000
2001 add_parseh_case("overide previous #define", R"SOURCE(
2002#define A_CHAR 'a'
2003#define A_CHAR 'b'
2004 )SOURCE", 1, "pub const A_CHAR = 'b';");
19872005}
19882006
19892007static void print_compiler_invocation(TestCase *test_case) {
......@@ -2007,7 +2025,7 @@ static void run_test(TestCase *test_case) {
20072025 int return_code;
20082026 os_exec_process(zig_exe, test_case->compiler_args, &return_code, &zig_stderr, &zig_stdout);
20092027
2010 if (test_case->compile_errors.length) {
2028 if (!test_case->is_parseh && test_case->compile_errors.length) {
20112029 if (return_code) {
20122030 for (int i = 0; i < test_case->compile_errors.length; i += 1) {
20132031 const char *err_text = test_case->compile_errors.at(i);
......@@ -2045,14 +2063,18 @@ static void run_test(TestCase *test_case) {
20452063 exit(1);
20462064 }
20472065
2048 if (!strstr(buf_ptr(&zig_stdout), test_case->output)) {
2049 printf("\n");
2050 printf("========= Expected this output: =========\n");
2051 printf("%s\n", test_case->output);
2052 printf("================================================\n");
2053 print_compiler_invocation(test_case);
2054 printf("%s\n", buf_ptr(&zig_stdout));
2055 exit(1);
2066 for (int i = 0; i < test_case->compile_errors.length; i += 1) {
2067 const char *output = test_case->compile_errors.at(i);
2068
2069 if (!strstr(buf_ptr(&zig_stdout), output)) {
2070 printf("\n");
2071 printf("========= Expected this output: =========\n");
2072 printf("%s\n", output);
2073 printf("================================================\n");
2074 print_compiler_invocation(test_case);
2075 printf("%s\n", buf_ptr(&zig_stdout));
2076 exit(1);
2077 }
20562078 }
20572079 } else {
20582080 Buf program_stderr = BUF_INIT;