Question

I'm trying to install the re::engine::RE2 module with cpan.

When building, the compiler emits the error message:

re2_xs.cc:254:25: error: variable length array of non-POD element type
      're2::StringPiece'
    re2::StringPiece res[re->nparens + 1];
                        ^

What does that mean? How can I fix that? I'm using a 2011 Macbook Air and Perl 5.12.

No correct solution

OTHER TIPS

re::engine::RE2 was never successfully tested on darwin/Perl 5.12.

Googling for the error message suggests that this is a flaw in old clang that ships with Xcode, and not present in newer versions.

Upgrade your software, you're out of support, perhaps use Macports to get a fresh gcc.

True, re::engine::RE2 never compiled on OS X. But you can get it to compile. Locate the cpan directory. For me it is ~/.cpan/build/re-engine-RE2-0.13-BY20k3/. Then within there, you change two C++ source files.

$ diff -p re2_xs.cc.old re2_xs.cc
*** re2_xs.cc.old   2015-04-20 20:20:15.000000000 +0200
--- re2_xs.cc   2015-04-20 20:22:24.000000000 +0200
*************** RE2_exec(pTHX_ REGEXP * const rx, char *
*** 229,236 ****
      RE2 * ri = (RE2*) SvANY(rx)->pprivate;
      regexp * re = SvANY(rx);

-     re2::StringPiece res[re->nparens + 1];
-
  #ifdef RE2_DEBUG
      Perl_warner(aTHX_ packWARN(WARN_MISC), "RE2: Matching '%s' (%p, %p) against '%s'", stringarg, strbeg, stringarg, RX_WRAPPED(rx));
  #endif
--- 229,234 ----
*************** RE2_exec(pTHX_ REGEXP * const rx, char *
*** 241,246 ****
--- 239,246 ----
        return 0;
      }

+     re2::StringPiece *res = new re2::StringPiece[re->nparens + 1];
+
      bool ok = ri->Match(
              re2::StringPiece(strbeg, strend - strbeg),
              stringarg - strbeg,
*************** RE2_exec(pTHX_ REGEXP * const rx, char *
*** 250,255 ****
--- 250,256 ----

      /* Matching failed */
      if (!ok) {
+         delete [] res;
          return 0;
      }

*************** RE2_exec(pTHX_ REGEXP * const rx, char *
*** 266,271 ****
--- 267,274 ----
          }
      }

+     delete [] res;
+
      return 1;
  }

Attempting to compile after that will get you to a tr1 issue.

/usr/bin/clang -o obj/util/arena.o -xc++   -O3 -DHAVE_PTHREAD -pthread -Wno-sign-compare -c -I.    -DNDEBUG util/arena.cc
In file included from util/arena.cc:5:
./util/util.h:45:10: fatal error: 'tr1/unordered_set' file not found

I'm sure there are simpler -D fixes for the Makefile but I changed the code.

# diff -p re2/util/util.h.old re2/util/util.h
*** re2/util/util.h.old 2015-04-20 20:29:01.000000000 +0200
--- re2/util/util.h 2015-04-20 20:29:26.000000000 +0200
*************** using std::make_pair;
*** 42,49 ****

  #if defined(__GNUC__) && !defined(USE_CXX0X)

! #include <tr1/unordered_set>
! using std::tr1::unordered_set;

  #else

--- 42,51 ----

  #if defined(__GNUC__) && !defined(USE_CXX0X)

! //#include <tr1/unordered_set>
! //using std::tr1::unordered_set;
! #include <unordered_set>
! using std::unordered_set;

  #else

Now just

make && make install

and you now have re::engine::RE2 available for OS X.

The answer by Douglas Scofield is correct, but one additional change is needed. In re2_xs.cc you also must change line 244 as follows:

          bool ok = ri->Match(
              re2::StringPiece(strbeg, strend - strbeg),
              stringarg - strbeg,
              strend - strbeg,
              RE2::UNANCHORED,
!             res, re->nparens + 1);

Douglas Scofield's answer changes the array res to pointer *res to avoid the compiler error of variable length array. However, after this change the line

sizeof res / sizeof *res

does not work because you would be taking the size of the pointer and dividing by the size of the array. Therefore, you have to make the change above to

re->nparens + 1

to get the size of the array pointed to by *res, which is what was desired in the original code.

If you don't make the change then capture groups will not work in re2. You will get errors that your capture groups like $1, $2, etc. are uninitialized.

So great answer above, just needs one addendum.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top