Here are the warnings from a recent build of clang (-r59324):
DeclSerialization.cpp: In member function 'void clang::ScopedDecl::ReadInRec(llvm::Deserializer&, clang::ASTContext&)':
DeclSerialization.cpp:170: warning: dereferencing type-punned pointer will break strict-aliasing rules
DeclSerialization.cpp:171: warning: dereferencing type-punned pointer will break strict-aliasing rules
Expr.cpp: In member function 'virtual clang::StmtIterator clang::SizeOfAlignOfExpr::child_begin()':
Expr.cpp:1379: warning: dereferencing type-punned pointer will break strict-aliasing rules
Expr.cpp: In member function 'virtual clang::StmtIterator clang::SizeOfAlignOfExpr::child_end()':
Expr.cpp:1384: warning: dereferencing type-punned pointer will break strict-aliasing rules
ExprCXX.cpp: In member function 'virtual clang::StmtIterator clang::CXXTypeidExpr::child_begin()':
ExprCXX.cpp:31: warning: dereferencing type-punned pointer will break strict-aliasing rules
ExprCXX.cpp: In member function 'virtual clang::StmtIterator clang::CXXTypeidExpr::child_end()':
ExprCXX.cpp:34: warning: dereferencing type-punned pointer will break strict-aliasing rules
Here are the warnings from a recent build of clang (-r59324):
DeclSerialization.cpp: In member function 'void clang::ScopedDecl::ReadInRec(llvm::Deserializer&, clang::ASTContext&)':
DeclSerialization.cpp:170: warning: dereferencing type-punned pointer will break strict-aliasing rules
DeclSerialization.cpp:171: warning: dereferencing type-punned pointer will break strict-aliasing rules
I'm the one to blame for these. Care to explain more about what is wrong and how to fix it ?
The offending line is:
Here are the warnings from a recent build of clang (-r59324):
DeclSerialization.cpp: In member function 'void clang::ScopedDecl::ReadInRec(llvm::Deserializer&, clang::ASTContext&)':
DeclSerialization.cpp:170: warning: dereferencing type-punned pointer will break strict-aliasing rules
DeclSerialization.cpp:171: warning: dereferencing type-punned pointer will break strict-aliasing rules
I'm the one to blame for these. Care to explain more about what is wrong and how to fix it ?
The offending line is:
where 'MDC->SemanticDC' is a pointer (DeclContext*).
What's wrong with D.ReadPtr(MDC->SemanticDC, SemaDCPtrID)? That template function does exactly the same thing, but then it's LLVM core's problem, not yours.
Here are the warnings from a recent build of clang (-r59324):
DeclSerialization.cpp: In member function 'void clang::ScopedDecl::ReadInRec(llvm::Deserializer&, clang::ASTContext&)':
DeclSerialization.cpp:170: warning: dereferencing type-punned pointer will break strict-aliasing rules
DeclSerialization.cpp:171: warning: dereferencing type-punned pointer will break strict-aliasing rules
I'm the one to blame for these. Care to explain more about what is wrong and how to fix it ?
Hum, depends upon your background. I'd assume you're new to the wording in the standards that says that you can only play with a type as the type (plus a few other obscure cases). If you change your code to always play with variables of type X, as type X, you can never get this error.
Here are the warnings from a recent build of clang (-r59324):
DeclSerialization.cpp: In member function 'void clang::ScopedDecl::ReadInRec(llvm::Deserializer&, clang::ASTContext&)':
DeclSerialization.cpp:170: warning: dereferencing type-punned pointer will break strict-aliasing rules
DeclSerialization.cpp:171: warning: dereferencing type-punned pointer will break strict-aliasing rules
I'm the one to blame for these. Care to explain more about what is wrong and how to fix it ?
Hum, depends upon your background. I'd assume you're new to the wording in the standards that says that you can only play with a type as the type (plus a few other obscure cases). If you change your code to always play with variables of type X, as type X, you can never get this error.
It's a trick to avoid GCC's warning. Because the function takes a reference and passes a reference on, the type-punned "pointer" (reference, actually) is never "dereferenced" (accessed). The actual ReadUIntPtr accesses it, but by then GCC has forgotten the original type.
As I read that, it sounds like someone is praying and hoping and violating the language standard. Don't do that. We don't trick gcc into not complaining, if you do that, you _will_ lose. One _must_ write code that is type safe. That isn't a trick. I reviewed lib/Bitcode/Reader/Deserialize.cpp Deserializer::ReadUIntPtr. I don't see anything that is type safe about this code.
It is really easy to fix, for example:
PtrRef = 0;
becomes:
uintptr_t x = 0;
memcpy (&PtrRef, x, sizeof());
suddenly, perfectly type safe, and, not an instruction larger, not a single clock slower to boot.
As I read that, it sounds like someone is praying and hoping and violating the language standard. Don't do that. We don't trick gcc into not complaining, if you do that, you _will_ lose. One _must_ write code that is type safe. That isn't a trick. I reviewed lib/Bitcode/Reader/Deserialize.cpp Deserializer::ReadUIntPtr. I don't see anything that is type safe about this code.
Huh? So, it isn't a trick and indeed legal? Or it's not type safe and illegal?
I said that it's a trick to silence GCC's warning, not that it is actually legal.
If by it, you mean playing with the addresses of memory, yes, that is legal. You can play all you want with the address, no harm will come of it. That _part_ isn't a trick and is valid.
If you mean, can you then use those addresses after playing with them. Yes, provided that the type of access is the same. In the case of ReadUIntPtr, it is wrong, the type is different, as I have said.
It's a trick to avoid GCC's warning. Because the function takes a reference and passes a reference on, the type-punned "pointer" (reference, actually) is never "dereferenced" (accessed). The actual ReadUIntPtr accesses it, but by then GCC has forgotten the original type.
Thanks for reviewing the deserializer code. If you are up for it, it would be really helpful if you submitted a patch that addresses your concerns. That way our dialogue can certain around your patch.