Hello,

I pushed a commit (c85eface68940368f696807e97ac7d707e8a73eb) that treats 
undefined macro invocations as undefined. Previously, if "WUMBO" were 
undefined, a macro invocation such as "WUMBO(O_RDONLY)" would evaluate to 
"0(0)" and the compiler would report this as a syntax error. Now, any undefined 
macro invocation (with parameters) will just evaluated to 0 and the parameters 
will be ignored.

I created this changed based on a real-world need: nolibc (included with the 
Linux kernel source) makes use of the __has_attribute macro, which seems to be 
built-in to GCC, but was defined in such a way in nolibc where it would not be 
defined everywhere it was used. This meant that compiling nolibc with
TinyCC would treat __has_attribute as 0 and its parameters as (0), as described 
above.

You could argue that this is undesirable behavior. I didn't know I could just 
push right to the mob branch, so I was expecting a little more debate and 
review before submitting.

The patch is attached.

Cheers,

- Jonathan M. Wilbur
diff --git a/tccpp.c b/tccpp.c
index 6f2bc173..a59ba814 100644
--- a/tccpp.c
+++ b/tccpp.c
@@ -1506,6 +1506,20 @@ static int expr_preprocess(TCCState *s1)
             tokc.i = c;
         } else {
             /* if undefined macro, replace with zero */
+            next_nomacro();
+            // If the undefined macro is followed by parens, just skip them.
+            if (tok == '(') {
+                int bracket_depth = 1;
+                while (bracket_depth > 0) {
+                    next();
+                    if (tok == '(')
+                        bracket_depth++;
+                    else if (tok == ')')
+                        bracket_depth--;
+                }
+            } else {
+                unget_tok(tok); // Is this actually the function I want?
+            }
             tok = TOK_CINT;
             tokc.i = 0;
         }
diff --git a/tests/undef_func_macro.c b/tests/undef_func_macro.c
new file mode 100644
index 00000000..00db8ad0
--- /dev/null
+++ b/tests/undef_func_macro.c
@@ -0,0 +1,13 @@
+int main () {
+// This used to evaluate to 0 (0), which is invalid.
+// Now it should evaluate to 0.
+#if WUMBOED(WUMBO)
+#endif
+
+// Just trying a more complicated test case.
+#if WUMBO && defined(WUMBOLOGY) || WUMBOED(WUMBO) && !WUMBOLOGY
+    return 0;
+#elif WUMBO && defined(WUMBOLOGY) || WUMBOED(WUMBO) && !WUMBOLOGY
+    return 1;
+#endif
+}
_______________________________________________
Tinycc-devel mailing list
Tinycc-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to