Tokenizer: Remove extra 'template' keywords

This commit is contained in:
Daniel Marjamäki 2020-11-28 21:55:28 +01:00
parent f42c104b0d
commit fd75837494
3 changed files with 31 additions and 1 deletions

View File

@ -4374,6 +4374,9 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
if (isCPP())
mTemplateSimplifier->fixAngleBrackets();
// Remove extra "template" tokens that are not used by cppcheck
removeExtraTemplateKeywords();
// Bail out if code is garbage
if (mTimerResults) {
Timer t("Tokenizer::tokenize::findGarbageCode", mSettings->showtime, mTimerResults);
@ -5152,6 +5155,16 @@ void Tokenizer::simplifyHeadersAndUnusedTemplates()
}
}
void Tokenizer::removeExtraTemplateKeywords()
{
if (isCPP()) {
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "%name% .|:: template %name%"))
tok->next()->deleteNext();
}
}
}
void Tokenizer::removeMacrosInGlobalScope()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {

View File

@ -171,6 +171,11 @@ public:
*/
void simplifyHeadersAndUnusedTemplates();
/**
* Remove extra "template" keywords that are not used by Cppcheck
*/
void removeExtraTemplateKeywords();
/**
* Deletes dead code between 'begin' and 'end'.
* In general not everything can be erased, such as:

View File

@ -516,6 +516,8 @@ private:
TEST_CASE(cppcast);
TEST_CASE(checkHeader1);
TEST_CASE(removeExtraTemplateKeywords);
}
std::string tokenizeAndStringify(const char code[], bool simplify = false, bool expand = true, Settings::PlatformType platform = Settings::Native, const char* filename = "test.cpp", bool cpp11 = true) {
@ -6469,7 +6471,7 @@ private:
ASSERT_EQUALS("template < typename T > void g ( S < & T :: operatorint > ) { }", tokenizeAndStringify(code3));
const char code4[] = "template <typename T> void g(S<&T::template operator- <double> >) {}";
ASSERT_EQUALS("template < typename T > void g ( S < & T :: template operator- < double > > ) { }", tokenizeAndStringify(code4));
ASSERT_EQUALS("template < typename T > void g ( S < & T :: operator- < double > > ) { }", tokenizeAndStringify(code4));
}
void simplifyOperatorName12() { // #9110
@ -8709,6 +8711,16 @@ private:
"5: ;\n",
checkHeaders(code, false));
}
void removeExtraTemplateKeywords() {
const char code1[] = "typename GridView::template Codim<0>::Iterator iterator;";
const char expected1[] = "GridView :: Codim < 0 > :: Iterator iterator ;";
ASSERT_EQUALS(expected1, tokenizeAndStringify(code1, false));
const char code2[] = "typename GridView::template Codim<0>::Iterator it = gv.template begin<0>();";
const char expected2[] = "GridView :: Codim < 0 > :: Iterator it ; it = gv . begin < 0 > ( ) ;";
ASSERT_EQUALS(expected2, tokenizeAndStringify(code2, false));
}
};
REGISTER_TEST(TestTokenizer)