From 7eb5ebe17ee00cd3588f997ee31acfb3f9fc0317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 10 Oct 2018 17:35:53 +0200 Subject: [PATCH] Fixed #7887 (User function 'search' is wrongly mixed with std function) --- lib/tokenize.cpp | 18 +++++++++++++++--- test/testtokenize.cpp | 5 +++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index caea334e5..c6e2c1b88 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9463,15 +9463,27 @@ void Tokenizer::simplifyNamespaceStd() const bool isCPP11 = mSettings->standards.cpp == Standards::CPP11; + std::set userFunctions; + for (const Token* tok = Token::findsimplematch(list.front(), "using namespace std ;"); tok; tok = tok->next()) { bool insert = false; if (Token::Match(tok, "enum class|struct| %name%| :|{")) { // Don't replace within enum definitions skipEnumBody(&tok); } if (!Token::Match(tok->previous(), ".|::")) { - if (Token::Match(tok, "%name% (") && !isFunctionHead(tok->next(),"{") && stdFunctions.find(tok->str()) != stdFunctions.end()) - insert = true; - else if (Token::Match(tok, "%name% <") && stdTemplates.find(tok->str()) != stdTemplates.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; + } else if (Token::Match(tok, "%name% <") && stdTemplates.find(tok->str()) != stdTemplates.end()) insert = true; else if (tok->isName() && !tok->varId() && !Token::Match(tok->next(), "(|<") && stdTypes.find(tok->str()) != stdTypes.end()) insert = true; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 47bf0db7a..04eb9a0cd 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5633,6 +5633,11 @@ private: "void search() {}"; 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" "void foo() {map(a, b); }"; // That's obviously not std::map<> ASSERT_EQUALS("void foo ( ) { map ( a , b ) ; }", tokenizeAndStringify(code, false));