From 627a5e75cc37765a96753d5d4391f6707c1f8c90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 21 Feb 2021 17:21:33 +0100 Subject: [PATCH] Token::stringify; refactor in/out parameter to return value --- lib/checkio.cpp | 9 ++------- lib/symboldatabase.cpp | 4 +--- lib/token.cpp | 41 ++++++++++++++++++++++------------------- lib/token.h | 5 ++--- 4 files changed, 27 insertions(+), 32 deletions(-) diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 8b9503d6f..4bf46a6cc 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -1938,9 +1938,7 @@ void CheckIO::argumentType(std::ostream& os, const ArgumentInfo * argInfo) os << type->str() << "::"; type = type->tokAt(2); } - std::string s; - type->stringify(s, false, true, false); - os << s; + os << type->stringify(false, true, false); if (type->strAt(1) == "*" && !argInfo->element) os << " *"; else if (argInfo->variableInfo && !argInfo->element && argInfo->variableInfo->isArray()) @@ -1957,10 +1955,7 @@ void CheckIO::argumentType(std::ostream& os, const ArgumentInfo * argInfo) os << type->originalName(); if (type->strAt(1) == "*" || argInfo->address) os << " *"; - os << " {aka "; - std::string s; - type->stringify(s, false, true, false); - os << s; + os << " {aka " << type->stringify(false, true, false); if (type->strAt(1) == "*" || argInfo->address) os << " *"; os << "}"; diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index f1e3f33e4..2ca536c1a 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -3488,9 +3488,7 @@ void SymbolDatabase::printOut(const char *title) const if (scope->type == Scope::eEnum) { std::cout << " enumType: "; if (scope->enumType) { - std::string s; - scope->enumType->stringify(s, false, true, false); - std::cout << s; + std::cout << scope->enumType->stringify(false, true, false); } else std::cout << "int"; std::cout << std::endl; diff --git a/lib/token.cpp b/lib/token.cpp index 7a3f6d797..b4f5fcf01 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1174,55 +1174,58 @@ void Token::printLines(int lines) const std::cout << stringifyList(stringifyOptions::forDebugExprId(), nullptr, end) << std::endl; } -void Token::stringify(std::string& os, const stringifyOptions& options) const +std::string Token::stringify(const stringifyOptions& options) const { + std::string ret; if (options.attributes) { if (isUnsigned()) - os += "unsigned "; + ret += "unsigned "; else if (isSigned()) - os += "signed "; + ret += "signed "; if (isComplex()) - os += "_Complex "; + ret += "_Complex "; if (isLong()) { if (!(mTokType == eString || mTokType == eChar)) - os += "long "; + ret += "long "; } } if (options.macro && isExpandedMacro()) - os += '$'; + ret += '$'; if (isName() && mStr.find(' ') != std::string::npos) { for (char i : mStr) { if (i != ' ') - os += i; + ret += i; } } else if (mStr[0] != '\"' || mStr.find('\0') == std::string::npos) - os += mStr; + ret += mStr; else { for (char i : mStr) { if (i == '\0') - os += "\\0"; + ret += "\\0"; else - os += i; + ret += i; } } if (options.varid && mImpl->mVarId != 0) { - os += '@'; - os += (options.idtype ? "var" : ""); - os += std::to_string(mImpl->mVarId); + ret += '@'; + ret += (options.idtype ? "var" : ""); + ret += std::to_string(mImpl->mVarId); } else if (options.exprid && mImpl->mExprId != 0) { - os += '@'; - os += (options.idtype ? "expr" : ""); - os += std::to_string(mImpl->mExprId); + ret += '@'; + ret += (options.idtype ? "expr" : ""); + ret += std::to_string(mImpl->mExprId); } + + return ret; } -void Token::stringify(std::string& os, bool varid, bool attributes, bool macro) const +std::string Token::stringify(bool varid, bool attributes, bool macro) const { stringifyOptions options; options.varid = varid; options.attributes = attributes; options.macro = macro; - stringify(os, options); + return stringify(options); } std::string Token::stringifyList(const stringifyOptions& options, const std::vector* fileNames, const Token* end) const @@ -1292,7 +1295,7 @@ std::string Token::stringifyList(const stringifyOptions& options, const std::vec lineNumber = tok->linenr(); } - tok->stringify(ret, options); // print token + ret += tok->stringify(options); // print token if (tok->next() != end && (!options.linebreaks || (tok->next()->linenr() == tok->linenr() && tok->next()->fileIndex() == tok->fileIndex()))) ret += ' '; } diff --git a/lib/token.h b/lib/token.h index 7d7408aa2..7b5e2f9ef 100644 --- a/lib/token.h +++ b/lib/token.h @@ -887,16 +887,15 @@ public: } }; - void stringify(std::string& os, const stringifyOptions& options) const; + std::string stringify(const stringifyOptions& options) const; /** * Stringify a token - * @param os The result is shifted into that output stream * @param varid Print varids. (Style: "varname\@id") * @param attributes Print attributes of tokens like "unsigned" in front of it. * @param macro Prints $ in front of the token if it was expanded from a macro. */ - void stringify(std::string& os, bool varid, bool attributes, bool macro) const; + std::string stringify(bool varid, bool attributes, bool macro) const; std::string stringifyList(const stringifyOptions& options, const std::vector* fileNames = nullptr, const Token* end = nullptr) const; std::string stringifyList(const Token* end, bool attributes = true) const;