Hi,

I've attached a new patch which tests for dollars in identifiers in C.

I've added a new parsing flag `PARSE_FLAG_ASM` to block the use of dollar identifiers when parsing asm. This could probably be combined with the `PARSE_FLAG_ASM_COMMENTS` flag.

Currently the flag only blocks identifiers in asm _starting_ with a dollar. I think this is the only syntactically valid place in which dollars occurring could conflict with gas asm, but they might appear inside identifiers if the programmer is either referencing a symbol with a dollar in _or_ if they've made a syntax error. I'll look into it more and do some more testing.

I'm on 64 bit and I have to hack the makefiles to run the assembly tests because they are written in 32-bit x86. Does anyone have a 32-bit machine they are testing tcc on?

- Dan


On 11/04/15 14:09, Sergey Korshunoff wrote:
Hi!
although in almost all cases it is enabled by default
This can be done by using FD_INVERT insteed of 0 in the patch. But
keeping an old behavior of the tcc is safer (I think).

so they also provide "-fno-dollars-in-identifiers"
this works in tcc too.  Try to test.


2015-04-11 14:49 GMT+03:00, Daniel Holden <[email protected]>:
Hi,

I like the idea of using std=c99 to disable. I believe this is how
gcc/clang handle it. But ultimately I don't mind too much. gcc/clang use
the switch "-fdollars-in-identifiers" (I've attached a modified patch)
although in almost all cases it is enabled by default so they also
provide "-fno-dollars-in-identifiers".

https://gcc.gnu.org/onlinedocs/gcc-3.0.4/gcc_3.html

I'm mainly interested in this change because I'm working on a new
version of my library Cello: http://libcello.org/ which uses `$` and
several variations of as macros. There is also RayLanguage which also
uses it as a macro for a kind of ObjC style message passing:
https://github.com/kojiba/RayLanguage . But I can also put together some
test cases using it in the various ways (variable, function, macro,
define, etc) so make sure it is completely covered.

Thanks,

Dan

On 11/04/2015 10:00, Thomas Preud'homme wrote:
Le samedi 11 avril 2015, 06:14:04 Sergey Korshunoff a écrit :
Hi! A modified version of the patch is attached.
Looks good at quick look. I'm not sure about the switch, we already
accept
some gnu extension without any switch for that. Or maybe introduce a more
general switch for all C extensions. You could enable it by default en
disable
it if std=c99 for instance?

Best regards,

Thomas


_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel


--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

--- ../tcc-0.9.26/tcc.h	2013-02-15 14:24:00.000000000 +0000
+++ tcc.h	2015-04-13 15:15:07.421945189 +0100
@@ -528,6 +528,7 @@
     /* C language options */
     int char_is_unsigned;
     int leading_underscore;
+    int dollars_in_identifiers;	/* allows '$' in indentifiers */
     
     /* warning switches */
     int warn_write_strings;
@@ -1083,6 +1084,7 @@
                                         returned at eof */
 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
 #define PARSE_FLAG_SPACES     0x0010 /* next() returns space tokens (for -E) */
+#define PARSE_FLAG_ASM        0x0020 /* currently parsing assembly */
 
 ST_FUNC TokenSym *tok_alloc(const char *str, int len);
 ST_FUNC char *get_tok_str(int v, CValue *cv);
--- ../tcc-0.9.26/libtcc.c	2013-02-15 14:24:00.000000000 +0000
+++ libtcc.c	2015-04-13 15:15:33.357944813 +0100
@@ -1423,6 +1423,7 @@
     { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
     { offsetof(TCCState, nocommon), FD_INVERT, "common" },
     { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
+    { offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" },
 };
 
 /* set/reset a flag */
--- ../tcc-0.9.26/tccpp.c	2013-02-15 14:24:00.000000000 +0000
+++ tccpp.c	2015-04-13 15:19:16.425941580 +0100
@@ -2178,7 +2178,12 @@
             }
         }
         break;
-
+    
+    /* allow $ in identifiers when not parsing asm */
+    case '$':
+        if (!tcc_state->dollars_in_identifiers
+        || (parse_flags & PARSE_FLAG_ASM)) goto parse_simple;       
+    
     case 'a': case 'b': case 'c': case 'd':
     case 'e': case 'f': case 'g': case 'h':
     case 'i': case 'j': case 'k': case 'l':
@@ -2477,8 +2482,8 @@
     case ':':
     case '?':
     case '~':
-    case '$': /* only used in assembler */
-    case '@': /* dito */
+    case '@': /* only used in assembler */
+    parse_simple:
         tok = c;
         p++;
         break;
@@ -3049,7 +3054,8 @@
 
     /* init isid table */
     for(i=CH_EOF;i<256;i++)
-        isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
+        isidnum_table[i-CH_EOF] = isid(i) || isnum(i) ||
+            (tcc_state->dollars_in_identifiers ? i == '$' : 0);
 
     /* add all tokens */
     table_ident = NULL;
--- ../tcc-0.9.26/tccasm.c	2013-02-15 14:24:00.000000000 +0000
+++ tccasm.c	2015-04-13 15:17:56.945942732 +0100
@@ -735,7 +735,7 @@
 
     ch = file->buf_ptr[0];
     tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
-    parse_flags = PARSE_FLAG_ASM_COMMENTS;
+    parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_ASM;
     if (do_preprocess)
         parse_flags |= PARSE_FLAG_PREPROCESS;
     next();
--- ../tcc-0.9.26/tcc-doc.texi	2013-02-15 14:24:00.000000000 +0000
+++ tcc-doc.texi	2015-04-10 16:53:14.697761387 +0100
@@ -590,6 +590,8 @@
 
 @item @code{#pragma pack} is supported for win32 compatibility.
 
+@item The dollar sign @code{$} is allowed in identifiers.
+
 @end itemize
 
 @section TinyCC extensions
--- ../tcc-0.9.26/tests/tests2/Makefile	2013-02-15 14:24:00.000000000 +0000
+++ ./tests/tests2/Makefile	2015-04-13 15:31:12.897931195 +0100
@@ -67,7 +67,8 @@
  51_static.test \
  52_unnamed_enum.test \
  54_goto.test \
- 55_lshift_type.test
+ 55_lshift_type.test \
+ 56_dollars.test
 
 # 30_hanoi.test -- seg fault in the code, gcc as well
 # 34_array_assignment.test -- array assignment is not in C standard
@@ -83,8 +84,10 @@
 
 %.test: %.c %.expect
 	@echo Test: $*...
-	@if [ "x`echo $* | grep args`" != "x" ]; \
+	@if  [ "x`echo $* | grep args`" != "x" ]; \
 	then $(TCC_RUN) $< - arg1 arg2 arg3 arg4 >$*.output; \
+	elif [ "x`echo $* | grep dollars`" != "x" ]; \
+	then $(TCC_RUN) -fdollars-in-identifiers $< >$*.output; \
 	else $(TCC_RUN) $< >$*.output; \
 	fi
 	@if diff -bu $(<:.c=.expect) $*.output ; \
diff -urN ../tcc-0.9.26/tests/tests2/56_dollars.c ./tests/tests2/56_dollars.c
--- ../tcc-0.9.26/tests/tests2/56_dollars.c	1970-01-01 01:00:00.000000000 +0100
+++ ./tests/tests2/56_dollars.c	2015-04-13 15:32:55.681929706 +0100
@@ -0,0 +1,42 @@
+#include <stdio.h>
+
+#define $(x) x
+#define $fred 10
+#define joe$ 20
+#define hen$y 30
+
+#define $10(x) x*10
+#define _$10(x) x/10
+
+int main()
+{
+   printf("fred=%d\n", $fred);
+   printf("joe=%d\n", joe$);
+   printf("henry=%d\n", hen$y);
+
+   printf("fred2=%d\n", $($fred));
+   printf("joe2=%d\n", $(joe$));
+   printf("henry2=%d\n", $(hen$y));
+   
+   printf("fred10=%d\n", $10($fred));
+   printf("joe_10=%d\n", _$10(joe$));
+   
+   int $ = 10; 
+   int a100$ = 100;
+   int a$$ = 1000;
+   int a$c$b = 2121;
+   int $100 = 10000;
+   const char *$$$ = "money";
+   
+   printf("local=%d\n", $);
+   printf("a100$=%d\n", a100$);
+   printf("a$$=%d\n", a$$);
+   printf("a$c$b=%d\n", a$c$b);
+   printf("$100=%d\n", $100);
+   printf("$$$=%s", $$$);
+
+   return 0;
+}
+
+/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/
+
diff -urN ../tcc-0.9.26/tests/tests2/56_dollars.expect ./tests/tests2/56_dollars.expect
--- ../tcc-0.9.26/tests/tests2/56_dollars.expect	1970-01-01 01:00:00.000000000 +0100
+++ ./tests/tests2/56_dollars.expect	2015-04-13 15:33:06.237929553 +0100
@@ -0,0 +1,14 @@
+fred=10
+joe=20
+henry=30
+fred2=10
+joe2=20
+henry2=30
+fred10=100
+joe_10=2
+local=10
+a100$=100
+a$$=1000
+a$c$b=2121
+$100=10000
+$$$=money
_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to