FormatString.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. // FormatString.cpp - Common stuff for handling printf/scanf formats -*- C++ -*-
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Shared details for processing format strings of printf and scanf
  10. // (and friends).
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "FormatStringParsing.h"
  14. #include "clang/Basic/LangOptions.h"
  15. #include "clang/Basic/TargetInfo.h"
  16. #include "llvm/Support/ConvertUTF.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::ConversionSpecifier;
  23. using namespace clang;
  24. // Key function to FormatStringHandler.
  25. FormatStringHandler::~FormatStringHandler() {}
  26. //===----------------------------------------------------------------------===//
  27. // Functions for parsing format strings components in both printf and
  28. // scanf format strings.
  29. //===----------------------------------------------------------------------===//
  30. OptionalAmount
  31. clang::analyze_format_string::ParseAmount(const char *&Beg, const char *E) {
  32. const char *I = Beg;
  33. UpdateOnReturn <const char*> UpdateBeg(Beg, I);
  34. unsigned accumulator = 0;
  35. bool hasDigits = false;
  36. for ( ; I != E; ++I) {
  37. char c = *I;
  38. if (c >= '0' && c <= '9') {
  39. hasDigits = true;
  40. accumulator = (accumulator * 10) + (c - '0');
  41. continue;
  42. }
  43. if (hasDigits)
  44. return OptionalAmount(OptionalAmount::Constant, accumulator, Beg, I - Beg,
  45. false);
  46. break;
  47. }
  48. return OptionalAmount();
  49. }
  50. OptionalAmount
  51. clang::analyze_format_string::ParseNonPositionAmount(const char *&Beg,
  52. const char *E,
  53. unsigned &argIndex) {
  54. if (*Beg == '*') {
  55. ++Beg;
  56. return OptionalAmount(OptionalAmount::Arg, argIndex++, Beg, 0, false);
  57. }
  58. return ParseAmount(Beg, E);
  59. }
  60. OptionalAmount
  61. clang::analyze_format_string::ParsePositionAmount(FormatStringHandler &H,
  62. const char *Start,
  63. const char *&Beg,
  64. const char *E,
  65. PositionContext p) {
  66. if (*Beg == '*') {
  67. const char *I = Beg + 1;
  68. const OptionalAmount &Amt = ParseAmount(I, E);
  69. if (Amt.getHowSpecified() == OptionalAmount::NotSpecified) {
  70. H.HandleInvalidPosition(Beg, I - Beg, p);
  71. return OptionalAmount(false);
  72. }
  73. if (I == E) {
  74. // No more characters left?
  75. H.HandleIncompleteSpecifier(Start, E - Start);
  76. return OptionalAmount(false);
  77. }
  78. assert(Amt.getHowSpecified() == OptionalAmount::Constant);
  79. if (*I == '$') {
  80. // Handle positional arguments
  81. // Special case: '*0$', since this is an easy mistake.
  82. if (Amt.getConstantAmount() == 0) {
  83. H.HandleZeroPosition(Beg, I - Beg + 1);
  84. return OptionalAmount(false);
  85. }
  86. const char *Tmp = Beg;
  87. Beg = ++I;
  88. return OptionalAmount(OptionalAmount::Arg, Amt.getConstantAmount() - 1,
  89. Tmp, 0, true);
  90. }
  91. H.HandleInvalidPosition(Beg, I - Beg, p);
  92. return OptionalAmount(false);
  93. }
  94. return ParseAmount(Beg, E);
  95. }
  96. bool
  97. clang::analyze_format_string::ParseFieldWidth(FormatStringHandler &H,
  98. FormatSpecifier &CS,
  99. const char *Start,
  100. const char *&Beg, const char *E,
  101. unsigned *argIndex) {
  102. // FIXME: Support negative field widths.
  103. if (argIndex) {
  104. CS.setFieldWidth(ParseNonPositionAmount(Beg, E, *argIndex));
  105. }
  106. else {
  107. const OptionalAmount Amt =
  108. ParsePositionAmount(H, Start, Beg, E,
  109. analyze_format_string::FieldWidthPos);
  110. if (Amt.isInvalid())
  111. return true;
  112. CS.setFieldWidth(Amt);
  113. }
  114. return false;
  115. }
  116. bool
  117. clang::analyze_format_string::ParseArgPosition(FormatStringHandler &H,
  118. FormatSpecifier &FS,
  119. const char *Start,
  120. const char *&Beg,
  121. const char *E) {
  122. const char *I = Beg;
  123. const OptionalAmount &Amt = ParseAmount(I, E);
  124. if (I == E) {
  125. // No more characters left?
  126. H.HandleIncompleteSpecifier(Start, E - Start);
  127. return true;
  128. }
  129. if (Amt.getHowSpecified() == OptionalAmount::Constant && *(I++) == '$') {
  130. // Warn that positional arguments are non-standard.
  131. H.HandlePosition(Start, I - Start);
  132. // Special case: '%0$', since this is an easy mistake.
  133. if (Amt.getConstantAmount() == 0) {
  134. H.HandleZeroPosition(Start, I - Start);
  135. return true;
  136. }
  137. FS.setArgIndex(Amt.getConstantAmount() - 1);
  138. FS.setUsesPositionalArg();
  139. // Update the caller's pointer if we decided to consume
  140. // these characters.
  141. Beg = I;
  142. return false;
  143. }
  144. return false;
  145. }
  146. bool
  147. clang::analyze_format_string::ParseVectorModifier(FormatStringHandler &H,
  148. FormatSpecifier &FS,
  149. const char *&I,
  150. const char *E,
  151. const LangOptions &LO) {
  152. if (!LO.OpenCL)
  153. return false;
  154. const char *Start = I;
  155. if (*I == 'v') {
  156. ++I;
  157. if (I == E) {
  158. H.HandleIncompleteSpecifier(Start, E - Start);
  159. return true;
  160. }
  161. OptionalAmount NumElts = ParseAmount(I, E);
  162. if (NumElts.getHowSpecified() != OptionalAmount::Constant) {
  163. H.HandleIncompleteSpecifier(Start, E - Start);
  164. return true;
  165. }
  166. FS.setVectorNumElts(NumElts);
  167. }
  168. return false;
  169. }
  170. bool
  171. clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS,
  172. const char *&I,
  173. const char *E,
  174. const LangOptions &LO,
  175. bool IsScanf) {
  176. LengthModifier::Kind lmKind = LengthModifier::None;
  177. const char *lmPosition = I;
  178. switch (*I) {
  179. default:
  180. return false;
  181. case 'h':
  182. ++I;
  183. if (I != E && *I == 'h') {
  184. ++I;
  185. lmKind = LengthModifier::AsChar;
  186. } else if (I != E && *I == 'l' && LO.OpenCL) {
  187. ++I;
  188. lmKind = LengthModifier::AsShortLong;
  189. } else {
  190. lmKind = LengthModifier::AsShort;
  191. }
  192. break;
  193. case 'l':
  194. ++I;
  195. if (I != E && *I == 'l') {
  196. ++I;
  197. lmKind = LengthModifier::AsLongLong;
  198. } else {
  199. lmKind = LengthModifier::AsLong;
  200. }
  201. break;
  202. case 'j': lmKind = LengthModifier::AsIntMax; ++I; break;
  203. case 'z': lmKind = LengthModifier::AsSizeT; ++I; break;
  204. case 't': lmKind = LengthModifier::AsPtrDiff; ++I; break;
  205. case 'L': lmKind = LengthModifier::AsLongDouble; ++I; break;
  206. case 'q': lmKind = LengthModifier::AsQuad; ++I; break;
  207. case 'a':
  208. if (IsScanf && !LO.C99 && !LO.CPlusPlus11) {
  209. // For scanf in C90, look at the next character to see if this should
  210. // be parsed as the GNU extension 'a' length modifier. If not, this
  211. // will be parsed as a conversion specifier.
  212. ++I;
  213. if (I != E && (*I == 's' || *I == 'S' || *I == '[')) {
  214. lmKind = LengthModifier::AsAllocate;
  215. break;
  216. }
  217. --I;
  218. }
  219. return false;
  220. case 'm':
  221. if (IsScanf) {
  222. lmKind = LengthModifier::AsMAllocate;
  223. ++I;
  224. break;
  225. }
  226. return false;
  227. // printf: AsInt64, AsInt32, AsInt3264
  228. // scanf: AsInt64
  229. case 'I':
  230. if (I + 1 != E && I + 2 != E) {
  231. if (I[1] == '6' && I[2] == '4') {
  232. I += 3;
  233. lmKind = LengthModifier::AsInt64;
  234. break;
  235. }
  236. if (IsScanf)
  237. return false;
  238. if (I[1] == '3' && I[2] == '2') {
  239. I += 3;
  240. lmKind = LengthModifier::AsInt32;
  241. break;
  242. }
  243. }
  244. ++I;
  245. lmKind = LengthModifier::AsInt3264;
  246. break;
  247. case 'w':
  248. lmKind = LengthModifier::AsWide; ++I; break;
  249. }
  250. LengthModifier lm(lmPosition, lmKind);
  251. FS.setLengthModifier(lm);
  252. return true;
  253. }
  254. bool clang::analyze_format_string::ParseUTF8InvalidSpecifier(
  255. const char *SpecifierBegin, const char *FmtStrEnd, unsigned &Len) {
  256. if (SpecifierBegin + 1 >= FmtStrEnd)
  257. return false;
  258. const llvm::UTF8 *SB =
  259. reinterpret_cast<const llvm::UTF8 *>(SpecifierBegin + 1);
  260. const llvm::UTF8 *SE = reinterpret_cast<const llvm::UTF8 *>(FmtStrEnd);
  261. const char FirstByte = *SB;
  262. // If the invalid specifier is a multibyte UTF-8 string, return the
  263. // total length accordingly so that the conversion specifier can be
  264. // properly updated to reflect a complete UTF-8 specifier.
  265. unsigned NumBytes = llvm::getNumBytesForUTF8(FirstByte);
  266. if (NumBytes == 1)
  267. return false;
  268. if (SB + NumBytes > SE)
  269. return false;
  270. Len = NumBytes + 1;
  271. return true;
  272. }
  273. //===----------------------------------------------------------------------===//
  274. // Methods on ArgType.
  275. //===----------------------------------------------------------------------===//
  276. clang::analyze_format_string::ArgType::MatchKind
  277. ArgType::matchesType(ASTContext &C, QualType argTy) const {
  278. if (Ptr) {
  279. // It has to be a pointer.
  280. const PointerType *PT = argTy->getAs<PointerType>();
  281. if (!PT)
  282. return NoMatch;
  283. // We cannot write through a const qualified pointer.
  284. if (PT->getPointeeType().isConstQualified())
  285. return NoMatch;
  286. argTy = PT->getPointeeType();
  287. }
  288. switch (K) {
  289. case InvalidTy:
  290. llvm_unreachable("ArgType must be valid");
  291. case UnknownTy:
  292. return Match;
  293. case AnyCharTy: {
  294. if (const EnumType *ETy = argTy->getAs<EnumType>()) {
  295. // If the enum is incomplete we know nothing about the underlying type.
  296. // Assume that it's 'int'.
  297. if (!ETy->getDecl()->isComplete())
  298. return NoMatch;
  299. argTy = ETy->getDecl()->getIntegerType();
  300. }
  301. if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
  302. switch (BT->getKind()) {
  303. default:
  304. break;
  305. case BuiltinType::Char_S:
  306. case BuiltinType::SChar:
  307. case BuiltinType::UChar:
  308. case BuiltinType::Char_U:
  309. case BuiltinType::Bool:
  310. return Match;
  311. }
  312. return NoMatch;
  313. }
  314. case SpecificTy: {
  315. if (const EnumType *ETy = argTy->getAs<EnumType>()) {
  316. // If the enum is incomplete we know nothing about the underlying type.
  317. // Assume that it's 'int'.
  318. if (!ETy->getDecl()->isComplete())
  319. argTy = C.IntTy;
  320. else
  321. argTy = ETy->getDecl()->getIntegerType();
  322. }
  323. argTy = C.getCanonicalType(argTy).getUnqualifiedType();
  324. if (T == argTy)
  325. return Match;
  326. // Check for "compatible types".
  327. if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
  328. switch (BT->getKind()) {
  329. default:
  330. break;
  331. case BuiltinType::Char_S:
  332. case BuiltinType::SChar:
  333. case BuiltinType::Char_U:
  334. case BuiltinType::UChar:
  335. case BuiltinType::Bool:
  336. if (T == C.UnsignedShortTy || T == C.ShortTy)
  337. return NoMatchTypeConfusion;
  338. return T == C.UnsignedCharTy || T == C.SignedCharTy ? Match
  339. : NoMatch;
  340. case BuiltinType::Short:
  341. return T == C.UnsignedShortTy ? Match : NoMatch;
  342. case BuiltinType::UShort:
  343. return T == C.ShortTy ? Match : NoMatch;
  344. case BuiltinType::Int:
  345. return T == C.UnsignedIntTy ? Match : NoMatch;
  346. case BuiltinType::UInt:
  347. return T == C.IntTy ? Match : NoMatch;
  348. case BuiltinType::Long:
  349. return T == C.UnsignedLongTy ? Match : NoMatch;
  350. case BuiltinType::ULong:
  351. return T == C.LongTy ? Match : NoMatch;
  352. case BuiltinType::LongLong:
  353. return T == C.UnsignedLongLongTy ? Match : NoMatch;
  354. case BuiltinType::ULongLong:
  355. return T == C.LongLongTy ? Match : NoMatch;
  356. }
  357. return NoMatch;
  358. }
  359. case CStrTy: {
  360. const PointerType *PT = argTy->getAs<PointerType>();
  361. if (!PT)
  362. return NoMatch;
  363. QualType pointeeTy = PT->getPointeeType();
  364. if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
  365. switch (BT->getKind()) {
  366. case BuiltinType::Char_U:
  367. case BuiltinType::UChar:
  368. case BuiltinType::Char_S:
  369. case BuiltinType::SChar:
  370. return Match;
  371. default:
  372. break;
  373. }
  374. return NoMatch;
  375. }
  376. case WCStrTy: {
  377. const PointerType *PT = argTy->getAs<PointerType>();
  378. if (!PT)
  379. return NoMatch;
  380. QualType pointeeTy =
  381. C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
  382. return pointeeTy == C.getWideCharType() ? Match : NoMatch;
  383. }
  384. case WIntTy: {
  385. QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
  386. if (C.getCanonicalType(argTy).getUnqualifiedType() == WInt)
  387. return Match;
  388. QualType PromoArg = argTy->isPromotableIntegerType()
  389. ? C.getPromotedIntegerType(argTy)
  390. : argTy;
  391. PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
  392. // If the promoted argument is the corresponding signed type of the
  393. // wint_t type, then it should match.
  394. if (PromoArg->hasSignedIntegerRepresentation() &&
  395. C.getCorrespondingUnsignedType(PromoArg) == WInt)
  396. return Match;
  397. return WInt == PromoArg ? Match : NoMatch;
  398. }
  399. case CPointerTy:
  400. if (argTy->isVoidPointerType()) {
  401. return Match;
  402. } if (argTy->isPointerType() || argTy->isObjCObjectPointerType() ||
  403. argTy->isBlockPointerType() || argTy->isNullPtrType()) {
  404. return NoMatchPedantic;
  405. } else {
  406. return NoMatch;
  407. }
  408. case ObjCPointerTy: {
  409. if (argTy->getAs<ObjCObjectPointerType>() ||
  410. argTy->getAs<BlockPointerType>())
  411. return Match;
  412. // Handle implicit toll-free bridging.
  413. if (const PointerType *PT = argTy->getAs<PointerType>()) {
  414. // Things such as CFTypeRef are really just opaque pointers
  415. // to C structs representing CF types that can often be bridged
  416. // to Objective-C objects. Since the compiler doesn't know which
  417. // structs can be toll-free bridged, we just accept them all.
  418. QualType pointee = PT->getPointeeType();
  419. if (pointee->getAsStructureType() || pointee->isVoidType())
  420. return Match;
  421. }
  422. return NoMatch;
  423. }
  424. }
  425. llvm_unreachable("Invalid ArgType Kind!");
  426. }
  427. ArgType ArgType::makeVectorType(ASTContext &C, unsigned NumElts) const {
  428. // Check for valid vector element types.
  429. if (T.isNull())
  430. return ArgType::Invalid();
  431. QualType Vec = C.getExtVectorType(T, NumElts);
  432. return ArgType(Vec, Name);
  433. }
  434. QualType ArgType::getRepresentativeType(ASTContext &C) const {
  435. QualType Res;
  436. switch (K) {
  437. case InvalidTy:
  438. llvm_unreachable("No representative type for Invalid ArgType");
  439. case UnknownTy:
  440. llvm_unreachable("No representative type for Unknown ArgType");
  441. case AnyCharTy:
  442. Res = C.CharTy;
  443. break;
  444. case SpecificTy:
  445. Res = T;
  446. break;
  447. case CStrTy:
  448. Res = C.getPointerType(C.CharTy);
  449. break;
  450. case WCStrTy:
  451. Res = C.getPointerType(C.getWideCharType());
  452. break;
  453. case ObjCPointerTy:
  454. Res = C.ObjCBuiltinIdTy;
  455. break;
  456. case CPointerTy:
  457. Res = C.VoidPtrTy;
  458. break;
  459. case WIntTy: {
  460. Res = C.getWIntType();
  461. break;
  462. }
  463. }
  464. if (Ptr)
  465. Res = C.getPointerType(Res);
  466. return Res;
  467. }
  468. std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
  469. std::string S = getRepresentativeType(C).getAsString(C.getPrintingPolicy());
  470. std::string Alias;
  471. if (Name) {
  472. // Use a specific name for this type, e.g. "size_t".
  473. Alias = Name;
  474. if (Ptr) {
  475. // If ArgType is actually a pointer to T, append an asterisk.
  476. Alias += (Alias[Alias.size()-1] == '*') ? "*" : " *";
  477. }
  478. // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
  479. if (S == Alias)
  480. Alias.clear();
  481. }
  482. if (!Alias.empty())
  483. return std::string("'") + Alias + "' (aka '" + S + "')";
  484. return std::string("'") + S + "'";
  485. }
  486. //===----------------------------------------------------------------------===//
  487. // Methods on OptionalAmount.
  488. //===----------------------------------------------------------------------===//
  489. ArgType
  490. analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
  491. return Ctx.IntTy;
  492. }
  493. //===----------------------------------------------------------------------===//
  494. // Methods on LengthModifier.
  495. //===----------------------------------------------------------------------===//
  496. const char *
  497. analyze_format_string::LengthModifier::toString() const {
  498. switch (kind) {
  499. case AsChar:
  500. return "hh";
  501. case AsShort:
  502. return "h";
  503. case AsShortLong:
  504. return "hl";
  505. case AsLong: // or AsWideChar
  506. return "l";
  507. case AsLongLong:
  508. return "ll";
  509. case AsQuad:
  510. return "q";
  511. case AsIntMax:
  512. return "j";
  513. case AsSizeT:
  514. return "z";
  515. case AsPtrDiff:
  516. return "t";
  517. case AsInt32:
  518. return "I32";
  519. case AsInt3264:
  520. return "I";
  521. case AsInt64:
  522. return "I64";
  523. case AsLongDouble:
  524. return "L";
  525. case AsAllocate:
  526. return "a";
  527. case AsMAllocate:
  528. return "m";
  529. case AsWide:
  530. return "w";
  531. case None:
  532. return "";
  533. }
  534. return nullptr;
  535. }
  536. //===----------------------------------------------------------------------===//
  537. // Methods on ConversionSpecifier.
  538. //===----------------------------------------------------------------------===//
  539. const char *ConversionSpecifier::toString() const {
  540. switch (kind) {
  541. case dArg: return "d";
  542. case DArg: return "D";
  543. case iArg: return "i";
  544. case oArg: return "o";
  545. case OArg: return "O";
  546. case uArg: return "u";
  547. case UArg: return "U";
  548. case xArg: return "x";
  549. case XArg: return "X";
  550. case fArg: return "f";
  551. case FArg: return "F";
  552. case eArg: return "e";
  553. case EArg: return "E";
  554. case gArg: return "g";
  555. case GArg: return "G";
  556. case aArg: return "a";
  557. case AArg: return "A";
  558. case cArg: return "c";
  559. case sArg: return "s";
  560. case pArg: return "p";
  561. case PArg:
  562. return "P";
  563. case nArg: return "n";
  564. case PercentArg: return "%";
  565. case ScanListArg: return "[";
  566. case InvalidSpecifier: return nullptr;
  567. // POSIX unicode extensions.
  568. case CArg: return "C";
  569. case SArg: return "S";
  570. // Objective-C specific specifiers.
  571. case ObjCObjArg: return "@";
  572. // FreeBSD kernel specific specifiers.
  573. case FreeBSDbArg: return "b";
  574. case FreeBSDDArg: return "D";
  575. case FreeBSDrArg: return "r";
  576. case FreeBSDyArg: return "y";
  577. // GlibC specific specifiers.
  578. case PrintErrno: return "m";
  579. // MS specific specifiers.
  580. case ZArg: return "Z";
  581. }
  582. return nullptr;
  583. }
  584. Optional<ConversionSpecifier>
  585. ConversionSpecifier::getStandardSpecifier() const {
  586. ConversionSpecifier::Kind NewKind;
  587. switch (getKind()) {
  588. default:
  589. return None;
  590. case DArg:
  591. NewKind = dArg;
  592. break;
  593. case UArg:
  594. NewKind = uArg;
  595. break;
  596. case OArg:
  597. NewKind = oArg;
  598. break;
  599. }
  600. ConversionSpecifier FixedCS(*this);
  601. FixedCS.setKind(NewKind);
  602. return FixedCS;
  603. }
  604. //===----------------------------------------------------------------------===//
  605. // Methods on OptionalAmount.
  606. //===----------------------------------------------------------------------===//
  607. void OptionalAmount::toString(raw_ostream &os) const {
  608. switch (hs) {
  609. case Invalid:
  610. case NotSpecified:
  611. return;
  612. case Arg:
  613. if (UsesDotPrefix)
  614. os << ".";
  615. if (usesPositionalArg())
  616. os << "*" << getPositionalArgIndex() << "$";
  617. else
  618. os << "*";
  619. break;
  620. case Constant:
  621. if (UsesDotPrefix)
  622. os << ".";
  623. os << amt;
  624. break;
  625. }
  626. }
  627. bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target,
  628. const LangOptions &LO) const {
  629. switch (LM.getKind()) {
  630. case LengthModifier::None:
  631. return true;
  632. // Handle most integer flags
  633. case LengthModifier::AsShort:
  634. // Length modifier only applies to FP vectors.
  635. if (LO.OpenCL && CS.isDoubleArg())
  636. return !VectorNumElts.isInvalid();
  637. if (Target.getTriple().isOSMSVCRT()) {
  638. switch (CS.getKind()) {
  639. case ConversionSpecifier::cArg:
  640. case ConversionSpecifier::CArg:
  641. case ConversionSpecifier::sArg:
  642. case ConversionSpecifier::SArg:
  643. case ConversionSpecifier::ZArg:
  644. return true;
  645. default:
  646. break;
  647. }
  648. }
  649. LLVM_FALLTHROUGH;
  650. case LengthModifier::AsChar:
  651. case LengthModifier::AsLongLong:
  652. case LengthModifier::AsQuad:
  653. case LengthModifier::AsIntMax:
  654. case LengthModifier::AsSizeT:
  655. case LengthModifier::AsPtrDiff:
  656. switch (CS.getKind()) {
  657. case ConversionSpecifier::dArg:
  658. case ConversionSpecifier::DArg:
  659. case ConversionSpecifier::iArg:
  660. case ConversionSpecifier::oArg:
  661. case ConversionSpecifier::OArg:
  662. case ConversionSpecifier::uArg:
  663. case ConversionSpecifier::UArg:
  664. case ConversionSpecifier::xArg:
  665. case ConversionSpecifier::XArg:
  666. case ConversionSpecifier::nArg:
  667. return true;
  668. case ConversionSpecifier::FreeBSDrArg:
  669. case ConversionSpecifier::FreeBSDyArg:
  670. return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
  671. default:
  672. return false;
  673. }
  674. case LengthModifier::AsShortLong:
  675. return LO.OpenCL && !VectorNumElts.isInvalid();
  676. // Handle 'l' flag
  677. case LengthModifier::AsLong: // or AsWideChar
  678. if (CS.isDoubleArg()) {
  679. // Invalid for OpenCL FP scalars.
  680. if (LO.OpenCL && VectorNumElts.isInvalid())
  681. return false;
  682. return true;
  683. }
  684. switch (CS.getKind()) {
  685. case ConversionSpecifier::dArg:
  686. case ConversionSpecifier::DArg:
  687. case ConversionSpecifier::iArg:
  688. case ConversionSpecifier::oArg:
  689. case ConversionSpecifier::OArg:
  690. case ConversionSpecifier::uArg:
  691. case ConversionSpecifier::UArg:
  692. case ConversionSpecifier::xArg:
  693. case ConversionSpecifier::XArg:
  694. case ConversionSpecifier::nArg:
  695. case ConversionSpecifier::cArg:
  696. case ConversionSpecifier::sArg:
  697. case ConversionSpecifier::ScanListArg:
  698. case ConversionSpecifier::ZArg:
  699. return true;
  700. case ConversionSpecifier::FreeBSDrArg:
  701. case ConversionSpecifier::FreeBSDyArg:
  702. return Target.getTriple().isOSFreeBSD() || Target.getTriple().isPS4();
  703. default:
  704. return false;
  705. }
  706. case LengthModifier::AsLongDouble:
  707. switch (CS.getKind()) {
  708. case ConversionSpecifier::aArg:
  709. case ConversionSpecifier::AArg:
  710. case ConversionSpecifier::fArg:
  711. case ConversionSpecifier::FArg:
  712. case ConversionSpecifier::eArg:
  713. case ConversionSpecifier::EArg:
  714. case ConversionSpecifier::gArg:
  715. case ConversionSpecifier::GArg:
  716. return true;
  717. // GNU libc extension.
  718. case ConversionSpecifier::dArg:
  719. case ConversionSpecifier::iArg:
  720. case ConversionSpecifier::oArg:
  721. case ConversionSpecifier::uArg:
  722. case ConversionSpecifier::xArg:
  723. case ConversionSpecifier::XArg:
  724. return !Target.getTriple().isOSDarwin() &&
  725. !Target.getTriple().isOSWindows();
  726. default:
  727. return false;
  728. }
  729. case LengthModifier::AsAllocate:
  730. switch (CS.getKind()) {
  731. case ConversionSpecifier::sArg:
  732. case ConversionSpecifier::SArg:
  733. case ConversionSpecifier::ScanListArg:
  734. return true;
  735. default:
  736. return false;
  737. }
  738. case LengthModifier::AsMAllocate:
  739. switch (CS.getKind()) {
  740. case ConversionSpecifier::cArg:
  741. case ConversionSpecifier::CArg:
  742. case ConversionSpecifier::sArg:
  743. case ConversionSpecifier::SArg:
  744. case ConversionSpecifier::ScanListArg:
  745. return true;
  746. default:
  747. return false;
  748. }
  749. case LengthModifier::AsInt32:
  750. case LengthModifier::AsInt3264:
  751. case LengthModifier::AsInt64:
  752. switch (CS.getKind()) {
  753. case ConversionSpecifier::dArg:
  754. case ConversionSpecifier::iArg:
  755. case ConversionSpecifier::oArg:
  756. case ConversionSpecifier::uArg:
  757. case ConversionSpecifier::xArg:
  758. case ConversionSpecifier::XArg:
  759. return Target.getTriple().isOSMSVCRT();
  760. default:
  761. return false;
  762. }
  763. case LengthModifier::AsWide:
  764. switch (CS.getKind()) {
  765. case ConversionSpecifier::cArg:
  766. case ConversionSpecifier::CArg:
  767. case ConversionSpecifier::sArg:
  768. case ConversionSpecifier::SArg:
  769. case ConversionSpecifier::ZArg:
  770. return Target.getTriple().isOSMSVCRT();
  771. default:
  772. return false;
  773. }
  774. }
  775. llvm_unreachable("Invalid LengthModifier Kind!");
  776. }
  777. bool FormatSpecifier::hasStandardLengthModifier() const {
  778. switch (LM.getKind()) {
  779. case LengthModifier::None:
  780. case LengthModifier::AsChar:
  781. case LengthModifier::AsShort:
  782. case LengthModifier::AsLong:
  783. case LengthModifier::AsLongLong:
  784. case LengthModifier::AsIntMax:
  785. case LengthModifier::AsSizeT:
  786. case LengthModifier::AsPtrDiff:
  787. case LengthModifier::AsLongDouble:
  788. return true;
  789. case LengthModifier::AsAllocate:
  790. case LengthModifier::AsMAllocate:
  791. case LengthModifier::AsQuad:
  792. case LengthModifier::AsInt32:
  793. case LengthModifier::AsInt3264:
  794. case LengthModifier::AsInt64:
  795. case LengthModifier::AsWide:
  796. case LengthModifier::AsShortLong: // ???
  797. return false;
  798. }
  799. llvm_unreachable("Invalid LengthModifier Kind!");
  800. }
  801. bool FormatSpecifier::hasStandardConversionSpecifier(
  802. const LangOptions &LangOpt) const {
  803. switch (CS.getKind()) {
  804. case ConversionSpecifier::cArg:
  805. case ConversionSpecifier::dArg:
  806. case ConversionSpecifier::iArg:
  807. case ConversionSpecifier::oArg:
  808. case ConversionSpecifier::uArg:
  809. case ConversionSpecifier::xArg:
  810. case ConversionSpecifier::XArg:
  811. case ConversionSpecifier::fArg:
  812. case ConversionSpecifier::FArg:
  813. case ConversionSpecifier::eArg:
  814. case ConversionSpecifier::EArg:
  815. case ConversionSpecifier::gArg:
  816. case ConversionSpecifier::GArg:
  817. case ConversionSpecifier::aArg:
  818. case ConversionSpecifier::AArg:
  819. case ConversionSpecifier::sArg:
  820. case ConversionSpecifier::pArg:
  821. case ConversionSpecifier::nArg:
  822. case ConversionSpecifier::ObjCObjArg:
  823. case ConversionSpecifier::ScanListArg:
  824. case ConversionSpecifier::PercentArg:
  825. case ConversionSpecifier::PArg:
  826. return true;
  827. case ConversionSpecifier::CArg:
  828. case ConversionSpecifier::SArg:
  829. return LangOpt.ObjC;
  830. case ConversionSpecifier::InvalidSpecifier:
  831. case ConversionSpecifier::FreeBSDbArg:
  832. case ConversionSpecifier::FreeBSDDArg:
  833. case ConversionSpecifier::FreeBSDrArg:
  834. case ConversionSpecifier::FreeBSDyArg:
  835. case ConversionSpecifier::PrintErrno:
  836. case ConversionSpecifier::DArg:
  837. case ConversionSpecifier::OArg:
  838. case ConversionSpecifier::UArg:
  839. case ConversionSpecifier::ZArg:
  840. return false;
  841. }
  842. llvm_unreachable("Invalid ConversionSpecifier Kind!");
  843. }
  844. bool FormatSpecifier::hasStandardLengthConversionCombination() const {
  845. if (LM.getKind() == LengthModifier::AsLongDouble) {
  846. switch(CS.getKind()) {
  847. case ConversionSpecifier::dArg:
  848. case ConversionSpecifier::iArg:
  849. case ConversionSpecifier::oArg:
  850. case ConversionSpecifier::uArg:
  851. case ConversionSpecifier::xArg:
  852. case ConversionSpecifier::XArg:
  853. return false;
  854. default:
  855. return true;
  856. }
  857. }
  858. return true;
  859. }
  860. Optional<LengthModifier> FormatSpecifier::getCorrectedLengthModifier() const {
  861. if (CS.isAnyIntArg() || CS.getKind() == ConversionSpecifier::nArg) {
  862. if (LM.getKind() == LengthModifier::AsLongDouble ||
  863. LM.getKind() == LengthModifier::AsQuad) {
  864. LengthModifier FixedLM(LM);
  865. FixedLM.setKind(LengthModifier::AsLongLong);
  866. return FixedLM;
  867. }
  868. }
  869. return None;
  870. }
  871. bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
  872. LengthModifier &LM) {
  873. assert(isa<TypedefType>(QT) && "Expected a TypedefType");
  874. const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
  875. for (;;) {
  876. const IdentifierInfo *Identifier = Typedef->getIdentifier();
  877. if (Identifier->getName() == "size_t") {
  878. LM.setKind(LengthModifier::AsSizeT);
  879. return true;
  880. } else if (Identifier->getName() == "ssize_t") {
  881. // Not C99, but common in Unix.
  882. LM.setKind(LengthModifier::AsSizeT);
  883. return true;
  884. } else if (Identifier->getName() == "intmax_t") {
  885. LM.setKind(LengthModifier::AsIntMax);
  886. return true;
  887. } else if (Identifier->getName() == "uintmax_t") {
  888. LM.setKind(LengthModifier::AsIntMax);
  889. return true;
  890. } else if (Identifier->getName() == "ptrdiff_t") {
  891. LM.setKind(LengthModifier::AsPtrDiff);
  892. return true;
  893. }
  894. QualType T = Typedef->getUnderlyingType();
  895. if (!isa<TypedefType>(T))
  896. break;
  897. Typedef = cast<TypedefType>(T)->getDecl();
  898. }
  899. return false;
  900. }