From f02636e995ce7e7ec47ae00a3d814fba06820921 Mon Sep 17 00:00:00 2001 From: amai2012 Date: Fri, 2 Aug 2019 21:14:29 +0200 Subject: [PATCH] Refactoring: Convert enums to enum classes --- lib/astutils.h | 2 +- lib/checkclass.cpp | 8 ++++---- lib/checkleakautovar.cpp | 2 +- lib/checkuninitvar.cpp | 6 +++--- lib/checkunusedvar.cpp | 2 +- lib/symboldatabase.cpp | 28 ++++++++++++++-------------- lib/symboldatabase.h | 6 +++--- lib/valueflow.cpp | 2 +- 8 files changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/astutils.h b/lib/astutils.h index eb9200f1f..437411f54 100644 --- a/lib/astutils.h +++ b/lib/astutils.h @@ -175,7 +175,7 @@ bool isConstVarExpression(const Token *tok); const Variable *getLHSVariable(const Token *tok); struct PathAnalysis { - enum Progress { + enum class Progress { Continue, Break }; diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index d787429c5..e17b20dcb 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -69,7 +69,7 @@ static const char * getFunctionTypeName(Function::Type type) static bool isVariableCopyNeeded(const Variable &var) { - return var.isPointer() || (var.type() && var.type()->needInitialization == Type::True) || (var.valueType()->type >= ValueType::Type::CHAR); + return var.isPointer() || (var.type() && var.type()->needInitialization == Type::NeedInitialization::True) || (var.valueType()->type >= ValueType::Type::CHAR); } //--------------------------------------------------------------------------- @@ -117,7 +117,7 @@ void CheckClass::constructors() initTok = initTok->linkAt(1); if (var.isPrivate() && !var.isStatic() && !Token::Match(var.nameToken(), "%varid% ; %varid% =", var.declarationId()) && !Token::Match(initTok, "%var%|] {|=") && - (!var.isClass() || (var.type() && var.type()->needInitialization == Type::True))) { + (!var.isClass() || (var.type() && var.type()->needInitialization == Type::NeedInitialization::True))) { noConstructorError(scope->classDef, scope->className, scope->classDef->str() == "struct"); break; } @@ -175,7 +175,7 @@ void CheckClass::constructors() if (usage[count].assign || usage[count].init || var.isStatic()) continue; - if (var.valueType()->pointer == 0 && var.type() && var.type()->needInitialization == Type::False && var.type()->derivedFrom.empty()) + if (var.valueType()->pointer == 0 && var.type() && var.type()->needInitialization == Type::NeedInitialization::False && var.type()->derivedFrom.empty()) continue; if (var.isConst() && func.isOperator()) // We can't set const members in assignment operator @@ -189,7 +189,7 @@ void CheckClass::constructors() // Known type that doesn't need initialization or // known type that has member variables of an unknown type - else if (var.type()->needInitialization != Type::True) + else if (var.type()->needInitialization != Type::NeedInitialization::True) continue; } diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index cdc1d6ed8..1614651fb 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -69,7 +69,7 @@ static bool isAutoDealloc(const Variable *var) /** @todo false negative: check base class for side effects */ /** @todo false negative: check constructors for side effects */ if (var->typeScope() && var->typeScope()->numConstructors == 0 && - (var->typeScope()->varlist.empty() || var->type()->needInitialization == Type::True) && + (var->typeScope()->varlist.empty() || var->type()->needInitialization == Type::NeedInitialization::True) && var->type()->derivedFrom.empty()) return false; diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 600316d8f..4131fbc9c 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -99,7 +99,7 @@ void CheckUninitVar::check() void CheckUninitVar::checkScope(const Scope* scope, const std::set &arrayTypeDefs) { for (const Variable &var : scope->varlist) { - if ((mTokenizer->isCPP() && var.type() && !var.isPointer() && var.type()->needInitialization != Type::True) || + if ((mTokenizer->isCPP() && var.type() && !var.isPointer() && var.type()->needInitialization != Type::NeedInitialization::True) || var.isStatic() || var.isExtern() || var.isReference()) continue; @@ -197,7 +197,7 @@ void CheckUninitVar::checkStruct(const Token *tok, const Variable &structvar) if (scope2->className == typeToken->str() && scope2->numConstructors == 0U) { for (const Variable &var : scope2->varlist) { if (var.isStatic() || var.hasDefault() || var.isArray() || - (!mTokenizer->isC() && var.isClass() && (!var.type() || var.type()->needInitialization != Type::True))) + (!mTokenizer->isC() && var.isClass() && (!var.type() || var.type()->needInitialization != Type::NeedInitialization::True))) continue; // is the variable declared in a inner union? @@ -693,7 +693,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var continue; } } - if (var.isPointer() && (var.typeStartToken()->isStandardType() || var.typeStartToken()->isEnumType() || (var.type() && var.type()->needInitialization == Type::True)) && Token::simpleMatch(tok->next(), "= new")) { + if (var.isPointer() && (var.typeStartToken()->isStandardType() || var.typeStartToken()->isEnumType() || (var.type() && var.type()->needInitialization == Type::NeedInitialization::True)) && Token::simpleMatch(tok->next(), "= new")) { *alloc = CTOR_CALL; // type has constructor(s) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index e9ec060fe..efb6b8793 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1376,7 +1376,7 @@ bool CheckUnusedVar::isRecordTypeWithoutSideEffects(const Type* type) return withoutSideEffects; if (type && type->classScope && type->classScope->numConstructors == 0 && - (type->classScope->varlist.empty() || type->needInitialization == Type::True)) { + (type->classScope->varlist.empty() || type->needInitialization == Type::NeedInitialization::True)) { for (std::vector::const_iterator i = type->derivedFrom.begin(); i != type->derivedFrom.end(); ++i) { if (!isRecordTypeWithoutSideEffects(i->type)) { withoutSideEffects=false; diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 18d323fd1..9cae0b18f 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -826,7 +826,7 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization() for (std::list::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { Scope *scope = &(*it); if (scope->definedType) - scope->definedType->needInitialization = Type::True; + scope->definedType->needInitialization = Type::NeedInitialization::True; } } else { // For C++, it is more difficult: Determine if user defined type needs initialization... @@ -844,7 +844,7 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization() scope->definedType = &mBlankTypes.back(); } - if (scope->isClassOrStruct() && scope->definedType->needInitialization == Type::Unknown) { + if (scope->isClassOrStruct() && scope->definedType->needInitialization == Type::NeedInitialization::Unknown) { // check for default constructor bool hasDefaultConstructor = false; @@ -870,7 +870,7 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization() // We assume the default constructor initializes everything. // Another check will figure out if the constructor actually initializes everything. if (hasDefaultConstructor) - scope->definedType->needInitialization = Type::False; + scope->definedType->needInitialization = Type::NeedInitialization::False; // check each member variable to see if it needs initialization else { @@ -882,9 +882,9 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization() if (var->isClass()) { if (var->type()) { // does this type need initialization? - if (var->type()->needInitialization == Type::True) + if (var->type()->needInitialization == Type::NeedInitialization::True) needInitialization = true; - else if (var->type()->needInitialization == Type::Unknown) { + else if (var->type()->needInitialization == Type::NeedInitialization::Unknown) { if (!(var->valueType() && var->valueType()->type == ValueType::CONTAINER)) unknown = true; } @@ -894,16 +894,16 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization() } if (needInitialization) - scope->definedType->needInitialization = Type::True; + scope->definedType->needInitialization = Type::NeedInitialization::True; else if (!unknown) - scope->definedType->needInitialization = Type::False; + scope->definedType->needInitialization = Type::NeedInitialization::False; else { - if (scope->definedType->needInitialization == Type::Unknown) + if (scope->definedType->needInitialization == Type::NeedInitialization::Unknown) unknowns++; } } - } else if (scope->type == Scope::eUnion && scope->definedType->needInitialization == Type::Unknown) - scope->definedType->needInitialization = Type::True; + } else if (scope->type == Scope::eUnion && scope->definedType->needInitialization == Type::NeedInitialization::Unknown) + scope->definedType->needInitialization = Type::NeedInitialization::True; } retry++; @@ -914,7 +914,7 @@ void SymbolDatabase::createSymbolDatabaseNeedInitialization() for (std::list::iterator it = scopeList.begin(); it != scopeList.end(); ++it) { const Scope *scope = &(*it); - if (scope->isClassOrStruct() && scope->definedType->needInitialization == Type::Unknown) + if (scope->isClassOrStruct() && scope->definedType->needInitialization == Type::NeedInitialization::Unknown) debugMessage(scope->classDef, "SymbolDatabase::SymbolDatabase couldn't resolve all user defined types."); } } @@ -2919,9 +2919,9 @@ void SymbolDatabase::printOut(const char *title) const << type->enclosingScope->className; } std::cout << std::endl; - std::cout << " needInitialization: " << (type->needInitialization == Type::Unknown ? "Unknown" : - type->needInitialization == Type::True ? "True" : - type->needInitialization == Type::False ? "False" : + std::cout << " needInitialization: " << (type->needInitialization == Type::NeedInitialization::Unknown ? "Unknown" : + type->needInitialization == Type::NeedInitialization::True ? "True" : + type->needInitialization == Type::NeedInitialization::False ? "False" : "Invalid") << std::endl; std::cout << " derivedFrom[" << type->derivedFrom.size() << "] = ("; diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index 8de5a2f0d..6281a8ef8 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -65,7 +65,7 @@ public: const Token* classDef; ///< Points to "class" token const Scope* classScope; const Scope* enclosingScope; - enum NeedInitialization { + enum class NeedInitialization { Unknown, True, False } needInitialization; @@ -106,11 +106,11 @@ public: classDef(classDef_), classScope(classScope_), enclosingScope(enclosingScope_), - needInitialization(Unknown), + needInitialization(NeedInitialization::Unknown), typeStart(nullptr), typeEnd(nullptr) { if (classDef_ && classDef_->str() == "enum") - needInitialization = True; + needInitialization = NeedInitialization::True; else if (classDef_ && classDef_->str() == "using") { typeStart = classDef->tokAt(3); typeEnd = typeStart; diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 97210e92e..ea05eef37 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4906,7 +4906,7 @@ static void valueFlowUninit(TokenList *tokenlist, SymbolDatabase * /*symbolDatab const Variable *var = vardecl->variable(); if (!var || var->nameToken() != vardecl) continue; - if ((!var->isPointer() && var->type() && var->type()->needInitialization != Type::True) || + if ((!var->isPointer() && var->type() && var->type()->needInitialization != Type::NeedInitialization::True) || !var->isLocal() || var->isStatic() || var->isExtern() || var->isReference() || var->isThrow()) continue;