Re: hypenator.cc errors

"Sebastian Geerken" <[email protected]>
Newsgroups gmane.comp.web.dillo.devel
Message-ID <[email protected]>
On Mo, Sep 02, 2013, Chris Sorenson wrote:
> > On Mo, Sep 02, 2013, Chris Sorenson wrote:
> > > Greets all,
> > > 
> > > Another sequence of bads that cropped up using the
> > > Silicon Graphics MIPSpro compiler, any thoughts? To wit:
> > > 
> > > cc-1028 CC: ERROR File = hyphenator.cc, Line = 147
> > >   The expression used must have a constant value.
> > > 
> > >      char chars [l + 1];
> > >                  ^
> > > [more similar cases]
> > 
> > It seems that your compiler does not support arrays with
> > variable length, which are standard since C99, as far as I
> > see. It could be fixed by replacing this line (as an
> > example) by:
> > 
> >    char *chars = new char [l + 1];
> > 
> > and later
> > 
> >    delete[] chars;
> > 
> > However, I'd like to check other possibilities. A short
> > search lead me to
> > <http://www.sgi.com/products/software/irix/tools/c.html>,
> > according to which newer versions support C99. Which
> > version do you use?
> > 
> 
> I'm using MIPSpro 7.4.3 (the most current version is 7.4.4)
> but in C99 mode it can't compile C++ code. Not to worry I'll
> #ifdef my way around the variable length arrays, I guess I
> didn't look closely enough to realize that was what those
> were, thanks! 

Thinking about it again, variable length arrays are a feature of C,
but not of C++, although it seems that most C++ compilers support it.
Since this is a nice feature, but standard C++ should be supported,
I've thought of testing it by the configure script, and then hide two
different implementations behind a macro. See attached patch, which
should be applied to the current hg repository.

What do you think?

Cris: If you run ./autogen.sh && ./configure, you should see a line:

checking for support of variable length arrays in C++... no

Is this correct?

Sebastian

_______________________________________________
Dillo-dev mailing list
[email protected]
http://lists.auriga.wearlab.de/cgi-bin/mailman/listinfo/dillo-dev
var_len_arr_cxx.diff (text/x-diff, 4.2 KB)
diff -r 808c0a0fc122 configure.ac
--- a/configure.ac	Mon Sep 02 11:31:37 2013 -0400
+++ b/configure.ac	Wed Sep 04 11:15:02 2013 +0200
@@ -105,6 +105,20 @@
                      [Define the real type of socklen_t])
 fi
 
+dnl --------------------------------------------------
+dnl Check for support of variable length arrays in C++
+dnl --------------------------------------------------
+AC_MSG_CHECKING([for support of variable length arrays in C++])
+AC_LANG_PUSH([C++])
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
+#include <stdlib.h>
+]],[[
+int n = rand() % 10 + 1;
+char c[n];
+]])], var_len_arr_cxx=yes,var_len_arr_cxx=no)
+AC_LANG_POP([C++])
+AC_MSG_RESULT($var_len_arr_cxx)
+
 
 dnl -------------------------
 dnl Test for FLTK 1.3 library
@@ -418,6 +432,9 @@
 dnl Command line options
 dnl --------------------
 dnl
+if test "x$var_len_arr_cxx" = "xyes" ; then
+  CXXFLAGS="$CXXFLAGS -DVAR_LEN_ARR_CXX"
+fi
 if test "x$enable_cookies" = "xno" ; then
   CFLAGS="$CFLAGS -DDISABLE_COOKIES"
   CXXFLAGS="$CXXFLAGS -DDISABLE_COOKIES"
diff -r 808c0a0fc122 dw/hyphenator.cc
--- a/dw/hyphenator.cc	Mon Sep 02 11:31:37 2013 -0400
+++ b/dw/hyphenator.cc	Wed Sep 04 11:15:02 2013 +0200
@@ -49,7 +49,7 @@
 {
    trie = NULL; // As long we are not sure whether a pattern file can be read.
 
-   char buf[strlen (patFile) + 5 + 1];
+   LOCAL_ARRAY (char, buf, strlen (patFile) + 5 + 1);
    snprintf(buf, sizeof (buf), "%s.trie", patFile);
    FILE *trieF = fopen (buf, "r");
 
@@ -118,10 +118,12 @@
    if (hyphenator)
       delete langString;
    else {
-      char patFile [strlen (DILLO_LIBDIR) + 13 + strlen (lang) + 4 + 1];
+      LOCAL_ARRAY (char, patFile,
+                   strlen (DILLO_LIBDIR) + 13 + strlen (lang) + 4 + 1);
       snprintf (patFile, sizeof (patFile), "%s/hyphenation/%s.pat",
                 DILLO_LIBDIR, lang);
-      char excFile [strlen (DILLO_LIBDIR) + 13 + strlen (lang) + 4 + 1];
+      LOCAL_ARRAY (char, excFile,
+                   strlen (DILLO_LIBDIR) + 13 + strlen (lang) + 4 + 1);
       snprintf (excFile, sizeof(excFile), "%s/hyphenation/%s.exc",
                 DILLO_LIBDIR, lang);
 
@@ -144,7 +146,7 @@
    // Convert the a pattern like 'a1bc3d4' into a string of chars 'abcd'
    // and a list of points [ 0, 1, 0, 3, 4 ].
    int l = strlen (s);
-   char chars [l + 1];
+   LOCAL_ARRAY (char, chars, l + 1);
    SimpleVector<char> points (1);
 
    // TODO numbers consisting of multiple digits?
@@ -182,7 +184,7 @@
       if((unsigned char)s[i] == 0xc2 && (unsigned char)s[i + 1] == 0xad)
          breaks->put (new Integer (i - 2 * breaks->size()));
 
-   char noHyphens[len - 2 * breaks->size() + 1];
+   LOCAL_ARRAY (char, noHyphens, len - 2 * breaks->size() + 1);
    int j = 0;
    for (int i = 0; i < len; ) {
       if(i < len - 1 &&
@@ -313,7 +315,7 @@
    if (trie == NULL)
       return;
 
-   char work[strlen (wordLc) + 3];
+   LOCAL_ARRAY (char, work, strlen (wordLc) + 3);
    strcpy (work, ".");
    strcat (work, wordLc);
    strcat (work, ".");
diff -r 808c0a0fc122 lout/misc.hh
--- a/lout/misc.hh	Mon Sep 02 11:31:37 2013 -0400
+++ b/lout/misc.hh	Wed Sep 04 11:15:02 2013 +0200
@@ -610,8 +610,43 @@
    }
 };
 
+// Used by the macro LOCAL_ARRAY
+template<class T> class LocalArrayWrapper
+{
+private:
+   T *array;
+
+public:
+   inline LocalArrayWrapper (T *array) { this->array = array; }
+   inline ~LocalArrayWrapper () { delete[] array; }
+};
+
 } // namespace misc
 
 } // namespace lout
 
+// Used for local arrays (allocated on the stack) with variable
+// length, which are supported by C99, but not by all C++ compilers.
+// Depending on VAR_LEN_ARR_CXX, which is set by the configure script
+// (testing variable length arrays in C++), two different
+// implementations are used. When not supported, the array is
+// allocated on the heap, and the destructor of LocalArrayWrapper
+// takes care that it is deleted again.
+//
+// Usage:
+//
+//    VAR_LEN_ARR_CXX (type, name, len);
+//
+// Example:
+//
+//    int num = rand() % 10 + 1;
+//    VAR_LEN_ARR_CXX (char, text, num);
+
+#ifdef VAR_LEN_ARR_CXX
+#define LOCAL_ARRAY(t, n, l)  t n[l]
+#else
+#define LOCAL_ARRAY(t, n, l)  t *n = new t[l]; \
+                              ::lout::misc::LocalArrayWrapper<t> n##_wrapper (n)
+#endif
+
 #endif // __LOUT_MISC_HH__
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.