From: Aaron Ballman Date: Tue, 18 Dec 2018 15:54:38 +0000 (+0000) Subject: Emit -Wformat properly for bit-field promotions. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=38ed7e5d7917d18c9c7af9c49c5d83a112c6222a;p=clang Emit -Wformat properly for bit-field promotions. Only explicitly look through integer and floating-point promotion where the result type is actually a promotion, which is not always the case for bit-fields in C. Patch by Bevin Hansson. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@349497 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 46cac25eed..b06f793b17 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -7771,6 +7771,30 @@ shouldNotPrintDirectly(const ASTContext &Context, return std::make_pair(QualType(), StringRef()); } +/// Return true if \p ICE is an implicit argument promotion of an arithmetic +/// type. Bit-field 'promotions' from a higher ranked type to a lower ranked +/// type do not count. +static bool +isArithmeticArgumentPromotion(Sema &S, const ImplicitCastExpr *ICE) { + QualType From = ICE->getSubExpr()->getType(); + QualType To = ICE->getType(); + // It's an integer promotion if the destination type is the promoted + // source type. + if (ICE->getCastKind() == CK_IntegralCast && + From->isPromotableIntegerType() && + S.Context.getPromotedIntegerType(From) == To) + return true; + // Look through vector types, since we do default argument promotion for + // those in OpenCL. + if (const auto *VecTy = From->getAs()) + From = VecTy->getElementType(); + if (const auto *VecTy = To->getAs()) + To = VecTy->getElementType(); + // It's a floating promotion if the source type is a lower rank. + return ICE->getCastKind() == CK_FloatingCast && + S.Context.getFloatingTypeOrder(From, To) < 0; +} + bool CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, const char *StartSpecifier, @@ -7798,11 +7822,11 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, // Look through argument promotions for our error message's reported type. // This includes the integral and floating promotions, but excludes array - // and function pointer decay; seeing that an argument intended to be a - // string has type 'char [6]' is probably more confusing than 'char *'. + // and function pointer decay (seeing that an argument intended to be a + // string has type 'char [6]' is probably more confusing than 'char *') and + // certain bitfield promotions (bitfields can be 'demoted' to a lesser type). if (const ImplicitCastExpr *ICE = dyn_cast(E)) { - if (ICE->getCastKind() == CK_IntegralCast || - ICE->getCastKind() == CK_FloatingCast) { + if (isArithmeticArgumentPromotion(S, ICE)) { E = ICE->getSubExpr(); ExprTy = E->getType(); diff --git a/test/Sema/format-strings-bitfield-promotion.c b/test/Sema/format-strings-bitfield-promotion.c new file mode 100644 index 0000000000..cbe00e7510 --- /dev/null +++ b/test/Sema/format-strings-bitfield-promotion.c @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -verify %s +// RUN: %clang_cc1 -triple x86_64-unknown-windows-msvc -fsyntax-only -verify %s + +int printf(const char *restrict, ...); + +struct bitfields { + long a : 2; + unsigned long b : 2; + long c : 32; // assumes that int is 32 bits + unsigned long d : 32; // assumes that int is 32 bits +} bf; + +void bitfield_promotion() { + printf("%ld", bf.a); // expected-warning {{format specifies type 'long' but the argument has type 'int'}} + printf("%lu", bf.b); // expected-warning {{format specifies type 'unsigned long' but the argument has type 'int'}} + printf("%ld", bf.c); // expected-warning {{format specifies type 'long' but the argument has type 'int'}} + printf("%lu", bf.d); // expected-warning {{format specifies type 'unsigned long' but the argument has type 'unsigned int'}} +} diff --git a/test/Sema/format-strings-bitfield-promotion.cxx b/test/Sema/format-strings-bitfield-promotion.cxx new file mode 100644 index 0000000000..2309e98066 --- /dev/null +++ b/test/Sema/format-strings-bitfield-promotion.cxx @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -verify %s +// RUN: %clang_cc1 -triple x86_64-unknown-windows-msvc -fsyntax-only -verify %s + +// In C++, the bitfield promotion from long to int does not occur, unlike C. +// expected-no-diagnostics + +int printf(const char *restrict, ...); + +struct bitfields { + long a : 2; + unsigned long b : 2; + long c : 32; // assumes that int is 32 bits + unsigned long d : 32; // assumes that int is 32 bits +} bf; + +void bitfield_promotion() { + printf("%ld", bf.a); + printf("%lu", bf.b); + printf("%ld", bf.c); + printf("%lu", bf.d); +}