Tokenizer: moved `VariableMap` into anonymous namespace (#5686)

This commit is contained in:
Oliver Stöneberg 2023-11-21 11:35:17 +01:00 committed by GitHub
parent f5630e7049
commit 2b61c9ef2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 92 deletions

View File

@ -206,7 +206,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
// skip variable declaration
else if (Token::Match(tok2, "*|&|>"))
continue;
else if (Token::Match(tok2, "%name% (") && mTokenizer.isFunctionHead(tok2->next(), "{;"))
else if (Token::Match(tok2, "%name% (") && Tokenizer::isFunctionHead(tok2->next(), "{;"))
continue;
else if (Token::Match(tok2, "%name% [|="))
continue;
@ -531,7 +531,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
// class function?
else if (isFunction(tok, scope, &funcStart, &argStart, &declEnd)) {
if (tok->previous()->str() != "::" || tok->strAt(-2) == scope->className) {
Function function(&mTokenizer, tok, scope, funcStart, argStart);
Function function(tok, scope, funcStart, argStart);
// save the access type
function.access = access[scope];
@ -547,7 +547,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
function.arg = function.argDef;
// out of line function
if (const Token *endTok = mTokenizer.isFunctionHead(end, ";")) {
if (const Token *endTok = Tokenizer::isFunctionHead(end, ";")) {
tok = endTok;
scope->addFunction(function);
}
@ -1864,7 +1864,7 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const
const Token* tok1 = tok->previous();
const Token* tok2 = tok->next()->link()->next();
if (!mTokenizer.isFunctionHead(tok->next(), ";:{"))
if (!Tokenizer::isFunctionHead(tok->next(), ";:{"))
return false;
// skip over destructor "~"
@ -2418,8 +2418,7 @@ static bool isOperator(const Token *tokenDef)
return name.size() > 8 && startsWith(name,"operator") && std::strchr("+-*/%&|~^<>!=[(", name[8]);
}
Function::Function(const Tokenizer *mTokenizer,
const Token *tok,
Function::Function(const Token *tok,
const Scope *scope,
const Token *tokDef,
const Token *tokArgDef)
@ -2520,7 +2519,7 @@ Function::Function(const Tokenizer *mTokenizer,
tok = tok->next();
}
if (mTokenizer->isFunctionHead(end, ":{")) {
if (Tokenizer::isFunctionHead(end, ":{")) {
// assume implementation is inline (definition and implementation same)
token = tokenDef;
arg = argDef;
@ -3168,7 +3167,7 @@ Function* SymbolDatabase::addGlobalFunction(Scope*& scope, const Token*& tok, co
Function* SymbolDatabase::addGlobalFunctionDecl(Scope*& scope, const Token *tok, const Token *argStart, const Token* funcStart)
{
Function function(&mTokenizer, tok, scope, funcStart, argStart);
Function function(tok, scope, funcStart, argStart);
scope->addFunction(std::move(function));
return &scope->functionList.back();
}
@ -3232,7 +3231,7 @@ void SymbolDatabase::addClassFunction(Scope **scope, const Token **tok, const To
if (!func->hasBody()) {
const Token *closeParen = (*tok)->next()->link();
if (closeParen) {
const Token *eq = mTokenizer.isFunctionHead(closeParen, ";");
const Token *eq = Tokenizer::isFunctionHead(closeParen, ";");
if (eq && Token::simpleMatch(eq->tokAt(-2), "= default ;")) {
func->isDefault(true);
return;
@ -3305,7 +3304,7 @@ void SymbolDatabase::addClassFunction(Scope **scope, const Token **tok, const To
if (func->argsMatch(scope1, func->argDef, (*tok)->next(), path, path_length)) {
const Token *closeParen = (*tok)->next()->link();
if (closeParen) {
const Token *eq = mTokenizer.isFunctionHead(closeParen, ";");
const Token *eq = Tokenizer::isFunctionHead(closeParen, ";");
if (eq && Token::simpleMatch(eq->tokAt(-2), "= default ;")) {
func->isDefault(true);
return;

View File

@ -749,7 +749,7 @@ class CPPCHECKLIB Function {
public:
enum Type { eConstructor, eCopyConstructor, eMoveConstructor, eOperatorEqual, eDestructor, eFunction, eLambda };
Function(const Tokenizer *mTokenizer, const Token *tok, const Scope *scope, const Token *tokDef, const Token *tokArgDef);
Function(const Token *tok, const Scope *scope, const Token *tokDef, const Token *tokArgDef);
Function(const Token *tokenDef, const std::string &clangType);
const std::string &name() const {
@ -1438,7 +1438,7 @@ private:
void debugSymbolDatabase() const;
void addClassFunction(Scope **scope, const Token **tok, const Token *argStart);
Function *addGlobalFunctionDecl(Scope*& scope, const Token* tok, const Token *argStart, const Token* funcStart);
static Function *addGlobalFunctionDecl(Scope*& scope, const Token* tok, const Token *argStart, const Token* funcStart);
Function *addGlobalFunction(Scope*& scope, const Token*& tok, const Token *argStart, const Token* funcStart);
void addNewFunction(Scope **scope, const Token **tok);
bool isFunction(const Token *tok, const Scope* outerScope, const Token **funcStart, const Token **argStart, const Token** declEnd) const;

View File

@ -1422,14 +1422,14 @@ bool TemplateSimplifier::getTemplateNamePositionTemplateFunction(const Token *to
} else if (Token::Match(tok->next(), "%type% <")) {
const Token *closing = tok->tokAt(2)->findClosingBracket();
if (closing) {
if (closing->strAt(1) == "(" && Tokenizer::isFunctionHead(closing->next(), ";|{|:", true))
if (closing->strAt(1) == "(" && Tokenizer::isFunctionHead(closing->next(), ";|{|:"))
return true;
while (tok->next() && tok->next() != closing) {
tok = tok->next();
namepos++;
}
}
} else if (Token::Match(tok->next(), "%type% (") && Tokenizer::isFunctionHead(tok->tokAt(2), ";|{|:", true)) {
} else if (Token::Match(tok->next(), "%type% (") && Tokenizer::isFunctionHead(tok->tokAt(2), ";|{|:")) {
return true;
}
tok = tok->next();
@ -1934,7 +1934,7 @@ void TemplateSimplifier::expandTemplate(
const Token *tok4 = tok3->next()->findClosingBracket();
while (tok4 && tok4->str() != "(")
tok4 = tok4->next();
if (!Tokenizer::isFunctionHead(tok4, ":{", true))
if (!Tokenizer::isFunctionHead(tok4, ":{"))
continue;
// find function return type start
tok5 = tok5->next()->findClosingBracket();

View File

@ -93,12 +93,7 @@ static void skipEnumBody(T **tok)
*tok = defStart->link()->next();
}
const Token * Tokenizer::isFunctionHead(const Token *tok, const std::string &endsWith) const
{
return Tokenizer::isFunctionHead(tok, endsWith, isCPP());
}
const Token * Tokenizer::isFunctionHead(const Token *tok, const std::string &endsWith, bool cpp)
const Token * Tokenizer::isFunctionHead(const Token *tok, const std::string &endsWith)
{
if (!tok)
return nullptr;
@ -113,7 +108,7 @@ const Token * Tokenizer::isFunctionHead(const Token *tok, const std::string &end
}
return (tok && endsWith.find(tok->str()) != std::string::npos) ? tok : nullptr;
}
if (cpp && tok->str() == ")") {
if (tok->isCpp() && tok->str() == ")") {
tok = tok->next();
while (Token::Match(tok, "const|noexcept|override|final|volatile|mutable|&|&& !!(") ||
(Token::Match(tok, "%name% !!(") && tok->isUpperCaseName()))
@ -2791,7 +2786,7 @@ namespace {
}
} // namespace
bool Tokenizer::isMemberFunction(const Token *openParen) const
bool Tokenizer::isMemberFunction(const Token *openParen)
{
return (Token::Match(openParen->tokAt(-2), ":: %name% (") ||
Token::Match(openParen->tokAt(-3), ":: ~ %name% (")) &&
@ -3901,7 +3896,7 @@ const Token * Tokenizer::startOfExecutableScope(const Token * tok)
if (tok->str() != ")")
return nullptr;
tok = isFunctionHead(tok, ":{", true);
tok = Tokenizer::isFunctionHead(tok, ":{");
if (Token::Match(tok, ": %name% [({]")) {
while (Token::Match(tok, "[:,] %name% [({]"))
@ -4088,32 +4083,31 @@ void Tokenizer::simplifyTemplates()
//---------------------------------------------------------------------------
/** Class used in Tokenizer::setVarIdPass1 */
class VariableMap {
private:
std::unordered_map<std::string, nonneg int> mVariableId;
std::unordered_map<std::string, nonneg int> mVariableId_global;
std::stack<std::vector<std::pair<std::string, nonneg int>>> mScopeInfo;
mutable nonneg int mVarId{};
public:
VariableMap() = default;
void enterScope();
bool leaveScope();
void addVariable(const std::string& varname, bool globalNamespace);
bool hasVariable(const std::string& varname) const {
return mVariableId.find(varname) != mVariableId.end();
}
namespace {
/** Class used in Tokenizer::setVarIdPass1 */
class VariableMap {
private:
std::unordered_map<std::string, nonneg int> mVariableId;
std::unordered_map<std::string, nonneg int> mVariableId_global;
std::stack<std::vector<std::pair<std::string, nonneg int>>> mScopeInfo;
mutable nonneg int mVarId{};
public:
VariableMap() = default;
void enterScope();
bool leaveScope();
void addVariable(const std::string& varname, bool globalNamespace);
bool hasVariable(const std::string& varname) const {
return mVariableId.find(varname) != mVariableId.end();
}
const std::unordered_map<std::string, nonneg int>& map(bool global) const {
return global ? mVariableId_global : mVariableId;
}
nonneg int getVarId() const {
return mVarId;
}
nonneg int& getVarId() {
return mVarId;
}
};
const std::unordered_map<std::string, nonneg int>& map(bool global) const {
return global ? mVariableId_global : mVariableId;
}
nonneg int& getVarId() {
return mVarId;
}
};
}
void VariableMap::enterScope()
@ -4298,9 +4292,9 @@ static bool setVarIdParseDeclaration(Token** tok, const VariableMap& variableMap
}
void Tokenizer::setVarIdStructMembers(Token **tok1,
std::map<nonneg int, std::map<std::string, nonneg int>>& structMembers,
nonneg int &varId) const
static void setVarIdStructMembers(Token **tok1,
std::map<nonneg int, std::map<std::string, nonneg int>>& structMembers,
nonneg int &varId)
{
Token *tok = *tok1;
@ -4334,7 +4328,7 @@ void Tokenizer::setVarIdStructMembers(Token **tok1,
while (Token::Match(tok->next(), ")| . %name% !!(")) {
// Don't set varid for trailing return type
if (tok->strAt(1) == ")" && (tok->linkAt(1)->previous()->isName() || tok->linkAt(1)->strAt(-1) == "]") &&
isFunctionHead(tok->linkAt(1), "{|;")) {
Tokenizer::isFunctionHead(tok->linkAt(1), "{|;")) {
tok = tok->tokAt(3);
continue;
}
@ -4363,10 +4357,10 @@ void Tokenizer::setVarIdStructMembers(Token **tok1,
*tok1 = tok;
}
void Tokenizer::setVarIdClassDeclaration(Token* const startToken,
VariableMap& variableMap,
const nonneg int scopeStartVarId,
std::map<nonneg int, std::map<std::string, nonneg int>>& structMembers)
static bool setVarIdClassDeclaration(Token* const startToken,
VariableMap& variableMap,
const nonneg int scopeStartVarId,
std::map<nonneg int, std::map<std::string, nonneg int>>& structMembers)
{
// end of scope
const Token* const endToken = startToken->link();
@ -4389,7 +4383,7 @@ void Tokenizer::setVarIdClassDeclaration(Token* const startToken,
const Token *initListArgLastToken = nullptr;
for (Token *tok = startToken->next(); tok != endToken; tok = tok->next()) {
if (!tok)
syntaxError(nullptr);
return false;
if (initList) {
if (tok == initListArgLastToken)
initListArgLastToken = nullptr;
@ -4416,7 +4410,7 @@ void Tokenizer::setVarIdClassDeclaration(Token* const startToken,
if (Token::Match(tok->previous(), "::|.") && tok->strAt(-2) != "this" && !Token::simpleMatch(tok->tokAt(-5), "( * this ) ."))
continue;
if (!tok->next())
syntaxError(nullptr);
return false;
if (tok->next()->str() == "::") {
if (tok->str() == className)
tok = tok->tokAt(2);
@ -4435,6 +4429,7 @@ void Tokenizer::setVarIdClassDeclaration(Token* const startToken,
} else if (indentlevel == 0 && tok->str() == ":" && !initListArgLastToken)
initList = true;
}
return true;
}
@ -4602,10 +4597,12 @@ void Tokenizer::setVarIdPass1()
}
// Set variable ids in class declaration..
if (!initlist && !isC() && !scopeStack.top().isExecutable && tok->link() && !isNamespace) {
setVarIdClassDeclaration(tok->link(),
variableMap,
scopeStack.top().startVarid,
structMembers);
if (!setVarIdClassDeclaration(tok->link(),
variableMap,
scopeStack.top().startVarid,
structMembers)) {
syntaxError(nullptr);
}
}
if (!scopeStack.top().isStructInit) {
@ -9883,7 +9880,7 @@ void Tokenizer::createSymbolDatabase()
mSymbolDatabase->validate();
}
bool Tokenizer::operatorEnd(const Token * tok) const
bool Tokenizer::operatorEnd(const Token * tok)
{
if (tok && tok->str() == ")") {
if (isFunctionHead(tok, "{|;|?|:|["))

View File

@ -38,7 +38,6 @@ class Token;
class TemplateSimplifier;
class ErrorLogger;
class Preprocessor;
class VariableMap;
enum class Severity;
namespace simplecpp {
@ -269,7 +268,7 @@ public:
/**
*/
bool isMemberFunction(const Token *openParen) const;
static bool isMemberFunction(const Token *openParen);
/**
*/
@ -366,16 +365,7 @@ public:
* @param endsWith string after function head
* @return token matching with endsWith if syntax seems to be a function head else nullptr
*/
const Token * isFunctionHead(const Token *tok, const std::string &endsWith) const;
/**
* is token pointing at function head?
* @param tok A '(' or ')' token in a possible function head
* @param endsWith string after function head
* @param cpp c++ code
* @return token matching with endsWith if syntax seems to be a function head else nullptr
*/
static const Token * isFunctionHead(const Token *tok, const std::string &endsWith, bool cpp);
static const Token * isFunctionHead(const Token *tok, const std::string &endsWith);
const Preprocessor *getPreprocessor() const {
assert(mPreprocessor);
@ -580,21 +570,12 @@ private:
void unsupportedTypedef(const Token *tok) const;
void setVarIdClassDeclaration(Token* const startToken, // cppcheck-suppress functionConst // has side effects
VariableMap& variableMap,
const nonneg int scopeStartVarId,
std::map<nonneg int, std::map<std::string, nonneg int>>& structMembers);
void setVarIdStructMembers(Token **tok1,
std::map<nonneg int, std::map<std::string, nonneg int>>& structMembers,
nonneg int &varId) const;
void setVarIdClassFunction(const std::string &classname, // cppcheck-suppress functionConst // has side effects
Token * const startToken,
const Token * const endToken,
const std::map<std::string, nonneg int> &varlist,
std::map<nonneg int, std::map<std::string, nonneg int>>& structMembers,
nonneg int &varId_);
static void setVarIdClassFunction(const std::string &classname,
Token * const startToken,
const Token * const endToken,
const std::map<std::string, nonneg int> &varlist,
std::map<nonneg int, std::map<std::string, nonneg int>>& structMembers,
nonneg int &varId_);
/**
* Output list of unknown types.
@ -604,7 +585,7 @@ private:
/** Find end of SQL (or PL/SQL) block */
static const Token *findSQLBlockEnd(const Token *tokSQLStart);
bool operatorEnd(const Token * tok) const;
static bool operatorEnd(const Token * tok);
public:
const SymbolDatabase *getSymbolDatabase() const {