Skip to content

Commit 5e6e050

Browse files
committed
improved --debug-analyzerinfo output when discarding results
1 parent 7aa4e2c commit 5e6e050

File tree

3 files changed

+24
-34
lines changed

3 files changed

+24
-34
lines changed

lib/analyzerinfo.cpp

Lines changed: 16 additions & 26 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,11 +170,15 @@ 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)
189176
std::cout << "skipping analysis - loaded " << errors.size() << " cached finding(s) from '" << analyzerInfoFile << "'" << 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)

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/testanalyzerinformation.cpp

Lines changed: 7 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("", 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("", 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("", 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("", AnalyzerInformationTest::skipAnalysis(doc, 99, errorList));
226226
ASSERT_EQUALS(0, errorList.size());
227227
}
228228

@@ -234,7 +234,7 @@ 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("", AnalyzerInformationTest::skipAnalysis(doc, 100, errorList));
238238
ASSERT_EQUALS(0, errorList.size());
239239
}
240240
}

0 commit comments

Comments
 (0)