cppcheck icon indicating copy to clipboard operation
cppcheck copied to clipboard

Macro ITERATE_TOKENS to simplify token for-loops

Open umanamente opened this issue 3 years ago • 2 comments

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)) { ... }

umanamente avatar Aug 05 '22 22:08 umanamente

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

danmar avatar Aug 08 '22 18:08 danmar

The original code is not very unreadable imho just more verbose.

I agree.

pfultz2 avatar Aug 09 '22 22:08 pfultz2

sorry but I close this PR.

danmar avatar Aug 21 '22 15:08 danmar