From 2f9fbfd9cd24fc7d373a44e90097e60bbb272ead Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 1 Sep 2025 07:40:40 +0200 Subject: [PATCH 1/3] enabled and fixed `modernize-pass-by-value` clang-tidy warnings --- .clang-tidy | 1 - simplecpp.cpp | 2 +- simplecpp.h | 9 +++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index c03da523..e305484f 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -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, diff --git a/simplecpp.cpp b/simplecpp.cpp index 84e4b54b..ab79d3e3 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1643,7 +1643,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; }; diff --git a/simplecpp.h b/simplecpp.h index ac367154..3461ab48 100644 --- a/simplecpp.h +++ b/simplecpp.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #if __cplusplus >= 202002L @@ -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(); } @@ -220,7 +221,7 @@ namespace simplecpp { FILE_NOT_FOUND, DUI_ERROR } type; - explicit Output(const std::vector& files, Type type, const std::string& msg) : type(type), location(files), msg(msg) {} + explicit Output(const std::vector& files, Type type, std::string msg) : type(type), location(files), msg(std::move(msg)) {} Location location; std::string msg; }; @@ -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 From c7311b08beb864b3fc5a7fb1379ac0c7b3eb3474 Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 1 Sep 2025 07:53:32 +0200 Subject: [PATCH 2/3] avoid some unnecessary copies --- simplecpp.cpp | 12 ++++++------ test.cpp | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index ab79d3e3..33fc2127 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1680,8 +1680,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) @@ -2300,7 +2300,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); @@ -3455,7 +3455,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 = '<'; } @@ -3615,7 +3615,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); @@ -3711,7 +3711,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); } diff --git a/test.cpp b/test.cpp index 0ecaa3b1..1d777b0c 100644 --- a/test.cpp +++ b/test.cpp @@ -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 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); From 5f396ee91be6667bfd42b020d02120a1361d9257 Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 1 Sep 2025 09:14:33 +0200 Subject: [PATCH 3/3] attempt to mitigate performance regression --- simplecpp.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 33fc2127..b41ed730 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -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; } } @@ -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; } @@ -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();