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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. };
  321. format_specs = {
  322. 's', 'd', 'l', 'L', 'v', 'V', 'f', 'F', 'g', 'G',
  323. 'T', 'z', 'D', 'c', 'p', 'P', 'e'
  324. };
  325. };
  326. bool VisitCallExpr (CallExpr *E)
  327. {
  328. if (E->getCalleeDecl () == nullptr) {
  329. llvm::errs () << "Bad callee\n";
  330. return false;
  331. }
  332. auto callee = dyn_cast<NamedDecl> (E->getCalleeDecl ());
  333. if (callee == NULL) {
  334. llvm::errs () << "Bad callee\n";
  335. return false;
  336. }
  337. auto fname = callee->getNameAsString ();
  338. auto pos_it = printf_functions.find (fname);
  339. if (pos_it != printf_functions.end ()) {
  340. const auto args = E->getArgs ();
  341. auto pos = pos_it->second;
  342. auto query = args[pos];
  343. if (!query->isEvaluatable (*pcontext)) {
  344. print_warning (std::string ("cannot evaluate query"),
  345. E, this->pcontext, this->ci);
  346. return false;
  347. }
  348. clang::Expr::EvalResult r;
  349. if (!query->EvaluateAsRValue (r, *pcontext)) {
  350. print_warning (std::string ("cannot evaluate rvalue of query"),
  351. E, this->pcontext, this->ci);
  352. return false;
  353. }
  354. auto qval = dyn_cast<StringLiteral> (
  355. r.Val.getLValueBase ().get<const Expr *> ());
  356. if (!qval) {
  357. print_warning (std::string ("bad or absent query string"),
  358. E, this->pcontext, this->ci);
  359. return false;
  360. }
  361. auto parsers = genParsers (qval->getString (), E);
  362. if (parsers) {
  363. if (parsers->size () != E->getNumArgs () - (pos + 1)) {
  364. std::ostringstream err_buf;
  365. err_buf << "number of arguments for " << fname
  366. << " mismatches query string '" <<
  367. qval->getString ().str ()
  368. << "', expected " << parsers->size () <<
  369. " args"
  370. << ", got " <<
  371. (E->getNumArgs () - (pos + 1))
  372. << " args";
  373. print_error (err_buf.str (), E, this->pcontext, this->ci);
  374. return false;
  375. }
  376. else {
  377. for (auto i = pos + 1; i < E->getNumArgs (); i++) {
  378. auto arg = args[i];
  379. if (arg) {
  380. if (!parsers->at (i - (pos + 1)) (arg)) {
  381. return false;
  382. }
  383. }
  384. }
  385. }
  386. }
  387. }
  388. return true;
  389. }
  390. };
  391. PrintfCheckVisitor::PrintfCheckVisitor (ASTContext *ctx,
  392. clang::CompilerInstance &ci) :
  393. pimpl{new impl (ctx, ci)}
  394. {
  395. }
  396. PrintfCheckVisitor::~PrintfCheckVisitor ()
  397. {
  398. }
  399. bool PrintfCheckVisitor::VisitCallExpr (clang::CallExpr *E)
  400. {
  401. return pimpl->VisitCallExpr (E);
  402. }
  403. /* Type handlers */
  404. static bool
  405. cstring_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  406. {
  407. auto type = arg->getType ().split ().Ty;
  408. if (!type->isPointerType ()) {
  409. print_error (
  410. std::string ("bad string argument for %s: ") +
  411. arg->getType ().getAsString (),
  412. arg, ctx->past, ctx->pci);
  413. return false;
  414. }
  415. auto ptr_type = type->getPointeeType ().split ().Ty;
  416. if (!ptr_type->isCharType ()) {
  417. /* We might have gchar * here */
  418. auto desugared_type = ptr_type->getUnqualifiedDesugaredType ();
  419. auto desugared_ptr_type = type->getUnqualifiedDesugaredType ();
  420. if (!desugared_type || (!desugared_type->isCharType () &&
  421. !desugared_ptr_type->isVoidPointerType ())) {
  422. if (desugared_type) {
  423. desugared_type->dump ();
  424. }
  425. print_error (
  426. std::string ("bad string argument for %s: ") +
  427. arg->getType ().getAsString (),
  428. arg, ctx->past, ctx->pci);
  429. return false;
  430. }
  431. }
  432. return true;
  433. }
  434. static bool
  435. check_builtin_type (const Expr *arg, struct PrintfArgChecker *ctx,
  436. const std::vector <BuiltinType::Kind> &k, const std::string &fmt)
  437. {
  438. auto type = arg->getType ().split ().Ty;
  439. auto desugared_type = type->getUnqualifiedDesugaredType ();
  440. if (!desugared_type->isBuiltinType ()) {
  441. print_error (
  442. std::string ("not a builtin type for ") + fmt + " arg: " +
  443. arg->getType ().getAsString (),
  444. arg, ctx->past, ctx->pci);
  445. return false;
  446. }
  447. auto builtin_type = dyn_cast<BuiltinType> (desugared_type);
  448. auto kind = builtin_type->getKind ();
  449. auto found = false;
  450. for (auto kk : k) {
  451. if (kind == kk) {
  452. found = true;
  453. break;
  454. }
  455. }
  456. if (!found) {
  457. print_error (
  458. std::string ("bad argument for ") + fmt + " arg: " +
  459. arg->getType ().getAsString () + ", resolved as: " +
  460. builtin_type->getNameAsCString (ctx->past->getPrintingPolicy ()),
  461. arg, ctx->past, ctx->pci);
  462. return false;
  463. }
  464. return true;
  465. }
  466. static bool
  467. int_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  468. {
  469. return check_builtin_type (arg,
  470. ctx,
  471. {BuiltinType::Kind::UInt,
  472. BuiltinType::Kind::Int},
  473. "%d or *");
  474. }
  475. static bool
  476. long_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  477. {
  478. return check_builtin_type (arg,
  479. ctx,
  480. {BuiltinType::Kind::ULong,
  481. BuiltinType::Kind::Long},
  482. "%l");
  483. }
  484. static bool
  485. char_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  486. {
  487. return check_builtin_type (arg,
  488. ctx,
  489. {BuiltinType::Kind::UChar,
  490. BuiltinType::Kind::SChar,
  491. BuiltinType::Kind::Int}, // Because of char -> int propagation
  492. "%c");
  493. }
  494. static bool
  495. size_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  496. {
  497. if (sizeof (size_t) == sizeof (long)) {
  498. return check_builtin_type (arg,
  499. ctx,
  500. {BuiltinType::Kind::ULong,
  501. BuiltinType::Kind::Long},
  502. "%z");
  503. }
  504. else if (sizeof (size_t) == sizeof (int)) {
  505. return check_builtin_type (arg,
  506. ctx,
  507. {BuiltinType::Kind::UInt,
  508. BuiltinType::Kind::Int},
  509. "%z");
  510. }
  511. else {
  512. assert (0);
  513. }
  514. return true;
  515. }
  516. static bool
  517. double_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  518. {
  519. return check_builtin_type (arg,
  520. ctx,
  521. {BuiltinType::Kind::Double},
  522. "%f or %g");
  523. }
  524. static bool
  525. long_double_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  526. {
  527. return check_builtin_type (arg,
  528. ctx,
  529. {BuiltinType::Kind::LongDouble},
  530. "%F or %G");
  531. }
  532. static bool
  533. pid_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  534. {
  535. if (sizeof (pid_t) == sizeof (long)) {
  536. return check_builtin_type (arg,
  537. ctx,
  538. {BuiltinType::Kind::ULong,
  539. BuiltinType::Kind::Long},
  540. "%P");
  541. }
  542. else if (sizeof (pid_t) == sizeof (int)) {
  543. return check_builtin_type (arg,
  544. ctx,
  545. {BuiltinType::Kind::UInt,
  546. BuiltinType::Kind::Int},
  547. "%P");
  548. }
  549. else {
  550. assert (0);
  551. }
  552. }
  553. static bool
  554. time_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  555. {
  556. if (sizeof (time_t) == sizeof (long)) {
  557. return check_builtin_type (arg,
  558. ctx,
  559. {BuiltinType::Kind::ULong,
  560. BuiltinType::Kind::Long},
  561. "%t");
  562. }
  563. else if (sizeof (time_t) == sizeof (int)) {
  564. return check_builtin_type (arg,
  565. ctx,
  566. {BuiltinType::Kind::UInt,
  567. BuiltinType::Kind::Int},
  568. "%t");
  569. }
  570. else {
  571. assert (0);
  572. }
  573. }
  574. static bool
  575. pointer_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  576. {
  577. auto type = arg->getType ().split ().Ty;
  578. if (!type->isPointerType ()) {
  579. print_error (
  580. std::string ("bad pointer argument for %p: ") +
  581. arg->getType ().getAsString (),
  582. arg, ctx->past, ctx->pci);
  583. return false;
  584. }
  585. return true;
  586. }
  587. static bool
  588. int64_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  589. {
  590. std::vector <BuiltinType::Kind> check;
  591. if (sizeof (int64_t) == sizeof (long long)) {
  592. check.push_back (BuiltinType::Kind::ULongLong);
  593. check.push_back (BuiltinType::Kind::LongLong);
  594. }
  595. if (sizeof (int64_t) == sizeof (long)) {
  596. check.push_back (BuiltinType::Kind::ULong);
  597. check.push_back (BuiltinType::Kind::Long);
  598. }
  599. return check_builtin_type (arg,
  600. ctx,
  601. check,
  602. "%L");
  603. return true;
  604. }
  605. static bool
  606. int32_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  607. {
  608. std::vector < BuiltinType::Kind> check;
  609. if (sizeof (int32_t) == sizeof (long)) {
  610. check.push_back (BuiltinType::Kind::ULong);
  611. check.push_back (BuiltinType::Kind::Long);
  612. }
  613. if (sizeof (int32_t) == sizeof (int)) {
  614. check.push_back (BuiltinType::Kind::UInt);
  615. check.push_back (BuiltinType::Kind::Int);
  616. }
  617. return check_builtin_type (arg,
  618. ctx,
  619. check,
  620. "%D");
  621. return true;
  622. }
  623. static bool
  624. check_struct_type (const Expr *arg, struct PrintfArgChecker *ctx,
  625. const std::string &sname, const std::string &fmt)
  626. {
  627. auto type = arg->getType ().split ().Ty;
  628. if (!type->isPointerType ()) {
  629. print_error (
  630. std::string ("bad string argument for %s: ") +
  631. arg->getType ().getAsString (),
  632. arg, ctx->past, ctx->pci);
  633. return false;
  634. }
  635. auto ptr_type = type->getPointeeType ().split ().Ty;
  636. auto desugared_type = ptr_type->getUnqualifiedDesugaredType ();
  637. if (!desugared_type->isRecordType ()) {
  638. print_error (
  639. std::string ("not a record type for ") + fmt + " arg: " +
  640. arg->getType ().getAsString (),
  641. arg, ctx->past, ctx->pci);
  642. return false;
  643. }
  644. auto struct_type = desugared_type->getAsStructureType ();
  645. auto struct_decl = struct_type->getDecl ();
  646. auto struct_def = struct_decl->getNameAsString ();
  647. if (struct_def != sname) {
  648. print_error (std::string ("bad argument '") + struct_def + "' for "
  649. + fmt + " arg: " +
  650. arg->getType ().getAsString (),
  651. arg, ctx->past, ctx->pci);
  652. return false;
  653. }
  654. return true;
  655. }
  656. static bool
  657. tok_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  658. {
  659. return check_struct_type (arg,
  660. ctx,
  661. "f_str_tok",
  662. "%T");
  663. }
  664. static bool
  665. fstring_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  666. {
  667. return check_struct_type (arg,
  668. ctx,
  669. "f_str_s",
  670. "%V");
  671. }
  672. static bool
  673. gstring_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  674. {
  675. return check_struct_type (arg,
  676. ctx,
  677. "_GString",
  678. "%v");
  679. }
  680. static bool
  681. gerr_arg_handler (const Expr *arg, struct PrintfArgChecker *ctx)
  682. {
  683. return check_struct_type (arg,
  684. ctx,
  685. "_GError",
  686. "%e");
  687. }
  688. }