From c9fb7f529d381bdb714ac9c6d31ff49ac30b1fea Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Thu, 3 Jan 2013 22:37:19 +0100 Subject: [PATCH] Use more Effective C++ code style in SymbolDatabase class: 1)The SymbolDatabase::findScope code is moved to the const-version. The non-const version is reimplemented as the combination with const_cast and static_cast. 2)Unrelated: change style of the declaration of some functions (the '*' is moved near the function name). --- lib/symboldatabase.cpp | 21 ++++++++------------- lib/symboldatabase.h | 28 ++++++++++++++++------------ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index f0193eb3b..be1069887 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -2377,7 +2377,7 @@ const Function* SymbolDatabase::findFunctionByNameAndArgs(const Token *tok, cons //--------------------------------------------------------------------------- -const Scope* SymbolDatabase::findScopeByName(const std::string& name) const +const Scope *SymbolDatabase::findScopeByName(const std::string& name) const { for (std::list::const_iterator it = scopeList.begin(); it != scopeList.end(); ++it) { if (it->className == name) @@ -2388,7 +2388,7 @@ const Scope* SymbolDatabase::findScopeByName(const std::string& name) const //--------------------------------------------------------------------------- -Scope * Scope::findInNestedList(const std::string & name) +Scope *Scope::findInNestedList(const std::string & name) { std::list::iterator it; @@ -2401,7 +2401,7 @@ Scope * Scope::findInNestedList(const std::string & name) //--------------------------------------------------------------------------- -Scope * Scope::findRecordInNestedList(const std::string & name) +const Scope *Scope::findRecordInNestedList(const std::string & name) const { std::list::const_iterator it; @@ -2414,7 +2414,7 @@ Scope * Scope::findRecordInNestedList(const std::string & name) //--------------------------------------------------------------------------- -Scope * Scope::findInNestedListRecursive(const std::string & name) +Scope *Scope::findInNestedListRecursive(const std::string & name) { std::list::iterator it; @@ -2433,7 +2433,7 @@ Scope * Scope::findInNestedListRecursive(const std::string & name) //--------------------------------------------------------------------------- -const Scope * Scope::findQualifiedScope(const std::string & name) const +const Scope *Scope::findQualifiedScope(const std::string & name) const { if (type == Scope::eClass || type == Scope::eStruct || type == Scope::eNamespace) { if (name.compare(0, className.size(), className) == 0) { @@ -2490,17 +2490,12 @@ bool SymbolDatabase::isCPP() const //--------------------------------------------------------------------------- -const Scope * SymbolDatabase::findScope(const Token *tok, const Scope *startScope) const -{ - return const_cast(this)->findScope(tok, const_cast(startScope)); -} - -Scope * SymbolDatabase::findScope(const Token *tok, Scope *startScope) +const Scope *SymbolDatabase::findScope(const Token *tok, const Scope *startScope) const { // absolute path if (tok->str() == "::") { tok = tok->next(); - Scope *scope = &scopeList.front(); + const Scope *scope = &scopeList.front(); while (scope && tok && tok->isName()) { scope = scope->findRecordInNestedList(tok->str()); @@ -2519,7 +2514,7 @@ Scope * SymbolDatabase::findScope(const Token *tok, Scope *startScope) // relative path else if (tok->isName()) { - Scope *scope = startScope; + const Scope *scope = startScope; while (scope && tok && tok->isName()) { scope = scope->findRecordInNestedList(tok->str()); diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index f09112b16..fcada5c46 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -509,17 +509,20 @@ public: * @brief find if name is in nested list * @param name name of nested scope */ - Scope * findInNestedList(const std::string & name); + Scope *findInNestedList(const std::string & name); - Scope * findRecordInNestedList(const std::string & name); + const Scope *findRecordInNestedList(const std::string & name) const; + Scope *findRecordInNestedList(const std::string & name) { + return const_cast(static_cast(this)->findRecordInNestedList(name)); + } /** * @brief find if name is in nested list * @param name name of nested scope */ - Scope * findInNestedListRecursive(const std::string & name); + Scope *findInNestedListRecursive(const std::string & name); - const Scope * findQualifiedScope(const std::string & name) const; + const Scope *findQualifiedScope(const std::string & name) const; void addVariable(const Token *token_, const Token *start_, const Token *end_, AccessControl access_, const Scope *type_, @@ -599,7 +602,7 @@ public: const Function *findFunctionByToken(const Token *tok) const; - const Function* findFunctionByName(const std::string& str, const Scope* startScope) const; + const Function *findFunctionByName(const std::string& str, const Scope* startScope) const; /** * @brief find a function by name and arguments @@ -607,13 +610,16 @@ public: * @param startScope scope to start looking in * @return pointer to function if found or NULL if not found */ - const Function* findFunctionByNameAndArgs(const Token *tok, const Scope *startScope) const; + const Function *findFunctionByNameAndArgs(const Token *tok, const Scope *startScope) const; - const Function* findFunctionByNameAndArgsInScope(const Token *tok, const Scope *scope) const; + const Function *findFunctionByNameAndArgsInScope(const Token *tok, const Scope *scope) const; - const Scope* findScopeByName(const std::string& name) const; + const Scope *findScopeByName(const std::string& name) const; const Scope *findScope(const Token *tok, const Scope *startScope) const; + Scope *findScope(const Token *tok, Scope *startScope) const { + return const_cast(this->findScope(tok, static_cast(startScope))); + } bool isClassOrStruct(const std::string &type) const { return bool(classAndStructTypes.find(type) != classAndStructTypes.end()); @@ -643,13 +649,11 @@ private: friend class Scope; void addClassFunction(Scope **info, const Token **tok, const Token *argStart); - Function* addGlobalFunctionDecl(Scope*& scope, const Token *argStart, const Token* funcStart); - Function* addGlobalFunction(Scope*& scope, const Token*& tok, const Token *argStart, const Token* funcStart); + Function *addGlobalFunctionDecl(Scope*& scope, const Token *argStart, const Token* funcStart); + Function *addGlobalFunction(Scope*& scope, const Token*& tok, const Token *argStart, const Token* funcStart); void addNewFunction(Scope **info, const Token **tok); static bool isFunction(const Token *tok, const Scope* outerScope, const Token **funcStart, const Token **argStart); - Scope *findScope(const Token *tok, Scope *startScope); - /** class/struct types */ std::set classAndStructTypes;