Use library config for unstable containers instead of hardcoded values (#2585)

* Use library config for unstable containers instead of hardcoded values

* Fix xml validation
This commit is contained in:
Paul Fultz II 2020-04-03 06:16:57 -05:00 committed by GitHub
parent 58e3f19ed8
commit a22a77c1fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 7 deletions

View File

@ -69,7 +69,9 @@
<container id="boostMultiSet" startPattern="boost :: flat_multiset|multiset &lt;" inherits="stdMultiSet"/>
<container id="boostMap" startPattern="boost :: map|flat_map|unordered_map &lt;" inherits="stdMap"/>
<container id="boostSet" startPattern="boost :: set|flat_set|unordered_set &lt;" inherits="stdSet"/>
<container id="boostVectorDeque" startPattern="boost :: deque|vector|small_vector|stable_vector|static_vector &lt;" inherits="stdVectorDeque"/>
<container id="boostVector" startPattern="boost :: vector|small_vector &lt;" inherits="stdVector"/>
<container id="boostStableVector" startPattern="boost :: stable_vector|static_vector &lt;" inherits="stdVectorDeque"/>
<container id="boostDeque" startPattern="boost :: deque &lt;" inherits="stdDeque"/>
<!-- ########## Boost smart pointers ########## -->
<!-- https://www.boost.org/doc/libs/1_70_0/libs/smart_ptr/doc/html/smart_ptr.html -->
<smart-pointer class-name="boost::scoped_ptr"/>

View File

@ -391,6 +391,19 @@
<value>std-like</value>
</attribute>
</optional>
<optional>
<attribute name="unstable">
<list>
<oneOrMore>
<data type="string"/>
<choice>
<value>erase</value>
<value>insert</value>
</choice>
</oneOrMore>
</list>
</attribute>
</optional>
<empty/>
</element>
<element name="size">

View File

@ -7871,7 +7871,7 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
<function name="crend" yields="end-iterator"/>
</access>
</container>
<container id="stdVectorDeque" startPattern="std :: vector|deque &lt;" inherits="stdContainer" opLessAllowed="true">
<container id="stdVectorDeque" inherits="stdContainer" opLessAllowed="true">
<size>
<function name="push_back" action="push"/>
<function name="emplace_back" action="push"/>
@ -7889,6 +7889,12 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
<function name="reserve" action="change-internal"/>
</access>
</container>
<container id="stdVector" startPattern="std :: vector &lt;" inherits="stdVectorDeque">
<type unstable="erase insert" />
</container>
<container id="stdDeque" startPattern="std :: deque &lt;" inherits="stdVectorDeque">
<type unstable="erase insert" />
</container>
<container id="stdArray" startPattern="std :: array &lt;" inherits="stdContainer" opLessAllowed="true">
<size templateParameter="1">
<function name="max_size" yields="size"/>
@ -7987,7 +7993,7 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
</access>
</container>
<container id="stdAllString" inherits="stdContainer" opLessAllowed="true" hasInitializerListConstructor="false">
<type string="std-like"/>
<type string="std-like" unstable="erase insert"/>
<size>
<function name="push_back" action="push"/>
<function name="pop_back" action="pop"/>

View File

@ -710,12 +710,35 @@ void CheckStl::mismatchingContainers()
static bool isInvalidMethod(const Token * tok)
{
if (Token::Match(tok->next(), ". assign|clear"))
if (Token::Match(tok->next(), ". assign|clear|swap"))
return true;
if (Token::Match(tok->next(), "%assign%"))
return true;
if (isVector(tok) && Token::Match(tok->next(), ". insert|emplace|emplace_back|push_back|erase|pop_back|reserve ("))
return true;
if (const Library::Container * c = tok->valueType()->container) {
Library::Container::Action action = c->getAction(tok->strAt(2));
if (c->unstableErase) {
if (action == Library::Container::Action::ERASE)
return true;
}
if (c->unstableInsert) {
if (action == Library::Container::Action::RESIZE)
return true;
if (action == Library::Container::Action::CLEAR)
return true;
if (action == Library::Container::Action::PUSH)
return true;
if (action == Library::Container::Action::POP)
return true;
if (action == Library::Container::Action::INSERT)
return true;
if (action == Library::Container::Action::CHANGE)
return true;
if (action == Library::Container::Action::CHANGE_INTERNAL)
return true;
if (Token::Match(tok->next(), ". insert|emplace"))
return true;
}
}
return false;
}

View File

@ -31,6 +31,7 @@
#include <cstdlib>
#include <cstring>
#include <list>
#include <string>
static std::vector<std::string> getnames(const char *names)
{
@ -500,6 +501,14 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
const char* const associative = containerNode->Attribute("associative");
if (associative)
container.stdAssociativeLike = std::string(associative) == "std-like";
const char* const unstable = containerNode->Attribute("unstable");
if (unstable) {
std::string unstableType = unstable;
if (unstableType.find("erase") != std::string::npos)
container.unstableErase = true;
if (unstableType.find("insert") != std::string::npos)
container.unstableInsert = true;
}
} else
unknown_elements.insert(containerNodeName);
}

View File

@ -197,7 +197,9 @@ public:
stdStringLike(false),
stdAssociativeLike(false),
opLessAllowed(true),
hasInitializerListConstructor(false) {
hasInitializerListConstructor(false),
unstableErase(false),
unstableInsert(false) {
}
enum class Action {
@ -221,6 +223,8 @@ public:
bool stdAssociativeLike;
bool opLessAllowed;
bool hasInitializerListConstructor;
bool unstableErase;
bool unstableInsert;
Action getAction(const std::string& function) const {
const std::map<std::string, Function>::const_iterator i = functions.find(function);