commit:     605ad0d675a64eb39144122cf284100192cdfea0
Author:     Thomas Bracht Laumann Jespersen <t <AT> laumann <DOT> xyz>
AuthorDate: Thu Apr 21 08:41:14 2022 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Thu Apr 28 15:50:16 2022 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=605ad0d6

ebegin/eend: accept properly nested calls in different functions

Turn the EBEGIN_EEND variable into a stack that tracks which function
called ebegin. Calls to ebegin may be stacked and do not generate a QA
warning if the callers are different functions. Calls to eend then check
that the function name at the top of the stack matches the caller's
function name.

Bug: https://bugs.gentoo.org/839585
Bug: https://bugs.gentoo.org/839588
Signed-off-by: Thomas Bracht Laumann Jespersen <t <AT> laumann.xyz>
Closes: https://github.com/gentoo/portage/pull/824
Signed-off-by: Sam James <sam <AT> gentoo.org>

 bin/isolated-functions.sh | 22 ++++++++++++++++++----
 bin/phase-functions.sh    |  4 +++-
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/bin/isolated-functions.sh b/bin/isolated-functions.sh
index 03983a9fa..912782914 100644
--- a/bin/isolated-functions.sh
+++ b/bin/isolated-functions.sh
@@ -340,9 +340,15 @@ ebegin() {
        LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
        LAST_E_CMD="ebegin"
        if [[ -v EBEGIN_EEND ]] ; then
-               eqawarn "QA Notice: ebegin called, but missing call to eend 
(phase: ${EBUILD_PHASE})"
+               # Already a call to ebegin
+               local prev="${EBEGIN_EEND[-1]}"
+               if [[ "${prev}" == "${FUNCNAME[1]}" ]] ; then
+                       eqawarn "QA Notice: ebegin called in ${prev}, but 
missing call to eend (${FUNCNAME[1]})"
+               fi
+               EBEGIN_EEND+=( "${FUNCNAME[1]}" )
+       else
+               EBEGIN_EEND=( "${FUNCNAME[1]}" )
        fi
-       EBEGIN_EEND=1
        return 0
 }
 
@@ -372,9 +378,17 @@ __eend() {
 eend() {
        [[ -n $1 ]] || eqawarn "QA Notice: eend called without first argument"
        if [[ -v EBEGIN_EEND ]] ; then
-               unset EBEGIN_EEND
+               local caller="${FUNCNAME[1]}"
+               local tos="${EBEGIN_EEND[-1]}"
+               if [[ "${caller}" != "${tos}" ]] ; then
+                       eqawarn "QA Notice: eend (in ${caller}) improperly 
matched with ebegin (called in ${tos})"
+               fi
+               unset EBEGIN_EEND[-1]
+               if [[ ${#EBEGIN_EEND[@]} -eq 0 ]] ; then
+                       unset EBEGIN_EEND
+               fi
        else
-               eqawarn "QA Notice: eend called without preceding ebegin 
(phase: ${EBUILD_PHASE})"
+               eqawarn "QA Notice: eend called without preceding ebegin 
(phase: ${FUNCNAME[1]})"
        fi
        local retval=${1:-0}
        shift

diff --git a/bin/phase-functions.sh b/bin/phase-functions.sh
index bccf88226..6b48c2351 100644
--- a/bin/phase-functions.sh
+++ b/bin/phase-functions.sh
@@ -1089,7 +1089,9 @@ __ebuild_main() {
        esac
 
        if [[ -v EBEGIN_EEND ]] ; then
-               eqawarn "QA Notice: ebegin called, but missing call to eend 
(phase: ${1})"
+               for func in "${EBEGIN_EEND[@]}" ; do
+                       eqawarn "QA Notice: ebegin called in ${func} but 
missing call to eend"
+               done
        fi
 
        # Save the env only for relevant phases.

Reply via email to