GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — simplecpp/simplecpp.cpp at master · chrchr-github/simplecpp Expand file tree/ Copy pathsimplecpp.cppMore file actions 3953 lines (3516 loc) · 148 KB / Copy pathsimplecpp.cppFile metadata and controls3953 lines (3516 loc) · 148 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 /* * simplecpp - A simple and high-fidelity C/C++ preprocessor library * Copyright (C) 2016-2023 simplecpp team */ #if defined(_WIN32) # ifndef _WIN32_WINNT # define _WIN32_WINNT 0x0602 # endif # ifndef NOMINMAX # define NOMINMAX # endif # ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN # endif # include <windows.h> # undef ERROR #endif #include "simplecpp.h" #if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) # define SIMPLECPP_WINDOWS #endif #include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cstddef> // IWYU pragma: keep #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iostream> #include <istream> #include <limits> #include <list> #include <map> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <string> #ifdef SIMPLECPP_WINDOWS # include <mutex> #endif #include <unordered_map> #include <utility> #include <vector> #ifdef _WIN32 # include <direct.h> #else # include <sys/stat.h> #endif static bool isHex(const std::string &s) { return s.size()>2 && (s.compare(0,2,"0x")==0 || s.compare(0,2,"0X")==0); } static bool isOct(const std::string &s) { return s.size()>1 && (s[0]=='0') && (s[1] >= '0') && (s[1] < '8'); } static bool isStringLiteral(const std::string &s) { return s.size() > 1 && (s[0]=='\"') && (*s.rbegin()=='\"'); } static bool isCharLiteral(const std::string &s) { // char literal patterns can include 'a', '\t', '\000', '\xff', 'abcd', and maybe '' // This only checks for the surrounding '' but doesn't parse the content. return s.size() > 1 && (s[0]=='\'') && (*s.rbegin()=='\''); } static const simplecpp::TokenString DEFINE("define"); static const simplecpp::TokenString UNDEF("undef"); static const simplecpp::TokenString INCLUDE("include"); static const simplecpp::TokenString ERROR("error"); static const simplecpp::TokenString WARNING("warning"); static const simplecpp::TokenString IF("if"); static const simplecpp::TokenString IFDEF("ifdef"); static const simplecpp::TokenString IFNDEF("ifndef"); static const simplecpp::TokenString DEFINED("defined"); static const simplecpp::TokenString ELSE("else"); static const simplecpp::TokenString ELIF("elif"); static const simplecpp::TokenString ENDIF("endif"); static const simplecpp::TokenString PRAGMA("pragma"); static const simplecpp::TokenString ONCE("once"); static const simplecpp::TokenString HAS_INCLUDE("__has_include"); template<class T> static std::string toString(T t) { // NOLINTNEXTLINE(misc-const-correctness) - false positive std::ostringstream ostr; ostr << t; return ostr.str(); } #ifdef SIMPLECPP_DEBUG_MACRO_EXPANSION static std::string locstring(const simplecpp::Location &loc) { std::ostringstream ostr; ostr << '[' << loc.file() << ':' << loc.line << ':' << loc.col << ']'; return ostr.str(); } #endif static long long stringToLL(const std::string &s) { long long ret; const bool hex = isHex(s); const bool oct = isOct(s); std::istringstream istr(hex ? s.substr(2) : oct ? s.substr(1) : s); if (hex) istr >> std::hex; else if (oct) istr >> std::oct; istr >> ret; return ret; } static unsigned long long stringToULL(const std::string &s) { unsigned long long ret; const bool hex = isHex(s); const bool oct = isOct(s); std::istringstream istr(hex ? s.substr(2) : oct ? s.substr(1) : s); if (hex) istr >> std::hex; else if (oct) istr >> std::oct; istr >> ret; return ret; } static bool endsWith(const std::string &s, const std::string &e) { return (s.size() >= e.size()) && std::equal(e.rbegin(), e.rend(), s.rbegin()); } static bool sameline(const simplecpp::Token *tok1, const simplecpp::Token *tok2) { return tok1 && tok2 && tok1->location.sameline(tok2->location); } static bool isAlternativeBinaryOp(const simplecpp::Token *tok, const std::string &alt) { return (tok->name && tok->str() == alt && tok->previous && tok->next && (tok->previous->number || tok->previous->name || tok->previous->op == ')') && (tok->next->number || tok->next->name || tok->next->op == '(')); } static bool isAlternativeUnaryOp(const simplecpp::Token *tok, const std::string &alt) { return ((tok->name && tok->str() == alt) && (!tok->previous || tok->previous->op == '(') && (tok->next && (tok->next->name || tok->next->number))); } static std::string replaceAll(std::string s, const std::string& from, const std::string& to) { for (size_t pos = s.find(from); pos != std::string::npos; pos = s.find(from, pos + to.size())) s.replace(pos, from.size(), to); return s; } void simplecpp::Location::adjust(const std::string &str) { if (strpbrk(str.c_str(), "\r\n") == nullptr) { col += str.size(); return; } for (std::size_t i = 0U; i < str.size(); ++i) { col++; if (str[i] == '\n' || str[i] == '\r') { col = 1; line++; if (str[i] == '\r' && (i+1)<str.size() && str[i+1]=='\n') ++i; } } } bool simplecpp::Token::isOneOf(const char ops[]) const { return (op != '\0') && (std::strchr(ops, op) != nullptr); } bool simplecpp::Token::startsWithOneOf(const char c[]) const { return std::strchr(c, string[0]) != nullptr; } bool simplecpp::Token::endsWithOneOf(const char c[]) const { return std::strchr(c, string[string.size() - 1U]) != nullptr; } void simplecpp::Token::printAll() const { const Token *tok = this; while (tok->previous) tok = tok->previous; for (; tok; tok = tok->next) { if (tok->previous) { std::cout << (sameline(tok, tok->previous) ? ' ' : '\n'); } std::cout << tok->str(); } std::cout << std::endl; } void simplecpp::Token::printOut() const { for (const Token *tok = this; tok; tok = tok->next) { if (tok != this) { std::cout << (sameline(tok, tok->previous) ? ' ' : '\n'); } std::cout << tok->str(); } std::cout << std::endl; } // cppcheck-suppress noConstructor - we call init() in the inherited to initialize the private members class simplecpp::TokenList::Stream { public: virtual ~Stream() = default; virtual int get() = 0; virtual int peek() = 0; virtual void unget() = 0; virtual bool good() = 0; unsigned char readChar() { auto ch = static_cast<unsigned char>(get()); // For UTF-16 encoded files the BOM is 0xfeff/0xfffe. If the // character is non-ASCII character then replace it with 0xff if (isUtf16) { const auto ch2 = static_cast<unsigned char>(get()); const int ch16 = makeUtf16Char(ch, ch2); ch = static_cast<unsigned char>(((ch16 >= 0x80) ? 0xff : ch16)); } // Handling of newlines.. if (ch == '\r') { ch = '\n'; int ch2 = get(); if (isUtf16) { const int c2 = get(); ch2 = makeUtf16Char(ch2, c2); } if (ch2 != '\n') ungetChar(); } return ch; } unsigned char peekChar() { auto ch = static_cast<unsigned char>(peek()); // For UTF-16 encoded files the BOM is 0xfeff/0xfffe. If the // character is non-ASCII character then replace it with 0xff if (isUtf16) { (void)get(); const auto ch2 = static_cast<unsigned char>(peek()); unget(); const int ch16 = makeUtf16Char(ch, ch2); ch = static_cast<unsigned char>(((ch16 >= 0x80) ? 0xff : ch16)); } // Handling of newlines.. if (ch == '\r') ch = '\n'; return ch; } void ungetChar() { unget(); if (isUtf16) unget(); } protected: void init() { // initialize since we use peek() in getAndSkipBOM() isUtf16 = false; bom = getAndSkipBOM(); isUtf16 = (bom == 0xfeff || bom == 0xfffe); } private: inline int makeUtf16Char(const unsigned char ch, const unsigned char ch2) const { return (bom == 0xfeff) ? (ch<<8 | ch2) : (ch2<<8 | ch); } unsigned short getAndSkipBOM() { const int ch1 = peek(); // The UTF-16 BOM is 0xfffe or 0xfeff. if (ch1 >= 0xfe) { (void)get(); const unsigned short byte = (static_cast<unsigned char>(ch1) << 8); if (peek() >= 0xfe) return byte | static_cast<unsigned char>(get()); unget(); return 0; } // Skip UTF-8 BOM 0xefbbbf if (ch1 == 0xef) { (void)get(); if (peek() == 0xbb) { (void)get(); if (peek() == 0xbf) { (void)get(); return 0; } unget(); } unget(); } return 0; } unsigned short bom; protected: bool isUtf16; }; namespace { class StdIStream : public simplecpp::TokenList::Stream { public: // cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members explicit StdIStream(std::istream &istr) : istr(istr) { assert(istr.good()); init(); } int get() override { return istr.get(); } int peek() override { return istr.peek(); } void unget() override { istr.unget(); } bool good() override { return istr.good(); } private: std::istream &istr; }; class StdCharBufStream : public simplecpp::TokenList::Stream { public: // cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members StdCharBufStream(const unsigned char* str, std::size_t size) : str(str) , size(size) { init(); } int get() override { if (pos >= size) return lastStatus = EOF; return str[pos++]; } int peek() override { if (pos >= size) return lastStatus = EOF; return str[pos]; } void unget() override { --pos; } bool good() override { return lastStatus != EOF; } private: const unsigned char *str; const std::size_t size; std::size_t pos{}; int lastStatus{}; }; class FileStream : public simplecpp::TokenList::Stream { public: /** * @throws simplecpp::Output thrown if file is not found */ // cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members explicit FileStream(const std::string &filename, std::vector<std::string> &files) : file(fopen(filename.c_str(), "rb")) { if (!file) { files.emplace_back(filename); throw simplecpp::Output(simplecpp::Output::FILE_NOT_FOUND, {}, "File is missing: " + filename); } init(); } FileStream(const FileStream&) = delete; FileStream &operator=(const FileStream&) = delete; ~FileStream() override { fclose(file); file = nullptr; } int get() override { lastStatus = lastCh = fgetc(file); return lastCh; } int peek() override { // keep lastCh intact const int ch = fgetc(file); unget_internal(ch); return ch; } void unget() override { unget_internal(lastCh); } bool good() override { return lastStatus != EOF; } private: void unget_internal(int ch) { if (isUtf16) { // TODO: use ungetc() as well // UTF-16 has subsequent unget() calls fseek(file, -1, SEEK_CUR); } else { ungetc(ch, file); } } FILE *file; int lastCh{}; int lastStatus{}; }; } simplecpp::TokenList::TokenList(std::vector<std::string> &filenames) : frontToken(nullptr), backToken(nullptr), files(filenames) {} simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList) : frontToken(nullptr), backToken(nullptr), files(filenames) { StdIStream stream(istr); readfile(stream,filename,outputList); } simplecpp::TokenList::TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList, int /*unused*/) : frontToken(nullptr), backToken(nullptr), files(filenames) { StdCharBufStream stream(data, size); readfile(stream,filename,outputList); } simplecpp::TokenList::TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList) : frontToken(nullptr), backToken(nullptr), files(filenames) { try { FileStream stream(filename, filenames); readfile(stream,filename,outputList); } catch (const simplecpp::Output & e) { outputList->emplace_back(e); } } simplecpp::TokenList::TokenList(const TokenList &other) : frontToken(nullptr), backToken(nullptr), files(other.files) { *this = other; } simplecpp::TokenList::TokenList(TokenList &&other) : frontToken(nullptr), backToken(nullptr), files(other.files) { *this = std::move(other); } simplecpp::TokenList::~TokenList() { clear(); } simplecpp::TokenList &simplecpp::TokenList::operator=(const TokenList &other) { if (this != &other) { clear(); files = other.files; for (const Token *tok = other.cfront(); tok; tok = tok->next) push_back(new Token(*tok)); sizeOfType = other.sizeOfType; } return *this; } simplecpp::TokenList &simplecpp::TokenList::operator=(TokenList &&other) { if (this != &other) { clear(); frontToken = other.frontToken; other.frontToken = nullptr; backToken = other.backToken; other.backToken = nullptr; files = other.files; sizeOfType = std::move(other.sizeOfType); } return *this; } void simplecpp::TokenList::clear() { backToken = nullptr; while (frontToken) { Token * const next = frontToken->next; delete frontToken; frontToken = next; } sizeOfType.clear(); } void simplecpp::TokenList::push_back(Token *tok) { if (!frontToken) frontToken = tok; else backToken->next = tok; tok->previous = backToken; backToken = tok; } void simplecpp::TokenList::dump(bool linenrs) const { std::cout << stringify(linenrs) << std::endl; } std::string simplecpp::TokenList::stringify(bool linenrs) const { std::ostringstream ret; Location loc; loc.line = 1; bool filechg = true; for (const Token *tok = cfront(); tok; tok = tok->next) { if (tok->location.line < loc.line || tok->location.fileIndex != loc.fileIndex) { ret << "\n#line " << tok->location.line << " \"" << file(tok->location) << "\"\n"; loc = tok->location; filechg = true; } if (linenrs && filechg) { ret << loc.line << ": "; filechg = false; } while (tok->location.line > loc.line) { ret << '\n'; loc.line++; if (linenrs) ret << loc.line << ": "; } if (sameline(tok->previous, tok)) ret << ' '; ret << tok->str(); loc.adjust(tok->str()); } return ret.str(); } static bool isNameChar(unsigned char ch) { return std::isalnum(ch) || ch == '_' || ch == '$'; } static std::string escapeString(const std::string &str) { std::ostringstream ostr; ostr << '\"'; for (std::size_t i = 1U; i < str.size() - 1; ++i) { const char c = str[i]; if (c == '\\' || c == '\"' || c == '\'') ostr << '\\'; ostr << c; } ostr << '\"'; return ostr.str(); } static void portabilityBackslash(simplecpp::OutputList *outputList, const simplecpp::Location &location) { if (!outputList) return; simplecpp::Output err{ simplecpp::Output::PORTABILITY_BACKSLASH, location, "Combination 'backslash space newline' is not portable." }; outputList->emplace_back(std::move(err)); } static bool isStringLiteralPrefix(const std::string &str) { return str == "u" || str == "U" || str == "L" || str == "u8" || str == "R" || str == "uR" || str == "UR" || str == "LR" || str == "u8R"; } void simplecpp::TokenList::lineDirective(unsigned int fileIndex, unsigned int line, Location &location) { if (fileIndex != location.fileIndex || line >= location.line) { location.fileIndex = fileIndex; location.line = line; return; } if (line + 2 >= location.line) { location.line = line; while (cback()->op != '#') deleteToken(back()); deleteToken(back()); return; } } static const std::string COMMENT_END("*/"); void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename, OutputList *outputList) { std::stack<simplecpp::Location> loc; unsigned int multiline = 0U; const Token *oldLastToken = nullptr; Location location(fileIndex(filename), 1, 1); while (stream.good()) { unsigned char ch = stream.readChar(); if (!stream.good()) break; if (ch >= 0x80) { if (outputList) { simplecpp::Output err{ simplecpp::Output::UNHANDLED_CHAR_ERROR, location, "The code contains unhandled character(s) (character code=" + std::to_string(static_cast<int>(ch)) + "). Neither unicode nor extended ascii is supported." }; outputList->emplace_back(std::move(err)); } clear(); return; } if (ch == '\n') { if (cback() && cback()->op == '\\') { if (location.col > cback()->location.col + 1U) portabilityBackslash(outputList, cback()->location); ++multiline; deleteToken(back()); } else { location.line += multiline + 1; multiline = 0U; } if (!multiline) location.col = 1; if (oldLastToken != cback()) { oldLastToken = cback(); const Token * const llTok = isLastLinePreprocessor(); if (!llTok) continue; const Token * const llNextToken = llTok->next; if (!llTok->next) continue; if (llNextToken->next) { // #file "file.c" if (llNextToken->str() == "file" && llNextToken->next->str()[0] == '\"') { const Token *strtok = cback(); while (strtok->comment) strtok = strtok->previous; loc.push(location); location.fileIndex = fileIndex(strtok->str().substr(1U, strtok->str().size() - 2U)); location.line = 1U; } // TODO: add support for "# 3" // #3 "file.c" // #line 3 "file.c" else if ((llNextToken->number && llNextToken->next->str()[0] == '\"') || (llNextToken->str() == "line" && llNextToken->next->number && llNextToken->next->next && llNextToken->next->next->str()[0] == '\"')) { const Token *strtok = cback(); while (strtok->comment) strtok = strtok->previous; const Token *numtok = strtok->previous; while (numtok->comment) numtok = numtok->previous; lineDirective(fileIndex(replaceAll(strtok->str().substr(1U, strtok->str().size() - 2U),"\\\\","\\")), std::atol(numtok->str().c_str()), location); } // #line 3 else if (llNextToken->str() == "line" && llNextToken->next->number) { const Token *numtok = cback(); while (numtok->comment) numtok = numtok->previous; lineDirective(location.fileIndex, std::atol(numtok->str().c_str()), location); } } // #endfile else if (llNextToken->str() == "endfile" && !loc.empty()) { location = loc.top(); loc.pop(); } } continue; } if (ch <= ' ') { location.col++; continue; } TokenString currentToken; if (cback() && cback()->location.line == location.line && cback()->previous && cback()->previous->op == '#') { const Token* const ppTok = cback()->previous; if (ppTok->next && (ppTok->next->str() == "error" || ppTok->next->str() == "warning")) { char prev = ' '; while (stream.good() && (prev == '\\' || (ch != '\r' && ch != '\n'))) { currentToken += ch; prev = ch; ch = stream.readChar(); } stream.ungetChar(); push_back(new Token(currentToken, location)); location.adjust(currentToken); continue; } } // number or name if (isNameChar(ch)) { const bool num = !!std::isdigit(ch); while (stream.good() && isNameChar(ch)) { currentToken += ch; ch = stream.readChar(); if (num && ch=='\'' && isNameChar(stream.peekChar())) ch = stream.readChar(); } stream.ungetChar(); } // comment else if (ch == '/' && stream.peekChar() == '/') { while (stream.good() && ch != '\n') { currentToken += ch; ch = stream.readChar(); if (ch == '\\') { TokenString tmp; char tmp_ch = ch; while ((stream.good()) && (tmp_ch == '\\' || tmp_ch == ' ' || tmp_ch == '\t')) { tmp += tmp_ch; tmp_ch = stream.readChar(); } if (!stream.good()) { break; } if (tmp_ch != '\n') { currentToken += tmp; } else { const TokenString check_portability = currentToken + tmp; const std::string::size_type pos = check_portability.find_last_not_of(" \t"); if (pos < check_portability.size() - 1U && check_portability[pos] == '\\') portabilityBackslash(outputList, location); ++multiline; tmp_ch = stream.readChar(); currentToken += '\n'; } ch = tmp_ch; } } if (ch == '\n') { stream.ungetChar(); } } // comment else if (ch == '/' && stream.peekChar() == '*') { currentToken = "/*"; (void)stream.readChar(); ch = stream.readChar(); while (stream.good()) { currentToken += ch; if (currentToken.size() >= 4U && endsWith(currentToken, COMMENT_END)) break; ch = stream.readChar(); } // multiline.. std::string::size_type pos = 0; while ((pos = currentToken.find("\\\n",pos)) != std::string::npos) { currentToken.erase(pos,2); ++multiline; } if (multiline || isLastLinePreprocessor()) { pos = 0; while ((pos = currentToken.find('\n',pos)) != std::string::npos) { currentToken.erase(pos,1); ++multiline; } } } // string / char literal else if (ch == '\"' || ch == '\'') { std::string prefix; if (cback() && cback()->name && isStringLiteralPrefix(cback()->str()) && ((cback()->location.col + cback()->str().size()) == location.col) && (cback()->location.line == location.line)) { prefix = cback()->str(); } // C++11 raw string literal if (ch == '\"' && !prefix.empty() && *cback()->str().rbegin() == 'R') { std::string delim; currentToken = ch; prefix.resize(prefix.size() - 1); ch = stream.readChar(); while (stream.good() && ch != '(' && ch != '\n') { delim += ch; ch = stream.readChar(); } if (!stream.good() || ch == '\n') { if (outputList) { Output err{ Output::SYNTAX_ERROR, location, "Invalid newline in raw string delimiter." }; outputList->emplace_back(std::move(err)); } return; } const std::string endOfRawString(')' + delim + currentToken); while (stream.good() && (!endsWith(currentToken, endOfRawString) || currentToken.size() <= 1)) currentToken += stream.readChar(); if (!endsWith(currentToken, endOfRawString)) { if (outputList) { Output err{ Output::SYNTAX_ERROR, location, "Raw string missing terminating delimiter." }; outputList->emplace_back(std::move(err)); } return; } currentToken.erase(currentToken.size() - endOfRawString.size(), endOfRawString.size() - 1U); currentToken = escapeString(currentToken); currentToken.insert(0, prefix); back()->setstr(currentToken); location.adjust(currentToken); if (currentToken.find_first_of("\r\n") == std::string::npos) location.col += 2 + (2 * delim.size()); else location.col += 1 + delim.size(); continue; } currentToken = readUntil(stream,location,ch,ch,outputList); if (currentToken.size() < 2U) // Error is reported by readUntil() return; std::string s = currentToken; std::string::size_type pos; int newlines = 0; while ((pos = s.find_first_of("\r\n")) != std::string::npos) { s.erase(pos,1); newlines++; } if (prefix.empty()) push_back(new Token(s, location, !!std::isspace(stream.peekChar()))); // push string without newlines else back()->setstr(prefix + s); if (newlines > 0) { const Token * const llTok = lastLineTok(); if (llTok && llTok->op == '#' && llTok->next && (llTok->next->str() == "define" || llTok->next->str() == "pragma") && llTok->next->next) { multiline += newlines; location.adjust(s); continue; } } location.adjust(currentToken); continue; } else { currentToken += ch; } if (*currentToken.begin() == '<') { const Token * const llTok = lastLineTok(); if (llTok && llTok->op == '#' && llTok->next && llTok->next->str() == "include") { currentToken = readUntil(stream, location, '<', '>', outputList); if (currentToken.size() < 2U) return; } } push_back(new Token(currentToken, location, !!std::isspace(stream.peekChar()))); if (multiline) location.col += currentToken.size(); else location.adjust(currentToken); } combineOperators(); } void simplecpp::TokenList::constFold() { while (cfront()) { // goto last '(' Token *tok = back(); while (tok && tok->op != '(') tok = tok->previous; // no '(', goto first token if (!tok) tok = front(); // Constant fold expression constFoldUnaryNotPosNeg(tok); constFoldMulDivRem(tok); constFoldAddSub(tok); constFoldShift(tok); constFoldComparison(tok); constFoldBitwise(tok); constFoldLogicalOp(tok); constFoldQuestionOp(tok); // If there is no '(' we are done with the constant folding if (tok->op != '(') break; if (!tok->next || !tok->next->next || tok->next->next->op != ')') break; tok = tok->next; deleteToken(tok->previous); deleteToken(tok->next); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |