[Sema] Check whether `__auto_type` has been deduced before merging
authorAkira Hatanaka <ahatanaka@apple.com>
Wed, 22 Jun 2022 18:52:22 +0000 (11:52 -0700)
committerAkira Hatanaka <ahatanaka@apple.com>
Fri, 24 Jun 2022 16:49:07 +0000 (09:49 -0700)
This fixes a bug in clang where it emits the following diagnostic when
compiling the test case:

"argument to 'sizeof' in 'memset' call is the same pointer type 'S' as
the destination"

The code that merges __auto_type with other types was committed in
https://reviews.llvm.org/D122029.

Differential Revision: https://reviews.llvm.org/D128373

clang/lib/AST/ASTContext.cpp
clang/test/Sema/warn-memset-bad-sizeof.c [new file with mode: 0644]

index fe1ef67..cb2a3e2 100644 (file)
@@ -10323,11 +10323,11 @@ QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
     // Allow __auto_type to match anything; it merges to the type with more
     // information.
     if (const auto *AT = LHS->getAs<AutoType>()) {
-      if (AT->isGNUAutoType())
+      if (!AT->isDeduced() && AT->isGNUAutoType())
         return RHS;
     }
     if (const auto *AT = RHS->getAs<AutoType>()) {
-      if (AT->isGNUAutoType())
+      if (!AT->isDeduced() && AT->isGNUAutoType())
         return LHS;
     }
     return {};
diff --git a/clang/test/Sema/warn-memset-bad-sizeof.c b/clang/test/Sema/warn-memset-bad-sizeof.c
new file mode 100644 (file)
index 0000000..c4768d8
--- /dev/null
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+typedef __SIZE_TYPE__ size_t;
+void *memset(void *, int, size_t);
+
+typedef struct {
+  int a;
+} S;
+
+void test() {
+  S s;
+  __auto_type dstptr = &s;
+  memset(dstptr, 0, sizeof(s));
+}