Skip to content

Commit

Permalink
optimized Library::detectContainerInternal() a bit (#5333)
Browse files Browse the repository at this point in the history
Scanning `cli/filelister.cpp` with `DISABLE_VALUEFLOW=1` and
`--enable=all -Ilib -D__GNUC__`

Clang 15 `111,300,996` -> `106,883,955`
GCC 13 `110,555,879` -> `105,983,608`
  • Loading branch information
firewave authored Aug 16, 2023
1 parent 8b309a8 commit 6a263ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
42 changes: 25 additions & 17 deletions lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)

const char* const inherits = node->Attribute("inherits");
if (inherits) {
const std::map<std::string, Container>::const_iterator i = containers.find(inherits);
const std::unordered_map<std::string, Container>::const_iterator i = containers.find(inherits);
if (i != containers.end())
container = i->second; // Take values from parent and overwrite them if necessary
else
Expand Down Expand Up @@ -1158,8 +1158,17 @@ bool Library::isScopeNoReturn(const Token *end, std::string *unknownFunc) const
return false;
}

const Library::Container* Library::detectContainerInternal(const Token* typeStart, DetectContainer detect, bool* isIterator, bool withoutStd) const
const Library::Container* Library::detectContainerInternal(const Token* const typeStart, DetectContainer detect, bool* isIterator, bool withoutStd) const
{
const Token* firstLinkedTok = nullptr;
for (const Token* tok = typeStart; tok && !tok->varId(); tok = tok->next()) {
if (!tok->link())
continue;

firstLinkedTok = tok;
break;
}

for (const std::pair<const std::string, Library::Container> & c : containers) {
const Container& container = c.second;
if (container.startPattern.empty())
Expand All @@ -1177,23 +1186,22 @@ const Library::Container* Library::detectContainerInternal(const Token* typeStar
return &container;
}

for (const Token* tok = typeStart; tok && !tok->varId(); tok = tok->next()) {
if (!tok->link())
continue;
if (!firstLinkedTok)
continue;

const bool matchedStartPattern = Token::Match(typeStart, container.startPattern2.c_str() + offset);
const bool matchedStartPattern = Token::Match(typeStart, container.startPattern2.c_str() + offset);
if (!matchedStartPattern)
continue;

if (detect != ContainerOnly && matchedStartPattern && Token::Match(tok->link(), container.itEndPattern.c_str())) {
if (isIterator)
*isIterator = true;
return &container;
}
if (detect != IteratorOnly && matchedStartPattern && Token::Match(tok->link(), container.endPattern.c_str())) {
if (isIterator)
*isIterator = false;
return &container;
}
break;
if (detect != ContainerOnly && Token::Match(firstLinkedTok->link(), container.itEndPattern.c_str())) {
if (isIterator)
*isIterator = true;
return &container;
}
if (detect != IteratorOnly && Token::Match(firstLinkedTok->link(), container.endPattern.c_str())) {
if (isIterator)
*isIterator = false;
return &container;
}
}
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion lib/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class CPPCHECKLIB Library {
static Yield yieldFrom(const std::string& yieldName);
static Action actionFrom(const std::string& actionName);
};
std::map<std::string, Container> containers;
std::unordered_map<std::string, Container> containers;
const Container* detectContainer(const Token* typeStart) const;
const Container* detectIterator(const Token* typeStart) const;
const Container* detectContainerOrIterator(const Token* typeStart, bool* isIterator = nullptr, bool withoutStd = false) const;
Expand Down

0 comments on commit 6a263ba

Please sign in to comment.