diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 730f3f380..5ec9b4aac 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1703,9 +1703,6 @@ bool Tokenizer::tokenize(std::istream &code, // remove unnecessary member qualification.. removeUnnecessaryQualification(); - // Add std:: in front of std classes, when using namespace std; was given - simplifyNamespaceStd(); - // remove Microsoft MFC.. simplifyMicrosoftMFC(); @@ -1990,6 +1987,9 @@ bool Tokenizer::tokenize(std::istream &code, setVarId(); } + // Add std:: in front of std classes, when using namespace std; was given + simplifyNamespaceStd(); + createLinks2(); // Change initialisation of variable to assignment @@ -8991,7 +8991,7 @@ void Tokenizer::simplifyNamespaceStd() "exception", "bad_exception", "logic_error", "domain_error", "invalid_argument_", "length_error", "out_of_rage", "runtime_error", "range_error", "overflow_error", "underflow_error", "locale", - "cout", "cerr", "clog", "cin", + "cout", "cerr", "clog", "cin", "endl", "fpos", "streamoff", "streampos", "streamsize" }; static const std::set stdTypes(stdTypes_, stdTypes_+sizeof(stdTypes_)/sizeof(*stdTypes_)); @@ -9019,7 +9019,7 @@ void Tokenizer::simplifyNamespaceStd() insert = true; else if (Token::Match(tok, "%var% <") && !Token::Match(tok->previous(), ".|::") && stdTemplates.find(tok->str()) != stdTemplates.end()) insert = true; - else if (tok->isName() && !Token::Match(tok->next(), "(|<") && !Token::Match(tok->previous(), ".|::") && stdTypes.find(tok->str()) != stdTypes.end()) + else if (tok->isName() && !tok->varId() && !Token::Match(tok->next(), "(|<") && !Token::Match(tok->previous(), ".|::") && stdTypes.find(tok->str()) != stdTypes.end()) insert = true; if (insert) { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 49e148a84..8eb78f075 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -7123,6 +7123,29 @@ private: static const char code10[] = "std::tr1::function f;"; ASSERT_EQUALS("std :: tr1 :: function < void ( int ) > f ;", tokenizeAndStringify(code10, false, true, Settings::Unspecified, "test.cpp", false)); ASSERT_EQUALS("std :: function < void ( int ) > f ;", tokenizeAndStringify(code10, false, true, Settings::Unspecified, "test.cpp", true)); + + // #4042 (Do not add 'std ::' to variables) + static const char code11[] = "using namespace std;\n" + "const char * string = \"Hi\";"; + ASSERT_EQUALS("const char * string ; string = \"Hi\" ;", tokenizeAndStringify(code11, false)); + + static const char code12[] = "using namespace std;\n" + "string f(const char * string) {\n" + " cout << string << endl;\n" + " return string;\n" + "}"; + static const char expected12[] = "std :: string f ( const char * string ) {\n" + "std :: cout << string << std :: endl ;\n" + "return string ;\n" + "}"; + ASSERT_EQUALS(expected12, tokenizeAndStringify(code12, false)); + + static const char code13[] = "using namespace std;\n" + "try { }\n" + "catch(std::exception &exception) { }"; + static const char expected13[] = "try { }\n" + "catch ( std :: exception & exception ) { }"; + ASSERT_EQUALS(expected13, tokenizeAndStringify(code13, false)); } void microsoftMFC() {