Fix issue where __attribute__ could get matched to the wrong token.
authorSam Jansen <sam@wand.net.nz>
Sun, 22 Apr 2012 00:32:16 +0100
changeset 302 71e524117d0a
parent 301 8a65cb678a50
child 303 f6e07f1d4ebe
Fix issue where __attribute__ could get matched to the wrong token. Unfortunately attribute support is a bit of a kludge, and this layers another kludge on top of that to make them work a bit better.
globaliser/ilex.cc
globaliser/test/attribute.c
--- a/globaliser/ilex.cc	Sun May 22 12:24:42 2011 -0700
+++ b/globaliser/ilex.cc	Sun Apr 22 00:32:16 2012 +0100
@@ -131,23 +131,28 @@
     return yylex();
   }
 
+  // Attributes are a hack. Normally, we assume they come *after* what they refer to. In some
+  // cases they come before, and this hack deals with that.
+  bool attr_hack = (last_token.token == ';') || (last_token.token == '}');
+
   // Use the saved information to set the global variables
   int tmp_token = last_token.token;
   token_text = last_token.token_text;
   ws_text = os.str();
-  attr_text = last_token.attr_text + oa.str();
-  lex_text = last_token.lex_text + ol.str() + oa.str();
+  attr_text = (attr_hack ? string() : last_token.attr_text) + oa.str();
+  lex_text = last_token.lex_text + ol.str() + (attr_hack ? string() : oa.str());
 
   // Save the currently scanned token for next time
   last_token.token = token;
   last_token.token_text = yytext;
-  last_token.attr_text = "";
-  last_token.lex_text = yytext;
+  last_token.attr_text = (attr_hack ? last_token.attr_text + oa.str() : "");
+  last_token.lex_text = (attr_hack ? oa.str() : string()) + yytext;
 
   // Check if the identifier has been typedef'd
   if (tmp_token == IDENTIFIER)
     return check_identifier(token_text.c_str());
 
+
   // Return the saved token
   return tmp_token;
 }
--- a/globaliser/test/attribute.c	Sun May 22 12:24:42 2011 -0700
+++ b/globaliser/test/attribute.c	Sun Apr 22 00:32:16 2012 +0100
@@ -1,3 +1,12 @@
+__attribute__((regparm(0))) long sys_listen(int, int);
+
+int random_var_here;
+
+__attribute__((regparm(0))) long sys_listen(int fd, int backlog)
+{
+	return 0;
+}
+
 static void
 dummynet(void * __attribute__((__unused__)) unused)
 {