From 033a9c0804f48119a03b73a2af42a04d4d0294ce Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Mon, 4 Jun 2012 22:49:02 +0000 Subject: [PATCH] Make suggestions for mismatched enum arguments to printf/scanf. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@157962 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/PrintfFormatString.cpp | 4 ++++ lib/Analysis/ScanfFormatString.cpp | 5 +++++ test/FixIt/format.m | 14 ++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/lib/Analysis/PrintfFormatString.cpp b/lib/Analysis/PrintfFormatString.cpp index 3b3a0b176e..aa6d7424c0 100644 --- a/lib/Analysis/PrintfFormatString.cpp +++ b/lib/Analysis/PrintfFormatString.cpp @@ -381,6 +381,10 @@ bool PrintfSpecifier::fixType(QualType QT, const LangOptions &LangOpt, return true; } + // If it's an enum, get its underlying type. + if (const EnumType *ETy = QT->getAs()) + QT = ETy->getDecl()->getIntegerType(); + // We can only work with builtin types. const BuiltinType *BT = QT->getAs(); if (!BT) diff --git a/lib/Analysis/ScanfFormatString.cpp b/lib/Analysis/ScanfFormatString.cpp index 6bc4adb4f3..066d5d6fa1 100644 --- a/lib/Analysis/ScanfFormatString.cpp +++ b/lib/Analysis/ScanfFormatString.cpp @@ -316,6 +316,11 @@ bool ScanfSpecifier::fixType(QualType QT, const LangOptions &LangOpt, return false; QualType PT = QT->getPointeeType(); + + // If it's an enum, get its underlying type. + if (const EnumType *ETy = QT->getAs()) + QT = ETy->getDecl()->getIntegerType(); + const BuiltinType *BT = PT->getAs(); if (!BT) return false; diff --git a/test/FixIt/format.m b/test/FixIt/format.m index d9589a0148..c4747019b2 100644 --- a/test/FixIt/format.m +++ b/test/FixIt/format.m @@ -79,3 +79,17 @@ void test_class_correction (Class x) { // CHECK: fix-it:"{{.*}}":{75:11-75:14}:"%@" } + +typedef enum : int { NSUTF8StringEncoding = 8 } NSStringEncoding; +void test_fixed_enum_correction(NSStringEncoding x) { + NSLog(@"%@", x); // expected-warning{{format specifies type 'id' but the argument has type 'NSStringEncoding'}} + // CHECK: fix-it:"{{.*}}":{85:11-85:13}:"%d" +} + +typedef __SIZE_TYPE__ size_t; +enum SomeSize : size_t { IntegerSize = sizeof(int) }; +void test_named_fixed_enum_correction(enum SomeSize x) { + NSLog(@"%@", x); // expected-warning{{format specifies type 'id' but the argument has type 'enum SomeSize'}} + // CHECK: fix-it:"{{.*}}":{92:11-92:13}:"%zu" +} + -- 2.40.0