[PATCH 3/4] glr2.cc: avoid type-punning issues

Akim Demaille <[email protected]>
Newsgroups gmane.comp.parsers.bison.patches
Message-ID <[email protected]>
On the CI, tests fail with GCC 4.6 to GCC 6 as follows:

    tests/synclines.at:440: COLUMNS=1000; export COLUMNS; NO_TERM_HYPERLINKS=1; export NO_TERM_HYPERLINKS;  bison --color=no -fno-caret  -o \"\\\"\".cc \"\\\"\".y
    tests/synclines.at:440: $CXX $CXXFLAGS $CPPFLAGS  $LDFLAGS -o \"\\\"\" \"\\\"\".cc $LIBS
    stderr:
    "\"".cc: In member function 'glr_state& glr_stack_item::getState()':
    "\"".cc:1404:47: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
         return *reinterpret_cast<glr_state*>(&raw_);
                                                   ^
    "\"".cc: In member function 'const glr_state& glr_stack_item::getState() const':
    "\"".cc:1408:53: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
         return *reinterpret_cast<const glr_state*>(&raw_);
                                                         ^
    "\"".cc: In member function 'semantic_option& glr_stack_item::getOption()':
    "\"".cc:1413:53: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
         return *reinterpret_cast<semantic_option*>(&raw_);
                                                         ^
    "\"".cc: In member function 'const semantic_option& glr_stack_item::getOption() const':
    "\"".cc:1417:59: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
         return *reinterpret_cast<const semantic_option*>(&raw_);
                                                           ^

See also be6fa942acae21a4a025bac5e339451be6ad136d.

* data/skeletons/glr2.cc (glr_stack_item): Use a temporary void*
variable to avoid type-punning issues with reinterpret_cast.
---
 data/skeletons/glr2.cc | 35 ++++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/data/skeletons/glr2.cc b/data/skeletons/glr2.cc
index 5f6a5c95..f15ab96d 100644
--- a/data/skeletons/glr2.cc
+++ b/data/skeletons/glr2.cc
@@ -1241,24 +1241,33 @@ public:
       getOption().~semantic_option();
   }
 
-  glr_state& getState() {
-    YYDASSERT(is_state());
-    return *reinterpret_cast<glr_state*>(&raw_);
+  glr_state& getState ()
+  {
+    YYDASSERT (is_state ());
+    void *yyp = raw_;
+    return *static_cast<glr_state*> (yyp);
   }
-  const glr_state& getState() const {
-    YYDASSERT(is_state());
-    return *reinterpret_cast<const glr_state*>(&raw_);
+  const glr_state& getState () const
+  {
+    YYDASSERT (is_state());
+    const void *yyp = raw_;
+    return *static_cast<const glr_state*> (yyp);
   }
 
-  semantic_option& getOption() {
-    YYDASSERT(!is_state());
-    return *reinterpret_cast<semantic_option*>(&raw_);
+  semantic_option& getOption ()
+  {
+    YYDASSERT (!is_state ());
+    void *yyp = raw_;
+    return *static_cast<semantic_option*> (yyp);
   }
-  const semantic_option& getOption() const {
-    YYDASSERT(!is_state());
-    return *reinterpret_cast<const semantic_option*>(&raw_);
+  const semantic_option& getOption () const
+  {
+    YYDASSERT (!is_state ());
+    const void *yyp = raw_;
+    return *static_cast<const semantic_option*> (yyp);
   }
-  bool is_state() const {
+  bool is_state () const
+  {
     return is_state_;
   }
  private:
-- 
2.28.0
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.