Skip to content

Commit fe13fed

Browse files
committed
vendor(simplecpp): Update simplecpp.{h,cpp} to master (37fa4f4); was master (538c5c4)
Upstream: danmar/simplecpp@37fa4f4 Source date: 2025-08-30 Files: generator/simplecpp/simplecpp.h, generator/simplecpp/simplecpp.cpp Patch: generator/simplecpp/do_not_stop_on_error.patch re-applied Compare: danmar/simplecpp@538c5c4...37fa4f4
1 parent abd0a98 commit fe13fed

File tree

3 files changed

+91
-37
lines changed

3 files changed

+91
-37
lines changed

generator/simplecpp/README.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This code is taken from https://github.com/danmar/simplecpp, version post-1.5.2 (538c5c4)
1+
This code is taken from https://github.com/danmar/simplecpp, version post-1.5.2 (37fa4f4)
22

33
The code was released under the 0BSD license (see LICENSE file).
44

generator/simplecpp/simplecpp.cpp

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -466,20 +466,13 @@ simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &fi
466466
readfile(stream,filename,outputList);
467467
}
468468

469-
simplecpp::TokenList::TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
469+
simplecpp::TokenList::TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList, int /*unused*/)
470470
: frontToken(nullptr), backToken(nullptr), files(filenames)
471471
{
472472
StdCharBufStream stream(data, size);
473473
readfile(stream,filename,outputList);
474474
}
475475

476-
simplecpp::TokenList::TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
477-
: frontToken(nullptr), backToken(nullptr), files(filenames)
478-
{
479-
StdCharBufStream stream(reinterpret_cast<const unsigned char*>(data), size);
480-
readfile(stream,filename,outputList);
481-
}
482-
483476
simplecpp::TokenList::TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList)
484477
: frontToken(nullptr), backToken(nullptr), files(filenames)
485478
{
@@ -611,7 +604,7 @@ static void portabilityBackslash(simplecpp::OutputList *outputList, const std::v
611604
err.type = simplecpp::Output::PORTABILITY_BACKSLASH;
612605
err.location = location;
613606
err.msg = "Combination 'backslash space newline' is not portable.";
614-
outputList->push_back(err);
607+
outputList->push_back(std::move(err));
615608
}
616609

617610
static bool isStringLiteralPrefix(const std::string &str)
@@ -761,18 +754,18 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
761754
while (stream.good() && ch != '\n') {
762755
currentToken += ch;
763756
ch = stream.readChar();
764-
if(ch == '\\') {
757+
if (ch == '\\') {
765758
TokenString tmp;
766759
char tmp_ch = ch;
767-
while((stream.good()) && (tmp_ch == '\\' || tmp_ch == ' ' || tmp_ch == '\t')) {
760+
while ((stream.good()) && (tmp_ch == '\\' || tmp_ch == ' ' || tmp_ch == '\t')) {
768761
tmp += tmp_ch;
769762
tmp_ch = stream.readChar();
770763
}
771-
if(!stream.good()) {
764+
if (!stream.good()) {
772765
break;
773766
}
774767

775-
if(tmp_ch != '\n') {
768+
if (tmp_ch != '\n') {
776769
currentToken += tmp;
777770
} else {
778771
const TokenString check_portability = currentToken + tmp;
@@ -855,7 +848,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
855848
err.type = Output::SYNTAX_ERROR;
856849
err.location = location;
857850
err.msg = "Raw string missing terminating delimiter.";
858-
outputList->push_back(err);
851+
outputList->push_back(std::move(err));
859852
}
860853
return;
861854
}
@@ -1397,7 +1390,7 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
13971390
err.type = Output::SYNTAX_ERROR;
13981391
err.location = location;
13991392
err.msg = std::string("No pair for character (") + start + "). Can't process file. File is either invalid or unicode, which is currently not supported.";
1400-
outputList->push_back(err);
1393+
outputList->push_back(std::move(err));
14011394
}
14021395
return "";
14031396
}
@@ -1667,7 +1660,7 @@ namespace simplecpp {
16671660
}
16681661

16691662
invalidHashHash(const Location &loc, const std::string &macroName, const std::string &message)
1670-
: Error(loc, format(macroName, message)) { }
1663+
: Error(loc, format(macroName, message)) {}
16711664

16721665
static inline invalidHashHash unexpectedToken(const Location &loc, const std::string &macroName, const Token *tokenA) {
16731666
return invalidHashHash(loc, macroName, "Unexpected token '"+ tokenA->str()+"'");
@@ -2178,7 +2171,7 @@ namespace simplecpp {
21782171
if (it != macros.end() && !partok->isExpandedFrom(&it->second) && (partok->str() == name() || expandedmacros.find(partok->str()) == expandedmacros.end())) {
21792172
std::set<TokenString> expandedmacros2(expandedmacros); // temporary amnesia to allow reexpansion of currently expanding macros during argument evaluation
21802173
expandedmacros2.erase(name());
2181-
partok = it->second.expand(output, loc, partok, macros, expandedmacros2);
2174+
partok = it->second.expand(output, loc, partok, macros, std::move(expandedmacros2));
21822175
} else {
21832176
output->push_back(newMacroToken(partok->str(), loc, isReplaced(expandedmacros), partok));
21842177
output->back()->macro = partok->macro;
@@ -2385,12 +2378,17 @@ namespace simplecpp {
23852378
namespace simplecpp {
23862379

23872380
#ifdef __CYGWIN__
2381+
static bool startsWith(const std::string &s, const std::string &p)
2382+
{
2383+
return (s.size() >= p.size()) && std::equal(p.begin(), p.end(), s.begin());
2384+
}
2385+
23882386
std::string convertCygwinToWindowsPath(const std::string &cygwinPath)
23892387
{
23902388
std::string windowsPath;
23912389

23922390
std::string::size_type pos = 0;
2393-
if (cygwinPath.size() >= 11 && startsWith_(cygwinPath, "/cygdrive/")) {
2391+
if (cygwinPath.size() >= 11 && startsWith(cygwinPath, "/cygdrive/")) {
23942392
const unsigned char driveLetter = cygwinPath[10];
23952393
if (std::isalpha(driveLetter)) {
23962394
if (cygwinPath.size() == 11) {
@@ -2667,7 +2665,7 @@ static unsigned long long stringToULLbounded(
26672665
int base = 0,
26682666
std::ptrdiff_t minlen = 1,
26692667
std::size_t maxlen = std::string::npos
2670-
)
2668+
)
26712669
{
26722670
const std::string sub = s.substr(pos, maxlen);
26732671
const char * const start = sub.c_str();
@@ -3024,8 +3022,7 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::tryload(FileDat
30243022
return {id_it->second, false};
30253023
}
30263024

3027-
std::ifstream f(path);
3028-
FileData *const data = new FileData {path, TokenList(f, filenames, path, outputList)};
3025+
FileData *const data = new FileData {path, TokenList(path, filenames, outputList)};
30293026

30303027
if (dui.removeComments)
30313028
data->tokens.removeComments();
@@ -3132,7 +3129,7 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
31323129
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
31333130
err.location = Location(filenames);
31343131
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
3135-
outputList->push_back(err);
3132+
outputList->push_back(std::move(err));
31363133
}
31373134
continue;
31383135
}
@@ -3205,7 +3202,7 @@ static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token
32053202
out.type = simplecpp::Output::SYNTAX_ERROR;
32063203
out.location = err.location;
32073204
out.msg = "failed to expand \'" + tok->str() + "\', " + err.what;
3208-
outputList->push_back(out);
3205+
outputList->push_back(std::move(out));
32093206
}
32103207
return false;
32113208
}
@@ -3349,7 +3346,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
33493346
includetokenstack.push(filedata->tokens.cfront());
33503347
}
33513348

3352-
std::map<std::string, std::list<Location> > maybeUsedMacros;
3349+
std::map<std::string, std::list<Location>> maybeUsedMacros;
33533350

33543351
for (const Token *rawtok = nullptr; rawtok || !includetokenstack.empty();) {
33553352
if (rawtok == nullptr) {
@@ -3392,7 +3389,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
33923389
err.msg += tok->str();
33933390
}
33943391
err.msg = '#' + rawtok->str() + ' ' + err.msg;
3395-
outputList->push_back(err);
3392+
outputList->push_back(std::move(err));
33963393
}
33973394
/* Patched for PythonQt generator: Do not stop on #error directive:
33983395
if (rawtok->str() == ERROR) {
@@ -3554,7 +3551,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
35543551
out.type = Output::SYNTAX_ERROR;
35553552
out.location = rawtok->location;
35563553
out.msg = "failed to evaluate " + std::string(rawtok->str() == IF ? "#if" : "#elif") + " condition";
3557-
outputList->push_back(out);
3554+
outputList->push_back(std::move(out));
35583555
}
35593556
output.clear();
35603557
return;
@@ -3733,7 +3730,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
37333730
mu.macroName = macro.name();
37343731
mu.macroLocation = macro.defineLocation();
37353732
mu.useLocation = *usageIt;
3736-
macroUsage->push_back(mu);
3733+
macroUsage->push_back(std::move(mu));
37373734
}
37383735
}
37393736
}
@@ -3748,11 +3745,11 @@ simplecpp::cstd_t simplecpp::getCStd(const std::string &std)
37483745
{
37493746
if (std == "c90" || std == "c89" || std == "iso9899:1990" || std == "iso9899:199409" || std == "gnu90" || std == "gnu89")
37503747
return C89;
3751-
if (std == "c99" || std == "c9x" || std == "iso9899:1999" || std == "iso9899:199x" || std == "gnu99"|| std == "gnu9x")
3748+
if (std == "c99" || std == "c9x" || std == "iso9899:1999" || std == "iso9899:199x" || std == "gnu99" || std == "gnu9x")
37523749
return C99;
37533750
if (std == "c11" || std == "c1x" || std == "iso9899:2011" || std == "gnu11" || std == "gnu1x")
37543751
return C11;
3755-
if (std == "c17" || std == "c18" || std == "iso9899:2017" || std == "iso9899:2018" || std == "gnu17"|| std == "gnu18")
3752+
if (std == "c17" || std == "c18" || std == "iso9899:2017" || std == "iso9899:2018" || std == "gnu17" || std == "gnu18")
37563753
return C17;
37573754
if (std == "c23" || std == "gnu23" || std == "c2x" || std == "gnu2x")
37583755
return C23;

generator/simplecpp/simplecpp.h

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
#include <string>
2121
#include <unordered_map>
2222
#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
2333

2434
#ifdef _WIN32
2535
# ifdef SIMPLECPP_EXPORT
@@ -46,6 +56,15 @@
4656
# pragma warning(disable : 4244)
4757
#endif
4858

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+
4968
namespace simplecpp {
5069
/** C code standard */
5170
enum cstd_t { CUnknown=-1, C89, C99, C11, C17, C23 };
@@ -114,8 +133,7 @@ namespace simplecpp {
114133
}
115134

116135
Token(const Token &tok) :
117-
macro(tok.macro), op(tok.op), comment(tok.comment), name(tok.name), number(tok.number), whitespaceahead(tok.whitespaceahead), location(tok.location), previous(nullptr), next(nullptr), nextcond(nullptr), string(tok.string), mExpandedFrom(tok.mExpandedFrom) {
118-
}
136+
macro(tok.macro), op(tok.op), comment(tok.comment), name(tok.name), number(tok.number), whitespaceahead(tok.whitespaceahead), location(tok.location), previous(nullptr), next(nullptr), nextcond(nullptr), string(tok.string), mExpandedFrom(tok.mExpandedFrom) {}
119137

120138
void flags() {
121139
name = (std::isalpha(static_cast<unsigned char>(string[0])) || string[0] == '_' || string[0] == '$')
@@ -217,10 +235,45 @@ namespace simplecpp {
217235
explicit TokenList(std::vector<std::string> &filenames);
218236
/** generates a token list from the given std::istream parameter */
219237
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+
220250
/** generates a token list from the given buffer */
221-
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+
{}
222254
/** generates a token list from the given buffer */
223-
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+
224277
/** generates a token list from the given filename parameter */
225278
TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr);
226279
TokenList(const TokenList &other);
@@ -296,6 +349,8 @@ namespace simplecpp {
296349
}
297350

298351
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+
299354
void combineOperators();
300355

301356
void constFoldUnaryNotPosNeg(Token *tok);
@@ -325,9 +380,9 @@ namespace simplecpp {
325380
struct SIMPLECPP_LIB MacroUsage {
326381
explicit MacroUsage(const std::vector<std::string> &f, bool macroValueKnown_) : macroLocation(f), useLocation(f), macroValueKnown(macroValueKnown_) {}
327382
std::string macroName;
328-
Location macroLocation;
329-
Location useLocation;
330-
bool macroValueKnown;
383+
Location macroLocation;
384+
Location useLocation;
385+
bool macroValueKnown;
331386
};
332387

333388
/** Tracking #if/#elif expressions */
@@ -506,6 +561,8 @@ namespace simplecpp {
506561
SIMPLECPP_LIB std::string getCppStdString(cppstd_t std);
507562
}
508563

564+
#undef SIMPLECPP_TOKENLIST_ALLOW_PTR
565+
509566
#if defined(_MSC_VER)
510567
# pragma warning(pop)
511568
#endif

0 commit comments

Comments
 (0)