cppcheck
cppcheck copied to clipboard
Macro ITERATE_TOKENS to simplify token for-loops
This pull request is related to #4269, which contained iterator implementation. Implementing a valid C++ iterator brings some complications, so I decided to put that pull request on hold and open this one containing just the macro.
Added macro ITERATE_TOKENS(tok, ...), which generates regular for-begin-end loop:
for (ITERATE_TOKENS(tok, start, end)) { ... }
for (ITERATE_TOKENS(tok, scope)) { ... }
for (ITERATE_TOKENS(tok, mTokenizer)) { ... }
Usage:
Start to end
Before:
for (const Token *tok = start; tok && tok != end; tok = tok->next()) {
After:
for (ITERATE_TOKENS(tok, start, end)) {
Scopes
Before:
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
After:
for (ITERATE_TOKENS(tok, scope)) { ... }
Tokenizer
Before:
for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
After:
for (ITERATE_TOKENS(tok, mTokenizer)) { ... }
I have a bit of second thought about this approach. Using macros is not exactly recommended these days.
The original code is not very unreadable imho just more verbose.
I wonder what the team thinks. @pfultz2 @chrchr-github etc
The original code is not very unreadable imho just more verbose.
I agree.
sorry but I close this PR.