Library: Added <smart-pointer> element

This commit is contained in:
Daniel Marjamäki 2019-04-24 13:06:58 +02:00
parent b59d7e2f35
commit 2513c1499b
6 changed files with 29 additions and 2 deletions

View File

@ -393,6 +393,10 @@
</zeroOrMore>
</element>
<element name="smart-pointer">
<attribute name="class-name"><ref name="DATA-EXTNAME"/></attribute>
</element>
<element name="podtype">
<attribute name="name"><ref name="DATA-EXTNAME"/></attribute>
<optional>

View File

@ -7470,6 +7470,8 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
<type templateParameter="0"/>
</container>
<container id="stdString" startPattern="std :: string|wstring|u16string|u32string" endPattern="" inherits="stdAllString"/>
<smart-pointer class-name="std::shared_ptr"/>
<smart-pointer class-name="std::unique_ptr"/>
<podtype name="char16_t,std::char16_t" sign="u" size="2"/>
<podtype name="char32_t,std::char32_t" sign="u" size="4"/>
<podtype name="int8_t,std::int8_t" sign="s" size="1"/>

View File

@ -1837,8 +1837,8 @@ void CheckClass::checkConst()
const std::string& opName = func.tokenDef->str();
if (opName.compare(8, 5, "const") != 0 && (endsWith(opName,'&') || endsWith(opName,'*')))
continue;
} else if (Token::simpleMatch(func.retDef, "std :: shared_ptr <")) {
// Don't warn if a std::shared_ptr is returned
} else if (mSettings->library.isSmartPointer(func.retDef)) {
// Don't warn if a std::shared_ptr etc is returned
continue;
} else {
// don't warn for unknown types..

View File

@ -488,6 +488,12 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
}
}
else if (nodename == "smart-pointer") {
const char *className = node->Attribute("class-name");
if (className)
smartPointers.insert(className);
}
else if (nodename == "podtype") {
const char * const name = node->Attribute("name");
if (!name)
@ -1302,3 +1308,14 @@ bool Library::isimporter(const std::string& file, const std::string &importer) c
mImporters.find(Path::getFilenameExtensionInLowerCase(file));
return (it != mImporters.end() && it->second.count(importer) > 0);
}
bool Library::isSmartPointer(const Token *tok) const
{
std::string typestr;
while (Token::Match(tok, "%name%|::")) {
typestr += tok->str();
tok = tok->next();
}
return smartPointers.find(typestr) != smartPointers.end();
}

View File

@ -390,6 +390,9 @@ public:
std::set<std::string> returnuninitdata;
std::vector<std::string> defines; // to provide some library defines
std::set<std::string> smartPointers;
bool isSmartPointer(const Token *tok) const;
struct PodType {
unsigned int size;
char sign;

View File

@ -46,6 +46,7 @@ private:
" <alloc init=\"false\">malloc</alloc>\n"
" <dealloc>free</dealloc>\n"
" </memory>\n"
" <smart-pointer class-name=\"std::shared_ptr\"/>\n"
"</def>";
tinyxml2::XMLDocument doc;
doc.Parse(xmldata, sizeof(xmldata));