Skip to content

Commit b8dd71c

Browse files
committed
Factorize toxml() into a single member function
lib/symboldatabase.cpp and lib/tokenize.cpp both define a static toxml() function. Make it a single static ErrorLogger::toxml() member function.
1 parent ca3c19c commit b8dd71c

4 files changed

Lines changed: 44 additions & 63 deletions

File tree

lib/errorlogger.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,36 @@ std::string ErrorLogger::ErrorMessage::FileLocation::stringify() const
421421
oss << ']';
422422
return oss.str();
423423
}
424+
425+
std::string ErrorLogger::toxml(const std::string &str)
426+
{
427+
std::ostringstream xml;
428+
const bool isstring(str[0] == '\"');
429+
for (std::size_t i = 0U; i < str.length(); i++) {
430+
char c = str[i];
431+
switch (c) {
432+
case '<':
433+
xml << "&lt;";
434+
break;
435+
case '>':
436+
xml << "&gt;";
437+
break;
438+
case '&':
439+
xml << "&amp;";
440+
break;
441+
case '\"':
442+
xml << "&quot;";
443+
break;
444+
case '\0':
445+
xml << "\\0";
446+
break;
447+
default:
448+
if (!isstring || (c >= ' ' && c <= 'z'))
449+
xml << c;
450+
else
451+
xml << 'x';
452+
break;
453+
}
454+
}
455+
return xml.str();
456+
}

lib/errorlogger.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,13 @@ class CPPCHECKLIB ErrorLogger {
311311
void reportUnmatchedSuppressions(const std::list<Suppressions::SuppressionEntry> &unmatched);
312312

313313
static std::string callStackToString(const std::list<ErrorLogger::ErrorMessage::FileLocation> &callStack);
314+
315+
/**
316+
* Convert XML-sensitive characters into XML entities
317+
* @param str The input string containing XML-sensitive characters
318+
* @return The ouput string containing XML entities
319+
*/
320+
static std::string toxml(const std::string &str);
314321
};
315322

316323
/// @}

lib/symboldatabase.cpp

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,32 +2452,6 @@ void SymbolDatabase::printOut(const char *title) const
24522452
std::cout << std::resetiosflags(std::ios::boolalpha);
24532453
}
24542454

2455-
static std::string toxml(const std::string &str)
2456-
{
2457-
std::ostringstream xml;
2458-
for (std::size_t i = 0U; i < str.length(); i++) {
2459-
char c = str[i];
2460-
switch (c) {
2461-
case '<':
2462-
xml << "&lt;";
2463-
break;
2464-
case '>':
2465-
xml << "&gt;";
2466-
break;
2467-
case '&':
2468-
xml << "&amp;";
2469-
break;
2470-
case '\"':
2471-
xml << "&quot;";
2472-
break;
2473-
default:
2474-
xml << c;
2475-
break;
2476-
}
2477-
}
2478-
return xml.str();
2479-
}
2480-
24812455
void SymbolDatabase::printXml(std::ostream &out) const
24822456
{
24832457
out << std::setiosflags(std::ios::boolalpha);
@@ -2488,7 +2462,7 @@ void SymbolDatabase::printXml(std::ostream &out) const
24882462
out << " id=\"" << &*scope << "\"";
24892463
out << " type=\"" << scope->type << "\"";
24902464
if (!scope->className.empty())
2491-
out << " className=\"" << toxml(scope->className) << "\"";
2465+
out << " className=\"" << ErrorLogger::toxml(scope->className) << "\"";
24922466
if (scope->classStart)
24932467
out << " classStart=\"" << scope->classStart << '\"';
24942468
if (scope->classEnd)
@@ -2504,7 +2478,7 @@ void SymbolDatabase::printXml(std::ostream &out) const
25042478
if (!scope->functionList.empty()) {
25052479
out << " <functionList>" << std::endl;
25062480
for (std::list<Function>::const_iterator function = scope->functionList.begin(); function != scope->functionList.end(); ++function) {
2507-
out << " <function id=\"" << &*function << "\" tokenDef=\"" << function->tokenDef << "\" name=\"" << toxml(function->name()) << '\"';
2481+
out << " <function id=\"" << &*function << "\" tokenDef=\"" << function->tokenDef << "\" name=\"" << ErrorLogger::toxml(function->name()) << '\"';
25082482
if (function->argCount() == 0U)
25092483
out << "/>" << std::endl;
25102484
else {

lib/tokenize.cpp

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3911,39 +3911,6 @@ void Tokenizer::printDebugOutput(unsigned int simplification) const
39113911
}
39123912
}
39133913

3914-
static std::string toxml(const std::string &str)
3915-
{
3916-
std::ostringstream xml;
3917-
const bool isstring(str[0] == '\"');
3918-
for (std::size_t i = 0U; i < str.length(); i++) {
3919-
char c = str[i];
3920-
switch (c) {
3921-
case '<':
3922-
xml << "&lt;";
3923-
break;
3924-
case '>':
3925-
xml << "&gt;";
3926-
break;
3927-
case '&':
3928-
xml << "&amp;";
3929-
break;
3930-
case '\"':
3931-
xml << "&quot;";
3932-
break;
3933-
case '\0':
3934-
xml << "\\0";
3935-
break;
3936-
default:
3937-
if (!isstring || (c >= ' ' && c <= 'z'))
3938-
xml << c;
3939-
else
3940-
xml << 'x';
3941-
break;
3942-
}
3943-
}
3944-
return xml.str();
3945-
}
3946-
39473914
void Tokenizer::dump(std::ostream &out) const
39483915
{
39493916
// Create a xml data dump.
@@ -3953,8 +3920,8 @@ void Tokenizer::dump(std::ostream &out) const
39533920
// tokens..
39543921
out << " <tokenlist>" << std::endl;
39553922
for (const Token *tok = list.front(); tok; tok = tok->next()) {
3956-
out << " <token id=\"" << tok << "\" file=\"" << toxml(list.file(tok)) << "\" linenr=\"" << tok->linenr() << '\"';
3957-
out << " str=\"" << toxml(tok->str()) << '\"';
3923+
out << " <token id=\"" << tok << "\" file=\"" << ErrorLogger::toxml(list.file(tok)) << "\" linenr=\"" << tok->linenr() << '\"';
3924+
out << " str=\"" << ErrorLogger::toxml(tok->str()) << '\"';
39583925
out << " scope=\"" << tok->scope() << '\"';
39593926
if (tok->isName())
39603927
out << " type=\"name\"";

0 commit comments

Comments
 (0)