aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Rose <jordan_rose@apple.com>2012-06-04 22:49:02 +0000
committerJordan Rose <jordan_rose@apple.com>2012-06-04 22:49:02 +0000
commit033a9c0804f48119a03b73a2af42a04d4d0294ce (patch)
treede9b92a1fecd17bdf4a6875c6b354389832fe0fc
parentee0259d308e72141982a85b40863e760a8447edf (diff)
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
-rw-r--r--lib/Analysis/PrintfFormatString.cpp4
-rw-r--r--lib/Analysis/ScanfFormatString.cpp5
-rw-r--r--test/FixIt/format.m14
3 files changed, 23 insertions, 0 deletions
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<EnumType>())
+ QT = ETy->getDecl()->getIntegerType();
+
// We can only work with builtin types.
const BuiltinType *BT = QT->getAs<BuiltinType>();
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<EnumType>())
+ QT = ETy->getDecl()->getIntegerType();
+
const BuiltinType *BT = PT->getAs<BuiltinType>();
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"
+}
+