|
| 1 | +/* |
| 2 | + * simplecpp - A simple and high-fidelity C/C++ preprocessor library |
| 3 | + * Copyright (C) 2016-2024 simplecpp team |
| 4 | + */ |
| 5 | + |
| 6 | +#include "simplecpp.h" |
| 7 | + |
| 8 | +#include <cstdint> |
| 9 | + |
| 10 | +#ifdef NO_FUZZ |
| 11 | +#include <cstdlib> |
| 12 | +#include <fstream> |
| 13 | +#include <sstream> |
| 14 | +#include <string> |
| 15 | +#endif |
| 16 | + |
| 17 | +static void doProcess(const uint8_t *data, size_t dataSize) |
| 18 | +{ |
| 19 | + simplecpp::OutputList outputList; |
| 20 | + std::vector<std::string> files; |
| 21 | + simplecpp::TokenList rawtokens(data, dataSize, files, "test.cpp", &outputList); |
| 22 | + rawtokens.removeComments(); |
| 23 | + |
| 24 | + simplecpp::TokenList outputTokens(files); |
| 25 | + std::map<std::string, simplecpp::TokenList*> filedata; |
| 26 | + simplecpp::DUI dui; |
| 27 | + dui.removeComments = true; |
| 28 | + std::list<simplecpp::MacroUsage> macroUsage; |
| 29 | + std::list<simplecpp::IfCond> ifCond; |
| 30 | + simplecpp::preprocess(outputTokens, rawtokens, files, filedata, dui, &outputList, ¯oUsage, &ifCond); |
| 31 | + |
| 32 | + simplecpp::cleanup(filedata); |
| 33 | +} |
| 34 | + |
| 35 | +#ifndef NO_FUZZ |
| 36 | +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize); |
| 37 | + |
| 38 | +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize) |
| 39 | +{ |
| 40 | + doProcess(data, dataSize); |
| 41 | + return 0; |
| 42 | +} |
| 43 | +#else |
| 44 | +int main(int argc, char * argv[]) |
| 45 | +{ |
| 46 | + if (argc < 2 || argc > 3) |
| 47 | + return EXIT_FAILURE; |
| 48 | + |
| 49 | + std::ifstream f(argv[1]); |
| 50 | + if (!f.is_open()) |
| 51 | + return EXIT_FAILURE; |
| 52 | + |
| 53 | + std::ostringstream oss; |
| 54 | + oss << f.rdbuf(); |
| 55 | + |
| 56 | + if (!f.good()) |
| 57 | + return EXIT_FAILURE; |
| 58 | + |
| 59 | + const int cnt = (argc == 3) ? std::stoi(argv[2]) : 1; |
| 60 | + |
| 61 | + const std::string code = oss.str(); |
| 62 | + for (int i = 0; i < cnt; ++i) |
| 63 | + doProcess(reinterpret_cast<const uint8_t*>(code.data()), code.size()); |
| 64 | + |
| 65 | + return EXIT_SUCCESS; |
| 66 | +} |
| 67 | +#endif |
0 commit comments