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

Method simplifyAddBracesPair

lib/tokenize.cpp:6916–7015  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6914}
6915
6916Token *Tokenizer::simplifyAddBracesPair(Token *tok, bool commandWithCondition)
6917{
6918 Token * tokCondition=tok->next();
6919 if (!tokCondition) // Missing condition
6920 return tok;
6921
6922 Token *tokAfterCondition=tokCondition;
6923 if (commandWithCondition) {
6924 if (tokCondition->str()=="(")
6925 tokAfterCondition=tokCondition->link();
6926 else
6927 syntaxError(tok); // Bad condition
6928
6929 if (!tokAfterCondition || tokAfterCondition->strAt(1) == "]")
6930 syntaxError(tok); // Bad condition
6931
6932 tokAfterCondition=tokAfterCondition->next();
6933 if (!tokAfterCondition || Token::Match(tokAfterCondition, ")|}|,")) {
6934 // No tokens left where to add braces around
6935 return tok;
6936 }
6937 }
6938 // Skip labels
6939 Token * tokStatement = tokAfterCondition;
6940 while (true) {
6941 if (Token::Match(tokStatement, "%name% :"))
6942 tokStatement = tokStatement->tokAt(2);
6943 else if (tokStatement->str() == "case") {
6944 tokStatement = skipCaseLabel(tokStatement);
6945 if (!tokStatement)
6946 return tok;
6947 if (tokStatement->str() != ":")
6948 syntaxError(tokStatement);
6949 tokStatement = tokStatement->next();
6950 } else
6951 break;
6952 if (!tokStatement)
6953 return tok;
6954 }
6955 Token * tokBracesEnd=nullptr;
6956 if (tokStatement->str() == "{") {
6957 // already surrounded by braces
6958 if (tokStatement != tokAfterCondition) {
6959 // Move the opening brace before labels
6960 Token::move(tokStatement, tokStatement, tokAfterCondition->previous());
6961 }
6962 tokBracesEnd = tokStatement->link();
6963 } else if (Token::simpleMatch(tokStatement, "try {") &&
6964 Token::simpleMatch(tokStatement->linkAt(1), "} catch (")) {
6965 tokAfterCondition->previous()->insertToken("{");
6966 Token * tokOpenBrace = tokAfterCondition->previous();
6967 Token * tokEnd = tokStatement->linkAt(1)->linkAt(2)->linkAt(1);
6968 if (!tokEnd) {
6969 syntaxError(tokStatement);
6970 }
6971 tokEnd->insertToken("}");
6972 Token * tokCloseBrace = tokEnd->next();
6973 tokCloseBrace->column(tokEnd->column());

Callers

nothing calls this directly

Calls 9

skipCaseLabelFunction · 0.85
nextMethod · 0.80
linkAtMethod · 0.80
insertTokenMethod · 0.80
columnMethod · 0.80
isUpperCaseNameMethod · 0.80
simpleMatchFunction · 0.70
strMethod · 0.45
tokAtMethod · 0.45

Tested by

no test coverage detected