You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

printf_check.cc 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <sys/types.h>
  17. #include "printf_check.h"
  18. #include "clang/AST/AST.h"
  19. #include "clang/AST/Expr.h"
  20. #include "clang/AST/ASTConsumer.h"
  21. #include "clang/AST/RecursiveASTVisitor.h"
  22. #include <unordered_map>
  23. #include <unordered_set>
  24. #include <vector>
  25. #include <sstream>
  26. #include <ctype.h>
  27. #include <signal.h>
  28. #include <assert.h>
  29. #include <cstdint>
  30. using namespace clang;
  31. namespace rspamd {
  32. struct PrintfArgChecker;
  33. static bool cstring_arg_handler (const Expr *arg,
  34. struct PrintfArgChecker *ctx);
  35. static bool int_arg_handler (const Expr *arg,
  36. struct PrintfArgChecker *ctx);
  37. static bool long_arg_handler (const Expr *arg,
  38. struct PrintfArgChecker *ctx);
  39. static bool size_arg_handler (const Expr *arg,
  40. struct PrintfArgChecker *ctx);
  41. static bool char_arg_handler (const Expr *arg,
  42. struct PrintfArgChecker *ctx);
  43. static bool double_arg_handler (const Expr *arg,
  44. struct PrintfArgChecker *ctx);
  45. static bool long_double_arg_handler (const Expr *arg,
  46. struct PrintfArgChecker *ctx);
  47. static bool pointer_arg_handler (const Expr *arg,
  48. struct PrintfArgChecker *ctx);
  49. static bool pid_arg_handler (const Expr *arg,
  50. struct PrintfArgChecker *ctx);
  51. static bool time_arg_handler (const Expr *arg,
  52. struct PrintfArgChecker *ctx);
  53. static bool int64_arg_handler (const Expr *arg,
  54. struct PrintfArgChecker *ctx);
  55. static bool int32_arg_handler (const Expr *arg,
  56. struct PrintfArgChecker *ctx);
  57. static bool tok_arg_handler (const Expr *arg,
  58. struct PrintfArgChecker *ctx);
  59. static bool fstring_arg_handler (const Expr *arg,
  60. struct PrintfArgChecker *ctx);
  61. static bool gstring_arg_handler (const Expr *arg,
  62. struct PrintfArgChecker *ctx);
  63. static bool gerr_arg_handler (const Expr *arg,
  64. struct PrintfArgChecker *ctx);
  65. using arg_parser_t = bool (*) (const Expr *, struct PrintfArgChecker *);
  66. static void
  67. print_error (const std::string &err, const Expr *e, const ASTContext *ast,
  68. CompilerInstance *ci)
  69. {
  70. auto loc = e->getExprLoc ();
  71. auto &diag = ci->getDiagnostics ();
  72. auto id = diag.getCustomDiagID (DiagnosticsEngine::Error,
  73. "format query error: %0");
  74. diag.Report (loc, id) << err;
  75. }
  76. static void
  77. print_warning (const std::string &err, const Expr *e, const ASTContext *ast,
  78. CompilerInstance *ci)
  79. {
  80. auto loc = e->getExprLoc ();
  81. auto &diag = ci->getDiagnostics ();
  82. auto id = diag.getCustomDiagID (DiagnosticsEngine::Warning,
  83. "format query warning: %0");
  84. diag.Report (loc, id) << err;
  85. }
  86. struct PrintfArgChecker {
  87. private:
  88. arg_parser_t parser;
  89. public:
  90. int width;
  91. int precision;
  92. bool is_unsigned;
  93. ASTContext *past;
  94. CompilerInstance *pci;
  95. PrintfArgChecker (arg_parser_t _p, ASTContext *_ast, CompilerInstance *_ci) :
  96. parser (_p), past (_ast), pci(_ci)
  97. {
  98. width = 0;
  99. precision = 0;
  100. is_unsigned = false;
  101. }
  102. virtual ~PrintfArgChecker ()
  103. {
  104. }
  105. bool operator() (const Expr *e)
  106. {
  107. return parser (e, this);
  108. }
  109. };
  110. class PrintfCheckVisitor::impl {
  111. std::unordered_map<std::string, unsigned int> printf_functions;
  112. std::unordered_set<char> format_specs;
  113. ASTContext *pcontext;
  114. CompilerInstance *ci;
  115. std::unique_ptr <PrintfArgChecker> parseFlags (const std::string &flags,
  116. const Expr *e)
  117. {
  118. auto type = flags.back ();
  119. switch (type) {
  120. case 's':
  121. return llvm::make_unique<PrintfArgChecker> (cstring_arg_handler,
  122. this->pcontext, this->ci);
  123. case 'd':
  124. return llvm::make_unique<PrintfArgChecker> (int_arg_handler,
  125. this->pcontext, this->ci);
  126. case 'z':
  127. return llvm::make_unique<PrintfArgChecker> (size_arg_handler,
  128. this->pcontext, this->ci);
  129. case 'l':
  130. return llvm::make_unique<PrintfArgChecker> (long_arg_handler,
  131. this->pcontext, this->ci);
  132. case 'f':
  133. case 'g':
  134. return llvm::make_unique<PrintfArgChecker> (double_arg_handler,
  135. this->pcontext, this->ci);
  136. case 'F':
  137. case 'G':
  138. return llvm::make_unique<PrintfArgChecker> (
  139. long_double_arg_handler,
  140. this->pcontext, this->ci);
  141. case 'c':
  142. return llvm::make_unique<PrintfArgChecker> (char_arg_handler,
  143. this->pcontext, this->ci);
  144. case 'p':
  145. return llvm::make_unique<PrintfArgChecker> (pointer_arg_handler,
  146. this->pcontext, this->ci);
  147. case 'P':
  148. return llvm::make_unique<PrintfArgChecker> (pid_arg_handler,
  149. this->pcontext, this->ci);
  150. case 't':
  151. return llvm::make_unique<PrintfArgChecker> (time_arg_handler,
  152. this->pcontext, this->ci);
  153. case 'L':
  154. return llvm::make_unique<PrintfArgChecker> (int64_arg_handler,
  155. this->pcontext, this->ci);
  156. case 'D':
  157. return llvm::make_unique<PrintfArgChecker> (int32_arg_handler,
  158. this->pcontext, this->ci);
  159. case 'T':
  160. return llvm::make_unique<PrintfArgChecker> (tok_arg_handler,
  161. this->pcontext, this->ci);
  162. case 'V':
  163. return llvm::make_unique<PrintfArgChecker> (fstring_arg_handler,
  164. this->pcontext, this->ci);
  165. case 'v':
  166. return llvm::make_unique<PrintfArgChecker> (gstring_arg_handler,
  167. this->pcontext, this->ci);
  168. case 'e':
  169. return llvm::make_unique<PrintfArgChecker> (gerr_arg_handler,
  170. this->pcontext, this->ci);
  171. default:
  172. print_warning (std::string("unknown parser flag: ") + type,
  173. e, this->pcontext, this->ci);
  174. break;
  175. }
  176. return nullptr;
  177. }
  178. std::shared_ptr <std::vector<PrintfArgChecker>>
  179. genParsers (const StringRef query, const Expr *e)
  180. {
  181. enum {
  182. ignore_chars = 0,
  183. read_percent,
  184. read_width,
  185. read_precision,
  186. read_arg
  187. } state = ignore_chars;
  188. int width, precision;
  189. std::string flags;
  190. auto res = std::make_shared<std::vector<PrintfArgChecker> > ();
  191. for (auto citer = query.begin(); citer != query.end(); ++citer) {
  192. auto c = *citer;
  193. switch (state) {
  194. case ignore_chars:
  195. if (c == '%') {
  196. state = read_percent;
  197. flags.clear ();
  198. width = precision = 0;
  199. }
  200. break;
  201. case read_percent:
  202. if (isdigit (c)) {
  203. state = read_width;
  204. width = c - '0';
  205. }
  206. else if (c == '.') {
  207. state = read_precision;
  208. precision = c - '0';
  209. }
  210. else if (c == '*') {
  211. /* %*s - need integer argument */
  212. res->emplace_back (int_arg_handler, this->pcontext,
  213. this->ci);
  214. if (*std::next (citer) == '.') {
  215. ++citer;
  216. state = read_precision;
  217. }
  218. else {
  219. state = read_arg;
  220. }
  221. }
  222. else if (c == '%') {
  223. /* Percent character, ignore */
  224. state = ignore_chars;
  225. }
  226. else {
  227. // Rewind iter
  228. --citer;
  229. state = read_arg;
  230. }
  231. break;
  232. case read_width:
  233. if (isdigit (c)) {
  234. width *= 10;
  235. width += c - '0';
  236. }
  237. else if (c == '.') {
  238. state = read_precision;
  239. precision = c - '0';
  240. }
  241. else {
  242. // Rewind iter
  243. --citer;
  244. state = read_arg;
  245. }
  246. break;
  247. case read_precision:
  248. if (isdigit (c)) {
  249. precision *= 10;
  250. precision += c - '0';
  251. }
  252. else if (c == '*') {
  253. res->emplace_back (int_arg_handler, this->pcontext,
  254. this->ci);
  255. state = read_arg;
  256. }
  257. else {
  258. // Rewind iter
  259. --citer;
  260. state = read_arg;
  261. }
  262. break;
  263. case read_arg:
  264. auto found = format_specs.find (c);
  265. if (found != format_specs.end () || !isalpha (c)) {
  266. if (isalpha (c)) {
  267. flags.push_back (c);
  268. }
  269. auto handler = parseFlags (flags, e);
  270. if (handler) {
  271. auto handler_copy = *handler;
  272. handler_copy.precision = precision;
  273. handler_copy.width = width;
  274. res->emplace_back (std::move (handler_copy));
  275. }
  276. else {
  277. return nullptr;
  278. }
  279. if (c == '%') {
  280. state = read_percent;
  281. }
  282. else {
  283. state = ignore_chars;
  284. }
  285. flags.clear ();
  286. width = precision = 0;
  287. }
  288. else {
  289. flags.push_back (c);
  290. }
  291. break;
  292. }
  293. }
  294. if (state == read_arg) {
  295. auto handler = parseFlags (flags, e);
  296. if (handler) {
  297. auto handler_copy = *handler;
  298. handler_copy.precision = precision;
  299. handler_copy.width = width;
  300. res->emplace_back (std::move (handler_copy));
  301. }
  302. else {
  303. return nullptr;
  304. }
  305. }
  306. return res;
  307. }
  308. public:
  309. impl (ASTContext *_ctx, clang::CompilerInstance &_ci)
  310. : pcontext (_ctx), ci(&_ci)
  311. {
  312. /* name -> format string position */
  313. printf_functions = {
  314. {"rspamd_printf", 0},
  315. {"rspamd_default_log_function", 4},
  316. {"rspamd_snprintf", 2},
  317. {"rspamd_fprintf", 1},
  318. {"rspamd_printf_gstring", 1},
  319. {"rspamd_printf_fstring", 1},
  320. {"rspamd_conditional_debug_fast", 6},
  321. };
  322. format_specs = {
  323. 's', 'd', 'l', 'L', 'v', 'V', 'f', 'F', 'g', 'G',
  324. 'T', 'z', 'D', 'c', 'p', 'P', 'e'
  325. };
  326. };
  327. bool VisitCallExpr (CallExpr *E)
  328. {
  329. if (E->getCalleeDecl () == nullptr) {
  330. llvm::errs () << "Bad callee\n";
  331. return false;
  332. }
  333. auto callee = dyn_cast<NamedDecl> (E->getCalleeDecl ());
  334. if (callee == NULL) {
  335. llvm::errs () << "Bad callee\n";
  336. return false;
  337. }
  338. auto fname = callee->getNameAsString ();
  339. auto pos_it = printf_functions.find (fname);
  340. if (pos_it != printf_functions.end ()) {
  341. const auto args = E->getArgs ();
  342. auto pos = pos_it->second;
  343. auto query = args[pos];
  344. if (!query->isEvaluatable (*pcontext)) {
  345. print_warning (std::string ("cannot evaluate query"),
  346. E, this->pcontext, this->ci);
  347. return false;
  348. }
  349. clang::Expr::EvalResult r;
  350. if (!query->EvaluateAsRValue (r, *pcontext)) {
  351. print_warning (std::string ("cannot evaluate rvalue of query"),
  352. E, this->pcontext, this->ci);
  353. return false;
  354. }
  355. auto qval = dyn_cast<StringLiteral> (
  356. r.Val.getLValueBase ().get<const Expr *> ());
  357. if (!qval) {
  358. print_warning (std::string ("bad or absent query string"),
  359. E, this->pcontext, this->ci);
  360. return false;
  361. }
  362. auto parsers = genParsers (qval->getString (), E);
  363. if (parsers) {
  364. if (parsers->size () != E->getNumArgs () - (pos + 1)) {
  365. std::ostringstream err_buf;
  366. err_buf << "number of arguments for " << fname
  367. << " mismatches query string '" <<
  368. qval->getString ().str ()
  369. << "', expected " << parsers->size () <<
  370. " args"
  371. << ", got " <<
  372. (E->getNumArgs () - (pos + 1))
  373. << " args";
  374. print_error (err_buf.str (), E, this->pcontext, this->ci);
  375. return false;
  376. }
  377. else {
  378. for (auto i = pos + 1; i < E->getNumArgs (); i++) {
  379. auto arg = args[i];
  380. if (arg) {
  381. if (!parsers->at (i - (pos + 1)) (arg)) {
  382. return false;
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. return true;
  390. }
  391. };
  392. PrintfCheckVisitor::PrintfCheckVisitor (ASTContext *ctx,
  393. clang::CompilerInstance &ci) :
  394. pimpl{new impl (ctx, ci)}
  395. {
  396. }
  397. PrintfCheckVisitor::~PrintfCheckVisitor ()
  398. {
  399. }
  400. bool PrintfCheckVisitor::VisitCallExpr (clang::CallExpr *E)
  401. {
  402. return pimpl->VisitCallExpr (E);
  403. }
  404. /* Type handlers */
  405. static bool
  406. cstring_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  407. {
  408. auto type = arg->getType ().split ().Ty;
  409. if (!type->isPointerType ()) {
  410. print_error (
  411. std::string ("bad string argument for %s: ") +
  412. arg->getType ().getAsString (),
  413. arg, ctx->past, ctx->pci);
  414. return false;
  415. }
  416. auto ptr_type = type->getPointeeType ().split ().Ty;
  417. if (!ptr_type->isCharType ()) {
  418. /* We might have gchar * here */
  419. auto desugared_type = ptr_type->getUnqualifiedDesugaredType ();
  420. auto desugared_ptr_type = type->getUnqualifiedDesugaredType ();
  421. if (!desugared_type || (!desugared_type->isCharType () &&
  422. !desugared_ptr_type->isVoidPointerType ())) {
  423. if (desugared_type) {
  424. desugared_type->dump ();
  425. }
  426. print_error (
  427. std::string ("bad string argument for %s: ") +
  428. arg->getType ().getAsString (),
  429. arg, ctx->past, ctx->pci);
  430. return false;
  431. }
  432. }
  433. return true;
  434. }
  435. static bool
  436. check_builtin_type (const Expr *arg, struct PrintfArgChecker *ctx,
  437. const std::vector <BuiltinType::Kind> &k, const std::string &fmt)
  438. {
  439. auto type = arg->getType ().split ().Ty;
  440. auto desugared_type = type->getUnqualifiedDesugaredType ();
  441. if (!desugared_type->isBuiltinType ()) {
  442. print_error (
  443. std::string ("not a builtin type for ") + fmt + " arg: " +
  444. arg->getType ().getAsString (),
  445. arg, ctx->past, ctx->pci);
  446. return false;
  447. }
  448. auto builtin_type = dyn_cast<BuiltinType> (desugared_type);
  449. auto kind = builtin_type->getKind ();
  450. auto found = false;
  451. for (auto kk : k) {
  452. if (kind == kk) {
  453. found = true;
  454. break;
  455. }
  456. }
  457. if (!found) {
  458. print_error (
  459. std::string ("bad argument for ") + fmt + " arg: " +
  460. arg->getType ().getAsString () + ", resolved as: " +
  461. builtin_type->getNameAsCString (ctx->past->getPrintingPolicy ()),
  462. arg, ctx->past, ctx->pci);
  463. return false;
  464. }
  465. return true;
  466. }
  467. static bool
  468. int_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  469. {
  470. return check_builtin_type (arg,
  471. ctx,
  472. {BuiltinType::Kind::UInt,
  473. BuiltinType::Kind::Int},
  474. "%d or *");
  475. }
  476. static bool
  477. long_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  478. {
  479. return check_builtin_type (arg,
  480. ctx,
  481. {BuiltinType::Kind::ULong,
  482. BuiltinType::Kind::Long},
  483. "%l");
  484. }
  485. static bool
  486. char_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  487. {
  488. return check_builtin_type (arg,
  489. ctx,
  490. {BuiltinType::Kind::UChar,
  491. BuiltinType::Kind::SChar,
  492. BuiltinType::Kind::Int}, // Because of char -> int propagation
  493. "%c");
  494. }
  495. static bool
  496. size_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  497. {
  498. if (sizeof (size_t) == sizeof (long)) {
  499. return check_builtin_type (arg,
  500. ctx,
  501. {BuiltinType::Kind::ULong,
  502. BuiltinType::Kind::Long},
  503. "%z");
  504. }
  505. else if (sizeof (size_t) == sizeof (int)) {
  506. return check_builtin_type (arg,
  507. ctx,
  508. {BuiltinType::Kind::UInt,
  509. BuiltinType::Kind::Int},
  510. "%z");
  511. }
  512. else {
  513. assert (0);
  514. }
  515. return true;
  516. }
  517. static bool
  518. double_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  519. {
  520. return check_builtin_type (arg,
  521. ctx,
  522. {BuiltinType::Kind::Double},
  523. "%f or %g");
  524. }
  525. static bool
  526. long_double_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  527. {
  528. return check_builtin_type (arg,
  529. ctx,
  530. {BuiltinType::Kind::LongDouble},
  531. "%F or %G");
  532. }
  533. static bool
  534. pid_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  535. {
  536. if (sizeof (pid_t) == sizeof (long)) {
  537. return check_builtin_type (arg,
  538. ctx,
  539. {BuiltinType::Kind::ULong,
  540. BuiltinType::Kind::Long},
  541. "%P");
  542. }
  543. else if (sizeof (pid_t) == sizeof (int)) {
  544. return check_builtin_type (arg,
  545. ctx,
  546. {BuiltinType::Kind::UInt,
  547. BuiltinType::Kind::Int},
  548. "%P");
  549. }
  550. else {
  551. assert (0);
  552. }
  553. }
  554. static bool
  555. time_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  556. {
  557. if (sizeof (time_t) == sizeof (long)) {
  558. return check_builtin_type (arg,
  559. ctx,
  560. {BuiltinType::Kind::ULong,
  561. BuiltinType::Kind::Long},
  562. "%t");
  563. }
  564. else if (sizeof (time_t) == sizeof (int)) {
  565. return check_builtin_type (arg,
  566. ctx,
  567. {BuiltinType::Kind::UInt,
  568. BuiltinType::Kind::Int},
  569. "%t");
  570. }
  571. else {
  572. assert (0);
  573. }
  574. }
  575. static bool
  576. pointer_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  577. {
  578. auto type = arg->getType ().split ().Ty;
  579. if (!type->isPointerType ()) {
  580. print_error (
  581. std::string ("bad pointer argument for %p: ") +
  582. arg->getType ().getAsString (),
  583. arg, ctx->past, ctx->pci);
  584. return false;
  585. }
  586. return true;
  587. }
  588. static bool
  589. int64_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  590. {
  591. std::vector <BuiltinType::Kind> check;
  592. if (sizeof (int64_t) == sizeof (long long)) {
  593. check.push_back (BuiltinType::Kind::ULongLong);
  594. check.push_back (BuiltinType::Kind::LongLong);
  595. }
  596. if (sizeof (int64_t) == sizeof (long)) {
  597. check.push_back (BuiltinType::Kind::ULong);
  598. check.push_back (BuiltinType::Kind::Long);
  599. }
  600. return check_builtin_type (arg,
  601. ctx,
  602. check,
  603. "%L");
  604. return true;
  605. }
  606. static bool
  607. int32_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  608. {
  609. std::vector < BuiltinType::Kind> check;
  610. if (sizeof (int32_t) == sizeof (long)) {
  611. check.push_back (BuiltinType::Kind::ULong);
  612. check.push_back (BuiltinType::Kind::Long);
  613. }
  614. if (sizeof (int32_t) == sizeof (int)) {
  615. check.push_back (BuiltinType::Kind::UInt);
  616. check.push_back (BuiltinType::Kind::Int);
  617. }
  618. return check_builtin_type (arg,
  619. ctx,
  620. check,
  621. "%D");
  622. return true;
  623. }
  624. static bool
  625. check_struct_type (const Expr *arg, struct PrintfArgChecker *ctx,
  626. const std::string &sname, const std::string &fmt)
  627. {
  628. auto type = arg->getType ().split ().Ty;
  629. if (!type->isPointerType ()) {
  630. print_error (
  631. std::string ("bad string argument for %s: ") +
  632. arg->getType ().getAsString (),
  633. arg, ctx->past, ctx->pci);
  634. return false;
  635. }
  636. auto ptr_type = type->getPointeeType ().split ().Ty;
  637. auto desugared_type = ptr_type->getUnqualifiedDesugaredType ();
  638. if (!desugared_type->isRecordType ()) {
  639. print_error (
  640. std::string ("not a record type for ") + fmt + " arg: " +
  641. arg->getType ().getAsString (),
  642. arg, ctx->past, ctx->pci);
  643. return false;
  644. }
  645. auto struct_type = desugared_type->getAsStructureType ();
  646. auto struct_decl = struct_type->getDecl ();
  647. auto struct_def = struct_decl->getNameAsString ();
  648. if (struct_def != sname) {
  649. print_error (std::string ("bad argument '") + struct_def + "' for "
  650. + fmt + " arg: " +
  651. arg->getType ().getAsString (),
  652. arg, ctx->past, ctx->pci);
  653. return false;
  654. }
  655. return true;
  656. }
  657. static bool
  658. tok_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  659. {
  660. return check_struct_type (arg,
  661. ctx,
  662. "f_str_tok",
  663. "%T");
  664. }
  665. static bool
  666. fstring_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  667. {
  668. return check_struct_type (arg,
  669. ctx,
  670. "f_str_s",
  671. "%V");
  672. }
  673. static bool
  674. gstring_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  675. {
  676. return check_struct_type (arg,
  677. ctx,
  678. "_GString",
  679. "%v");
  680. }
  681. static bool
  682. gerr_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  683. {
  684. return check_struct_type (arg,
  685. ctx,
  686. "_GError",
  687. "%e");
  688. }
  689. }