FormatString.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. // FormatString.cpp - Common stuff for handling printf/scanf formats -*- 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. // Shared details for processing format strings of printf and scanf
  11. // (and friends).
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "FormatStringParsing.h"
  15. #include "clang/Basic/LangOptions.h"
  16. #include "clang/Basic/TargetInfo.h"
  17. using clang::analyze_format_string::ArgType;
  18. using clang::analyze_format_string::FormatStringHandler;
  19. using clang::analyze_format_string::FormatSpecifier;
  20. using clang::analyze_format_string::LengthModifier;
  21. using clang::analyze_format_string::OptionalAmount;
  22. using clang::analyze_format_string::PositionContext;
  23. using clang::analyze_format_string::ConversionSpecifier;
  24. using namespace clang;
  25. // Key function to FormatStringHandler.
  26. FormatStringHandler::~FormatStringHandler() {}
  27. //===----------------------------------------------------------------------===//
  28. // Functions for parsing format strings components in both printf and
  29. // scanf format strings.
  30. //===----------------------------------------------------------------------===//
  31. OptionalAmount
  32. clang::analyze_format_string::ParseAmount(const char *&Beg, const char *E) {
  33. const char *I = Beg;
  34. UpdateOnReturn <const char*> UpdateBeg(Beg, I);
  35. unsigned accumulator = 0;
  36. bool hasDigits = false;
  37. for ( ; I != E; ++I) {
  38. char c = *I;
  39. if (c >= '0' && c <= '9') {
  40. hasDigits = true;
  41. accumulator = (accumulator * 10) + (c - '0');
  42. continue;
  43. }
  44. if (hasDigits)
  45. return OptionalAmount(OptionalAmount::Constant, accumulator, Beg, I - Beg,
  46. false);
  47. break;
  48. }
  49. return OptionalAmount();
  50. }
  51. OptionalAmount
  52. clang::analyze_format_string::ParseNonPositionAmount(const char *&Beg,
  53. const char *E,
  54. unsigned &argIndex) {
  55. if (*Beg == '*') {
  56. ++Beg;
  57. return OptionalAmount(OptionalAmount::Arg, argIndex++, Beg, 0, false);
  58. }
  59. return ParseAmount(Beg, E);
  60. }
  61. OptionalAmount
  62. clang::analyze_format_string::ParsePositionAmount(FormatStringHandler &H,
  63. const char *Start,
  64. const char *&Beg,
  65. const char *E,
  66. PositionContext p) {
  67. if (*Beg == '*') {
  68. const char *I = Beg + 1;
  69. const OptionalAmount &Amt = ParseAmount(I, E);
  70. if (Amt.getHowSpecified() == OptionalAmount::NotSpecified) {
  71. H.HandleInvalidPosition(Beg, I - Beg, p);
  72. return OptionalAmount(false);
  73. }
  74. if (I == E) {
  75. // No more characters left?
  76. H.HandleIncompleteSpecifier(Start, E - Start);
  77. return OptionalAmount(false);
  78. }
  79. assert(Amt.getHowSpecified() == OptionalAmount::Constant);
  80. if (*I == '$') {
  81. // Handle positional arguments
  82. // Special case: '*0$', since this is an easy mistake.
  83. if (Amt.getConstantAmount() == 0) {
  84. H.HandleZeroPosition(Beg, I - Beg + 1);
  85. return OptionalAmount(false);
  86. }
  87. const char *Tmp = Beg;
  88. Beg = ++I;
  89. return OptionalAmount(OptionalAmount::Arg, Amt.getConstantAmount() - 1,
  90. Tmp, 0, true);
  91. }
  92. H.HandleInvalidPosition(Beg, I - Beg, p);
  93. return OptionalAmount(false);
  94. }
  95. return ParseAmount(Beg, E);
  96. }
  97. bool
  98. clang::analyze_format_string::ParseFieldWidth(FormatStringHandler &H,
  99. FormatSpecifier &CS,
  100. const char *Start,
  101. const char *&Beg, const char *E,
  102. unsigned *argIndex) {
  103. // FIXME: Support negative field widths.
  104. if (argIndex) {
  105. CS.setFieldWidth(ParseNonPositionAmount(Beg, E, *argIndex));
  106. }
  107. else {
  108. const OptionalAmount Amt =
  109. ParsePositionAmount(H, Start, Beg, E,
  110. analyze_format_string::FieldWidthPos);
  111. if (Amt.isInvalid())
  112. return true;
  113. CS.setFieldWidth(Amt);
  114. }
  115. return false;
  116. }
  117. bool
  118. clang::analyze_format_string::ParseArgPosition(FormatStringHandler &H,
  119. FormatSpecifier &FS,
  120. const char *Start,
  121. const char *&Beg,
  122. const char *E) {
  123. const char *I = Beg;
  124. const OptionalAmount &Amt = ParseAmount(I, E);
  125. if (I == E) {
  126. // No more characters left?
  127. H.HandleIncompleteSpecifier(Start, E - Start);
  128. return true;
  129. }
  130. if (Amt.getHowSpecified() == OptionalAmount::Constant && *(I++) == '$') {
  131. // Warn that positional arguments are non-standard.
  132. H.HandlePosition(Start, I - Start);
  133. // Special case: '%0$', since this is an easy mistake.
  134. if (Amt.getConstantAmount() == 0) {
  135. H.HandleZeroPosition(Start, I - Start);
  136. return true;
  137. }
  138. FS.setArgIndex(Amt.getConstantAmount() - 1);
  139. FS.setUsesPositionalArg();
  140. // Update the caller's pointer if we decided to consume
  141. // these characters.
  142. Beg = I;
  143. return false;
  144. }
  145. return false;
  146. }
  147. bool
  148. clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS,
  149. const char *&I,
  150. const char *E,
  151. const LangOptions &LO,
  152. bool IsScanf) {
  153. LengthModifier::Kind lmKind = LengthModifier::None;
  154. const char *lmPosition = I;
  155. switch (*I) {
  156. default:
  157. return false;
  158. case 'h':
  159. ++I;
  160. lmKind = (I != E && *I == 'h') ? (++I, LengthModifier::AsChar)
  161. : LengthModifier::AsShort;
  162. break;
  163. case 'l':
  164. ++I;
  165. lmKind = (I != E && *I == 'l') ? (++I, LengthModifier::AsLongLong)
  166. : LengthModifier::AsLong;
  167. break;
  168. case 'j': lmKind = LengthModifier::AsIntMax; ++I; break;
  169. case 'z': lmKind = LengthModifier::AsSizeT; ++I; break;
  170. case 't': lmKind = LengthModifier::AsPtrDiff; ++I; break;
  171. case 'L': lmKind = LengthModifier::AsLongDouble; ++I; break;
  172. case 'q': lmKind = LengthModifier::AsQuad; ++I; break;
  173. case 'a':
  174. if (IsScanf && !LO.C99 && !LO.CPlusPlus0x) {
  175. // For scanf in C90, look at the next character to see if this should
  176. // be parsed as the GNU extension 'a' length modifier. If not, this
  177. // will be parsed as a conversion specifier.
  178. ++I;
  179. if (I != E && (*I == 's' || *I == 'S' || *I == '[')) {
  180. lmKind = LengthModifier::AsAllocate;
  181. break;
  182. }
  183. --I;
  184. }
  185. return false;
  186. case 'm':
  187. if (IsScanf) {
  188. lmKind = LengthModifier::AsMAllocate;
  189. ++I;
  190. break;
  191. }
  192. return false;
  193. }
  194. LengthModifier lm(lmPosition, lmKind);
  195. FS.setLengthModifier(lm);
  196. return true;
  197. }
  198. //===----------------------------------------------------------------------===//
  199. // Methods on ArgType.
  200. //===----------------------------------------------------------------------===//
  201. bool ArgType::matchesType(ASTContext &C, QualType argTy) const {
  202. if (Ptr) {
  203. // It has to be a pointer.
  204. const PointerType *PT = argTy->getAs<PointerType>();
  205. if (!PT)
  206. return false;
  207. // We cannot write through a const qualified pointer.
  208. if (PT->getPointeeType().isConstQualified())
  209. return false;
  210. argTy = PT->getPointeeType();
  211. }
  212. switch (K) {
  213. case InvalidTy:
  214. llvm_unreachable("ArgType must be valid");
  215. case UnknownTy:
  216. return true;
  217. case AnyCharTy: {
  218. if (const EnumType *ETy = argTy->getAs<EnumType>())
  219. argTy = ETy->getDecl()->getIntegerType();
  220. if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
  221. switch (BT->getKind()) {
  222. default:
  223. break;
  224. case BuiltinType::Char_S:
  225. case BuiltinType::SChar:
  226. case BuiltinType::UChar:
  227. case BuiltinType::Char_U:
  228. return true;
  229. }
  230. return false;
  231. }
  232. case SpecificTy: {
  233. if (const EnumType *ETy = argTy->getAs<EnumType>())
  234. argTy = ETy->getDecl()->getIntegerType();
  235. argTy = C.getCanonicalType(argTy).getUnqualifiedType();
  236. if (T == argTy)
  237. return true;
  238. // Check for "compatible types".
  239. if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
  240. switch (BT->getKind()) {
  241. default:
  242. break;
  243. case BuiltinType::Char_S:
  244. case BuiltinType::SChar:
  245. case BuiltinType::Char_U:
  246. case BuiltinType::UChar:
  247. return T == C.UnsignedCharTy || T == C.SignedCharTy;
  248. case BuiltinType::Short:
  249. return T == C.UnsignedShortTy;
  250. case BuiltinType::UShort:
  251. return T == C.ShortTy;
  252. case BuiltinType::Int:
  253. return T == C.UnsignedIntTy;
  254. case BuiltinType::UInt:
  255. return T == C.IntTy;
  256. case BuiltinType::Long:
  257. return T == C.UnsignedLongTy;
  258. case BuiltinType::ULong:
  259. return T == C.LongTy;
  260. case BuiltinType::LongLong:
  261. return T == C.UnsignedLongLongTy;
  262. case BuiltinType::ULongLong:
  263. return T == C.LongLongTy;
  264. }
  265. return false;
  266. }
  267. case CStrTy: {
  268. const PointerType *PT = argTy->getAs<PointerType>();
  269. if (!PT)
  270. return false;
  271. QualType pointeeTy = PT->getPointeeType();
  272. if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
  273. switch (BT->getKind()) {
  274. case BuiltinType::Void:
  275. case BuiltinType::Char_U:
  276. case BuiltinType::UChar:
  277. case BuiltinType::Char_S:
  278. case BuiltinType::SChar:
  279. return true;
  280. default:
  281. break;
  282. }
  283. return false;
  284. }
  285. case WCStrTy: {
  286. const PointerType *PT = argTy->getAs<PointerType>();
  287. if (!PT)
  288. return false;
  289. QualType pointeeTy =
  290. C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
  291. return pointeeTy == C.getWCharType();
  292. }
  293. case WIntTy: {
  294. QualType PromoArg =
  295. argTy->isPromotableIntegerType()
  296. ? C.getPromotedIntegerType(argTy) : argTy;
  297. QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
  298. PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
  299. // If the promoted argument is the corresponding signed type of the
  300. // wint_t type, then it should match.
  301. if (PromoArg->hasSignedIntegerRepresentation() &&
  302. C.getCorrespondingUnsignedType(PromoArg) == WInt)
  303. return true;
  304. return WInt == PromoArg;
  305. }
  306. case CPointerTy:
  307. return argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
  308. argTy->isBlockPointerType() || argTy->isNullPtrType();
  309. case ObjCPointerTy: {
  310. if (argTy->getAs<ObjCObjectPointerType>() ||
  311. argTy->getAs<BlockPointerType>())
  312. return true;
  313. // Handle implicit toll-free bridging.
  314. if (const PointerType *PT = argTy->getAs<PointerType>()) {
  315. // Things such as CFTypeRef are really just opaque pointers
  316. // to C structs representing CF types that can often be bridged
  317. // to Objective-C objects. Since the compiler doesn't know which
  318. // structs can be toll-free bridged, we just accept them all.
  319. QualType pointee = PT->getPointeeType();
  320. if (pointee->getAsStructureType() || pointee->isVoidType())
  321. return true;
  322. }
  323. return false;
  324. }
  325. }
  326. llvm_unreachable("Invalid ArgType Kind!");
  327. }
  328. QualType ArgType::getRepresentativeType(ASTContext &C) const {
  329. QualType Res;
  330. switch (K) {
  331. case InvalidTy:
  332. llvm_unreachable("No representative type for Invalid ArgType");
  333. case UnknownTy:
  334. llvm_unreachable("No representative type for Unknown ArgType");
  335. case AnyCharTy:
  336. Res = C.CharTy;
  337. break;
  338. case SpecificTy:
  339. Res = T;
  340. break;
  341. case CStrTy:
  342. Res = C.getPointerType(C.CharTy);
  343. break;
  344. case WCStrTy:
  345. Res = C.getPointerType(C.getWCharType());
  346. break;
  347. case ObjCPointerTy:
  348. Res = C.ObjCBuiltinIdTy;
  349. break;
  350. case CPointerTy:
  351. Res = C.VoidPtrTy;
  352. break;
  353. case WIntTy: {
  354. Res = C.getWIntType();
  355. break;
  356. }
  357. }
  358. if (Ptr)
  359. Res = C.getPointerType(Res);
  360. return Res;
  361. }
  362. std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
  363. std::string S = getRepresentativeType(C).getAsString();
  364. std::string Alias;
  365. if (Name) {
  366. // Use a specific name for this type, e.g. "size_t".
  367. Alias = Name;
  368. if (Ptr) {
  369. // If ArgType is actually a pointer to T, append an asterisk.
  370. Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
  371. }
  372. // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
  373. if (S == Alias)
  374. Alias.clear();
  375. }
  376. if (!Alias.empty())
  377. return std::string("'") + Alias + "' (aka '" + S + "')";
  378. return std::string("'") + S + "'";
  379. }
  380. //===----------------------------------------------------------------------===//
  381. // Methods on OptionalAmount.
  382. //===----------------------------------------------------------------------===//
  383. ArgType
  384. analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
  385. return Ctx.IntTy;
  386. }
  387. //===----------------------------------------------------------------------===//
  388. // Methods on LengthModifier.
  389. //===----------------------------------------------------------------------===//
  390. const char *
  391. analyze_format_string::LengthModifier::toString() const {
  392. switch (kind) {
  393. case AsChar:
  394. return "hh";
  395. case AsShort:
  396. return "h";
  397. case AsLong: // or AsWideChar
  398. return "l";
  399. case AsLongLong:
  400. return "ll";
  401. case AsQuad:
  402. return "q";
  403. case AsIntMax:
  404. return "j";
  405. case AsSizeT:
  406. return "z";
  407. case AsPtrDiff:
  408. return "t";
  409. case AsLongDouble:
  410. return "L";
  411. case AsAllocate:
  412. return "a";
  413. case AsMAllocate:
  414. return "m";
  415. case None:
  416. return "";
  417. }
  418. return NULL;
  419. }
  420. //===----------------------------------------------------------------------===//
  421. // Methods on ConversionSpecifier.
  422. //===----------------------------------------------------------------------===//
  423. const char *ConversionSpecifier::toString() const {
  424. switch (kind) {
  425. case dArg: return "d";
  426. case iArg: return "i";
  427. case oArg: return "o";
  428. case uArg: return "u";
  429. case xArg: return "x";
  430. case XArg: return "X";
  431. case fArg: return "f";
  432. case FArg: return "F";
  433. case eArg: return "e";
  434. case EArg: return "E";
  435. case gArg: return "g";
  436. case GArg: return "G";
  437. case aArg: return "a";
  438. case AArg: return "A";
  439. case cArg: return "c";
  440. case sArg: return "s";
  441. case pArg: return "p";
  442. case nArg: return "n";
  443. case PercentArg: return "%";
  444. case ScanListArg: return "[";
  445. case InvalidSpecifier: return NULL;
  446. // MacOS X unicode extensions.
  447. case CArg: return "C";
  448. case SArg: return "S";
  449. // Objective-C specific specifiers.
  450. case ObjCObjArg: return "@";
  451. // GlibC specific specifiers.
  452. case PrintErrno: return "m";
  453. }
  454. return NULL;
  455. }
  456. //===----------------------------------------------------------------------===//
  457. // Methods on OptionalAmount.
  458. //===----------------------------------------------------------------------===//
  459. void OptionalAmount::toString(raw_ostream &os) const {
  460. switch (hs) {
  461. case Invalid:
  462. case NotSpecified:
  463. return;
  464. case Arg:
  465. if (UsesDotPrefix)
  466. os << ".";
  467. if (usesPositionalArg())
  468. os << "*" << getPositionalArgIndex() << "$";
  469. else
  470. os << "*";
  471. break;
  472. case Constant:
  473. if (UsesDotPrefix)
  474. os << ".";
  475. os << amt;
  476. break;
  477. }
  478. }
  479. bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const {
  480. switch (LM.getKind()) {
  481. case LengthModifier::None:
  482. return true;
  483. // Handle most integer flags
  484. case LengthModifier::AsChar:
  485. case LengthModifier::AsShort:
  486. case LengthModifier::AsLongLong:
  487. case LengthModifier::AsQuad:
  488. case LengthModifier::AsIntMax:
  489. case LengthModifier::AsSizeT:
  490. case LengthModifier::AsPtrDiff:
  491. switch (CS.getKind()) {
  492. case ConversionSpecifier::dArg:
  493. case ConversionSpecifier::iArg:
  494. case ConversionSpecifier::oArg:
  495. case ConversionSpecifier::uArg:
  496. case ConversionSpecifier::xArg:
  497. case ConversionSpecifier::XArg:
  498. case ConversionSpecifier::nArg:
  499. return true;
  500. default:
  501. return false;
  502. }
  503. // Handle 'l' flag
  504. case LengthModifier::AsLong:
  505. switch (CS.getKind()) {
  506. case ConversionSpecifier::dArg:
  507. case ConversionSpecifier::iArg:
  508. case ConversionSpecifier::oArg:
  509. case ConversionSpecifier::uArg:
  510. case ConversionSpecifier::xArg:
  511. case ConversionSpecifier::XArg:
  512. case ConversionSpecifier::aArg:
  513. case ConversionSpecifier::AArg:
  514. case ConversionSpecifier::fArg:
  515. case ConversionSpecifier::FArg:
  516. case ConversionSpecifier::eArg:
  517. case ConversionSpecifier::EArg:
  518. case ConversionSpecifier::gArg:
  519. case ConversionSpecifier::GArg:
  520. case ConversionSpecifier::nArg:
  521. case ConversionSpecifier::cArg:
  522. case ConversionSpecifier::sArg:
  523. case ConversionSpecifier::ScanListArg:
  524. return true;
  525. default:
  526. return false;
  527. }
  528. case LengthModifier::AsLongDouble:
  529. switch (CS.getKind()) {
  530. case ConversionSpecifier::aArg:
  531. case ConversionSpecifier::AArg:
  532. case ConversionSpecifier::fArg:
  533. case ConversionSpecifier::FArg:
  534. case ConversionSpecifier::eArg:
  535. case ConversionSpecifier::EArg:
  536. case ConversionSpecifier::gArg:
  537. case ConversionSpecifier::GArg:
  538. return true;
  539. // GNU libc extension.
  540. case ConversionSpecifier::dArg:
  541. case ConversionSpecifier::iArg:
  542. case ConversionSpecifier::oArg:
  543. case ConversionSpecifier::uArg:
  544. case ConversionSpecifier::xArg:
  545. case ConversionSpecifier::XArg:
  546. return !Target.getTriple().isOSDarwin() &&
  547. !Target.getTriple().isOSWindows();
  548. default:
  549. return false;
  550. }
  551. case LengthModifier::AsAllocate:
  552. switch (CS.getKind()) {
  553. case ConversionSpecifier::sArg:
  554. case ConversionSpecifier::SArg:
  555. case ConversionSpecifier::ScanListArg:
  556. return true;
  557. default:
  558. return false;
  559. }
  560. case LengthModifier::AsMAllocate:
  561. switch (CS.getKind()) {
  562. case ConversionSpecifier::cArg:
  563. case ConversionSpecifier::CArg:
  564. case ConversionSpecifier::sArg:
  565. case ConversionSpecifier::SArg:
  566. case ConversionSpecifier::ScanListArg:
  567. return true;
  568. default:
  569. return false;
  570. }
  571. }
  572. llvm_unreachable("Invalid LengthModifier Kind!");
  573. }
  574. bool FormatSpecifier::hasStandardLengthModifier() const {
  575. switch (LM.getKind()) {
  576. case LengthModifier::None:
  577. case LengthModifier::AsChar:
  578. case LengthModifier::AsShort:
  579. case LengthModifier::AsLong:
  580. case LengthModifier::AsLongLong:
  581. case LengthModifier::AsIntMax:
  582. case LengthModifier::AsSizeT:
  583. case LengthModifier::AsPtrDiff:
  584. case LengthModifier::AsLongDouble:
  585. return true;
  586. case LengthModifier::AsAllocate:
  587. case LengthModifier::AsMAllocate:
  588. case LengthModifier::AsQuad:
  589. return false;
  590. }
  591. llvm_unreachable("Invalid LengthModifier Kind!");
  592. }
  593. bool FormatSpecifier::hasStandardConversionSpecifier(const LangOptions &LangOpt) const {
  594. switch (CS.getKind()) {
  595. case ConversionSpecifier::cArg:
  596. case ConversionSpecifier::dArg:
  597. case ConversionSpecifier::iArg:
  598. case ConversionSpecifier::oArg:
  599. case ConversionSpecifier::uArg:
  600. case ConversionSpecifier::xArg:
  601. case ConversionSpecifier::XArg:
  602. case ConversionSpecifier::fArg:
  603. case ConversionSpecifier::FArg:
  604. case ConversionSpecifier::eArg:
  605. case ConversionSpecifier::EArg:
  606. case ConversionSpecifier::gArg:
  607. case ConversionSpecifier::GArg:
  608. case ConversionSpecifier::aArg:
  609. case ConversionSpecifier::AArg:
  610. case ConversionSpecifier::sArg:
  611. case ConversionSpecifier::pArg:
  612. case ConversionSpecifier::nArg:
  613. case ConversionSpecifier::ObjCObjArg:
  614. case ConversionSpecifier::ScanListArg:
  615. case ConversionSpecifier::PercentArg:
  616. return true;
  617. case ConversionSpecifier::CArg:
  618. case ConversionSpecifier::SArg:
  619. return LangOpt.ObjC1 || LangOpt.ObjC2;
  620. case ConversionSpecifier::InvalidSpecifier:
  621. case ConversionSpecifier::PrintErrno:
  622. return false;
  623. }
  624. llvm_unreachable("Invalid ConversionSpecifier Kind!");
  625. }
  626. bool FormatSpecifier::hasStandardLengthConversionCombination() const {
  627. if (LM.getKind() == LengthModifier::AsLongDouble) {
  628. switch(CS.getKind()) {
  629. case ConversionSpecifier::dArg:
  630. case ConversionSpecifier::iArg:
  631. case ConversionSpecifier::oArg:
  632. case ConversionSpecifier::uArg:
  633. case ConversionSpecifier::xArg:
  634. case ConversionSpecifier::XArg:
  635. return false;
  636. default:
  637. return true;
  638. }
  639. }
  640. return true;
  641. }
  642. llvm::Optional<LengthModifier>
  643. FormatSpecifier::getCorrectedLengthModifier() const {
  644. if (LM.getKind() == LengthModifier::AsLongDouble) {
  645. switch (CS.getKind()) {
  646. case ConversionSpecifier::dArg:
  647. case ConversionSpecifier::iArg:
  648. case ConversionSpecifier::oArg:
  649. case ConversionSpecifier::uArg:
  650. case ConversionSpecifier::xArg:
  651. case ConversionSpecifier::XArg: {
  652. LengthModifier FixedLM(LM);
  653. FixedLM.setKind(LengthModifier::AsLongLong);
  654. return FixedLM;
  655. }
  656. default:
  657. break;
  658. }
  659. }
  660. return llvm::Optional<LengthModifier>();
  661. }
  662. bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
  663. LengthModifier &LM) {
  664. assert(isa<TypedefType>(QT) && "Expected a TypedefType");
  665. const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
  666. for (;;) {
  667. const IdentifierInfo *Identifier = Typedef->getIdentifier();
  668. if (Identifier->getName() == "size_t") {
  669. LM.setKind(LengthModifier::AsSizeT);
  670. return true;
  671. } else if (Identifier->getName() == "ssize_t") {
  672. // Not C99, but common in Unix.
  673. LM.setKind(LengthModifier::AsSizeT);
  674. return true;
  675. } else if (Identifier->getName() == "intmax_t") {
  676. LM.setKind(LengthModifier::AsIntMax);
  677. return true;
  678. } else if (Identifier->getName() == "uintmax_t") {
  679. LM.setKind(LengthModifier::AsIntMax);
  680. return true;
  681. } else if (Identifier->getName() == "ptrdiff_t") {
  682. LM.setKind(LengthModifier::AsPtrDiff);
  683. return true;
  684. }
  685. QualType T = Typedef->getUnderlyingType();
  686. if (!isa<TypedefType>(T))
  687. break;
  688. Typedef = cast<TypedefType>(T)->getDecl();
  689. }
  690. return false;
  691. }