template simplifier: add cli --debug-template option to print the parsed template information (#1598)

This commit is contained in:
IOBYTE 2019-01-14 02:29:32 -05:00 committed by Daniel Marjamäki
parent 0dbe803d22
commit f0cfe03f7b
5 changed files with 104 additions and 3 deletions

View File

@ -147,6 +147,10 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
else if (std::strcmp(argv[i], "--debug-warnings") == 0) else if (std::strcmp(argv[i], "--debug-warnings") == 0)
mSettings->debugwarnings = true; mSettings->debugwarnings = true;
// Show template information
else if (std::strcmp(argv[i], "--debug-template") == 0)
mSettings->debugtemplate = true;
// dump cppcheck data // dump cppcheck data
else if (std::strcmp(argv[i], "--dump") == 0) else if (std::strcmp(argv[i], "--dump") == 0)
mSettings->dump = true; mSettings->dump = true;

View File

@ -27,6 +27,7 @@ Settings::Settings()
debugSimplified(false), debugSimplified(false),
debugnormal(false), debugnormal(false),
debugwarnings(false), debugwarnings(false),
debugtemplate(false),
dump(false), dump(false),
exceptionHandling(false), exceptionHandling(false),
inconclusive(false), inconclusive(false),

View File

@ -82,6 +82,9 @@ public:
/** @brief Is --debug-warnings given? */ /** @brief Is --debug-warnings given? */
bool debugwarnings; bool debugwarnings;
/** @brief Is --debug-template given? */
bool debugtemplate;
/** @brief Is --dump given? */ /** @brief Is --dump given? */
bool dump; bool dump;
std::string dumpFile; std::string dumpFile;

View File

@ -51,10 +51,20 @@ namespace {
private: private:
const std::string mName; const std::string mName;
}; };
class FindFullName {
public:
explicit FindFullName(const std::string &fullName) : mFullName(fullName) {}
bool operator()(const TemplateSimplifier::TokenAndName &tokenAndName) const {
return tokenAndName.fullName == mFullName;
}
private:
const std::string mFullName;
};
} }
TemplateSimplifier::TokenAndName::TokenAndName(Token *tok, const std::string &s, const std::string &n, const Token *nt, const Token *pe) : TemplateSimplifier::TokenAndName::TokenAndName(Token *tok, const std::string &s, const std::string &n, const Token *nt, const Token *pe) :
token(tok), scope(s), name(n), nameToken(nt), paramEnd(pe), flags(0) token(tok), scope(s), name(n), fullName(s.empty() ? n : (s + " :: " + name)), nameToken(nt), paramEnd(pe), flags(0)
{ {
// only set flags for declaration // only set flags for declaration
if (token && nameToken && paramEnd) { if (token && nameToken && paramEnd) {
@ -72,7 +82,7 @@ TemplateSimplifier::TokenAndName::TokenAndName(Token *tok, const std::string &s,
} }
TemplateSimplifier::TokenAndName::TokenAndName(const TokenAndName& otherTok) : TemplateSimplifier::TokenAndName::TokenAndName(const TokenAndName& otherTok) :
token(otherTok.token), scope(otherTok.scope), name(otherTok.name), token(otherTok.token), scope(otherTok.scope), name(otherTok.name), fullName(otherTok.fullName),
nameToken(otherTok.nameToken), paramEnd(otherTok.paramEnd), flags(otherTok.flags) nameToken(otherTok.nameToken), paramEnd(otherTok.paramEnd), flags(otherTok.flags)
{ {
if (token) if (token)
@ -2175,6 +2185,80 @@ void TemplateSimplifier::fixForwardDeclaredDefaultArgumentValues()
} }
} }
void TemplateSimplifier::printOut(const TokenAndName &tokenAndName, const std::string &indent) const
{
std::cout << indent << "token: ";
if (tokenAndName.token)
std::cout << "\"" << tokenAndName.token->str() << "\" " << mTokenList.fileLine(tokenAndName.token);
else
std::cout << "nullptr";
std::cout << std::endl;
std::cout << indent << "scope: \"" << tokenAndName.scope << "\"" << std::endl;
std::cout << indent << "name: \"" << tokenAndName.name << "\"" << std::endl;
std::cout << indent << "fullName: \"" << tokenAndName.fullName << "\"" << std::endl;
std::cout << indent << "nameToken: ";
if (tokenAndName.nameToken)
std::cout << "\"" << tokenAndName.nameToken->str() << "\" " << mTokenList.fileLine(tokenAndName.nameToken);
else
std::cout << "nullptr";
std::cout << std::endl;
std::cout << indent << "paramEnd: ";
if (tokenAndName.paramEnd)
std::cout << "\"" << tokenAndName.paramEnd->str() << "\" " << mTokenList.fileLine(tokenAndName.paramEnd);
else
std::cout << "nullptr";
std::cout << std::endl;
std::cout << indent << "flags: ";
if (tokenAndName.isClass())
std::cout << " isClass";
if (tokenAndName.isFunction())
std::cout << " isFunction";
if (tokenAndName.isVariable())
std::cout << " isVariable";
if (tokenAndName.isAlias())
std::cout << " isAlias";
if (tokenAndName.isSpecialized())
std::cout << " isSpecialized";
std::cout << std::endl;
if (tokenAndName.token && !tokenAndName.paramEnd && tokenAndName.token->strAt(1) == "<") {
const Token *end = tokenAndName.token->next()->findClosingBracket();
if (end) {
const Token *start = tokenAndName.token->next();
std::cout << indent << "type: ";
while (start && start != end) {
std::cout << start->str();
start = start->next();
}
std::cout << end->str() << std::endl;
}
}
}
void TemplateSimplifier::printOut(const std::string & text) const
{
std::cout << std::endl;
std::cout << text << std::endl;
std::cout << std::endl;
std::cout << "mTemplateDeclarations: " << mTemplateDeclarations.size() << std::endl;
int count = 0;
for (const auto & decl : mTemplateDeclarations) {
std::cout << "mTemplateDeclarations[" << count++ << "]:" << std::endl;
printOut(decl);
}
std::cout << "mTemplateForwardDeclarations: " << mTemplateForwardDeclarations.size() << std::endl;
count = 0;
for (const auto & decl : mTemplateForwardDeclarations) {
std::cout << "mTemplateForwardDeclarations[" << count++ << "]:" << std::endl;
printOut(decl);
}
std::cout << "mTemplateInstantiations: " << mTemplateInstantiations.size() << std::endl;
count = 0;
for (const auto & decl : mTemplateInstantiations) {
std::cout << "mTemplateInstantiations[" << count++ << "]:" << std::endl;
printOut(decl);
}
}
void TemplateSimplifier::simplifyTemplates( void TemplateSimplifier::simplifyTemplates(
const std::time_t maxtime, const std::time_t maxtime,
bool &codeWithTemplates) bool &codeWithTemplates)
@ -2236,6 +2320,9 @@ void TemplateSimplifier::simplifyTemplates(
simplifyTemplateAliases(); simplifyTemplateAliases();
if (mSettings->debugtemplate)
printOut("### Template Simplifier pass " + std::to_string(i + 1) + " ###");
std::set<std::string> expandedtemplates; std::set<std::string> expandedtemplates;
for (std::list<TokenAndName>::reverse_iterator iter1 = mTemplateDeclarations.rbegin(); iter1 != mTemplateDeclarations.rend(); ++iter1) { for (std::list<TokenAndName>::reverse_iterator iter1 = mTemplateDeclarations.rbegin(); iter1 != mTemplateDeclarations.rend(); ++iter1) {

View File

@ -74,12 +74,13 @@ public:
~TokenAndName(); ~TokenAndName();
bool operator == (const TokenAndName & rhs) const { bool operator == (const TokenAndName & rhs) const {
return token == rhs.token && scope == rhs.scope && name == rhs.name && return token == rhs.token && scope == rhs.scope && name == rhs.name && fullName == rhs.fullName &&
nameToken == rhs.nameToken && paramEnd == rhs.paramEnd && flags == rhs.flags; nameToken == rhs.nameToken && paramEnd == rhs.paramEnd && flags == rhs.flags;
} }
Token *token; Token *token;
std::string scope; std::string scope;
std::string name; std::string name;
std::string fullName;
const Token *nameToken; const Token *nameToken;
const Token *paramEnd; const Token *paramEnd;
unsigned int flags; unsigned int flags;
@ -338,6 +339,11 @@ private:
Token *tok2, Token *tok2,
std::list<std::string> &typeStringsUsedInTemplateInstantiation); std::list<std::string> &typeStringsUsedInTemplateInstantiation);
void printOut(
const TokenAndName &tokenAndName,
const std::string &indent = " ") const;
void printOut(const std::string &text = "") const;
TokenList &mTokenList; TokenList &mTokenList;
const Settings *mSettings; const Settings *mSettings;
ErrorLogger *mErrorLogger; ErrorLogger *mErrorLogger;