* Parse format string @a format used in functions such as @c printf or @c scanf * into vector of data types in context of module @a module. * If @a calledFnc provided and called function name contains "scan" string, all * types are transformed to pointers. * @return Vector of data types used in format string. * * This is done according to: * http://www.cplusplus.com/reference/cstdio/printf/
| 421 | * http://www.cplusplus.com/reference/cstdio/scanf/ |
| 422 | */ |
| 423 | std::vector<llvm::Type*> parseFormatString( |
| 424 | llvm::Module* module, |
| 425 | const std::string& format, |
| 426 | llvm::Function* calledFnc) |
| 427 | { |
| 428 | LLVMContext& ctx = module->getContext(); |
| 429 | std::vector<Type*> ret; |
| 430 | |
| 431 | const char *cp = format.c_str(); |
| 432 | size_t max_width_length = 0; |
| 433 | size_t max_precision_length = 0; |
| 434 | |
| 435 | while (*cp != '\0') |
| 436 | { |
| 437 | char c = *cp++; |
| 438 | if (c != '%') |
| 439 | { |
| 440 | continue; |
| 441 | } |
| 442 | |
| 443 | // Test for positional argument. |
| 444 | // |
| 445 | if (*cp >= '0' && *cp <= '9') |
| 446 | { |
| 447 | const char *np; |
| 448 | |
| 449 | for (np = cp; *np >= '0' && *np <= '9'; np++) {}; |
| 450 | |
| 451 | if (*np == '$') |
| 452 | { |
| 453 | size_t n = 0; |
| 454 | for (np = cp; *np >= '0' && *np <= '9'; np++) |
| 455 | { |
| 456 | n += n*10 + *np - '0'; |
| 457 | } |
| 458 | if (n == 0) // Positional argument 0. |
| 459 | { |
| 460 | return ret; |
| 461 | } |
| 462 | cp = np + 1; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | // Read the flags. |
| 467 | // |
| 468 | for (;;) |
| 469 | { |
| 470 | if (*cp == '\'') |
| 471 | { |
| 472 | cp++; |
| 473 | } |
| 474 | else if (*cp == '-') |
| 475 | { |
| 476 | cp++; |
| 477 | } |
| 478 | else if (*cp == '+') |
| 479 | { |
| 480 | cp++; |