Skip to content

Commit 07da4b4

Browse files
IOBYTEdanmar
authored andcommitted
template simplifier: Add flag variable to cache information about dec… (#1569)
* template simplifier: Add flag variable to cache information about declarations. Also fix some cppcheck warnings. * Make variable const.
1 parent 286cb2f commit 07da4b4

2 files changed

Lines changed: 97 additions & 30 deletions

File tree

‎lib/templatesimplifier.cpp‎

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,26 @@ namespace {
5454
}
5555

5656
TemplateSimplifier::TokenAndName::TokenAndName(Token *tok, const std::string &s, const std::string &n, const Token *nt, const Token *pe) :
57-
token(tok), scope(s), name(n), nameToken(nt), paramEnd(pe)
57+
token(tok), scope(s), name(n), nameToken(nt), paramEnd(pe), flags(0)
5858
{
59+
// only set flags for declaration
60+
if (token && nameToken && paramEnd) {
61+
isClass(Token::Match(paramEnd->next(), "class|struct|union %name% <|{|:"));
62+
isFunction(nameToken->strAt(1) == "(" ||
63+
(nameToken->strAt(1) == "<" && nameToken->next()->findClosingBracket()->strAt(1) == "("));
64+
isVariable(Token::Match(nameToken->next(), "=|;") ||
65+
(nameToken->strAt(1) == "<" && Token::Match(nameToken->next()->findClosingBracket()->next(), "=|;")));
66+
isAlias(paramEnd->strAt(1) == "using");
67+
isSpecialized(Token::Match(token, "template < >"));
68+
}
69+
5970
if (token)
6071
token->templateSimplifierPointer(this);
6172
}
6273

6374
TemplateSimplifier::TokenAndName::TokenAndName(const TokenAndName& otherTok) :
64-
token(otherTok.token), scope(otherTok.scope), name(otherTok.name), nameToken(otherTok.nameToken), paramEnd(otherTok.paramEnd)
75+
token(otherTok.token), scope(otherTok.scope), name(otherTok.name),
76+
nameToken(otherTok.nameToken), paramEnd(otherTok.paramEnd), flags(otherTok.flags)
6577
{
6678
if (token)
6779
token->templateSimplifierPointer(this);
@@ -983,7 +995,7 @@ void TemplateSimplifier::addNamespace(const TokenAndName &templateDeclaration, c
983995
}
984996
}
985997

986-
bool TemplateSimplifier::alreadyHasNamespace(const TokenAndName &templateDeclaration, const Token *tok) const
998+
bool TemplateSimplifier::alreadyHasNamespace(const TokenAndName &templateDeclaration, const Token *tok)
987999
{
9881000
std::string scope = templateDeclaration.scope;
9891001

@@ -1012,17 +1024,16 @@ void TemplateSimplifier::expandTemplate(
10121024
const Token *endOfTemplateDefinition = nullptr;
10131025
const Token * const templateDeclarationNameToken = templateDeclaration.nameToken;
10141026
const Token * const templateDeclarationToken = templateDeclaration.paramEnd;
1015-
const bool isClass = Token::Match(templateDeclaration.paramEnd->next(), "class|struct|union %name% <|{|:");
1016-
const bool isFunction = templateDeclarationNameToken->strAt(1) == "(" ||
1017-
(templateDeclarationNameToken->strAt(1) == "<" && templateDeclarationNameToken->next()->findClosingBracket()->strAt(1) == "(");
1018-
const bool isSpecialization = Token::Match(templateDeclaration.token, "template < >");
1027+
const bool isClass = templateDeclaration.isClass();
1028+
const bool isFunction = templateDeclaration.isFunction();
1029+
const bool isSpecialization = templateDeclaration.isSpecialized();
10191030

10201031
// add forward declarations
10211032
if (copy && isClass) {
10221033
templateDeclaration.token->insertToken(templateDeclarationToken->strAt(1), "", true);
10231034
templateDeclaration.token->insertToken(newName, "", true);
10241035
templateDeclaration.token->insertToken(";", "", true);
1025-
} else if (isFunction && (copy || (!copy && isSpecialization))) {
1036+
} else if (isFunction && (copy || isSpecialization)) {
10261037
Token * dst = templateDeclaration.token;
10271038
bool isStatic = false;
10281039
std::string scope;
@@ -1812,16 +1823,8 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
18121823
std::vector<const Token *> typeParametersInDeclaration;
18131824
getTemplateParametersInDeclaration(templateDeclaration.token->tokAt(2), typeParametersInDeclaration);
18141825
const bool printDebug = mSettings->debugwarnings;
1815-
const bool specialized = Token::simpleMatch(templateDeclaration.token, "template < >");
1816-
bool isfunc = false;
1817-
1818-
if (templateDeclaration.nameToken->strAt(1) == "(")
1819-
isfunc = true;
1820-
else if (templateDeclaration.nameToken->strAt(1) == "<") {
1821-
const Token *tok1 = templateDeclaration.nameToken->tokAt(1)->findClosingBracket();
1822-
if (tok1 && tok1->strAt(1) == "(")
1823-
isfunc = true;
1824-
}
1826+
const bool specialized = templateDeclaration.isSpecialized();
1827+
const bool isfunc = templateDeclaration.isFunction();
18251828

18261829
// locate template usage..
18271830
std::string::size_type numberOfTemplateInstantiations = mTemplateInstantiations.size();
@@ -2098,19 +2101,20 @@ void TemplateSimplifier::getUserDefinedSpecializations()
20982101
{
20992102
// try to locate a matching declaration for each user defined specialization
21002103
for (auto & spec : mTemplateDeclarations) {
2101-
if (Token::Match(spec.token, "template < >")) {
2104+
if (spec.isSpecialized()) {
21022105
std::string specName = getPathName(spec);
21032106

21042107
bool found = false;
21052108
for (auto & decl : mTemplateDeclarations) {
2106-
if (Token::Match(decl.token, "template < >"))
2109+
if (decl.isSpecialized())
21072110
continue;
21082111
std::string declName = getPathName(decl);
21092112

21102113
// make sure the scopes and names match
21112114
if (specName == declName) {
21122115
// @todo make sure function parameters also match
21132116
mTemplateUserSpecializationMap[spec.token] = decl.token;
2117+
found = true;
21142118
}
21152119
}
21162120

@@ -2258,7 +2262,7 @@ void TemplateSimplifier::simplifyTemplates(
22582262
break;
22592263
}
22602264
if (decl != mTemplateDeclarations.end()) {
2261-
if (Token::simpleMatch(it->token, "template < >")) {
2265+
if (it->isSpecialized()) {
22622266
// delete the "template < >"
22632267
Token * tok = it->token;
22642268
tok->deleteNext(2);
@@ -2296,7 +2300,7 @@ void TemplateSimplifier::simplifyTemplates(
22962300
end = end->next();
22972301
if (start->previous())
22982302
start = start->previous();
2299-
if (end->next())
2303+
if (end && end->next())
23002304
end = end->next();
23012305
eraseTokens(start, end);
23022306
}

‎lib/templatesimplifier.h‎

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class TokenList;
4343
/** @brief Simplify templates from the preprocessed and partially simplified code. */
4444
class CPPCHECKLIB TemplateSimplifier {
4545
public:
46-
TemplateSimplifier(Tokenizer *tokenizer);
46+
explicit TemplateSimplifier(Tokenizer *tokenizer);
4747
~TemplateSimplifier();
4848

4949
/**
@@ -74,13 +74,76 @@ class CPPCHECKLIB TemplateSimplifier {
7474
~TokenAndName();
7575

7676
bool operator == (const TokenAndName & rhs) const {
77-
return token == rhs.token && scope == rhs.scope && name == rhs.name && nameToken == rhs.nameToken && paramEnd == rhs.paramEnd;;
77+
return token == rhs.token && scope == rhs.scope && name == rhs.name &&
78+
nameToken == rhs.nameToken && paramEnd == rhs.paramEnd && flags == rhs.flags;
7879
}
7980
Token *token;
8081
std::string scope;
8182
std::string name;
8283
const Token *nameToken;
8384
const Token *paramEnd;
85+
unsigned int flags;
86+
87+
enum {
88+
fIsClass = (1 << 0), // class template
89+
fIsFunction = (1 << 1), // function template
90+
fIsVariable = (1 << 2), // variable template
91+
fIsAlias = (1 << 3), // alias template
92+
fIsSpecialized = (1 << 4), // user specialized template
93+
};
94+
95+
bool isClass() const {
96+
return getFlag(fIsClass);
97+
}
98+
void isClass(bool state) {
99+
setFlag(fIsClass, state);
100+
}
101+
102+
bool isFunction() const {
103+
return getFlag(fIsFunction);
104+
}
105+
void isFunction(bool state) {
106+
setFlag(fIsFunction, state);
107+
}
108+
109+
bool isVariable() const {
110+
return getFlag(fIsVariable);
111+
}
112+
void isVariable(bool state) {
113+
setFlag(fIsVariable, state);
114+
}
115+
116+
bool isAlias() const {
117+
return getFlag(fIsAlias);
118+
}
119+
void isAlias(bool state) {
120+
setFlag(fIsAlias, state);
121+
}
122+
123+
bool isSpecialized() const {
124+
return getFlag(fIsSpecialized);
125+
}
126+
void isSpecialized(bool state) {
127+
setFlag(fIsSpecialized, state);
128+
}
129+
130+
/**
131+
* Get specified flag state.
132+
* @param flag flag to get state of
133+
* @return true if flag set or false in flag not set
134+
*/
135+
bool getFlag(unsigned int flag) const {
136+
return ((flags & flag) != 0);
137+
}
138+
139+
/**
140+
* Set specified flag state.
141+
* @param flag flag to set state
142+
* @param state new state of flag
143+
*/
144+
void setFlag(unsigned int flag, bool state) {
145+
flags = state ? flags | flag : flags & ~flag;
146+
}
84147
};
85148

86149
/**
@@ -99,15 +162,15 @@ class CPPCHECKLIB TemplateSimplifier {
99162
* @return -1 to bail out or positive integer to identity the position
100163
* of the template name.
101164
*/
102-
int getTemplateNamePosition(const Token *tok, bool forward = false);
165+
static int getTemplateNamePosition(const Token *tok, bool forward = false);
103166

104167
/**
105168
* Get template name position
106169
* @param tok The ">" token e.g. before "class"
107170
* @param namepos return offset to name
108171
* @return true if name found, false if not
109172
* */
110-
bool getTemplateNamePositionTemplateFunction(const Token *tok, int &namepos);
173+
static bool getTemplateNamePositionTemplateFunction(const Token *tok, int &namepos);
111174

112175
/**
113176
* Simplify templates
@@ -132,7 +195,7 @@ class CPPCHECKLIB TemplateSimplifier {
132195
* @return true if modifications to token-list are done.
133196
* false if no modifications are done.
134197
*/
135-
bool simplifyCalculations(Token* tok = nullptr);
198+
bool simplifyCalculations(Token* frontToken = nullptr);
136199

137200
private:
138201
/**
@@ -196,7 +259,7 @@ class CPPCHECKLIB TemplateSimplifier {
196259
* @param tok place to start looking for namespace
197260
* @return true if namespace already present
198261
*/
199-
bool alreadyHasNamespace(const TokenAndName &templateDeclaration, const Token *tok) const;
262+
static bool alreadyHasNamespace(const TokenAndName &templateDeclaration, const Token *tok);
200263

201264
/**
202265
* Expand a template. Create "expanded" class/function at end of tokenlist.
@@ -241,7 +304,7 @@ class CPPCHECKLIB TemplateSimplifier {
241304
/**
242305
* Remove a specific "template < ..." template class/function
243306
*/
244-
bool removeTemplate(Token *tok);
307+
static bool removeTemplate(Token *tok);
245308

246309
/** Syntax error */
247310
static void syntaxError(const Token *tok);
@@ -256,7 +319,7 @@ class CPPCHECKLIB TemplateSimplifier {
256319
* @param begin Tokens after this will be erased.
257320
* @param end Tokens before this will be erased.
258321
*/
259-
void eraseTokens(Token *begin, const Token *end);
322+
static void eraseTokens(Token *begin, const Token *end);
260323

261324
/**
262325
* Delete specified token without invalidating pointer to following token.

0 commit comments

Comments
 (0)