Silence false positive diagnostics regarding passing an object of enumeration type...
authorAaron Ballman <aaron@aaronballman.com>
Thu, 15 Sep 2016 14:01:10 +0000 (14:01 +0000)
committerAaron Ballman <aaron@aaronballman.com>
Thu, 15 Sep 2016 14:01:10 +0000 (14:01 +0000)
This patch addresses PR29140.

llvm-svn: 281609

clang/lib/Sema/SemaChecking.cpp
clang/test/Sema/varargs.c

index 81dc23724e929f7f36673aa425f2f52f33e6017e..f3f204ffa04ee7389a0e552e073fbd3ae7e9dbec 100644 (file)
@@ -3240,8 +3240,17 @@ bool Sema::SemaBuiltinVAStartImpl(CallExpr *TheCall) {
     Diag(TheCall->getArg(1)->getLocStart(),
          diag::warn_second_arg_of_va_start_not_last_named_param);
   else if (IsCRegister || Type->isReferenceType() ||
-           Type->isPromotableIntegerType() ||
-           Type->isSpecificBuiltinType(BuiltinType::Float)) {
+           Type->isSpecificBuiltinType(BuiltinType::Float) || [=] {
+             // Promotable integers are UB, but enumerations need a bit of
+             // extra checking to see what their promotable type actually is.
+             if (!Type->isPromotableIntegerType())
+               return false;
+             if (!Type->isEnumeralType())
+               return true;
+             const EnumDecl *ED = Type->getAs<EnumType>()->getDecl();
+             return !(ED &&
+                      Context.typesAreCompatible(ED->getPromotionType(), Type));
+           }()) {
     unsigned Reason = 0;
     if (Type->isReferenceType())  Reason = 1;
     else if (IsCRegister)         Reason = 2;
index 457d84c212f7d94304f82399daa6e5bab54e1b58..27d49ea1f2e9a81e5700f46f9e24bda92fcc356f 100644 (file)
@@ -1,5 +1,6 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -verify %s -triple i386-pc-unknown
 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple x86_64-apple-darwin9
+// RUN: %clang_cc1 -fsyntax-only -fms-compatibility -DMS -verify %s
 
 void f1(int a)
 {
@@ -94,3 +95,20 @@ void f12(register int i, ...) {  // expected-note {{parameter of type 'int' is d
   __builtin_va_start(ap, i); // expected-warning {{passing a parameter declared with the 'register' keyword to 'va_start' has undefined behavior}}
   __builtin_va_end(ap);
 }
+
+enum __attribute__((packed)) E1 {
+  one1
+};
+
+void f13(enum E1 e, ...) {
+  __builtin_va_list va;
+  __builtin_va_start(va, e);
+#ifndef MS
+  // In Microsoft compatibility mode, all enum types are int, but in
+  // non-ms-compatibility mode, this enumeration type will undergo default
+  // argument promotions.
+  // expected-note@-7 {{parameter of type 'enum E1' is declared here}}
+  // expected-warning@-6 {{passing an object that undergoes default argument promotion to 'va_start' has undefined behavior}}
+#endif
+  __builtin_va_end(va);
+}