Modernize: Use enum class for Library::Action and Library::Yield

This commit is contained in:
Daniel Marjamäki 2019-07-17 11:39:30 +02:00
parent ec4d68e231
commit 9973db3a71
5 changed files with 50 additions and 49 deletions

View File

@ -571,14 +571,14 @@ bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token
return isDifferentKnownValues(cond1->astOperand1(), cond2->astOperand1());
}
// TODO: Handle reverse conditions
if (Library::isContainerYield(cond1, Library::Container::EMPTY, "empty") &&
Library::isContainerYield(cond2->astOperand1(), Library::Container::SIZE, "size") &&
if (Library::isContainerYield(cond1, Library::Container::Yield::EMPTY, "empty") &&
Library::isContainerYield(cond2->astOperand1(), Library::Container::Yield::SIZE, "size") &&
cond1->astOperand1()->astOperand1()->varId() == cond2->astOperand1()->astOperand1()->astOperand1()->varId()) {
return !isZeroBoundCond(cond2);
}
if (Library::isContainerYield(cond2, Library::Container::EMPTY, "empty") &&
Library::isContainerYield(cond1->astOperand1(), Library::Container::SIZE, "size") &&
if (Library::isContainerYield(cond2, Library::Container::Yield::EMPTY, "empty") &&
Library::isContainerYield(cond1->astOperand1(), Library::Container::Yield::SIZE, "size") &&
cond2->astOperand1()->astOperand1()->varId() == cond1->astOperand1()->astOperand1()->astOperand1()->varId()) {
return !isZeroBoundCond(cond1);
}

View File

@ -821,7 +821,7 @@ void CheckStl::stlOutOfBounds()
const Library::Container* container = containerToken->valueType() ? containerToken->valueType()->container : nullptr;
if (!container)
continue;
if (container->getYield(containerToken->strAt(2)) != Library::Container::SIZE)
if (container->getYield(containerToken->strAt(2)) != Library::Container::Yield::SIZE)
continue;
// variable id for loop variable.
@ -835,13 +835,13 @@ void CheckStl::stlOutOfBounds()
if (tok3->varId() == declarationId) {
tok3 = tok3->next();
if (Token::Match(tok3, ". %name% ( )")) {
if (container->getYield(tok3->strAt(1)) == Library::Container::SIZE)
if (container->getYield(tok3->strAt(1)) == Library::Container::Yield::SIZE)
break;
} else if (container->arrayLike_indexOp && Token::Match(tok3, "[ %varid% ]", numId))
stlOutOfBoundsError(tok3, tok3->strAt(1), containerName, false);
else if (Token::Match(tok3, ". %name% ( %varid% )", numId)) {
const Library::Container::Yield yield = container->getYield(tok3->strAt(1));
if (yield == Library::Container::AT_INDEX)
if (yield == Library::Container::Yield::AT_INDEX)
stlOutOfBoundsError(tok3, tok3->strAt(3), containerName, true);
}
}
@ -1205,11 +1205,11 @@ void CheckStl::if_find()
}
}
if (container && container->getAction(funcTok->str()) == Library::Container::FIND) {
if (container && container->getAction(funcTok->str()) == Library::Container::Action::FIND) {
if (if_findCompare(funcTok->next()))
continue;
if (printWarning && container->getYield(funcTok->str()) == Library::Container::ITERATOR)
if (printWarning && container->getYield(funcTok->str()) == Library::Container::Yield::ITERATOR)
if_findError(tok, false);
else if (printPerformance && container->stdStringLike && funcTok->str() == "find")
if_findError(tok, true);

View File

@ -420,55 +420,55 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
return Error(MISSING_ATTRIBUTE, "name");
const char* const action_ptr = functionNode->Attribute("action");
Container::Action action = Container::NO_ACTION;
Container::Action action = Container::Action::NO_ACTION;
if (action_ptr) {
std::string actionName = action_ptr;
if (actionName == "resize")
action = Container::RESIZE;
action = Container::Action::RESIZE;
else if (actionName == "clear")
action = Container::CLEAR;
action = Container::Action::CLEAR;
else if (actionName == "push")
action = Container::PUSH;
action = Container::Action::PUSH;
else if (actionName == "pop")
action = Container::POP;
action = Container::Action::POP;
else if (actionName == "find")
action = Container::FIND;
action = Container::Action::FIND;
else if (actionName == "insert")
action = Container::INSERT;
action = Container::Action::INSERT;
else if (actionName == "erase")
action = Container::ERASE;
action = Container::Action::ERASE;
else if (actionName == "change-content")
action = Container::CHANGE_CONTENT;
action = Container::Action::CHANGE_CONTENT;
else if (actionName == "change-internal")
action = Container::CHANGE_INTERNAL;
action = Container::Action::CHANGE_INTERNAL;
else if (actionName == "change")
action = Container::CHANGE;
action = Container::Action::CHANGE;
else
return Error(BAD_ATTRIBUTE_VALUE, actionName);
}
const char* const yield_ptr = functionNode->Attribute("yields");
Container::Yield yield = Container::NO_YIELD;
Container::Yield yield = Container::Yield::NO_YIELD;
if (yield_ptr) {
std::string yieldName = yield_ptr;
if (yieldName == "at_index")
yield = Container::AT_INDEX;
yield = Container::Yield::AT_INDEX;
else if (yieldName == "item")
yield = Container::ITEM;
yield = Container::Yield::ITEM;
else if (yieldName == "buffer")
yield = Container::BUFFER;
yield = Container::Yield::BUFFER;
else if (yieldName == "buffer-nt")
yield = Container::BUFFER_NT;
yield = Container::Yield::BUFFER_NT;
else if (yieldName == "start-iterator")
yield = Container::START_ITERATOR;
yield = Container::Yield::START_ITERATOR;
else if (yieldName == "end-iterator")
yield = Container::END_ITERATOR;
yield = Container::Yield::END_ITERATOR;
else if (yieldName == "iterator")
yield = Container::ITERATOR;
yield = Container::Yield::ITERATOR;
else if (yieldName == "size")
yield = Container::SIZE;
yield = Container::Yield::SIZE;
else if (yieldName == "empty")
yield = Container::EMPTY;
yield = Container::Yield::EMPTY;
else
return Error(BAD_ATTRIBUTE_VALUE, yieldName);
}

View File

@ -199,11 +199,11 @@ public:
opLessAllowed(true) {
}
enum Action {
enum class Action {
RESIZE, CLEAR, PUSH, POP, FIND, INSERT, ERASE, CHANGE_CONTENT, CHANGE, CHANGE_INTERNAL,
NO_ACTION
};
enum Yield {
enum class Yield {
AT_INDEX, ITEM, BUFFER, BUFFER_NT, START_ITERATOR, END_ITERATOR, ITERATOR, SIZE, EMPTY,
NO_YIELD
};
@ -224,14 +224,14 @@ public:
const std::map<std::string, Function>::const_iterator i = functions.find(function);
if (i != functions.end())
return i->second.action;
return NO_ACTION;
return Action::NO_ACTION;
}
Yield getYield(const std::string& function) const {
const std::map<std::string, Function>::const_iterator i = functions.find(function);
if (i != functions.end())
return i->second.yield;
return NO_YIELD;
return Yield::NO_YIELD;
}
};
std::map<std::string, Container> containers;

View File

@ -31,6 +31,7 @@
#include <string>
#include <vector>
#define ASSERT_EQ(expected, actual) ASSERT(expected == actual)
class TestLibrary : public TestFixture {
public:
@ -792,21 +793,21 @@ private:
ASSERT_EQUALS(A.stdStringLike, false);
ASSERT_EQUALS(A.arrayLike_indexOp, false);
ASSERT_EQUALS(A.opLessAllowed, true);
ASSERT_EQUALS(Library::Container::SIZE, A.getYield("size"));
ASSERT_EQUALS(Library::Container::EMPTY, A.getYield("empty"));
ASSERT_EQUALS(Library::Container::AT_INDEX, A.getYield("at"));
ASSERT_EQUALS(Library::Container::START_ITERATOR, A.getYield("begin"));
ASSERT_EQUALS(Library::Container::END_ITERATOR, A.getYield("end"));
ASSERT_EQUALS(Library::Container::BUFFER, A.getYield("data"));
ASSERT_EQUALS(Library::Container::BUFFER_NT, A.getYield("c_str"));
ASSERT_EQUALS(Library::Container::ITEM, A.getYield("front"));
ASSERT_EQUALS(Library::Container::NO_YIELD, A.getYield("foo"));
ASSERT_EQUALS(Library::Container::RESIZE, A.getAction("resize"));
ASSERT_EQUALS(Library::Container::CLEAR, A.getAction("clear"));
ASSERT_EQUALS(Library::Container::PUSH, A.getAction("push_back"));
ASSERT_EQUALS(Library::Container::POP, A.getAction("pop_back"));
ASSERT_EQUALS(Library::Container::FIND, A.getAction("find"));
ASSERT_EQUALS(Library::Container::NO_ACTION, A.getAction("foo"));
ASSERT_EQ(Library::Container::Yield::SIZE, A.getYield("size"));
ASSERT_EQ(Library::Container::Yield::EMPTY, A.getYield("empty"));
ASSERT_EQ(Library::Container::Yield::AT_INDEX, A.getYield("at"));
ASSERT_EQ(Library::Container::Yield::START_ITERATOR, A.getYield("begin"));
ASSERT_EQ(Library::Container::Yield::END_ITERATOR, A.getYield("end"));
ASSERT_EQ(Library::Container::Yield::BUFFER, A.getYield("data"));
ASSERT_EQ(Library::Container::Yield::BUFFER_NT, A.getYield("c_str"));
ASSERT_EQ(Library::Container::Yield::ITEM, A.getYield("front"));
ASSERT_EQ(Library::Container::Yield::NO_YIELD, A.getYield("foo"));
ASSERT_EQ(Library::Container::Action::RESIZE, A.getAction("resize"));
ASSERT_EQ(Library::Container::Action::CLEAR, A.getAction("clear"));
ASSERT_EQ(Library::Container::Action::PUSH, A.getAction("push_back"));
ASSERT_EQ(Library::Container::Action::POP, A.getAction("pop_back"));
ASSERT_EQ(Library::Container::Action::FIND, A.getAction("find"));
ASSERT_EQ(Library::Container::Action::NO_ACTION, A.getAction("foo"));
ASSERT_EQUALS(B.type_templateArgNo, 1);
ASSERT_EQUALS(B.size_templateArgNo, 3);