[clang] [AST] Use explicit type erasure in TypeSourceInfo constructor (PR #68435)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 10 03:41:38 PDT 2023


https://github.com/bonktree updated https://github.com/llvm/llvm-project/pull/68435

>From 021a0664905385b0bfb212316810bd15032d1df2 Mon Sep 17 00:00:00 2001
From: Arseny Maslennikov <arseny at altlinux.org>
Date: Thu, 5 Oct 2023 10:05:14 +0300
Subject: [PATCH] [AST] Use explicit type erasure in TypeSourceInfo constructor

When this file is included in a project compiled with GCC 13 -Werror,
compilation fails with the following diagnostic:
  /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);
        |   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

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: 4498663f3de0 ("[AST] Initialized data after TypeSourceInfo")
---
 clang/include/clang/AST/TypeLoc.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/include/clang/AST/TypeLoc.h b/clang/include/clang/AST/TypeLoc.h
index 98427a8dcbfe660..5bb487f1a7f4455 100644
--- a/clang/include/clang/AST/TypeLoc.h
+++ b/clang/include/clang/AST/TypeLoc.h
@@ -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.



More information about the cfe-commits mailing list