Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ Improvements to Clang's diagnostics
``ACQUIRED_AFTER(...)`` have been moved to the stable feature set and no
longer require ``-Wthread-safety-beta`` to be used.

- Clang now looks through parenthesis for ``-Wundefined-reinterpret-cast`` diagnostic.

Improvements to Clang's time-trace
----------------------------------

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14774,7 +14774,7 @@ static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK,
QualType OpTy = Op->getType();
QualType Result;

if (isa<CXXReinterpretCastExpr>(Op)) {
if (isa<CXXReinterpretCastExpr>(Op->IgnoreParens())) {
QualType OpOrigType = Op->IgnoreParenCasts()->getType();
S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true,
Op->getSourceRange());
Expand Down
4 changes: 4 additions & 0 deletions clang/test/SemaCXX/reinterpret-cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ void dereference_reinterpret_cast() {
(void)reinterpret_cast<float&>(d); // expected-warning {{reinterpret_cast from 'double' to 'float &' has undefined behavior}}
(void)*reinterpret_cast<float*>(&d); // expected-warning {{dereference of type 'float *' that was reinterpret_cast from type 'double *' has undefined behavior}}

// Look through parens
(void)*(reinterpret_cast<double*>(&l)); // expected-warning {{dereference of type 'double *' that was reinterpret_cast from type 'long *' has undefined behavior}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am assuming this ignores multiple parens, maybe worth a test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, added a test.

(void)*((reinterpret_cast<double*>((&l)))); // expected-warning {{dereference of type 'double *' that was reinterpret_cast from type 'long *' has undefined behavior}}

// TODO: add warning for tag types
(void)reinterpret_cast<A&>(b);
(void)*reinterpret_cast<A*>(&b);
Expand Down