Skip to content

Commit

Permalink
Scope: made symdb a reference
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Nov 29, 2024
1 parent e799f3c commit 06e3c9b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 40 deletions.
6 changes: 3 additions & 3 deletions lib/clangimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ const ::Type * clangimport::AstNode::addTypeTokens(TokenList &tokenList, const s
for (const Token *typeToken = tokenList.back(); Token::Match(typeToken, "&|*|%name%"); typeToken = typeToken->previous()) {
if (!typeToken->isName())
continue;
const ::Type *recordType = scope->symdb->findVariableType(scope, typeToken);
const ::Type *recordType = scope->symdb.findVariableType(scope, typeToken);
if (recordType) {
const_cast<Token*>(typeToken)->type(recordType);
return recordType;
Expand Down Expand Up @@ -1360,7 +1360,7 @@ void clangimport::AstNode::createTokensFunctionDecl(TokenList &tokenList)

Scope *scope = nullptr;
if (hasBody) {
symbolDatabase->scopeList.emplace_back(symbolDatabase, nullptr, nestedIn);
symbolDatabase->scopeList.emplace_back(*symbolDatabase, nullptr, nestedIn);
scope = &symbolDatabase->scopeList.back();
scope->function = function;
scope->classDef = nameToken; // TODO: pass into ctor
Expand Down Expand Up @@ -1575,7 +1575,7 @@ void clangimport::parseClangAstDump(Tokenizer &tokenizer, std::istream &f)

tokenizer.createSymbolDatabase();
auto *symbolDatabase = const_cast<SymbolDatabase *>(tokenizer.getSymbolDatabase());
symbolDatabase->scopeList.emplace_back(symbolDatabase, nullptr, nullptr);
symbolDatabase->scopeList.emplace_back(*symbolDatabase, nullptr, nullptr);
symbolDatabase->scopeList.back().type = Scope::ScopeType::eGlobal;

clangimport::Data data;
Expand Down
65 changes: 31 additions & 34 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const Token* SymbolDatabase::isEnumDefinition(const Token* tok)
void SymbolDatabase::createSymbolDatabaseFindAllScopes()
{
// create global scope
scopeList.emplace_back(this, nullptr, nullptr);
scopeList.emplace_back(*this, nullptr, nullptr);

// pointer to current scope
Scope *scope = &scopeList.back();
Expand Down Expand Up @@ -285,7 +285,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
scope = new_scope;
tok = tok2;
} else {
scopeList.emplace_back(this, tok, scope);
scopeList.emplace_back(*this, tok, scope);
new_scope = &scopeList.back();

if (tok->str() == "class")
Expand Down Expand Up @@ -350,7 +350,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
Token::Match(tok, "namespace %name% %type% (") &&
tok->tokAt(2)->isUpperCaseName() &&
Token::simpleMatch(tok->linkAt(3), ") {")) {
scopeList.emplace_back(this, tok, scope);
scopeList.emplace_back(*this, tok, scope);

Scope *new_scope = &scopeList.back();
access[new_scope] = AccessControl::Public;
Expand Down Expand Up @@ -426,7 +426,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
// unnamed struct and union
else if (tok->isKeyword() && Token::Match(tok, "struct|union {") &&
Token::Match(tok->linkAt(1), "} *|&| %name% ;|[|=")) {
scopeList.emplace_back(this, tok, scope);
scopeList.emplace_back(*this, tok, scope);

Scope *new_scope = &scopeList.back();
access[new_scope] = AccessControl::Public;
Expand Down Expand Up @@ -468,7 +468,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
else if (tok->isKeyword() && ((Token::Match(tok, "struct|union {") &&
Token::simpleMatch(tok->linkAt(1), "} ;")) ||
Token::simpleMatch(tok, "namespace {"))) {
scopeList.emplace_back(this, tok, scope);
scopeList.emplace_back(*this, tok, scope);

Scope *new_scope = &scopeList.back();
access[new_scope] = AccessControl::Public;
Expand Down Expand Up @@ -706,27 +706,27 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
if (tok->isKeyword() && Token::Match(tok, "else|try|do {")) {
const Token* tok1 = tok->next();
if (tok->str() == "else")
scopeList.emplace_back(this, tok, scope, Scope::eElse, tok1);
scopeList.emplace_back(*this, tok, scope, Scope::eElse, tok1);
else if (tok->str() == "do")
scopeList.emplace_back(this, tok, scope, Scope::eDo, tok1);
scopeList.emplace_back(*this, tok, scope, Scope::eDo, tok1);
else //if (tok->str() == "try")
scopeList.emplace_back(this, tok, scope, Scope::eTry, tok1);
scopeList.emplace_back(*this, tok, scope, Scope::eTry, tok1);

tok = tok1;
scope->nestedList.push_back(&scopeList.back());
scope = &scopeList.back();
} else if (tok->isKeyword() && Token::Match(tok, "if|for|while|catch|switch (") && Token::simpleMatch(tok->linkAt(1), ") {")) {
const Token *scopeStartTok = tok->linkAt(1)->next();
if (tok->str() == "if")
scopeList.emplace_back(this, tok, scope, Scope::eIf, scopeStartTok);
scopeList.emplace_back(*this, tok, scope, Scope::eIf, scopeStartTok);
else if (tok->str() == "for") {
scopeList.emplace_back(this, tok, scope, Scope::eFor, scopeStartTok);
scopeList.emplace_back(*this, tok, scope, Scope::eFor, scopeStartTok);
} else if (tok->str() == "while")
scopeList.emplace_back(this, tok, scope, Scope::eWhile, scopeStartTok);
scopeList.emplace_back(*this, tok, scope, Scope::eWhile, scopeStartTok);
else if (tok->str() == "catch") {
scopeList.emplace_back(this, tok, scope, Scope::eCatch, scopeStartTok);
scopeList.emplace_back(*this, tok, scope, Scope::eCatch, scopeStartTok);
} else // if (tok->str() == "switch")
scopeList.emplace_back(this, tok, scope, Scope::eSwitch, scopeStartTok);
scopeList.emplace_back(*this, tok, scope, Scope::eSwitch, scopeStartTok);

scope->nestedList.push_back(&scopeList.back());
scope = &scopeList.back();
Expand All @@ -747,7 +747,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
} else if (!inIfCondition.empty() && tok == inIfCondition.top()) {
inIfCondition.pop();
} else if (isExecutableScope(tok)) {
scopeList.emplace_back(this, tok, scope, Scope::eUnconditional, tok);
scopeList.emplace_back(*this, tok, scope, Scope::eUnconditional, tok);
scope->nestedList.push_back(&scopeList.back());
scope = &scopeList.back();
} else {
Expand Down Expand Up @@ -2789,10 +2789,10 @@ static bool typesMatch(
const Token *&new_second)
{
// get first type
const Type* first_type = first_scope->symdb->findType(first_token, first_scope, /*lookOutside*/ true);
const Type* first_type = first_scope->symdb.findType(first_token, first_scope, /*lookOutside*/ true);
if (first_type) {
// get second type
const Type* second_type = second_scope->symdb->findType(second_token, second_scope, /*lookOutside*/ true);
const Type* second_type = second_scope->symdb.findType(second_token, second_scope, /*lookOutside*/ true);
// check if types match
if (first_type == second_type) {
const Token* tok1 = first_token;
Expand Down Expand Up @@ -3438,7 +3438,7 @@ void SymbolDatabase::addClassFunction(Scope *&scope, const Token *&tok, const To
void SymbolDatabase::addNewFunction(Scope *&scope, const Token *&tok)
{
const Token *tok1 = tok;
scopeList.emplace_back(this, tok1, scope);
scopeList.emplace_back(*this, tok1, scope);
Scope *newScope = &scopeList.back();

// find start of function '{'
Expand Down Expand Up @@ -3566,7 +3566,7 @@ const Token *Type::initBaseInfo(const Token *tok, const Token *tok1)
}
}

const Type * baseType = classScope->symdb->findType(base.nameTok, enclosingScope);
const Type * baseType = classScope->symdb.findType(base.nameTok, enclosingScope);
if (baseType && !baseType->findDependency(this))
base.type = baseType;

Expand Down Expand Up @@ -4414,15 +4414,15 @@ void SymbolDatabase::printXml(std::ostream &out) const

static const Type* findVariableTypeIncludingUsedNamespaces(const Scope* scope, const Token* typeTok)
{
const Type* argType = scope->symdb->findVariableType(scope, typeTok);
const Type* argType = scope->symdb.findVariableType(scope, typeTok);
if (argType)
return argType;

// look for variable type in any using namespace in this scope or above
while (scope) {
for (const Scope::UsingInfo &ui : scope->usingList) {
if (ui.scope) {
argType = scope->symdb->findVariableType(ui.scope, typeTok);
argType = scope->symdb.findVariableType(ui.scope, typeTok);
if (argType)
return argType;
}
Expand Down Expand Up @@ -4495,7 +4495,7 @@ void Function::addArguments(const Scope *scope)
while (Token::Match(typeTok, "const|volatile|enum|struct|::"))
typeTok = typeTok->next();
if (Token::Match(typeTok, ",|)")) { // #8333
scope->symdb->mTokenizer.syntaxError(typeTok);
scope->symdb.mTokenizer.syntaxError(typeTok);
}
if (Token::Match(typeTok, "%type% <") && Token::Match(typeTok->linkAt(1), "> :: %type%"))
typeTok = typeTok->linkAt(1)->tokAt(2);
Expand All @@ -4514,7 +4514,7 @@ void Function::addArguments(const Scope *scope)
endTok = nameTok->previous();

if (hasBody())
scope->symdb->debugMessage(nameTok, "varid0", "Function::addArguments found argument \'" + nameTok->str() + "\' with varid 0.");
scope->symdb.debugMessage(nameTok, "varid0", "Function::addArguments found argument \'" + nameTok->str() + "\' with varid 0.");
} else
endTok = typeTok;
} else
Expand Down Expand Up @@ -4545,7 +4545,7 @@ void Function::addArguments(const Scope *scope)
if (startTok == nameTok)
break;

argumentList.emplace_back(nameTok, startTok, endTok, count++, AccessControl::Argument, argType, functionScope, scope->symdb->mTokenizer.getSettings());
argumentList.emplace_back(nameTok, startTok, endTok, count++, AccessControl::Argument, argType, functionScope, scope->symdb.mTokenizer.getSettings());

if (tok->str() == ")") {
// check for a variadic function or a variadic template function
Expand Down Expand Up @@ -4716,23 +4716,20 @@ const Variable* Function::getArgumentVar(nonneg int num) const

//---------------------------------------------------------------------------

Scope::Scope(const SymbolDatabase *symdb_, const Token *classDef_, const Scope *nestedIn_, ScopeType type_, const Token *start_) :
Scope::Scope(const SymbolDatabase &symdb_, const Token *classDef_, const Scope *nestedIn_, ScopeType type_, const Token *start_) :
symdb(symdb_),
classDef(classDef_),
nestedIn(nestedIn_),
type(type_)
{
assert(symdb_);
setBodyStartEnd(start_);
}

Scope::Scope(const SymbolDatabase *symdb_, const Token *classDef_, const Scope *nestedIn_) :
Scope::Scope(const SymbolDatabase &symdb_, const Token *classDef_, const Scope *nestedIn_) :
symdb(symdb_),
classDef(classDef_),
nestedIn(nestedIn_)
{
assert(symdb_);

const Token *nameTok = classDef;
if (!classDef) {
type = Scope::eGlobal;
Expand Down Expand Up @@ -4790,7 +4787,7 @@ void Scope::addVariable(const Token *token_, const Token *start_, const Token *e
// keep possible size_t -> int truncation outside emplace_back() to have a single line
// C4267 VC++ warning instead of several dozens lines
const int varIndex = varlist.size();
varlist.emplace_back(token_, start_, end_, varIndex, access_, type_, scope_, scope_->symdb->mTokenizer.getSettings());
varlist.emplace_back(token_, start_, end_, varIndex, access_, type_, scope_, scope_->symdb.mTokenizer.getSettings());
}

// Get variable list..
Expand All @@ -4803,7 +4800,7 @@ void Scope::getVariableList()

// global scope
else if (type == Scope::eGlobal)
getVariableList(symdb->mTokenizer.tokens(), nullptr);
getVariableList(symdb.mTokenizer.tokens(), nullptr);

// forward declaration
else
Expand Down Expand Up @@ -4948,7 +4945,7 @@ const Token *Scope::checkVariable(const Token *tok, AccessControl varaccess)
const Token *typestart = tok;

// C++17 structured bindings
if (tok && tok->isCpp() && (symdb->mTokenizer.getSettings().standards.cpp >= Standards::CPP17) && Token::Match(tok, "auto &|&&| [")) {
if (tok && tok->isCpp() && (symdb.mTokenizer.getSettings().standards.cpp >= Standards::CPP17) && Token::Match(tok, "auto &|&&| [")) {
const Token *typeend = Token::findsimplematch(typestart, "[")->previous();
for (tok = typeend->tokAt(2); Token::Match(tok, "%name%|,"); tok = tok->next()) {
if (tok->varId())
Expand All @@ -4973,7 +4970,7 @@ const Token *Scope::checkVariable(const Token *tok, AccessControl varaccess)

if (vartok->varId() == 0) {
if (!vartok->isBoolean())
symdb->debugMessage(vartok, "varid0", "Scope::checkVariable found variable \'" + vartok->str() + "\' with varid 0.");
symdb.debugMessage(vartok, "varid0", "Scope::checkVariable found variable \'" + vartok->str() + "\' with varid 0.");
return tok;
}

Expand Down Expand Up @@ -5053,7 +5050,7 @@ bool Scope::isVariableDeclaration(const Token* const tok, const Token*& vartok,
if (isCPP && Token::Match(tok, "throw|new"))
return false;

const bool isCPP11 = isCPP && symdb->mTokenizer.getSettings().standards.cpp >= Standards::CPP11;
const bool isCPP11 = isCPP && symdb.mTokenizer.getSettings().standards.cpp >= Standards::CPP11;

if (isCPP11 && tok->str() == "using")
return false;
Expand Down Expand Up @@ -7249,7 +7246,7 @@ static const Token* parsedecl(const Token* type,
valuetype->container = container;
} else {
const Scope *scope = type->scope();
valuetype->typeScope = scope->symdb->findScope(typeTokens.front(), scope);
valuetype->typeScope = scope->symdb.findScope(typeTokens.front(), scope);
if (valuetype->typeScope)
valuetype->type = (scope->type == Scope::ScopeType::eClass) ? ValueType::Type::RECORD : ValueType::Type::NONSTD;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/symboldatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1029,10 +1029,10 @@ class CPPCHECKLIB Scope {

enum ScopeType : std::uint8_t { eGlobal, eClass, eStruct, eUnion, eNamespace, eFunction, eIf, eElse, eFor, eWhile, eDo, eSwitch, eUnconditional, eTry, eCatch, eLambda, eEnum };

Scope(const SymbolDatabase *symdb_, const Token *classDef_, const Scope *nestedIn_);
Scope(const SymbolDatabase *symdb_, const Token *classDef_, const Scope *nestedIn_, ScopeType type_, const Token *start_);
Scope(const SymbolDatabase &symdb_, const Token *classDef_, const Scope *nestedIn_);
Scope(const SymbolDatabase &symdb_, const Token *classDef_, const Scope *nestedIn_, ScopeType type_, const Token *start_);

const SymbolDatabase* symdb{};
const SymbolDatabase& symdb;
std::string className;
const Token* classDef{}; ///< class/struct/union/namespace token
const Token* bodyStart{}; ///< '{' token
Expand Down

0 comments on commit 06e3c9b

Please sign in to comment.