2010-07-16 10:11:22 +08:00
|
|
|
// FormatString.cpp - Common stuff for handling printf/scanf formats -*- C++ -*-
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Shared details for processing format strings of printf and scanf
|
|
|
|
// (and friends).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "FormatStringParsing.h"
|
2011-12-15 18:25:47 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2012-09-08 12:00:03 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2010-07-16 10:11:22 +08:00
|
|
|
|
2012-08-07 16:11:26 +08:00
|
|
|
using clang::analyze_format_string::ArgType;
|
2010-07-16 10:11:22 +08:00
|
|
|
using clang::analyze_format_string::FormatStringHandler;
|
|
|
|
using clang::analyze_format_string::FormatSpecifier;
|
|
|
|
using clang::analyze_format_string::LengthModifier;
|
|
|
|
using clang::analyze_format_string::OptionalAmount;
|
|
|
|
using clang::analyze_format_string::PositionContext;
|
2010-07-21 04:04:42 +08:00
|
|
|
using clang::analyze_format_string::ConversionSpecifier;
|
2010-07-16 10:11:22 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
// Key function to FormatStringHandler.
|
|
|
|
FormatStringHandler::~FormatStringHandler() {}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Functions for parsing format strings components in both printf and
|
|
|
|
// scanf format strings.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
OptionalAmount
|
|
|
|
clang::analyze_format_string::ParseAmount(const char *&Beg, const char *E) {
|
|
|
|
const char *I = Beg;
|
|
|
|
UpdateOnReturn <const char*> UpdateBeg(Beg, I);
|
|
|
|
|
|
|
|
unsigned accumulator = 0;
|
|
|
|
bool hasDigits = false;
|
|
|
|
|
|
|
|
for ( ; I != E; ++I) {
|
|
|
|
char c = *I;
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
hasDigits = true;
|
|
|
|
accumulator = (accumulator * 10) + (c - '0');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasDigits)
|
|
|
|
return OptionalAmount(OptionalAmount::Constant, accumulator, Beg, I - Beg,
|
|
|
|
false);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OptionalAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionalAmount
|
|
|
|
clang::analyze_format_string::ParseNonPositionAmount(const char *&Beg,
|
|
|
|
const char *E,
|
|
|
|
unsigned &argIndex) {
|
|
|
|
if (*Beg == '*') {
|
|
|
|
++Beg;
|
|
|
|
return OptionalAmount(OptionalAmount::Arg, argIndex++, Beg, 0, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParseAmount(Beg, E);
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionalAmount
|
|
|
|
clang::analyze_format_string::ParsePositionAmount(FormatStringHandler &H,
|
|
|
|
const char *Start,
|
|
|
|
const char *&Beg,
|
|
|
|
const char *E,
|
|
|
|
PositionContext p) {
|
|
|
|
if (*Beg == '*') {
|
|
|
|
const char *I = Beg + 1;
|
|
|
|
const OptionalAmount &Amt = ParseAmount(I, E);
|
|
|
|
|
|
|
|
if (Amt.getHowSpecified() == OptionalAmount::NotSpecified) {
|
|
|
|
H.HandleInvalidPosition(Beg, I - Beg, p);
|
|
|
|
return OptionalAmount(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (I == E) {
|
|
|
|
// No more characters left?
|
|
|
|
H.HandleIncompleteSpecifier(Start, E - Start);
|
|
|
|
return OptionalAmount(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(Amt.getHowSpecified() == OptionalAmount::Constant);
|
|
|
|
|
|
|
|
if (*I == '$') {
|
|
|
|
// Handle positional arguments
|
|
|
|
|
|
|
|
// Special case: '*0$', since this is an easy mistake.
|
|
|
|
if (Amt.getConstantAmount() == 0) {
|
|
|
|
H.HandleZeroPosition(Beg, I - Beg + 1);
|
|
|
|
return OptionalAmount(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *Tmp = Beg;
|
|
|
|
Beg = ++I;
|
|
|
|
|
|
|
|
return OptionalAmount(OptionalAmount::Arg, Amt.getConstantAmount() - 1,
|
|
|
|
Tmp, 0, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
H.HandleInvalidPosition(Beg, I - Beg, p);
|
|
|
|
return OptionalAmount(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParseAmount(Beg, E);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
clang::analyze_format_string::ParseFieldWidth(FormatStringHandler &H,
|
|
|
|
FormatSpecifier &CS,
|
|
|
|
const char *Start,
|
|
|
|
const char *&Beg, const char *E,
|
|
|
|
unsigned *argIndex) {
|
|
|
|
// FIXME: Support negative field widths.
|
|
|
|
if (argIndex) {
|
|
|
|
CS.setFieldWidth(ParseNonPositionAmount(Beg, E, *argIndex));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const OptionalAmount Amt =
|
|
|
|
ParsePositionAmount(H, Start, Beg, E,
|
|
|
|
analyze_format_string::FieldWidthPos);
|
|
|
|
|
|
|
|
if (Amt.isInvalid())
|
|
|
|
return true;
|
|
|
|
CS.setFieldWidth(Amt);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
clang::analyze_format_string::ParseArgPosition(FormatStringHandler &H,
|
|
|
|
FormatSpecifier &FS,
|
|
|
|
const char *Start,
|
|
|
|
const char *&Beg,
|
|
|
|
const char *E) {
|
|
|
|
const char *I = Beg;
|
|
|
|
|
|
|
|
const OptionalAmount &Amt = ParseAmount(I, E);
|
|
|
|
|
|
|
|
if (I == E) {
|
|
|
|
// No more characters left?
|
|
|
|
H.HandleIncompleteSpecifier(Start, E - Start);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Amt.getHowSpecified() == OptionalAmount::Constant && *(I++) == '$') {
|
2012-03-09 18:10:54 +08:00
|
|
|
// Warn that positional arguments are non-standard.
|
|
|
|
H.HandlePosition(Start, I - Start);
|
|
|
|
|
2010-07-16 10:11:22 +08:00
|
|
|
// Special case: '%0$', since this is an easy mistake.
|
|
|
|
if (Amt.getConstantAmount() == 0) {
|
|
|
|
H.HandleZeroPosition(Start, I - Start);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FS.setArgIndex(Amt.getConstantAmount() - 1);
|
|
|
|
FS.setUsesPositionalArg();
|
|
|
|
// Update the caller's pointer if we decided to consume
|
|
|
|
// these characters.
|
|
|
|
Beg = I;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS,
|
|
|
|
const char *&I,
|
2011-12-15 18:25:47 +08:00
|
|
|
const char *E,
|
|
|
|
const LangOptions &LO,
|
|
|
|
bool IsScanf) {
|
2010-07-16 10:11:22 +08:00
|
|
|
LengthModifier::Kind lmKind = LengthModifier::None;
|
|
|
|
const char *lmPosition = I;
|
|
|
|
switch (*I) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case 'h':
|
|
|
|
++I;
|
2011-10-25 12:20:41 +08:00
|
|
|
lmKind = (I != E && *I == 'h') ? (++I, LengthModifier::AsChar)
|
|
|
|
: LengthModifier::AsShort;
|
2010-07-16 10:11:22 +08:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
++I;
|
2011-10-25 12:20:41 +08:00
|
|
|
lmKind = (I != E && *I == 'l') ? (++I, LengthModifier::AsLongLong)
|
|
|
|
: LengthModifier::AsLong;
|
2010-07-16 10:11:22 +08:00
|
|
|
break;
|
|
|
|
case 'j': lmKind = LengthModifier::AsIntMax; ++I; break;
|
|
|
|
case 'z': lmKind = LengthModifier::AsSizeT; ++I; break;
|
|
|
|
case 't': lmKind = LengthModifier::AsPtrDiff; ++I; break;
|
|
|
|
case 'L': lmKind = LengthModifier::AsLongDouble; ++I; break;
|
2012-02-17 00:34:54 +08:00
|
|
|
case 'q': lmKind = LengthModifier::AsQuad; ++I; break;
|
2011-12-15 18:25:47 +08:00
|
|
|
case 'a':
|
2013-01-02 19:42:31 +08:00
|
|
|
if (IsScanf && !LO.C99 && !LO.CPlusPlus11) {
|
2011-12-15 18:25:47 +08:00
|
|
|
// For scanf in C90, look at the next character to see if this should
|
|
|
|
// be parsed as the GNU extension 'a' length modifier. If not, this
|
|
|
|
// will be parsed as a conversion specifier.
|
|
|
|
++I;
|
|
|
|
if (I != E && (*I == 's' || *I == 'S' || *I == '[')) {
|
|
|
|
lmKind = LengthModifier::AsAllocate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--I;
|
|
|
|
}
|
|
|
|
return false;
|
2012-01-13 01:11:12 +08:00
|
|
|
case 'm':
|
|
|
|
if (IsScanf) {
|
|
|
|
lmKind = LengthModifier::AsMAllocate;
|
|
|
|
++I;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
2010-07-16 10:11:22 +08:00
|
|
|
}
|
|
|
|
LengthModifier lm(lmPosition, lmKind);
|
|
|
|
FS.setLengthModifier(lm);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-08-07 16:11:26 +08:00
|
|
|
// Methods on ArgType.
|
2010-07-16 10:11:22 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-08-07 16:11:26 +08:00
|
|
|
bool ArgType::matchesType(ASTContext &C, QualType argTy) const {
|
2012-08-07 16:59:46 +08:00
|
|
|
if (Ptr) {
|
|
|
|
// It has to be a pointer.
|
|
|
|
const PointerType *PT = argTy->getAs<PointerType>();
|
|
|
|
if (!PT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We cannot write through a const qualified pointer.
|
|
|
|
if (PT->getPointeeType().isConstQualified())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
argTy = PT->getPointeeType();
|
|
|
|
}
|
|
|
|
|
2010-07-16 10:11:22 +08:00
|
|
|
switch (K) {
|
|
|
|
case InvalidTy:
|
2012-08-07 16:11:26 +08:00
|
|
|
llvm_unreachable("ArgType must be valid");
|
2010-07-16 10:11:22 +08:00
|
|
|
|
|
|
|
case UnknownTy:
|
|
|
|
return true;
|
2011-10-25 12:20:41 +08:00
|
|
|
|
|
|
|
case AnyCharTy: {
|
2012-06-05 06:48:57 +08:00
|
|
|
if (const EnumType *ETy = argTy->getAs<EnumType>())
|
|
|
|
argTy = ETy->getDecl()->getIntegerType();
|
|
|
|
|
2011-10-25 12:20:41 +08:00
|
|
|
if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
|
|
|
|
switch (BT->getKind()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case BuiltinType::Char_S:
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
case BuiltinType::Char_U:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-16 10:11:22 +08:00
|
|
|
case SpecificTy: {
|
2012-06-05 06:48:57 +08:00
|
|
|
if (const EnumType *ETy = argTy->getAs<EnumType>())
|
|
|
|
argTy = ETy->getDecl()->getIntegerType();
|
2010-07-16 10:11:22 +08:00
|
|
|
argTy = C.getCanonicalType(argTy).getUnqualifiedType();
|
2012-06-05 06:48:57 +08:00
|
|
|
|
2011-12-03 07:21:43 +08:00
|
|
|
if (T == argTy)
|
2010-07-16 10:11:22 +08:00
|
|
|
return true;
|
2011-07-14 01:35:14 +08:00
|
|
|
// Check for "compatible types".
|
2011-07-15 01:05:32 +08:00
|
|
|
if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
|
2010-07-16 10:11:22 +08:00
|
|
|
switch (BT->getKind()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case BuiltinType::Char_S:
|
|
|
|
case BuiltinType::SChar:
|
2011-07-14 01:35:14 +08:00
|
|
|
case BuiltinType::Char_U:
|
2011-07-15 01:05:32 +08:00
|
|
|
case BuiltinType::UChar:
|
2012-05-09 01:21:31 +08:00
|
|
|
return T == C.UnsignedCharTy || T == C.SignedCharTy;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::Short:
|
2011-12-03 07:21:43 +08:00
|
|
|
return T == C.UnsignedShortTy;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::UShort:
|
2011-12-03 07:21:43 +08:00
|
|
|
return T == C.ShortTy;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::Int:
|
2011-12-03 07:21:43 +08:00
|
|
|
return T == C.UnsignedIntTy;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::UInt:
|
2011-12-03 07:21:43 +08:00
|
|
|
return T == C.IntTy;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::Long:
|
2011-12-03 07:21:43 +08:00
|
|
|
return T == C.UnsignedLongTy;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::ULong:
|
2011-12-03 07:21:43 +08:00
|
|
|
return T == C.LongTy;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::LongLong:
|
2011-12-03 07:21:43 +08:00
|
|
|
return T == C.UnsignedLongLongTy;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::ULongLong:
|
2011-12-03 07:21:43 +08:00
|
|
|
return T == C.LongLongTy;
|
2010-07-16 10:11:22 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CStrTy: {
|
|
|
|
const PointerType *PT = argTy->getAs<PointerType>();
|
|
|
|
if (!PT)
|
|
|
|
return false;
|
|
|
|
QualType pointeeTy = PT->getPointeeType();
|
|
|
|
if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
|
|
|
|
switch (BT->getKind()) {
|
|
|
|
case BuiltinType::Void:
|
|
|
|
case BuiltinType::Char_U:
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
case BuiltinType::Char_S:
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WCStrTy: {
|
|
|
|
const PointerType *PT = argTy->getAs<PointerType>();
|
|
|
|
if (!PT)
|
|
|
|
return false;
|
|
|
|
QualType pointeeTy =
|
|
|
|
C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
|
2013-05-10 18:08:40 +08:00
|
|
|
return pointeeTy == C.getWideCharType();
|
2010-07-16 10:11:22 +08:00
|
|
|
}
|
2010-08-25 06:24:51 +08:00
|
|
|
|
|
|
|
case WIntTy: {
|
2012-05-04 18:55:22 +08:00
|
|
|
|
2010-08-25 06:24:51 +08:00
|
|
|
QualType PromoArg =
|
|
|
|
argTy->isPromotableIntegerType()
|
|
|
|
? C.getPromotedIntegerType(argTy) : argTy;
|
|
|
|
|
2012-05-04 18:55:22 +08:00
|
|
|
QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
|
2010-08-25 06:24:51 +08:00
|
|
|
PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
|
|
|
|
|
2012-05-04 18:55:22 +08:00
|
|
|
// If the promoted argument is the corresponding signed type of the
|
|
|
|
// wint_t type, then it should match.
|
|
|
|
if (PromoArg->hasSignedIntegerRepresentation() &&
|
|
|
|
C.getCorrespondingUnsignedType(PromoArg) == WInt)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return WInt == PromoArg;
|
2010-08-25 06:24:51 +08:00
|
|
|
}
|
2010-07-16 10:11:22 +08:00
|
|
|
|
|
|
|
case CPointerTy:
|
2010-11-06 22:58:53 +08:00
|
|
|
return argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
|
2012-03-16 05:22:27 +08:00
|
|
|
argTy->isBlockPointerType() || argTy->isNullPtrType();
|
2010-07-16 10:11:22 +08:00
|
|
|
|
2012-02-07 05:45:29 +08:00
|
|
|
case ObjCPointerTy: {
|
|
|
|
if (argTy->getAs<ObjCObjectPointerType>() ||
|
|
|
|
argTy->getAs<BlockPointerType>())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Handle implicit toll-free bridging.
|
|
|
|
if (const PointerType *PT = argTy->getAs<PointerType>()) {
|
|
|
|
// Things such as CFTypeRef are really just opaque pointers
|
|
|
|
// to C structs representing CF types that can often be bridged
|
|
|
|
// to Objective-C objects. Since the compiler doesn't know which
|
|
|
|
// structs can be toll-free bridged, we just accept them all.
|
|
|
|
QualType pointee = PT->getPointeeType();
|
|
|
|
if (pointee->getAsStructureType() || pointee->isVoidType())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-16 10:11:22 +08:00
|
|
|
}
|
|
|
|
|
2012-08-07 16:11:26 +08:00
|
|
|
llvm_unreachable("Invalid ArgType Kind!");
|
2010-07-16 10:11:22 +08:00
|
|
|
}
|
|
|
|
|
2012-08-07 16:11:26 +08:00
|
|
|
QualType ArgType::getRepresentativeType(ASTContext &C) const {
|
2012-08-07 16:59:46 +08:00
|
|
|
QualType Res;
|
2010-07-16 10:11:22 +08:00
|
|
|
switch (K) {
|
|
|
|
case InvalidTy:
|
2012-08-07 16:11:26 +08:00
|
|
|
llvm_unreachable("No representative type for Invalid ArgType");
|
2010-07-16 10:11:22 +08:00
|
|
|
case UnknownTy:
|
2012-08-07 16:59:46 +08:00
|
|
|
llvm_unreachable("No representative type for Unknown ArgType");
|
2011-10-25 12:20:41 +08:00
|
|
|
case AnyCharTy:
|
2012-08-07 16:59:46 +08:00
|
|
|
Res = C.CharTy;
|
|
|
|
break;
|
2010-07-16 10:11:22 +08:00
|
|
|
case SpecificTy:
|
2012-08-07 16:59:46 +08:00
|
|
|
Res = T;
|
|
|
|
break;
|
2010-07-16 10:11:22 +08:00
|
|
|
case CStrTy:
|
2012-08-07 16:59:46 +08:00
|
|
|
Res = C.getPointerType(C.CharTy);
|
|
|
|
break;
|
2010-07-16 10:11:22 +08:00
|
|
|
case WCStrTy:
|
2013-05-10 18:08:40 +08:00
|
|
|
Res = C.getPointerType(C.getWideCharType());
|
2012-08-07 16:59:46 +08:00
|
|
|
break;
|
2010-07-16 10:11:22 +08:00
|
|
|
case ObjCPointerTy:
|
2012-08-07 16:59:46 +08:00
|
|
|
Res = C.ObjCBuiltinIdTy;
|
|
|
|
break;
|
2010-07-16 10:11:22 +08:00
|
|
|
case CPointerTy:
|
2012-08-07 16:59:46 +08:00
|
|
|
Res = C.VoidPtrTy;
|
|
|
|
break;
|
2010-08-25 06:24:51 +08:00
|
|
|
case WIntTy: {
|
2012-08-07 16:59:46 +08:00
|
|
|
Res = C.getWIntType();
|
|
|
|
break;
|
2010-08-25 06:24:51 +08:00
|
|
|
}
|
2010-07-16 10:11:22 +08:00
|
|
|
}
|
|
|
|
|
2012-08-07 16:59:46 +08:00
|
|
|
if (Ptr)
|
|
|
|
Res = C.getPointerType(Res);
|
|
|
|
return Res;
|
2010-07-16 10:11:22 +08:00
|
|
|
}
|
|
|
|
|
2012-08-07 16:11:26 +08:00
|
|
|
std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
|
2011-12-09 20:22:12 +08:00
|
|
|
std::string S = getRepresentativeType(C).getAsString();
|
2012-08-07 16:59:46 +08:00
|
|
|
|
|
|
|
std::string Alias;
|
|
|
|
if (Name) {
|
|
|
|
// Use a specific name for this type, e.g. "size_t".
|
|
|
|
Alias = Name;
|
|
|
|
if (Ptr) {
|
|
|
|
// If ArgType is actually a pointer to T, append an asterisk.
|
|
|
|
Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
|
|
|
|
}
|
|
|
|
// If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
|
|
|
|
if (S == Alias)
|
|
|
|
Alias.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Alias.empty())
|
|
|
|
return std::string("'") + Alias + "' (aka '" + S + "')";
|
2011-12-09 20:22:12 +08:00
|
|
|
return std::string("'") + S + "'";
|
2011-12-07 18:33:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-16 10:11:22 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods on OptionalAmount.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-08-07 16:11:26 +08:00
|
|
|
ArgType
|
2010-07-16 10:11:22 +08:00
|
|
|
analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
|
|
|
|
return Ctx.IntTy;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods on LengthModifier.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
const char *
|
|
|
|
analyze_format_string::LengthModifier::toString() const {
|
|
|
|
switch (kind) {
|
|
|
|
case AsChar:
|
|
|
|
return "hh";
|
|
|
|
case AsShort:
|
|
|
|
return "h";
|
|
|
|
case AsLong: // or AsWideChar
|
|
|
|
return "l";
|
|
|
|
case AsLongLong:
|
|
|
|
return "ll";
|
2012-02-17 00:34:54 +08:00
|
|
|
case AsQuad:
|
|
|
|
return "q";
|
2010-07-16 10:11:22 +08:00
|
|
|
case AsIntMax:
|
|
|
|
return "j";
|
|
|
|
case AsSizeT:
|
|
|
|
return "z";
|
|
|
|
case AsPtrDiff:
|
|
|
|
return "t";
|
|
|
|
case AsLongDouble:
|
|
|
|
return "L";
|
2011-12-15 18:25:47 +08:00
|
|
|
case AsAllocate:
|
|
|
|
return "a";
|
2012-01-13 01:11:12 +08:00
|
|
|
case AsMAllocate:
|
|
|
|
return "m";
|
2010-07-16 10:11:22 +08:00
|
|
|
case None:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-12-09 19:11:07 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods on ConversionSpecifier.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
const char *ConversionSpecifier::toString() const {
|
|
|
|
switch (kind) {
|
|
|
|
case dArg: return "d";
|
2012-09-13 10:11:03 +08:00
|
|
|
case DArg: return "D";
|
2011-12-09 19:11:07 +08:00
|
|
|
case iArg: return "i";
|
|
|
|
case oArg: return "o";
|
2012-09-13 10:11:03 +08:00
|
|
|
case OArg: return "O";
|
2011-12-09 19:11:07 +08:00
|
|
|
case uArg: return "u";
|
2012-09-13 10:11:03 +08:00
|
|
|
case UArg: return "U";
|
2011-12-09 19:11:07 +08:00
|
|
|
case xArg: return "x";
|
|
|
|
case XArg: return "X";
|
|
|
|
case fArg: return "f";
|
|
|
|
case FArg: return "F";
|
|
|
|
case eArg: return "e";
|
|
|
|
case EArg: return "E";
|
|
|
|
case gArg: return "g";
|
|
|
|
case GArg: return "G";
|
|
|
|
case aArg: return "a";
|
|
|
|
case AArg: return "A";
|
|
|
|
case cArg: return "c";
|
|
|
|
case sArg: return "s";
|
|
|
|
case pArg: return "p";
|
|
|
|
case nArg: return "n";
|
|
|
|
case PercentArg: return "%";
|
|
|
|
case ScanListArg: return "[";
|
|
|
|
case InvalidSpecifier: return NULL;
|
|
|
|
|
|
|
|
// MacOS X unicode extensions.
|
|
|
|
case CArg: return "C";
|
|
|
|
case SArg: return "S";
|
|
|
|
|
|
|
|
// Objective-C specific specifiers.
|
|
|
|
case ObjCObjArg: return "@";
|
|
|
|
|
|
|
|
// GlibC specific specifiers.
|
|
|
|
case PrintErrno: return "m";
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<ConversionSpecifier>
|
2012-09-13 10:11:15 +08:00
|
|
|
ConversionSpecifier::getStandardSpecifier() const {
|
|
|
|
ConversionSpecifier::Kind NewKind;
|
|
|
|
|
|
|
|
switch (getKind()) {
|
|
|
|
default:
|
2013-02-21 09:47:18 +08:00
|
|
|
return None;
|
2012-09-13 10:11:15 +08:00
|
|
|
case DArg:
|
|
|
|
NewKind = dArg;
|
|
|
|
break;
|
|
|
|
case UArg:
|
|
|
|
NewKind = uArg;
|
|
|
|
break;
|
|
|
|
case OArg:
|
|
|
|
NewKind = oArg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConversionSpecifier FixedCS(*this);
|
|
|
|
FixedCS.setKind(NewKind);
|
|
|
|
return FixedCS;
|
|
|
|
}
|
|
|
|
|
2010-07-16 10:11:22 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods on OptionalAmount.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void OptionalAmount::toString(raw_ostream &os) const {
|
2010-07-16 10:11:22 +08:00
|
|
|
switch (hs) {
|
|
|
|
case Invalid:
|
|
|
|
case NotSpecified:
|
|
|
|
return;
|
|
|
|
case Arg:
|
|
|
|
if (UsesDotPrefix)
|
|
|
|
os << ".";
|
|
|
|
if (usesPositionalArg())
|
|
|
|
os << "*" << getPositionalArgIndex() << "$";
|
|
|
|
else
|
|
|
|
os << "*";
|
|
|
|
break;
|
|
|
|
case Constant:
|
|
|
|
if (UsesDotPrefix)
|
|
|
|
os << ".";
|
|
|
|
os << amt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-08 12:00:03 +08:00
|
|
|
bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const {
|
2010-07-21 04:04:42 +08:00
|
|
|
switch (LM.getKind()) {
|
|
|
|
case LengthModifier::None:
|
|
|
|
return true;
|
|
|
|
|
2012-02-17 00:34:54 +08:00
|
|
|
// Handle most integer flags
|
2010-07-21 04:04:42 +08:00
|
|
|
case LengthModifier::AsChar:
|
|
|
|
case LengthModifier::AsShort:
|
|
|
|
case LengthModifier::AsLongLong:
|
2012-02-17 00:34:54 +08:00
|
|
|
case LengthModifier::AsQuad:
|
2010-07-21 04:04:42 +08:00
|
|
|
case LengthModifier::AsIntMax:
|
|
|
|
case LengthModifier::AsSizeT:
|
|
|
|
case LengthModifier::AsPtrDiff:
|
|
|
|
switch (CS.getKind()) {
|
|
|
|
case ConversionSpecifier::dArg:
|
2012-09-13 10:11:03 +08:00
|
|
|
case ConversionSpecifier::DArg:
|
2010-07-21 04:04:42 +08:00
|
|
|
case ConversionSpecifier::iArg:
|
|
|
|
case ConversionSpecifier::oArg:
|
2012-09-13 10:11:03 +08:00
|
|
|
case ConversionSpecifier::OArg:
|
2010-07-21 04:04:42 +08:00
|
|
|
case ConversionSpecifier::uArg:
|
2012-09-13 10:11:03 +08:00
|
|
|
case ConversionSpecifier::UArg:
|
2010-07-21 04:04:42 +08:00
|
|
|
case ConversionSpecifier::xArg:
|
|
|
|
case ConversionSpecifier::XArg:
|
|
|
|
case ConversionSpecifier::nArg:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-17 00:34:54 +08:00
|
|
|
// Handle 'l' flag
|
2010-07-21 04:04:42 +08:00
|
|
|
case LengthModifier::AsLong:
|
|
|
|
switch (CS.getKind()) {
|
|
|
|
case ConversionSpecifier::dArg:
|
2012-09-13 10:11:03 +08:00
|
|
|
case ConversionSpecifier::DArg:
|
2010-07-21 04:04:42 +08:00
|
|
|
case ConversionSpecifier::iArg:
|
|
|
|
case ConversionSpecifier::oArg:
|
2012-09-13 10:11:03 +08:00
|
|
|
case ConversionSpecifier::OArg:
|
2010-07-21 04:04:42 +08:00
|
|
|
case ConversionSpecifier::uArg:
|
2012-09-13 10:11:03 +08:00
|
|
|
case ConversionSpecifier::UArg:
|
2010-07-21 04:04:42 +08:00
|
|
|
case ConversionSpecifier::xArg:
|
|
|
|
case ConversionSpecifier::XArg:
|
|
|
|
case ConversionSpecifier::aArg:
|
|
|
|
case ConversionSpecifier::AArg:
|
|
|
|
case ConversionSpecifier::fArg:
|
|
|
|
case ConversionSpecifier::FArg:
|
|
|
|
case ConversionSpecifier::eArg:
|
|
|
|
case ConversionSpecifier::EArg:
|
|
|
|
case ConversionSpecifier::gArg:
|
|
|
|
case ConversionSpecifier::GArg:
|
|
|
|
case ConversionSpecifier::nArg:
|
|
|
|
case ConversionSpecifier::cArg:
|
|
|
|
case ConversionSpecifier::sArg:
|
2012-01-21 06:11:52 +08:00
|
|
|
case ConversionSpecifier::ScanListArg:
|
2010-07-21 04:04:42 +08:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
case LengthModifier::AsLongDouble:
|
|
|
|
switch (CS.getKind()) {
|
|
|
|
case ConversionSpecifier::aArg:
|
|
|
|
case ConversionSpecifier::AArg:
|
|
|
|
case ConversionSpecifier::fArg:
|
|
|
|
case ConversionSpecifier::FArg:
|
|
|
|
case ConversionSpecifier::eArg:
|
|
|
|
case ConversionSpecifier::EArg:
|
|
|
|
case ConversionSpecifier::gArg:
|
|
|
|
case ConversionSpecifier::GArg:
|
|
|
|
return true;
|
2012-09-08 12:00:03 +08:00
|
|
|
// GNU libc extension.
|
2012-01-25 05:29:54 +08:00
|
|
|
case ConversionSpecifier::dArg:
|
|
|
|
case ConversionSpecifier::iArg:
|
|
|
|
case ConversionSpecifier::oArg:
|
|
|
|
case ConversionSpecifier::uArg:
|
|
|
|
case ConversionSpecifier::xArg:
|
|
|
|
case ConversionSpecifier::XArg:
|
2012-09-08 12:00:03 +08:00
|
|
|
return !Target.getTriple().isOSDarwin() &&
|
|
|
|
!Target.getTriple().isOSWindows();
|
2010-07-21 04:04:42 +08:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-15 18:25:47 +08:00
|
|
|
|
|
|
|
case LengthModifier::AsAllocate:
|
|
|
|
switch (CS.getKind()) {
|
|
|
|
case ConversionSpecifier::sArg:
|
|
|
|
case ConversionSpecifier::SArg:
|
2012-01-12 23:07:16 +08:00
|
|
|
case ConversionSpecifier::ScanListArg:
|
2011-12-15 18:25:47 +08:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-13 01:11:12 +08:00
|
|
|
|
|
|
|
case LengthModifier::AsMAllocate:
|
|
|
|
switch (CS.getKind()) {
|
|
|
|
case ConversionSpecifier::cArg:
|
|
|
|
case ConversionSpecifier::CArg:
|
|
|
|
case ConversionSpecifier::sArg:
|
|
|
|
case ConversionSpecifier::SArg:
|
|
|
|
case ConversionSpecifier::ScanListArg:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-21 04:04:42 +08:00
|
|
|
}
|
2012-01-21 05:50:17 +08:00
|
|
|
llvm_unreachable("Invalid LengthModifier Kind!");
|
2010-07-21 04:04:42 +08:00
|
|
|
}
|
2012-02-22 18:17:01 +08:00
|
|
|
|
|
|
|
bool FormatSpecifier::hasStandardLengthModifier() const {
|
|
|
|
switch (LM.getKind()) {
|
|
|
|
case LengthModifier::None:
|
|
|
|
case LengthModifier::AsChar:
|
|
|
|
case LengthModifier::AsShort:
|
|
|
|
case LengthModifier::AsLong:
|
|
|
|
case LengthModifier::AsLongLong:
|
|
|
|
case LengthModifier::AsIntMax:
|
|
|
|
case LengthModifier::AsSizeT:
|
|
|
|
case LengthModifier::AsPtrDiff:
|
|
|
|
case LengthModifier::AsLongDouble:
|
|
|
|
return true;
|
|
|
|
case LengthModifier::AsAllocate:
|
|
|
|
case LengthModifier::AsMAllocate:
|
|
|
|
case LengthModifier::AsQuad:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid LengthModifier Kind!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FormatSpecifier::hasStandardConversionSpecifier(const LangOptions &LangOpt) const {
|
|
|
|
switch (CS.getKind()) {
|
|
|
|
case ConversionSpecifier::cArg:
|
|
|
|
case ConversionSpecifier::dArg:
|
|
|
|
case ConversionSpecifier::iArg:
|
|
|
|
case ConversionSpecifier::oArg:
|
|
|
|
case ConversionSpecifier::uArg:
|
|
|
|
case ConversionSpecifier::xArg:
|
|
|
|
case ConversionSpecifier::XArg:
|
|
|
|
case ConversionSpecifier::fArg:
|
|
|
|
case ConversionSpecifier::FArg:
|
|
|
|
case ConversionSpecifier::eArg:
|
|
|
|
case ConversionSpecifier::EArg:
|
|
|
|
case ConversionSpecifier::gArg:
|
|
|
|
case ConversionSpecifier::GArg:
|
|
|
|
case ConversionSpecifier::aArg:
|
|
|
|
case ConversionSpecifier::AArg:
|
|
|
|
case ConversionSpecifier::sArg:
|
|
|
|
case ConversionSpecifier::pArg:
|
|
|
|
case ConversionSpecifier::nArg:
|
|
|
|
case ConversionSpecifier::ObjCObjArg:
|
|
|
|
case ConversionSpecifier::ScanListArg:
|
|
|
|
case ConversionSpecifier::PercentArg:
|
|
|
|
return true;
|
|
|
|
case ConversionSpecifier::CArg:
|
|
|
|
case ConversionSpecifier::SArg:
|
|
|
|
return LangOpt.ObjC1 || LangOpt.ObjC2;
|
|
|
|
case ConversionSpecifier::InvalidSpecifier:
|
|
|
|
case ConversionSpecifier::PrintErrno:
|
2012-09-13 10:11:03 +08:00
|
|
|
case ConversionSpecifier::DArg:
|
|
|
|
case ConversionSpecifier::OArg:
|
|
|
|
case ConversionSpecifier::UArg:
|
2012-02-22 18:17:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid ConversionSpecifier Kind!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FormatSpecifier::hasStandardLengthConversionCombination() const {
|
|
|
|
if (LM.getKind() == LengthModifier::AsLongDouble) {
|
|
|
|
switch(CS.getKind()) {
|
|
|
|
case ConversionSpecifier::dArg:
|
|
|
|
case ConversionSpecifier::iArg:
|
|
|
|
case ConversionSpecifier::oArg:
|
|
|
|
case ConversionSpecifier::uArg:
|
|
|
|
case ConversionSpecifier::xArg:
|
|
|
|
case ConversionSpecifier::XArg:
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-28 03:17:46 +08:00
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<LengthModifier> FormatSpecifier::getCorrectedLengthModifier() const {
|
2012-09-08 12:00:12 +08:00
|
|
|
if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
|
|
|
|
if (LM.getKind() == LengthModifier::AsLongDouble ||
|
|
|
|
LM.getKind() == LengthModifier::AsQuad) {
|
2012-09-08 12:00:03 +08:00
|
|
|
LengthModifier FixedLM(LM);
|
|
|
|
FixedLM.setKind(LengthModifier::AsLongLong);
|
|
|
|
return FixedLM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-21 09:47:18 +08:00
|
|
|
return None;
|
2012-09-08 12:00:03 +08:00
|
|
|
}
|
|
|
|
|
2012-07-28 03:17:46 +08:00
|
|
|
bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
|
|
|
|
LengthModifier &LM) {
|
|
|
|
assert(isa<TypedefType>(QT) && "Expected a TypedefType");
|
|
|
|
const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const IdentifierInfo *Identifier = Typedef->getIdentifier();
|
|
|
|
if (Identifier->getName() == "size_t") {
|
|
|
|
LM.setKind(LengthModifier::AsSizeT);
|
|
|
|
return true;
|
|
|
|
} else if (Identifier->getName() == "ssize_t") {
|
|
|
|
// Not C99, but common in Unix.
|
|
|
|
LM.setKind(LengthModifier::AsSizeT);
|
|
|
|
return true;
|
|
|
|
} else if (Identifier->getName() == "intmax_t") {
|
|
|
|
LM.setKind(LengthModifier::AsIntMax);
|
|
|
|
return true;
|
|
|
|
} else if (Identifier->getName() == "uintmax_t") {
|
|
|
|
LM.setKind(LengthModifier::AsIntMax);
|
|
|
|
return true;
|
|
|
|
} else if (Identifier->getName() == "ptrdiff_t") {
|
|
|
|
LM.setKind(LengthModifier::AsPtrDiff);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType T = Typedef->getUnderlyingType();
|
|
|
|
if (!isa<TypedefType>(T))
|
|
|
|
break;
|
|
|
|
|
|
|
|
Typedef = cast<TypedefType>(T)->getDecl();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|