Skip to content

Commit 8e1d6dc

Browse files
committed
improved --debug-analyzerinfo output [skip ci]
1 parent 6926f96 commit 8e1d6dc

File tree

4 files changed

+65
-45
lines changed

4 files changed

+65
-45
lines changed

lib/analyzerinfo.cpp

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,32 +83,20 @@ void AnalyzerInformation::close()
8383
}
8484
}
8585

86-
bool AnalyzerInformation::skipAnalysis(const tinyxml2::XMLDocument &analyzerInfoDoc, std::size_t hash, std::list<ErrorMessage> &errors, bool debug)
86+
std::string AnalyzerInformation::skipAnalysis(const tinyxml2::XMLDocument &analyzerInfoDoc, std::size_t hash, std::list<ErrorMessage> &errors)
8787
{
8888
const tinyxml2::XMLElement * const rootNode = analyzerInfoDoc.FirstChildElement();
89-
if (rootNode == nullptr) {
90-
if (debug)
91-
std::cout << "discarding cached result - no root node found" << std::endl;
92-
return false;
93-
}
89+
if (rootNode == nullptr)
90+
return "no root node found";
9491

95-
if (strcmp(rootNode->Name(), "analyzerinfo") != 0) {
96-
if (debug)
97-
std::cout << "discarding cached result - unexpected root node" << std::endl;
98-
return false;
99-
}
92+
if (strcmp(rootNode->Name(), "analyzerinfo") != 0)
93+
return "unexpected root node";
10094

10195
const char * const attr = rootNode->Attribute("hash");
102-
if (!attr) {
103-
if (debug)
104-
std::cout << "discarding cached result - no 'hash' attribute found" << std::endl;
105-
return false;
106-
}
107-
if (attr != std::to_string(hash)) {
108-
if (debug)
109-
std::cout << "discarding cached result - hash mismatch" << std::endl;
110-
return false;
111-
}
96+
if (!attr)
97+
return "no 'hash' attribute found";
98+
if (attr != std::to_string(hash))
99+
return "hash mismatch";
112100

113101
for (const tinyxml2::XMLElement *e = rootNode->FirstChildElement(); e; e = e->NextSiblingElement()) {
114102
if (std::strcmp(e->Name(), "error") != 0)
@@ -125,17 +113,15 @@ bool AnalyzerInformation::skipAnalysis(const tinyxml2::XMLDocument &analyzerInfo
125113
{
126114
// cppcheck-suppress useStlAlgorithm
127115
if (e->Attribute("id", id)) {
128-
if (debug)
129-
std::cout << "discarding cached result - '" << id << "' encountered" << std::endl;
130116
errors.clear();
131-
return false;
117+
return std::string("'") + id + "' encountered";
132118
}
133119
}
134120

135121
errors.emplace_back(e);
136122
}
137123

138-
return true;
124+
return "";
139125
}
140126

141127
std::string AnalyzerInformation::getAnalyzerInfoFileFromFilesTxt(std::istream& filesTxt, const std::string &sourcefile, const std::string &cfg, int fsFileId)
@@ -184,18 +170,22 @@ bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::st
184170
tinyxml2::XMLDocument analyzerInfoDoc;
185171
const tinyxml2::XMLError xmlError = analyzerInfoDoc.LoadFile(analyzerInfoFile.c_str());
186172
if (xmlError == tinyxml2::XML_SUCCESS) {
187-
if (skipAnalysis(analyzerInfoDoc, hash, errors, debug)) {
173+
const std::string err = skipAnalysis(analyzerInfoDoc, hash, errors);
174+
if (err.empty()) {
188175
if (debug)
189-
std::cout << "skipping analysis - loaded " << errors.size() << " cached finding(s) from '" << analyzerInfoFile << "'" << std::endl;
176+
std::cout << "skipping analysis - loaded " << errors.size() << " cached finding(s) from '" << analyzerInfoFile << "' for '" << sourcefile << "'" << std::endl;
190177
return false;
191178
}
179+
if (debug) {
180+
std::cout << "discarding cached result from '" << analyzerInfoFile << "' for '" << sourcefile << "' - " << err << std::endl;
181+
}
192182
}
193183
else if (xmlError != tinyxml2::XML_ERROR_FILE_NOT_FOUND) {
194184
if (debug)
195-
std::cout << "discarding cached result - failed to load '" << analyzerInfoFile << "' (" << tinyxml2::XMLDocument::ErrorIDToName(xmlError) << ")" << std::endl;
185+
std::cout << "discarding cached result - failed to load '" << analyzerInfoFile << "' for '" << sourcefile << "' (" << tinyxml2::XMLDocument::ErrorIDToName(xmlError) << ")" << std::endl;
196186
}
197187
else if (debug)
198-
std::cout << "no cached result '" << analyzerInfoFile << "' found" << std::endl;
188+
std::cout << "no cached result '" << analyzerInfoFile << "' for '" << sourcefile << "' found" << std::endl;
199189
}
200190

201191
mOutputStream.open(analyzerInfoFile);

lib/analyzerinfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class CPPCHECKLIB AnalyzerInformation {
8787

8888
static std::string getAnalyzerInfoFileFromFilesTxt(std::istream& filesTxt, const std::string &sourcefile, const std::string &cfg, int fsFileId);
8989

90-
static bool skipAnalysis(const tinyxml2::XMLDocument &analyzerInfoDoc, std::size_t hash, std::list<ErrorMessage> &errors, bool debug = false);
90+
static std::string skipAnalysis(const tinyxml2::XMLDocument &analyzerInfoDoc, std::size_t hash, std::list<ErrorMessage> &errors);
9191

9292
private:
9393
std::ofstream mOutputStream;

test/cli/other_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4122,60 +4122,60 @@ def run_and_assert_cppcheck(stdout_exp):
41224122

41234123
# no cached results
41244124
run_and_assert_cppcheck([
4125-
"no cached result '{}' found".format(test_a1_file_s)
4125+
"no cached result '{}' for '{}' found".format(test_a1_file_s, test_file)
41264126
])
41274127

41284128
# cached results
41294129
run_and_assert_cppcheck([
4130-
"skipping analysis - loaded 1 cached finding(s) from '{}'".format(test_a1_file_s)
4130+
"skipping analysis - loaded 1 cached finding(s) from '{}' for '{}'".format(test_a1_file_s, test_file)
41314131
])
41324132

41334133
# modified file
41344134
with open(test_file, 'a') as f:
41354135
f.write('\n#define DEF')
41364136

41374137
run_and_assert_cppcheck([
4138-
"discarding cached result - hash mismatch" # TODO: add filename
4138+
"discarding cached result from '{}' for '{}' - hash mismatch".format(test_a1_file_s, test_file)
41394139
])
41404140

41414141
# invalid XML
41424142
with open(test_a1_file, 'a') as f:
41434143
f.write('.')
41444144

41454145
run_and_assert_cppcheck([
4146-
"discarding cached result - failed to load '{}' (XML_ERROR_PARSING_TEXT)".format(test_a1_file_s)
4146+
"discarding cached result - failed to load '{}' for '{}' (XML_ERROR_PARSING_TEXT)".format(test_a1_file_s, test_file)
41474147
])
41484148

41494149
# missing root node
41504150
with open(test_a1_file, 'w') as f:
41514151
f.write('<?xml version="1.0"?>')
41524152

41534153
run_and_assert_cppcheck([
4154-
"discarding cached result - no root node found" # TODO: add filename
4154+
"discarding cached result from '{}' for '{}' - no root node found".format(test_a1_file_s, test_file)
41554155
])
41564156

41574157
# mismatched root node
41584158
with open(test_a1_file, 'w') as f:
41594159
f.write('<?xml version="1.0"?><root/>')
41604160

41614161
run_and_assert_cppcheck([
4162-
"discarding cached result - unexpected root node" # TODO: add filename
4162+
"discarding cached result from '{}' for '{}' - unexpected root node".format(test_a1_file_s, test_file)
41634163
])
41644164

41654165
# missing 'hash' attribute
41664166
with open(test_a1_file, 'w') as f:
41674167
f.write('<?xml version="1.0"?><analyzerinfo/>')
41684168

41694169
run_and_assert_cppcheck([
4170-
"discarding cached result - no 'hash' attribute found" # TODO: add filename
4170+
"discarding cached result from '{}' for '{}' - no 'hash' attribute found".format(test_a1_file_s, test_file)
41714171
])
41724172

41734173
# invalid 'hash' attribute
41744174
with open(test_a1_file, 'w') as f:
41754175
f.write('<?xml version="1.0"?><analyzerinfo hash="hash"/>')
41764176

41774177
run_and_assert_cppcheck([
4178-
"discarding cached result - hash mismatch" # TODO: add filename
4178+
"discarding cached result from '{}' for '{}' - hash mismatch".format(test_a1_file_s, test_file)
41794179
])
41804180

41814181
# TODO:

test/testanalyzerinformation.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class TestAnalyzerInformation : public TestFixture {
126126
);
127127
ASSERT_EQUALS(tinyxml2::XML_SUCCESS, xmlError);
128128

129-
ASSERT_EQUALS(false, AnalyzerInformationTest::skipAnalysis(doc, 100, errorList));
129+
ASSERT_EQUALS("'premium-invalidLicense' encountered", AnalyzerInformationTest::skipAnalysis(doc, 100, errorList));
130130
ASSERT_EQUALS(0, errorList.size());
131131
}
132132

@@ -145,7 +145,7 @@ class TestAnalyzerInformation : public TestFixture {
145145
);
146146
ASSERT_EQUALS(tinyxml2::XML_SUCCESS, xmlError);
147147

148-
ASSERT_EQUALS(false, AnalyzerInformationTest::skipAnalysis(doc, 100, errorList));
148+
ASSERT_EQUALS("'premium-internalError' encountered", AnalyzerInformationTest::skipAnalysis(doc, 100, errorList));
149149
ASSERT_EQUALS(0, errorList.size());
150150
}
151151

@@ -164,7 +164,7 @@ class TestAnalyzerInformation : public TestFixture {
164164
);
165165
ASSERT_EQUALS(tinyxml2::XML_SUCCESS, xmlError);
166166

167-
ASSERT_EQUALS(false, AnalyzerInformationTest::skipAnalysis(doc, 100, errorList));
167+
ASSERT_EQUALS("'internalError' encountered", AnalyzerInformationTest::skipAnalysis(doc, 100, errorList));
168168
ASSERT_EQUALS(0, errorList.size());
169169
}
170170

@@ -185,7 +185,7 @@ class TestAnalyzerInformation : public TestFixture {
185185
);
186186
ASSERT_EQUALS(tinyxml2::XML_SUCCESS, xmlError);
187187

188-
ASSERT_EQUALS(true, AnalyzerInformationTest::skipAnalysis(doc, 100, errorList));
188+
ASSERT_EQUALS("", AnalyzerInformationTest::skipAnalysis(doc, 100, errorList));
189189
ASSERT_EQUALS(1, errorList.size());
190190
}
191191

@@ -201,7 +201,7 @@ class TestAnalyzerInformation : public TestFixture {
201201
);
202202
ASSERT_EQUALS(tinyxml2::XML_SUCCESS, xmlError);
203203

204-
ASSERT_EQUALS(true, AnalyzerInformationTest::skipAnalysis(doc, 100, errorList));
204+
ASSERT_EQUALS("", AnalyzerInformationTest::skipAnalysis(doc, 100, errorList));
205205
ASSERT_EQUALS(0, errorList.size());
206206
}
207207

@@ -222,7 +222,7 @@ class TestAnalyzerInformation : public TestFixture {
222222
);
223223
ASSERT_EQUALS(tinyxml2::XML_SUCCESS, xmlError);
224224

225-
ASSERT_EQUALS(false, AnalyzerInformationTest::skipAnalysis(doc, 99, errorList));
225+
ASSERT_EQUALS("hash mismatch", AnalyzerInformationTest::skipAnalysis(doc, 99, errorList));
226226
ASSERT_EQUALS(0, errorList.size());
227227
}
228228

@@ -234,7 +234,37 @@ class TestAnalyzerInformation : public TestFixture {
234234
const tinyxml2::XMLError xmlError = doc.Parse("");
235235
ASSERT_EQUALS(tinyxml2::XML_ERROR_EMPTY_DOCUMENT, xmlError);
236236

237-
ASSERT_EQUALS(false, AnalyzerInformationTest::skipAnalysis(doc, 100, errorList));
237+
ASSERT_EQUALS("no root node found", AnalyzerInformationTest::skipAnalysis(doc, 100, errorList));
238+
ASSERT_EQUALS(0, errorList.size());
239+
}
240+
241+
// Unexpected root node
242+
{
243+
std::list<ErrorMessage> errorList;
244+
tinyxml2::XMLDocument doc;
245+
246+
const tinyxml2::XMLError xmlError = doc.Parse(
247+
"<?xml version=\"1.0\"?>"
248+
"<ainfo/>"
249+
);
250+
ASSERT_EQUALS(tinyxml2::XML_SUCCESS, xmlError);
251+
252+
ASSERT_EQUALS("unexpected root node", AnalyzerInformationTest::skipAnalysis(doc, 99, errorList));
253+
ASSERT_EQUALS(0, errorList.size());
254+
}
255+
256+
// No 'hash' attribute found
257+
{
258+
std::list<ErrorMessage> errorList;
259+
tinyxml2::XMLDocument doc;
260+
261+
const tinyxml2::XMLError xmlError = doc.Parse(
262+
"<?xml version=\"1.0\"?>"
263+
"<analyzerinfo/>"
264+
);
265+
ASSERT_EQUALS(tinyxml2::XML_SUCCESS, xmlError);
266+
267+
ASSERT_EQUALS("no 'hash' attribute found", AnalyzerInformationTest::skipAnalysis(doc, 99, errorList));
238268
ASSERT_EQUALS(0, errorList.size());
239269
}
240270
}

0 commit comments

Comments
 (0)