return ArgType();
}
-bool ScanfSpecifier::fixType(QualType QT, const LangOptions &LangOpt,
+bool ScanfSpecifier::fixType(QualType QT, QualType RawQT,
+ const LangOptions &LangOpt,
ASTContext &Ctx) {
- if (!QT->isPointerType())
- return false;
// %n is different from other conversion specifiers; don't try to fix it.
if (CS.getKind() == ConversionSpecifier::nArg)
return false;
+ if (!QT->isPointerType())
+ return false;
+
QualType PT = QT->getPointeeType();
// If it's an enum, get its underlying type.
- if (const EnumType *ETy = QT->getAs<EnumType>())
- QT = ETy->getDecl()->getIntegerType();
-
+ if (const EnumType *ETy = PT->getAs<EnumType>())
+ PT = ETy->getDecl()->getIntegerType();
+
const BuiltinType *BT = PT->getAs<BuiltinType>();
if (!BT)
return false;
LM.setKind(LengthModifier::AsWideChar);
else
LM.setKind(LengthModifier::None);
+
+ // If we know the target array length, we can use it as a field width.
+ if (const ConstantArrayType *CAT = Ctx.getAsConstantArrayType(RawQT)) {
+ if (CAT->getSizeModifier() == ArrayType::Normal)
+ FieldWidth = OptionalAmount(OptionalAmount::Constant,
+ CAT->getSize().getZExtValue() - 1,
+ "", 0, false);
+
+ }
return true;
}
const analyze_format_string::ArgType &AT = FS.getArgType(S.Context);
if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) {
ScanfSpecifier fixedFS = FS;
- bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(),
- S.Context);
+ bool success = fixedFS.fixType(Ex->getType(),
+ Ex->IgnoreImpCasts()->getType(),
+ S.getLangOpts(), S.Context);
if (success) {
// Get the fix string from the fixed format specifier.
typedef __INTMAX_TYPE__ intmax_t;
typedef __UINTMAX_TYPE__ uintmax_t;
typedef __PTRDIFF_TYPE__ ptrdiff_t;
+typedef __WCHAR_TYPE__ wchar_t;
void test() {
// Basic types
int scanf(char const *, ...);
-void test2() {
+void test2(int intSAParm[static 2]) {
char str[100];
+ char *vstr = "abc";
+ wchar_t wstr[100];
short shortVar;
unsigned short uShortVar;
int intVar;
+ int intAVar[2];
unsigned uIntVar;
float floatVar;
double doubleVar;
intmax_t intmaxVar;
uintmax_t uIntmaxVar;
ptrdiff_t ptrdiffVar;
+ enum {A, B, C} enumVar;
+ // Some string types.
scanf("%lf", str);
+ scanf("%lf", vstr);
+ scanf("%ls", str);
+ scanf("%f", wstr); // FIXME: wchar_t should resolve to %ls, not %d.
+ scanf("%s", wstr); // FIXME: wchar_t should resolve to %ls, not %d.
+ scanf("%ls", str);
+
+ // Some integer types.
scanf("%f", &shortVar);
scanf("%f", &uShortVar);
scanf("%p", &intVar);
+ scanf("%f", intAVar);
+ scanf("%f", intSAParm);
scanf("%Lf", &uIntVar);
scanf("%ld", &floatVar);
scanf("%f", &doubleVar);
scanf("%f", &uLongVar);
scanf("%f", &longLongVar);
scanf("%f", &uLongLongVar);
+ scanf("%d", &enumVar); // FIXME: We ought to fix specifiers for enums.
// Some named ints.
scanf("%f", &sizeVar);
// CHECK: printf("%La", (long double) 42);
// CHECK: printf("%LA", (long double) 42);
-// CHECK: scanf("%s", str);
+// CHECK: scanf("%99s", str);
+// CHECK: scanf("%s", vstr);
+// CHECK: scanf("%99s", str);
+// CHECK: scanf("%d", wstr);
+// CHECK: scanf("%d", wstr);
+// CHECK: scanf("%99s", str);
// CHECK: scanf("%hd", &shortVar);
// CHECK: scanf("%hu", &uShortVar);
// CHECK: scanf("%d", &intVar);
+// CHECK: scanf("%d", intAVar);
+// CHECK: scanf("%d", intSAParm);
// CHECK: scanf("%u", &uIntVar);
// CHECK: scanf("%f", &floatVar);
// CHECK: scanf("%lf", &doubleVar);
// CHECK: scanf("%lu", &uLongVar);
// CHECK: scanf("%lld", &longLongVar);
// CHECK: scanf("%llu", &uLongLongVar);
+// CHECK: scanf("%d", &enumVar);
// CHECK: scanf("%zu", &sizeVar);
// CHECK: scanf("%jd", &intmaxVar);
// CHECK: scanf("%ju", &uIntmaxVar);