Skip to content

Commit f72486f

Browse files
committed
Cleanup
1 parent 255e8a3 commit f72486f

7 files changed

Lines changed: 245 additions & 254 deletions

‎.selfcheck_suppressions‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ autoNoType:tools/triage/mainwindow.cpp
6161
templateInstantiation:test/testutils.cpp
6262
# the "calculate<T, T>(...)" call in the 1-arg overload cannot be instantiated by the template simplifier
6363
templateInstantiation:lib/calculate.h
64-
# used in a member initializer - the check does not see usage there
65-
unusedPrivateFunction:test/testsimplifytemplate.cpp
6664

6765
naming-varname:externals/simplecpp/simplecpp.h
6866
naming-privateMemberVariable:externals/simplecpp/simplecpp.h

‎lib/symboldatabase.cpp‎

Lines changed: 79 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,57 @@
5858

5959
static const Type* findVariableTypeInBase(const Scope* scope, const Token* typeTok);
6060

61+
namespace {
62+
// An access specifier in a class body: "private:", "__published:", "public slots:", ..
63+
struct AccessSpecifier {
64+
AccessControl access = AccessControl::Public;
65+
const Token* last = nullptr; // the last token of the specifier, nullptr when there is none
66+
};
67+
}
68+
69+
// Parse the access specifier that starts at tok. The result has no last token when tok
70+
// does not start an access specifier.
71+
static AccessSpecifier parseAccessSpecifier(const Token* tok)
72+
{
73+
AccessSpecifier specifier;
74+
if (tok->str() == "private:") {
75+
specifier.access = AccessControl::Private;
76+
specifier.last = tok;
77+
} else if (tok->str() == "protected:") {
78+
specifier.access = AccessControl::Protected;
79+
specifier.last = tok;
80+
} else if (tok->str() == "public:" || tok->str() == "__published:") {
81+
specifier.access = AccessControl::Public;
82+
specifier.last = tok;
83+
} else if (Token::Match(tok, "public|protected|private %name% :")) {
84+
if (tok->str() == "private")
85+
specifier.access = AccessControl::Private;
86+
else if (tok->str() == "protected")
87+
specifier.access = AccessControl::Protected;
88+
else
89+
specifier.access = AccessControl::Public;
90+
specifier.last = tok->tokAt(2);
91+
}
92+
return specifier;
93+
}
94+
95+
// insert the addresses of the elements of a container into a set of pointers
96+
template<class Container, class Set>
97+
static void insertPointers(const Container& container, Set& set)
98+
{
99+
for (const auto& element : container)
100+
set.insert(&element);
101+
}
102+
103+
// erase the elements of a sequence container of pointers that are in the set
104+
template<class Container, class Set>
105+
static void erasePointersIn(Container& container, const Set& set)
106+
{
107+
container.erase(std::remove_if(container.begin(), container.end(), [&](const typename Container::value_type& p) {
108+
return set.count(p) != 0;
109+
}), container.end());
110+
}
111+
61112
SymbolDatabase::SymbolDatabase(Tokenizer& tokenizer)
62113
: mTokenizer(tokenizer)
63114
, mSettings(tokenizer.getSettings())
@@ -589,21 +640,10 @@ void SymbolDatabase::findAllScopes(const Token* startToken,
589640
const Token *declEnd = nullptr;
590641

591642
// What section are we in..
592-
if (tok->str() == "private:")
593-
access[scope] = AccessControl::Private;
594-
else if (tok->str() == "protected:")
595-
access[scope] = AccessControl::Protected;
596-
else if (tok->str() == "public:" || tok->str() == "__published:")
597-
access[scope] = AccessControl::Public;
598-
else if (Token::Match(tok, "public|protected|private %name% :")) {
599-
if (tok->str() == "private")
600-
access[scope] = AccessControl::Private;
601-
else if (tok->str() == "protected")
602-
access[scope] = AccessControl::Protected;
603-
else
604-
access[scope] = AccessControl::Public;
605-
606-
tok = tok->tokAt(2);
643+
const AccessSpecifier accessSpecifier = parseAccessSpecifier(tok);
644+
if (accessSpecifier.last) {
645+
access[scope] = accessSpecifier.access;
646+
tok = accessSpecifier.last;
607647
}
608648

609649
// class function?
@@ -1890,18 +1930,8 @@ void SymbolDatabase::removeSymbolsForTokens(const std::unordered_set<const Token
18901930
if ((scope.classDef && removedTokens.count(scope.classDef) != 0) ||
18911931
(scope.bodyStart && removedTokens.count(scope.bodyStart) != 0)) {
18921932
removedScopes.insert(&scope);
1893-
std::transform(scope.varlist.cbegin(),
1894-
scope.varlist.cend(),
1895-
std::inserter(removedVariables, removedVariables.end()),
1896-
[](const Variable& var) {
1897-
return &var;
1898-
});
1899-
std::transform(scope.enumeratorList.cbegin(),
1900-
scope.enumeratorList.cend(),
1901-
std::inserter(removedEnumerators, removedEnumerators.end()),
1902-
[](const Enumerator& enumerator) {
1903-
return &enumerator;
1904-
});
1933+
insertPointers(scope.varlist, removedVariables);
1934+
insertPointers(scope.enumeratorList, removedEnumerators);
19051935
}
19061936
}
19071937
std::unordered_set<const Function*> removedFunctions;
@@ -1915,12 +1945,7 @@ void SymbolDatabase::removeSymbolsForTokens(const std::unordered_set<const Token
19151945
const bool definitionRemoved = function.token && removedTokens.count(function.token) != 0;
19161946
if (!declarationRemoved && !definitionRemoved)
19171947
continue;
1918-
std::transform(function.argumentList.cbegin(),
1919-
function.argumentList.cend(),
1920-
std::inserter(removedVariables, removedVariables.end()),
1921-
[](const Variable& arg) {
1922-
return &arg;
1923-
});
1948+
insertPointers(function.argumentList, removedVariables);
19241949
if (declarationRemoved)
19251950
removedFunctions.insert(&function);
19261951
else {
@@ -1964,29 +1989,14 @@ void SymbolDatabase::removeSymbolsForTokens(const std::unordered_set<const Token
19641989
return var && removedVariables.count(var) != 0;
19651990
},
19661991
nullptr);
1967-
functionScopes.erase(std::remove_if(functionScopes.begin(),
1968-
functionScopes.end(),
1969-
[&](const Scope* scope) {
1970-
return removedScopes.count(scope) != 0;
1971-
}),
1972-
functionScopes.end());
1973-
classAndStructScopes.erase(std::remove_if(classAndStructScopes.begin(),
1974-
classAndStructScopes.end(),
1975-
[&](const Scope* scope) {
1976-
return removedScopes.count(scope) != 0;
1977-
}),
1978-
classAndStructScopes.end());
1992+
erasePointersIn(functionScopes, removedScopes);
1993+
erasePointersIn(classAndStructScopes, removedScopes);
19791994

19801995
// remove from the owning scopes
19811996
for (Scope& scope : scopeList) {
19821997
if (removedScopes.count(&scope) != 0)
19831998
continue;
1984-
scope.nestedList.erase(std::remove_if(scope.nestedList.begin(),
1985-
scope.nestedList.end(),
1986-
[&](const Scope* nested) {
1987-
return removedScopes.count(nested) != 0;
1988-
}),
1989-
scope.nestedList.end());
1999+
erasePointersIn(scope.nestedList, removedScopes);
19902000
for (auto it = scope.functionMap.begin(); it != scope.functionMap.end();) {
19912001
if (removedFunctions.count(it->second) != 0)
19922002
it = scope.functionMap.erase(it);
@@ -2040,25 +2050,27 @@ static AccessControl accessControlAt(const Scope& classScope, const Token* tok)
20402050
--depth;
20412051
if (depth != 0)
20422052
continue;
2043-
if (tok2->str() == "private:")
2044-
access = AccessControl::Private;
2045-
else if (tok2->str() == "protected:")
2046-
access = AccessControl::Protected;
2047-
else if (tok2->str() == "public:" || tok2->str() == "__published:")
2048-
access = AccessControl::Public;
2049-
else if (Token::Match(tok2, "public|protected|private %name% :")) {
2050-
if (tok2->str() == "private")
2051-
access = AccessControl::Private;
2052-
else if (tok2->str() == "protected")
2053-
access = AccessControl::Protected;
2054-
else
2055-
access = AccessControl::Public;
2056-
tok2 = tok2->tokAt(2);
2053+
const AccessSpecifier accessSpecifier = parseAccessSpecifier(tok2);
2054+
if (accessSpecifier.last) {
2055+
access = accessSpecifier.access;
2056+
tok2 = accessSpecifier.last;
20572057
}
20582058
}
20592059
return access;
20602060
}
20612061

2062+
const Scope* SymbolDatabase::scopeAfterToken(const Token* tok)
2063+
{
2064+
const Scope* scope = tok->scope();
2065+
if (!scope || !scope->nestedIn)
2066+
return scope;
2067+
// a namespace can consist of multiple blocks and bodyStart/bodyEnd only track the
2068+
// latest block - so for namespaces any closing brace with the namespace scope closes it
2069+
if (tok == scope->bodyEnd || (tok->str() == "}" && scope->type == ScopeType::eNamespace))
2070+
return scope->nestedIn;
2071+
return scope;
2072+
}
2073+
20622074
void SymbolDatabase::addSymbolsForNewTokenRanges(const std::vector<std::pair<Token*, Token*>>& newRanges)
20632075
{
20642076
// the incremental setVarId() can assign new variable ids even when there are no new
@@ -2087,14 +2099,10 @@ void SymbolDatabase::addSymbolsForNewTokenRanges(const std::vector<std::pair<Tok
20872099
const Token* anchor = range.first->previous();
20882100
while (anchor && !anchor->scope())
20892101
anchor = anchor->previous();
2090-
const Scope* enclosing = anchor ? anchor->scope() : &scopeList.front();
2102+
// an anchor that closes the enclosing scope means the new tokens are in its parent
2103+
const Scope* enclosing = anchor ? scopeAfterToken(anchor) : &scopeList.front();
20912104
if (!enclosing)
20922105
continue;
2093-
// an anchor that closes the enclosing scope means the new tokens are in its parent.
2094-
// a namespace can consist of multiple blocks and bodyStart/bodyEnd only track the
2095-
// latest block - so for namespaces any closing brace with the namespace scope closes it.
2096-
if (anchor && anchor->str() == "}" && (anchor == enclosing->bodyEnd || enclosing->type == ScopeType::eNamespace))
2097-
enclosing = enclosing->nestedIn ? enclosing->nestedIn : &scopeList.front();
20982106
// in a class body the access specifier that is in force at the new tokens applies to them
20992107
const AccessControl startAccess =
21002108
enclosing->isClassOrStructOrUnion() ? accessControlAt(*enclosing, range.first) : AccessControl::Public;

‎lib/symboldatabase.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,12 @@ class CPPCHECKLIB SymbolDatabase {
14771477
*/
14781478
void finalize();
14791479

1480+
/**
1481+
* The scope that is in force after the given token: the scope of the token, or the
1482+
* enclosing scope when the token is the closing brace of its scope.
1483+
*/
1484+
static const Scope* scopeAfterToken(const Token* tok);
1485+
14801486
/* returns the opening { if tok points to enum */
14811487
static const Token* isEnumDefinition(const Token* tok);
14821488

0 commit comments

Comments
 (0)