Skip to content

Commit 954400a

Browse files
committed
Add simple xml debug output. When both --xml and --debug is used there will be xml debug output.
1 parent 40030ce commit 954400a

5 files changed

Lines changed: 57 additions & 7 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,6 +2074,20 @@ void SymbolDatabase::printOut(const char *title) const
20742074
}
20752075
}
20762076

2077+
void SymbolDatabase::printXml() const
2078+
{
2079+
// Scopes..
2080+
for (std::list<Scope>::const_iterator scope = scopeList.begin(); scope != scopeList.end(); ++scope) {
2081+
std::cout << "<scope";
2082+
std::cout << " id=\"" << &*scope << "\"";
2083+
std::cout << " type=\"" << scope->type << "\"";
2084+
if (!scope->className.empty())
2085+
std::cout << " className=\"" << scope->className << "\"";
2086+
std::cout << " nestedIn=\"" << scope->nestedIn << "\"";
2087+
std::cout << "/>" << std::endl;
2088+
}
2089+
}
2090+
20772091
//---------------------------------------------------------------------------
20782092

20792093
void Function::addArguments(const SymbolDatabase *symbolDatabase, const Scope *scope)

lib/symboldatabase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ class CPPCHECKLIB SymbolDatabase {
836836

837837
void printOut(const char * title = NULL) const;
838838
void printVariable(const Variable *var, const char *indent) const;
839+
void printXml() const;
839840

840841
bool isCPP() const;
841842

lib/token.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,17 +1083,42 @@ std::string Token::expressionString() const
10831083

10841084
}
10851085

1086-
void Token::printAst(bool verbose) const
1086+
static std::string astStringXml(const Token *tok, std::size_t indent)
1087+
{
1088+
const std::string strindent(indent, ' ');
1089+
1090+
if (!tok->astOperand1() && !tok->astOperand2()) {
1091+
std::ostringstream ret;
1092+
ret << strindent << "<token text=\"" << tok->str() << "\"";
1093+
if (tok->varId() > 0U)
1094+
ret << " varId=\"" << MathLib::toString(tok->varId()) << "\"";
1095+
ret << "/>";
1096+
return ret.str();
1097+
}
1098+
1099+
std::string ret = strindent + "<token text=\"" + tok->str() + "\">\n";
1100+
if (tok->astOperand1())
1101+
ret += astStringXml(tok->astOperand1(),indent+2U) + '\n';
1102+
if (tok->astOperand2())
1103+
ret += astStringXml(tok->astOperand2(),indent+2U) + '\n';
1104+
return ret + strindent + "</token>";
1105+
}
1106+
1107+
void Token::printAst(bool verbose, bool xml) const
10871108
{
10881109
bool title = false;
10891110

10901111
bool print = true;
10911112
for (const Token *tok = this; tok; tok = tok->next()) {
10921113
if (print && tok->_astOperand1) {
1093-
if (!title)
1114+
if (!title && !xml)
10941115
std::cout << "\n\n##AST" << std::endl;
10951116
title = true;
1096-
if (verbose)
1117+
if (xml) {
1118+
std::cout << "<ast scope=\"" << tok->scope() << "\">" << std::endl;
1119+
std::cout << astStringXml(tok->astTop(), 2U) << std::endl;
1120+
std::cout << "</ast>" << std::endl;
1121+
} else if (verbose)
10971122
std::cout << tok->astTop()->astStringVerbose(0,0) << std::endl;
10981123
else
10991124
std::cout << tok->astTop()->astString(" ") << std::endl;

lib/token.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ class CPPCHECKLIB Token {
811811

812812
std::string expressionString() const;
813813

814-
void printAst(bool verbose) const;
814+
void printAst(bool verbose, bool xml) const;
815815

816816
void printValueFlow() const;
817817
};

lib/tokenize.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3738,10 +3738,20 @@ void Tokenizer::printDebugOutput() const
37383738
if (_settings->debug) {
37393739
list.front()->printOut(0, list.getFiles());
37403740

3741-
if (_settings->_verbose && _symbolDatabase)
3742-
_symbolDatabase->printOut("Symbol database");
3741+
if (_settings->_xml)
3742+
std::cout << "<dump>" << std::endl;
37433743

3744-
list.front()->printAst(_settings->_verbose);
3744+
if (_symbolDatabase) {
3745+
if (_settings->_xml)
3746+
_symbolDatabase->printXml();
3747+
else if (_settings->_verbose)
3748+
_symbolDatabase->printOut("Symbol database");
3749+
}
3750+
3751+
list.front()->printAst(_settings->_verbose, _settings->_xml);
3752+
3753+
if (_settings->_xml)
3754+
std::cout << "</dump>" << std::endl;
37453755

37463756
list.front()->printValueFlow();
37473757
}

0 commit comments

Comments
 (0)