Page MenuHome

Code Quality: Replace for loops with LISTBASE_FOREACH
ClosedPublic

Authored by Dalai Felinto (dfelinto) on Apr 3 2020, 2:20 PM.

Details

Summary

The script itself is:

#!/bin/bash

# ListBase for loop cleanup
#
# "Usage: ./listbase_foreach.sh FILE [dryrun]";
#
# Note: It runs clang-format in the files so some harmless
# false positive changes may occur.

if [[ $# -eq 0 ]]; 
    then
	echo "Replace for loops with LISTBASE_FOREACH"
	echo "Usage: ./listbase_foreach.sh FILE [dryrun]";
	exit 1
fi

DRYRUN=0
if [[ $# -gt 1 ]]; then DRYRUN=1; fi

FILE="$1"
TMP_FILE="/tmp/LISTBASE_FOREACH"

if [ ! -f "$FILE" ]; then
	echo "File invalid: \"$FILE\""
	exit 1
fi

# The main regex
< $FILE tr '\n' '\f' |
sed "s/for (\([^;]\+\);[[:space:]]\+\([^;]\+\);[[:space:]]\+\([^;]\+\)) {/for (\1; \2; \3) {/g" |
tr '\f' '\n' |
sed "s/for (\(const \)\?\(\b\w*\b\) \*\(\b\w*\b\) = \(.*\)\.first; \3; \3 = \3->next) {/LISTBASE_FOREACH (\1\2 *, \3, \&\4) {/" |
sed "s/for (\(const \)\?\(\b\w*\b\) \*\(\b\w*\b\) = \(.*\)\->first; \3; \3 = \3->next) {/LISTBASE_FOREACH (\1\2 *, \3, \4) {/" > $TMP_FILE

if [[ $DRYRUN -eq 1 ]]; then
	grep LISTBASE_FOREACH $TMP_FILE
else
	cp $TMP_FILE $FILE
	clang-format -i $FILE
fi

And I ran it with for i in find . -name *.c`; do echo $i; listbase_foreach.sh $i; done`

Diff Detail

Repository
rB Blender
Branch
listbase-foreach (branched from master)
Build Status
Buildable 7446
Build 7446: arc lint + arc unit

Event Timeline

Dalai Felinto (dfelinto) planned changes to this revision.Apr 3 2020, 2:31 PM
Dalai Felinto (dfelinto) retitled this revision from Code Quality: Replace for loops with LISTBASE_FOREACH to Code Quality: Replace for loops with LISTBASE_FOREACH (wip).
Dalai Felinto (dfelinto) edited the summary of this revision. (Show Details)
Dalai Felinto (dfelinto) edited the summary of this revision. (Show Details)
Dalai Felinto (dfelinto) edited the summary of this revision. (Show Details)Apr 3 2020, 2:44 PM
  • Including missing includes
  • Adjustment required

It seems fine.

It's missing case where there is no variable definition inside the for loop, but that's very hard to automate to this extent.

There's also the case of id.next for the datablock lists in Main, but not sure we have a macro for that.

Single build error on windows, smooth sailing sofar , but have not run any tests.

source/blender/render/intern/source/pipeline.c
2103
LISTBASE_FOREACH (ViewLayer *, view_layer, re->view_layers) {

Final updates, build seems to be working fine

Final take, using BLI_listbase.h instead.

Dalai Felinto (dfelinto) retitled this revision from Code Quality: Replace for loops with LISTBASE_FOREACH (wip) to Code Quality: Replace for loops with LISTBASE_FOREACH.Apr 3 2020, 7:14 PM
Dalai Felinto (dfelinto) edited the summary of this revision. (Show Details)
This revision was not accepted when it landed; it landed in state Needs Review.Apr 3 2020, 7:29 PM
This revision was automatically updated to reflect the committed changes.