MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / simplifyNamespaceStd

Method simplifyNamespaceStd

lib/tokenize.cpp:10363–10414  ·  view source on GitHub ↗

Add std:: in front of std classes, when using namespace std; was given

Source from the content-addressed store, hash-verified

10361
10362// Add std:: in front of std classes, when using namespace std; was given
10363void Tokenizer::simplifyNamespaceStd()
10364{
10365 if (!isCPP())
10366 return;
10367
10368 std::set<std::string> userFunctions;
10369
10370 for (Token* tok = Token::findsimplematch(list.front(), "using namespace std ;"); tok; tok = tok->next()) {
10371 bool insert = false;
10372 if (Token::Match(tok, "enum class|struct| %name%| :|{")) { // Don't replace within enum definitions
10373 skipEnumBody(tok);
10374 }
10375 if (!tok->isName() || tok->isKeyword() || tok->isStandardType() || tok->varId())
10376 continue;
10377 if (Token::Match(tok->previous(), ".|::|namespace"))
10378 continue;
10379 if (Token::simpleMatch(tok->next(), "(")) {
10380 if (TokenList::isFunctionHead(tok->next(), "{"))
10381 userFunctions.insert(tok->str());
10382 else if (TokenList::isFunctionHead(tok->next(), ";")) {
10383 const Token *start = tok;
10384 while (Token::Match(start->previous(), "%type%|*|&"))
10385 start = start->previous();
10386 if (start != tok && start->isName() && !start->isKeyword() && (!start->previous() || Token::Match(start->previous(), "[;{}]")))
10387 userFunctions.insert(tok->str());
10388 }
10389 if ((userFunctions.find(tok->str()) == userFunctions.end() && mSettings.library.matchArguments(tok, "std::" + tok->str())) ||
10390 (tok->tokAt(-1)->isKeyword() && isLibraryType(tok, mSettings.library)))
10391 insert = true;
10392 } else if (Token::simpleMatch(tok->next(), "<") &&
10393 (isStdContainerOrIterator(tok, mSettings.library) || isStdSmartPointer(tok, mSettings.library)))
10394 insert = true;
10395 else if (isLibraryType(tok, mSettings.library))
10396 insert = true;
10397 else if (Token::simpleMatch(tok, "aligned_storage"))
10398 insert = true;
10399
10400 if (insert) {
10401 tok->previous()->insertToken("std");
10402 tok->previous()->linenr(tok->linenr()); // For stylistic reasons we put the std:: in the same line as the following token
10403 tok->previous()->fileIndex(tok->fileIndex());
10404 tok->previous()->insertToken("::");
10405 }
10406 }
10407
10408 for (Token* tok = list.front(); tok; tok = tok->next()) {
10409 if (Token::simpleMatch(tok, "using namespace std ;")) {
10410 Token::eraseTokens(tok, tok->tokAt(4));
10411 tok->deleteThis();
10412 }
10413 }
10414}
10415
10416
10417void Tokenizer::simplifyMicrosoftMemoryFunctions()

Callers

nothing calls this directly

Calls 15

isCPPFunction · 0.85
findsimplematchFunction · 0.85
skipEnumBodyFunction · 0.85
isLibraryTypeFunction · 0.85
isStdContainerOrIteratorFunction · 0.85
isStdSmartPointerFunction · 0.85
frontMethod · 0.80
nextMethod · 0.80
matchArgumentsMethod · 0.80
insertTokenMethod · 0.80
fileIndexMethod · 0.80
deleteThisMethod · 0.80

Tested by

no test coverage detected