Skip to content

Commit

Permalink
Token: avoid direct std::cout usage (#6870)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Oct 7, 2024
1 parent fb994f9 commit 3fda2b0
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 34 deletions.
4 changes: 2 additions & 2 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
#include <ctime>
#include <exception> // IWYU pragma: keep
#include <fstream>
#include <iostream> // <- TEMPORARY
#include <iostream>
#include <new>
#include <set>
#include <sstream>
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/templatesimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
Expand Down Expand Up @@ -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<std::string> &fileNames) const
void Token::printOut(std::ostream& out, const char *title, const std::vector<std::string> &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
Expand Down
6 changes: 3 additions & 3 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<std::string> &fileNames) const;
void printOut(std::ostream& out, const char *title, const std::vector<std::string> &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,
Expand Down
34 changes: 17 additions & 17 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ namespace {
}
// TODO: handle all typedefs
if ((false))
printTypedef(typedefToken);
printTypedef(typedefToken, std::cout);
mFail = true;
}

Expand Down Expand Up @@ -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";
}
};
}
Expand Down Expand Up @@ -3500,7 +3500,7 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration)
mSymbolDatabase->setArrayDimensionsUsingValueFlow();
}

printDebugOutput(1);
printDebugOutput(1, std::cout);

return true;
}
Expand Down Expand Up @@ -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 << "<debug>" << std::endl;
out << "<debug>" << 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 << "</debug>" << std::endl;
out << "</debug>" << std::endl;
}

if (mSymbolDatabase && simplification == 2U && mSettings.debugwarnings) {
Expand Down Expand Up @@ -8122,27 +8122,27 @@ 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);
}

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);
}

Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <cstdint>
#include <exception>
#include <functional>
#include <iostream>
#include <utility>
#include <set>
#include <stack>
Expand Down Expand Up @@ -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<const Token*> safeAstTokens; // list of "safe" AST tokens without endless recursion
Expand Down

0 comments on commit 3fda2b0

Please sign in to comment.