|
20 | 20 | #include <string> |
21 | 21 | #include <unordered_map> |
22 | 22 | #include <vector> |
| 23 | +#if __cplusplus >= 202002L |
| 24 | +# include <version> |
| 25 | +#endif |
| 26 | + |
| 27 | +#if defined(__cpp_lib_string_view) && !defined(__cpp_lib_span) |
| 28 | +#include <string_view> |
| 29 | +#endif |
| 30 | +#ifdef __cpp_lib_span |
| 31 | +#include <span> |
| 32 | +#endif |
23 | 33 |
|
24 | 34 | #ifdef _WIN32 |
25 | 35 | # ifdef SIMPLECPP_EXPORT |
|
46 | 56 | # pragma warning(disable : 4244) |
47 | 57 | #endif |
48 | 58 |
|
| 59 | +// provide legacy (i.e. raw pointer) API for TokenList |
| 60 | +// note: std::istream has an overhead compared to raw pointers |
| 61 | +#ifndef SIMPLECPP_TOKENLIST_ALLOW_PTR |
| 62 | +// still provide the legacy API in case we lack the performant wrappers |
| 63 | +# if !defined(__cpp_lib_string_view) && !defined(__cpp_lib_span) |
| 64 | +# define SIMPLECPP_TOKENLIST_ALLOW_PTR |
| 65 | +# endif |
| 66 | +#endif |
| 67 | + |
49 | 68 | namespace simplecpp { |
50 | 69 | /** C code standard */ |
51 | 70 | enum cstd_t { CUnknown=-1, C89, C99, C11, C17, C23 }; |
@@ -216,10 +235,45 @@ namespace simplecpp { |
216 | 235 | explicit TokenList(std::vector<std::string> &filenames); |
217 | 236 | /** generates a token list from the given std::istream parameter */ |
218 | 237 | TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr); |
| 238 | +#ifdef SIMPLECPP_TOKENLIST_ALLOW_PTR |
| 239 | + /** generates a token list from the given buffer */ |
| 240 | + template<size_t size> |
| 241 | + TokenList(const char (&data)[size], std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr) |
| 242 | + : TokenList(reinterpret_cast<const unsigned char*>(data), size-1, filenames, filename, outputList, 0) |
| 243 | + {} |
| 244 | + /** generates a token list from the given buffer */ |
| 245 | + template<size_t size> |
| 246 | + TokenList(const unsigned char (&data)[size], std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr) |
| 247 | + : TokenList(data, size-1, filenames, filename, outputList, 0) |
| 248 | + {} |
| 249 | + |
219 | 250 | /** generates a token list from the given buffer */ |
220 | | - TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr); |
| 251 | + TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr) |
| 252 | + : TokenList(data, size, filenames, filename, outputList, 0) |
| 253 | + {} |
221 | 254 | /** generates a token list from the given buffer */ |
222 | | - TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr); |
| 255 | + TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr) |
| 256 | + : TokenList(reinterpret_cast<const unsigned char*>(data), size, filenames, filename, outputList, 0) |
| 257 | + {} |
| 258 | +#endif |
| 259 | +#if defined(__cpp_lib_string_view) && !defined(__cpp_lib_span) |
| 260 | + /** generates a token list from the given buffer */ |
| 261 | + TokenList(std::string_view data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr) |
| 262 | + : TokenList(reinterpret_cast<const unsigned char*>(data.data()), data.size(), filenames, filename, outputList, 0) |
| 263 | + {} |
| 264 | +#endif |
| 265 | +#ifdef __cpp_lib_span |
| 266 | + /** generates a token list from the given buffer */ |
| 267 | + TokenList(std::span<const char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr) |
| 268 | + : TokenList(reinterpret_cast<const unsigned char*>(data.data()), data.size(), filenames, filename, outputList, 0) |
| 269 | + {} |
| 270 | + |
| 271 | + /** generates a token list from the given buffer */ |
| 272 | + TokenList(std::span<const unsigned char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr) |
| 273 | + : TokenList(data.data(), data.size(), filenames, filename, outputList, 0) |
| 274 | + {} |
| 275 | +#endif |
| 276 | + |
223 | 277 | /** generates a token list from the given filename parameter */ |
224 | 278 | TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr); |
225 | 279 | TokenList(const TokenList &other); |
@@ -295,6 +349,8 @@ namespace simplecpp { |
295 | 349 | } |
296 | 350 |
|
297 | 351 | private: |
| 352 | + TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList, int unused); |
| 353 | + |
298 | 354 | void combineOperators(); |
299 | 355 |
|
300 | 356 | void constFoldUnaryNotPosNeg(Token *tok); |
@@ -505,6 +561,8 @@ namespace simplecpp { |
505 | 561 | SIMPLECPP_LIB std::string getCppStdString(cppstd_t std); |
506 | 562 | } |
507 | 563 |
|
| 564 | +#undef SIMPLECPP_TOKENLIST_ALLOW_PTR |
| 565 | + |
508 | 566 | #if defined(_MSC_VER) |
509 | 567 | # pragma warning(pop) |
510 | 568 | #endif |
|
0 commit comments