Skip to content

Commit

Permalink
Refactor %__file_lineno management into an auxiliary macro
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pmatilai committed Nov 7, 2023
1 parent a8ec768 commit f9ae0a4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
25 changes: 14 additions & 11 deletions build/parseSpec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 14 additions & 4 deletions rpmio/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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, "<aux>", 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;
Expand All @@ -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);

Expand Down

0 comments on commit f9ae0a4

Please sign in to comment.