From f9ae0a4da82ee3c92b2740a2c22bbe28a47391bf Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Tue, 7 Nov 2023 09:46:46 +0200 Subject: [PATCH] Refactor %__file_lineno management into an auxiliary macro Now that we can, just define __file_lineno as an auxiliary macro that only does any work in the rare case where an error or warning occurred. This saves an enormous amount of huffing and puffing defining and undefining macros that are not used at all in the normal paths, on every rpm startup and spec parse. Technically we could use a common macro function for both but as they're in separate libraries, this doesn't seem worth the few lines of saving. --- build/parseSpec.c | 25 ++++++++++++++----------- rpmio/macro.c | 18 ++++++++++++++---- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/build/parseSpec.c b/build/parseSpec.c index d61c477439..69df50c5eb 100644 --- a/build/parseSpec.c +++ b/build/parseSpec.c @@ -129,6 +129,17 @@ int handleComments(char *s) return 0; } +static void ofilineMacro(rpmMacroBuf mb, + rpmMacroEntry me, ARGV_t margs, size_t *parsed) +{ + OFI_t *ofi = rpmMacroEntryPriv(me); + if (ofi) { + char lnobuf[16]; + snprintf(lnobuf, sizeof(lnobuf), "%d", ofi->lineNum); + rpmMacroBufAppendStr(mb, lnobuf); + } +} + /* Push a file to spec's file stack, return the newly pushed entry */ static OFI_t * pushOFI(rpmSpec spec, const char *fn) { @@ -144,6 +155,7 @@ static OFI_t * pushOFI(rpmSpec spec, const char *fn) ofi->next = spec->fileStack; rpmPushMacroFlags(spec->macros, "__file_name", NULL, fn, RMIL_SPEC, RPMMACRO_LITERAL); + rpmPushMacroAux(spec->macros, "__file_lineno", NULL, ofilineMacro, ofi, -1, 0, 0); spec->fileStack = ofi; return spec->fileStack; @@ -162,6 +174,7 @@ static OFI_t * popOFI(rpmSpec spec) free(ofi->readBuf); free(ofi); rpmPopMacro(spec->macros, "__file_name"); + rpmPopMacro(spec->macros, "__file_lineno"); } return spec->fileStack; } @@ -197,17 +210,7 @@ static parsedSpecLine parseLineType(char *line) int specExpand(rpmSpec spec, int lineno, const char *sbuf, char **obuf) { - char lnobuf[16]; - int rc; - - snprintf(lnobuf, sizeof(lnobuf), "%d", lineno); - rpmPushMacroFlags(spec->macros, "__file_lineno", NULL, lnobuf, RMIL_SPEC, RPMMACRO_LITERAL); - - rc = (rpmExpandMacros(spec->macros, sbuf, obuf, 0) < 0); - - rpmPopMacro(spec->macros, "__file_lineno"); - - return rc; + return (rpmExpandMacros(spec->macros, sbuf, obuf, 0) < 0); } static int expandMacrosInSpecBuf(rpmSpec spec, int strip) diff --git a/rpmio/macro.c b/rpmio/macro.c index 046abdef8b..12fde275ec 100644 --- a/rpmio/macro.c +++ b/rpmio/macro.c @@ -1837,6 +1837,17 @@ static int defineMacro(rpmMacroContext mc, const char * macro, int level) return rc; } +static void linenoMacro(rpmMacroBuf mb, + rpmMacroEntry me, ARGV_t margs, size_t *parsed) +{ + int *lineno = rpmMacroEntryPriv(me); + if (lineno) { + char lnobuf[16]; + snprintf(lnobuf, sizeof(lnobuf), "%d", *lineno); + rpmMacroBufAppendStr(mb, lnobuf); + } +} + static int loadMacroFile(rpmMacroContext mc, const char * fn) { FILE *fd = fopen(fn, "r"); @@ -1851,11 +1862,12 @@ static int loadMacroFile(rpmMacroContext mc, const char * fn) goto exit; pushMacro(mc, "__file_name", NULL, fn, RMIL_MACROFILES, ME_LITERAL); + pushMacroAny(mc, "__file_lineno", NULL, "", linenoMacro, &lineno, 0, + RMIL_MACROFILES, ME_FUNC); buf[0] = '\0'; while ((nlines = rdcl(buf, blen, fd)) > 0) { char c, *n; - char lnobuf[16]; lineno += nlines; n = buf; @@ -1865,14 +1877,12 @@ static int loadMacroFile(rpmMacroContext mc, const char * fn) continue; n++; /* skip % */ - snprintf(lnobuf, sizeof(lnobuf), "%d", lineno); - pushMacro(mc, "__file_lineno", NULL, lnobuf, RMIL_MACROFILES, ME_LITERAL); if (defineMacro(mc, n, RMIL_MACROFILES)) nfailed++; - popMacro(mc, "__file_lineno"); } fclose(fd); popMacro(mc, "__file_name"); + popMacro(mc, "__file_lineno"); rc = (nfailed > 0);