Hi all,

I was working with some C++ code and I got lots and lots of warnings on
my terminal like this:

test.cpp:175:58: warning: ISO C++ forbids converting a string constant to 
‘char*’ [-Wwrite-strings]
 lf[4]=C_h_intern(&lf[4],25,"\003sysimplicit-exit-handler");
                                                          ^
test.cpp: At global scope:
test.cpp:191:12: warning: ISO C++ forbids converting a string constant to 
‘char*’ [-Wwrite-strings]
 {NULL,NULL}};
            ^
test.cpp:191:12: warning: ISO C++ forbids converting a string constant to 
‘char*’ [-Wwrite-strings]
test.cpp:191:12: warning: ISO C++ forbids converting a string constant to 
‘char*’ [-Wwrite-strings]
test.cpp:191:12: warning: ISO C++ forbids converting a string constant to 
‘char*’ [-Wwrite-strings]
test.cpp:191:12: warning: ISO C++ forbids converting a string constant to 
‘char*’ [-Wwrite-strings]

This is quite noisy and annoying, causing real issues to disappear.
It's easily fixed by wrapping all the strings in C_text() calls, so that
they're casted to char *, even though that's not really a "proper" fix
we do that everywhere.  And trying to get everything const-correct is
a hell of a job, not to mention a breaking API change (if there are any
takers who want fix this in CHICKEN 5, now's the time!)

After having fixed those warnings, I got a runtime error (segfault) due
to a symbol being undefined (though with CHICKEN 5 I got a linking error),
the function C_register_debug_info was added at the top of the chicken.h
file, where it wasn't wrapped in extern "C" { ... }, so it gets
name-mangled by the C++ compiler.  I just moved it down to the rest of the
function prototypes, which are wrapped in such a block (via C_BEGIN_C_DECLS).
This error happens only when compiling with -debug-info, BTW.

This is something that I encountered with CHICKEN 4 while working on an
actual project, so I think it's definitely worth fixing in both branches,
so I attached two patches.

I might've missed a few string constants; I don't really know how to
easily find them, except by accidentally writing code which happens to
trigger generated C that emits such warnings.

Cheers,
Peter
From 36d97bd058cd597198ee5ac0fb33d632e9f12b10 Mon Sep 17 00:00:00 2001
From: Peter Bex <[email protected]>
Date: Sat, 4 Nov 2017 18:40:26 +0100
Subject: [PATCH] Fix several C++ compilation issues.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- The compiler generates several calls to C functions that accept a
   "char *" as type, but with string constants.  Those must be wrapped
   in C_text() to shut up these warnings, which get spammed like crazy:
   "blah.cpp:1147:12: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]"
- When compiling a program with -debug-info or -d3, the resulting
   binary will fail with a segfault due to unresolved symbols, which
   are caused by name mangling: C_register_debug_info is defined
   outside an extern "C" { ... } block.
- C_header_size returns a C_header, which is a C_uword.  This results
   in another warning from C_u_i_string_equal_p():
   "chicken.h:2262:22:warning: comparison between signed and unsigned integer expressions [-Wsign-compare]"
---
 NEWS          |  2 ++
 c-backend.scm | 22 +++++++++++-----------
 chicken.h     | 10 +++-------
 3 files changed, 16 insertions(+), 18 deletions(-)

diff --git a/NEWS b/NEWS
index c849aede..d0927e58 100644
--- a/NEWS
+++ b/NEWS
@@ -145,6 +145,8 @@
   - The scheduler no longer indirectly hangs on to the old thread
     when switching to a new one, which caused excessive memory
     consumption (#1367, thanks to "megane").
+  - C++ programs no longer fail with a symbol lookup error when
+    compiled with debugger support (-d3 or -debug-info).
 
 - Syntax expander
   - Renaming an identifier twice no longer results in an undo of the
diff --git a/c-backend.scm b/c-backend.scm
index d7b8844b..c16e8dbb 100644
--- a/c-backend.scm
+++ b/c-backend.scm
@@ -218,9 +218,9 @@
 	       (cond [block
 		      (if safe
 			  (gen "lf[" index "]")
-			  (gen "C_retrieve2(lf[" index "]," 
+			  (gen "C_retrieve2(lf[" index "],C_text("
 			       (c-ify-string (##sys#symbol->qualified-string
-					      (fourth params))) #\)) ) ]
+					      (fourth params))) "))") ) ]
 		     [safe (gen "*((C_word*)lf[" index "]+1)")]
 		     [else (gen "C_fast_retrieve(lf[" index "])")] ) ) )
 
@@ -332,8 +332,8 @@
 			       (set! carg (string-append "lf[" (number->string index) "]"))
 			       (if safe
 				   (gen "C_fast_retrieve_proc(" carg ")")
-				   (gen "C_retrieve2_symbol_proc(" carg "," 
-					(c-ify-string (##sys#symbol->qualified-string (fourth gparams))) #\)) ) )
+				   (gen "C_retrieve2_symbol_proc(" carg ",C_text("
+					(c-ify-string (##sys#symbol->qualified-string (fourth gparams))) "))") ) )
 			      (safe
 			       (set! carg 
 				 (string-append "*((C_word*)lf[" (number->string index) "]+1)"))
@@ -742,15 +742,15 @@
 		    [cstr (c-ify-string str)]
 		    [len (##sys#size str)] )
 	       (gen #t to "=")
-	       (gen "C_h_intern(&" to #\, len #\, cstr ");") ) )
+	       (gen "C_h_intern(&" to #\, len ", C_text(" cstr "));") ) )
 	    ((null? lit) 
 	     (gen #t to "=C_SCHEME_END_OF_LIST;") )
 	    ((and (not (##sys#immediate? lit)) ; nop
 		  (##core#inline "C_lambdainfop" lit)))
 	    ((or (fixnum? lit) (not (##sys#immediate? lit)))
-	     (gen #t to "=C_decode_literal(C_heaptop,")
+	     (gen #t to "=C_decode_literal(C_heaptop,C_text(")
 	     (gen-string-constant (encode-literal lit))
-	     (gen ");") )
+	     (gen "));") )
 	    (else (bad-literal lit))))
 
     (define (gen-string-constant str)
@@ -959,9 +959,9 @@
    (lambda (info)
      (gen #t "{" (second info) ",0,")
      (for-each
-       (lambda (x)
-         (gen "\"" (backslashify (->string x)) "\","))
-       (cddr info))
+      (lambda (x)
+	(gen "C_text(\"" (backslashify (->string x)) "\"),"))
+      (cddr info))
      (gen "},"))
    (sort dbg-info-table (lambda (i1 i2) (< (car i1) (car i2)))))
   (gen #t "{0,0,NULL,NULL}};\n"))
@@ -976,7 +976,7 @@
    (lambda (p)
      (let ((id (car p))
 	   (ll (cdr p)))
-       (gen #t "{\"" id #\: (string->c-identifier sf) "\",(void*)")
+       (gen #t "{C_text(\"" id #\: (string->c-identifier sf) "\"),(void*)")
        (if (eq? 'toplevel id)
 	   (gen "C_" (toplevel unit-name) "},")
 	   (gen id "},") ) ) )
diff --git a/chicken.h b/chicken.h
index 400c80e2..dfe4bb1c 100644
--- a/chicken.h
+++ b/chicken.h
@@ -1651,10 +1651,7 @@ typedef struct C_DEBUG_INFO {
 #define C_DEBUG_LISTEN              6
 #define C_DEBUG_INTERRUPTED         7
 
-#define C_debugger(cell, c, av)     (C_debugger_hook != NULL ? C_debugger_hook(cell, c, av, __FILE__, __LINE__) : C_SCHEME_UNDEFINED)
-
-C_fctexport void C_register_debug_info(C_DEBUG_INFO *);
-
+#define C_debugger(cell, c, av)     (C_debugger_hook != NULL ? C_debugger_hook(cell, c, av, C_text(__FILE__), __LINE__) : C_SCHEME_UNDEFINED)
 
 /* Variables: */
 
@@ -1719,6 +1716,7 @@ C_varextern C_TLS C_word (*C_get_unbound_variable_value_hook)(C_word sym);
 
 C_BEGIN_C_DECLS
 
+C_fctexport void C_register_debug_info(C_DEBUG_INFO *);
 C_fctexport int CHICKEN_main(int argc, char *argv[], void *toplevel);
 C_fctexport int CHICKEN_initialize(int heap, int stack, int symbols, void *toplevel);
 C_fctexport C_word CHICKEN_run(void *toplevel);
@@ -2577,9 +2575,7 @@ inline static C_ulong C_num_to_unsigned_long(C_word x)
 
 inline static C_word C_u_i_string_equal_p(C_word x, C_word y)
 {
-  C_word n;
-
-  n = C_header_size(x);
+  C_uword n = C_header_size(x);
   return C_mk_bool(n == C_header_size(y)
          && !C_memcmp((char *)C_data_pointer(x), (char *)C_data_pointer(y), n));
 }
-- 
2.11.0

From 47842ccc8b60c2c0a5215b0274985d811bdf1a51 Mon Sep 17 00:00:00 2001
From: Peter Bex <[email protected]>
Date: Sat, 4 Nov 2017 18:40:26 +0100
Subject: [PATCH] Fix several C++ compilation issues.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- The compiler generates several calls to C functions that accept a
   "char *" as type, but with string constants.  Those must be wrapped
   in C_text() to shut up these warnings, which get spammed like crazy:
   "blah.cpp:1147:12: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]"
- When compiling a program with -debug-info or -d3, the resulting
   binary will fail with a segfault due to unresolved symbols, which
   are caused by name mangling: C_register_debug_info is defined
   outside an extern "C" { ... } block.
- C_header_size returns a C_header, which is a C_uword.  This results
   in another warning from C_u_i_string_equal_p():
   "chicken.h:2262:22:warning: comparison between signed and unsigned integer expressions [-Wsign-compare]"
---
 NEWS          |  2 ++
 c-backend.scm | 18 +++++++++---------
 chicken.h     | 10 +++-------
 3 files changed, 14 insertions(+), 16 deletions(-)

diff --git a/NEWS b/NEWS
index 397fd532..48a48402 100644
--- a/NEWS
+++ b/NEWS
@@ -26,6 +26,8 @@
   - The scheduler no longer indirectly hangs on to the old thread
     when switching to a new one, which caused excessive memory
     consumption (#1367, thanks to "megane").
+  - C++ programs no longer fail with a symbol lookup error when
+    compiled with debugger support (-d3 or -debug-info).
 
 - Syntax expander
   - Renaming an identifier twice no longer results in an undo of the
diff --git a/c-backend.scm b/c-backend.scm
index 3e742c9f..18052078 100644
--- a/c-backend.scm
+++ b/c-backend.scm
@@ -190,9 +190,9 @@
 	       (cond [block
 		      (if safe
 			  (gen "lf[" index "]")
-			  (gen "C_retrieve2(lf[" index "]," 
+			  (gen "C_retrieve2(lf[" index "],C_text("
 			       (c-ify-string (##sys#symbol->qualified-string
-					      (fourth params))) #\)) ) ]
+					      (fourth params))) "))") ) ]
 		     [safe (gen "*((C_word*)lf[" index "]+1)")]
 		     [else (gen "C_fast_retrieve(lf[" index "])")] ) ) )
 
@@ -304,8 +304,8 @@
 			       (set! carg (string-append "lf[" (number->string index) "]"))
 			       (if safe
 				   (gen "C_fast_retrieve_proc(" carg ")")
-				   (gen "C_retrieve2_symbol_proc(" carg "," 
-					(c-ify-string (##sys#symbol->qualified-string (fourth gparams))) #\)) ) )
+				   (gen "C_retrieve2_symbol_proc(" carg ",C_text("
+					(c-ify-string (##sys#symbol->qualified-string (fourth gparams))) "))") ) )
 			      (safe
 			       (set! carg 
 				 (string-append "*((C_word*)lf[" (number->string index) "]+1)"))
@@ -707,15 +707,15 @@
 		    [cstr (c-ify-string str)]
 		    [len (##sys#size str)] )
 	       (gen #t to "=")
-	       (gen "C_h_intern(&" to #\, len #\, cstr ");") ) )
+	       (gen "C_h_intern(&" to #\, len ", C_text(" cstr "));") ) )
 	    ((null? lit) 
 	     (gen #t to "=C_SCHEME_END_OF_LIST;") )
 	    ((and (not (##sys#immediate? lit)) ; nop
 		  (##core#inline "C_lambdainfop" lit)))
 	    ((or (fixnum? lit) (not (##sys#immediate? lit)))
-	     (gen #t to "=C_decode_literal(C_heaptop,")
+	     (gen #t to "=C_decode_literal(C_heaptop,C_text(")
 	     (gen-string-constant (encode-literal lit))
-	     (gen ");") )
+	     (gen "));") )
 	    (else (bad-literal lit))))
 
     (define (gen-string-constant str)
@@ -925,7 +925,7 @@
      (gen #t "{" (second info) ",0,")
      (for-each
       (lambda (x)
-	(gen "\"" (backslashify (->string x)) "\","))
+	(gen "C_text(\"" (backslashify (->string x)) "\"),"))
       (cddr info))
      (gen "},"))
    (sort dbg-info-table (lambda (i1 i2) (< (car i1) (car i2)))))
@@ -939,7 +939,7 @@
        #t "static C_PTABLE_ENTRY ptable[" (add1 (##sys#hash-table-size lambda-table)) "] = {")
   (##sys#hash-table-for-each
    (lambda (id ll)
-     (gen #t "{\"" id #\: (string->c-identifier sf) "\",(void*)")
+     (gen #t "{C_text(\"" id #\: (string->c-identifier sf) "\"),(void*)")
      (if (eq? 'toplevel id)
          (if unit-name
              (gen "C_" unit-name "_toplevel},")
diff --git a/chicken.h b/chicken.h
index 4ecb52cc..ef62fd4d 100644
--- a/chicken.h
+++ b/chicken.h
@@ -1613,10 +1613,7 @@ typedef struct C_DEBUG_INFO {
 #define C_DEBUG_LISTEN              6
 #define C_DEBUG_INTERRUPTED         7
 
-#define C_debugger(cell, c, av)     (C_debugger_hook != NULL ? C_debugger_hook(cell, c, av, __FILE__, __LINE__) : C_SCHEME_UNDEFINED)
-
-C_fctexport void C_register_debug_info(C_DEBUG_INFO *);
-
+#define C_debugger(cell, c, av)     (C_debugger_hook != NULL ? C_debugger_hook(cell, c, av, C_text(__FILE__), __LINE__) : C_SCHEME_UNDEFINED)
 
 /* Variables: */
 
@@ -1674,6 +1671,7 @@ C_varextern C_TLS C_word (*C_get_unbound_variable_value_hook)(C_word sym);
 
 C_BEGIN_C_DECLS
 
+C_fctexport void C_register_debug_info(C_DEBUG_INFO *);
 C_fctexport int CHICKEN_main(int argc, char *argv[], void *toplevel);
 C_fctexport int CHICKEN_initialize(int heap, int stack, int symbols, void *toplevel);
 C_fctexport C_word CHICKEN_run(void *toplevel);
@@ -2256,9 +2254,7 @@ C_inline C_ulong C_num_to_unsigned_long(C_word x)
 
 C_inline C_word C_u_i_string_equal_p(C_word x, C_word y)
 {
-  C_word n;
-
-  n = C_header_size(x);
+  C_uword n = C_header_size(x);
   return C_mk_bool(n == C_header_size(y)
          && !C_memcmp((char *)C_data_pointer(x), (char *)C_data_pointer(y), n));
 }
-- 
2.11.0

Attachment: signature.asc
Description: PGP signature

_______________________________________________
Chicken-hackers mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/chicken-hackers

Reply via email to