forked from OSchip/llvm-project
Make printf warnings refer to intmax_t et al. by name
in addition to underlying type. For example, the warning for printf("%zu", 42.0); changes from "conversion specifies type 'unsigned long'" to "conversion specifies type 'size_t' (aka 'unsigned long')" (This is a second attempt after r145697, which got reverted.) llvm-svn: 146032
This commit is contained in:
parent
1d578e8835
commit
772e9270f6
|
@ -199,15 +199,17 @@ 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 };
|
AnyCharTy, CStrTy, WCStrTy, WIntTy, TypedefTy };
|
||||||
private:
|
private:
|
||||||
const Kind K;
|
const Kind K;
|
||||||
QualType T;
|
QualType T;
|
||||||
ArgTypeResult(bool) : K(InvalidTy) {}
|
const char *Name;
|
||||||
|
ArgTypeResult(bool) : K(InvalidTy), Name(0) {}
|
||||||
public:
|
public:
|
||||||
ArgTypeResult(Kind k = UnknownTy) : K(k) {}
|
ArgTypeResult(Kind k = UnknownTy) : K(k), Name(0) {}
|
||||||
ArgTypeResult(QualType t) : K(SpecificTy), T(t) {}
|
ArgTypeResult(QualType t) : K(SpecificTy), T(t), Name(0) {}
|
||||||
ArgTypeResult(CanQualType t) : K(SpecificTy), T(t) {}
|
ArgTypeResult(QualType t, const char *n) : K(TypedefTy), T(t), Name(n) {}
|
||||||
|
ArgTypeResult(CanQualType t) : K(SpecificTy), T(t), Name(0) {}
|
||||||
|
|
||||||
static ArgTypeResult Invalid() { return ArgTypeResult(true); }
|
static ArgTypeResult Invalid() { return ArgTypeResult(true); }
|
||||||
|
|
||||||
|
@ -222,6 +224,8 @@ public:
|
||||||
bool matchesAnyObjCObjectRef() const { return K == ObjCPointerTy; }
|
bool matchesAnyObjCObjectRef() const { return K == ObjCPointerTy; }
|
||||||
|
|
||||||
QualType getRepresentativeType(ASTContext &C) const;
|
QualType getRepresentativeType(ASTContext &C) const;
|
||||||
|
|
||||||
|
std::string getRepresentativeTypeName(ASTContext &C) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class OptionalAmount {
|
class OptionalAmount {
|
||||||
|
|
|
@ -228,6 +228,7 @@ 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)
|
||||||
|
@ -331,6 +332,7 @@ 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);
|
||||||
|
@ -351,6 +353,13 @@ QualType ArgTypeResult::getRepresentativeType(ASTContext &C) const {
|
||||||
return QualType();
|
return QualType();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string ArgTypeResult::getRepresentativeTypeName(ASTContext &C) const {
|
||||||
|
if (K != TypedefTy)
|
||||||
|
return std::string("'") + getRepresentativeType(C).getAsString() + "'";
|
||||||
|
return std::string("'") + Name + "' (aka '" + T.getAsString() + "')";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
// Methods on OptionalAmount.
|
// Methods on OptionalAmount.
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
@ -485,5 +494,3 @@ bool FormatSpecifier::hasValidLengthModifier() const {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -301,11 +301,13 @@ ArgTypeResult PrintfSpecifier::getArgType(ASTContext &Ctx) const {
|
||||||
case LengthModifier::AsShort: return Ctx.ShortTy;
|
case LengthModifier::AsShort: return Ctx.ShortTy;
|
||||||
case LengthModifier::AsLong: return Ctx.LongTy;
|
case LengthModifier::AsLong: return Ctx.LongTy;
|
||||||
case LengthModifier::AsLongLong: return Ctx.LongLongTy;
|
case LengthModifier::AsLongLong: return Ctx.LongLongTy;
|
||||||
case LengthModifier::AsIntMax: return Ctx.getIntMaxType();
|
case LengthModifier::AsIntMax:
|
||||||
|
return ArgTypeResult(Ctx.getIntMaxType(), "intmax_t");
|
||||||
case LengthModifier::AsSizeT:
|
case LengthModifier::AsSizeT:
|
||||||
// FIXME: How to get the corresponding signed version of size_t?
|
// FIXME: How to get the corresponding signed version of size_t?
|
||||||
return ArgTypeResult();
|
return ArgTypeResult();
|
||||||
case LengthModifier::AsPtrDiff: return Ctx.getPointerDiffType();
|
case LengthModifier::AsPtrDiff:
|
||||||
|
return ArgTypeResult(Ctx.getPointerDiffType(), "ptrdiff_t");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CS.isUIntArg())
|
if (CS.isUIntArg())
|
||||||
|
@ -317,9 +319,10 @@ ArgTypeResult PrintfSpecifier::getArgType(ASTContext &Ctx) const {
|
||||||
case LengthModifier::AsShort: return Ctx.UnsignedShortTy;
|
case LengthModifier::AsShort: return Ctx.UnsignedShortTy;
|
||||||
case LengthModifier::AsLong: return Ctx.UnsignedLongTy;
|
case LengthModifier::AsLong: return Ctx.UnsignedLongTy;
|
||||||
case LengthModifier::AsLongLong: return Ctx.UnsignedLongLongTy;
|
case LengthModifier::AsLongLong: return Ctx.UnsignedLongLongTy;
|
||||||
case LengthModifier::AsIntMax: return Ctx.getUIntMaxType();
|
case LengthModifier::AsIntMax:
|
||||||
|
return ArgTypeResult(Ctx.getUIntMaxType(), "uintmax_t");
|
||||||
case LengthModifier::AsSizeT:
|
case LengthModifier::AsSizeT:
|
||||||
return Ctx.getSizeType();
|
return ArgTypeResult(Ctx.getSizeType(), "size_t");
|
||||||
case LengthModifier::AsPtrDiff:
|
case LengthModifier::AsPtrDiff:
|
||||||
// FIXME: How to get the corresponding unsigned
|
// FIXME: How to get the corresponding unsigned
|
||||||
// version of ptrdiff_t?
|
// version of ptrdiff_t?
|
||||||
|
|
|
@ -2014,7 +2014,7 @@ bool CheckPrintfHandler::HandleAmount(
|
||||||
|
|
||||||
if (!ATR.matchesType(S.Context, T)) {
|
if (!ATR.matchesType(S.Context, T)) {
|
||||||
EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type)
|
EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type)
|
||||||
<< k << ATR.getRepresentativeType(S.Context)
|
<< k << ATR.getRepresentativeTypeName(S.Context)
|
||||||
<< T << Arg->getSourceRange(),
|
<< T << Arg->getSourceRange(),
|
||||||
getLocationOfByte(Amt.getStart()),
|
getLocationOfByte(Amt.getStart()),
|
||||||
/*IsStringLocation*/true,
|
/*IsStringLocation*/true,
|
||||||
|
@ -2234,7 +2234,7 @@ CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
|
||||||
// type is 'wint_t' (which is defined in the system headers).
|
// 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.getRepresentativeType(S.Context) << Ex->getType()
|
<< ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
|
||||||
<< Ex->getSourceRange(),
|
<< Ex->getSourceRange(),
|
||||||
getLocationOfByte(CS.getStart()),
|
getLocationOfByte(CS.getStart()),
|
||||||
/*IsStringLocation*/true,
|
/*IsStringLocation*/true,
|
||||||
|
@ -2246,7 +2246,7 @@ CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
|
||||||
else {
|
else {
|
||||||
S.Diag(getLocationOfByte(CS.getStart()),
|
S.Diag(getLocationOfByte(CS.getStart()),
|
||||||
diag::warn_printf_conversion_argument_type_mismatch)
|
diag::warn_printf_conversion_argument_type_mismatch)
|
||||||
<< ATR.getRepresentativeType(S.Context) << Ex->getType()
|
<< ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
|
||||||
<< getSpecifierRange(startSpecifier, specifierLen)
|
<< getSpecifierRange(startSpecifier, specifierLen)
|
||||||
<< Ex->getSourceRange();
|
<< Ex->getSourceRange();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
// RUN: %clang_cc1 -triple i386-apple-darwin9 -fsyntax-only -verify %s
|
||||||
|
|
||||||
|
int printf(char const *, ...);
|
||||||
|
|
||||||
|
void test(void) {
|
||||||
|
printf("%jd", 42.0); // expected-warning {{conversion specifies type 'intmax_t' (aka '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("%td", 42.0); // expected-warning {{conversion specifies type 'ptrdiff_t' (aka 'int')}}
|
||||||
|
|
||||||
|
// typedef size_t et al. to something crazy.
|
||||||
|
typedef void *size_t;
|
||||||
|
typedef void *intmax_t;
|
||||||
|
typedef void *uintmax_t;
|
||||||
|
typedef void *ptrdiff_t;
|
||||||
|
|
||||||
|
// The warning still fires, because it checks the underlying type.
|
||||||
|
printf("%jd", (intmax_t)42); // expected-warning {{conversion specifies type 'intmax_t' (aka 'long long') but the argument has type 'intmax_t' (aka 'void *')}}
|
||||||
|
printf("%ju", (uintmax_t)42); // expected-warning {{conversion specifies type 'uintmax_t' (aka 'unsigned long long') but the argument has type 'uintmax_t' (aka 'void *')}}
|
||||||
|
printf("%zu", (size_t)42); // expected-warning {{conversion specifies type 'size_t' (aka 'unsigned long') but the argument has type 'size_t' (aka 'void *')}}
|
||||||
|
printf("%td", (ptrdiff_t)42); // expected-warning {{conversion specifies type 'ptrdiff_t' (aka 'int') but the argument has type 'ptrdiff_t' (aka 'void *')}}
|
||||||
|
}
|
|
@ -4,12 +4,12 @@ int printf(char const *, ...);
|
||||||
|
|
||||||
void test(void) {
|
void test(void) {
|
||||||
// size_t
|
// size_t
|
||||||
printf("%zu", (double)42); // expected-warning {{conversion specifies type 'unsigned long' but the argument has type 'double''}}
|
printf("%zu", (double)42); // expected-warning {{conversion specifies type 'size_t' (aka 'unsigned long') but the argument has type 'double''}}
|
||||||
|
|
||||||
// intmax_t / uintmax_t
|
// intmax_t / uintmax_t
|
||||||
printf("%jd", (double)42); // expected-warning {{conversion specifies type 'long' but the argument has type 'double''}}
|
printf("%jd", (double)42); // expected-warning {{conversion specifies type 'intmax_t' (aka 'long') but the argument has type 'double''}}
|
||||||
printf("%ju", (double)42); // expected-warning {{conversion specifies type 'unsigned long' but the argument has type 'double''}}
|
printf("%ju", (double)42); // expected-warning {{conversion specifies type 'uintmax_t' (aka 'unsigned long') but the argument has type 'double''}}
|
||||||
|
|
||||||
// ptrdiff_t
|
// ptrdiff_t
|
||||||
printf("%td", (double)42); // expected-warning {{conversion specifies type 'long' but the argument has type 'double''}}
|
printf("%td", (double)42); // expected-warning {{conversion specifies type 'ptrdiff_t' (aka 'long') but the argument has type 'double''}}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue