Skip to content

Commit 1ec64ef

Browse files
committed
SymbolDatabase: got rid of some const_cast usage
1 parent 2788204 commit 1ec64ef

2 files changed

Lines changed: 39 additions & 21 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5943,47 +5943,59 @@ const Scope *SymbolDatabase::findScopeByName(const std::string& name) const
59435943

59445944
//---------------------------------------------------------------------------
59455945

5946-
const Scope *Scope::findRecordInNestedList(const std::string & name, bool isC) const
5946+
template<class S, class T, REQUIRES("S must be a Scope class", std::is_convertible<S*, const Scope*>), REQUIRES("T must be a Type class", std::is_convertible<T*, const Type*> )>
5947+
S* findRecordInNestedListImpl(S& thisScope, const std::string & name, bool isC)
59475948
{
5948-
for (const Scope* scope: nestedList) {
5949-
if (scope->className == name && scope->type != eFunction)
5949+
for (S* scope: thisScope.nestedList) {
5950+
if (scope->className == name && scope->type != Scope::eFunction)
59505951
return scope;
59515952
if (isC) {
5952-
const Scope* nestedScope = scope->findRecordInNestedList(name, isC);
5953+
S* nestedScope = scope->findRecordInNestedList(name, isC);
59535954
if (nestedScope)
59545955
return nestedScope;
59555956
}
59565957
}
59575958

5958-
const Type * nested_type = findType(name);
5959+
T * nested_type = thisScope.findType(name);
59595960

59605961
if (nested_type) {
59615962
if (nested_type->isTypeAlias()) {
59625963
if (nested_type->typeStart == nested_type->typeEnd)
5963-
return findRecordInNestedList(nested_type->typeStart->str());
5964+
return thisScope.findRecordInNestedList(nested_type->typeStart->str()); // TODO: pass isC?
59645965
} else
5965-
return nested_type->classScope;
5966+
return const_cast<S*>(nested_type->classScope);
59665967
}
59675968

59685969
return nullptr;
59695970
}
59705971

5972+
const Scope* Scope::findRecordInNestedList(const std::string & name, bool isC) const
5973+
{
5974+
return findRecordInNestedListImpl<const Scope, const Type>(*this, name, isC);
5975+
}
5976+
5977+
Scope* Scope::findRecordInNestedList(const std::string & name, bool isC)
5978+
{
5979+
return findRecordInNestedListImpl<Scope, Type>(*this, name, isC);
5980+
}
5981+
59715982
//---------------------------------------------------------------------------
59725983

5973-
const Type* Scope::findType(const std::string & name) const
5984+
template<class S, class T, REQUIRES("S must be a Scope class", std::is_convertible<S*, const Scope*> ), REQUIRES("T must be a Type class", std::is_convertible<T*, const Type*> )>
5985+
T* findTypeImpl(S& thisScope, const std::string & name)
59745986
{
5975-
auto it = definedTypesMap.find(name);
5987+
auto it = thisScope.definedTypesMap.find(name);
59765988

59775989
// Type was found
5978-
if (definedTypesMap.end() != it)
5990+
if (thisScope.definedTypesMap.end() != it)
59795991
return it->second;
59805992

59815993
// is type defined in anonymous namespace..
5982-
it = definedTypesMap.find(emptyString);
5983-
if (it != definedTypesMap.end()) {
5984-
for (const Scope *scope : nestedList) {
5985-
if (scope->className.empty() && (scope->type == eNamespace || scope->isClassOrStructOrUnion())) {
5986-
const Type *t = scope->findType(name);
5994+
it = thisScope.definedTypesMap.find(emptyString);
5995+
if (it != thisScope.definedTypesMap.end()) {
5996+
for (S *scope : thisScope.nestedList) {
5997+
if (scope->className.empty() && (scope->type == thisScope.eNamespace || scope->isClassOrStructOrUnion())) {
5998+
T *t = scope->findType(name);
59875999
if (t)
59886000
return t;
59896001
}
@@ -5994,6 +6006,16 @@ const Type* Scope::findType(const std::string & name) const
59946006
return nullptr;
59956007
}
59966008

6009+
const Type* Scope::findType(const std::string& name) const
6010+
{
6011+
return findTypeImpl<const Scope, const Type>(*this, name);
6012+
}
6013+
6014+
Type* Scope::findType(const std::string& name)
6015+
{
6016+
return findTypeImpl<Scope, Type>(*this, name);
6017+
}
6018+
59976019
//---------------------------------------------------------------------------
59986020

59996021
Scope *Scope::findInNestedListRecursive(const std::string & name)

lib/symboldatabase.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,14 +1130,10 @@ class CPPCHECKLIB Scope {
11301130
const Function *findFunction(const Token *tok, bool requireConst=false) const;
11311131

11321132
const Scope *findRecordInNestedList(const std::string & name, bool isC = false) const;
1133-
Scope *findRecordInNestedList(const std::string & name) {
1134-
return const_cast<Scope *>(const_cast<const Scope *>(this)->findRecordInNestedList(name));
1135-
}
1133+
Scope *findRecordInNestedList(const std::string & name, bool isC = false);
11361134

11371135
const Type* findType(const std::string& name) const;
1138-
Type* findType(const std::string& name) {
1139-
return const_cast<Type*>(const_cast<const Scope *>(this)->findType(name));
1140-
}
1136+
Type* findType(const std::string& name);
11411137

11421138
/**
11431139
* @brief find if name is in nested list

0 commit comments

Comments
 (0)