Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions generator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
#include "generatorset.h"
#include "fileout.h"
#include "control.h"

// Enable simplecpp’s legacy raw-pointer TokenList API so that the PythonQt
// generator can keep using its existing integration.
// Reference: https://github.com/danmar/simplecpp/pull/496
#define SIMPLECPP_TOKENLIST_ALLOW_PTR
#include "simplecpp.h"

#include <QDir>
Expand Down
2 changes: 1 addition & 1 deletion generator/simplecpp/README.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This code is taken from https://github.com/danmar/simplecpp, version post-1.5.2 (538c5c4)
This code is taken from https://github.com/danmar/simplecpp, version post-1.5.2 (37fa4f4)

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

Expand Down
12 changes: 6 additions & 6 deletions generator/simplecpp/do_not_stop_on_error.patch
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
diff --git generator/simplecpp/simplecpp.cpp generator/simplecpp/simplecpp.cpp
index 3e9dda6c..ee8f7f7c 100644
--- generator/simplecpp/simplecpp.cpp
+++ generator/simplecpp/simplecpp.cpp
@@ -3455,10 +3455,12 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
diff --git a/generator/simplecpp/simplecpp.cpp b/generator/simplecpp/simplecpp.cpp
index 84e4b54b..88cedd4e 100644
--- a/generator/simplecpp/simplecpp.cpp
+++ b/generator/simplecpp/simplecpp.cpp
@@ -3391,10 +3391,12 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
err.msg = '#' + rawtok->str() + ' ' + err.msg;
outputList->push_back(err);
outputList->push_back(std::move(err));
}
+/* Patched for PythonQt generator: Do not stop on #error directive:
if (rawtok->str() == ERROR) {
Expand Down
55 changes: 26 additions & 29 deletions generator/simplecpp/simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,20 +466,13 @@ simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &fi
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)
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 char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList)
: frontToken(nullptr), backToken(nullptr), files(filenames)
{
StdCharBufStream stream(reinterpret_cast<const unsigned char*>(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)
{
Expand Down Expand Up @@ -611,7 +604,7 @@ static void portabilityBackslash(simplecpp::OutputList *outputList, const std::v
err.type = simplecpp::Output::PORTABILITY_BACKSLASH;
err.location = location;
err.msg = "Combination 'backslash space newline' is not portable.";
outputList->push_back(err);
outputList->push_back(std::move(err));
}

static bool isStringLiteralPrefix(const std::string &str)
Expand Down Expand Up @@ -761,18 +754,18 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
while (stream.good() && ch != '\n') {
currentToken += ch;
ch = stream.readChar();
if(ch == '\\') {
if (ch == '\\') {
TokenString tmp;
char tmp_ch = ch;
while((stream.good()) && (tmp_ch == '\\' || tmp_ch == ' ' || tmp_ch == '\t')) {
while ((stream.good()) && (tmp_ch == '\\' || tmp_ch == ' ' || tmp_ch == '\t')) {
tmp += tmp_ch;
tmp_ch = stream.readChar();
}
if(!stream.good()) {
if (!stream.good()) {
break;
}

if(tmp_ch != '\n') {
if (tmp_ch != '\n') {
currentToken += tmp;
} else {
const TokenString check_portability = currentToken + tmp;
Expand Down Expand Up @@ -855,7 +848,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
err.type = Output::SYNTAX_ERROR;
err.location = location;
err.msg = "Raw string missing terminating delimiter.";
outputList->push_back(err);
outputList->push_back(std::move(err));
}
return;
}
Expand Down Expand Up @@ -1397,7 +1390,7 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
err.type = Output::SYNTAX_ERROR;
err.location = location;
err.msg = std::string("No pair for character (") + start + "). Can't process file. File is either invalid or unicode, which is currently not supported.";
outputList->push_back(err);
outputList->push_back(std::move(err));
}
return "";
}
Expand Down Expand Up @@ -1667,7 +1660,7 @@ namespace simplecpp {
}

invalidHashHash(const Location &loc, const std::string &macroName, const std::string &message)
: Error(loc, format(macroName, message)) { }
: Error(loc, format(macroName, message)) {}

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

#ifdef __CYGWIN__
static bool startsWith(const std::string &s, const std::string &p)
{
return (s.size() >= p.size()) && std::equal(p.begin(), p.end(), s.begin());
}

std::string convertCygwinToWindowsPath(const std::string &cygwinPath)
{
std::string windowsPath;

std::string::size_type pos = 0;
if (cygwinPath.size() >= 11 && startsWith_(cygwinPath, "/cygdrive/")) {
if (cygwinPath.size() >= 11 && startsWith(cygwinPath, "/cygdrive/")) {
const unsigned char driveLetter = cygwinPath[10];
if (std::isalpha(driveLetter)) {
if (cygwinPath.size() == 11) {
Expand Down Expand Up @@ -2667,7 +2665,7 @@ static unsigned long long stringToULLbounded(
int base = 0,
std::ptrdiff_t minlen = 1,
std::size_t maxlen = std::string::npos
)
)
{
const std::string sub = s.substr(pos, maxlen);
const char * const start = sub.c_str();
Expand Down Expand Up @@ -3024,8 +3022,7 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::tryload(FileDat
return {id_it->second, false};
}

std::ifstream f(path);
FileData *const data = new FileData {path, TokenList(f, filenames, path, outputList)};
FileData *const data = new FileData {path, TokenList(path, filenames, outputList)};

if (dui.removeComments)
data->tokens.removeComments();
Expand Down Expand Up @@ -3132,7 +3129,7 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
err.location = Location(filenames);
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
outputList->push_back(err);
outputList->push_back(std::move(err));
}
continue;
}
Expand Down Expand Up @@ -3205,7 +3202,7 @@ static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token
out.type = simplecpp::Output::SYNTAX_ERROR;
out.location = err.location;
out.msg = "failed to expand \'" + tok->str() + "\', " + err.what;
outputList->push_back(out);
outputList->push_back(std::move(out));
}
return false;
}
Expand Down Expand Up @@ -3349,7 +3346,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
includetokenstack.push(filedata->tokens.cfront());
}

std::map<std::string, std::list<Location> > maybeUsedMacros;
std::map<std::string, std::list<Location>> maybeUsedMacros;

for (const Token *rawtok = nullptr; rawtok || !includetokenstack.empty();) {
if (rawtok == nullptr) {
Expand Down Expand Up @@ -3392,7 +3389,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
err.msg += tok->str();
}
err.msg = '#' + rawtok->str() + ' ' + err.msg;
outputList->push_back(err);
outputList->push_back(std::move(err));
}
/* Patched for PythonQt generator: Do not stop on #error directive:
if (rawtok->str() == ERROR) {
Expand Down Expand Up @@ -3554,7 +3551,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
out.type = Output::SYNTAX_ERROR;
out.location = rawtok->location;
out.msg = "failed to evaluate " + std::string(rawtok->str() == IF ? "#if" : "#elif") + " condition";
outputList->push_back(out);
outputList->push_back(std::move(out));
}
output.clear();
return;
Expand Down Expand Up @@ -3733,7 +3730,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
mu.macroName = macro.name();
mu.macroLocation = macro.defineLocation();
mu.useLocation = *usageIt;
macroUsage->push_back(mu);
macroUsage->push_back(std::move(mu));
}
}
}
Expand All @@ -3748,11 +3745,11 @@ simplecpp::cstd_t simplecpp::getCStd(const std::string &std)
{
if (std == "c90" || std == "c89" || std == "iso9899:1990" || std == "iso9899:199409" || std == "gnu90" || std == "gnu89")
return C89;
if (std == "c99" || std == "c9x" || std == "iso9899:1999" || std == "iso9899:199x" || std == "gnu99"|| std == "gnu9x")
if (std == "c99" || std == "c9x" || std == "iso9899:1999" || std == "iso9899:199x" || std == "gnu99" || std == "gnu9x")
return C99;
if (std == "c11" || std == "c1x" || std == "iso9899:2011" || std == "gnu11" || std == "gnu1x")
return C11;
if (std == "c17" || std == "c18" || std == "iso9899:2017" || std == "iso9899:2018" || std == "gnu17"|| std == "gnu18")
if (std == "c17" || std == "c18" || std == "iso9899:2017" || std == "iso9899:2018" || std == "gnu17" || std == "gnu18")
return C17;
if (std == "c23" || std == "gnu23" || std == "c2x" || std == "gnu2x")
return C23;
Expand Down
71 changes: 64 additions & 7 deletions generator/simplecpp/simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
#include <string>
#include <unordered_map>
#include <vector>
#if __cplusplus >= 202002L
# include <version>
#endif

#if defined(__cpp_lib_string_view) && !defined(__cpp_lib_span)
#include <string_view>
#endif
#ifdef __cpp_lib_span
#include <span>
#endif

#ifdef _WIN32
# ifdef SIMPLECPP_EXPORT
Expand All @@ -46,6 +56,15 @@
# pragma warning(disable : 4244)
#endif

// provide legacy (i.e. raw pointer) API for TokenList
// note: std::istream has an overhead compared to raw pointers
#ifndef SIMPLECPP_TOKENLIST_ALLOW_PTR
// still provide the legacy API in case we lack the performant wrappers
# if !defined(__cpp_lib_string_view) && !defined(__cpp_lib_span)
# define SIMPLECPP_TOKENLIST_ALLOW_PTR
# endif
#endif

namespace simplecpp {
/** C code standard */
enum cstd_t { CUnknown=-1, C89, C99, C11, C17, C23 };
Expand Down Expand Up @@ -114,8 +133,7 @@ namespace simplecpp {
}

Token(const Token &tok) :
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) {
}
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) {}

void flags() {
name = (std::isalpha(static_cast<unsigned char>(string[0])) || string[0] == '_' || string[0] == '$')
Expand Down Expand Up @@ -217,10 +235,45 @@ namespace simplecpp {
explicit TokenList(std::vector<std::string> &filenames);
/** generates a token list from the given std::istream parameter */
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
#ifdef SIMPLECPP_TOKENLIST_ALLOW_PTR
/** generates a token list from the given buffer */
template<size_t size>
TokenList(const char (&data)[size], std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
: TokenList(reinterpret_cast<const unsigned char*>(data), size-1, filenames, filename, outputList, 0)
{}
/** generates a token list from the given buffer */
template<size_t size>
TokenList(const unsigned char (&data)[size], std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
: TokenList(data, size-1, filenames, filename, outputList, 0)
{}

/** generates a token list from the given buffer */
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
: TokenList(data, size, filenames, filename, outputList, 0)
{}
/** generates a token list from the given buffer */
TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
: TokenList(reinterpret_cast<const unsigned char*>(data), size, filenames, filename, outputList, 0)
{}
#endif
#if defined(__cpp_lib_string_view) && !defined(__cpp_lib_span)
/** generates a token list from the given buffer */
TokenList(std::string_view data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
: TokenList(reinterpret_cast<const unsigned char*>(data.data()), data.size(), filenames, filename, outputList, 0)
{}
#endif
#ifdef __cpp_lib_span
/** generates a token list from the given buffer */
TokenList(std::span<const char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
: TokenList(reinterpret_cast<const unsigned char*>(data.data()), data.size(), filenames, filename, outputList, 0)
{}

/** generates a token list from the given buffer */
TokenList(std::span<const unsigned char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
: TokenList(data.data(), data.size(), filenames, filename, outputList, 0)
{}
#endif

/** generates a token list from the given filename parameter */
TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr);
TokenList(const TokenList &other);
Expand Down Expand Up @@ -296,6 +349,8 @@ namespace simplecpp {
}

private:
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList, int unused);

void combineOperators();

void constFoldUnaryNotPosNeg(Token *tok);
Expand Down Expand Up @@ -325,9 +380,9 @@ namespace simplecpp {
struct SIMPLECPP_LIB MacroUsage {
explicit MacroUsage(const std::vector<std::string> &f, bool macroValueKnown_) : macroLocation(f), useLocation(f), macroValueKnown(macroValueKnown_) {}
std::string macroName;
Location macroLocation;
Location useLocation;
bool macroValueKnown;
Location macroLocation;
Location useLocation;
bool macroValueKnown;
};

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

#undef SIMPLECPP_TOKENLIST_ALLOW_PTR

#if defined(_MSC_VER)
# pragma warning(pop)
#endif
Expand Down
Loading