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

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