Skip to content

Commit 3d02bfa

Browse files
committed
Use the newly implemented token recording mechanism to record expressions checked in __static_assert and __static_check
1 parent 8970ad2 commit 3d02bfa

File tree

1 file changed

+27
-46
lines changed

1 file changed

+27
-46
lines changed

source/compiler/sc1.c

+27-46
Original file line numberDiff line numberDiff line change
@@ -8548,16 +8548,13 @@ SC_FUNC void pragma_nodestruct(symbol *sym)
85488548
*/
85498549
SC_FUNC cell do_static_check(int use_warning)
85508550
{
8551-
int already_staging,didalloc,optmsg;
8551+
int already_staging,already_recording,optmsg;
85528552
int ident,index;
8553-
int bck_litidx;
8554-
int exprstartfline,exprendfline;
8553+
int bck_litidx,recstartpos;
85558554
cell cidx,val;
85568555
char *str;
8557-
const unsigned char *exprstart,*exprend;
85588556

8559-
str=NULL;
8560-
didalloc=optmsg=FALSE;
8557+
optmsg=FALSE;
85618558
index=0;
85628559
cidx=0;
85638560

@@ -8567,53 +8564,34 @@ SC_FUNC cell do_static_check(int use_warning)
85678564
stgset(TRUE); /* start stage-buffering */
85688565
errorset(sEXPRMARK,0);
85698566
} /* if */
8570-
exprstart=lptr;
8571-
exprstartfline=fline;
8567+
already_recording=pc_isrecording;
8568+
if (!already_recording) {
8569+
recstart();
8570+
recstartpos=0;
8571+
} else {
8572+
recstop(); /* trim out the part of the current line that hasn't been read by lex() yet */
8573+
recstartpos=strlen(pc_recstr);
8574+
recstart(); /* restart recording */
8575+
} /* if */
85728576
ident=expression(&val,NULL,NULL,FALSE);
8577+
if (!already_recording || val==0)
8578+
recstop();
8579+
str=&pc_recstr[recstartpos];
8580+
if (recstartpos!=0 && pc_recstr[recstartpos]==' ')
8581+
str++; /* skip leading whitespace */
85738582
if (ident!=iCONSTEXPR)
85748583
error(8); /* must be constant expression */
8575-
exprend=lptr;
8576-
exprendfline=fline;
85778584
stgdel(index,cidx); /* scratch generated code */
85788585
if (!already_staging) {
85798586
errorset(sEXPRRELEASE,0);
85808587
stgset(FALSE); /* stop stage-buffering */
85818588
} /* if */
85828589

8583-
/* don't bother allocating space and copying the message
8584-
* if the expression is true */
8585-
if (val==0) {
8586-
if (exprstartfline==exprendfline) {
8587-
/* skip leading whitespaces */
8588-
while (*exprstart==' ')
8589-
exprstart++;
8590-
/* strip the trailing ',' or ')'. as well as the whitespaces */
8591-
exprend--;
8592-
if (*exprend==')' || *exprend==',') {
8593-
while (*(exprend-1)==' ')
8594-
exprend--;
8595-
} /* if */
8596-
/* copy the expression string */
8597-
str=malloc((exprend-exprstart+1)*sizeof(char));
8598-
if (str==NULL)
8599-
error(103); /* insufficient memory */
8600-
memcpy(str,exprstart,exprend-exprstart);
8601-
str[exprend-exprstart]='\0';
8602-
didalloc=TRUE;
8603-
} else {
8604-
/* Currently there's no reliable way to capture multiline expressions,
8605-
* as the lexer would only keep the contents of the line the expression
8606-
* ends at, so try to print "-epression-" instead. Not the prefect
8607-
* solution, but at least it's better than not printing anything. */
8608-
str="-expression-";
8609-
} /* if */
8610-
} /* if */
8611-
86128590
/* read the optional message */
86138591
if (matchtoken(',')) {
8614-
if (didalloc) {
8615-
free(str);
8616-
didalloc=FALSE;
8592+
if (!already_recording) {
8593+
free(pc_recstr);
8594+
pc_recstr=NULL;
86178595
} /* if */
86188596
optmsg=TRUE;
86198597
str=parsestringparam(val!=0,&bck_litidx);
@@ -8623,12 +8601,15 @@ SC_FUNC cell do_static_check(int use_warning)
86238601
int errnum=use_warning ? 249 /* check failed */
86248602
: 110; /* assertion failed */
86258603
error(errnum,(str!=NULL) ? str : "");
8626-
if (didalloc)
8627-
free(str);
8628-
else if (optmsg && str!=NULL)
8604+
if (optmsg)
86298605
litidx=bck_litidx; /* remove the string from the literal queue */
8606+
if (already_recording)
8607+
recstart(); /* restart recording */
8608+
} /* if */
8609+
if (!optmsg && !already_recording) {
8610+
free(pc_recstr);
8611+
pc_recstr=NULL;
86308612
} /* if */
8631-
86328613
needtoken(')');
86338614
return !!val;
86348615
}

0 commit comments

Comments
 (0)