Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 30 additions & 1 deletion cli/cppcheckexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,33 @@ namespace {
return mCtuInfo;
}

void reportInlineSuppressions(const Suppressions &suppressions) const
{
const auto &suppressionList = suppressions.nomsg.getSuppressions();
const bool hasInline = std::any_of(suppressionList.cbegin(), suppressionList.cend(),
[](const SuppressionList::Suppression &s) {
return s.isInline;
});
if (!hasInline)
return;

auto &out = mErrorOutput ? *mErrorOutput : std::cerr;
out << " <inline-suppressions>" << std::endl;
Comment thread
ludviggunne marked this conversation as resolved.

for (const auto &suppression : suppressionList) {
if (!suppression.isInline)
continue;
const std::string msg =
" <suppression file=\"" + ErrorLogger::toxml(suppression.fileName) +
"\" id=\"" + suppression.errorId +
"\" lineNumber=\"" + std::to_string(suppression.lineNumber) +
"\"/>";
out << msg << std::endl;
}

out << " </inline-suppressions>" << std::endl;
}

private:
/**
* Information about progress is directed here. This should be
Expand Down Expand Up @@ -509,8 +536,10 @@ int CppCheckExecutor::check_internal(const Settings& settings, Suppressions& sup
stdLogger.writeCheckersReport(supprs);

if (settings.outputFormat == Settings::OutputFormat::xml) {
if (settings.xml_version == 3)
if (settings.xml_version == 3) {
stdLogger.reportMetrics();
stdLogger.reportInlineSuppressions(cppcheck.getSuppressions());
}
stdLogger.reportErr(ErrorMessage::getXMLFooter(settings.xml_version));
}

Expand Down
5 changes: 5 additions & 0 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2121,6 +2121,11 @@ void CppCheck::printTimerResults(SHOWTIME_MODES mode)
s_timerResults.showResults(mode);
}

const Suppressions &CppCheck::getSuppressions() const
{
return mSuppressions;
}

bool CppCheck::isPremiumCodingStandardId(const std::string& id) const {
if (mSettings.premiumArgs.find("--misra") != std::string::npos) {
if (startsWith(id, "misra-") || startsWith(id, "premium-misra-"))
Expand Down
2 changes: 2 additions & 0 deletions lib/cppcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class CPPCHECKLIB CppCheck {
static void resetTimerResults();
static void printTimerResults(SHOWTIME_MODES mode);

const Suppressions &getSuppressions() const;

private:
void purgedConfigurationMessage(const std::string &file, const std::string& configuration);

Expand Down
42 changes: 41 additions & 1 deletion test/cli/inline-suppress_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,4 +456,44 @@ def test_unmatched_cfg():
'{}cfg.c:14:0: information: Unmatched suppression: id [unmatchedSuppression]'.format(__proj_inline_suppres_path),
]
assert stdout == ''
assert ret == 0, stdout
assert ret == 0, stdout

def test_xml_report_stdout(tmpdir):
args = [
'-q',
'--inline-suppr',
Comment thread
ludviggunne marked this conversation as resolved.
'--xml-version=3',
f'{__proj_inline_suppres_path}template.cpp'
Comment thread
ludviggunne marked this conversation as resolved.
]

ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
lines = stderr.splitlines()

assert ret == 0
assert stdout == ''

assert ' <inline-suppressions>' in lines
assert f' <suppression file="{__proj_inline_suppres_path}template.cpp" id="unusedFunction" lineNumber="9"/>' in lines
assert ' </inline-suppressions>' in lines

def test_xml_report_file(tmpdir):
output_file = os.path.join(tmpdir, "results.xml")
args = [
'-q',
'--inline-suppr',
'--xml-version=3',
f'--output-file={output_file}',
f'{__proj_inline_suppres_path}template.cpp'
]

ret, stdout, stderr = cppcheck(args, cwd=__script_dir)

assert ret == 0
assert stdout == ''

with open(output_file, 'r') as file:
xml = file.read()

assert ' <inline-suppressions>' in xml
assert f' <suppression file="{__proj_inline_suppres_path}template.cpp" id="unusedFunction" lineNumber="9"/>' in xml
assert ' </inline-suppressions>' in xml
Loading