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

Method simplifyEmptyNamespaces

lib/tokenize.cpp:6803–6840  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6801//---------------------------------------------------------------------------
6802
6803void Tokenizer::simplifyEmptyNamespaces()
6804{
6805 if (isC())
6806 return;
6807
6808 bool goback = false;
6809 for (Token *tok = list.front(); tok; tok = tok ? tok->next() : nullptr) {
6810 if (goback) {
6811 tok = tok->previous();
6812 goback = false;
6813 }
6814 if (Token::Match(tok, "(|[|{")) {
6815 tok = tok->link();
6816 continue;
6817 }
6818 if (!Token::Match(tok, "namespace %name%| {"))
6819 continue;
6820 const bool isAnonymousNS = tok->strAt(1) == "{";
6821 if (tok->strAt(3 - isAnonymousNS) == "}") {
6822 tok->deleteNext(3 - isAnonymousNS); // remove '%name%| { }'
6823 if (!tok->previous()) {
6824 // remove 'namespace' or replace it with ';' if isolated
6825 tok->deleteThis();
6826 goback = true;
6827 } else { // '%any% namespace %any%'
6828 tok = tok->previous(); // goto previous token
6829 tok->deleteNext(); // remove next token: 'namespace'
6830 if (tok->str() == "{") {
6831 // Go back in case we were within a namespace that's empty now
6832 tok = tok->tokAt(-2) ? tok->tokAt(-2) : tok->previous();
6833 goback = true;
6834 }
6835 }
6836 } else {
6837 tok = tok->tokAt(2 - isAnonymousNS);
6838 }
6839 }
6840}
6841
6842void Tokenizer::removeRedundantSemicolons()
6843{

Callers

nothing calls this directly

Calls 7

frontMethod · 0.80
nextMethod · 0.80
deleteNextMethod · 0.80
deleteThisMethod · 0.80
isCFunction · 0.70
strMethod · 0.45
tokAtMethod · 0.45

Tested by

no test coverage detected