added `CmdLineLogger::printRaw()` / CppCheckExecutor: more `CmdLineLogger` usage (#5537)
This commit is contained in:
parent
84f2485a26
commit
7393083d5d
|
@ -26,8 +26,12 @@ class CmdLineLogger
|
||||||
public:
|
public:
|
||||||
virtual ~CmdLineLogger() = default;
|
virtual ~CmdLineLogger() = default;
|
||||||
|
|
||||||
|
/** print a regular message */
|
||||||
virtual void printMessage(const std::string &message) = 0;
|
virtual void printMessage(const std::string &message) = 0;
|
||||||
|
/** print an error message */
|
||||||
virtual void printError(const std::string &message) = 0;
|
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
|
#endif // CMD_LINE_LOGGER_H
|
||||||
|
|
|
@ -313,7 +313,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
|
||||||
<< info << "\n";
|
<< info << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << doc.str();
|
mLogger.printRaw(doc.str());
|
||||||
mExitAfterPrint = true;
|
mExitAfterPrint = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -1057,7 +1057,8 @@ void CmdLineParser::printHelp()
|
||||||
"https://cppcheck.sourceforge.io/manual.pdf" :
|
"https://cppcheck.sourceforge.io/manual.pdf" :
|
||||||
"https://files.cppchecksolutions.com/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"
|
"\n"
|
||||||
"Syntax:\n"
|
"Syntax:\n"
|
||||||
" cppcheck [OPTIONS] [files or paths]\n"
|
" cppcheck [OPTIONS] [files or paths]\n"
|
||||||
|
@ -1249,20 +1250,21 @@ void CmdLineParser::printHelp()
|
||||||
" Generate Clang-plist output files in folder.\n";
|
" Generate Clang-plist output files in folder.\n";
|
||||||
|
|
||||||
if (isCppcheckPremium()) {
|
if (isCppcheckPremium()) {
|
||||||
std::cout << " --premium=<option>\n"
|
oss <<
|
||||||
<< " Coding standards:\n"
|
" --premium=<option>\n"
|
||||||
<< " * autosar Autosar (partial)\n"
|
" Coding standards:\n"
|
||||||
<< " * cert-c-2016 Cert C 2016 checking\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"
|
" * cert-c++-2016 Cert C++ 2016 checking\n"
|
||||||
<< " * misra-c-2023 Misra C 2023\n"
|
" * misra-c-2012 Misra C 2012\n"
|
||||||
<< " * misra-c++-2008 Misra C++ 2008 (partial)\n"
|
" * misra-c-2023 Misra C 2023\n"
|
||||||
<< " Other:\n"
|
" * misra-c++-2008 Misra C++ 2008 (partial)\n"
|
||||||
<< " * bughunting Soundy analysis\n"
|
" Other:\n"
|
||||||
<< " * cert-c-int-precision=BITS Integer precision to use in Cert C analysis.\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"
|
" --project=<file> Run Cppcheck on project. The <file> can be a Visual\n"
|
||||||
" Studio Solution (*.sln), Visual Studio Project\n"
|
" Studio Solution (*.sln), Visual Studio Project\n"
|
||||||
" (*.vcxproj), compile database (compile_commands.json),\n"
|
" (*.vcxproj), compile database (compile_commands.json),\n"
|
||||||
|
@ -1402,6 +1404,8 @@ void CmdLineParser::printHelp()
|
||||||
" * picojson -- loading compile database.\n"
|
" * picojson -- loading compile database.\n"
|
||||||
" * pcre -- rules.\n"
|
" * pcre -- rules.\n"
|
||||||
" * qt -- used in GUI\n";
|
" * qt -- used in GUI\n";
|
||||||
|
|
||||||
|
mLogger.printRaw(oss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CmdLineParser::isCppcheckPremium() const {
|
bool CmdLineParser::isCppcheckPremium() const {
|
||||||
|
|
|
@ -92,13 +92,18 @@ public:
|
||||||
|
|
||||||
void printMessage(const std::string &message) override
|
void printMessage(const std::string &message) override
|
||||||
{
|
{
|
||||||
std::cout << "cppcheck: " << message << std::endl;
|
printRaw("cppcheck: " + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void printError(const std::string &message) override
|
void printError(const std::string &message) override
|
||||||
{
|
{
|
||||||
printMessage("error: " + message);
|
printMessage("error: " + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void printRaw(const std::string &message) override
|
||||||
|
{
|
||||||
|
std::cout << message << std::endl;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: do not directly write to stdout
|
// TODO: do not directly write to stdout
|
||||||
|
@ -119,14 +124,13 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
|
||||||
if (success) {
|
if (success) {
|
||||||
if (parser.getShowVersion() && !parser.getShowErrorMessages()) {
|
if (parser.getShowVersion() && !parser.getShowErrorMessages()) {
|
||||||
if (!settings.cppcheckCfgProductName.empty()) {
|
if (!settings.cppcheckCfgProductName.empty()) {
|
||||||
std::cout << settings.cppcheckCfgProductName << std::endl;
|
logger.printRaw(settings.cppcheckCfgProductName);
|
||||||
} else {
|
} else {
|
||||||
const char * const extraVersion = CppCheck::extraVersion();
|
const char * const extraVersion = CppCheck::extraVersion();
|
||||||
if (*extraVersion != 0)
|
if (*extraVersion != 0)
|
||||||
std::cout << "Cppcheck " << CppCheck::version() << " ("
|
logger.printRaw(std::string("Cppcheck ") + CppCheck::version() + " ("+ extraVersion + ')');
|
||||||
<< extraVersion << ')' << std::endl;
|
|
||||||
else
|
else
|
||||||
std::cout << "Cppcheck " << CppCheck::version() << std::endl;
|
logger.printRaw(std::string("Cppcheck ") + CppCheck::version());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,8 +180,8 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
|
||||||
return Path::isHeader(i);
|
return Path::isHeader(i);
|
||||||
});
|
});
|
||||||
if (warn) {
|
if (warn) {
|
||||||
std::cout << "cppcheck: filename exclusion does not apply to header (.h and .hpp) files." << std::endl;
|
logger.printMessage("filename exclusion does not apply to header (.h and .hpp) files.");
|
||||||
std::cout << "cppcheck: Please use --suppress for ignoring results from the header files." << std::endl;
|
logger.printMessage("Please use --suppress for ignoring results from the header files.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<std::string>& pathnames = parser.getPathNames();
|
const std::vector<std::string>& pathnames = parser.getPathNames();
|
||||||
|
@ -199,7 +203,7 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
|
||||||
if (!newList.empty())
|
if (!newList.empty())
|
||||||
settings.project.fileSettings = newList;
|
settings.project.fileSettings = newList;
|
||||||
else {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
} else if (!pathnames.empty()) {
|
} else if (!pathnames.empty()) {
|
||||||
|
@ -208,15 +212,16 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
|
||||||
for (const std::string &pathname : pathnames) {
|
for (const std::string &pathname : pathnames) {
|
||||||
std::string err = FileLister::recursiveAddFiles(mFiles, Path::toNativeSeparators(pathname), settings.library.markupExtensions(), matcher);
|
std::string err = FileLister::recursiveAddFiles(mFiles, Path::toNativeSeparators(pathname), settings.library.markupExtensions(), matcher);
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cout << "cppcheck: " << err << std::endl;
|
// TODO: bail out?
|
||||||
|
logger.printMessage(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mFiles.empty() && settings.project.fileSettings.empty()) {
|
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())
|
if (!ignored.empty())
|
||||||
std::cout << "cppcheck: Maybe all paths were ignored?" << std::endl;
|
logger.printMessage("Maybe all paths were ignored?");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!settings.fileFilters.empty() && settings.project.fileSettings.empty()) {
|
if (!settings.fileFilters.empty() && settings.project.fileSettings.empty()) {
|
||||||
|
@ -227,7 +232,7 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
|
||||||
}
|
}
|
||||||
mFiles = newMap;
|
mFiles = newMap;
|
||||||
if (mFiles.empty()) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -58,6 +58,11 @@ private:
|
||||||
printMessage("error: " + message);
|
printMessage("error: " + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void printRaw(const std::string &message) override
|
||||||
|
{
|
||||||
|
printInternal(message + '\n');
|
||||||
|
}
|
||||||
|
|
||||||
std::string str()
|
std::string str()
|
||||||
{
|
{
|
||||||
std::string s;
|
std::string s;
|
||||||
|
@ -314,24 +319,24 @@ private:
|
||||||
REDIRECT;
|
REDIRECT;
|
||||||
const char * const argv[] = {"cppcheck"};
|
const char * const argv[] = {"cppcheck"};
|
||||||
ASSERT(parser->parseFromArgs(1, argv));
|
ASSERT(parser->parseFromArgs(1, argv));
|
||||||
ASSERT(startsWith(GET_REDIRECT_OUTPUT, "Cppcheck - A tool for static C/C++ code analysis"));
|
ASSERT(startsWith(logger->str(), "Cppcheck - A tool for static C/C++ code analysis"));
|
||||||
ASSERT_EQUALS("", logger->str());
|
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void helpshort() {
|
void helpshort() {
|
||||||
REDIRECT;
|
REDIRECT;
|
||||||
const char * const argv[] = {"cppcheck", "-h"};
|
const char * const argv[] = {"cppcheck", "-h"};
|
||||||
ASSERT(parser->parseFromArgs(2, argv));
|
ASSERT(parser->parseFromArgs(2, argv));
|
||||||
ASSERT(startsWith(GET_REDIRECT_OUTPUT, "Cppcheck - A tool for static C/C++ code analysis"));
|
ASSERT(startsWith(logger->str(), "Cppcheck - A tool for static C/C++ code analysis"));
|
||||||
ASSERT_EQUALS("", logger->str());
|
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void helplong() {
|
void helplong() {
|
||||||
REDIRECT;
|
REDIRECT;
|
||||||
const char * const argv[] = {"cppcheck", "--help"};
|
const char * const argv[] = {"cppcheck", "--help"};
|
||||||
ASSERT(parser->parseFromArgs(2, argv));
|
ASSERT(parser->parseFromArgs(2, argv));
|
||||||
ASSERT(startsWith(GET_REDIRECT_OUTPUT, "Cppcheck - A tool for static C/C++ code analysis"));
|
ASSERT(startsWith(logger->str(), "Cppcheck - A tool for static C/C++ code analysis"));
|
||||||
ASSERT_EQUALS("", logger->str());
|
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void showversion() {
|
void showversion() {
|
||||||
|
@ -1578,8 +1583,8 @@ private:
|
||||||
const char * const argv[] = {"cppcheck", "--doc"};
|
const char * const argv[] = {"cppcheck", "--doc"};
|
||||||
ASSERT(parser->parseFromArgs(2, argv));
|
ASSERT(parser->parseFromArgs(2, argv));
|
||||||
ASSERT(parser->exitAfterPrinting());
|
ASSERT(parser->exitAfterPrinting());
|
||||||
ASSERT(startsWith(GET_REDIRECT_OUTPUT, "## "));
|
ASSERT(startsWith(logger->str(), "## "));
|
||||||
ASSERT_EQUALS("", logger->str());
|
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void showtimeSummary() {
|
void showtimeSummary() {
|
||||||
|
|
Loading…
Reference in New Issue