【问题标题】:warning about format not a string literal and no format arguments while compiling yacc file with gcc使用 gcc 编译 yacc 文件时有关格式不是字符串文字且没有格式参数的警告
【发布时间】:2014-01-06 07:19:14
【问题描述】:

这是我的 yacc 文件的一部分,我已经用 bison 编译过,然后当我想使用 gcc 编译成可执行文件时,收到一些警告

search2:WORD    {fprintf(outFile_p,$1);}
search2:WHITE   {fprintf(outFile_p,$1);}
       |TAB     {fprintf(outFile_p,"\t");}
       |EOL     {counter=counter+1;fprintf(outFile_p,"\n");}
       |LBRAK   {fprintf(outFile_p,$1);}
       |RBRAK   {fprintf(outFile_p,$1);}
       |SIMICOL {fprintf(outFile_p,",");}
       |PERCENT {fprintf(outFile_p,"%%");}
       |PLUS    {fprintf(outFile_p,"+");}
       |ANY     {fprintf(outFile_p,$1);}
       |TCOM    {fprintf(outFile_p,"\"");}
       |MUL     {fprintf(outFile_p,$1);}
       |COM     {fprintf(outFile_p,";");}
       |LB      {fprintf(outFile_p,"[");}
       |RB      {fprintf(outFile_p,"]");}

那些带有“{fprintf(outFile_p,$1);}”的代码当我想使用 gcc 进行编译时,我收到了这个警告

格式不是字符串文字,也没有格式参数

如果有人可以帮我解决这个警告或者我需要发布整个代码............

警告看起来像这样

y.y:509:5: warning: format not a string literal and no format arguments [-Wformat-security]
 search2:WORD    {fprintf(outFile_p,$1);}
     ^
y.y:510:5: warning: format not a string literal and no format arguments [-Wformat-security]
 search2:WHITE   {fprintf(outFile_p,$1);}
     ^
y.y:513:5: warning: format not a string literal and no format arguments [-Wformat-security]
        |LBRAK   {fprintf(outFile_p,$1);}
     ^
y.y:514:5: warning: format not a string literal and no format arguments [-Wformat-security]
        |RBRAK   {fprintf(outFile_p,$1);}
     ^
y.y:518:5: warning: format not a string literal and no format arguments [-Wformat-security]
        |ANY     {fprintf(outFile_p,$1);}
     ^
y.y:520:5: warning: format not a string literal and no format arguments [-Wformat-security]
        |MUL     {fprintf(outFile_p,$1);}
     ^

【问题讨论】:

    标签: gcc


    【解决方案1】:

    以下是 fprintf() 函数的声明。

    int fprintf(FILE *stream, const char *format, ...)
    

    您的代码缺少$1 的格式

    search2:WORD    {fprintf(outFile_p,"%s",$1);}
    

    或者你也可以使用 fputs -

    search2:WORD    {fputs($1,outFile_p);}
    

    【讨论】:

      猜你喜欢
      • 2013-04-07
      • 2010-12-13
      • 2011-05-24
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 2011-08-12
      相关资源
      最近更新 更多