-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SARIF output support. #4651
Changes from all commits
8f10eef
6f0b5f0
e736e59
963267b
b495ccd
44ea258
918d9cf
f92e1fd
a5298d3
a1f442d
349dc0b
c501cdd
ca7214f
b81988e
df790fd
b1b7239
6835c6a
d92b72b
551f7ed
5f13e12
9a73d59
9b5310f
c7a9611
b4058ca
915d9fb
455ab5f
a4671e5
2e88265
3a647e5
4ecafee
a22a1ac
1a20b60
d1c2d83
13ce02e
03d91ac
e14ff71
74950dc
3abccfc
1cc3516
21eba3b
b341465
1b32554
eacd087
9995bb1
e53984e
3b2fda2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Cppcheck - A tool for static C/C++ code analysis | ||
* Copyright (C) 2007-2022 Cppcheck team. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef ANALYSIS_REPORT_H | ||
#define ANALYSIS_REPORT_H | ||
|
||
#include "errorlogger.h" | ||
|
||
#include <string> | ||
|
||
/** | ||
* @brief The AnalysisReport class is an abstract class meant to be sub-classed | ||
* by others classes that will contain the results of a CppCheck analysis, and | ||
* output those results in a particular format. | ||
*/ | ||
class AnalysisReport { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe the constructor for AnalysisReport could take a output stream argument:
Then you pass the ofstream/std::cout/std::cerr as argument to the report. |
||
public: | ||
/** | ||
* Submit a CppCheck result for inclusion into the report. | ||
*/ | ||
virtual void addFinding(ErrorMessage msg) = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change parameter type to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it used to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm.. ok but then I think you can't use the forward declaration.. maybe firewave did not see that. Could you double check if the forward declaration is possible if a const reference is used instead. |
||
|
||
/** | ||
* Output the results as a string. | ||
*/ | ||
virtual std::string serialize() = 0; | ||
|
||
virtual ~AnalysisReport() = default; | ||
}; | ||
|
||
#endif // ANALYSIS_REPORT_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Cppcheck - A tool for static C/C++ code analysis | ||
* Copyright (C) 2007-2022 Cppcheck team. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <iostream> | ||
#include <fstream> | ||
|
||
#include "clianalysisreport.h" | ||
|
||
#ifdef _WIN32 | ||
#include <windows.h> | ||
#endif | ||
|
||
CLIAnalysisReport::CLIAnalysisReport(bool verbose, std::string templateFormat, std::string templateLocation, std::ofstream* errorOutput) | ||
: mVerbose(verbose), mTemplateFormat(std::move(templateFormat)), mTemplateLocation(std::move(templateLocation)), mErrorOutput(errorOutput) {} | ||
|
||
std::string CLIAnalysisReport::serialize() { | ||
return ""; // CLIAnalysisReport emits the findings immediately, so no need to return a report. | ||
} | ||
|
||
#ifdef _WIN32 | ||
// fix trac ticket #439 'Cppcheck reports wrong filename for filenames containing 8-bit ASCII' | ||
static inline std::string ansiToOEM(const std::string &msg, bool doConvert) | ||
{ | ||
if (doConvert) { | ||
const unsigned msglength = msg.length(); | ||
// convert ANSI strings to OEM strings in two steps | ||
std::vector<WCHAR> wcContainer(msglength); | ||
std::string result(msglength, '\0'); | ||
|
||
// ansi code page characters to wide characters | ||
MultiByteToWideChar(CP_ACP, 0, msg.data(), msglength, wcContainer.data(), msglength); | ||
// wide characters to oem codepage characters | ||
WideCharToMultiByte(CP_OEMCP, 0, wcContainer.data(), msglength, const_cast<char *>(result.data()), msglength, nullptr, nullptr); | ||
|
||
return result; // hope for return value optimization | ||
} | ||
return msg; | ||
} | ||
#else | ||
// no performance regression on non-windows systems | ||
#define ansiToOEM(msg, doConvert) (msg) | ||
#endif | ||
|
||
void CLIAnalysisReport::addFinding(const ErrorMessage msg) { | ||
const std::string errmsg = msg.toString(mVerbose, mTemplateFormat, mTemplateLocation); | ||
if (mErrorOutput) | ||
*mErrorOutput << errmsg << std::endl; | ||
else { | ||
std::cerr << ansiToOEM(errmsg, true) << std::endl; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Cppcheck - A tool for static C/C++ code analysis | ||
* Copyright (C) 2007-2022 Cppcheck team. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef CLI_ANALYSIS_REPORT_H | ||
#define CLI_ANALYSIS_REPORT_H | ||
|
||
#include "analysisreport.h" | ||
|
||
/** | ||
* @brief The CLIAnalysisReport class is used to contain the results of a CppCheck analysis | ||
* and output the results to STDERR. | ||
*/ | ||
class CLIAnalysisReport : public AnalysisReport { | ||
public: | ||
CLIAnalysisReport(bool verbose, std::string templateFormat, std::string templateLocation, std::ofstream* errorOutput); | ||
void addFinding(const ErrorMessage msg) override; | ||
std::string serialize() override; | ||
|
||
private: | ||
bool mVerbose; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have the feeling these members can be const. |
||
std::string mTemplateFormat; | ||
std::string mTemplateLocation; | ||
std::ofstream *mErrorOutput; | ||
}; | ||
|
||
#endif //CLI_ANALYSIS_REPORT_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for the include. Just forward declare
ErrorMessage
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure? I tried forward-declaring
ErrorMessage
as such:But it then failed to build.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes because you pass ErrorMessage by value to addFinding. It should be passed by const reference.. then I believe a forward declaration will be enough.