From 342a18da8f466030405312ac81b49dde462d4df2 Mon Sep 17 00:00:00 2001 From: m0_56903617 Date: Sat, 29 May 2021 17:25:30 +0800 Subject: [PATCH] 202105291721 error corrected --- preprocess/include/filestack.h | 23 ++++++++++++++++++++++- preprocess/src/verilog_preprocess.c | 29 +++++++++++++---------------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/preprocess/include/filestack.h b/preprocess/include/filestack.h index 8493745..42cd2bf 100644 --- a/preprocess/include/filestack.h +++ b/preprocess/include/filestack.h @@ -35,6 +35,8 @@ */ #define BUFLEN 2048 +#define MAX_UNGET 4 + typedef struct _sfilestack { struct _sfilestack * pNext; struct _sfilestack * pLast; @@ -46,7 +48,9 @@ typedef struct _sfilestack { int filepos; char *buf; - int ungetch; + int ungetchwr; + int ungetchrd; + int ungetchs[MAX_UNGET]; int bufindex; int buflen; int isbufonly; @@ -73,6 +77,23 @@ static void filestack_destroy(filestack * stack) stack->pNext = stack->pLast = stack; } +static int filestack_unget(filestack* stack, int ch) +{ + stack->ungetchs[stack->ungetchwr] = ch; + stack->ungetchwr = (stack->ungetchwr + 1) % MAX_UNGET; + return 0; +} + +static int filestack_get_unget(filestack* stack) +{ + int ch; + if (stack->ungetchrd == stack->ungetchwr) + return -1; + ch = stack->ungetchs[stack->ungetchrd]; + stack->ungetchrd = (stack->ungetchrd + 1) % MAX_UNGET; + return ch; +} + static filestack* filestack_push_file(filestack * stack, const char * filename) { filestack * pitem; diff --git a/preprocess/src/verilog_preprocess.c b/preprocess/src/verilog_preprocess.c index 9922834..c484fc4 100644 --- a/preprocess/src/verilog_preprocess.c +++ b/preprocess/src/verilog_preprocess.c @@ -32,6 +32,7 @@ /* 修改记录: 202105140816: 根据git的要求增加License + 202105291720: correct an error on get ch from a filestack */ #include "stdio.h" @@ -642,9 +643,7 @@ static int verilog_preprocess_GetCh(sVerilogPreprocess * pPreprocess) ch = 0; goto returnch; } - if (curfile->ungetch != 0) { - ch = curfile->ungetch; - curfile->ungetch = 0; + if ((ch = filestack_get_unget(curfile)) > 0) { goto returnch; } /* 找到一个有字符的缓冲区 */ @@ -685,6 +684,9 @@ static int verilog_preprocess_GetCh(sVerilogPreprocess * pPreprocess) } } while (curfile->bufindex < curfile->buflen) { + if ((ch = filestack_get_unget(curfile)) > 0) { + goto returnch; + } ch = curfile->buf[curfile->bufindex]; curfile->bufindex++; /* 滤掉非字符串中的\r字符 */ @@ -744,7 +746,7 @@ static int verilog_preprocess_GetToken(sVerilogPreprocess* pPreprocess, int ch, pPreprocess->tokenstate = TOKEN_NONE; pPreprocess->tokentype = TT_NUMBER; pPreprocess->token[pPreprocess->tokenlen] = 0; - pPreprocess->file_stack_top->ungetch = ch; + filestack_unget(pPreprocess->file_stack_top, ch); *pch = ' '; } } else if (pPreprocess->tokenstate == TOKEN_IN_SIMIDENT) { @@ -753,7 +755,7 @@ static int verilog_preprocess_GetToken(sVerilogPreprocess* pPreprocess, int ch, pPreprocess->token[pPreprocess->tokenlen++] = ch; return NEXT_CONTINUE; } else { /* 完成一个SIMIDENT */ - pPreprocess->file_stack_top->ungetch = ch; + filestack_unget(pPreprocess->file_stack_top, ch); *pch = ' '; pPreprocess->tokenstate = TOKEN_NONE; pPreprocess->tokentype = TT_SIMIDENT; @@ -764,7 +766,7 @@ static int verilog_preprocess_GetToken(sVerilogPreprocess* pPreprocess, int ch, pPreprocess->token[pPreprocess->tokenlen++] = ch; return NEXT_CONTINUE; } else { - pPreprocess->file_stack_top->ungetch = ch; + filestack_unget(pPreprocess->file_stack_top, ch); *pch = ' '; pPreprocess->token[pPreprocess->tokenlen] = 0; pPreprocess->tokenstate = TOKEN_NONE; @@ -810,7 +812,7 @@ static int verilog_preprocess_GetToken(sVerilogPreprocess* pPreprocess, int ch, pPreprocess->lastch = ' '; /* 这个*号不能作为结束注释的开始*号 */ return NEXT_CONTINUE; } else { - pPreprocess->file_stack_top->ungetch = ch; + filestack_unget(pPreprocess->file_stack_top, ch); *pch = '/'; pPreprocess->tokenstate = TOKEN_NONE; pPreprocess->tokentype = TT_SYMBOL; /*token text is in ch*/ @@ -819,7 +821,7 @@ static int verilog_preprocess_GetToken(sVerilogPreprocess* pPreprocess, int ch, } } else if (pPreprocess->tokenstate == TOKEN_IN_COMMENT) { if ( (ch == '/') && (pPreprocess->lastch == '*') ) { - pPreprocess->file_stack_top->ungetch = ' '; + filestack_unget(pPreprocess->file_stack_top, ' '); *pch = ' '; pPreprocess->tokenstate = TOKEN_NONE; pPreprocess->tokentype = TT_NONE; @@ -832,7 +834,7 @@ static int verilog_preprocess_GetToken(sVerilogPreprocess* pPreprocess, int ch, if (ch == '\n') { pPreprocess->tokenstate = TOKEN_NONE; pPreprocess->tokentype = TT_NONE; - pPreprocess->file_stack_top->ungetch = '\n'; + filestack_unget(pPreprocess->file_stack_top, ch); pPreprocess->lastch = ch; return NEXT_CONTINUE; } else { @@ -844,7 +846,7 @@ static int verilog_preprocess_GetToken(sVerilogPreprocess* pPreprocess, int ch, pPreprocess->tokenstate = TOKEN_NONE; pPreprocess->tokentype = TT_MACRO_TEXT; pPreprocess->token[pPreprocess->tokenlen] = 0; - pPreprocess->file_stack_top->ungetch = '\n'; + filestack_unget(pPreprocess->file_stack_top, ch); } else if (ch == '\\') { pPreprocess->lastch = ch; pPreprocess->tokenstate = TOKEN_IN_MACRO_TEXT_SLASH; @@ -1478,12 +1480,8 @@ static int verilog_preprocess_state_macro(sVerilogPreprocess * pPreprocess, int pPreprocess->state = STATE_CD_MACRO_PARAM_TEXT; pPreprocess->tokenlen = 0; DLIST_DESTROY(stringitem, &pPreprocess->macro_param_list); - } else if (isblank(ch) || (ch == '\n') ) { - } else if ( (ch == '/') && (pPreprocess->tokentype != TT_SYMBOL)) { - pPreprocess->tokenstate = TOKEN_COMMENT_START; - pPreprocess->lastch = '/'; } else { - pPreprocess->file_stack_top->ungetch = ch; + filestack_unget(pPreprocess->file_stack_top, ch); verilog_preprocess_preprocess_PreAction(pPreprocess, PA_MACRO, pPreprocess->macro_name, ""); pPreprocess->state = STATE_INITIAL; pPreprocess->tokenstate = TOKEN_NONE; @@ -1491,7 +1489,6 @@ static int verilog_preprocess_state_macro(sVerilogPreprocess * pPreprocess, int } return NEXT_CONTINUE; } else if (pPreprocess->state == STATE_CD_MACRO_PARAM_TEXT) { - pPreprocess->file_stack_top->ungetch = ch; ch = verilog_preprocess_GetMacroParamText(pPreprocess); pPreprocess->token[pPreprocess->tokenlen] = 0; stringlistaddstring(&pPreprocess->macro_param_list, pPreprocess->token); -- GitLab