Skip to content

Commit

Permalink
[AST] Use explicit type erasure in TypeSourceInfo constructor (#68435)
Browse files Browse the repository at this point in the history
When this file is included in a project compiled with GCC 13 `-Werror`,
compilation fails with the following diagnostic:
<pre>
/usr/lib/llvm-17.0/include/clang/AST/TypeLoc.h: In constructor
'clang::TypeSourceInfo::TypeSourceInfo(clang::QualType, size_t)':
/usr/lib/llvm-17.0/include/clang/AST/TypeLoc.h:245:9: error: 'void*
memset(void*, int, size_t)' clearing an object of non-trivial type
'class clang::TypeSourceInfo'; use assignment instead
[-Werror=class-memaccess]
  245 |   memset(this + 1, 0, DataSize);
      |   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

</pre>
To avoid this, we add an explicit type cast. The cast to `void*` makes
sense, since other member functions of `TypeSourceInfo` also treat the
buffer `(this + 1)` of length `DataSize` as opaque memory, and was
likely left out by mistake.

Fixes: 4498663 ("[AST] Initialized data after TypeSourceInfo")

I also suggest to apply this to release/17.x if possible.
  • Loading branch information
bonktree committed Oct 21, 2023
1 parent cb5612c commit 826c93f
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion clang/include/clang/AST/TypeLoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class TypeLoc {

inline TypeSourceInfo::TypeSourceInfo(QualType ty, size_t DataSize) : Ty(ty) {
// Init data attached to the object. See getTypeLoc.
memset(this + 1, 0, DataSize);
memset(static_cast<void *>(this + 1), 0, DataSize);
}

/// Return the TypeLoc for a type source info.
Expand Down

0 comments on commit 826c93f

Please sign in to comment.