@@ -1220,26 +1220,83 @@ static bool isAlternativeAndBitandBitor(const simplecpp::Token* tok)
12201220 return isAlternativeBinaryOp (tok, AND ) || isAlternativeBinaryOp (tok, BITAND ) || isAlternativeBinaryOp (tok, BITOR );
12211221}
12221222
1223+ /* *
1224+ * Keep track of whether the code is in a function body. This is not part of
1225+ * preprocessing, it is used by the cppcheck-specific handling of &= in
1226+ * combineOperators().
1227+ * @param executableScope one entry for each open {, true if it is a function
1228+ * body or a block in a function body
1229+ * @param tok the current token
1230+ * @return true if tok is { or }
1231+ */
1232+ static bool updateExecutableScope (std::stack<bool , std::vector<bool >> &executableScope, const simplecpp::Token *tok)
1233+ {
1234+ if (tok->op == ' {' ) {
1235+ if (executableScope.top ()) {
1236+ executableScope.push (true );
1237+ return true ;
1238+ }
1239+ const simplecpp::Token *prev = tok->previous ;
1240+ while (prev && prev->isOneOf (" ;{}(" ))
1241+ prev = prev->previous ;
1242+ executableScope.push (prev && prev->op == ' )' );
1243+ return true ;
1244+ }
1245+ if (tok->op == ' }' ) {
1246+ if (executableScope.size () > 1 )
1247+ executableScope.pop ();
1248+ return true ;
1249+ }
1250+ return false ;
1251+ }
1252+
1253+ /* *
1254+ * Is tok the & of an unnamed reference parameter with a default value in a
1255+ * function declaration, as in "void f(x&=2)"? Then "&" and "=" should not be
1256+ * combined into "&=". This is a cppcheck-specific heuristic, compilers
1257+ * tokenize "&=" as one operator.
1258+ * @param tok a & token that is directly followed by =
1259+ */
1260+ static bool isAnonymousReferenceParameter (const simplecpp::Token *tok)
1261+ {
1262+ int indentlevel = 0 ;
1263+ const simplecpp::Token *start = tok;
1264+ while (indentlevel >= 0 && start) {
1265+ if (start->op == ' )' )
1266+ ++indentlevel;
1267+ else if (start->op == ' (' )
1268+ --indentlevel;
1269+ else if (start->isOneOf (" ;{}" ))
1270+ break ;
1271+ start = start->previous ;
1272+ }
1273+ if (indentlevel == -1 && start) {
1274+ const simplecpp::Token * const ftok = start;
1275+ bool isFuncDecl = ftok->name ;
1276+ while (isFuncDecl) {
1277+ if (!start->name && start->str () != " ::" && start->op != ' *' && start->op != ' &' )
1278+ isFuncDecl = false ;
1279+ if (!start->previous )
1280+ break ;
1281+ if (start->previous ->isOneOf (" ;{}:" ))
1282+ break ;
1283+ start = start->previous ;
1284+ }
1285+ isFuncDecl &= start != ftok && start->name ;
1286+ if (isFuncDecl) {
1287+ // TODO: we could loop through the parameters here and check if they are correct.
1288+ return true ;
1289+ }
1290+ }
1291+ return false ;
1292+ }
1293+
12231294void simplecpp::TokenList::combineOperators ()
12241295{
12251296 std::stack<bool , std::vector<bool >> executableScope{{false }};
12261297 for (Token *tok = front (); tok; tok = tok->next ) {
1227- if (tok->op == ' {' ) {
1228- if (executableScope.top ()) {
1229- executableScope.push (true );
1230- continue ;
1231- }
1232- const Token *prev = tok->previous ;
1233- while (prev && prev->isOneOf (" ;{}(" ))
1234- prev = prev->previous ;
1235- executableScope.push (prev && prev->op == ' )' );
1298+ if (updateExecutableScope (executableScope, tok))
12361299 continue ;
1237- }
1238- if (tok->op == ' }' ) {
1239- if (executableScope.size () > 1 )
1240- executableScope.pop ();
1241- continue ;
1242- }
12431300
12441301 if (tok->op == ' .' ) {
12451302 // ellipsis ...
@@ -1283,39 +1340,10 @@ void simplecpp::TokenList::combineOperators()
12831340 continue ;
12841341
12851342 if (tok->next ->op == ' =' && tok->isOneOf (" =!<>+-*/%&|^" )) {
1286- if (tok->op == ' &' && !executableScope.top ()) {
1287- // don't combine &= if it is a anonymous reference parameter with default value:
1288- // void f(x&=2)
1289- int indentlevel = 0 ;
1290- const Token *start = tok;
1291- while (indentlevel >= 0 && start) {
1292- if (start->op == ' )' )
1293- ++indentlevel;
1294- else if (start->op == ' (' )
1295- --indentlevel;
1296- else if (start->isOneOf (" ;{}" ))
1297- break ;
1298- start = start->previous ;
1299- }
1300- if (indentlevel == -1 && start) {
1301- const Token * const ftok = start;
1302- bool isFuncDecl = ftok->name ;
1303- while (isFuncDecl) {
1304- if (!start->name && start->str () != " ::" && start->op != ' *' && start->op != ' &' )
1305- isFuncDecl = false ;
1306- if (!start->previous )
1307- break ;
1308- if (start->previous ->isOneOf (" ;{}:" ))
1309- break ;
1310- start = start->previous ;
1311- }
1312- isFuncDecl &= start != ftok && start->name ;
1313- if (isFuncDecl) {
1314- // TODO: we could loop through the parameters here and check if they are correct.
1315- continue ;
1316- }
1317- }
1318- }
1343+ // don't combine &= if it is a anonymous reference parameter with default value:
1344+ // void f(x&=2)
1345+ if (tok->op == ' &' && !executableScope.top () && isAnonymousReferenceParameter (tok))
1346+ continue ;
13191347 tok->setstr (tok->str () + " =" );
13201348 deleteToken (tok->next );
13211349 } else if ((tok->op == ' |' || tok->op == ' &' ) && tok->op == tok->next ->op ) {
0 commit comments