Skip to content

Commit

Permalink
Correct line count and handling of inline comments for macro definitions
Browse files Browse the repository at this point in the history
Based on the proposed pull request #8656, the handling of whitespace has been adjusted so that comments stay aligned (when they were aligned).

Furthermore a small problem in the line counting (rule: `<CopyCComment>\n`) was corrected
  • Loading branch information
albert-github committed Jul 13, 2021
1 parent 60732c5 commit 7896864
Showing 1 changed file with 61 additions and 17 deletions.
78 changes: 61 additions & 17 deletions src/pre.l
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ struct preYY_state
QCString defLitText;
QCString defArgsStr;
QCString defExtraSpacing;
bool defContinue = false;
bool defVarArgs = false;
int lastCContext = 0;
int lastCPPContext = 0;
Expand All @@ -278,6 +279,7 @@ struct preYY_state

bool macroExpansion = false; // from the configuration
bool expandOnlyPredef = false; // from the configuration
QCString potentialDefine;
int commentCount = 0;
bool insideComment = false;
bool isImported = false;
Expand Down Expand Up @@ -315,6 +317,8 @@ static char resolveTrigraph(char c);
static inline void outputArray(yyscan_t yyscanner,const char *a,yy_size_t len);
static inline void outputString(yyscan_t yyscanner,const QCString &s);
static inline void outputChar(yyscan_t yyscanner,char c);
static inline void outputSpaces(yyscan_t yyscanner,char *s);
static inline void extraSpacing(yyscan_t yyscanner);
static QCString expandMacro(yyscan_t yyscanner,const QCString &name);
static void readIncludeFile(yyscan_t yyscanner,const QCString &inc);
static void incrLevel(yyscan_t yyscanner);
Expand Down Expand Up @@ -409,7 +413,7 @@ WSopt [ \t\r]*
<*>"??"[=/'()!<>-] { // Trigraph
unput(resolveTrigraph(yytext[2]));
}
<Start>^{B}*"#" { BEGIN(Command); yyextra->yyColNr+=(int)yyleng; yyextra->yyMLines=0;}
<Start>^{B}*"#" { BEGIN(Command); yyextra->yyColNr+=(int)yyleng; yyextra->yyMLines=0; yyextra->potentialDefine = yytext;}
<Start>^("%top{"|"%{") {
if (getLanguageFromFileName(yyextra->yyFileName)!=SrcLangExt_Lex) REJECT
outputArray(yyscanner,yytext,yyleng);
Expand Down Expand Up @@ -739,6 +743,7 @@ WSopt [ \t\r]*
BEGIN(Include);
}
<Command>("cmake")?"define"{B}+ {
yyextra->potentialDefine += substitute(yytext,"cmake"," ");
//printf("!!!DefName\n");
yyextra->yyColNr+=(int)yyleng;
BEGIN(DefName);
Expand Down Expand Up @@ -822,7 +827,8 @@ WSopt [ \t\r]*
yyextra->yyLineNr++;
}
<IgnoreLine>.
<Command>. {yyextra->yyColNr+=(int)yyleng;}
<Command>\t { yyextra->potentialDefine += '\t';yyextra->yyColNr+=(int)yyleng;}
<Command>. { yyextra->potentialDefine += ' ';yyextra->yyColNr+=(int)yyleng;}
<UndefName>{ID} {
Define *def;
if ((def=isDefined(yyscanner,yytext))
Expand Down Expand Up @@ -1013,6 +1019,7 @@ WSopt [ \t\r]*
yyextra->defName = yytext;
yyextra->defVarArgs = FALSE;
yyextra->defExtraSpacing.resize(0);
yyextra->defContinue = false;
BEGIN(DefineArg);
}
<DefName>{ID}{B}+"1"/[ \r\t\n] { // special case: define with 1 -> can be "guard"
Expand All @@ -1027,7 +1034,8 @@ WSopt [ \t\r]*
// qPrint(yyextra->defName),qPrint(yyextra->lastGuardName),yyextra->expectGuard);
if (yyextra->curlyCount>0 || yyextra->defName!=yyextra->lastGuardName || !yyextra->expectGuard)
{ // define may appear in the output
QCString tmp=(QCString)"#define "+yyextra->defName;
QCString tmp=yyextra->potentialDefine+yyextra->defName+
substitute(QCString(yytext).mid(yyextra->defName.length()),"1"," ");
outputString(yyscanner,tmp);
yyextra->quoteArg=FALSE;
yyextra->insideComment=FALSE;
Expand Down Expand Up @@ -1057,7 +1065,7 @@ WSopt [ \t\r]*
// qPrint(yyextra->defName),qPrint(yyextra->lastGuardName),yyextra->expectGuard);
if (yyextra->curlyCount>0 || yyextra->defName!=yyextra->lastGuardName || !yyextra->expectGuard)
{ // define may appear in the output
QCString tmp=(QCString)"#define "+yyextra->defName;
QCString tmp=yyextra->potentialDefine+yyextra->defName;
outputString(yyscanner,tmp);
yyextra->quoteArg=FALSE;
yyextra->insideComment=FALSE;
Expand All @@ -1082,21 +1090,24 @@ WSopt [ \t\r]*
yyextra->defLitText.resize(0);
yyextra->defName = yytext;
yyextra->defVarArgs = FALSE;
QCString tmp=(QCString)"#define "+yyextra->defName+yyextra->defArgsStr;
QCString tmp=yyextra->potentialDefine+yyextra->defName+yyextra->defArgsStr;
outputString(yyscanner,tmp);
yyextra->quoteArg=FALSE;
yyextra->insideComment=FALSE;
BEGIN(DefineText);
}
<DefineArg>"\\\n" {
yyextra->defExtraSpacing+="\n";
yyextra->defContinue = true;
yyextra->yyLineNr++;
}
<DefineArg>{B}* { yyextra->defExtraSpacing+=yytext;}
<DefineArg>","{B}* { yyextra->defArgsStr+=yytext; }
<DefineArg>"("{B}* { yyextra->defArgsStr+=yytext; }
<DefineArg>{B}*")"{B}* {
extraSpacing(yyscanner);
yyextra->defArgsStr+=yytext;
QCString tmp=(QCString)"#define "+yyextra->defName+yyextra->defArgsStr+yyextra->defExtraSpacing;
QCString tmp=yyextra->potentialDefine+yyextra->defName+yyextra->defArgsStr+yyextra->defExtraSpacing;
outputString(yyscanner,tmp);
yyextra->quoteArg=FALSE;
yyextra->insideComment=FALSE;
Expand All @@ -1120,6 +1131,7 @@ WSopt [ \t\r]*
yyextra->defArgsStr+=yytext;
yyextra->argMap.emplace(toStdString(argName),yyextra->defArgs);
yyextra->defArgs++;
extraSpacing(yyscanner);
}
/*
<DefineText>"/ **"|"/ *!" {
Expand Down Expand Up @@ -1376,7 +1388,6 @@ WSopt [ \t\r]*
yyextra->yyLineNr++;
yyextra->defLitText+=yytext;
yyextra->defText+=' ';
outputChar(yyscanner,'\n');
}
<RemoveCComment>{CCE}{B}*"#" { // see bug 594021 for a usecase for this rule
if (yyextra->lastCContext==SkipCPPBlock)
Expand Down Expand Up @@ -1418,11 +1429,13 @@ WSopt [ \t\r]*
<RemoveCPPComment>[^\x06\n]+
<RemoveCPPComment>.
<DefineText>"#" {
outputChar(yyscanner,' ');
yyextra->quoteArg=TRUE;
yyextra->defLitText+=yytext;
}
<DefineText,CopyCComment>{ID} {
yyextra->defLitText+=yytext;
if (YY_START == DefineText) outputSpaces(yyscanner,yytext);
if (yyextra->quoteArg)
{
yyextra->defText+="\"";
Expand Down Expand Up @@ -1509,40 +1522,47 @@ WSopt [ \t\r]*
yyextra->lastGuardName.resize(0);
BEGIN(Start);
}
<DefineText>{B}* { yyextra->defText += ' '; yyextra->defLitText+=yytext; }
<DefineText>{B}*"##"{B}* { yyextra->defText += "##"; yyextra->defLitText+=yytext; }
<DefineText>"@" { yyextra->defText += "@@"; yyextra->defLitText+=yytext; }
<DefineText>{B}* { outputString(yyscanner,yytext);yyextra->defText += ' '; yyextra->defLitText+=yytext; }
<DefineText>{B}*"##"{B}* { outputString(yyscanner,substitute(yytext,"##"," "));yyextra->defText += "##"; yyextra->defLitText+=yytext; }
<DefineText>"@" { outputString(yyscanner,substitute(yytext,"@@"," "));yyextra->defText += "@@"; yyextra->defLitText+=yytext; }
<DefineText>\" {
outputChar(yyscanner,' ');
yyextra->defText += *yytext;
yyextra->defLitText+=yytext;
if (!yyextra->insideComment)
{
BEGIN(SkipDoubleQuote);
}
}
<DefineText>\' { yyextra->defText += *yytext;
<DefineText>\' {
outputChar(yyscanner,' ');
yyextra->defText += *yytext;
yyextra->defLitText+=yytext;
if (!yyextra->insideComment)
{
BEGIN(SkipSingleQuote);
}
}
<SkipDoubleQuote>{CPPC}[/]? { yyextra->defText += yytext; yyextra->defLitText+=yytext; }
<SkipDoubleQuote>{CCS}[*]? { yyextra->defText += yytext; yyextra->defLitText+=yytext; }
<SkipDoubleQuote>{CPPC}[/]? { outputSpaces(yyscanner,yytext);yyextra->defText += yytext; yyextra->defLitText+=yytext; }
<SkipDoubleQuote>{CCS}[*]? { outputSpaces(yyscanner,yytext);yyextra->defText += yytext; yyextra->defLitText+=yytext; }
<SkipDoubleQuote>\" {
outputChar(yyscanner,' ');
yyextra->defText += *yytext; yyextra->defLitText+=yytext;
BEGIN(DefineText);
}
<SkipSingleQuote,SkipDoubleQuote>\\. {
yyextra->defText += yytext; yyextra->defLitText+=yytext;
outputSpaces(yyscanner,yytext);
yyextra->defText += yytext; yyextra->defLitText+=yytext;
}
<SkipSingleQuote>\' {
outputChar(yyscanner,' ');
yyextra->defText += *yytext; yyextra->defLitText+=yytext;
BEGIN(DefineText);
}
<SkipDoubleQuote>. { yyextra->defText += *yytext; yyextra->defLitText+=yytext; }
<SkipSingleQuote>. { yyextra->defText += *yytext; yyextra->defLitText+=yytext; }
<DefineText>. { yyextra->defText += *yytext; yyextra->defLitText+=yytext; }
<SkipDoubleQuote,SkipSingleQuote>\t { outputChar(yyscanner,'\t');yyextra->defText += *yytext; yyextra->defLitText+=yytext; }
<SkipDoubleQuote,SkipSingleQuote>. { outputChar(yyscanner,' ');yyextra->defText += *yytext; yyextra->defLitText+=yytext; }
<DefineText>\t { outputChar(yyscanner,'\t');yyextra->defText += *yytext; yyextra->defLitText+=yytext; }
<DefineText>. { outputChar(yyscanner,' ');yyextra->defText += *yytext; yyextra->defLitText+=yytext; }
<<EOF>> {
DBG_CTX((stderr,"End of include file\n"));
//printf("Include stack depth=%d\n",yyextra->includeStack.size());
Expand Down Expand Up @@ -2855,6 +2875,30 @@ static inline void outputString(yyscan_t yyscanner,const QCString &a)
if (state->includeStack.empty() || state->curlyCount>0) state->outputBuf->addArray(a.data(),a.length());
}

static inline void outputSpaces(yyscan_t yyscanner,char *s)
{
const char *p=s;
char c;
while ((c=*p++))
{
if (c=='\t') outputChar(yyscanner,'\t');
else outputChar(yyscanner,' ');
}
}

static inline void extraSpacing(yyscan_t yyscanner)
{
struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
if (!yyextra->defContinue) return;
for (unsigned int i = 0; i < yyleng; i++)
{
if (yytext[i] == '\t')
yyextra->defExtraSpacing+='\t';
else
yyextra->defExtraSpacing+=' ';
}
}

static QCString determineAbsoluteIncludeName(const QCString &curFile,const QCString &incFileName)
{
bool searchIncludes = Config_getBool(SEARCH_INCLUDES);
Expand Down

0 comments on commit 7896864

Please sign in to comment.