Fixed #7887 (User function 'search' is wrongly mixed with std function)

This commit is contained in:
Daniel Marjamäki 2018-10-10 17:35:53 +02:00
parent c048bd45f6
commit 7eb5ebe17e
2 changed files with 20 additions and 3 deletions

View File

@ -9463,15 +9463,27 @@ void Tokenizer::simplifyNamespaceStd()
const bool isCPP11 = mSettings->standards.cpp == Standards::CPP11; const bool isCPP11 = mSettings->standards.cpp == Standards::CPP11;
std::set<std::string> userFunctions;
for (const Token* tok = Token::findsimplematch(list.front(), "using namespace std ;"); tok; tok = tok->next()) { for (const Token* tok = Token::findsimplematch(list.front(), "using namespace std ;"); tok; tok = tok->next()) {
bool insert = false; bool insert = false;
if (Token::Match(tok, "enum class|struct| %name%| :|{")) { // Don't replace within enum definitions if (Token::Match(tok, "enum class|struct| %name%| :|{")) { // Don't replace within enum definitions
skipEnumBody(&tok); skipEnumBody(&tok);
} }
if (!Token::Match(tok->previous(), ".|::")) { if (!Token::Match(tok->previous(), ".|::")) {
if (Token::Match(tok, "%name% (") && !isFunctionHead(tok->next(),"{") && stdFunctions.find(tok->str()) != stdFunctions.end()) if (Token::Match(tok, "%name% (")) {
if (isFunctionHead(tok->next(), "{"))
userFunctions.insert(tok->str());
else if (isFunctionHead(tok->next(), ";")) {
const Token *start = tok;
while (Token::Match(start->previous(), "%type%|*|&"))
start = start->previous();
if (start != tok && start->isName() && (!start->previous() || Token::Match(start->previous(), "[;{}]")))
userFunctions.insert(tok->str());
}
if (userFunctions.find(tok->str()) == userFunctions.end() && stdFunctions.find(tok->str()) != stdFunctions.end())
insert = true; insert = true;
else if (Token::Match(tok, "%name% <") && stdTemplates.find(tok->str()) != stdTemplates.end()) } else if (Token::Match(tok, "%name% <") && stdTemplates.find(tok->str()) != stdTemplates.end())
insert = true; insert = true;
else if (tok->isName() && !tok->varId() && !Token::Match(tok->next(), "(|<") && stdTypes.find(tok->str()) != stdTypes.end()) else if (tok->isName() && !tok->varId() && !Token::Match(tok->next(), "(|<") && stdTypes.find(tok->str()) != stdTypes.end())
insert = true; insert = true;

View File

@ -5633,6 +5633,11 @@ private:
"void search() {}"; "void search() {}";
ASSERT_EQUALS("void search ( ) { }", tokenizeAndStringify(code, false)); ASSERT_EQUALS("void search ( ) { }", tokenizeAndStringify(code, false));
code = "using namespace std;\n"
"void search();\n"
"void dostuff() { search(); }";
ASSERT_EQUALS("void search ( ) ;\nvoid dostuff ( ) { search ( ) ; }", tokenizeAndStringify(code, false));
code = "using namespace std;\n" code = "using namespace std;\n"
"void foo() {map(a, b); }"; // That's obviously not std::map<> "void foo() {map(a, b); }"; // That's obviously not std::map<>
ASSERT_EQUALS("void foo ( ) { map ( a , b ) ; }", tokenizeAndStringify(code, false)); ASSERT_EQUALS("void foo ( ) { map ( a , b ) ; }", tokenizeAndStringify(code, false));