Skip to content
Draft
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
1 change: 0 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Checks: >
-misc-use-anonymous-namespace,
-modernize-avoid-c-arrays,
-modernize-loop-convert,
-modernize-pass-by-value,
-modernize-return-braced-init-list,
-modernize-use-auto,
-modernize-use-emplace,
Expand Down
38 changes: 22 additions & 16 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,8 +730,9 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
ch = stream.readChar();
}
stream.ungetChar();
push_back(new Token(currentToken, location));
const Location l = location;
location.adjust(currentToken);
push_back(new Token(std::move(currentToken), l));
continue;
}
}
Expand Down Expand Up @@ -878,21 +879,26 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
newlines++;
}

if (prefix.empty())
push_back(new Token(s, location, !!std::isspace(stream.peekChar()))); // push string without newlines
else
back()->setstr(prefix + s);
const Location l = location;

bool adjust = true;
if (newlines > 0) {
const Token * const llTok = lastLineTok();
if (llTok && llTok->op == '#' && llTok->next && (llTok->next->str() == "define" || llTok->next->str() == "pragma") && llTok->next->next) {
multiline += newlines;
location.adjust(s);
continue;
adjust = false;
}
}

location.adjust(currentToken);
if (adjust)
location.adjust(currentToken);

if (prefix.empty())
push_back(new Token(std::move(s), l, !!std::isspace(stream.peekChar()))); // push string without newlines
else
back()->setstr(prefix + s);

continue;
}

Expand All @@ -909,12 +915,12 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
}
}

push_back(new Token(currentToken, location, !!std::isspace(stream.peekChar())));

const Location l = location;
if (multiline)
location.col += currentToken.size();
else
location.adjust(currentToken);
push_back(new Token(std::move(currentToken), l, !!std::isspace(stream.peekChar())));
}

combineOperators();
Expand Down Expand Up @@ -1643,7 +1649,7 @@ namespace simplecpp {

/** base class for errors */
struct Error {
Error(const Location &loc, const std::string &s) : location(loc), what(s) {}
Error(const Location &loc, std::string s) : location(loc), what(std::move(s)) {}
const Location location;
const std::string what;
};
Expand Down Expand Up @@ -1680,8 +1686,8 @@ namespace simplecpp {
};
private:
/** Create new token where Token::macro is set for replaced tokens */
Token *newMacroToken(const TokenString &str, const Location &loc, bool replaced, const Token *expandedFromToken=nullptr) const {
Token *tok = new Token(str,loc);
Token *newMacroToken(TokenString str, const Location &loc, bool replaced, const Token *expandedFromToken=nullptr) const {
Token *tok = new Token(std::move(str),loc);
if (replaced)
tok->macro = nameTokDef->str();
if (expandedFromToken)
Expand Down Expand Up @@ -2300,7 +2306,7 @@ namespace simplecpp {
output->takeTokens(tokensB);
} else if (sameline(B, nextTok) && sameline(B, nextTok->next) && nextTok->op == '#' && nextTok->next->op == '#') {
TokenList output2(files);
output2.push_back(new Token(strAB, tok->location));
output2.push_back(new Token(std::move(strAB), tok->location));
nextTok = expandHashHash(&output2, loc, nextTok, macros, expandedmacros, parametertokens);
output->deleteToken(A);
output->takeTokens(output2);
Expand Down Expand Up @@ -3455,7 +3461,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
hdr += tok->str();
}
inc2.clear();
inc2.push_back(new Token(hdr, inc1.cfront()->location));
inc2.push_back(new Token(std::move(hdr), inc1.cfront()->location));
inc2.front()->op = '<';
}

Expand Down Expand Up @@ -3615,7 +3621,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
E += (E.empty() ? "" : " ") + tok->str();
const long long result = evaluate(expr, dui, sizeOfType);
conditionIsTrue = (result != 0);
ifCond->push_back(IfCond(rawtok->location, E, result));
ifCond->push_back(IfCond(rawtok->location, std::move(E), result));
} else {
const long long result = evaluate(expr, dui, sizeOfType);
conditionIsTrue = (result != 0);
Expand Down Expand Up @@ -3711,7 +3717,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
else if (output.back())
output.back()->setstr(output.cback()->str() + s);
else
output.push_back(new Token(s, loc));
output.push_back(new Token(std::move(s), loc));
} else {
output.takeTokens(tokens);
}
Expand Down
9 changes: 5 additions & 4 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <unordered_map>
#include <vector>
#if __cplusplus >= 202002L
Expand Down Expand Up @@ -127,8 +128,8 @@ namespace simplecpp {
*/
class SIMPLECPP_LIB Token {
public:
Token(const TokenString &s, const Location &loc, bool wsahead = false) :
whitespaceahead(wsahead), location(loc), previous(nullptr), next(nullptr), nextcond(nullptr), string(s) {
Token(TokenString s, const Location &loc, bool wsahead = false) :
whitespaceahead(wsahead), location(loc), previous(nullptr), next(nullptr), nextcond(nullptr), string(std::move(s)) {
flags();
}

Expand Down Expand Up @@ -220,7 +221,7 @@ namespace simplecpp {
FILE_NOT_FOUND,
DUI_ERROR
} type;
explicit Output(const std::vector<std::string>& files, Type type, const std::string& msg) : type(type), location(files), msg(msg) {}
explicit Output(const std::vector<std::string>& files, Type type, std::string msg) : type(type), location(files), msg(std::move(msg)) {}
Location location;
std::string msg;
};
Expand Down Expand Up @@ -387,7 +388,7 @@ namespace simplecpp {

/** Tracking #if/#elif expressions */
struct SIMPLECPP_LIB IfCond {
explicit IfCond(const Location& location, const std::string &E, long long result) : location(location), E(E), result(result) {}
explicit IfCond(const Location& location, std::string E, long long result) : location(location), E(std::move(E)), result(result) {}
Location location; // location of #if/#elif
std::string E; // preprocessed condition
long long result; // condition result
Expand Down
4 changes: 2 additions & 2 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3083,11 +3083,11 @@ static void stdValid()
outputList.clear();
}

static void assertToken(const std::string& s, bool name, bool number, bool comment, char op, int line)
static void assertToken(std::string s, bool name, bool number, bool comment, char op, int line)
{
const std::vector<std::string> f;
const simplecpp::Location l(f);
const simplecpp::Token t(s, l);
const simplecpp::Token t(std::move(s), l);
assertEquals(name, t.name, line);
assertEquals(number, t.number, line);
assertEquals(comment, t.comment, line);
Expand Down