Added --remove-unused-included-templates option

This commit is contained in:
Daniel Marjamäki 2019-03-04 19:10:16 +01:00
parent 973ccda733
commit 3675318208
4 changed files with 63 additions and 39 deletions

View File

@ -158,6 +158,8 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
// --check-headers=no
else if (std::strcmp(argv[i], "--check-headers=no") == 0)
mSettings->checkHeaders = false;
else if (std::strcmp(argv[i], "--remove-unused-included-templates") == 0)
mSettings->removeUnusedIncludedTemplates = true;
// max ctu depth
else if (std::strncmp(argv[i], "--max-ctu-depth=", 16) == 0)
@ -1075,6 +1077,9 @@ void CmdLineParser::printHelp()
" using e.g. ~ for home folder does not work. It is\n"
" currently only possible to apply the base paths to\n"
" files that are on a lower level in the directory tree.\n"
" --remove-unused-included-templates\n"
" Remove unused templates in included files. This option\n"
" can be used to speed up the analysis.\n"
" --report-progress Report progress messages while checking a file.\n"
#ifdef HAVE_RULES
" --rule=<rule> Match regular expression.\n"

View File

@ -50,6 +50,7 @@ Settings::Settings()
enforcedLang(None),
reportProgress(false),
checkHeaders(true),
removeUnusedIncludedTemplates(false),
checkConfiguration(false),
checkLibrary(false)
{

View File

@ -285,10 +285,14 @@ public:
/** Check for incomplete info in library files? */
bool checkLibrary;
/** Check code in the headers, this is on by default but can
/**
* Check code in the headers, this is on by default but can
* be turned off to save CPU */
bool checkHeaders;
/** Remove unused included templates */
bool removeUnusedIncludedTemplates;
/** Struct contains standards settings */
Standards standards;

View File

@ -4256,10 +4256,15 @@ void Tokenizer::simplifyHeaders()
// TODO : can we remove anything in headers here? Like unused declarations.
// Maybe if --dump is used we want to have _everything_.
if (mSettings->checkHeaders)
if (mSettings->checkHeaders && !mSettings->removeUnusedIncludedTemplates)
// Default=full analysis. All information in the headers are kept.
return;
const bool checkHeaders = mSettings->checkHeaders;
const bool removeUnusedIncludedFunctions = mSettings->checkHeaders;
const bool removeUnusedIncludedClasses = mSettings->checkHeaders;
const bool removeUnusedIncludedTemplates = mSettings->checkHeaders || mSettings->removeUnusedIncludedTemplates;
// We want to remove selected stuff from the headers but not *everything*.
// The intention here is to not damage the analysis of the source file.
// You should get all warnings in the source file.
@ -4269,7 +4274,10 @@ void Tokenizer::simplifyHeaders()
// functions and types to keep
std::set<std::string> keep;
for (const Token *tok = list.front(); tok; tok = tok->next()) {
if (tok->fileIndex() != 0 || !tok->isName())
if (!tok->isName())
continue;
if (checkHeaders && tok->fileIndex() != 0)
continue;
if (Token::Match(tok, "%name% (") && !Token::simpleMatch(tok->linkAt(1), ") {")) {
@ -4290,7 +4298,7 @@ void Tokenizer::simplifyHeaders()
continue;
// Remove executable code
if (tok->str() == "{") {
if (mSettings->checkHeaders && tok->str() == "{") {
// TODO: We probably need to keep the executable code if this function is called from the source file.
const Token *prev = tok->previous();
while (prev && prev->isName())
@ -4305,6 +4313,7 @@ void Tokenizer::simplifyHeaders()
if (Token::Match(tok, "[;{}]")) {
// Remove unused function declarations
if (removeUnusedIncludedFunctions) {
while (1) {
Token *start = tok->next();
while (start && functionStart.find(start->str()) != functionStart.end())
@ -4314,7 +4323,9 @@ void Tokenizer::simplifyHeaders()
else
break;
}
}
if (removeUnusedIncludedClasses) {
if (Token::Match(tok, "[;{}] class|struct %name% [:{]") && keep.find(tok->strAt(2)) == keep.end()) {
// Remove this class/struct
const Token *endToken = tok->tokAt(3);
@ -4326,7 +4337,9 @@ void Tokenizer::simplifyHeaders()
if (endToken && endToken->str() == "{" && Token::simpleMatch(endToken->link(), "} ;"))
Token::eraseTokens(tok, endToken->link()->next());
}
}
if (removeUnusedIncludedTemplates) {
if (Token::Match(tok->next(), "template < %name%")) {
const Token *tok2 = tok->tokAt(3);
while (Token::Match(tok2, "%name% %name% [,=>]")) {
@ -4352,6 +4365,7 @@ void Tokenizer::simplifyHeaders()
}
}
}
}
void Tokenizer::removeMacrosInGlobalScope()
{