r177931 - For printf checking, handle nested typedefs for darwin-specific checking.

Ted Kremenek kremenek at apple.com
Mon Mar 25 15:28:37 PDT 2013


Author: kremenek
Date: Mon Mar 25 17:28:37 2013
New Revision: 177931

URL: http://llvm.org/viewvc/llvm-project?rev=177931&view=rev
Log:
For printf checking, handle nested typedefs for darwin-specific checking.

Fixes <rdar://problem/13491605>.

Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/FixIt/format-darwin.m

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=177931&r1=177930&r2=177931&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Mon Mar 25 17:28:37 2013
@@ -2803,7 +2803,9 @@ CheckPrintfHandler::checkFormatExpr(cons
   // 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(cons
       if (!CastTy.isNull()) {
         ShouldNotPrintDirectly = true;
         IntendedTy = CastTy;
+        break;
       }
+      TyTy = UserTy->desugar();
     }
   }
 

Modified: cfe/trunk/test/FixIt/format-darwin.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/FixIt/format-darwin.m?rev=177931&r1=177930&r2=177931&view=diff
==============================================================================
--- cfe/trunk/test/FixIt/format-darwin.m (original)
+++ cfe/trunk/test/FixIt/format-darwin.m Mon Mar 25 17:28:37 2013
@@ -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"
+}
+





More information about the cfe-commits mailing list