Added __FUNCTION__ support
authorRobert Pengelly <robertapengelly@hotmail.com>
Tue, 8 Sep 2026 15:23:24 +0000 (16:23 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Tue, 8 Sep 2026 15:23:24 +0000 (16:23 +0100)
amd64.c
i386.c
token.c
token.h

diff --git a/amd64.c b/amd64.c
index 20c9dc006f398f083416fcff5203ced04ecc5b3f..9cf3d8977d7413bf3e995cb0da1150c9d61ba8db 100644 (file)
--- a/amd64.c
+++ b/amd64.c
@@ -39162,8 +39162,11 @@ static void parse_function_body (const char *name, int storage_class, int is_inl
     emit_function_start (name, !emit_public);
     
     statement_ends_control_flow = 0;
+    set_current_function_name (name);
     
     parse_block ();
+    
+    set_current_function_name (0);
     check_goto_labels ();
     
     if (current_function_is_noreturn && !current_function_has_return_statement && !statement_ends_control_flow) {
diff --git a/i386.c b/i386.c
index c4e8cd2e5a4c52080f480a9a7f2e1d99d2231637..7a086c07d6dcb07644536403103e3070e7599eeb 100644 (file)
--- a/i386.c
+++ b/i386.c
@@ -36679,8 +36679,11 @@ static void parse_function_body (const char *name, int storage_class, int is_inl
     emit_function_start (name, !emit_public);
     
     statement_ends_control_flow = 0;
+    set_current_function_name (name);
     
     parse_block ();
+    
+    set_current_function_name (0);
     check_goto_labels ();
     
     if (current_function_is_noreturn && !current_function_has_return_statement && !statement_ends_control_flow) {
diff --git a/token.c b/token.c
index cee195ebb45cc45561826e2c3e477d5139fa009c..e4b90ef4dbb47bfb28f3b03d09d56748308af974 100755 (executable)
--- a/token.c
+++ b/token.c
@@ -2050,6 +2050,23 @@ static char *token_collect_macro_continuation (char *line_start, char **line_end
 
 }
 
+static char *current_function_name = 0;
+
+void set_current_function_name (const char *name) {
+
+    if (current_function_name) {
+    
+        free (current_function_name);
+        current_function_name = 0;
+    
+    }
+    
+    if (name) {
+        current_function_name = xstrdup (name);
+    }
+
+}
+
 void get_token (void) {
 
     struct history *history;
@@ -2180,6 +2197,20 @@ void get_token (void) {
         
         if (tok.kind == TOK_IDENT) {
         
+            if (current_function_name && strcmp (tok.ident, "__FUNCTION__") == 0) {
+            
+                char *value = xmalloc (strlen (current_function_name) + 3);
+                sprintf (value, "\"%s\"", current_function_name);
+                
+                free (tok.ident);
+                
+                tok.ident = value;
+                tok.kind = TOK_PPSTR;
+                
+                return;
+            
+            }
+            
             tok.kind = find_kind (tok.start, tok.caret, tok.ident);;
             return;
         
diff --git a/token.h b/token.h
index 18db011401e9a1c8d23876e202ef2116bf3b81a1..302da170794dd87849d47d86a85d3fe1a6d08a24 100755 (executable)
--- a/token.h
+++ b/token.h
@@ -174,4 +174,6 @@ struct punct {
 struct punct *find_punct (char *p);
 int init_tokenizer (const char *ifile);
 
+void set_current_function_name (const char *name);
+
 #endif      /* _TOKEN_H */