Bläddra i källkod

Make printf warnings refer to wint_t and wchar_t by name
in addition to underlying type.

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

Hans Wennborg 13 år sedan
förälder
incheckning
f4f0c6095d

+ 3 - 2
include/clang/Analysis/Analyses/FormatString.h

@@ -199,7 +199,7 @@ protected:
 class ArgTypeResult {
 class ArgTypeResult {
 public:
 public:
   enum Kind { UnknownTy, InvalidTy, SpecificTy, ObjCPointerTy, CPointerTy,
   enum Kind { UnknownTy, InvalidTy, SpecificTy, ObjCPointerTy, CPointerTy,
-              AnyCharTy, CStrTy, WCStrTy, WIntTy, TypedefTy };
+              AnyCharTy, CStrTy, WCStrTy, WIntTy };
 private:
 private:
   const Kind K;
   const Kind K;
   QualType T;
   QualType T;
@@ -207,8 +207,9 @@ private:
   ArgTypeResult(bool) : K(InvalidTy), Name(0) {}
   ArgTypeResult(bool) : K(InvalidTy), Name(0) {}
 public:
 public:
   ArgTypeResult(Kind k = UnknownTy) : K(k), Name(0) {}
   ArgTypeResult(Kind k = UnknownTy) : K(k), Name(0) {}
+  ArgTypeResult(Kind k, const char *n) : K(k), Name(n) {}
   ArgTypeResult(QualType t) : K(SpecificTy), T(t), Name(0) {}
   ArgTypeResult(QualType t) : K(SpecificTy), T(t), Name(0) {}
-  ArgTypeResult(QualType t, const char *n) : K(TypedefTy), T(t), Name(n)  {}
+  ArgTypeResult(QualType t, const char *n) : K(SpecificTy), T(t), Name(n)  {}
   ArgTypeResult(CanQualType t) : K(SpecificTy), T(t), Name(0) {}
   ArgTypeResult(CanQualType t) : K(SpecificTy), T(t), Name(0) {}
 
 
   static ArgTypeResult Invalid() { return ArgTypeResult(true); }
   static ArgTypeResult Invalid() { return ArgTypeResult(true); }

+ 4 - 5
lib/Analysis/FormatString.cpp

@@ -228,7 +228,6 @@ bool ArgTypeResult::matchesType(ASTContext &C, QualType argTy) const {
       return false;
       return false;
     }
     }
       
       
-    case TypedefTy:
     case SpecificTy: {
     case SpecificTy: {
       argTy = C.getCanonicalType(argTy).getUnqualifiedType();
       argTy = C.getCanonicalType(argTy).getUnqualifiedType();
       if (T == argTy)
       if (T == argTy)
@@ -332,7 +331,6 @@ QualType ArgTypeResult::getRepresentativeType(ASTContext &C) const {
     case AnyCharTy:
     case AnyCharTy:
       return C.CharTy;
       return C.CharTy;
     case SpecificTy:
     case SpecificTy:
-    case TypedefTy:
       return T;
       return T;
     case CStrTy:
     case CStrTy:
       return C.getPointerType(C.CharTy);
       return C.getPointerType(C.CharTy);
@@ -354,9 +352,10 @@ QualType ArgTypeResult::getRepresentativeType(ASTContext &C) const {
 }
 }
 
 
 std::string ArgTypeResult::getRepresentativeTypeName(ASTContext &C) const {
 std::string ArgTypeResult::getRepresentativeTypeName(ASTContext &C) const {
-  if (K != TypedefTy)
-    return std::string("'") + getRepresentativeType(C).getAsString() + "'";
-  return std::string("'") + Name + "' (aka '" + T.getAsString() + "')";
+  std::string S = getRepresentativeType(C).getAsString();
+  if (Name)
+    return std::string("'") + Name + "' (aka '" + S + "')";
+  return std::string("'") + S + "'";
 }
 }
 
 
 
 

+ 9 - 6
lib/Analysis/PrintfFormatString.cpp

@@ -247,7 +247,8 @@ ArgTypeResult PrintfSpecifier::getArgType(ASTContext &Ctx) const {
   if (CS.getKind() == ConversionSpecifier::cArg)
   if (CS.getKind() == ConversionSpecifier::cArg)
     switch (LM.getKind()) {
     switch (LM.getKind()) {
       case LengthModifier::None: return Ctx.IntTy;
       case LengthModifier::None: return Ctx.IntTy;
-      case LengthModifier::AsLong: return ArgTypeResult::WIntTy;
+      case LengthModifier::AsLong:
+        return ArgTypeResult(ArgTypeResult::WIntTy, "wint_t");
       default:
       default:
         return ArgTypeResult::Invalid();
         return ArgTypeResult::Invalid();
     }
     }
@@ -296,14 +297,16 @@ ArgTypeResult PrintfSpecifier::getArgType(ASTContext &Ctx) const {
   }
   }
 
 
   switch (CS.getKind()) {
   switch (CS.getKind()) {
-    case ConversionSpecifier::sArg:
-      return ArgTypeResult(LM.getKind() == LengthModifier::AsWideChar ?
-          ArgTypeResult::WCStrTy : ArgTypeResult::CStrTy);
+    case ConversionSpecifier::sArg: {
+      if (LM.getKind() == LengthModifier::AsWideChar)
+        return ArgTypeResult(ArgTypeResult::WCStrTy, "wchar_t *");
+      return ArgTypeResult::CStrTy;
+    }
     case ConversionSpecifier::SArg:
     case ConversionSpecifier::SArg:
       // FIXME: This appears to be Mac OS X specific.
       // FIXME: This appears to be Mac OS X specific.
-      return ArgTypeResult::WCStrTy;
+      return ArgTypeResult(ArgTypeResult::WCStrTy, "wchar_t *");
     case ConversionSpecifier::CArg:
     case ConversionSpecifier::CArg:
-      return Ctx.WCharTy;
+      return ArgTypeResult(Ctx.WCharTy, "wchar_t");
     case ConversionSpecifier::pArg:
     case ConversionSpecifier::pArg:
       return ArgTypeResult::CPointerTy;
       return ArgTypeResult::CPointerTy;
     default:
     default:

+ 0 - 3
lib/Sema/SemaChecking.cpp

@@ -2229,9 +2229,6 @@ CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
       llvm::raw_svector_ostream os(buf);
       llvm::raw_svector_ostream os(buf);
       fixedFS.toString(os);
       fixedFS.toString(os);
 
 
-      // FIXME: getRepresentativeType() perhaps should return a string
-      // instead of a QualType to better handle when the representative
-      // type is 'wint_t' (which is defined in the system headers).
       EmitFormatDiagnostic(
       EmitFormatDiagnostic(
         S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
         S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
           << ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
           << ATR.getRepresentativeTypeName(S.Context) << Ex->getType()

+ 5 - 0
test/Sema/format-strings-int-typedefs.c

@@ -7,6 +7,11 @@ void test(void) {
   printf("%ju", 42.0); // expected-warning {{conversion specifies type 'uintmax_t' (aka 'unsigned long long')}}
   printf("%ju", 42.0); // expected-warning {{conversion specifies type 'uintmax_t' (aka 'unsigned long long')}}
   printf("%zu", 42.0); // expected-warning {{conversion specifies type 'size_t' (aka 'unsigned long')}}
   printf("%zu", 42.0); // expected-warning {{conversion specifies type 'size_t' (aka 'unsigned long')}}
   printf("%td", 42.0); // expected-warning {{conversion specifies type 'ptrdiff_t' (aka 'int')}}
   printf("%td", 42.0); // expected-warning {{conversion specifies type 'ptrdiff_t' (aka 'int')}}
+  printf("%lc", 42.0); // expected-warning {{conversion specifies type 'wint_t' (aka 'int')}}
+  printf("%ls", 42.0); // expected-warning {{conversion specifies type 'wchar_t *' (aka 'int *')}}
+  printf("%S", 42.0);  // expected-warning {{conversion specifies type 'wchar_t *' (aka 'int *')}}
+  printf("%C", 42.0);  // expected-warning {{conversion specifies type 'wchar_t' (aka 'int')}}
+
 
 
   // typedef size_t et al. to something crazy.
   // typedef size_t et al. to something crazy.
   typedef void *size_t;
   typedef void *size_t;