JavaCC生成死代码

时间:2017-05-25 17:34:01

标签: java compiler-construction javacc

我第一次使用JavaCC,并且我注意到它会产生大量死代码。有许多线条看起来像(请原谅间距,它是自动的):

{if ("" != null) return result;}
    throw new Error("Missing return statement in function");
  }

是否可以避免生成此死代码?它会导致数十个编译器警告,这些警告是可以避免的。

谢谢!

这里是.jj文件的完整最小示例:

Statement UseStatement():
{
    String dbName;
}
{
    <USE> dbName=DbObjectName()
    {
        return new UseStatement(dbName);
    }
}

生成:

final public Statement UseStatement() throws ParseException {String dbName;
    jj_consume_token(USE);
    dbName = DbObjectName();
{if ("" != null) return new UseStatement(dbName);}
    throw new Error("Missing return statement in function");
  }

此外,JavaCC正在生成一个抛出TokenMgrError的ParserTokenManager文件 - 但代码没有编译。它声明protected int curChar,它应该声明为char。谷歌搜索这个短语显示了许多正确声明为char的例子 - 这是不是很多人手工编辑结果?

1 个答案:

答案 0 :(得分:0)

我去了源码并防止你必须触发Options.isLegacyExceptionHandling的死代码。

  // Add if statement to prevent subsequent code generated
  // from being dead code.
  // NB: eclipse now detects 'if (true)' as dead code, so use the more complicated
  // 'if ("" != null)'
  if (inAction  && (Options.isLegacyExceptionHandling()) ) {
    t.image = "{if (\"\" != null) return";
    jumpPatched = true;
  }

然后触发:

if (p.isJumpPatched() && !voidReturn) {
  if (isJavaDialect) {
    // TODO :: I don't think we need to throw an Error/Exception to mark that a return statement is missing as the compiler will flag this error automatically
    if (Options.isLegacyExceptionHandling()) {
        codeGenerator.genCodeLine("    throw new "+(Options.isLegacyExceptionHandling() ? "Error" : "RuntimeException")+"(\"Missing return statement in function\");");
    }
  } else {
    codeGenerator.genCodeLine("    throw \"Missing return statement in function\";");
  }
}

https://github.com/javacc/javacc/blob/e38cbdb1db7ca8bd66f892859fef88b4876e69ba/src/main/javacc/JavaCC.jj#L2771-L2779

https://github.com/javacc/javacc/blob/2ac628df1f899fdf6acf1f87fad313b6797085f7/src/main/java/org/javacc/parser/ParseEngine.java#L707-L712

传统异常处理是一种派生选项,仅在JAVA_TEMPLATE_TYPE=modern时才为false。正确设置它的唯一方法是将它包含在.jj文件的options块中,如下所示:

options {
    JAVA_TEMPLATE_TYPE="modern";
}

理论上它也可以通过命令行选项进行设置,但在撰写本文时,实际上不可能在解析命令行参数之前设置派生选项(#25