Skip to content

Display inline suppressions in XML output #7581

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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;
Copy link
Owner

Choose a reason for hiding this comment

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

it feels like it might be possible to reuse the SuppressionList::dump.
both inline and non-inline suppressions should be stored in the xml file and it's not required that they are stored separately.


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',
Copy link
Owner

Choose a reason for hiding this comment

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

I don't remember if some -j2 and -j1 is injected somehow in the CI. but it would make sense to test that this works with multiple jobs also.

'--inline-suppr',
'--xml-version=3',
Copy link
Owner

Choose a reason for hiding this comment

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

I suggest that os.path.join is used instead of direct string concatenation.

f'{__proj_inline_suppres_path}template.cpp'
]

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