PrintfFormatString.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. //== PrintfFormatString.cpp - Analysis of printf format strings --*- 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. // Handling of format string in printf and friends. The structure of format
  10. // strings for fprintf() are described in C99 7.19.6.1.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "FormatStringParsing.h"
  14. #include "clang/AST/FormatString.h"
  15. #include "clang/AST/OSLog.h"
  16. #include "clang/Basic/TargetInfo.h"
  17. #include "llvm/Support/Regex.h"
  18. using clang::analyze_format_string::ArgType;
  19. using clang::analyze_format_string::FormatStringHandler;
  20. using clang::analyze_format_string::LengthModifier;
  21. using clang::analyze_format_string::OptionalAmount;
  22. using clang::analyze_format_string::ConversionSpecifier;
  23. using clang::analyze_printf::PrintfSpecifier;
  24. using namespace clang;
  25. typedef clang::analyze_format_string::SpecifierResult<PrintfSpecifier>
  26. PrintfSpecifierResult;
  27. //===----------------------------------------------------------------------===//
  28. // Methods for parsing format strings.
  29. //===----------------------------------------------------------------------===//
  30. using analyze_format_string::ParseNonPositionAmount;
  31. static bool ParsePrecision(FormatStringHandler &H, PrintfSpecifier &FS,
  32. const char *Start, const char *&Beg, const char *E,
  33. unsigned *argIndex) {
  34. if (argIndex) {
  35. FS.setPrecision(ParseNonPositionAmount(Beg, E, *argIndex));
  36. } else {
  37. const OptionalAmount Amt = ParsePositionAmount(H, Start, Beg, E,
  38. analyze_format_string::PrecisionPos);
  39. if (Amt.isInvalid())
  40. return true;
  41. FS.setPrecision(Amt);
  42. }
  43. return false;
  44. }
  45. static bool ParseObjCFlags(FormatStringHandler &H, PrintfSpecifier &FS,
  46. const char *FlagBeg, const char *E, bool Warn) {
  47. StringRef Flag(FlagBeg, E - FlagBeg);
  48. // Currently there is only one flag.
  49. if (Flag == "tt") {
  50. FS.setHasObjCTechnicalTerm(FlagBeg);
  51. return false;
  52. }
  53. // Handle either the case of no flag or an invalid flag.
  54. if (Warn) {
  55. if (Flag == "")
  56. H.HandleEmptyObjCModifierFlag(FlagBeg, E - FlagBeg);
  57. else
  58. H.HandleInvalidObjCModifierFlag(FlagBeg, E - FlagBeg);
  59. }
  60. return true;
  61. }
  62. static PrintfSpecifierResult ParsePrintfSpecifier(FormatStringHandler &H,
  63. const char *&Beg,
  64. const char *E,
  65. unsigned &argIndex,
  66. const LangOptions &LO,
  67. const TargetInfo &Target,
  68. bool Warn,
  69. bool isFreeBSDKPrintf) {
  70. using namespace clang::analyze_format_string;
  71. using namespace clang::analyze_printf;
  72. const char *I = Beg;
  73. const char *Start = nullptr;
  74. UpdateOnReturn <const char*> UpdateBeg(Beg, I);
  75. // Look for a '%' character that indicates the start of a format specifier.
  76. for ( ; I != E ; ++I) {
  77. char c = *I;
  78. if (c == '\0') {
  79. // Detect spurious null characters, which are likely errors.
  80. H.HandleNullChar(I);
  81. return true;
  82. }
  83. if (c == '%') {
  84. Start = I++; // Record the start of the format specifier.
  85. break;
  86. }
  87. }
  88. // No format specifier found?
  89. if (!Start)
  90. return false;
  91. if (I == E) {
  92. // No more characters left?
  93. if (Warn)
  94. H.HandleIncompleteSpecifier(Start, E - Start);
  95. return true;
  96. }
  97. PrintfSpecifier FS;
  98. if (ParseArgPosition(H, FS, Start, I, E))
  99. return true;
  100. if (I == E) {
  101. // No more characters left?
  102. if (Warn)
  103. H.HandleIncompleteSpecifier(Start, E - Start);
  104. return true;
  105. }
  106. if (*I == '{') {
  107. ++I;
  108. unsigned char PrivacyFlags = 0;
  109. StringRef MatchedStr;
  110. do {
  111. StringRef Str(I, E - I);
  112. std::string Match = "^[[:space:]]*"
  113. "(private|public|sensitive|mask\\.[^[:space:],}]*)"
  114. "[[:space:]]*(,|})";
  115. llvm::Regex R(Match);
  116. SmallVector<StringRef, 2> Matches;
  117. if (R.match(Str, &Matches)) {
  118. MatchedStr = Matches[1];
  119. I += Matches[0].size();
  120. // Set the privacy flag if the privacy annotation in the
  121. // comma-delimited segment is at least as strict as the privacy
  122. // annotations in previous comma-delimited segments.
  123. if (MatchedStr.startswith("mask")) {
  124. StringRef MaskType = MatchedStr.substr(sizeof("mask.") - 1);
  125. unsigned Size = MaskType.size();
  126. if (Warn && (Size == 0 || Size > 8))
  127. H.handleInvalidMaskType(MaskType);
  128. FS.setMaskType(MaskType);
  129. } else if (MatchedStr.equals("sensitive"))
  130. PrivacyFlags = clang::analyze_os_log::OSLogBufferItem::IsSensitive;
  131. else if (PrivacyFlags !=
  132. clang::analyze_os_log::OSLogBufferItem::IsSensitive &&
  133. MatchedStr.equals("private"))
  134. PrivacyFlags = clang::analyze_os_log::OSLogBufferItem::IsPrivate;
  135. else if (PrivacyFlags == 0 && MatchedStr.equals("public"))
  136. PrivacyFlags = clang::analyze_os_log::OSLogBufferItem::IsPublic;
  137. } else {
  138. size_t CommaOrBracePos =
  139. Str.find_if([](char c) { return c == ',' || c == '}'; });
  140. if (CommaOrBracePos == StringRef::npos) {
  141. // Neither a comma nor the closing brace was found.
  142. if (Warn)
  143. H.HandleIncompleteSpecifier(Start, E - Start);
  144. return true;
  145. }
  146. I += CommaOrBracePos + 1;
  147. }
  148. // Continue until the closing brace is found.
  149. } while (*(I - 1) == ',');
  150. // Set the privacy flag.
  151. switch (PrivacyFlags) {
  152. case 0:
  153. break;
  154. case clang::analyze_os_log::OSLogBufferItem::IsPrivate:
  155. FS.setIsPrivate(MatchedStr.data());
  156. break;
  157. case clang::analyze_os_log::OSLogBufferItem::IsPublic:
  158. FS.setIsPublic(MatchedStr.data());
  159. break;
  160. case clang::analyze_os_log::OSLogBufferItem::IsSensitive:
  161. FS.setIsSensitive(MatchedStr.data());
  162. break;
  163. default:
  164. llvm_unreachable("Unexpected privacy flag value");
  165. }
  166. }
  167. // Look for flags (if any).
  168. bool hasMore = true;
  169. for ( ; I != E; ++I) {
  170. switch (*I) {
  171. default: hasMore = false; break;
  172. case '\'':
  173. // FIXME: POSIX specific. Always accept?
  174. FS.setHasThousandsGrouping(I);
  175. break;
  176. case '-': FS.setIsLeftJustified(I); break;
  177. case '+': FS.setHasPlusPrefix(I); break;
  178. case ' ': FS.setHasSpacePrefix(I); break;
  179. case '#': FS.setHasAlternativeForm(I); break;
  180. case '0': FS.setHasLeadingZeros(I); break;
  181. }
  182. if (!hasMore)
  183. break;
  184. }
  185. if (I == E) {
  186. // No more characters left?
  187. if (Warn)
  188. H.HandleIncompleteSpecifier(Start, E - Start);
  189. return true;
  190. }
  191. // Look for the field width (if any).
  192. if (ParseFieldWidth(H, FS, Start, I, E,
  193. FS.usesPositionalArg() ? nullptr : &argIndex))
  194. return true;
  195. if (I == E) {
  196. // No more characters left?
  197. if (Warn)
  198. H.HandleIncompleteSpecifier(Start, E - Start);
  199. return true;
  200. }
  201. // Look for the precision (if any).
  202. if (*I == '.') {
  203. ++I;
  204. if (I == E) {
  205. if (Warn)
  206. H.HandleIncompleteSpecifier(Start, E - Start);
  207. return true;
  208. }
  209. if (ParsePrecision(H, FS, Start, I, E,
  210. FS.usesPositionalArg() ? nullptr : &argIndex))
  211. return true;
  212. if (I == E) {
  213. // No more characters left?
  214. if (Warn)
  215. H.HandleIncompleteSpecifier(Start, E - Start);
  216. return true;
  217. }
  218. }
  219. if (ParseVectorModifier(H, FS, I, E, LO))
  220. return true;
  221. // Look for the length modifier.
  222. if (ParseLengthModifier(FS, I, E, LO) && I == E) {
  223. // No more characters left?
  224. if (Warn)
  225. H.HandleIncompleteSpecifier(Start, E - Start);
  226. return true;
  227. }
  228. // Look for the Objective-C modifier flags, if any.
  229. // We parse these here, even if they don't apply to
  230. // the conversion specifier, and then emit an error
  231. // later if the conversion specifier isn't '@'. This
  232. // enables better recovery, and we don't know if
  233. // these flags are applicable until later.
  234. const char *ObjCModifierFlagsStart = nullptr,
  235. *ObjCModifierFlagsEnd = nullptr;
  236. if (*I == '[') {
  237. ObjCModifierFlagsStart = I;
  238. ++I;
  239. auto flagStart = I;
  240. for (;; ++I) {
  241. ObjCModifierFlagsEnd = I;
  242. if (I == E) {
  243. if (Warn)
  244. H.HandleIncompleteSpecifier(Start, E - Start);
  245. return true;
  246. }
  247. // Did we find the closing ']'?
  248. if (*I == ']') {
  249. if (ParseObjCFlags(H, FS, flagStart, I, Warn))
  250. return true;
  251. ++I;
  252. break;
  253. }
  254. // There are no separators defined yet for multiple
  255. // Objective-C modifier flags. When those are
  256. // defined, this is the place to check.
  257. }
  258. }
  259. if (*I == '\0') {
  260. // Detect spurious null characters, which are likely errors.
  261. H.HandleNullChar(I);
  262. return true;
  263. }
  264. // Finally, look for the conversion specifier.
  265. const char *conversionPosition = I++;
  266. ConversionSpecifier::Kind k = ConversionSpecifier::InvalidSpecifier;
  267. switch (*conversionPosition) {
  268. default:
  269. break;
  270. // C99: 7.19.6.1 (section 8).
  271. case '%': k = ConversionSpecifier::PercentArg; break;
  272. case 'A': k = ConversionSpecifier::AArg; break;
  273. case 'E': k = ConversionSpecifier::EArg; break;
  274. case 'F': k = ConversionSpecifier::FArg; break;
  275. case 'G': k = ConversionSpecifier::GArg; break;
  276. case 'X': k = ConversionSpecifier::XArg; break;
  277. case 'a': k = ConversionSpecifier::aArg; break;
  278. case 'c': k = ConversionSpecifier::cArg; break;
  279. case 'd': k = ConversionSpecifier::dArg; break;
  280. case 'e': k = ConversionSpecifier::eArg; break;
  281. case 'f': k = ConversionSpecifier::fArg; break;
  282. case 'g': k = ConversionSpecifier::gArg; break;
  283. case 'i': k = ConversionSpecifier::iArg; break;
  284. case 'n':
  285. // Not handled, but reserved in OpenCL.
  286. if (!LO.OpenCL)
  287. k = ConversionSpecifier::nArg;
  288. break;
  289. case 'o': k = ConversionSpecifier::oArg; break;
  290. case 'p': k = ConversionSpecifier::pArg; break;
  291. case 's': k = ConversionSpecifier::sArg; break;
  292. case 'u': k = ConversionSpecifier::uArg; break;
  293. case 'x': k = ConversionSpecifier::xArg; break;
  294. // C23.
  295. case 'b':
  296. if (isFreeBSDKPrintf)
  297. k = ConversionSpecifier::FreeBSDbArg; // int followed by char *
  298. else
  299. k = ConversionSpecifier::bArg;
  300. break;
  301. case 'B': k = ConversionSpecifier::BArg; break;
  302. // POSIX specific.
  303. case 'C': k = ConversionSpecifier::CArg; break;
  304. case 'S': k = ConversionSpecifier::SArg; break;
  305. // Apple extension for os_log
  306. case 'P':
  307. k = ConversionSpecifier::PArg;
  308. break;
  309. // Objective-C.
  310. case '@': k = ConversionSpecifier::ObjCObjArg; break;
  311. // Glibc specific.
  312. case 'm': k = ConversionSpecifier::PrintErrno; break;
  313. case 'r':
  314. if (isFreeBSDKPrintf)
  315. k = ConversionSpecifier::FreeBSDrArg; // int
  316. break;
  317. case 'y':
  318. if (isFreeBSDKPrintf)
  319. k = ConversionSpecifier::FreeBSDyArg; // int
  320. break;
  321. // Apple-specific.
  322. case 'D':
  323. if (isFreeBSDKPrintf)
  324. k = ConversionSpecifier::FreeBSDDArg; // void * followed by char *
  325. else if (Target.getTriple().isOSDarwin())
  326. k = ConversionSpecifier::DArg;
  327. break;
  328. case 'O':
  329. if (Target.getTriple().isOSDarwin())
  330. k = ConversionSpecifier::OArg;
  331. break;
  332. case 'U':
  333. if (Target.getTriple().isOSDarwin())
  334. k = ConversionSpecifier::UArg;
  335. break;
  336. // MS specific.
  337. case 'Z':
  338. if (Target.getTriple().isOSMSVCRT())
  339. k = ConversionSpecifier::ZArg;
  340. break;
  341. }
  342. // Check to see if we used the Objective-C modifier flags with
  343. // a conversion specifier other than '@'.
  344. if (k != ConversionSpecifier::ObjCObjArg &&
  345. k != ConversionSpecifier::InvalidSpecifier &&
  346. ObjCModifierFlagsStart) {
  347. H.HandleObjCFlagsWithNonObjCConversion(ObjCModifierFlagsStart,
  348. ObjCModifierFlagsEnd + 1,
  349. conversionPosition);
  350. return true;
  351. }
  352. PrintfConversionSpecifier CS(conversionPosition, k);
  353. FS.setConversionSpecifier(CS);
  354. if (CS.consumesDataArgument() && !FS.usesPositionalArg())
  355. FS.setArgIndex(argIndex++);
  356. // FreeBSD kernel specific.
  357. if (k == ConversionSpecifier::FreeBSDbArg ||
  358. k == ConversionSpecifier::FreeBSDDArg)
  359. argIndex++;
  360. if (k == ConversionSpecifier::InvalidSpecifier) {
  361. unsigned Len = I - Start;
  362. if (ParseUTF8InvalidSpecifier(Start, E, Len)) {
  363. CS.setEndScanList(Start + Len);
  364. FS.setConversionSpecifier(CS);
  365. }
  366. // Assume the conversion takes one argument.
  367. return !H.HandleInvalidPrintfConversionSpecifier(FS, Start, Len);
  368. }
  369. return PrintfSpecifierResult(Start, FS);
  370. }
  371. bool clang::analyze_format_string::ParsePrintfString(FormatStringHandler &H,
  372. const char *I,
  373. const char *E,
  374. const LangOptions &LO,
  375. const TargetInfo &Target,
  376. bool isFreeBSDKPrintf) {
  377. unsigned argIndex = 0;
  378. // Keep looking for a format specifier until we have exhausted the string.
  379. while (I != E) {
  380. const PrintfSpecifierResult &FSR = ParsePrintfSpecifier(H, I, E, argIndex,
  381. LO, Target, true,
  382. isFreeBSDKPrintf);
  383. // Did a fail-stop error of any kind occur when parsing the specifier?
  384. // If so, don't do any more processing.
  385. if (FSR.shouldStop())
  386. return true;
  387. // Did we exhaust the string or encounter an error that
  388. // we can recover from?
  389. if (!FSR.hasValue())
  390. continue;
  391. // We have a format specifier. Pass it to the callback.
  392. if (!H.HandlePrintfSpecifier(FSR.getValue(), FSR.getStart(),
  393. I - FSR.getStart(), Target))
  394. return true;
  395. }
  396. assert(I == E && "Format string not exhausted");
  397. return false;
  398. }
  399. bool clang::analyze_format_string::ParseFormatStringHasSArg(const char *I,
  400. const char *E,
  401. const LangOptions &LO,
  402. const TargetInfo &Target) {
  403. unsigned argIndex = 0;
  404. // Keep looking for a %s format specifier until we have exhausted the string.
  405. FormatStringHandler H;
  406. while (I != E) {
  407. const PrintfSpecifierResult &FSR = ParsePrintfSpecifier(H, I, E, argIndex,
  408. LO, Target, false,
  409. false);
  410. // Did a fail-stop error of any kind occur when parsing the specifier?
  411. // If so, don't do any more processing.
  412. if (FSR.shouldStop())
  413. return false;
  414. // Did we exhaust the string or encounter an error that
  415. // we can recover from?
  416. if (!FSR.hasValue())
  417. continue;
  418. const analyze_printf::PrintfSpecifier &FS = FSR.getValue();
  419. // Return true if this a %s format specifier.
  420. if (FS.getConversionSpecifier().getKind() == ConversionSpecifier::Kind::sArg)
  421. return true;
  422. }
  423. return false;
  424. }
  425. bool clang::analyze_format_string::parseFormatStringHasFormattingSpecifiers(
  426. const char *Begin, const char *End, const LangOptions &LO,
  427. const TargetInfo &Target) {
  428. unsigned ArgIndex = 0;
  429. // Keep looking for a formatting specifier until we have exhausted the string.
  430. FormatStringHandler H;
  431. while (Begin != End) {
  432. const PrintfSpecifierResult &FSR =
  433. ParsePrintfSpecifier(H, Begin, End, ArgIndex, LO, Target, false, false);
  434. if (FSR.shouldStop())
  435. break;
  436. if (FSR.hasValue())
  437. return true;
  438. }
  439. return false;
  440. }
  441. //===----------------------------------------------------------------------===//
  442. // Methods on PrintfSpecifier.
  443. //===----------------------------------------------------------------------===//
  444. ArgType PrintfSpecifier::getScalarArgType(ASTContext &Ctx,
  445. bool IsObjCLiteral) const {
  446. if (CS.getKind() == ConversionSpecifier::cArg)
  447. switch (LM.getKind()) {
  448. case LengthModifier::None:
  449. return Ctx.IntTy;
  450. case LengthModifier::AsLong:
  451. case LengthModifier::AsWide:
  452. return ArgType(ArgType::WIntTy, "wint_t");
  453. case LengthModifier::AsShort:
  454. if (Ctx.getTargetInfo().getTriple().isOSMSVCRT())
  455. return Ctx.IntTy;
  456. [[fallthrough]];
  457. default:
  458. return ArgType::Invalid();
  459. }
  460. if (CS.isIntArg())
  461. switch (LM.getKind()) {
  462. case LengthModifier::AsLongDouble:
  463. // GNU extension.
  464. return Ctx.LongLongTy;
  465. case LengthModifier::None:
  466. case LengthModifier::AsShortLong:
  467. return Ctx.IntTy;
  468. case LengthModifier::AsInt32:
  469. return ArgType(Ctx.IntTy, "__int32");
  470. case LengthModifier::AsChar:
  471. return ArgType::AnyCharTy;
  472. case LengthModifier::AsShort: return Ctx.ShortTy;
  473. case LengthModifier::AsLong: return Ctx.LongTy;
  474. case LengthModifier::AsLongLong:
  475. case LengthModifier::AsQuad:
  476. return Ctx.LongLongTy;
  477. case LengthModifier::AsInt64:
  478. return ArgType(Ctx.LongLongTy, "__int64");
  479. case LengthModifier::AsIntMax:
  480. return ArgType(Ctx.getIntMaxType(), "intmax_t");
  481. case LengthModifier::AsSizeT:
  482. return ArgType::makeSizeT(ArgType(Ctx.getSignedSizeType(), "ssize_t"));
  483. case LengthModifier::AsInt3264:
  484. return Ctx.getTargetInfo().getTriple().isArch64Bit()
  485. ? ArgType(Ctx.LongLongTy, "__int64")
  486. : ArgType(Ctx.IntTy, "__int32");
  487. case LengthModifier::AsPtrDiff:
  488. return ArgType::makePtrdiffT(
  489. ArgType(Ctx.getPointerDiffType(), "ptrdiff_t"));
  490. case LengthModifier::AsAllocate:
  491. case LengthModifier::AsMAllocate:
  492. case LengthModifier::AsWide:
  493. return ArgType::Invalid();
  494. }
  495. if (CS.isUIntArg())
  496. switch (LM.getKind()) {
  497. case LengthModifier::AsLongDouble:
  498. // GNU extension.
  499. return Ctx.UnsignedLongLongTy;
  500. case LengthModifier::None:
  501. case LengthModifier::AsShortLong:
  502. return Ctx.UnsignedIntTy;
  503. case LengthModifier::AsInt32:
  504. return ArgType(Ctx.UnsignedIntTy, "unsigned __int32");
  505. case LengthModifier::AsChar: return Ctx.UnsignedCharTy;
  506. case LengthModifier::AsShort: return Ctx.UnsignedShortTy;
  507. case LengthModifier::AsLong: return Ctx.UnsignedLongTy;
  508. case LengthModifier::AsLongLong:
  509. case LengthModifier::AsQuad:
  510. return Ctx.UnsignedLongLongTy;
  511. case LengthModifier::AsInt64:
  512. return ArgType(Ctx.UnsignedLongLongTy, "unsigned __int64");
  513. case LengthModifier::AsIntMax:
  514. return ArgType(Ctx.getUIntMaxType(), "uintmax_t");
  515. case LengthModifier::AsSizeT:
  516. return ArgType::makeSizeT(ArgType(Ctx.getSizeType(), "size_t"));
  517. case LengthModifier::AsInt3264:
  518. return Ctx.getTargetInfo().getTriple().isArch64Bit()
  519. ? ArgType(Ctx.UnsignedLongLongTy, "unsigned __int64")
  520. : ArgType(Ctx.UnsignedIntTy, "unsigned __int32");
  521. case LengthModifier::AsPtrDiff:
  522. return ArgType::makePtrdiffT(
  523. ArgType(Ctx.getUnsignedPointerDiffType(), "unsigned ptrdiff_t"));
  524. case LengthModifier::AsAllocate:
  525. case LengthModifier::AsMAllocate:
  526. case LengthModifier::AsWide:
  527. return ArgType::Invalid();
  528. }
  529. if (CS.isDoubleArg()) {
  530. if (!VectorNumElts.isInvalid()) {
  531. switch (LM.getKind()) {
  532. case LengthModifier::AsShort:
  533. return Ctx.HalfTy;
  534. case LengthModifier::AsShortLong:
  535. return Ctx.FloatTy;
  536. case LengthModifier::AsLong:
  537. default:
  538. return Ctx.DoubleTy;
  539. }
  540. }
  541. if (LM.getKind() == LengthModifier::AsLongDouble)
  542. return Ctx.LongDoubleTy;
  543. return Ctx.DoubleTy;
  544. }
  545. if (CS.getKind() == ConversionSpecifier::nArg) {
  546. switch (LM.getKind()) {
  547. case LengthModifier::None:
  548. return ArgType::PtrTo(Ctx.IntTy);
  549. case LengthModifier::AsChar:
  550. return ArgType::PtrTo(Ctx.SignedCharTy);
  551. case LengthModifier::AsShort:
  552. return ArgType::PtrTo(Ctx.ShortTy);
  553. case LengthModifier::AsLong:
  554. return ArgType::PtrTo(Ctx.LongTy);
  555. case LengthModifier::AsLongLong:
  556. case LengthModifier::AsQuad:
  557. return ArgType::PtrTo(Ctx.LongLongTy);
  558. case LengthModifier::AsIntMax:
  559. return ArgType::PtrTo(ArgType(Ctx.getIntMaxType(), "intmax_t"));
  560. case LengthModifier::AsSizeT:
  561. return ArgType::PtrTo(ArgType(Ctx.getSignedSizeType(), "ssize_t"));
  562. case LengthModifier::AsPtrDiff:
  563. return ArgType::PtrTo(ArgType(Ctx.getPointerDiffType(), "ptrdiff_t"));
  564. case LengthModifier::AsLongDouble:
  565. return ArgType(); // FIXME: Is this a known extension?
  566. case LengthModifier::AsAllocate:
  567. case LengthModifier::AsMAllocate:
  568. case LengthModifier::AsInt32:
  569. case LengthModifier::AsInt3264:
  570. case LengthModifier::AsInt64:
  571. case LengthModifier::AsWide:
  572. return ArgType::Invalid();
  573. case LengthModifier::AsShortLong:
  574. llvm_unreachable("only used for OpenCL which doesn not handle nArg");
  575. }
  576. }
  577. switch (CS.getKind()) {
  578. case ConversionSpecifier::sArg:
  579. if (LM.getKind() == LengthModifier::AsWideChar) {
  580. if (IsObjCLiteral)
  581. return ArgType(Ctx.getPointerType(Ctx.UnsignedShortTy.withConst()),
  582. "const unichar *");
  583. return ArgType(ArgType::WCStrTy, "wchar_t *");
  584. }
  585. if (LM.getKind() == LengthModifier::AsWide)
  586. return ArgType(ArgType::WCStrTy, "wchar_t *");
  587. return ArgType::CStrTy;
  588. case ConversionSpecifier::SArg:
  589. if (IsObjCLiteral)
  590. return ArgType(Ctx.getPointerType(Ctx.UnsignedShortTy.withConst()),
  591. "const unichar *");
  592. if (Ctx.getTargetInfo().getTriple().isOSMSVCRT() &&
  593. LM.getKind() == LengthModifier::AsShort)
  594. return ArgType::CStrTy;
  595. return ArgType(ArgType::WCStrTy, "wchar_t *");
  596. case ConversionSpecifier::CArg:
  597. if (IsObjCLiteral)
  598. return ArgType(Ctx.UnsignedShortTy, "unichar");
  599. if (Ctx.getTargetInfo().getTriple().isOSMSVCRT() &&
  600. LM.getKind() == LengthModifier::AsShort)
  601. return Ctx.IntTy;
  602. return ArgType(Ctx.WideCharTy, "wchar_t");
  603. case ConversionSpecifier::pArg:
  604. case ConversionSpecifier::PArg:
  605. return ArgType::CPointerTy;
  606. case ConversionSpecifier::ObjCObjArg:
  607. return ArgType::ObjCPointerTy;
  608. default:
  609. break;
  610. }
  611. // FIXME: Handle other cases.
  612. return ArgType();
  613. }
  614. ArgType PrintfSpecifier::getArgType(ASTContext &Ctx,
  615. bool IsObjCLiteral) const {
  616. const PrintfConversionSpecifier &CS = getConversionSpecifier();
  617. if (!CS.consumesDataArgument())
  618. return ArgType::Invalid();
  619. ArgType ScalarTy = getScalarArgType(Ctx, IsObjCLiteral);
  620. if (!ScalarTy.isValid() || VectorNumElts.isInvalid())
  621. return ScalarTy;
  622. return ScalarTy.makeVectorType(Ctx, VectorNumElts.getConstantAmount());
  623. }
  624. bool PrintfSpecifier::fixType(QualType QT, const LangOptions &LangOpt,
  625. ASTContext &Ctx, bool IsObjCLiteral) {
  626. // %n is different from other conversion specifiers; don't try to fix it.
  627. if (CS.getKind() == ConversionSpecifier::nArg)
  628. return false;
  629. // Handle Objective-C objects first. Note that while the '%@' specifier will
  630. // not warn for structure pointer or void pointer arguments (because that's
  631. // how CoreFoundation objects are implemented), we only show a fixit for '%@'
  632. // if we know it's an object (block, id, class, or __attribute__((NSObject))).
  633. if (QT->isObjCRetainableType()) {
  634. if (!IsObjCLiteral)
  635. return false;
  636. CS.setKind(ConversionSpecifier::ObjCObjArg);
  637. // Disable irrelevant flags
  638. HasThousandsGrouping = false;
  639. HasPlusPrefix = false;
  640. HasSpacePrefix = false;
  641. HasAlternativeForm = false;
  642. HasLeadingZeroes = false;
  643. Precision.setHowSpecified(OptionalAmount::NotSpecified);
  644. LM.setKind(LengthModifier::None);
  645. return true;
  646. }
  647. // Handle strings next (char *, wchar_t *)
  648. if (QT->isPointerType() && (QT->getPointeeType()->isAnyCharacterType())) {
  649. CS.setKind(ConversionSpecifier::sArg);
  650. // Disable irrelevant flags
  651. HasAlternativeForm = false;
  652. HasLeadingZeroes = false;
  653. // Set the long length modifier for wide characters
  654. if (QT->getPointeeType()->isWideCharType())
  655. LM.setKind(LengthModifier::AsWideChar);
  656. else
  657. LM.setKind(LengthModifier::None);
  658. return true;
  659. }
  660. // If it's an enum, get its underlying type.
  661. if (const EnumType *ETy = QT->getAs<EnumType>())
  662. QT = ETy->getDecl()->getIntegerType();
  663. const BuiltinType *BT = QT->getAs<BuiltinType>();
  664. if (!BT) {
  665. const VectorType *VT = QT->getAs<VectorType>();
  666. if (VT) {
  667. QT = VT->getElementType();
  668. BT = QT->getAs<BuiltinType>();
  669. VectorNumElts = OptionalAmount(VT->getNumElements());
  670. }
  671. }
  672. // We can only work with builtin types.
  673. if (!BT)
  674. return false;
  675. // Set length modifier
  676. switch (BT->getKind()) {
  677. case BuiltinType::Bool:
  678. case BuiltinType::WChar_U:
  679. case BuiltinType::WChar_S:
  680. case BuiltinType::Char8: // FIXME: Treat like 'char'?
  681. case BuiltinType::Char16:
  682. case BuiltinType::Char32:
  683. case BuiltinType::UInt128:
  684. case BuiltinType::Int128:
  685. case BuiltinType::Half:
  686. case BuiltinType::BFloat16:
  687. case BuiltinType::Float16:
  688. case BuiltinType::Float128:
  689. case BuiltinType::Ibm128:
  690. case BuiltinType::ShortAccum:
  691. case BuiltinType::Accum:
  692. case BuiltinType::LongAccum:
  693. case BuiltinType::UShortAccum:
  694. case BuiltinType::UAccum:
  695. case BuiltinType::ULongAccum:
  696. case BuiltinType::ShortFract:
  697. case BuiltinType::Fract:
  698. case BuiltinType::LongFract:
  699. case BuiltinType::UShortFract:
  700. case BuiltinType::UFract:
  701. case BuiltinType::ULongFract:
  702. case BuiltinType::SatShortAccum:
  703. case BuiltinType::SatAccum:
  704. case BuiltinType::SatLongAccum:
  705. case BuiltinType::SatUShortAccum:
  706. case BuiltinType::SatUAccum:
  707. case BuiltinType::SatULongAccum:
  708. case BuiltinType::SatShortFract:
  709. case BuiltinType::SatFract:
  710. case BuiltinType::SatLongFract:
  711. case BuiltinType::SatUShortFract:
  712. case BuiltinType::SatUFract:
  713. case BuiltinType::SatULongFract:
  714. // Various types which are non-trivial to correct.
  715. return false;
  716. #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
  717. case BuiltinType::Id:
  718. #include "clang/Basic/OpenCLImageTypes.def"
  719. #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
  720. case BuiltinType::Id:
  721. #include "clang/Basic/OpenCLExtensionTypes.def"
  722. #define SVE_TYPE(Name, Id, SingletonId) \
  723. case BuiltinType::Id:
  724. #include "clang/Basic/AArch64SVEACLETypes.def"
  725. #define PPC_VECTOR_TYPE(Name, Id, Size) \
  726. case BuiltinType::Id:
  727. #include "clang/Basic/PPCTypes.def"
  728. #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
  729. #include "clang/Basic/RISCVVTypes.def"
  730. #define SIGNED_TYPE(Id, SingletonId)
  731. #define UNSIGNED_TYPE(Id, SingletonId)
  732. #define FLOATING_TYPE(Id, SingletonId)
  733. #define BUILTIN_TYPE(Id, SingletonId) \
  734. case BuiltinType::Id:
  735. #include "clang/AST/BuiltinTypes.def"
  736. // Misc other stuff which doesn't make sense here.
  737. return false;
  738. case BuiltinType::UInt:
  739. case BuiltinType::Int:
  740. case BuiltinType::Float:
  741. LM.setKind(VectorNumElts.isInvalid() ?
  742. LengthModifier::None : LengthModifier::AsShortLong);
  743. break;
  744. case BuiltinType::Double:
  745. LM.setKind(VectorNumElts.isInvalid() ?
  746. LengthModifier::None : LengthModifier::AsLong);
  747. break;
  748. case BuiltinType::Char_U:
  749. case BuiltinType::UChar:
  750. case BuiltinType::Char_S:
  751. case BuiltinType::SChar:
  752. LM.setKind(LengthModifier::AsChar);
  753. break;
  754. case BuiltinType::Short:
  755. case BuiltinType::UShort:
  756. LM.setKind(LengthModifier::AsShort);
  757. break;
  758. case BuiltinType::Long:
  759. case BuiltinType::ULong:
  760. LM.setKind(LengthModifier::AsLong);
  761. break;
  762. case BuiltinType::LongLong:
  763. case BuiltinType::ULongLong:
  764. LM.setKind(LengthModifier::AsLongLong);
  765. break;
  766. case BuiltinType::LongDouble:
  767. LM.setKind(LengthModifier::AsLongDouble);
  768. break;
  769. }
  770. // Handle size_t, ptrdiff_t, etc. that have dedicated length modifiers in C99.
  771. if (LangOpt.C99 || LangOpt.CPlusPlus11)
  772. namedTypeToLengthModifier(QT, LM);
  773. // If fixing the length modifier was enough, we might be done.
  774. if (hasValidLengthModifier(Ctx.getTargetInfo(), LangOpt)) {
  775. // If we're going to offer a fix anyway, make sure the sign matches.
  776. switch (CS.getKind()) {
  777. case ConversionSpecifier::uArg:
  778. case ConversionSpecifier::UArg:
  779. if (QT->isSignedIntegerType())
  780. CS.setKind(clang::analyze_format_string::ConversionSpecifier::dArg);
  781. break;
  782. case ConversionSpecifier::dArg:
  783. case ConversionSpecifier::DArg:
  784. case ConversionSpecifier::iArg:
  785. if (QT->isUnsignedIntegerType() && !HasPlusPrefix)
  786. CS.setKind(clang::analyze_format_string::ConversionSpecifier::uArg);
  787. break;
  788. default:
  789. // Other specifiers do not have signed/unsigned variants.
  790. break;
  791. }
  792. const analyze_printf::ArgType &ATR = getArgType(Ctx, IsObjCLiteral);
  793. if (ATR.isValid() && ATR.matchesType(Ctx, QT))
  794. return true;
  795. }
  796. // Set conversion specifier and disable any flags which do not apply to it.
  797. // Let typedefs to char fall through to int, as %c is silly for uint8_t.
  798. if (!QT->getAs<TypedefType>() && QT->isCharType()) {
  799. CS.setKind(ConversionSpecifier::cArg);
  800. LM.setKind(LengthModifier::None);
  801. Precision.setHowSpecified(OptionalAmount::NotSpecified);
  802. HasAlternativeForm = false;
  803. HasLeadingZeroes = false;
  804. HasPlusPrefix = false;
  805. }
  806. // Test for Floating type first as LongDouble can pass isUnsignedIntegerType
  807. else if (QT->isRealFloatingType()) {
  808. CS.setKind(ConversionSpecifier::fArg);
  809. } else if (QT->isSignedIntegerType()) {
  810. CS.setKind(ConversionSpecifier::dArg);
  811. HasAlternativeForm = false;
  812. } else if (QT->isUnsignedIntegerType()) {
  813. CS.setKind(ConversionSpecifier::uArg);
  814. HasAlternativeForm = false;
  815. HasPlusPrefix = false;
  816. } else {
  817. llvm_unreachable("Unexpected type");
  818. }
  819. return true;
  820. }
  821. void PrintfSpecifier::toString(raw_ostream &os) const {
  822. // Whilst some features have no defined order, we are using the order
  823. // appearing in the C99 standard (ISO/IEC 9899:1999 (E) 7.19.6.1)
  824. os << "%";
  825. // Positional args
  826. if (usesPositionalArg()) {
  827. os << getPositionalArgIndex() << "$";
  828. }
  829. // Conversion flags
  830. if (IsLeftJustified) os << "-";
  831. if (HasPlusPrefix) os << "+";
  832. if (HasSpacePrefix) os << " ";
  833. if (HasAlternativeForm) os << "#";
  834. if (HasLeadingZeroes) os << "0";
  835. // Minimum field width
  836. FieldWidth.toString(os);
  837. // Precision
  838. Precision.toString(os);
  839. // Vector modifier
  840. if (!VectorNumElts.isInvalid())
  841. os << 'v' << VectorNumElts.getConstantAmount();
  842. // Length modifier
  843. os << LM.toString();
  844. // Conversion specifier
  845. os << CS.toString();
  846. }
  847. bool PrintfSpecifier::hasValidPlusPrefix() const {
  848. if (!HasPlusPrefix)
  849. return true;
  850. // The plus prefix only makes sense for signed conversions
  851. switch (CS.getKind()) {
  852. case ConversionSpecifier::dArg:
  853. case ConversionSpecifier::DArg:
  854. case ConversionSpecifier::iArg:
  855. case ConversionSpecifier::fArg:
  856. case ConversionSpecifier::FArg:
  857. case ConversionSpecifier::eArg:
  858. case ConversionSpecifier::EArg:
  859. case ConversionSpecifier::gArg:
  860. case ConversionSpecifier::GArg:
  861. case ConversionSpecifier::aArg:
  862. case ConversionSpecifier::AArg:
  863. case ConversionSpecifier::FreeBSDrArg:
  864. case ConversionSpecifier::FreeBSDyArg:
  865. return true;
  866. default:
  867. return false;
  868. }
  869. }
  870. bool PrintfSpecifier::hasValidAlternativeForm() const {
  871. if (!HasAlternativeForm)
  872. return true;
  873. // Alternate form flag only valid with the bBoxXaAeEfFgG conversions
  874. switch (CS.getKind()) {
  875. case ConversionSpecifier::bArg:
  876. case ConversionSpecifier::BArg:
  877. case ConversionSpecifier::oArg:
  878. case ConversionSpecifier::OArg:
  879. case ConversionSpecifier::xArg:
  880. case ConversionSpecifier::XArg:
  881. case ConversionSpecifier::aArg:
  882. case ConversionSpecifier::AArg:
  883. case ConversionSpecifier::eArg:
  884. case ConversionSpecifier::EArg:
  885. case ConversionSpecifier::fArg:
  886. case ConversionSpecifier::FArg:
  887. case ConversionSpecifier::gArg:
  888. case ConversionSpecifier::GArg:
  889. case ConversionSpecifier::FreeBSDrArg:
  890. case ConversionSpecifier::FreeBSDyArg:
  891. return true;
  892. default:
  893. return false;
  894. }
  895. }
  896. bool PrintfSpecifier::hasValidLeadingZeros() const {
  897. if (!HasLeadingZeroes)
  898. return true;
  899. // Leading zeroes flag only valid with the bBdiouxXaAeEfFgG conversions
  900. switch (CS.getKind()) {
  901. case ConversionSpecifier::bArg:
  902. case ConversionSpecifier::BArg:
  903. case ConversionSpecifier::dArg:
  904. case ConversionSpecifier::DArg:
  905. case ConversionSpecifier::iArg:
  906. case ConversionSpecifier::oArg:
  907. case ConversionSpecifier::OArg:
  908. case ConversionSpecifier::uArg:
  909. case ConversionSpecifier::UArg:
  910. case ConversionSpecifier::xArg:
  911. case ConversionSpecifier::XArg:
  912. case ConversionSpecifier::aArg:
  913. case ConversionSpecifier::AArg:
  914. case ConversionSpecifier::eArg:
  915. case ConversionSpecifier::EArg:
  916. case ConversionSpecifier::fArg:
  917. case ConversionSpecifier::FArg:
  918. case ConversionSpecifier::gArg:
  919. case ConversionSpecifier::GArg:
  920. case ConversionSpecifier::FreeBSDrArg:
  921. case ConversionSpecifier::FreeBSDyArg:
  922. return true;
  923. default:
  924. return false;
  925. }
  926. }
  927. bool PrintfSpecifier::hasValidSpacePrefix() const {
  928. if (!HasSpacePrefix)
  929. return true;
  930. // The space prefix only makes sense for signed conversions
  931. switch (CS.getKind()) {
  932. case ConversionSpecifier::dArg:
  933. case ConversionSpecifier::DArg:
  934. case ConversionSpecifier::iArg:
  935. case ConversionSpecifier::fArg:
  936. case ConversionSpecifier::FArg:
  937. case ConversionSpecifier::eArg:
  938. case ConversionSpecifier::EArg:
  939. case ConversionSpecifier::gArg:
  940. case ConversionSpecifier::GArg:
  941. case ConversionSpecifier::aArg:
  942. case ConversionSpecifier::AArg:
  943. case ConversionSpecifier::FreeBSDrArg:
  944. case ConversionSpecifier::FreeBSDyArg:
  945. return true;
  946. default:
  947. return false;
  948. }
  949. }
  950. bool PrintfSpecifier::hasValidLeftJustified() const {
  951. if (!IsLeftJustified)
  952. return true;
  953. // The left justified flag is valid for all conversions except n
  954. switch (CS.getKind()) {
  955. case ConversionSpecifier::nArg:
  956. return false;
  957. default:
  958. return true;
  959. }
  960. }
  961. bool PrintfSpecifier::hasValidThousandsGroupingPrefix() const {
  962. if (!HasThousandsGrouping)
  963. return true;
  964. switch (CS.getKind()) {
  965. case ConversionSpecifier::dArg:
  966. case ConversionSpecifier::DArg:
  967. case ConversionSpecifier::iArg:
  968. case ConversionSpecifier::uArg:
  969. case ConversionSpecifier::UArg:
  970. case ConversionSpecifier::fArg:
  971. case ConversionSpecifier::FArg:
  972. case ConversionSpecifier::gArg:
  973. case ConversionSpecifier::GArg:
  974. return true;
  975. default:
  976. return false;
  977. }
  978. }
  979. bool PrintfSpecifier::hasValidPrecision() const {
  980. if (Precision.getHowSpecified() == OptionalAmount::NotSpecified)
  981. return true;
  982. // Precision is only valid with the bBdiouxXaAeEfFgGsP conversions
  983. switch (CS.getKind()) {
  984. case ConversionSpecifier::bArg:
  985. case ConversionSpecifier::BArg:
  986. case ConversionSpecifier::dArg:
  987. case ConversionSpecifier::DArg:
  988. case ConversionSpecifier::iArg:
  989. case ConversionSpecifier::oArg:
  990. case ConversionSpecifier::OArg:
  991. case ConversionSpecifier::uArg:
  992. case ConversionSpecifier::UArg:
  993. case ConversionSpecifier::xArg:
  994. case ConversionSpecifier::XArg:
  995. case ConversionSpecifier::aArg:
  996. case ConversionSpecifier::AArg:
  997. case ConversionSpecifier::eArg:
  998. case ConversionSpecifier::EArg:
  999. case ConversionSpecifier::fArg:
  1000. case ConversionSpecifier::FArg:
  1001. case ConversionSpecifier::gArg:
  1002. case ConversionSpecifier::GArg:
  1003. case ConversionSpecifier::sArg:
  1004. case ConversionSpecifier::FreeBSDrArg:
  1005. case ConversionSpecifier::FreeBSDyArg:
  1006. case ConversionSpecifier::PArg:
  1007. return true;
  1008. default:
  1009. return false;
  1010. }
  1011. }
  1012. bool PrintfSpecifier::hasValidFieldWidth() const {
  1013. if (FieldWidth.getHowSpecified() == OptionalAmount::NotSpecified)
  1014. return true;
  1015. // The field width is valid for all conversions except n
  1016. switch (CS.getKind()) {
  1017. case ConversionSpecifier::nArg:
  1018. return false;
  1019. default:
  1020. return true;
  1021. }
  1022. }