Skip to content

Commit 16ebb90

Browse files
firewavedanmar
authored andcommitted
library.cpp: optimized Library::detectContainer() (danmar#1778)
* library.cpp: optimized Library::detectContainer() reduces Ir from 5882 to 1149 according to callgrind * fixed hang in tests
1 parent 6ab6cfe commit 16ebb90

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

lib/library.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,10 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
381381
}
382382

383383
const char* const startPattern = node->Attribute("startPattern");
384-
if (startPattern)
384+
if (startPattern) {
385385
container.startPattern = startPattern;
386+
container.startPattern2 = container.startPattern + " !!::";
387+
}
386388
const char* const endPattern = node->Attribute("endPattern");
387389
if (endPattern)
388390
container.endPattern = endPattern;
@@ -1012,10 +1014,7 @@ const Library::Container* Library::detectContainer(const Token* typeStart, bool
10121014
if (container.startPattern.empty())
10131015
continue;
10141016

1015-
if (!endsWith(container.startPattern, '<')) {
1016-
if (!Token::Match(typeStart, (container.startPattern + " !!::").c_str()))
1017-
continue;
1018-
} else if (!Token::Match(typeStart, (container.startPattern + " !!::").c_str()))
1017+
if (!Token::Match(typeStart, container.startPattern2.c_str()))
10191018
continue;
10201019

10211020
if (!iterator && container.endPattern.empty()) // If endPattern is undefined, it will always match, but itEndPattern has to be defined.

lib/library.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class CPPCHECKLIB Library {
196196
Action action;
197197
Yield yield;
198198
};
199-
std::string startPattern, endPattern, itEndPattern;
199+
std::string startPattern, startPattern2, endPattern, itEndPattern;
200200
std::map<std::string, Function> functions;
201201
int type_templateArgNo;
202202
int size_templateArgNo;

test/testsymboldatabase.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6113,6 +6113,7 @@ class TestSymbolDatabase: public TestFixture {
61136113
Settings sC;
61146114
Library::Container c;
61156115
c.startPattern = "C";
6116+
c.startPattern2 = "C !!::";
61166117
sC.library.containers["C"] = c;
61176118
ASSERT_EQUALS("container(C) *", typeOf("C*c=new C;","new","test.cpp",&sC));
61186119
ASSERT_EQUALS("container(C) *", typeOf("x=(C*)c;","(","test.cpp",&sC));
@@ -6122,6 +6123,7 @@ class TestSymbolDatabase: public TestFixture {
61226123
Settings set;
61236124
Library::Container vector;
61246125
vector.startPattern = "Vector";
6126+
vector.startPattern2 = "Vector !!::";
61256127
vector.type_templateArgNo = 0;
61266128
vector.arrayLike_indexOp = true;
61276129
set.library.containers["Vector"] = vector;

0 commit comments

Comments
 (0)