Giter VIP home page Giter VIP logo

Comments (8)

GoogleCodeExporter avatar GoogleCodeExporter commented on May 9, 2024
I guess gcc 4.4 turned on strict aliasing by default?  Or maybe the detection 
just 
got better; I'm not seeing any problems with gcc 4.2, even with 
-fstrict-aliasing 
explicitly enabled.

In any cased, I thought strict aliasing problems were a warning, not an error.  
Are 
you using -Werror as well?

I don't have gcc 4.4.1 handy.  Can you try compiling, say, the unittests (via 
'make 
check'), and attaching the full output?  With line numbers, it should be easy 
to 
diagnose the problem and, hopefully, fix it.

Original comment by [email protected] on 8 Jan 2010 at 3:57

from gflags.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 9, 2024

Yes, I'm using -Werror for my compiles - that's why I see the error. Otherwise, 
its a 
warning - which is still ugly.

Attached is a file that shows the complete output when a 'make' is done inside 
the 
gflags-1.2 source. You can see the relevant warnings when the unittests are 
being 
compiled. Here's an excerpt:

src/gflags_unittest.cc:84: warning: dereferencing type-punned pointer will 
break 
strict-aliasing rules
src/gflags_unittest.cc:85: warning: dereferencing type-punned pointer will 
break 
strict-aliasing rules
src/gflags_unittest.cc:94: warning: dereferencing type-punned pointer will 
break 
strict-aliasing rules
src/gflags_unittest.cc:104: warning: dereferencing type-punned pointer will 
break 
strict-aliasing rules
src/gflags_unittest.cc:105: warning: dereferencing type-punned pointer will 
break 
strict-aliasing rules
src/gflags_unittest.cc:106: warning: dereferencing type-punned pointer will 
break 
strict-aliasing rules
src/gflags_unittest.cc:109: warning: dereferencing type-punned pointer will 
break 
strict-aliasing rules
src/gflags_unittest.cc:123: warning: dereferencing type-punned pointer will 
break 
strict-aliasing rules
src/gflags_unittest.cc:134: warning: dereferencing type-punned pointer will 
break 
strict-aliasing rules

Original comment by [email protected] on 8 Jan 2010 at 6:45

Attachments:

from gflags.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 9, 2024

Just a little more information to help out with fixing this bug. The line 
numbers 
reported by the compiler while emitting the warning correspond to the location 
where 
the DEFINE_string macro is used. It doesn't give the precise line within the 
macro's 
implementation that's causing the warning to be emitted. 

So what I did was replace one of the uses of the macro DEFINE_string with its 
implementation (after fixing the implementation appropriately to make it valid 
outside a macro definition). The compiler reported the warning this time at the 
following line within the macro's implementation:

   std::string& FLAGS_##name = *(reinterpret_cast<std::string*>(s_##name[0].s));


Original comment by [email protected] on 8 Jan 2010 at 7:10

from gflags.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 9, 2024

I found that changing the offending line:
   std::string& FLAGS_##name = *(reinterpret_cast<std::string*>(s_##name[0].s));
to:
   std::string& FLAGS_##name = *const_cast<std::string*>(FLAGS_no##name);
fixes the issue. Given below is the complete macro implementation with the fix.


#define DEFINE_string(name, val, txt)                                     \
  namespace fLS {                                                         \
    static union { void* align; char s[sizeof(std::string)]; } s_##name[2]; \
    const std::string* const FLAGS_no##name = new (s_##name[0].s) std::string(val); \
    static ::google::FlagRegisterer o_##name(                \
      #name, "string", MAYBE_STRIPPED_HELP(txt), __FILE__,                \
      s_##name[0].s, new (s_##name[1].s) std::string(*FLAGS_no##name));   \
    extern std::string& FLAGS_##name;                                     \
    using fLS::FLAGS_##name;                                              \
    std::string& FLAGS_##name = *const_cast<std::string*>(FLAGS_no##name);   \
  }                                                                       \
  using fLS::FLAGS_##name


Original comment by [email protected] on 8 Jan 2010 at 7:26

from gflags.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 9, 2024
Nice fix!  But I'm wondering now why we'd bother to have the const at all in 
this 
case.  What if you changed it to this:
---
#define DEFINE_string(name, val, txt)                                     \
  namespace fLS {                                                         \
    static union { void* align; char s[sizeof(std::string)]; } s_##name[2]; \
    std::string* const FLAGS_no##name = new (s_##name[0].s) std::string(val); \
    static ::google::FlagRegisterer o_##name(                \
      #name, "string", MAYBE_STRIPPED_HELP(txt), __FILE__,                \
      s_##name[0].s, new (s_##name[1].s) std::string(*FLAGS_no##name));   \
    extern std::string& FLAGS_##name;                                     \
    using fLS::FLAGS_##name;                                              \
    std::string& FLAGS_##name = *FLAGS_no##name;   \
  }                                                                       \
  using fLS::FLAGS_##name
---

Does that still work for you?  If so, I'll make it part of the next release.

Original comment by [email protected] on 8 Jan 2010 at 7:51

  • Changed state: Accepted

from gflags.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 9, 2024

> Does that still work for you?  If so, I'll make it part of the next release.

That was actually my first fix and that does work too. The only reason I even 
suggested 
the other fix was because I wanted to keep the suggested changes to a minimum. 
:)

Original comment by [email protected] on 8 Jan 2010 at 7:54

from gflags.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 9, 2024

Original comment by [email protected] on 30 Apr 2010 at 11:28

  • Changed state: Started

from gflags.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 9, 2024
This should be fixed in gflags 1.4, just released.

Original comment by [email protected] on 14 Oct 2010 at 1:21

  • Changed state: Fixed

from gflags.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.