PrintfFormatString.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. //== PrintfFormatString.cpp - Analysis of printf format strings --*- C++ -*-==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Handling of format string in printf and friends. The structure of format
  11. // strings for fprintf() are described in C99 7.19.6.1.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Analysis/Analyses/FormatString.h"
  15. #include "FormatStringParsing.h"
  16. using clang::analyze_format_string::ArgTypeResult;
  17. using clang::analyze_format_string::FormatStringHandler;
  18. using clang::analyze_format_string::LengthModifier;
  19. using clang::analyze_format_string::OptionalAmount;
  20. using clang::analyze_format_string::ConversionSpecifier;
  21. using clang::analyze_printf::PrintfSpecifier;
  22. using namespace clang;
  23. typedef clang::analyze_format_string::SpecifierResult<PrintfSpecifier>
  24. PrintfSpecifierResult;
  25. //===----------------------------------------------------------------------===//
  26. // Methods for parsing format strings.
  27. //===----------------------------------------------------------------------===//
  28. using analyze_format_string::ParseNonPositionAmount;
  29. static bool ParsePrecision(FormatStringHandler &H, PrintfSpecifier &FS,
  30. const char *Start, const char *&Beg, const char *E,
  31. unsigned *argIndex) {
  32. if (argIndex) {
  33. FS.setPrecision(ParseNonPositionAmount(Beg, E, *argIndex));
  34. } else {
  35. const OptionalAmount Amt = ParsePositionAmount(H, Start, Beg, E,
  36. analyze_format_string::PrecisionPos);
  37. if (Amt.isInvalid())
  38. return true;
  39. FS.setPrecision(Amt);
  40. }
  41. return false;
  42. }
  43. static PrintfSpecifierResult ParsePrintfSpecifier(FormatStringHandler &H,
  44. const char *&Beg,
  45. const char *E,
  46. unsigned &argIndex,
  47. const LangOptions &LO) {
  48. using namespace clang::analyze_format_string;
  49. using namespace clang::analyze_printf;
  50. const char *I = Beg;
  51. const char *Start = 0;
  52. UpdateOnReturn <const char*> UpdateBeg(Beg, I);
  53. // Look for a '%' character that indicates the start of a format specifier.
  54. for ( ; I != E ; ++I) {
  55. char c = *I;
  56. if (c == '\0') {
  57. // Detect spurious null characters, which are likely errors.
  58. H.HandleNullChar(I);
  59. return true;
  60. }
  61. if (c == '%') {
  62. Start = I++; // Record the start of the format specifier.
  63. break;
  64. }
  65. }
  66. // No format specifier found?
  67. if (!Start)
  68. return false;
  69. if (I == E) {
  70. // No more characters left?
  71. H.HandleIncompleteSpecifier(Start, E - Start);
  72. return true;
  73. }
  74. PrintfSpecifier FS;
  75. if (ParseArgPosition(H, FS, Start, I, E))
  76. return true;
  77. if (I == E) {
  78. // No more characters left?
  79. H.HandleIncompleteSpecifier(Start, E - Start);
  80. return true;
  81. }
  82. // Look for flags (if any).
  83. bool hasMore = true;
  84. for ( ; I != E; ++I) {
  85. switch (*I) {
  86. default: hasMore = false; break;
  87. case '\'':
  88. // FIXME: POSIX specific. Always accept?
  89. FS.setHasThousandsGrouping(I);
  90. break;
  91. case '-': FS.setIsLeftJustified(I); break;
  92. case '+': FS.setHasPlusPrefix(I); break;
  93. case ' ': FS.setHasSpacePrefix(I); break;
  94. case '#': FS.setHasAlternativeForm(I); break;
  95. case '0': FS.setHasLeadingZeros(I); break;
  96. }
  97. if (!hasMore)
  98. break;
  99. }
  100. if (I == E) {
  101. // No more characters left?
  102. H.HandleIncompleteSpecifier(Start, E - Start);
  103. return true;
  104. }
  105. // Look for the field width (if any).
  106. if (ParseFieldWidth(H, FS, Start, I, E,
  107. FS.usesPositionalArg() ? 0 : &argIndex))
  108. return true;
  109. if (I == E) {
  110. // No more characters left?
  111. H.HandleIncompleteSpecifier(Start, E - Start);
  112. return true;
  113. }
  114. // Look for the precision (if any).
  115. if (*I == '.') {
  116. ++I;
  117. if (I == E) {
  118. H.HandleIncompleteSpecifier(Start, E - Start);
  119. return true;
  120. }
  121. if (ParsePrecision(H, FS, Start, I, E,
  122. FS.usesPositionalArg() ? 0 : &argIndex))
  123. return true;
  124. if (I == E) {
  125. // No more characters left?
  126. H.HandleIncompleteSpecifier(Start, E - Start);
  127. return true;
  128. }
  129. }
  130. // Look for the length modifier.
  131. if (ParseLengthModifier(FS, I, E, LO) && I == E) {
  132. // No more characters left?
  133. H.HandleIncompleteSpecifier(Start, E - Start);
  134. return true;
  135. }
  136. if (*I == '\0') {
  137. // Detect spurious null characters, which are likely errors.
  138. H.HandleNullChar(I);
  139. return true;
  140. }
  141. // Finally, look for the conversion specifier.
  142. const char *conversionPosition = I++;
  143. ConversionSpecifier::Kind k = ConversionSpecifier::InvalidSpecifier;
  144. switch (*conversionPosition) {
  145. default:
  146. break;
  147. // C99: 7.19.6.1 (section 8).
  148. case '%': k = ConversionSpecifier::PercentArg; break;
  149. case 'A': k = ConversionSpecifier::AArg; break;
  150. case 'E': k = ConversionSpecifier::EArg; break;
  151. case 'F': k = ConversionSpecifier::FArg; break;
  152. case 'G': k = ConversionSpecifier::GArg; break;
  153. case 'X': k = ConversionSpecifier::XArg; break;
  154. case 'a': k = ConversionSpecifier::aArg; break;
  155. case 'c': k = ConversionSpecifier::cArg; break;
  156. case 'd': k = ConversionSpecifier::dArg; break;
  157. case 'e': k = ConversionSpecifier::eArg; break;
  158. case 'f': k = ConversionSpecifier::fArg; break;
  159. case 'g': k = ConversionSpecifier::gArg; break;
  160. case 'i': k = ConversionSpecifier::iArg; break;
  161. case 'n': k = ConversionSpecifier::nArg; break;
  162. case 'o': k = ConversionSpecifier::oArg; break;
  163. case 'p': k = ConversionSpecifier::pArg; break;
  164. case 's': k = ConversionSpecifier::sArg; break;
  165. case 'u': k = ConversionSpecifier::uArg; break;
  166. case 'x': k = ConversionSpecifier::xArg; break;
  167. // POSIX specific.
  168. case 'C': k = ConversionSpecifier::CArg; break;
  169. case 'S': k = ConversionSpecifier::SArg; break;
  170. // Objective-C.
  171. case '@': k = ConversionSpecifier::ObjCObjArg; break;
  172. // Glibc specific.
  173. case 'm': k = ConversionSpecifier::PrintErrno; break;
  174. }
  175. PrintfConversionSpecifier CS(conversionPosition, k);
  176. FS.setConversionSpecifier(CS);
  177. if (CS.consumesDataArgument() && !FS.usesPositionalArg())
  178. FS.setArgIndex(argIndex++);
  179. if (k == ConversionSpecifier::InvalidSpecifier) {
  180. // Assume the conversion takes one argument.
  181. return !H.HandleInvalidPrintfConversionSpecifier(FS, Start, I - Start);
  182. }
  183. return PrintfSpecifierResult(Start, FS);
  184. }
  185. bool clang::analyze_format_string::ParsePrintfString(FormatStringHandler &H,
  186. const char *I,
  187. const char *E,
  188. const LangOptions &LO) {
  189. unsigned argIndex = 0;
  190. // Keep looking for a format specifier until we have exhausted the string.
  191. while (I != E) {
  192. const PrintfSpecifierResult &FSR = ParsePrintfSpecifier(H, I, E, argIndex,
  193. LO);
  194. // Did a fail-stop error of any kind occur when parsing the specifier?
  195. // If so, don't do any more processing.
  196. if (FSR.shouldStop())
  197. return true;;
  198. // Did we exhaust the string or encounter an error that
  199. // we can recover from?
  200. if (!FSR.hasValue())
  201. continue;
  202. // We have a format specifier. Pass it to the callback.
  203. if (!H.HandlePrintfSpecifier(FSR.getValue(), FSR.getStart(),
  204. I - FSR.getStart()))
  205. return true;
  206. }
  207. assert(I == E && "Format string not exhausted");
  208. return false;
  209. }
  210. //===----------------------------------------------------------------------===//
  211. // Methods on PrintfSpecifier.
  212. //===----------------------------------------------------------------------===//
  213. ArgTypeResult PrintfSpecifier::getArgType(ASTContext &Ctx,
  214. bool IsObjCLiteral) const {
  215. const PrintfConversionSpecifier &CS = getConversionSpecifier();
  216. if (!CS.consumesDataArgument())
  217. return ArgTypeResult::Invalid();
  218. if (CS.getKind() == ConversionSpecifier::cArg)
  219. switch (LM.getKind()) {
  220. case LengthModifier::None: return Ctx.IntTy;
  221. case LengthModifier::AsLong:
  222. return ArgTypeResult(ArgTypeResult::WIntTy, "wint_t");
  223. default:
  224. return ArgTypeResult::Invalid();
  225. }
  226. if (CS.isIntArg())
  227. switch (LM.getKind()) {
  228. case LengthModifier::AsLongDouble:
  229. // GNU extension.
  230. return Ctx.LongLongTy;
  231. case LengthModifier::None: return Ctx.IntTy;
  232. case LengthModifier::AsChar: return ArgTypeResult::AnyCharTy;
  233. case LengthModifier::AsShort: return Ctx.ShortTy;
  234. case LengthModifier::AsLong: return Ctx.LongTy;
  235. case LengthModifier::AsLongLong:
  236. case LengthModifier::AsQuad:
  237. return Ctx.LongLongTy;
  238. case LengthModifier::AsIntMax:
  239. return ArgTypeResult(Ctx.getIntMaxType(), "intmax_t");
  240. case LengthModifier::AsSizeT:
  241. // FIXME: How to get the corresponding signed version of size_t?
  242. return ArgTypeResult();
  243. case LengthModifier::AsPtrDiff:
  244. return ArgTypeResult(Ctx.getPointerDiffType(), "ptrdiff_t");
  245. case LengthModifier::AsAllocate:
  246. case LengthModifier::AsMAllocate:
  247. return ArgTypeResult::Invalid();
  248. }
  249. if (CS.isUIntArg())
  250. switch (LM.getKind()) {
  251. case LengthModifier::AsLongDouble:
  252. // GNU extension.
  253. return Ctx.UnsignedLongLongTy;
  254. case LengthModifier::None: return Ctx.UnsignedIntTy;
  255. case LengthModifier::AsChar: return Ctx.UnsignedCharTy;
  256. case LengthModifier::AsShort: return Ctx.UnsignedShortTy;
  257. case LengthModifier::AsLong: return Ctx.UnsignedLongTy;
  258. case LengthModifier::AsLongLong:
  259. case LengthModifier::AsQuad:
  260. return Ctx.UnsignedLongLongTy;
  261. case LengthModifier::AsIntMax:
  262. return ArgTypeResult(Ctx.getUIntMaxType(), "uintmax_t");
  263. case LengthModifier::AsSizeT:
  264. return ArgTypeResult(Ctx.getSizeType(), "size_t");
  265. case LengthModifier::AsPtrDiff:
  266. // FIXME: How to get the corresponding unsigned
  267. // version of ptrdiff_t?
  268. return ArgTypeResult();
  269. case LengthModifier::AsAllocate:
  270. case LengthModifier::AsMAllocate:
  271. return ArgTypeResult::Invalid();
  272. }
  273. if (CS.isDoubleArg()) {
  274. if (LM.getKind() == LengthModifier::AsLongDouble)
  275. return Ctx.LongDoubleTy;
  276. return Ctx.DoubleTy;
  277. }
  278. switch (CS.getKind()) {
  279. case ConversionSpecifier::sArg:
  280. if (LM.getKind() == LengthModifier::AsWideChar) {
  281. if (IsObjCLiteral)
  282. return Ctx.getPointerType(Ctx.UnsignedShortTy.withConst());
  283. return ArgTypeResult(ArgTypeResult::WCStrTy, "wchar_t *");
  284. }
  285. return ArgTypeResult::CStrTy;
  286. case ConversionSpecifier::SArg:
  287. if (IsObjCLiteral)
  288. return Ctx.getPointerType(Ctx.UnsignedShortTy.withConst());
  289. return ArgTypeResult(ArgTypeResult::WCStrTy, "wchar_t *");
  290. case ConversionSpecifier::CArg:
  291. if (IsObjCLiteral)
  292. return Ctx.UnsignedShortTy;
  293. return ArgTypeResult(Ctx.WCharTy, "wchar_t");
  294. case ConversionSpecifier::pArg:
  295. return ArgTypeResult::CPointerTy;
  296. case ConversionSpecifier::nArg:
  297. return Ctx.getPointerType(Ctx.IntTy);
  298. case ConversionSpecifier::ObjCObjArg:
  299. return ArgTypeResult::ObjCPointerTy;
  300. default:
  301. break;
  302. }
  303. // FIXME: Handle other cases.
  304. return ArgTypeResult();
  305. }
  306. bool PrintfSpecifier::fixType(QualType QT, const LangOptions &LangOpt,
  307. ASTContext &Ctx, bool IsObjCLiteral) {
  308. // %n is different from other conversion specifiers; don't try to fix it.
  309. if (CS.getKind() == ConversionSpecifier::nArg)
  310. return false;
  311. // Handle Objective-C objects first. Note that while the '%@' specifier will
  312. // not warn for structure pointer or void pointer arguments (because that's
  313. // how CoreFoundation objects are implemented), we only show a fixit for '%@'
  314. // if we know it's an object (block, id, class, or __attribute__((NSObject))).
  315. if (QT->isObjCRetainableType()) {
  316. if (!IsObjCLiteral)
  317. return false;
  318. CS.setKind(ConversionSpecifier::ObjCObjArg);
  319. // Disable irrelevant flags
  320. HasThousandsGrouping = false;
  321. HasPlusPrefix = false;
  322. HasSpacePrefix = false;
  323. HasAlternativeForm = false;
  324. HasLeadingZeroes = false;
  325. Precision.setHowSpecified(OptionalAmount::NotSpecified);
  326. LM.setKind(LengthModifier::None);
  327. return true;
  328. }
  329. // Handle strings next (char *, wchar_t *)
  330. if (QT->isPointerType() && (QT->getPointeeType()->isAnyCharacterType())) {
  331. CS.setKind(ConversionSpecifier::sArg);
  332. // Disable irrelevant flags
  333. HasAlternativeForm = 0;
  334. HasLeadingZeroes = 0;
  335. // Set the long length modifier for wide characters
  336. if (QT->getPointeeType()->isWideCharType())
  337. LM.setKind(LengthModifier::AsWideChar);
  338. else
  339. LM.setKind(LengthModifier::None);
  340. return true;
  341. }
  342. // If it's an enum, get its underlying type.
  343. if (const EnumType *ETy = QT->getAs<EnumType>())
  344. QT = ETy->getDecl()->getIntegerType();
  345. // We can only work with builtin types.
  346. const BuiltinType *BT = QT->getAs<BuiltinType>();
  347. if (!BT)
  348. return false;
  349. // Set length modifier
  350. switch (BT->getKind()) {
  351. case BuiltinType::Bool:
  352. case BuiltinType::WChar_U:
  353. case BuiltinType::WChar_S:
  354. case BuiltinType::Char16:
  355. case BuiltinType::Char32:
  356. case BuiltinType::UInt128:
  357. case BuiltinType::Int128:
  358. case BuiltinType::Half:
  359. // Various types which are non-trivial to correct.
  360. return false;
  361. #define SIGNED_TYPE(Id, SingletonId)
  362. #define UNSIGNED_TYPE(Id, SingletonId)
  363. #define FLOATING_TYPE(Id, SingletonId)
  364. #define BUILTIN_TYPE(Id, SingletonId) \
  365. case BuiltinType::Id:
  366. #include "clang/AST/BuiltinTypes.def"
  367. // Misc other stuff which doesn't make sense here.
  368. return false;
  369. case BuiltinType::UInt:
  370. case BuiltinType::Int:
  371. case BuiltinType::Float:
  372. case BuiltinType::Double:
  373. LM.setKind(LengthModifier::None);
  374. break;
  375. case BuiltinType::Char_U:
  376. case BuiltinType::UChar:
  377. case BuiltinType::Char_S:
  378. case BuiltinType::SChar:
  379. LM.setKind(LengthModifier::AsChar);
  380. break;
  381. case BuiltinType::Short:
  382. case BuiltinType::UShort:
  383. LM.setKind(LengthModifier::AsShort);
  384. break;
  385. case BuiltinType::Long:
  386. case BuiltinType::ULong:
  387. LM.setKind(LengthModifier::AsLong);
  388. break;
  389. case BuiltinType::LongLong:
  390. case BuiltinType::ULongLong:
  391. LM.setKind(LengthModifier::AsLongLong);
  392. break;
  393. case BuiltinType::LongDouble:
  394. LM.setKind(LengthModifier::AsLongDouble);
  395. break;
  396. }
  397. // Handle size_t, ptrdiff_t, etc. that have dedicated length modifiers in C99.
  398. if (isa<TypedefType>(QT) && (LangOpt.C99 || LangOpt.CPlusPlus0x))
  399. namedTypeToLengthModifier(QT, LM);
  400. // If fixing the length modifier was enough, we are done.
  401. const analyze_printf::ArgTypeResult &ATR = getArgType(Ctx, IsObjCLiteral);
  402. if (hasValidLengthModifier() && ATR.isValid() && ATR.matchesType(Ctx, QT))
  403. return true;
  404. // Set conversion specifier and disable any flags which do not apply to it.
  405. // Let typedefs to char fall through to int, as %c is silly for uint8_t.
  406. if (isa<TypedefType>(QT) && QT->isAnyCharacterType()) {
  407. CS.setKind(ConversionSpecifier::cArg);
  408. LM.setKind(LengthModifier::None);
  409. Precision.setHowSpecified(OptionalAmount::NotSpecified);
  410. HasAlternativeForm = 0;
  411. HasLeadingZeroes = 0;
  412. HasPlusPrefix = 0;
  413. }
  414. // Test for Floating type first as LongDouble can pass isUnsignedIntegerType
  415. else if (QT->isRealFloatingType()) {
  416. CS.setKind(ConversionSpecifier::fArg);
  417. }
  418. else if (QT->isSignedIntegerType()) {
  419. CS.setKind(ConversionSpecifier::dArg);
  420. HasAlternativeForm = 0;
  421. }
  422. else if (QT->isUnsignedIntegerType()) {
  423. CS.setKind(ConversionSpecifier::uArg);
  424. HasAlternativeForm = 0;
  425. HasPlusPrefix = 0;
  426. } else {
  427. llvm_unreachable("Unexpected type");
  428. }
  429. return true;
  430. }
  431. void PrintfSpecifier::toString(raw_ostream &os) const {
  432. // Whilst some features have no defined order, we are using the order
  433. // appearing in the C99 standard (ISO/IEC 9899:1999 (E) 7.19.6.1)
  434. os << "%";
  435. // Positional args
  436. if (usesPositionalArg()) {
  437. os << getPositionalArgIndex() << "$";
  438. }
  439. // Conversion flags
  440. if (IsLeftJustified) os << "-";
  441. if (HasPlusPrefix) os << "+";
  442. if (HasSpacePrefix) os << " ";
  443. if (HasAlternativeForm) os << "#";
  444. if (HasLeadingZeroes) os << "0";
  445. // Minimum field width
  446. FieldWidth.toString(os);
  447. // Precision
  448. Precision.toString(os);
  449. // Length modifier
  450. os << LM.toString();
  451. // Conversion specifier
  452. os << CS.toString();
  453. }
  454. bool PrintfSpecifier::hasValidPlusPrefix() const {
  455. if (!HasPlusPrefix)
  456. return true;
  457. // The plus prefix only makes sense for signed conversions
  458. switch (CS.getKind()) {
  459. case ConversionSpecifier::dArg:
  460. case ConversionSpecifier::iArg:
  461. case ConversionSpecifier::fArg:
  462. case ConversionSpecifier::FArg:
  463. case ConversionSpecifier::eArg:
  464. case ConversionSpecifier::EArg:
  465. case ConversionSpecifier::gArg:
  466. case ConversionSpecifier::GArg:
  467. case ConversionSpecifier::aArg:
  468. case ConversionSpecifier::AArg:
  469. return true;
  470. default:
  471. return false;
  472. }
  473. }
  474. bool PrintfSpecifier::hasValidAlternativeForm() const {
  475. if (!HasAlternativeForm)
  476. return true;
  477. // Alternate form flag only valid with the oxXaAeEfFgG conversions
  478. switch (CS.getKind()) {
  479. case ConversionSpecifier::oArg:
  480. case ConversionSpecifier::xArg:
  481. case ConversionSpecifier::XArg:
  482. case ConversionSpecifier::aArg:
  483. case ConversionSpecifier::AArg:
  484. case ConversionSpecifier::eArg:
  485. case ConversionSpecifier::EArg:
  486. case ConversionSpecifier::fArg:
  487. case ConversionSpecifier::FArg:
  488. case ConversionSpecifier::gArg:
  489. case ConversionSpecifier::GArg:
  490. return true;
  491. default:
  492. return false;
  493. }
  494. }
  495. bool PrintfSpecifier::hasValidLeadingZeros() const {
  496. if (!HasLeadingZeroes)
  497. return true;
  498. // Leading zeroes flag only valid with the diouxXaAeEfFgG conversions
  499. switch (CS.getKind()) {
  500. case ConversionSpecifier::dArg:
  501. case ConversionSpecifier::iArg:
  502. case ConversionSpecifier::oArg:
  503. case ConversionSpecifier::uArg:
  504. case ConversionSpecifier::xArg:
  505. case ConversionSpecifier::XArg:
  506. case ConversionSpecifier::aArg:
  507. case ConversionSpecifier::AArg:
  508. case ConversionSpecifier::eArg:
  509. case ConversionSpecifier::EArg:
  510. case ConversionSpecifier::fArg:
  511. case ConversionSpecifier::FArg:
  512. case ConversionSpecifier::gArg:
  513. case ConversionSpecifier::GArg:
  514. return true;
  515. default:
  516. return false;
  517. }
  518. }
  519. bool PrintfSpecifier::hasValidSpacePrefix() const {
  520. if (!HasSpacePrefix)
  521. return true;
  522. // The space prefix only makes sense for signed conversions
  523. switch (CS.getKind()) {
  524. case ConversionSpecifier::dArg:
  525. case ConversionSpecifier::iArg:
  526. case ConversionSpecifier::fArg:
  527. case ConversionSpecifier::FArg:
  528. case ConversionSpecifier::eArg:
  529. case ConversionSpecifier::EArg:
  530. case ConversionSpecifier::gArg:
  531. case ConversionSpecifier::GArg:
  532. case ConversionSpecifier::aArg:
  533. case ConversionSpecifier::AArg:
  534. return true;
  535. default:
  536. return false;
  537. }
  538. }
  539. bool PrintfSpecifier::hasValidLeftJustified() const {
  540. if (!IsLeftJustified)
  541. return true;
  542. // The left justified flag is valid for all conversions except n
  543. switch (CS.getKind()) {
  544. case ConversionSpecifier::nArg:
  545. return false;
  546. default:
  547. return true;
  548. }
  549. }
  550. bool PrintfSpecifier::hasValidThousandsGroupingPrefix() const {
  551. if (!HasThousandsGrouping)
  552. return true;
  553. switch (CS.getKind()) {
  554. case ConversionSpecifier::dArg:
  555. case ConversionSpecifier::iArg:
  556. case ConversionSpecifier::uArg:
  557. case ConversionSpecifier::fArg:
  558. case ConversionSpecifier::FArg:
  559. case ConversionSpecifier::gArg:
  560. case ConversionSpecifier::GArg:
  561. return true;
  562. default:
  563. return false;
  564. }
  565. }
  566. bool PrintfSpecifier::hasValidPrecision() const {
  567. if (Precision.getHowSpecified() == OptionalAmount::NotSpecified)
  568. return true;
  569. // Precision is only valid with the diouxXaAeEfFgGs conversions
  570. switch (CS.getKind()) {
  571. case ConversionSpecifier::dArg:
  572. case ConversionSpecifier::iArg:
  573. case ConversionSpecifier::oArg:
  574. case ConversionSpecifier::uArg:
  575. case ConversionSpecifier::xArg:
  576. case ConversionSpecifier::XArg:
  577. case ConversionSpecifier::aArg:
  578. case ConversionSpecifier::AArg:
  579. case ConversionSpecifier::eArg:
  580. case ConversionSpecifier::EArg:
  581. case ConversionSpecifier::fArg:
  582. case ConversionSpecifier::FArg:
  583. case ConversionSpecifier::gArg:
  584. case ConversionSpecifier::GArg:
  585. case ConversionSpecifier::sArg:
  586. return true;
  587. default:
  588. return false;
  589. }
  590. }
  591. bool PrintfSpecifier::hasValidFieldWidth() const {
  592. if (FieldWidth.getHowSpecified() == OptionalAmount::NotSpecified)
  593. return true;
  594. // The field width is valid for all conversions except n
  595. switch (CS.getKind()) {
  596. case ConversionSpecifier::nArg:
  597. return false;
  598. default:
  599. return true;
  600. }
  601. }