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"
|
2016-03-30 01:35:02 +08:00
|
|
|
#include "llvm/Support/ConvertUTF.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.
|
2015-10-20 21:23:58 +08:00
|
|
|
FormatStringHandler::~FormatStringHandler() {}
|
2010-07-16 10:11:22 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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;
|
2016-02-19 06:34:54 +08:00
|
|
|
if (I != E && *I == 'h') {
|
|
|
|
++I;
|
|
|
|
lmKind = LengthModifier::AsChar;
|
|
|
|
} else {
|
|
|
|
lmKind = LengthModifier::AsShort;
|
|
|
|
}
|
2010-07-16 10:11:22 +08:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
++I;
|
2016-02-19 06:34:54 +08:00
|
|
|
if (I != E && *I == 'l') {
|
|
|
|
++I;
|
|
|
|
lmKind = LengthModifier::AsLongLong;
|
|
|
|
} else {
|
|
|
|
lmKind = 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;
|
2013-08-22 05:54:46 +08:00
|
|
|
// printf: AsInt64, AsInt32, AsInt3264
|
|
|
|
// scanf: AsInt64
|
|
|
|
case 'I':
|
|
|
|
if (I + 1 != E && I + 2 != E) {
|
|
|
|
if (I[1] == '6' && I[2] == '4') {
|
|
|
|
I += 3;
|
|
|
|
lmKind = LengthModifier::AsInt64;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (IsScanf)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (I[1] == '3' && I[2] == '2') {
|
|
|
|
I += 3;
|
|
|
|
lmKind = LengthModifier::AsInt32;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++I;
|
|
|
|
lmKind = LengthModifier::AsInt3264;
|
|
|
|
break;
|
2014-09-05 05:39:46 +08:00
|
|
|
case 'w':
|
|
|
|
lmKind = LengthModifier::AsWide; ++I; break;
|
2010-07-16 10:11:22 +08:00
|
|
|
}
|
|
|
|
LengthModifier lm(lmPosition, lmKind);
|
|
|
|
FS.setLengthModifier(lm);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-30 01:35:02 +08:00
|
|
|
bool clang::analyze_format_string::ParseUTF8InvalidSpecifier(
|
|
|
|
const char *SpecifierBegin, const char *FmtStrEnd, unsigned &Len) {
|
|
|
|
if (SpecifierBegin + 1 >= FmtStrEnd)
|
|
|
|
return false;
|
|
|
|
|
2016-09-30 08:38:45 +08:00
|
|
|
const llvm::UTF8 *SB =
|
|
|
|
reinterpret_cast<const llvm::UTF8 *>(SpecifierBegin + 1);
|
|
|
|
const llvm::UTF8 *SE = reinterpret_cast<const llvm::UTF8 *>(FmtStrEnd);
|
2016-03-30 01:35:02 +08:00
|
|
|
const char FirstByte = *SB;
|
|
|
|
|
|
|
|
// If the invalid specifier is a multibyte UTF-8 string, return the
|
|
|
|
// total length accordingly so that the conversion specifier can be
|
|
|
|
// properly updated to reflect a complete UTF-8 specifier.
|
2016-09-30 08:38:45 +08:00
|
|
|
unsigned NumBytes = llvm::getNumBytesForUTF8(FirstByte);
|
2016-03-30 01:35:02 +08:00
|
|
|
if (NumBytes == 1)
|
|
|
|
return false;
|
|
|
|
if (SB + NumBytes > SE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Len = NumBytes + 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-16 10:11:22 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2012-08-07 16:11:26 +08:00
|
|
|
// Methods on ArgType.
|
2010-07-16 10:11:22 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-04 11:12:10 +08:00
|
|
|
clang::analyze_format_string::ArgType::MatchKind
|
|
|
|
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)
|
2015-03-04 11:12:10 +08:00
|
|
|
return NoMatch;
|
2012-08-07 16:59:46 +08:00
|
|
|
|
|
|
|
// We cannot write through a const qualified pointer.
|
|
|
|
if (PT->getPointeeType().isConstQualified())
|
2015-03-04 11:12:10 +08:00
|
|
|
return NoMatch;
|
2012-08-07 16:59:46 +08:00
|
|
|
|
|
|
|
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:
|
2015-03-04 11:12:10 +08:00
|
|
|
return Match;
|
|
|
|
|
2011-10-25 12:20:41 +08:00
|
|
|
case AnyCharTy: {
|
2016-08-21 00:51:33 +08:00
|
|
|
if (const EnumType *ETy = argTy->getAs<EnumType>()) {
|
|
|
|
// If the enum is incomplete we know nothing about the underlying type.
|
|
|
|
// Assume that it's 'int'.
|
|
|
|
if (!ETy->getDecl()->isComplete())
|
|
|
|
return NoMatch;
|
2012-06-05 06:48:57 +08:00
|
|
|
argTy = ETy->getDecl()->getIntegerType();
|
2016-08-21 00:51:33 +08:00
|
|
|
}
|
2012-06-05 06:48:57 +08:00
|
|
|
|
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:
|
2015-03-04 11:12:10 +08:00
|
|
|
return Match;
|
2011-10-25 12:20:41 +08:00
|
|
|
}
|
2015-03-04 11:12:10 +08:00
|
|
|
return NoMatch;
|
2011-10-25 12:20:41 +08:00
|
|
|
}
|
2015-03-04 11:12:10 +08:00
|
|
|
|
2010-07-16 10:11:22 +08:00
|
|
|
case SpecificTy: {
|
2016-08-21 00:51:33 +08:00
|
|
|
if (const EnumType *ETy = argTy->getAs<EnumType>()) {
|
|
|
|
// If the enum is incomplete we know nothing about the underlying type.
|
|
|
|
// Assume that it's 'int'.
|
|
|
|
if (!ETy->getDecl()->isComplete())
|
|
|
|
argTy = C.IntTy;
|
|
|
|
else
|
|
|
|
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)
|
2015-03-04 11:12:10 +08:00
|
|
|
return Match;
|
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:
|
2015-03-04 11:12:10 +08:00
|
|
|
case BuiltinType::UChar:
|
|
|
|
return T == C.UnsignedCharTy || T == C.SignedCharTy ? Match
|
|
|
|
: NoMatch;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::Short:
|
2015-03-04 11:12:10 +08:00
|
|
|
return T == C.UnsignedShortTy ? Match : NoMatch;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::UShort:
|
2015-03-04 11:12:10 +08:00
|
|
|
return T == C.ShortTy ? Match : NoMatch;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::Int:
|
2015-03-04 11:12:10 +08:00
|
|
|
return T == C.UnsignedIntTy ? Match : NoMatch;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::UInt:
|
2015-03-04 11:12:10 +08:00
|
|
|
return T == C.IntTy ? Match : NoMatch;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::Long:
|
2015-03-04 11:12:10 +08:00
|
|
|
return T == C.UnsignedLongTy ? Match : NoMatch;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::ULong:
|
2015-03-04 11:12:10 +08:00
|
|
|
return T == C.LongTy ? Match : NoMatch;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::LongLong:
|
2015-03-04 11:12:10 +08:00
|
|
|
return T == C.UnsignedLongLongTy ? Match : NoMatch;
|
2010-07-16 10:11:22 +08:00
|
|
|
case BuiltinType::ULongLong:
|
2015-03-04 11:12:10 +08:00
|
|
|
return T == C.LongLongTy ? Match : NoMatch;
|
2010-07-16 10:11:22 +08:00
|
|
|
}
|
2015-03-04 11:12:10 +08:00
|
|
|
return NoMatch;
|
2010-07-16 10:11:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case CStrTy: {
|
|
|
|
const PointerType *PT = argTy->getAs<PointerType>();
|
|
|
|
if (!PT)
|
2015-03-04 11:12:10 +08:00
|
|
|
return NoMatch;
|
2010-07-16 10:11:22 +08:00
|
|
|
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:
|
2015-03-04 11:12:10 +08:00
|
|
|
return Match;
|
2010-07-16 10:11:22 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-04 11:12:10 +08:00
|
|
|
return NoMatch;
|
2010-07-16 10:11:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case WCStrTy: {
|
|
|
|
const PointerType *PT = argTy->getAs<PointerType>();
|
|
|
|
if (!PT)
|
2015-03-04 11:12:10 +08:00
|
|
|
return NoMatch;
|
2010-07-16 10:11:22 +08:00
|
|
|
QualType pointeeTy =
|
|
|
|
C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
|
2015-03-04 11:12:10 +08:00
|
|
|
return pointeeTy == C.getWideCharType() ? Match : NoMatch;
|
2010-07-16 10:11:22 +08:00
|
|
|
}
|
2015-03-04 11:12:10 +08:00
|
|
|
|
2010-08-25 06:24:51 +08:00
|
|
|
case WIntTy: {
|
2015-03-04 11:12:10 +08:00
|
|
|
|
2010-08-25 06:24:51 +08:00
|
|
|
QualType PromoArg =
|
|
|
|
argTy->isPromotableIntegerType()
|
|
|
|
? C.getPromotedIntegerType(argTy) : argTy;
|
2015-03-04 11:12:10 +08:00
|
|
|
|
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();
|
2015-03-04 11:12:10 +08:00
|
|
|
|
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)
|
2015-03-04 11:12:10 +08:00
|
|
|
return Match;
|
2012-05-04 18:55:22 +08:00
|
|
|
|
2015-03-04 11:12:10 +08:00
|
|
|
return WInt == PromoArg ? Match : NoMatch;
|
2010-08-25 06:24:51 +08:00
|
|
|
}
|
2010-07-16 10:11:22 +08:00
|
|
|
|
|
|
|
case CPointerTy:
|
2015-03-04 11:12:10 +08:00
|
|
|
if (argTy->isVoidPointerType()) {
|
|
|
|
return Match;
|
|
|
|
} if (argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
|
|
|
|
argTy->isBlockPointerType() || argTy->isNullPtrType()) {
|
|
|
|
return NoMatchPedantic;
|
|
|
|
} else {
|
|
|
|
return NoMatch;
|
|
|
|
}
|
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>())
|
2015-03-04 11:12:10 +08:00
|
|
|
return Match;
|
|
|
|
|
2012-02-07 05:45:29 +08:00
|
|
|
// 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())
|
2015-03-04 11:12:10 +08:00
|
|
|
return Match;
|
2012-02-07 05:45:29 +08:00
|
|
|
}
|
2015-03-04 11:12:10 +08:00
|
|
|
return NoMatch;
|
2012-02-07 05:45:29 +08:00
|
|
|
}
|
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";
|
2013-08-22 05:54:46 +08:00
|
|
|
case AsInt32:
|
|
|
|
return "I32";
|
|
|
|
case AsInt3264:
|
|
|
|
return "I";
|
|
|
|
case AsInt64:
|
|
|
|
return "I64";
|
2010-07-16 10:11:22 +08:00
|
|
|
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";
|
2014-09-05 05:39:46 +08:00
|
|
|
case AsWide:
|
|
|
|
return "w";
|
2010-07-16 10:11:22 +08:00
|
|
|
case None:
|
|
|
|
return "";
|
|
|
|
}
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2010-07-16 10:11:22 +08:00
|
|
|
}
|
|
|
|
|
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 "[";
|
2014-05-20 12:30:07 +08:00
|
|
|
case InvalidSpecifier: return nullptr;
|
2011-12-09 19:11:07 +08:00
|
|
|
|
2013-08-22 05:54:46 +08:00
|
|
|
// POSIX unicode extensions.
|
2011-12-09 19:11:07 +08:00
|
|
|
case CArg: return "C";
|
|
|
|
case SArg: return "S";
|
|
|
|
|
|
|
|
// Objective-C specific specifiers.
|
|
|
|
case ObjCObjArg: return "@";
|
|
|
|
|
2015-02-20 06:32:33 +08:00
|
|
|
// FreeBSD kernel specific specifiers.
|
|
|
|
case FreeBSDbArg: return "b";
|
|
|
|
case FreeBSDDArg: return "D";
|
|
|
|
case FreeBSDrArg: return "r";
|
|
|
|
case FreeBSDyArg: return "y";
|
|
|
|
|
2011-12-09 19:11:07 +08:00
|
|
|
// GlibC specific specifiers.
|
|
|
|
case PrintErrno: return "m";
|
2014-09-07 11:03:51 +08:00
|
|
|
|
|
|
|
// MS specific specifiers.
|
|
|
|
case ZArg: return "Z";
|
2011-12-09 19:11:07 +08:00
|
|
|
}
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2011-12-09 19:11:07 +08:00
|
|
|
}
|
|
|
|
|
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::AsShort:
|
2014-09-05 05:39:52 +08:00
|
|
|
if (Target.getTriple().isOSMSVCRT()) {
|
|
|
|
switch (CS.getKind()) {
|
|
|
|
case ConversionSpecifier::cArg:
|
|
|
|
case ConversionSpecifier::CArg:
|
|
|
|
case ConversionSpecifier::sArg:
|
|
|
|
case ConversionSpecifier::SArg:
|
2014-09-07 11:03:51 +08:00
|
|
|
case ConversionSpecifier::ZArg:
|
2014-09-05 05:39:52 +08:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Fall through.
|
|
|
|
case LengthModifier::AsChar:
|
2010-07-21 04:04:42 +08:00
|
|
|
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;
|
2015-02-20 06:32:33 +08:00
|
|
|
case ConversionSpecifier::FreeBSDrArg:
|
|
|
|
case ConversionSpecifier::FreeBSDyArg:
|
2016-04-27 07:19:00 +08:00
|
|
|
return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
|
2010-07-21 04:04:42 +08:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-17 00:34:54 +08:00
|
|
|
// Handle 'l' flag
|
2014-08-30 05:05:44 +08:00
|
|
|
case LengthModifier::AsLong: // or AsWideChar
|
2010-07-21 04:04:42 +08:00
|
|
|
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:
|
2014-09-07 11:03:51 +08:00
|
|
|
case ConversionSpecifier::ZArg:
|
2010-07-21 04:04:42 +08:00
|
|
|
return true;
|
2015-02-20 06:32:33 +08:00
|
|
|
case ConversionSpecifier::FreeBSDrArg:
|
|
|
|
case ConversionSpecifier::FreeBSDyArg:
|
2016-04-27 07:19:00 +08:00
|
|
|
return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
|
2010-07-21 04:04:42 +08:00
|
|
|
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;
|
|
|
|
}
|
2013-08-22 05:54:46 +08:00
|
|
|
case LengthModifier::AsInt32:
|
|
|
|
case LengthModifier::AsInt3264:
|
|
|
|
case LengthModifier::AsInt64:
|
|
|
|
switch (CS.getKind()) {
|
|
|
|
case ConversionSpecifier::dArg:
|
|
|
|
case ConversionSpecifier::iArg:
|
|
|
|
case ConversionSpecifier::oArg:
|
|
|
|
case ConversionSpecifier::uArg:
|
|
|
|
case ConversionSpecifier::xArg:
|
|
|
|
case ConversionSpecifier::XArg:
|
|
|
|
return Target.getTriple().isOSMSVCRT();
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-05 05:39:46 +08:00
|
|
|
case LengthModifier::AsWide:
|
|
|
|
switch (CS.getKind()) {
|
|
|
|
case ConversionSpecifier::cArg:
|
|
|
|
case ConversionSpecifier::CArg:
|
|
|
|
case ConversionSpecifier::sArg:
|
2014-09-07 11:03:51 +08:00
|
|
|
case ConversionSpecifier::SArg:
|
|
|
|
case ConversionSpecifier::ZArg:
|
2014-09-05 05:39:46 +08:00
|
|
|
return Target.getTriple().isOSMSVCRT();
|
|
|
|
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:
|
2013-08-22 05:54:46 +08:00
|
|
|
case LengthModifier::AsInt32:
|
|
|
|
case LengthModifier::AsInt3264:
|
|
|
|
case LengthModifier::AsInt64:
|
2014-09-05 05:39:46 +08:00
|
|
|
case LengthModifier::AsWide:
|
2012-02-22 18:17:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid LengthModifier Kind!");
|
|
|
|
}
|
|
|
|
|
2015-05-18 10:41:17 +08:00
|
|
|
bool FormatSpecifier::hasStandardConversionSpecifier(
|
|
|
|
const LangOptions &LangOpt) const {
|
2012-02-22 18:17:01 +08:00
|
|
|
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:
|
2015-02-20 06:32:33 +08:00
|
|
|
case ConversionSpecifier::FreeBSDbArg:
|
|
|
|
case ConversionSpecifier::FreeBSDDArg:
|
|
|
|
case ConversionSpecifier::FreeBSDrArg:
|
|
|
|
case ConversionSpecifier::FreeBSDyArg:
|
2012-02-22 18:17:01 +08:00
|
|
|
case ConversionSpecifier::PrintErrno:
|
2012-09-13 10:11:03 +08:00
|
|
|
case ConversionSpecifier::DArg:
|
|
|
|
case ConversionSpecifier::OArg:
|
|
|
|
case ConversionSpecifier::UArg:
|
2014-09-07 11:03:51 +08:00
|
|
|
case ConversionSpecifier::ZArg:
|
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;
|
|
|
|
}
|