Hi,

I'm not sure if this is the right list, if not please redirect me.


So, I tried compiling luametatex using g++, and noticed some things that 
prevent it from also being considered valid C++ code.

Two general problems are:

1) Initialization order. C++ wants initializers to be specified in the order in 
which they will actually initialize the object, i.e. in the order of the 
members.

2) Reserved names. Some variables are named `class` or `template`, which of 
course, breaks C++.

But more interesting are the specific pieces of code that get rejected, because 
C++ is a bit more strict than C in some cases.

3) We get one complaint about comparing a pointer to an integer using <, which  
I believe is an actual bug in the code.
4) A second complaint is about dropping const in `tex_to_cstring`. I don't 
think this causes an actual error in the program, though.
5) Some of the `goto`s cause problems, because the jump skips over the 
initialization of variables in the target scope. Again, these variables are not 
used, so I don't think there is an actual error. The easy fix here is to just 
move the code from the `goto` target into its own function, which is probably 
better for readability anyway.

6) The last problem is that things break with g++ if the marco `infinity` is 
defined.

I've attached patches for 3), 4), and 5), as I think that these benefit code 
quality in general. I can also send ones for 1) and 2), if there is interest. 
6) seems to be a more general incompatibility, I'm not sure if there is a "fix" 
that doesn't require #if s.

Kind regards,

Erik
Index: source/tex/texmlist.c
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/source/tex/texmlist.c b/source/tex/texmlist.c
--- a/source/tex/texmlist.c	(revision e065af67f70265bc58297b005c8887aa217c1cde)
+++ b/source/tex/texmlist.c	(revision 94c9ec7a07287fa9c63f722be67d96262fb1cf0d)
@@ -1662,7 +1662,7 @@
     /*tex integer part of |m| */
     *n = tex_x_over_n_r(m, unity, f);
     /*tex the new glue specification */
-    if (f < 0) {
+    if (*f < 0) {
         --n;
         f += unity;
     }
Index: source/lua/lmttexlib.c
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/source/lua/lmttexlib.c b/source/lua/lmttexlib.c
--- a/source/lua/lmttexlib.c	(revision ddfc90669a2f10e64c4635369bcb3c82f7a8adb5)
+++ b/source/lua/lmttexlib.c	(revision 1b77819a3301db5a7fddd24551c4e59ba775e459)
@@ -2688,7 +2688,7 @@
         default:
             {
                 int texstr = tex_the_scanned_result();
-                char *str = tex_to_cstring(texstr);
+                const char *str = tex_to_cstring(texstr);
                 if (str) {
                     lua_pushstring(L, str);
                 } else {
@@ -3580,7 +3580,7 @@
                     for (int cs = 0; cs < prim_size; cs++) {
                         strnumber s = get_prim_text(cs);
                         if (s > 0) {
-                            char *prm = tex_to_cstring(s);
+                            const char *prm = tex_to_cstring(s);
                             texlib_aux_enableprimitive(pre, lpre, prm);
                         }
                     }
Index: source/lua/lmttokenlib.c
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/source/lua/lmttokenlib.c b/source/lua/lmttokenlib.c
--- a/source/lua/lmttokenlib.c	(revision ddfc90669a2f10e64c4635369bcb3c82f7a8adb5)
+++ b/source/lua/lmttokenlib.c	(revision 1b77819a3301db5a7fddd24551c4e59ba775e459)
@@ -2370,7 +2370,7 @@
     while (cs < prim_size) {
         strnumber s = get_prim_text(cs);
         if (s > 0 && (get_prim_origin(cs) != no_command)) {
-            char *ss = tex_to_cstring(s);
+            const char *ss = tex_to_cstring(s);
             int cmd = prim_eq_type(cs);
             int chr = prim_equiv(cs);
             if (! raw) {
Index: source/tex/texmaincontrol.c
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/source/tex/texmaincontrol.c b/source/tex/texmaincontrol.c
--- a/source/tex/texmaincontrol.c	(revision ddfc90669a2f10e64c4635369bcb3c82f7a8adb5)
+++ b/source/tex/texmaincontrol.c	(revision 1b77819a3301db5a7fddd24551c4e59ba775e459)
@@ -5896,7 +5896,7 @@
                 strnumber s = tex_aux_scan_string();
                 if (error_help_par) {
                     strnumber helpinfo = tex_tokens_to_string(error_help_par);
-                    char *h = tex_to_cstring(helpinfo);
+                    const char *h = tex_to_cstring(helpinfo);
                     tex_handle_error(
                         normal_error_type,
                         "%T",
Index: source/tex/texstringpool.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/source/tex/texstringpool.h b/source/tex/texstringpool.h
--- a/source/tex/texstringpool.h	(revision ddfc90669a2f10e64c4635369bcb3c82f7a8adb5)
+++ b/source/tex/texstringpool.h	(revision 1b77819a3301db5a7fddd24551c4e59ba775e459)
@@ -107,6 +107,6 @@
                    
 extern void         tex_compact_string_pool    (void);
                   
-inline static char *tex_to_cstring             (int s) { return str_length(s) > 0 ? (char *) str_string(s) : ""; }
+inline static const char *tex_to_cstring       (int s) { return str_length(s) > 0 ? (char *) str_string(s) : ""; }
 
 # endif

Index: source/tex/texscanning.c
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/source/tex/texscanning.c b/source/tex/texscanning.c
--- a/source/tex/texscanning.c	(revision 46da4d8a3fda7797830b1385f8e31c19ab0750c3)
+++ b/source/tex/texscanning.c	(revision e065af67f70265bc58297b005c8887aa217c1cde)
@@ -1890,6 +1890,18 @@
 
 */
 
+static inline void tex_aux_scan_int_nonumber() {
+    /*tex Express astonishment that no number was here */
+    if (lmt_error_state.intercept) {
+        lmt_error_state.last_intercept = 1 ;
+        if (cur_cmd != spacer_cmd) {
+            tex_back_input(cur_tok);
+        }
+    } else {
+        tex_aux_missing_number_error();
+    }
+}
+
 halfword tex_scan_int(int optional_equal, int *radix)
 {
     int negative = 0;
@@ -1959,7 +1971,7 @@
         result = tex_aux_scan_something_internal(cur_cmd, cur_chr, int_val_level, 0, 0);
         if (cur_val_level != int_val_level) {
             result = 0;
-            goto NONUMBER;
+            tex_aux_scan_int_nonumber();
         }
     } else if (cur_cmd == math_style_cmd) {
         /* A pity that we need to check this way in |scan_int|. */
@@ -1970,7 +1982,7 @@
             result = cur_chr;
         } else {
             result = 0;
-            goto NONUMBER;
+            tex_aux_scan_int_nonumber();
         }
     } else {
         /*tex has an error message been issued? */
@@ -2077,16 +2089,7 @@
         }
       DONE:
         if (vacuous) {
-            NONUMBER:
-            /*tex Express astonishment that no number was here */
-            if (lmt_error_state.intercept) {
-                lmt_error_state.last_intercept = 1 ;
-                if (cur_cmd != spacer_cmd) {
-                    tex_back_input(cur_tok);
-                }
-            } else {
-                tex_aux_missing_number_error();
-            }
+            tex_aux_scan_int_nonumber();
         } else {
             tex_push_back(cur_tok, cur_cmd, cur_chr);
         }
@@ -3255,6 +3258,16 @@
     }
 }
 
+static inline halfword tex_aux_scan_font_id_bad() {
+    tex_handle_error(
+        back_error_type,
+        "Missing or invalid font identifier (or equivalent) or integer (register or otherwise)",
+        "I was looking for a control sequence whose current meaning has been defined by\n"
+        "\\font or a valid font id number."
+    );
+    return null_font;
+}
+
 halfword tex_scan_font_identifier(halfword *spec)
 {
     /*tex Get the next non-blank non-call. */
@@ -3289,7 +3302,7 @@
                 if (tex_is_valid_font(fnt)) {
                     return fnt;
                 } else {
-                    goto BAD;
+                    return tex_aux_scan_font_id_bad();
                 }
             }
         case internal_int_cmd:
@@ -3301,7 +3314,7 @@
                         return fnt;
                     }
                 }
-                goto BAD;
+                return tex_aux_scan_font_id_bad();
             }
         default:
             {
@@ -3315,14 +3328,7 @@
                 } else {
                     /*tex Fall through to a font error message. */
                 }
-              BAD:
-                tex_handle_error(
-                    back_error_type,
-                    "Missing or invalid font identifier (or equivalent) or integer (register or otherwise)",
-                    "I was looking for a control sequence whose current meaning has been defined by\n"
-                    "\\font or a valid font id number."
-                );
-                return null_font;
+                return tex_aux_scan_font_id_bad();
             }
     }
 }
@@ -4869,7 +4875,7 @@
         result = tex_aux_scan_something_internal(cur_cmd, cur_chr, int_val_level, 0, 0);
         if (cur_val_level != int_val_level) {
             result = 0;
-            goto NONUMBER;
+            tex_aux_missing_number_error();
         }
     } else if (cur_cmd == math_style_cmd) {
         result = (cur_chr == yet_unset_math_style) ? tex_scan_math_style_identifier(0, 0) : cur_chr;
@@ -4878,7 +4884,7 @@
             result = cur_chr;
         } else {
             result = 0;
-            goto NONUMBER;
+            tex_aux_missing_number_error();
         }
     } else {
         int vacuous = 1;
@@ -4966,7 +4972,6 @@
         }
       DONE:
         if (vacuous) {
-          NONUMBER:
             tex_aux_missing_number_error();
         } else {
             tex_push_back(cur_tok, cur_cmd, cur_chr);
_______________________________________________
dev-context mailing list
dev-context@ntg.nl
https://mailman.ntg.nl/mailman/listinfo/dev-context

Reply via email to