Skip to content
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

Closed
wants to merge 46 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
8f10eef
Refactor XML output through the new `XMLAnalysisReport` class.
mario-campos Dec 15, 2022
6f0b5f0
Refactor out CLI error messages into the new class `CLIAnalysisReport`.
mario-campos Dec 16, 2022
e736e59
Change snake-cased `add_finding` to camelCased `addFinding`.
mario-campos Dec 16, 2022
963267b
Add SARIF output format with the new `SARIFAnalysisReport` class.
mario-campos Dec 16, 2022
b495ccd
Reformat files to fix warnings.
mario-campos Dec 16, 2022
44ea258
Include externals/picojson for building cppcheck CLI with cmake.
mario-campos Dec 17, 2022
918d9cf
Abstract classes must implement a destructor, even if its virtual.
mario-campos Dec 17, 2022
f92e1fd
Add a parameter name to the XMLAnalysisReport method.
mario-campos Dec 17, 2022
a5298d3
Use std::unique_ptr instead of new/free.
mario-campos Dec 17, 2022
a1f442d
Output severity in SARIF report.
mario-campos Dec 17, 2022
349dc0b
Output precision in SARIF report.
mario-campos Dec 17, 2022
c501cdd
Output line/column numbers in SARIF report.
mario-campos Dec 17, 2022
ca7214f
Unify spacing and alignment in std::map literal.
mario-campos Dec 17, 2022
b81988e
Unify spacing around std::map literals.
mario-campos Dec 17, 2022
df790fd
Change static methods to static functions.
mario-campos Dec 17, 2022
b1b7239
Fix SARIF report by making `runs` property an array.
mario-campos Dec 18, 2022
6835c6a
Include Cppcheck homepage URL to SARIF report.
mario-campos Dec 18, 2022
d92b72b
Add SARIFAnalysisReport documentation as comment.
mario-campos Dec 18, 2022
551f7ed
Add missing <memory> include to use `std::unique_ptr`.
mario-campos Dec 19, 2022
5f13e12
Fix `switch`/`case` alignment to please uncrustify.
mario-campos Dec 19, 2022
9a73d59
Fix clang-tidy findings.
mario-campos Dec 19, 2022
9b5310f
Add analysis report objects to Makefile's testrunner target.
mario-campos Dec 19, 2022
c7a9611
Fix --errorlist by hoisting mReport assignment earlier in the code.
mario-campos Dec 19, 2022
b4058ca
Check (and error) if XML and SARIF outputs are both specified.
mario-campos Dec 19, 2022
915d9fb
Fix XML pretty-print by appending new-line to XML error message.
mario-campos Dec 19, 2022
455ab5f
Add CLI system test to test that the SARIF file has expected structur…
mario-campos Dec 19, 2022
a4671e5
Rename *AnalysisReport::emit() to serialize().
mario-campos Dec 19, 2022
2e88265
Fix forgotten `sarif` field initialization in settings.cpp.
mario-campos Dec 20, 2022
3a647e5
Refactor duplicate-branch conditional statement.
mario-campos Dec 20, 2022
4ecafee
Put `sarif` property in alphabetical order of other properties.
mario-campos Dec 20, 2022
a22a1ac
Refactor SARIFAnalysisReport::addFinding() from 2 to 1 line!
mario-campos Dec 20, 2022
1a20b60
Fix XML alignment by adding missing newline character.
mario-campos Dec 22, 2022
d1c2d83
Undo changes to merge CLI findings into separate class.
mario-campos Dec 22, 2022
13ce02e
`std::move` more objects in sarifanalysisreport.cpp.
mario-campos Jan 10, 2023
03d91ac
Use range-based for loop for SARIF generation.
mario-campos Jan 10, 2023
e14ff71
Use constant reference.
mario-campos Jan 10, 2023
74950dc
Improve error message to be more helpful.
mario-campos Jan 10, 2023
3abccfc
Add CLIAnalysisReport class for outputting findings to CLI.
mario-campos Jan 10, 2023
1cc3516
Indent the member initialization for uncrusty.
mario-campos Jan 10, 2023
21eba3b
Fix the extra new-line character that was added.
mario-campos Jan 11, 2023
b341465
Use `--output-format` instead of `--sarif`.
mario-campos Jan 11, 2023
1b32554
Use move semantics for SARIF generation if PicoJSON supports it.
mario-campos Jan 11, 2023
eacd087
Fix tests by using `--output-format=xml` instead of `--xml`.
mario-campos Jan 11, 2023
9995bb1
Forgot to include windows.h for Windows OSes.
mario-campos Jan 11, 2023
e53984e
Fix selfcheck and clang-tidy warnings about passing std::string by va…
mario-campos Jan 11, 2023
3b2fda2
Need to link the *analysisreport.o object files for testrunner.
mario-campos Jan 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,19 @@ LIBOBJ = $(libcppdir)/analyzerinfo.o \
EXTOBJ = externals/simplecpp/simplecpp.o \
externals/tinyxml2/tinyxml2.o

CLIOBJ = cli/cmdlineparser.o \
CLIOBJ = cli/clianalysisreport.o \
cli/cmdlineparser.o \
cli/cppcheckexecutor.o \
cli/cppcheckexecutorseh.o \
cli/cppcheckexecutorsig.o \
cli/executor.o \
cli/filelister.o \
cli/main.o \
cli/processexecutor.o \
cli/sarifanalysisreport.o \
cli/stacktrace.o \
cli/threadexecutor.o
cli/threadexecutor.o \
cli/xmlanalysisreport.o

TESTOBJ = test/options.o \
test/test64bit.o \
Expand Down Expand Up @@ -338,7 +341,7 @@ cppcheck: $(LIBOBJ) $(CLIOBJ) $(EXTOBJ)

all: cppcheck testrunner

testrunner: $(TESTOBJ) $(LIBOBJ) $(EXTOBJ) cli/executor.o cli/processexecutor.o cli/threadexecutor.o cli/cmdlineparser.o cli/cppcheckexecutor.o cli/cppcheckexecutorseh.o cli/cppcheckexecutorsig.o cli/stacktrace.o cli/filelister.o
testrunner: $(TESTOBJ) $(LIBOBJ) $(EXTOBJ) cli/executor.o cli/processexecutor.o cli/threadexecutor.o cli/cmdlineparser.o cli/cppcheckexecutor.o cli/cppcheckexecutorseh.o cli/cppcheckexecutorsig.o cli/stacktrace.o cli/filelister.o cli/clianalysisreport.o cli/xmlanalysisreport.o cli/sarifanalysisreport.o
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $^ $(LIBS) $(LDFLAGS) $(RDYNAMIC)

test: all
Expand Down Expand Up @@ -621,16 +624,19 @@ $(libcppdir)/utils.o: lib/utils.cpp lib/config.h lib/utils.h
$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/analyzer.h lib/astutils.h lib/calculate.h lib/check.h lib/checkuninitvar.h lib/color.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/forwardanalyzer.h lib/importproject.h lib/infer.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/programmemory.h lib/reverseanalyzer.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/valueflow.cpp

cli/cmdlineparser.o: cli/cmdlineparser.cpp cli/cmdlineparser.h cli/cppcheckexecutor.h cli/filelister.h externals/tinyxml2/tinyxml2.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h
cli/clianalysisreport.o: cli/clianalysisreport.cpp cli/analysisreport.h cli/clianalysisreport.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/clianalysisreport.cpp

cli/cmdlineparser.o: cli/cmdlineparser.cpp cli/analysisreport.h cli/cmdlineparser.h cli/cppcheckexecutor.h cli/filelister.h externals/tinyxml2/tinyxml2.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cmdlineparser.cpp

cli/cppcheckexecutor.o: cli/cppcheckexecutor.cpp cli/cmdlineparser.h cli/cppcheckexecutor.h cli/cppcheckexecutorseh.h cli/cppcheckexecutorsig.h cli/executor.h cli/filelister.h cli/processexecutor.h cli/threadexecutor.h externals/simplecpp/simplecpp.h lib/analyzerinfo.h lib/check.h lib/checkunusedfunctions.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h
cli/cppcheckexecutor.o: cli/cppcheckexecutor.cpp cli/analysisreport.h cli/clianalysisreport.h cli/cmdlineparser.h cli/cppcheckexecutor.h cli/cppcheckexecutorseh.h cli/cppcheckexecutorsig.h cli/executor.h cli/filelister.h cli/processexecutor.h cli/sarifanalysisreport.h cli/threadexecutor.h cli/xmlanalysisreport.h externals/simplecpp/simplecpp.h lib/analyzerinfo.h lib/check.h lib/checkunusedfunctions.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cppcheckexecutor.cpp

cli/cppcheckexecutorseh.o: cli/cppcheckexecutorseh.cpp cli/cppcheckexecutor.h cli/cppcheckexecutorseh.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h lib/utils.h
cli/cppcheckexecutorseh.o: cli/cppcheckexecutorseh.cpp cli/analysisreport.h cli/cppcheckexecutor.h cli/cppcheckexecutorseh.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h lib/utils.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cppcheckexecutorseh.cpp

cli/cppcheckexecutorsig.o: cli/cppcheckexecutorsig.cpp cli/cppcheckexecutor.h cli/cppcheckexecutorsig.h cli/stacktrace.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h
cli/cppcheckexecutorsig.o: cli/cppcheckexecutorsig.cpp cli/analysisreport.h cli/cppcheckexecutor.h cli/cppcheckexecutorsig.h cli/stacktrace.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cppcheckexecutorsig.cpp

cli/executor.o: cli/executor.cpp cli/executor.h
Expand All @@ -639,18 +645,24 @@ cli/executor.o: cli/executor.cpp cli/executor.h
cli/filelister.o: cli/filelister.cpp cli/filelister.h lib/config.h lib/path.h lib/pathmatch.h lib/utils.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/filelister.cpp

cli/main.o: cli/main.cpp cli/cppcheckexecutor.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h
cli/main.o: cli/main.cpp cli/analysisreport.h cli/cppcheckexecutor.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/main.cpp

cli/processexecutor.o: cli/processexecutor.cpp cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h
cli/processexecutor.o: cli/processexecutor.cpp cli/analysisreport.h cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/processexecutor.cpp

cli/sarifanalysisreport.o: cli/sarifanalysisreport.cpp cli/analysisreport.h cli/sarifanalysisreport.h externals/picojson/picojson.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h
$(CXX) ${INCLUDE_FOR_CLI} -isystem externals/picojson $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/sarifanalysisreport.cpp

cli/stacktrace.o: cli/stacktrace.cpp cli/stacktrace.h lib/config.h lib/utils.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/stacktrace.cpp

cli/threadexecutor.o: cli/threadexecutor.cpp cli/cppcheckexecutor.h cli/executor.h cli/threadexecutor.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h
cli/threadexecutor.o: cli/threadexecutor.cpp cli/analysisreport.h cli/cppcheckexecutor.h cli/executor.h cli/threadexecutor.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/threadexecutor.cpp

cli/xmlanalysisreport.o: cli/xmlanalysisreport.cpp cli/analysisreport.h cli/xmlanalysisreport.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/xmlanalysisreport.cpp

test/options.o: test/options.cpp test/options.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/options.cpp

Expand Down Expand Up @@ -687,7 +699,7 @@ test/testclangimport.o: test/testclangimport.cpp lib/clangimport.h lib/color.h l
test/testclass.o: test/testclass.cpp externals/tinyxml2/tinyxml2.h lib/check.h lib/checkclass.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testclass.cpp

test/testcmdlineparser.o: test/testcmdlineparser.cpp cli/cmdlineparser.h cli/cppcheckexecutor.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h test/redirect.h test/testsuite.h
test/testcmdlineparser.o: test/testcmdlineparser.cpp cli/analysisreport.h cli/cmdlineparser.h cli/cppcheckexecutor.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h test/redirect.h test/testsuite.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testcmdlineparser.cpp

test/testcondition.o: test/testcondition.cpp externals/simplecpp/simplecpp.h externals/tinyxml2/tinyxml2.h lib/check.h lib/checkcondition.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h
Expand Down Expand Up @@ -795,7 +807,7 @@ test/testsuite.o: test/testsuite.cpp lib/color.h lib/config.h lib/errorlogger.h
test/testsummaries.o: test/testsummaries.cpp lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/summaries.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testsummaries.cpp

test/testsuppressions.o: test/testsuppressions.cpp cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h cli/threadexecutor.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h test/testutils.h
test/testsuppressions.o: test/testsuppressions.cpp cli/analysisreport.h cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h cli/threadexecutor.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h test/testutils.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testsuppressions.cpp

test/testsymboldatabase.o: test/testsymboldatabase.cpp externals/tinyxml2/tinyxml2.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h test/testutils.h
Expand Down
1 change: 1 addition & 0 deletions cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ else()
target_include_directories(cli_objs SYSTEM PRIVATE ${tinyxml2_INCLUDE_DIRS})
endif()
target_include_directories(cli_objs PRIVATE ${PROJECT_SOURCE_DIR}/externals/simplecpp/)
target_include_directories(cli_objs PRIVATE ${PROJECT_SOURCE_DIR}/externals/picojson/)

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 14)
# false positive warning in up to Clang 13 - caused by FD_ZERO macro
Expand Down
46 changes: 46 additions & 0 deletions cli/analysisreport.h
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"
Copy link
Collaborator

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.

Copy link
Author

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:

class ErrorMessage;

But it then failed to build.

Copy link
Owner

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.


#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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the constructor for AnalysisReport could take a output stream argument:

std::ostream *outputStream

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change parameter type to const ErrorMessage& msg

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it used to be const ErrorMessage&, but @firewave suggested passing by value so it could be std::moved.

Copy link
Owner

Choose a reason for hiding this comment

The 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
66 changes: 66 additions & 0 deletions cli/clianalysisreport.cpp
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;
}
}
41 changes: 41 additions & 0 deletions cli/clianalysisreport.h
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;
Copy link
Owner

Choose a reason for hiding this comment

The 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
Loading