[cfe-commits] r97005 - in /cfe/trunk: include/clang/Analysis/Analyses/PrintfFormatString.h lib/Analysis/PrintfFormatString.cpp test/Sema/format-strings.c

Ted Kremenek kremenek at apple.com
Tue Feb 23 16:05:54 PST 2010


Author: kremenek
Date: Tue Feb 23 18:05:54 2010
New Revision: 97005

URL: http://llvm.org/viewvc/llvm-project?rev=97005&view=rev
Log:
Add support for '%C' and '%S' printf conversion specifiers.

Modified:
    cfe/trunk/include/clang/Analysis/Analyses/PrintfFormatString.h
    cfe/trunk/lib/Analysis/PrintfFormatString.cpp
    cfe/trunk/test/Sema/format-strings.c

Modified: cfe/trunk/include/clang/Analysis/Analyses/PrintfFormatString.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/Analyses/PrintfFormatString.h?rev=97005&r1=97004&r2=97005&view=diff
==============================================================================
--- cfe/trunk/include/clang/Analysis/Analyses/PrintfFormatString.h (original)
+++ cfe/trunk/include/clang/Analysis/Analyses/PrintfFormatString.h Tue Feb 23 18:05:54 2010
@@ -75,11 +75,14 @@
    VoidPtrArg,    // 'p'
    OutIntPtrArg,  // 'n'
    PercentArg,    // '%'
-    // Objective-C specific specifiers.
+   // MacOS X unicode extensions.
+   CArg, // 'C'
+   UnicodeStrArg, // 'S'
+   // Objective-C specific specifiers.
    ObjCObjArg,    // '@'
-    // GlibC specific specifiers.
+   // GlibC specific specifiers.
    PrintErrno,    // 'm'
-    // Specifier ranges.
+   // Specifier ranges.
    IntArgBeg = dArg,
    IntArgEnd = iArg,
    UIntArgBeg = oArg,

Modified: cfe/trunk/lib/Analysis/PrintfFormatString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/PrintfFormatString.cpp?rev=97005&r1=97004&r2=97005&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/PrintfFormatString.cpp (original)
+++ cfe/trunk/lib/Analysis/PrintfFormatString.cpp Tue Feb 23 18:05:54 2010
@@ -214,25 +214,28 @@
     default:
       break;
     // C99: 7.19.6.1 (section 8).
-    case 'd': k = ConversionSpecifier::dArg; break;
-    case 'i': k = ConversionSpecifier::iArg; break;
-    case 'o': k = ConversionSpecifier::oArg; break;
-    case 'u': k = ConversionSpecifier::uArg; break;
-    case 'x': k = ConversionSpecifier::xArg; break;
-    case 'X': k = ConversionSpecifier::XArg; break;
-    case 'f': k = ConversionSpecifier::fArg; break;
-    case 'F': k = ConversionSpecifier::FArg; break;
-    case 'e': k = ConversionSpecifier::eArg; break;
+    case '%': k = ConversionSpecifier::PercentArg;   break;
+    case 'A': k = ConversionSpecifier::AArg; break;
     case 'E': k = ConversionSpecifier::EArg; break;
-    case 'g': k = ConversionSpecifier::gArg; break;
+    case 'F': k = ConversionSpecifier::FArg; break;
     case 'G': k = ConversionSpecifier::GArg; break;
+    case 'X': k = ConversionSpecifier::XArg; break;
     case 'a': k = ConversionSpecifier::aArg; break;
-    case 'A': k = ConversionSpecifier::AArg; break;
     case 'c': k = ConversionSpecifier::IntAsCharArg; break;
-    case 's': k = ConversionSpecifier::CStrArg;      break;
-    case 'p': k = ConversionSpecifier::VoidPtrArg;   break;
+    case 'd': k = ConversionSpecifier::dArg; break;
+    case 'e': k = ConversionSpecifier::eArg; break;
+    case 'f': k = ConversionSpecifier::fArg; break;
+    case 'g': k = ConversionSpecifier::gArg; break;
+    case 'i': k = ConversionSpecifier::iArg; break;
     case 'n': k = ConversionSpecifier::OutIntPtrArg; break;
-    case '%': k = ConversionSpecifier::PercentArg;   break;
+    case 'o': k = ConversionSpecifier::oArg; break;
+    case 'p': k = ConversionSpecifier::VoidPtrArg;   break;
+    case 's': k = ConversionSpecifier::CStrArg;      break;
+    case 'u': k = ConversionSpecifier::uArg; break;
+    case 'x': k = ConversionSpecifier::xArg; break;
+    // Mac OS X (unicode) specific
+    case 'C': k = ConversionSpecifier::CArg; break;
+    case 'S': k = ConversionSpecifier::UnicodeStrArg; break;
     // Objective-C.
     case '@': k = ConversionSpecifier::ObjCObjArg; break;
     // Glibc specific.
@@ -345,8 +348,10 @@
     if (!PT)
       return false;
 
-    QualType pointeeTy = PT->getPointeeType();
-    return pointeeTy == C.WCharTy;
+    QualType pointeeTy =
+      C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
+
+    return pointeeTy == C.getWCharType();
   }
 
   return false;
@@ -359,7 +364,7 @@
   if (K == CStrTy)
     return C.getPointerType(C.CharTy);
   if (K == WCStrTy)
-    return C.getPointerType(C.WCharTy);
+    return C.getPointerType(C.getWCharType());
   if (K == ObjCPointerTy)
     return C.ObjCBuiltinIdTy;
 
@@ -425,11 +430,19 @@
       return Ctx.LongDoubleTy;
     return Ctx.DoubleTy;
   }
-
-  if (CS.getKind() == ConversionSpecifier::CStrArg)
-    return ArgTypeResult(LM == AsWideChar ? ArgTypeResult::WCStrTy
-                                          : ArgTypeResult::CStrTy);
-
+  
+  switch (CS.getKind()) {
+    case ConversionSpecifier::CStrArg:
+      return ArgTypeResult(LM == AsWideChar ? ArgTypeResult::WCStrTy                                            : ArgTypeResult::CStrTy);
+    case ConversionSpecifier::UnicodeStrArg:
+      // FIXME: This appears to be Mac OS X specific.
+      return ArgTypeResult::WCStrTy;
+    case ConversionSpecifier::CArg:
+      return Ctx.WCharTy;    
+    default:
+      break;
+  }
+  
   // FIXME: Handle other cases.
   return ArgTypeResult();
 }

Modified: cfe/trunk/test/Sema/format-strings.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings.c?rev=97005&r1=97004&r2=97005&view=diff
==============================================================================
--- cfe/trunk/test/Sema/format-strings.c (original)
+++ cfe/trunk/test/Sema/format-strings.c Tue Feb 23 18:05:54 2010
@@ -204,3 +204,18 @@
 // <rdar://problem/7595366>
 typedef enum { A } int_t;
 void f0(int_t x) { printf("%d\n", x); }
+
+// Unicode test cases.  These are possibly specific to Mac OS X.  If so, they should
+// eventually be moved into a separate test.
+typedef __WCHAR_TYPE__ wchar_t;
+
+void test_unicode_conversions(wchar_t *s) {
+  printf("%S", s); // no-warning
+  printf("%s", s); // expected-warning{{conversion specifies type 'char *' but the argument has type 'wchar_t *'}}
+  printf("%C", s[0]); // no-warning
+  printf("%c", s[0]);
+  printf("%C", 10);
+  // FIXME: we report the expected type as 'int*' instead of 'wchar_t*'
+  printf("%S", "hello"); // expected-warning{{but the argument has type 'char *'}}
+}
+





More information about the cfe-commits mailing list