Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a %no_suppress_warnings directive #1027

Merged
merged 6 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ jobs:
with:
patterns: |
-**/LexParse.java:java/local-variable-is-never-read
-**/LexScan.java:java/misleading-indentation
-**/LexScan.java:java/uncaught-number-format-exception
input: sarif-results/java.sarif
output: sarif-results/java.sarif

Expand Down
6 changes: 6 additions & 0 deletions docs/md/lex-specs.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ generated scanner class.

Makes the generated class abstract.

- `%no_suppress_warnings`

Do not generate any `@SuppressWarnings(...)` annotation for the generated
class. This means, you can add your own `@SuppressWarnings(...)` annotation
in the preamble without getting a compiler error on duplicate annotations.

- `%apiprivate`

Makes all generated methods and fields of the class private.
Expand Down
2 changes: 1 addition & 1 deletion javatests/de/jflex/testcase/negation/negation-0.output
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
line: 1 col: 1 match: --aba\u000A--
action [17] { /* all */ }
action [22] { /* all */ }
-1
2 changes: 1 addition & 1 deletion javatests/de/jflex/testcase/negation/negation-1.output
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
line: 1 col: 1 match: --baba--
action [17] { /* all */ }
action [22] { /* all */ }
-1
4 changes: 2 additions & 2 deletions javatests/de/jflex/testcase/nevermatch/expected_sysout.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Constructing NFA : 12 states in NFA
Converting NFA to DFA :
.....

Warning in file "javatests/de/jflex/testcase/nevermatch/never.flex" (line 15):
Warning in file "javatests/de/jflex/testcase/nevermatch/never.flex" (line 20):
Rule can never be matched:
<A> . { /* action */ }

Warning in file "javatests/de/jflex/testcase/nevermatch/never.flex" (line 21):
Warning in file "javatests/de/jflex/testcase/nevermatch/never.flex" (line 26):
Rule can never be matched:
<A> <<EOF>> { /* action 4 */ }
11 states before minimization, 4 states in minimized DFA
Expand Down
18 changes: 18 additions & 0 deletions javatests/de/jflex/testcase/no_suppress_warnings/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2023, Gerwin Klein
# SPDX-License-Identifier: BSD-3-Clause

load("//testsuite:testsuite.bzl", "jflex_testsuite")

jflex_testsuite(
name = "NoSuppressTest",
srcs = [
"NoSuppressTest.java",
],
data = [
"expected_sysout.txt",
"no_suppress.flex",
],
deps = [
"//third_party/com/google/guava",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2023, Gerwin Klein.
* SPDX-License-Identifier: BSD-3-Clause
*/
package de.jflex.testcase.no_suppress_warnings;

import de.jflex.testing.testsuite.JFlexTestRunner;
import de.jflex.testing.testsuite.annotations.TestSpec;
import org.junit.Test;
import org.junit.runner.RunWith;

/** Test nevermatch warning. */
@RunWith(JFlexTestRunner.class)
@TestSpec(lex = "javatests/de/jflex/testcase/no_suppress_warnings/no_suppress.flex", quiet = true)
public class NoSuppressTest {

/** Tests that the scanner was successfully generated and can be instantiated. */
@Test
public void ok() throws Exception {}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2023, Gerwin Klein <[email protected]>
* SPDX-License-Identifier: BSD-3-Clause
*/

package de.jflex.testcase.no_suppress_warnings;

// will lead to compile error if JFlex emits its own @SuppressWarnings
@SuppressWarnings("all")
%%

%no_suppress_warnings
%class NoSuppress
%debug
%int

%%

a|b { return 1; }
[^] { return 0; }
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Reading "javatests/de/jflex/testcase/semcheck/semcheck.flex"

Error in file "javatests/de/jflex/testcase/semcheck/semcheck.flex" (line 12):
Error in file "javatests/de/jflex/testcase/semcheck/semcheck.flex" (line 17):
Lookahead expression must have match with length of at least 1.
!"foo" / "bar" { }
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Reading "javatests/de/jflex/testcase/unused_warning/unused.flex"

Warning : Macro "UNUSED" has been declared but never used.
Warning: Macro "UNUSED" has been declared but never used.
Constructing NFA : 4 states in NFA
Converting NFA to DFA :
.
Expand Down
6 changes: 5 additions & 1 deletion jflex/src/main/java/jflex/core/AbstractLexScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public enum CharSetSize {
boolean standalone;
boolean debugOption;
boolean eofclose;
boolean noSuppressWarnings;

String isImplementing;
String isExtending;
Expand All @@ -89,7 +90,6 @@ public UnicodeProperties getUnicodeProperties() {
return unicodeProperties;
}

// TODO(regisd) Return an immutable representation of char classes
@SuppressWarnings("unused") // Used in generated LexParse
public CharClasses getCharClasses() {
return charClasses;
Expand Down Expand Up @@ -396,6 +396,10 @@ public int bufferSize() {
return bufferSize;
}

public boolean noSuppressWarnings() {
return noSuppressWarnings;
}

/**
* Returns the current line number.
*
Expand Down
7 changes: 4 additions & 3 deletions jflex/src/main/java/jflex/generator/Emitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,10 @@ private void emitUserCode() {
}

private void emitClassName() {
// TODO(#222) Actually fix the fall-through violations
println("// See https://github.com/jflex-de/jflex/issues/222");
println("@SuppressWarnings(\"FallThrough\")");
if (!scanner.noSuppressWarnings()) {
// TODO(#222) Actually fix the fall-through violations
println("@SuppressWarnings(\"fallthrough\")");
}
if (scanner.isPublic()) print("public ");

if (scanner.isAbstract()) print("abstract ");
Expand Down
2 changes: 2 additions & 0 deletions jflex/src/main/jflex/LexScan.flex
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ DottedVersion = [1-9][0-9]*(\.[0-9]+){0,2}
"%warn" {WSP}+ {WarningIdent} {WSP}* { OptionUtils.enableWarning(yytext().substring(6).trim()); }
"%warn" {WSP}+ {NNL}* { throw new ScannerException(file, ErrorMessages.NOT_A_WARNING_ID, yyline); }

"%no_suppress_warnings" {WSP}* { noSuppressWarnings = true; }

{Ident} { return symbol(IDENT, yytext()); }
"="{WSP}* { yybegin(REGEXP);
return symbol(EQUALS);
Expand Down