From 3fda2b0370c16ca5da0f57d9e780cb026ee3597e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Mon, 7 Oct 2024 20:11:41 +0200 Subject: [PATCH] Token: avoid direct `std::cout` usage (#6870) --- lib/cppcheck.cpp | 4 ++-- lib/templatesimplifier.cpp | 2 +- lib/token.cpp | 17 ++++++++--------- lib/token.h | 6 +++--- lib/tokenize.cpp | 34 +++++++++++++++++----------------- lib/tokenize.h | 2 +- lib/tokenlist.cpp | 3 ++- 7 files changed, 34 insertions(+), 34 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index a02db276b82..f0fb71f62aa 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -50,7 +50,7 @@ #include #include // IWYU pragma: keep #include -#include // <- TEMPORARY +#include #include #include #include @@ -516,7 +516,7 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file) mSettings, &s_timerResults); if (mSettings.debugnormal) - tokenizer.printDebugOutput(1); + tokenizer.printDebugOutput(1, std::cout); checkNormalTokens(tokenizer); // create dumpfile diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 3baeae75f65..c2067c94a73 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -3813,7 +3813,7 @@ void TemplateSimplifier::simplifyTemplates(const std::time_t maxtime) if (mSettings.debugtemplate && mSettings.debugnormal) { std::string title("Template Simplifier pass " + std::to_string(passCount + 1)); - mTokenList.front()->printOut(title.c_str(), mTokenList.getFiles()); + mTokenList.front()->printOut(std::cout, title.c_str(), mTokenList.getFiles()); } // Copy default argument values from forward declaration to declaration diff --git a/lib/token.cpp b/lib/token.cpp index 6d56e7f9714..8e5bef7be32 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -36,7 +36,6 @@ #include #include #include -#include #include #include #include @@ -1178,27 +1177,27 @@ void Token::createMutualLinks(Token *begin, Token *end) end->link(begin); } -void Token::printOut(const char *title) const +void Token::printOut(std::ostream& out, const char *title) const { if (title && title[0]) - std::cout << "\n### " << title << " ###\n"; - std::cout << stringifyList(stringifyOptions::forPrintOut(), nullptr, nullptr) << std::endl; + out << "\n### " << title << " ###\n"; + out << stringifyList(stringifyOptions::forPrintOut(), nullptr, nullptr) << std::endl; } -void Token::printOut(const char *title, const std::vector &fileNames) const +void Token::printOut(std::ostream& out, const char *title, const std::vector &fileNames) const { if (title && title[0]) - std::cout << "\n### " << title << " ###\n"; - std::cout << stringifyList(stringifyOptions::forPrintOut(), &fileNames, nullptr) << std::endl; + out << "\n### " << title << " ###\n"; + out << stringifyList(stringifyOptions::forPrintOut(), &fileNames, nullptr) << std::endl; } // cppcheck-suppress unusedFunction - used for debugging -void Token::printLines(int lines) const +void Token::printLines(std::ostream& out, int lines) const { const Token *end = this; while (end && end->linenr() < lines + linenr()) end = end->next(); - std::cout << stringifyList(stringifyOptions::forDebugExprId(), nullptr, end) << std::endl; + out << stringifyList(stringifyOptions::forDebugExprId(), nullptr, end) << std::endl; } std::string Token::stringify(const stringifyOptions& options) const diff --git a/lib/token.h b/lib/token.h index 38481ed6a99..99599c96e63 100644 --- a/lib/token.h +++ b/lib/token.h @@ -973,7 +973,7 @@ class CPPCHECKLIB Token { * @param title Title for the printout or use default parameter or 0 * for no title. */ - void printOut(const char *title = nullptr) const; + void printOut(std::ostream& out, const char *title = nullptr) const; /** * For debugging purposes, prints token and all tokens @@ -983,12 +983,12 @@ class CPPCHECKLIB Token { * @param fileNames Prints out file name instead of file index. * File index should match the index of the string in this vector. */ - void printOut(const char *title, const std::vector &fileNames) const; + void printOut(std::ostream& out, const char *title, const std::vector &fileNames) const; /** * print out tokens - used for debugging */ - void printLines(int lines=5) const; + void printLines(std::ostream& out, int lines=5) const; /** * Replace token replaceThis with tokens between start and end, diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 92cbd53ae7e..fae4b6fe2b2 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -710,7 +710,7 @@ namespace { } // TODO: handle all typedefs if ((false)) - printTypedef(typedefToken); + printTypedef(typedefToken, std::cout); mFail = true; } @@ -1081,17 +1081,17 @@ namespace { return to; } - static void printTypedef(const Token *tok) { + static void printTypedef(const Token *tok, std::ostream& out) { int indent = 0; while (tok && (indent > 0 || tok->str() != ";")) { if (tok->str() == "{") ++indent; else if (tok->str() == "}") --indent; - std::cout << " " << tok->str(); + out << " " << tok->str(); tok = tok->next(); } - std::cout << "\n"; + out << "\n"; } }; } @@ -3500,7 +3500,7 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration) mSymbolDatabase->setArrayDimensionsUsingValueFlow(); } - printDebugOutput(1); + printDebugOutput(1, std::cout); return true; } @@ -5919,32 +5919,32 @@ bool Tokenizer::simplifyTokenList1(const char FileName[]) } //--------------------------------------------------------------------------- -void Tokenizer::printDebugOutput(int simplification) const +void Tokenizer::printDebugOutput(int simplification, std::ostream &out) const { const bool debug = (simplification != 1U && mSettings.debugSimplified) || (simplification != 2U && mSettings.debugnormal); if (debug && list.front()) { - list.front()->printOut(nullptr, list.getFiles()); + list.front()->printOut(out, nullptr, list.getFiles()); if (mSettings.xml) - std::cout << "" << std::endl; + out << "" << std::endl; if (mSymbolDatabase) { if (mSettings.xml) - mSymbolDatabase->printXml(std::cout); + mSymbolDatabase->printXml(out); else if (mSettings.verbose) { mSymbolDatabase->printOut("Symbol database"); } } if (mSettings.verbose) - list.front()->printAst(mSettings.verbose, mSettings.xml, list.getFiles(), std::cout); + list.front()->printAst(mSettings.verbose, mSettings.xml, list.getFiles(), out); - list.front()->printValueFlow(mSettings.xml, std::cout); + list.front()->printValueFlow(mSettings.xml, out); if (mSettings.xml) - std::cout << "" << std::endl; + out << "" << std::endl; } if (mSymbolDatabase && simplification == 2U && mSettings.debugwarnings) { @@ -8122,13 +8122,13 @@ bool Tokenizer::isScopeNoReturn(const Token *endScopeToken, bool *unknown) const void Tokenizer::syntaxError(const Token *tok, const std::string &code) const { - printDebugOutput(0); + printDebugOutput(0, std::cout); throw InternalError(tok, code.empty() ? "syntax error" : "syntax error: " + code, InternalError::SYNTAX); } void Tokenizer::unmatchedToken(const Token *tok) const { - printDebugOutput(0); + printDebugOutput(0, std::cout); throw InternalError(tok, "Unmatched '" + tok->str() + "'. Configuration: '" + mConfiguration + "'.", InternalError::SYNTAX); @@ -8136,13 +8136,13 @@ void Tokenizer::unmatchedToken(const Token *tok) const void Tokenizer::syntaxErrorC(const Token *tok, const std::string &what) const { - printDebugOutput(0); + printDebugOutput(0, std::cout); throw InternalError(tok, "Code '"+what+"' is invalid C code.", "Use --std, -x or --language to enforce C++. Or --cpp-header-probe to identify C++ headers via the Emacs marker.", InternalError::SYNTAX); } void Tokenizer::unknownMacroError(const Token *tok1) const { - printDebugOutput(0); + printDebugOutput(0, std::cout); throw InternalError(tok1, "There is an unknown macro here somewhere. Configuration is required. If " + tok1->str() + " is a macro then please configure it.", InternalError::UNKNOWN_MACRO); } @@ -8168,7 +8168,7 @@ void Tokenizer::macroWithSemicolonError(const Token *tok, const std::string &mac void Tokenizer::cppcheckError(const Token *tok) const { - printDebugOutput(0); + printDebugOutput(0, std::cout); throw InternalError(tok, "Analysis failed. If the code is valid then please report this failure.", InternalError::INTERNAL); } diff --git a/lib/tokenize.h b/lib/tokenize.h index 715edadf499..46cf44f7cdc 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -571,7 +571,7 @@ class CPPCHECKLIB Tokenizer { * 1=1st simplifications * 2=2nd simplifications */ - void printDebugOutput(int simplification) const; + void printDebugOutput(int simplification, std::ostream &out) const; void dump(std::ostream &out) const; diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index d31a85f4a96..7f09c2eaa2c 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -1808,7 +1809,7 @@ void TokenList::validateAst(bool print) const { OnException oe{[&] { if (print) - mTokensFrontBack.front->printOut(); + mTokensFrontBack.front->printOut(std::cout); }}; // Check for some known issues in AST to avoid crash/hang later on std::set safeAstTokens; // list of "safe" AST tokens without endless recursion