|
17 | 17 | */ |
18 | 18 |
|
19 | 19 | #if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) |
| 20 | +#define SIMPLECPP_WINDOWS |
20 | 21 | #define NOMINMAX |
21 | 22 | #endif |
22 | 23 | #include "simplecpp.h" |
|
33 | 34 | #include <stdexcept> |
34 | 35 | #include <utility> |
35 | 36 |
|
36 | | -#if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) |
| 37 | +#ifdef SIMPLECPP_WINDOWS |
37 | 38 | #include <windows.h> |
38 | 39 | #undef ERROR |
39 | 40 | #undef TRUE |
40 | | -#define SIMPLECPP_WINDOWS |
41 | 41 | #endif |
42 | 42 |
|
43 | 43 | static bool isHex(const std::string &s) |
@@ -338,7 +338,7 @@ static unsigned short getAndSkipBOM(std::istream &istr) |
338 | 338 |
|
339 | 339 | // Skip UTF-8 BOM 0xefbbbf |
340 | 340 | if (ch1 == 0xef) { |
341 | | - istr.get(); |
| 341 | + (void)istr.get(); |
342 | 342 | if (istr.get() == 0xbb && istr.peek() == 0xbf) { |
343 | 343 | (void)istr.get(); |
344 | 344 | } else { |
@@ -699,17 +699,24 @@ void simplecpp::TokenList::combineOperators() |
699 | 699 | } |
700 | 700 | } |
701 | 701 |
|
| 702 | +static const std::string COMPL("compl"); |
702 | 703 | static const std::string NOT("not"); |
703 | 704 | void simplecpp::TokenList::constFoldUnaryNotPosNeg(simplecpp::Token *tok) |
704 | 705 | { |
705 | 706 | for (; tok && tok->op != ')'; tok = tok->next) { |
706 | 707 | // "not" might be ! |
707 | 708 | if (isAlternativeUnaryOp(tok, NOT)) |
708 | 709 | tok->op = '!'; |
| 710 | + // "compl" might be ~ |
| 711 | + else if (isAlternativeUnaryOp(tok, COMPL)) |
| 712 | + tok->op = '~'; |
709 | 713 |
|
710 | 714 | if (tok->op == '!' && tok->next && tok->next->number) { |
711 | 715 | tok->setstr(tok->next->str == "0" ? "1" : "0"); |
712 | 716 | deleteToken(tok->next); |
| 717 | + } else if (tok->op == '~' && tok->next && tok->next->number) { |
| 718 | + tok->setstr(toString(~stringToLL(tok->next->str))); |
| 719 | + deleteToken(tok->next); |
713 | 720 | } else { |
714 | 721 | if (tok->previous && (tok->previous->number || tok->previous->name)) |
715 | 722 | continue; |
@@ -1662,7 +1669,12 @@ namespace simplecpp { |
1662 | 1669 |
|
1663 | 1670 | if (varargs && tokensB.empty() && tok->previous->str == ",") |
1664 | 1671 | output->deleteToken(A); |
1665 | | - else { |
| 1672 | + else if (strAB != "," && macros.find(strAB) == macros.end()) { |
| 1673 | + A->setstr(strAB); |
| 1674 | + for (Token *b = tokensB.front(); b; b = b->next) |
| 1675 | + b->location = loc; |
| 1676 | + output->takeTokens(tokensB); |
| 1677 | + } else { |
1666 | 1678 | output->deleteToken(A); |
1667 | 1679 | TokenList tokens(files); |
1668 | 1680 | tokens.push_back(new Token(strAB, tok->location)); |
@@ -1738,15 +1750,11 @@ static bool realFileName(const std::string &f, std::string *result) |
1738 | 1750 | if (!alpha) |
1739 | 1751 | return false; |
1740 | 1752 |
|
1741 | | - // Convert char path to CHAR path |
1742 | | - std::vector<CHAR> buf(f.size()+1U, 0); |
1743 | | - for (unsigned int i = 0; i < f.size(); ++i) |
1744 | | - buf[i] = f[i]; |
1745 | | - |
1746 | 1753 | // Lookup filename or foldername on file system |
1747 | 1754 | WIN32_FIND_DATAA FindFileData; |
1748 | | - HANDLE hFind = FindFirstFileA(&buf[0], &FindFileData); |
1749 | | - if (hFind == INVALID_HANDLE_VALUE) |
| 1755 | + HANDLE hFind = FindFirstFileExA(f.c_str(), FindExInfoBasic, &FindFileData, FindExSearchNameMatch, NULL, 0); |
| 1756 | + |
| 1757 | + if (INVALID_HANDLE_VALUE == hFind) |
1750 | 1758 | return false; |
1751 | 1759 | *result = FindFileData.cFileName; |
1752 | 1760 | FindClose(hFind); |
@@ -1821,6 +1829,9 @@ namespace simplecpp { |
1821 | 1829 | */ |
1822 | 1830 | std::string simplifyPath(std::string path) |
1823 | 1831 | { |
| 1832 | + if (path.empty()) |
| 1833 | + return path; |
| 1834 | + |
1824 | 1835 | std::string::size_type pos; |
1825 | 1836 |
|
1826 | 1837 | // replace backslash separators |
@@ -1929,15 +1940,15 @@ static void simplifySizeof(simplecpp::TokenList &expr, const std::map<std::strin |
1929 | 1940 | } |
1930 | 1941 | } |
1931 | 1942 |
|
1932 | | -static const char * const altopData[] = {"and","or","bitand","bitor","not","not_eq","xor"}; |
1933 | | -static const std::set<std::string> altop(&altopData[0], &altopData[7]); |
| 1943 | +static const char * const altopData[] = {"and","or","bitand","bitor","compl","not","not_eq","xor"}; |
| 1944 | +static const std::set<std::string> altop(&altopData[0], &altopData[8]); |
1934 | 1945 | static void simplifyName(simplecpp::TokenList &expr) |
1935 | 1946 | { |
1936 | 1947 | for (simplecpp::Token *tok = expr.front(); tok; tok = tok->next) { |
1937 | 1948 | if (tok->name) { |
1938 | 1949 | if (altop.find(tok->str) != altop.end()) { |
1939 | 1950 | bool alt; |
1940 | | - if (tok->str == "not") { |
| 1951 | + if (tok->str == "not" || tok->str == "compl") { |
1941 | 1952 | alt = isAlternativeUnaryOp(tok,tok->str); |
1942 | 1953 | } else { |
1943 | 1954 | alt = isAlternativeBinaryOp(tok,tok->str); |
@@ -2016,6 +2027,9 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const |
2016 | 2027 |
|
2017 | 2028 | static std::string getFileName(const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string &sourcefile, const std::string &header, const simplecpp::DUI &dui, bool systemheader) |
2018 | 2029 | { |
| 2030 | + if (filedata.empty()) { |
| 2031 | + return ""; |
| 2032 | + } |
2019 | 2033 | if (isAbsolutePath(header)) { |
2020 | 2034 | return (filedata.find(header) != filedata.end()) ? simplecpp::simplifyPath(header) : ""; |
2021 | 2035 | } |
|
0 commit comments