Skip to content

Commit

Permalink
added CmdLineLogger::printRaw() / CppCheckExecutor: more `CmdLineLo…
Browse files Browse the repository at this point in the history
…gger` usage (danmar#5537)
  • Loading branch information
firewave authored Oct 9, 2023
1 parent 84f2485 commit 7393083
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 34 deletions.
4 changes: 4 additions & 0 deletions cli/cmdlinelogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ class CmdLineLogger
public:
virtual ~CmdLineLogger() = default;

/** print a regular message */
virtual void printMessage(const std::string &message) = 0;
/** print an error message */
virtual void printError(const std::string &message) = 0;
/** print to the output */
virtual void printRaw(const std::string &message) = 0;
};

#endif // CMD_LINE_LOGGER_H
32 changes: 18 additions & 14 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
<< info << "\n";
}

std::cout << doc.str();
mLogger.printRaw(doc.str());
mExitAfterPrint = true;
return true;
}
Expand Down Expand Up @@ -1057,7 +1057,8 @@ void CmdLineParser::printHelp()
"https://cppcheck.sourceforge.io/manual.pdf" :
"https://files.cppchecksolutions.com/manual.pdf");

std::cout << "Cppcheck - A tool for static C/C++ code analysis\n"
std::ostringstream oss;
oss << "Cppcheck - A tool for static C/C++ code analysis\n"
"\n"
"Syntax:\n"
" cppcheck [OPTIONS] [files or paths]\n"
Expand Down Expand Up @@ -1249,20 +1250,21 @@ void CmdLineParser::printHelp()
" Generate Clang-plist output files in folder.\n";

if (isCppcheckPremium()) {
std::cout << " --premium=<option>\n"
<< " Coding standards:\n"
<< " * autosar Autosar (partial)\n"
<< " * cert-c-2016 Cert C 2016 checking\n"
<< " * cert-c++-2016 Cert C++ 2016 checking\n"
<< " * misra-c-2012 Misra C 2012\n"
<< " * misra-c-2023 Misra C 2023\n"
<< " * misra-c++-2008 Misra C++ 2008 (partial)\n"
<< " Other:\n"
<< " * bughunting Soundy analysis\n"
<< " * cert-c-int-precision=BITS Integer precision to use in Cert C analysis.\n";
oss <<
" --premium=<option>\n"
" Coding standards:\n"
" * autosar Autosar (partial)\n"
" * cert-c-2016 Cert C 2016 checking\n"
" * cert-c++-2016 Cert C++ 2016 checking\n"
" * misra-c-2012 Misra C 2012\n"
" * misra-c-2023 Misra C 2023\n"
" * misra-c++-2008 Misra C++ 2008 (partial)\n"
" Other:\n"
" * bughunting Soundy analysis\n"
" * cert-c-int-precision=BITS Integer precision to use in Cert C analysis.\n";
}

std::cout <<
oss <<
" --project=<file> Run Cppcheck on project. The <file> can be a Visual\n"
" Studio Solution (*.sln), Visual Studio Project\n"
" (*.vcxproj), compile database (compile_commands.json),\n"
Expand Down Expand Up @@ -1402,6 +1404,8 @@ void CmdLineParser::printHelp()
" * picojson -- loading compile database.\n"
" * pcre -- rules.\n"
" * qt -- used in GUI\n";

mLogger.printRaw(oss.str());
}

bool CmdLineParser::isCppcheckPremium() const {
Expand Down
29 changes: 17 additions & 12 deletions cli/cppcheckexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,18 @@ class CmdLineLoggerStd : public CmdLineLogger

void printMessage(const std::string &message) override
{
std::cout << "cppcheck: " << message << std::endl;
printRaw("cppcheck: " + message);
}

void printError(const std::string &message) override
{
printMessage("error: " + message);
}

void printRaw(const std::string &message) override
{
std::cout << message << std::endl;
}
};

// TODO: do not directly write to stdout
Expand All @@ -119,14 +124,13 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
if (success) {
if (parser.getShowVersion() && !parser.getShowErrorMessages()) {
if (!settings.cppcheckCfgProductName.empty()) {
std::cout << settings.cppcheckCfgProductName << std::endl;
logger.printRaw(settings.cppcheckCfgProductName);
} else {
const char * const extraVersion = CppCheck::extraVersion();
if (*extraVersion != 0)
std::cout << "Cppcheck " << CppCheck::version() << " ("
<< extraVersion << ')' << std::endl;
logger.printRaw(std::string("Cppcheck ") + CppCheck::version() + " ("+ extraVersion + ')');
else
std::cout << "Cppcheck " << CppCheck::version() << std::endl;
logger.printRaw(std::string("Cppcheck ") + CppCheck::version());
}
}

Expand Down Expand Up @@ -176,8 +180,8 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
return Path::isHeader(i);
});
if (warn) {
std::cout << "cppcheck: filename exclusion does not apply to header (.h and .hpp) files." << std::endl;
std::cout << "cppcheck: Please use --suppress for ignoring results from the header files." << std::endl;
logger.printMessage("filename exclusion does not apply to header (.h and .hpp) files.");
logger.printMessage("Please use --suppress for ignoring results from the header files.");
}

const std::vector<std::string>& pathnames = parser.getPathNames();
Expand All @@ -199,7 +203,7 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
if (!newList.empty())
settings.project.fileSettings = newList;
else {
std::cout << "cppcheck: error: could not find any files matching the filter." << std::endl;
logger.printError("could not find any files matching the filter.");
return false;
}
} else if (!pathnames.empty()) {
Expand All @@ -208,15 +212,16 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
for (const std::string &pathname : pathnames) {
std::string err = FileLister::recursiveAddFiles(mFiles, Path::toNativeSeparators(pathname), settings.library.markupExtensions(), matcher);
if (!err.empty()) {
std::cout << "cppcheck: " << err << std::endl;
// TODO: bail out?
logger.printMessage(err);
}
}
}

if (mFiles.empty() && settings.project.fileSettings.empty()) {
std::cout << "cppcheck: error: could not find or open any of the paths given." << std::endl;
logger.printError("could not find or open any of the paths given.");
if (!ignored.empty())
std::cout << "cppcheck: Maybe all paths were ignored?" << std::endl;
logger.printMessage("Maybe all paths were ignored?");
return false;
}
if (!settings.fileFilters.empty() && settings.project.fileSettings.empty()) {
Expand All @@ -227,7 +232,7 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
}
mFiles = newMap;
if (mFiles.empty()) {
std::cout << "cppcheck: error: could not find any files matching the filter." << std::endl;
logger.printError("could not find any files matching the filter.");
return false;
}

Expand Down
21 changes: 13 additions & 8 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class TestCmdlineParser : public TestFixture {
printMessage("error: " + message);
}

void printRaw(const std::string &message) override
{
printInternal(message + '\n');
}

std::string str()
{
std::string s;
Expand Down Expand Up @@ -314,24 +319,24 @@ class TestCmdlineParser : public TestFixture {
REDIRECT;
const char * const argv[] = {"cppcheck"};
ASSERT(parser->parseFromArgs(1, argv));
ASSERT(startsWith(GET_REDIRECT_OUTPUT, "Cppcheck - A tool for static C/C++ code analysis"));
ASSERT_EQUALS("", logger->str());
ASSERT(startsWith(logger->str(), "Cppcheck - A tool for static C/C++ code analysis"));
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}

void helpshort() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-h"};
ASSERT(parser->parseFromArgs(2, argv));
ASSERT(startsWith(GET_REDIRECT_OUTPUT, "Cppcheck - A tool for static C/C++ code analysis"));
ASSERT_EQUALS("", logger->str());
ASSERT(startsWith(logger->str(), "Cppcheck - A tool for static C/C++ code analysis"));
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}

void helplong() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--help"};
ASSERT(parser->parseFromArgs(2, argv));
ASSERT(startsWith(GET_REDIRECT_OUTPUT, "Cppcheck - A tool for static C/C++ code analysis"));
ASSERT_EQUALS("", logger->str());
ASSERT(startsWith(logger->str(), "Cppcheck - A tool for static C/C++ code analysis"));
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}

void showversion() {
Expand Down Expand Up @@ -1578,8 +1583,8 @@ class TestCmdlineParser : public TestFixture {
const char * const argv[] = {"cppcheck", "--doc"};
ASSERT(parser->parseFromArgs(2, argv));
ASSERT(parser->exitAfterPrinting());
ASSERT(startsWith(GET_REDIRECT_OUTPUT, "## "));
ASSERT_EQUALS("", logger->str());
ASSERT(startsWith(logger->str(), "## "));
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}

void showtimeSummary() {
Expand Down

0 comments on commit 7393083

Please sign in to comment.