Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.6k
Add SARIF output support.#4651
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
8f10eef6f0b5f0e736e59963267bb495ccd44ea258918d9cff92e1fda5298d3a1f442d349dc0bc501cddca7214fb81988edf790fdb1b72396835c6ad92b72b551f7ed5f13e129a73d599b5310fc7a9611b4058ca915d9fb455ab5fa4671e52e882653a647e54ecafeea22a1ac1a20b60d1c2d8313ce02e03d91ace14ff7174950dc3abccfc1cc351621eba3bb3414651b32554eacd0879995bb1e53984e3b2fda2File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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 { | ||
Collaborator 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; | ||
Collaborator 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 ContributorAuthor 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 Collaborator 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; | ||
Collaborator 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 | ||
Uh oh!
There was an error while loading. Please reload this page.
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
ErrorMessageas 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.