Skip to content
Closed
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
59 changes: 45 additions & 14 deletions externals/simplecpp/simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,10 @@ class simplecpp::TokenList::Stream {
return ch;
}

unsigned char peekChar() {
auto ch = static_cast<unsigned char>(peek());
int peekChar() {
int ch = peek();
if (ch == EOF)
return ch;

// For UTF-16 encoded files the BOM is 0xfeff/0xfffe. If the
// character is non-ASCII character then replace it with 0xff
Expand All @@ -285,7 +287,7 @@ class simplecpp::TokenList::Stream {
const auto ch2 = static_cast<unsigned char>(peek());
unget();
const int ch16 = makeUtf16Char(ch, ch2);
ch = static_cast<unsigned char>(((ch16 >= 0x80) ? 0xff : ch16));
ch = (ch16 >= 0x80) ? 0xff : ch16;
}

// Handling of newlines..
Expand Down Expand Up @@ -598,7 +600,7 @@ std::string simplecpp::TokenList::stringify(bool linenrs) const
return ret.str();
}

static bool isNameChar(unsigned char ch)
static bool isNameChar(int ch)
{
return std::isalnum(ch) || ch == '_' || ch == '$';
}
Expand Down Expand Up @@ -635,10 +637,10 @@ static bool isStringLiteralPrefix(const std::string &str)
str == "R" || str == "uR" || str == "UR" || str == "LR" || str == "u8R";
}

void simplecpp::TokenList::lineDirective(unsigned int fileIndex, unsigned int line, Location &location)
void simplecpp::TokenList::lineDirective(unsigned int fileIndex_, unsigned int line, Location &location)
{
if (fileIndex != location.fileIndex || line >= location.line) {
location.fileIndex = fileIndex;
if (fileIndex_ != location.fileIndex || line >= location.line) {
location.fileIndex = fileIndex_;
location.line = line;
return;
}
Expand Down Expand Up @@ -771,8 +773,21 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
ch = stream.readChar();
}
stream.ungetChar();
push_back(new Token(currentToken, location));
location.adjust(currentToken);
std::string::size_type pos = 0;
unsigned int spliced = 0;
while ((pos = currentToken.find('\\', pos)) != std::string::npos) {
if (pos + 1 < currentToken.size() && currentToken[pos + 1] == '\n') {
currentToken.erase(pos, 2);
++spliced;
} else {
++pos;
}
}
if (!currentToken.empty()) {
push_back(new Token(currentToken, location));
location.adjust(currentToken);
}
location.line += spliced;
continue;
}
}
Expand Down Expand Up @@ -1739,6 +1754,9 @@ namespace simplecpp {
return tok;
}

/**
* @throws Error thrown in case of __VA_OPT__ issues
*/
bool parseDefine(const Token *nametoken) {
nameTokDef = nametoken;
variadic = false;
Expand Down Expand Up @@ -2201,6 +2219,8 @@ namespace simplecpp {
}

output.push_back(newMacroToken(tok->str(), loc, true, tok));
if (it != macros.end())
output.back()->markExpandedFrom(&it->second);
return tok->next;
}

Expand Down Expand Up @@ -3379,6 +3399,17 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
}
output.clear();
return;
} catch (const simplecpp::Macro::Error& e) {
if (outputList) {
simplecpp::Output err{
Output::DUI_ERROR,
{},
e.what
};
outputList->emplace_back(std::move(err));
}
output.clear();
return;
}
}

Expand Down Expand Up @@ -3507,14 +3538,14 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
else
it->second = macro;
}
} catch (const std::runtime_error &) {
} catch (const std::runtime_error &err) {
if (outputList) {
simplecpp::Output err{
simplecpp::Output out{
Output::SYNTAX_ERROR,
rawtok->location,
"Failed to parse #define"
std::string("Failed to parse #define, ") + err.what()
};
outputList->emplace_back(std::move(err));
outputList->emplace_back(std::move(out));
}
output.clear();
return;
Expand Down Expand Up @@ -3671,7 +3702,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
bool closingAngularBracket = false;
if (tok) {
const std::string &sourcefile = rawtokens.file(rawtok->location);
const bool systemheader = (tok && tok->op == '<');
const bool systemheader = tok->op == '<';
std::string header;

if (systemheader) {
Expand Down
13 changes: 8 additions & 5 deletions externals/simplecpp/simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ namespace simplecpp {
bool isOneOf(const char ops[]) const;
bool startsWithOneOf(const char c[]) const;
bool endsWithOneOf(const char c[]) const;
static bool isNumberLike(const std::string& str) {
return std::isdigit(static_cast<unsigned char>(str[0])) ||
(str.size() > 1U && (str[0] == '-' || str[0] == '+') && std::isdigit(static_cast<unsigned char>(str[1])));
static bool isNumberLike(const std::string& s) {
return std::isdigit(static_cast<unsigned char>(s[0])) ||
(s.size() > 1U && (s[0] == '-' || s[0] == '+') && std::isdigit(static_cast<unsigned char>(s[1])));
}

TokenString macro;
Expand Down Expand Up @@ -213,6 +213,9 @@ namespace simplecpp {
bool isExpandedFrom(const Macro* m) const {
return mExpandedFrom.find(m) != mExpandedFrom.end();
}
void markExpandedFrom(const Macro* m) {
mExpandedFrom.insert(m);
}

void printAll() const;
void printOut() const;
Expand Down Expand Up @@ -376,7 +379,7 @@ namespace simplecpp {
const std::string& file(const Location& loc) const;

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

void combineOperators();

Expand All @@ -396,7 +399,7 @@ namespace simplecpp {
void constFoldQuestionOp(Token *&tok1);

std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList);
void lineDirective(unsigned int fileIndex, unsigned int line, Location &location);
void lineDirective(unsigned int fileIndex_, unsigned int line, Location &location);

const Token* lastLineTok(int maxsize=1000) const;
const Token* isLastLinePreprocessor(int maxsize=1000) const;
Expand Down
Loading