From 8da9316c5a97887da0bf84a2ea45daf43ddf572b Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Wed, 13 Jul 2011 20:20:58 +0000 Subject: [PATCH] format string checking: long and int have the same widths on 32-bit, so we shouldn't warn about using an "int" format specifier with a "long" type in 32-bit. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@135075 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/FormatString.cpp | 19 +++++++++---------- test/Sema/format-strings-i386.c | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 test/Sema/format-strings-i386.c diff --git a/lib/Analysis/FormatString.cpp b/lib/Analysis/FormatString.cpp index 5f3cd4c615..74f1e92794 100644 --- a/lib/Analysis/FormatString.cpp +++ b/lib/Analysis/FormatString.cpp @@ -206,6 +206,10 @@ clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS, // Methods on ArgTypeResult. //===----------------------------------------------------------------------===// +static bool hasSameSize(ASTContext &astContext, QualType typeA, QualType typeB) { + return astContext.getTypeSize(typeA) == astContext.getTypeSize(typeB); +} + bool ArgTypeResult::matchesType(ASTContext &C, QualType argTy) const { switch (K) { case InvalidTy: @@ -226,26 +230,21 @@ bool ArgTypeResult::matchesType(ASTContext &C, QualType argTy) const { break; case BuiltinType::Char_S: case BuiltinType::SChar: - return T == C.UnsignedCharTy; case BuiltinType::Char_U: case BuiltinType::UChar: - return T == C.SignedCharTy; + return hasSameSize(C, T, C.UnsignedCharTy); case BuiltinType::Short: - return T == C.UnsignedShortTy; case BuiltinType::UShort: - return T == C.ShortTy; + return hasSameSize(C, T, C.ShortTy); case BuiltinType::Int: - return T == C.UnsignedIntTy; case BuiltinType::UInt: - return T == C.IntTy; + return hasSameSize(C, T, C.IntTy); case BuiltinType::Long: - return T == C.UnsignedLongTy; case BuiltinType::ULong: - return T == C.LongTy; + return hasSameSize(C, T, C.LongTy); case BuiltinType::LongLong: - return T == C.UnsignedLongLongTy; case BuiltinType::ULongLong: - return T == C.LongLongTy; + return hasSameSize(C, T, C.LongLongTy); } return false; } diff --git a/test/Sema/format-strings-i386.c b/test/Sema/format-strings-i386.c new file mode 100644 index 0000000000..d2d9862796 --- /dev/null +++ b/test/Sema/format-strings-i386.c @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -triple i386-apple-macosx10.7.0 -fsyntax-only -verify -Wformat-nonliteral %s + +int printf(const char *restrict, ...); + +// Test that 'long' is compatible with 'int' on 32-bit. +typedef unsigned int UInt32; +void test_rdar_9763999() { + UInt32 x = 7; + printf("x = %u\n", x); // no-warning +} + +void test_positive() { + printf("%d", "hello"); // expected-warning {{conversion specifies type 'int' but the argument has type 'char *'}} +}