Skip to content

Commit

Permalink
issue #10762 Macro define a class name to a different one causes extr…
Browse files Browse the repository at this point in the history
…a whitespace to appear
  • Loading branch information
doxygen committed Apr 1, 2024
1 parent cd5d623 commit d5bc73a
Showing 1 changed file with 52 additions and 31 deletions.
83 changes: 52 additions & 31 deletions src/pre.l
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,9 @@ struct preYY_state
DefineList macroDefinitions;
LinkedMap<PreIncludeInfo> includeRelations;

int lastContext = 0;
bool lexRulesPart = false;
int lastContext = 0;
bool lexRulesPart = false;
char prevChar=0;
};

// stateless functions
Expand Down Expand Up @@ -829,7 +830,7 @@ WSopt [ \t\r]*
QCString result=def->isPredefined && !def->expandAsDefined ?
def->definition :
expandMacro(yyscanner,yytext);
outputString(yyscanner,result.stripWhiteSpace());
outputString(yyscanner,result);
}
else
{
Expand Down Expand Up @@ -2529,8 +2530,7 @@ static void skipCommentMacroName(yyscan_t yyscanner, const QCString &expr, QCStr
*/
static bool replaceFunctionMacro(yyscan_t yyscanner,const QCString &expr,QCString *rest,int pos,int &len,const Define *def,QCString &result,int level)
{
YY_EXTRA_TYPE state = preYYget_extra(yyscanner);
//printf(">replaceFunctionMacro(expr='%s',rest='%s',pos=%d,def='%s') level=%d\n",qPrint(expr),rest ? qPrint(*rest) : 0,pos,qPrint(def->name),state->levelGuard.size());
//printf(">replaceFunctionMacro(expr='%s',rest='%s',pos=%d,def='%s') level=%zu\n",qPrint(expr),rest ? qPrint(*rest) : 0,pos,qPrint(def->name),state->levelGuard.size());
uint32_t j=pos;
len=0;
result.clear();
Expand All @@ -2540,7 +2540,10 @@ static bool replaceFunctionMacro(yyscan_t yyscanner,const QCString &expr,QCStrin

if (cc!='(')
{
unputChar(yyscanner,expr,rest,j,' ');
if (cc!=':') // don't add spaces for colons
{
unputChar(yyscanner,expr,rest,j,' ');
}
return FALSE;
}
getNextChar(yyscanner,expr,rest,j); // eat the '(' character
Expand Down Expand Up @@ -2743,14 +2746,7 @@ static bool replaceFunctionMacro(yyscan_t yyscanner,const QCString &expr,QCStrin
{
resExpr+="@E"; // empty argument will be remove later on
}
else if (state->nospaces)
{
resExpr+=substArg;
}
else
{
resExpr+=" "+substArg+" ";
}
resExpr+=substArg;
}
}
}
Expand All @@ -2770,10 +2766,10 @@ static bool replaceFunctionMacro(yyscan_t yyscanner,const QCString &expr,QCStrin
}
len=j-pos;
result=resExpr;
//printf("<replaceFunctionMacro(expr='%s',rest='%s',pos=%d,def='%s',result='%s') level=%d return=TRUE\n",qPrint(expr),rest ? qPrint(*rest) : 0,pos,qPrint(def->name),qPrint(result),state->levelGuard.size());
//printf("<replaceFunctionMacro(expr='%s',rest='%s',pos=%d,def='%s',result='%s') level=%zu return=TRUE\n",qPrint(expr),rest ? qPrint(*rest) : 0,pos,qPrint(def->name),qPrint(result),state->levelGuard.size());
return TRUE;
}
//printf("<replaceFunctionMacro(expr='%s',rest='%s',pos=%d,def='%s',result='%s') level=%d return=FALSE\n",qPrint(expr),rest ? qPrint(*rest) : 0,pos,qPrint(def->name),qPrint(result),state->levelGuard.size());
//printf("<replaceFunctionMacro(expr='%s',rest='%s',pos=%d,def='%s',result='%s') level=%zu return=FALSE\n",qPrint(expr),rest ? qPrint(*rest) : 0,pos,qPrint(def->name),qPrint(result),state->levelGuard.size());
return FALSE;
}

Expand Down Expand Up @@ -2838,6 +2834,34 @@ static int getNextId(const QCString &expr,int p,int *l)

#define MAX_EXPANSION_DEPTH 50

static void addSeparatorsIfNeeded(yyscan_t yyscanner,const QCString &expr,QCString &resultExpr,QCString &restExpr,int pos)
{
YY_EXTRA_TYPE state = preYYget_extra(yyscanner);
if (!state->nospaces)
{
// peek back in the stream, for a colon character
char ccPrev = pos==0 || (int)expr.length()<pos ? state->prevChar : expr.at(pos-1);
QCString leftSpace = ccPrev!=':' && ccPrev!=' ' ? " " : "";
int ccNext;
restExpr=restExpr.stripWhiteSpace();
if (restExpr.isEmpty()) // peek ahead in the stream for non-whitespace
{
uint32_t j=resultExpr.length();
while ((ccNext=getNextChar(yyscanner,resultExpr,nullptr,j))!=EOF && ccNext==' ') { }
unputChar(yyscanner,resultExpr,nullptr,j,ccNext);
}
else // take first char from remainder
{
ccNext=restExpr.at(0);
}
// don't add whitespace before a colon
QCString rightSpace = ccNext!=':' && ccNext!=' ' ? " " : "";
//printf("ccPrev='%c' ccNext='%c' p=%d expr=%zu restExpr='%s' left='%s' right='%s'\n",
// ccPrev,ccNext,pos,expr.length(),qPrint(restExpr),qPrint(leftSpace),qPrint(rightSpace));
resultExpr=leftSpace+resultExpr+rightSpace;
}
}

/*! performs recursive macro expansion on the string \a expr
* starting at position \a pos.
* May read additional characters from the input while re-scanning!
Expand Down Expand Up @@ -2896,15 +2920,7 @@ static bool expandExpression(yyscan_t yyscanner,QCString &expr,QCString *rest,in
else if (def && def->nargs==-1) // simple macro
{
// substitute the definition of the macro
//printf("macro '%s'->'%s'\n",qPrint(macroName),qPrint(def->definition));
if (state->nospaces)
{
expMacro=def->definition.stripWhiteSpace();
}
else
{
expMacro=" "+def->definition.stripWhiteSpace()+" ";
}
expMacro=def->definition.stripWhiteSpace();
//expMacro=def->definition.stripWhiteSpace();
replaced=TRUE;
len=l;
Expand All @@ -2924,6 +2940,7 @@ static bool expandExpression(yyscan_t yyscanner,QCString &expr,QCString *rest,in
//printf(" replacing '%s'->'%s'\n",qPrint(expr.mid(p,len)),qPrint(expMacro));
QCString resultExpr=expMacro;
QCString restExpr=expr.right(expr.length()-len-p);
addSeparatorsIfNeeded(yyscanner,expr,resultExpr,restExpr,p);
processConcatOperators(resultExpr);
//printf(" macroName=%s restExpr='%s' def->nonRecursive=%d\n",qPrint(macroName),qPrint(restExpr),def->nonRecursive);
bool expanded=false;
Expand All @@ -2939,12 +2956,13 @@ static bool expandExpression(yyscan_t yyscanner,QCString &expr,QCString *rest,in
}
if (expanded)
{
//printf("expanded '%s' + '%s' + '%s'\n",qPrint(expr.left(p)),qPrint(resultExpr),qPrint(restExpr));
expr=expr.left(p)+resultExpr+restExpr;
//printf(" new expression: '%s' old i=%d new i=%d\n",qPrint(expr),i,p);
i=p;
}
else
{
//printf("not expanded '%s' + @- '%s'\n",qPrint(expr.left(p)),qPrint(expr.right(expr.length()-p)));
expr=expr.left(p)+"@-"+expr.right(expr.length()-p);
i=p+l+2;
}
Expand Down Expand Up @@ -3307,11 +3325,14 @@ static bool computeExpression(yyscan_t yyscanner,const QCString &expr)

static QCString expandMacro(yyscan_t yyscanner,const QCString &name)
{
struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
YY_EXTRA_TYPE state = preYYget_extra(yyscanner);
state->prevChar = yyscanner->yytext_r > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ? *(yyscanner->yytext_r-1) : 0;
QCString n=name;
state->expanded.clear();
expandExpression(yyscanner,n,0,0,0);
n=removeMarkers(n);
state->prevChar=0;
//printf("expandMacro '%s'->'%s'\n",qPrint(name),qPrint(n));
return n;
}
Expand Down Expand Up @@ -3704,20 +3725,20 @@ static int getNextChar(yyscan_t yyscanner,const QCString &expr,QCString *rest,ui
//printf("getNextChar(%s,%s,%d)\n",qPrint(expr),rest ? rest->data() : 0,pos);
if (pos<expr.length())
{
//printf("%c=expr()\n",expr.at(pos));
//printf(" expr()='%c'\n",expr.at(pos));
return expr.at(pos++);
}
else if (rest && !rest->isEmpty())
{
int cc=rest->at(0);
*rest=rest->right(rest->length()-1);
//printf("%c=rest\n",cc);
//printf(" rest='%c'\n",cc);
return cc;
}
else
{
int cc=yyinput(yyscanner);
//printf("%d=yyinput() %d\n",cc,EOF);
//printf(" yyinput()='%c' %d\n",cc,EOF);
return cc;
}
}
Expand Down Expand Up @@ -3754,13 +3775,13 @@ static void unputChar(yyscan_t yyscanner,const QCString &expr,QCString *rest,uin
}
else if (rest)
{
//printf("Prepending to rest!\n");
//printf(" prepending '%c' to rest!\n",c);
char cs[2];cs[0]=c;cs[1]='\0';
rest->prepend(cs);
}
else
{
//unput(c);
//printf(" yyunput()='%c'\n",c);
returnCharToStream(yyscanner,c);
}
//printf("result: unputChar(%s,%s,%d,%c)\n",qPrint(expr),rest ? rest->data() : 0,pos,c);
Expand Down

0 comments on commit d5bc73a

Please sign in to comment.