Revert "SymbolDatabase; First fix to handle when a namespace have many scopes"

This reverts commit e6cc7201b0.
This commit is contained in:
Daniel Marjamäki 2021-08-04 22:30:39 +02:00
parent ad478914f7
commit 27a4df06d2
3 changed files with 31 additions and 81 deletions

View File

@ -41,21 +41,6 @@
#include <unordered_map>
//---------------------------------------------------------------------------
static std::vector<const Scope *> getAllScopes(const Scope *scope)
{
if (!scope)
return {};
if (scope->type != Scope::ScopeType::eNamespace)
return {scope};
std::vector<const Scope *> ret;
for (const Scope *s: scope->nestedIn->nestedList) {
if (s->type == Scope::ScopeType::eNamespace && s->className == scope->className)
ret.push_back(s);
}
return ret;
}
//---------------------------------------------------------------------------
SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: mTokenizer(tokenizer), mSettings(settings), mErrorLogger(errorLogger)
{
@ -226,8 +211,6 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
name = name->next();
Scope *new_scope = findScope(name, scope);
if (new_scope && new_scope->bodyStart && Token::Match(name, "%name% {") && new_scope->type == Scope::ScopeType::eNamespace)
new_scope = nullptr;
if (new_scope) {
// only create base list for classes and structures
@ -747,21 +730,14 @@ void SymbolDatabase::createSymbolDatabaseClassInfo()
// fill in base class info
for (Type& type : typeList) {
const std::vector<const Scope *> allEnclosingScopes = getAllScopes(type.enclosingScope);
// finish filling in base class info
for (Type::BaseInfo & baseInfo : type.derivedFrom) {
baseInfo.type = nullptr;
for (const Scope *enclosingScope: allEnclosingScopes) {
const Type* found = findType(baseInfo.nameTok, enclosingScope);
if (found) {
if (found->findDependency(&type)) {
// circular dependency
//mTokenizer->syntaxError(nullptr);
} else {
baseInfo.type = found;
break;
}
}
for (Type::BaseInfo & i : type.derivedFrom) {
const Type* found = findType(i.nameTok, type.enclosingScope);
if (found && found->findDependency(&type)) {
// circular dependency
//mTokenizer->syntaxError(nullptr);
} else {
i.type = found;
}
}
}
@ -4723,16 +4699,15 @@ const Type* SymbolDatabase::findVariableType(const Scope *start, const Token *ty
return start->definedType;
while (scope) {
for (const Scope *scope2: getAllScopes(scope)) {
// look for type in this scope
const Type * type = scope2->findType(typeTok->str());
if (type)
return type;
}
// look for type in this scope
const Type * type = scope->findType(typeTok->str());
if (type)
return type;
// look for type in base classes if possible
if (scope->isClassOrStruct()) {
const Type* type = findVariableTypeInBase(scope, typeTok);
type = findVariableTypeInBase(scope, typeTok);
if (type)
return type;
@ -4742,7 +4717,7 @@ const Type* SymbolDatabase::findVariableType(const Scope *start, const Token *ty
if (scope->type == Scope::eFunction && scope->functionOf) {
const Scope *scope1 = scope->functionOf;
const Type* type = scope1->findType(typeTok->str());
type = scope1->findType(typeTok->str());
if (type)
return type;

View File

@ -788,7 +788,7 @@ private:
}
void garbageCode83() { // #6771
checkCode("namespace A { class } class A { friend C ; } { } ;");
ASSERT_THROW(checkCode("namespace A { class } class A { friend C ; } { } ;"), InternalError);
}
void garbageCode84() { // #6780

View File

@ -259,7 +259,6 @@ private:
TEST_CASE(namespaces2);
TEST_CASE(namespaces3); // #3854 - unknown macro
TEST_CASE(namespaces4);
TEST_CASE(namespacesSameName1);
TEST_CASE(tryCatch1);
@ -355,7 +354,6 @@ private:
TEST_CASE(symboldatabase95); // #10295
TEST_CASE(createSymbolDatabaseFindAllScopes1);
TEST_CASE(createSymbolDatabaseFindAllScopes2);
TEST_CASE(enum1);
TEST_CASE(enum2);
@ -2608,18 +2606,6 @@ private:
ASSERT_EQUALS(2U, fredAType->classDef->linenr());
}
void namespacesSameName1() {
GET_SYMBOL_DB("namespace N { struct Base{}; }\n"
"namespace N { class Derived : public Base {}; }\n");
const Token *Base = Token::findsimplematch(tokenizer.tokens(), "public Base")->next();
const Type *baseType = Base->type();
ASSERT(baseType);
const Type *derivedType = Token::findsimplematch(tokenizer.tokens(), "Derived")->type();
ASSERT(derivedType);
ASSERT_EQUALS(1, derivedType->derivedFrom.size());
ASSERT_EQUALS(baseType, derivedType->derivedFrom[0].type);
}
void tryCatch1() {
const char str[] = "void foo() {\n"
" try { }\n"
@ -3808,31 +3794,31 @@ private:
"}");
ASSERT(db != nullptr);
ASSERT_EQUALS(9, db->scopeList.size());
ASSERT_EQUALS(2, db->classAndStructScopes.size());
ASSERT_EQUALS(2, db->typeList.size());
ASSERT_EQUALS(4, db->functionScopes.size());
ASSERT(db && db->scopeList.size() == 8);
ASSERT(db && db->classAndStructScopes.size() == 2);
ASSERT(db && db->typeList.size() == 2);
ASSERT(db && db->functionScopes.size() == 4);
const Token * functionToken = Token::findsimplematch(tokenizer.tokens(), "impl ( ) { }");
ASSERT(functionToken && functionToken->function() &&
ASSERT(db && functionToken && functionToken->function() &&
functionToken->function()->functionScope &&
functionToken->function()->tokenDef->linenr() == 5 &&
functionToken->function()->token->linenr() == 11);
functionToken = Token::findsimplematch(tokenizer.tokens(), "~ impl ( ) { }");
ASSERT(functionToken && functionToken->next()->function() &&
ASSERT(db && functionToken && functionToken->next()->function() &&
functionToken->next()->function()->functionScope &&
functionToken->next()->function()->tokenDef->linenr() == 6 &&
functionToken->next()->function()->token->linenr() == 12);
functionToken = Token::findsimplematch(tokenizer.tokens(), "impl ( const Fred :: impl & ) { }");
ASSERT(functionToken && functionToken->function() &&
ASSERT(db && functionToken && functionToken->function() &&
functionToken->function()->functionScope &&
functionToken->function()->tokenDef->linenr() == 7 &&
functionToken->function()->token->linenr() == 13);
functionToken = Token::findsimplematch(tokenizer.tokens(), "foo ( const Fred :: impl & , const Fred :: impl & ) const { }");
ASSERT(functionToken && functionToken->function() &&
ASSERT(db && functionToken && functionToken->function() &&
functionToken->function()->functionScope &&
functionToken->function()->tokenDef->linenr() == 8 &&
functionToken->function()->token->linenr() == 14);
@ -4200,31 +4186,31 @@ private:
"}");
ASSERT(db != nullptr);
ASSERT_EQUALS(9, db->scopeList.size());
ASSERT_EQUALS(2, db->classAndStructScopes.size());
ASSERT_EQUALS(2, db->typeList.size());
ASSERT_EQUALS(4, db->functionScopes.size());
ASSERT(db && db->scopeList.size() == 8);
ASSERT(db && db->classAndStructScopes.size() == 2);
ASSERT(db && db->typeList.size() == 2);
ASSERT(db && db->functionScopes.size() == 4);
const Token * functionToken = Token::findsimplematch(tokenizer.tokens(), "impl ( ) { }");
ASSERT(functionToken && functionToken->function() &&
ASSERT(db && functionToken && functionToken->function() &&
functionToken->function()->functionScope &&
functionToken->function()->tokenDef->linenr() == 5 &&
functionToken->function()->token->linenr() == 11);
functionToken = Token::findsimplematch(tokenizer.tokens(), "~ impl ( ) { }");
ASSERT(functionToken && functionToken->next()->function() &&
ASSERT(db && functionToken && functionToken->next()->function() &&
functionToken->next()->function()->functionScope &&
functionToken->next()->function()->tokenDef->linenr() == 6 &&
functionToken->next()->function()->token->linenr() == 12);
functionToken = Token::findsimplematch(tokenizer.tokens(), "impl ( const Fred < A > :: impl & ) { }");
ASSERT(functionToken && functionToken->function() &&
ASSERT(db && functionToken && functionToken->function() &&
functionToken->function()->functionScope &&
functionToken->function()->tokenDef->linenr() == 7 &&
functionToken->function()->token->linenr() == 13);
functionToken = Token::findsimplematch(tokenizer.tokens(), "foo ( const Fred < A > :: impl & , const Fred < A > :: impl & ) const { }");
ASSERT(functionToken && functionToken->function() &&
ASSERT(db && functionToken && functionToken->function() &&
functionToken->function()->functionScope &&
functionToken->function()->tokenDef->linenr() == 8 &&
functionToken->function()->token->linenr() == 14);
@ -4803,17 +4789,6 @@ private:
ASSERT_EQUALS(Scope::eUnion, db->scopeList.back().type);
}
void createSymbolDatabaseFindAllScopes2() {
GET_SYMBOL_DB("namespace ns { auto var1{0}; }\n"
"namespace ns { auto var2{0}; }\n");
ASSERT(db);
ASSERT_EQUALS(3, db->scopeList.size());
const Token* const var1 = Token::findsimplematch(tokenizer.tokens(), "var1");
const Token* const var2 = Token::findsimplematch(tokenizer.tokens(), "var2");
ASSERT(var1->variable());
ASSERT(var2->variable());
}
void enum1() {
GET_SYMBOL_DB("enum BOOL { FALSE, TRUE }; enum BOOL b;");