From 2513c1499bc1520655b145f4a232056806e1e3a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 24 Apr 2019 13:06:58 +0200 Subject: [PATCH] Library: Added element --- cfg/cppcheck-cfg.rng | 4 ++++ cfg/std.cfg | 2 ++ lib/checkclass.cpp | 4 ++-- lib/library.cpp | 17 +++++++++++++++++ lib/library.h | 3 +++ test/testclass.cpp | 1 + 6 files changed, 29 insertions(+), 2 deletions(-) diff --git a/cfg/cppcheck-cfg.rng b/cfg/cppcheck-cfg.rng index a6dba3351..93787c605 100644 --- a/cfg/cppcheck-cfg.rng +++ b/cfg/cppcheck-cfg.rng @@ -393,6 +393,10 @@ + + + + diff --git a/cfg/std.cfg b/cfg/std.cfg index d4441bc8c..bb7d6ed80 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -7470,6 +7470,8 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun + + diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index b8e04ea23..ba88dab8d 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -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.. diff --git a/lib/library.cpp b/lib/library.cpp index 44c0a636d..c99d15269 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -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(); +} + diff --git a/lib/library.h b/lib/library.h index 224fc819e..cfe5f87ed 100644 --- a/lib/library.h +++ b/lib/library.h @@ -390,6 +390,9 @@ public: std::set returnuninitdata; std::vector defines; // to provide some library defines + std::set smartPointers; + bool isSmartPointer(const Token *tok) const; + struct PodType { unsigned int size; char sign; diff --git a/test/testclass.cpp b/test/testclass.cpp index 0a8dfcba5..65e0c4fb0 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -46,6 +46,7 @@ private: " malloc\n" " free\n" " \n" + " \n" ""; tinyxml2::XMLDocument doc; doc.Parse(xmldata, sizeof(xmldata));