]> granicus.if.org Git - clang/commitdiff
For printf checking, handle nested typedefs for darwin-specific checking.
authorTed Kremenek <kremenek@apple.com>
Mon, 25 Mar 2013 22:28:37 +0000 (22:28 +0000)
committerTed Kremenek <kremenek@apple.com>
Mon, 25 Mar 2013 22:28:37 +0000 (22:28 +0000)
Fixes <rdar://problem/13491605>.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@177931 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/FixIt/format-darwin.m

index cfce85eef01507f7b5df2560520ea90034dd5f65..4e11b3aa7920cf4c9f3e954d54720c58fd986d2c 100644 (file)
@@ -2803,7 +2803,9 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
   // casts to primitive types that are known to be large enough.
   bool ShouldNotPrintDirectly = false;
   if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
-    if (const TypedefType *UserTy = IntendedTy->getAs<TypedefType>()) {
+    // Use a 'while' to peel off layers of typedefs.
+    QualType TyTy = IntendedTy;
+    while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) {
       StringRef Name = UserTy->getDecl()->getName();
       QualType CastTy = llvm::StringSwitch<QualType>(Name)
         .Case("NSInteger", S.Context.LongTy)
@@ -2815,7 +2817,9 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
       if (!CastTy.isNull()) {
         ShouldNotPrintDirectly = true;
         IntendedTy = CastTy;
+        break;
       }
+      TyTy = UserTy->desugar();
     }
   }
 
index cfaac29e904b12b38c8628c130477ab94258dd87..79a3b388e76eca48524bf58c15093a7f1ce61887 100644 (file)
@@ -23,6 +23,8 @@ typedef long SInt32;
 typedef unsigned long UInt32;
 #endif
 
+typedef SInt32 OSStatus;
+
 NSInteger getNSInteger();
 NSUInteger getNSUInteger();
 SInt32 getSInt32();
@@ -210,3 +212,9 @@ void testCapitals() {
   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:13-[[@LINE-3]]:14}:"d"
   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:11-[[@LINE-4]]:14}:"%D"
 }
+
+void testLayeredTypedefs(OSStatus i) {
+  printf("%s", i); // expected-warning {{values of type 'OSStatus' should not be used as format arguments}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"d"
+}
+