diff --git a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/tests/dotnet/VisualStudioTestResultsFileParser.java b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/tests/dotnet/VisualStudioTestResultsFileParser.java index fcc2b2f939..6aa25ab37c 100644 --- a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/tests/dotnet/VisualStudioTestResultsFileParser.java +++ b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/tests/dotnet/VisualStudioTestResultsFileParser.java @@ -122,7 +122,7 @@ private Date getRequiredDateAttribute(XmlParserHelper xmlParserHelper, String na } private String keepOnlyMilliseconds(String value) { - var sb = new StringBuffer(256); + var sb = new StringBuilder(256); var matcher = millisecondsPattern.matcher(value); var trailingZeros = new StringBuilder(128); diff --git a/cxx-squid-bridge/src/main/java/org/sonar/cxx/squidbridge/api/SourceCode.java b/cxx-squid-bridge/src/main/java/org/sonar/cxx/squidbridge/api/SourceCode.java index 05d48b63ef..4677ee530f 100644 --- a/cxx-squid-bridge/src/main/java/org/sonar/cxx/squidbridge/api/SourceCode.java +++ b/cxx-squid-bridge/src/main/java/org/sonar/cxx/squidbridge/api/SourceCode.java @@ -45,11 +45,11 @@ public abstract class SourceCode implements Measurable, Comparable { private SourceCodeIndexer indexer; private Set messages; - public SourceCode(String key) { + protected SourceCode(String key) { this(key, null); } - public SourceCode(String key, @Nullable String name) { + protected SourceCode(String key, @Nullable String name) { this.key = key; this.name = name; } diff --git a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CxxPreprocessor.java b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CxxPreprocessor.java index 38e9bc4e17..5151381815 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CxxPreprocessor.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CxxPreprocessor.java @@ -394,12 +394,18 @@ private boolean addUnitForcedIncludes(String level) { private void parseIncludeLine(String includeLine) { AstNode astNode = lineParser(includeLine); - handleIncludeLine(astNode, astNode.getFirstDescendant(PPGrammarImpl.includeBodyQuoted).getToken()); + if (astNode != null) { + handleIncludeLine(astNode, astNode.getFirstDescendant(PPGrammarImpl.includeBodyQuoted).getToken()); + } } + @CheckForNull PPMacro parseMacroDefinition(String macroDef) { AstNode astNode = lineParser(macroDef); - return PPMacro.create(astNode.getFirstDescendant(PPGrammarImpl.defineLine)); + if (astNode != null) { + return PPMacro.create(astNode.getFirstDescendant(PPGrammarImpl.defineLine)); + } + return null; } /** @@ -573,42 +579,33 @@ private PreprocessorAction handleModuleLine(AstNode ast, Token token) { return oneConsumedToken(token); } - private static List createChildrenTokenList(AstNode ast) { - var children = ast.getChildren(); - var list = new ArrayList(children.size()); - for (var child : children) { - list.add(child.getToken()); - } - return list; - } - private PreprocessorAction mapFromPPToCxx(AstNode ast, Token token) { - List replTokens = new ArrayList<>(); - for (Token ppToken : TokenUtils.removeLastTokenIfEof(createChildrenTokenList(ast))) { // TODO removeLastTokenIfEof? - String value = ppToken.getValue(); - if (!value.isBlank()) { - // call CXX lexer to create a CXX token - var lexer = lineLexerwithoutPP.borrowLexer(); - List cxxTokens = lexer.lex(value); - lineLexerwithoutPP.returnLexer(lexer); - var cxxToken = cxxTokens.get(0); - var cxxType = cxxToken.getType(); - - if (!cxxType.equals(GenericTokenType.EOF)) { - cxxToken = Token.builder() + var ppTokens = ast.getTokens(); + List result = new ArrayList<>(ppTokens.size()); + var lexer = lineLexerwithoutPP.borrowLexer(); + try { + for (var ppToken : ppTokens) { + String value = ppToken.getValue(); + if (!"EOF".equals(value) && !value.isBlank()) { + + // call CXX lexer to create a CXX token + List cxxTokens = lexer.lex(value); + + var cxxToken = Token.builder() .setLine(token.getLine() + ppToken.getLine() - 1) .setColumn(token.getColumn() + ppToken.getColumn()) .setURI(ppToken.getURI()) .setValueAndOriginalValue(ppToken.getValue()) - .setType(cxxType) + .setType(cxxTokens.get(0).getType()) .build(); - replTokens.add(cxxToken); + result.add(cxxToken); } } + } finally { + lineLexerwithoutPP.returnLexer(lexer); } - - return new PreprocessorAction(1, Collections.singletonList(Trivia.createPreprocessingToken(token)), replTokens); + return new PreprocessorAction(1, Collections.singletonList(Trivia.createPreprocessingToken(token)), result); } static private List adjustPosition(List tokens, Token position) { diff --git a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/IncludeDirectiveLexer.java b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/IncludeDirectiveLexer.java index fbfc3b14e6..887b8da904 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/IncludeDirectiveLexer.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/IncludeDirectiveLexer.java @@ -27,16 +27,16 @@ import org.sonar.cxx.channels.PreprocessorChannel; import org.sonar.cxx.config.CxxSquidConfiguration; -public final class IncludeDirectiveLexer { +final class IncludeDirectiveLexer { private IncludeDirectiveLexer() { } - public static Lexer create(Preprocessor... preprocessors) { + static Lexer create(Preprocessor... preprocessors) { return create(new CxxSquidConfiguration(), preprocessors); } - public static Lexer create(CxxSquidConfiguration squidConfig, Preprocessor... preprocessors) { + static Lexer create(CxxSquidConfiguration squidConfig, Preprocessor... preprocessors) { var builder = Lexer.builder() .withCharset(squidConfig.getCharset()) .withFailIfNoChannelToConsumeOneCharacter(true) diff --git a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/MacroContainer.java b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/MacroContainer.java index f3f3aaeb58..57d8492567 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/MacroContainer.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/MacroContainer.java @@ -19,9 +19,10 @@ */ package org.sonar.cxx.preprocessor; +import java.util.ArrayDeque; +import java.util.Deque; import java.util.HashMap; import java.util.Map; -import java.util.Stack; import java.util.stream.Collectors; /** @@ -35,7 +36,7 @@ public class MacroContainer { private final Map values = new HashMap<>(); - private final Stack disabled = new Stack<>(); + private final Deque disabled = new ArrayDeque<>(); /** * get value for key. @@ -44,11 +45,10 @@ public class MacroContainer { */ public V get(K key) { V v = values.get(key); - if (v != null) { - if (disabled.isEmpty() || disabled.search(key) == -1) { - return v; - } + if ((v != null) && (disabled.isEmpty() || !disabled.contains(key))) { + return v; } + return null; } @@ -109,10 +109,9 @@ public void popDisable() { */ @Override public String toString() { - String mapAsString = values.values().stream() - .map(value -> value.toString()) + return values.values().stream() + .map(Object::toString) .collect(Collectors.joining(", ", "[", "]")); - return mapAsString; } } diff --git a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPConcatenation.java b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPConcatenation.java index 0c97792948..06f22fa748 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPConcatenation.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPConcatenation.java @@ -35,7 +35,7 @@ * a longer identifier, digits that form a number, or operators + and = that form a +=. A comment cannot be created by * pasting / and * because comments are removed from text before macro substitution is considered. */ -class PPConcatenation { +final class PPConcatenation { private static final Logger LOG = Loggers.get(PPConcatenation.class); diff --git a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPGeneratedToken.java b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPGeneratedToken.java index 62110df56c..f0e7f922dc 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPGeneratedToken.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPGeneratedToken.java @@ -26,7 +26,7 @@ import java.util.ArrayList; import java.util.List; -class PPGeneratedToken { +final class PPGeneratedToken { private PPGeneratedToken() { diff --git a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPInclude.java b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPInclude.java index 0b954a8ac0..30d4b7cd41 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPInclude.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPInclude.java @@ -28,7 +28,7 @@ class PPInclude { private CxxPreprocessor pp; - public PPInclude(CxxPreprocessor pp) { + PPInclude(CxxPreprocessor pp) { this.pp = pp; } diff --git a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPMacro.java b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPMacro.java index 6787b876c2..6eecd97e96 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPMacro.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPMacro.java @@ -73,7 +73,7 @@ private PPMacro(String name, String body) { this.isVariadic = false; } - public static PPMacro create(AstNode defineLineAst) { + static PPMacro create(AstNode defineLineAst) { var ast = defineLineAst.getFirstChild(); var nameNode = ast.getFirstDescendant(PPGrammarImpl.ppToken); String macroName = nameNode.getTokenValue(); @@ -131,7 +131,7 @@ public String toString() { return ab.toString(); } - public boolean checkArgumentsCount(int count) { + boolean checkArgumentsCount(int count) { if (params != null) { return isVariadic ? count >= params.size() - 1 : count == params.size(); } diff --git a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPPredefinedMacros.java b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPPredefinedMacros.java index a64b43ef9f..e9c10a11fd 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPPredefinedMacros.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPPredefinedMacros.java @@ -24,9 +24,9 @@ * * The macro names of this class are predefined in every translation unit. */ -class PPPredefinedMacros { +final class PPPredefinedMacros { - static String[] predefinedMacros = { + private static final String[] predefinedMacros = { "__FILE__ \"file\"", "__LINE__ 1", // indicates 'date unknown'. should suffice @@ -41,6 +41,10 @@ class PPPredefinedMacros { "__has_include 1" }; + private PPPredefinedMacros() { + + } + static String[] predefinedMacroValues() { return predefinedMacros.clone(); } diff --git a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPReplace.java b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPReplace.java index 86bdf4a079..d32bd9a3d8 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPReplace.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPReplace.java @@ -38,7 +38,7 @@ class PPReplace { private static final Logger LOG = Loggers.get(PPReplace.class); private CxxPreprocessor pp; - public PPReplace(CxxPreprocessor pp) { + PPReplace(CxxPreprocessor pp) { this.pp = pp; } diff --git a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPSpecialIdentifier.java b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPSpecialIdentifier.java index d3dc2fe85a..613e7bc7d6 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPSpecialIdentifier.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPSpecialIdentifier.java @@ -38,7 +38,7 @@ public enum PPSpecialIdentifier implements TokenType { this.value = value; } - public static String[] keywordValues() { + static String[] keywordValues() { PPSpecialIdentifier[] keywordsEnum = PPSpecialIdentifier.values(); var keywords = new String[keywordsEnum.length]; for (var i = 0; i < keywords.length; i++) { diff --git a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPStringification.java b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPStringification.java index 8bc9c4f3fb..d95777cc82 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPStringification.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/PPStringification.java @@ -28,7 +28,7 @@ * operation is called "stringification". If the result of stringification is not a valid string literal, the behavior * is undefined. */ -class PPStringification { +final class PPStringification { private PPStringification() { diff --git a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/JoinStringsPreprocessorTest.java b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/JoinStringsPreprocessorTest.java index b7796b883f..9745d17675 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/JoinStringsPreprocessorTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/JoinStringsPreprocessorTest.java @@ -27,19 +27,19 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class JoinStringsPreprocessorTest { +class JoinStringsPreprocessorTest { private JoinStringsPreprocessor pp; private Lexer lexer; @BeforeEach - public void setUp() { + void setUp() { pp = new JoinStringsPreprocessor(); lexer = PPLexer.create(); } @Test - public void testProcess() { + void testProcess() { List tokens = lexer.lex("\"A\"\"B\""); PreprocessorAction result = pp.process(tokens); assertThat(result.getNumberOfConsumedTokens()).isEqualTo(2); diff --git a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/MacroContainerTest.java b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/MacroContainerTest.java index 70cb24c7a9..9027e19176 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/MacroContainerTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/MacroContainerTest.java @@ -26,7 +26,7 @@ class MacroContainerTest { private final MacroContainer mc; - public MacroContainerTest() { + MacroContainerTest() { mc = new MacroContainer<>(); } diff --git a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPConcatenationTest.java b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPConcatenationTest.java index 9b3bd764be..fbb3acd4c0 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPConcatenationTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPConcatenationTest.java @@ -26,17 +26,17 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class PPConcatenationTest { +class PPConcatenationTest { private Lexer lexer; @BeforeEach - public void setUp() { + void setUp() { lexer = PPLexer.create(); } @Test - public void testConcatenate() { + void testConcatenate() { List tokens = lexer.lex("x ## y"); List result = PPConcatenation.concatenate(tokens); assertThat(result) diff --git a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPGeneratedTokenTest.java b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPGeneratedTokenTest.java index c08286fe08..906dba37ed 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPGeneratedTokenTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPGeneratedTokenTest.java @@ -28,17 +28,17 @@ import static org.assertj.core.api.Assertions.*; import org.junit.jupiter.api.Test; -public class PPGeneratedTokenTest { +class PPGeneratedTokenTest { @Test - public void testNewGeneratedTokenFromExistingToken() { + void testNewGeneratedTokenFromExistingToken() { Token token = TokenUtils.tokenBuilder(IDENTIFIER, "Token", 1, 2); Token result = PPGeneratedToken.build(token); assertThat(result.isGeneratedCode()).isTrue(); } @Test - public void testNewGeneratedTokenWithNewValueAndType() { + void testNewGeneratedTokenWithNewValueAndType() { Token token = TokenUtils.tokenBuilder(IDENTIFIER, "Token", 1, 2); Token result = PPGeneratedToken.build(token, CONSTANT, "3"); @@ -50,7 +50,7 @@ public void testNewGeneratedTokenWithNewValueAndType() { } @Test - public void testNewGeneratedTokenWithNewPosition() throws URISyntaxException { + void testNewGeneratedTokenWithNewPosition() throws URISyntaxException { Token token = TokenUtils.tokenBuilder(IDENTIFIER, "Token", 1, 2); var uri = new URI("tests://sample"); Token result = PPGeneratedToken.build(token, uri, 3, 4); @@ -64,7 +64,7 @@ public void testNewGeneratedTokenWithNewPosition() throws URISyntaxException { } @Test - public void testNewGeneratedToken() { + void testNewGeneratedToken() { Token result = PPGeneratedToken.build(CONSTANT, "3", 1, 2); assertThat(result.isGeneratedCode()).isTrue(); @@ -75,7 +75,7 @@ public void testNewGeneratedToken() { } @Test - public void testMarkAllAsGenerated() { + void testMarkAllAsGenerated() { List tokens = PPLexer.create().lex("A B"); List result = PPGeneratedToken.markAllAsGenerated(tokens); diff --git a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPIncludeTest.java b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPIncludeTest.java index b3efecfb37..dc3216e901 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPIncludeTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPIncludeTest.java @@ -35,7 +35,7 @@ import static org.mockito.Mockito.when; import org.sonar.cxx.squidbridge.SquidAstVisitorContext; -public class PPIncludeTest { +class PPIncludeTest { private CxxPreprocessor pp; private PPInclude include; @@ -46,7 +46,7 @@ public class PPIncludeTest { File tempDir; @BeforeEach - public void setUp() throws IOException { + void setUp() throws IOException { var context = mock(SquidAstVisitorContext.class); when(context.getFile()).thenReturn(new File("dummy")); // necessary for init pp = new CxxPreprocessor(context); @@ -60,14 +60,14 @@ public void setUp() throws IOException { } @Test - public void testFindIncludedFileQuoted() { + void testFindIncludedFileQuoted() { AstNode ast = lineParser.parse("#include " + "\"" + foo.toAbsolutePath().toString() + "\""); File result = include.findIncludedFile(ast); assertThat(result).isEqualTo(foo.toFile()); } @Test - public void testFindIncludedFileBracketed() { + void testFindIncludedFileBracketed() { AstNode ast = lineParser.parse("#include " + "<" + foo.toAbsolutePath().toString() + ">"); File result = include.findIncludedFile(ast); assertThat(result).isEqualTo(foo.toFile()); diff --git a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPMacroTest.java b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPMacroTest.java index 64432ccadb..8682032475 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPMacroTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPMacroTest.java @@ -27,17 +27,17 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class PPMacroTest { +class PPMacroTest { private Parser lineParser; @BeforeEach - public void setUp() { + void setUp() { lineParser = PPParser.create(Charset.defaultCharset()); } @Test - public void testCreateMacro() { + void testCreateMacro() { AstNode lineAst = lineParser.parse("#define MACRO(P1, P2) REPLACEMENT_LIST"); PPMacro result = PPMacro.create(lineAst); @@ -55,7 +55,7 @@ public void testCreateMacro() { } @Test - public void testCreateVariadicMacro() { + void testCreateVariadicMacro() { AstNode lineAst = lineParser.parse("#define MACRO(...) REPLACEMENT_LIST"); PPMacro result = PPMacro.create(lineAst); diff --git a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPNumberTest.java b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPNumberTest.java index 574de9af67..8a73a3348e 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPNumberTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPNumberTest.java @@ -23,7 +23,7 @@ import static org.assertj.core.api.Assertions.*; import org.junit.jupiter.api.Test; -public class PPNumberTest { +class PPNumberTest { @Test void decode_numbers() { diff --git a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPParserTest.java b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPParserTest.java index a8a3900ebc..758d1df5a0 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPParserTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPParserTest.java @@ -26,16 +26,16 @@ import static org.assertj.core.api.Assertions.*; import org.junit.jupiter.api.Test; -public class PPParserTest { +class PPParserTest { @Test - public void testCreate() { + void testCreate() { Parser result = PPParser.create(Charset.defaultCharset()); assertThat(result.getRootRule().getName()).isEqualTo("preprocessorLine"); } @Test - public void testParse() { + void testParse() { Parser result = PPParser.create(Charset.defaultCharset()); AstNode astNode = result.parse("#define HELLO WORLD") .getFirstDescendant(PPGrammarImpl.objectlikeMacroDefinition); diff --git a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPPredefinedMacrosTest.java b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPPredefinedMacrosTest.java index 0b5f2444ca..7543ca62f9 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPPredefinedMacrosTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPPredefinedMacrosTest.java @@ -23,10 +23,10 @@ import static org.assertj.core.api.Assertions.*; import org.junit.jupiter.api.Test; -public class PPPredefinedMacrosTest { +class PPPredefinedMacrosTest { @Test - public void testPredefinedMacroValues() { + void testPredefinedMacroValues() { var expResult = Arrays.asList( "__FILE__", "__LINE__", diff --git a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPReplaceTest.java b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPReplaceTest.java index d23b63cf99..0b74e4daca 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPReplaceTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPReplaceTest.java @@ -32,14 +32,14 @@ import static org.mockito.Mockito.when; import org.sonar.cxx.squidbridge.SquidAstVisitorContext; -public class PPReplaceTest { +class PPReplaceTest { private PPReplace replace; private Lexer lexer; private CxxPreprocessor pp; @BeforeEach - public void setUp() { + void setUp() { var context = mock(SquidAstVisitorContext.class); when(context.getFile()).thenReturn(new File("dummy")); // necessary for init pp = spy(new CxxPreprocessor(context)); @@ -49,7 +49,7 @@ public void setUp() { } @Test - public void testReplaceObjectLikeMacro() { + void testReplaceObjectLikeMacro() { String macroName = "SAMPLE"; String macroExpression = "__LINE__"; List result = replace.replaceObjectLikeMacro(macroName, macroExpression); @@ -59,7 +59,7 @@ public void testReplaceObjectLikeMacro() { } @Test - public void testReplaceFunctionLikeMacro() { + void testReplaceFunctionLikeMacro() { List args = lexer.lex("(1, 2)"); PPMacro macro = pp.parseMacroDefinition("#define TEST(a, b) a+b"); when(pp.getMacro(macro.name)).thenReturn(macro); diff --git a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPSpecialIdentifierTest.java b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPSpecialIdentifierTest.java index ec66feca98..e8128b3a7e 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPSpecialIdentifierTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPSpecialIdentifierTest.java @@ -22,29 +22,29 @@ import static org.assertj.core.api.Assertions.*; import org.junit.jupiter.api.Test; -public class PPSpecialIdentifierTest { +class PPSpecialIdentifierTest { @Test - public void testValueOf() { + void testValueOf() { PPSpecialIdentifier result = PPSpecialIdentifier.valueOf("IMPORT"); assertThat(result).isEqualTo(PPSpecialIdentifier.IMPORT); } @Test - public void testGetter() { + void testGetter() { PPSpecialIdentifier result = PPSpecialIdentifier.valueOf("IMPORT"); assertThat(result.getName()).isEqualTo("IMPORT"); assertThat(result.getValue()).isEqualTo("import"); } @Test - public void testHasToBeSkippedFromAst() { + void testHasToBeSkippedFromAst() { PPSpecialIdentifier result = PPSpecialIdentifier.valueOf("IMPORT"); assertThat(result.hasToBeSkippedFromAst(null /* always false */)).isFalse(); } @Test - public void testKeywordValues() { + void testKeywordValues() { String[] result = PPSpecialIdentifier.keywordValues(); assertThat(result).hasSize(3); } diff --git a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPStringificationTest.java b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPStringificationTest.java index 1c1d37e72e..e9be21af0b 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPStringificationTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/preprocessor/PPStringificationTest.java @@ -22,28 +22,28 @@ import static org.assertj.core.api.Assertions.*; import org.junit.jupiter.api.Test; -public class PPStringificationTest { +class PPStringificationTest { @Test - public void testStringify() { + void testStringify() { String result = PPStringification.stringify("A"); assertThat(result).isEqualTo("\"A\""); } @Test - public void testStringifyDoubleQuotes() { + void testStringifyDoubleQuotes() { String result = PPStringification.stringify("\""); assertThat(result).isEqualTo("\"\\\"\""); } @Test - public void testStringifyBackslash() { + void testStringifyBackslash() { String result = PPStringification.stringify("\\"); assertThat(result).isEqualTo("\"\\\\\""); } @Test - public void testStringifyComplex() { + void testStringifyComplex() { String result = PPStringification.stringify("A \" \\ B"); assertThat(result).isEqualTo("\"A \\\"\\\\B\""); } diff --git a/cxx-sslr/sslr-core/src/main/java/com/sonar/cxx/sslr/api/Grammar.java b/cxx-sslr/sslr-core/src/main/java/com/sonar/cxx/sslr/api/Grammar.java index d10821afcb..ba7342ef05 100644 --- a/cxx-sslr/sslr-core/src/main/java/com/sonar/cxx/sslr/api/Grammar.java +++ b/cxx-sslr/sslr-core/src/main/java/com/sonar/cxx/sslr/api/Grammar.java @@ -40,7 +40,7 @@ */ public abstract class Grammar { - public Grammar() { + protected Grammar() { instanciateRuleFields(); } diff --git a/cxx-sslr/sslr-core/src/main/java/org/sonar/cxx/sslr/channel/CodeReaderFilter.java b/cxx-sslr/sslr-core/src/main/java/org/sonar/cxx/sslr/channel/CodeReaderFilter.java index ec8c982203..4c3b7ac31b 100644 --- a/cxx-sslr/sslr-core/src/main/java/org/sonar/cxx/sslr/channel/CodeReaderFilter.java +++ b/cxx-sslr/sslr-core/src/main/java/org/sonar/cxx/sslr/channel/CodeReaderFilter.java @@ -28,8 +28,10 @@ /** * This class can be extended to provide filtering capabilities for the CodeReader class.
- * The purpose is to filter the character flow before the CodeReader class passes it to the different channels. It is possible to give - * several filters to a CodeReader: they will be called one after another, following the declaration order in the CodeReader constructor, to + * The purpose is to filter the character flow before the CodeReader class passes it to the different channels. It is + * possible to give + * several filters to a CodeReader: they will be called one after another, following the declaration order in the + * CodeReader constructor, to * sequentially filter the character flow. */ public abstract class CodeReaderFilter { @@ -40,10 +42,10 @@ public abstract class CodeReaderFilter { private CodeReaderConfiguration configuration; - public CodeReaderFilter() { + protected CodeReaderFilter() { } - public CodeReaderFilter(O output) { + protected CodeReaderFilter(O output) { this.output = output; } @@ -60,7 +62,7 @@ public Reader getReader() { * Sets the reader from which this class will read the character stream. * * @param reader - * the reader + * the reader */ public void setReader(Reader reader) { this.reader = reader; @@ -79,7 +81,7 @@ public O getOutput() { * Sets the output object * * @param output - * the output to set + * the output to set */ public void setOutput(O output) { this.output = output; @@ -98,7 +100,7 @@ public CodeReaderConfiguration getConfiguration() { * Sets the configuration that must be used by the CodeReader * * @param configuration - * the configuration to set + * the configuration to set */ public void setConfiguration(CodeReaderConfiguration configuration) { this.configuration = configuration; @@ -116,14 +118,14 @@ public void setConfiguration(CodeReaderConfiguration configuration) { * * * @param filteredBuffer - * the output buffer that must contain the filtered data + * the output buffer that must contain the filtered data * @param offset - * the offset to start reading from the reader + * the offset to start reading from the reader * @param length - * the number of characters to read from the reader + * the number of characters to read from the reader * @return The number of characters read, or -1 if the end of the stream has been reached * @throws IOException - * If an I/O error occurs + * If an I/O error occurs */ public abstract int read(char[] filteredBuffer, int offset, int length) throws IOException; diff --git a/cxx-sslr/sslr-core/src/main/java/org/sonar/cxx/sslr/channel/RegexChannel.java b/cxx-sslr/sslr-core/src/main/java/org/sonar/cxx/sslr/channel/RegexChannel.java index d9ddcb1230..dcd226f808 100644 --- a/cxx-sslr/sslr-core/src/main/java/org/sonar/cxx/sslr/channel/RegexChannel.java +++ b/cxx-sslr/sslr-core/src/main/java/org/sonar/cxx/sslr/channel/RegexChannel.java @@ -27,7 +27,8 @@ import java.util.regex.Pattern; /** - * The RegexChannel can be used to be called each time the next characters in the character stream match a regular expression + * The RegexChannel can be used to be called each time the next characters in the character stream match a regular + * expression */ public abstract class RegexChannel extends Channel { @@ -38,9 +39,9 @@ public abstract class RegexChannel extends Channel { * Create a RegexChannel object with the required regular expression * * @param regex - * regular expression to be used to try matching the next characters in the stream + * regular expression to be used to try matching the next characters in the stream */ - public RegexChannel(String regex) { + protected RegexChannel(String regex) { matcher = Pattern.compile(regex).matcher(""); } @@ -55,13 +56,14 @@ public final boolean consume(CodeReader code, O output) { } /** - * The consume method is called each time the regular expression used to create the RegexChannel object matches the next characters in the + * The consume method is called each time the regular expression used to create the RegexChannel object matches the + * next characters in the * character streams. * * @param token - * the token consumed in the character stream and matching the regular expression + * the token consumed in the character stream and matching the regular expression * @param output - * the OUTPUT object which can be optionally fed + * the OUTPUT object which can be optionally fed */ protected abstract void consume(CharSequence token, O output); diff --git a/cxx-sslr/sslr-core/src/main/java/org/sonar/cxx/sslr/internal/vm/Machine.java b/cxx-sslr/sslr-core/src/main/java/org/sonar/cxx/sslr/internal/vm/Machine.java index 111e1cf47d..9d39a5a34d 100644 --- a/cxx-sslr/sslr-core/src/main/java/org/sonar/cxx/sslr/internal/vm/Machine.java +++ b/cxx-sslr/sslr-core/src/main/java/org/sonar/cxx/sslr/internal/vm/Machine.java @@ -138,8 +138,10 @@ private Machine(@Nullable char[] input, @Nullable Token[] tokens, Instruction[] this.tokens = tokens; if (input != null) { this.inputLength = input.length; - } else { + } else if (tokens != null) { this.inputLength = tokens.length; + } else { + this.inputLength = 0; } this.handler = handler; diff --git a/cxx-sslr/sslr-toolkit/src/main/java/org/sonar/cxx/sslr/toolkit/AbstractConfigurationModel.java b/cxx-sslr/sslr-toolkit/src/main/java/org/sonar/cxx/sslr/toolkit/AbstractConfigurationModel.java index 696cd27e9c..a602bf743e 100644 --- a/cxx-sslr/sslr-toolkit/src/main/java/org/sonar/cxx/sslr/toolkit/AbstractConfigurationModel.java +++ b/cxx-sslr/sslr-toolkit/src/main/java/org/sonar/cxx/sslr/toolkit/AbstractConfigurationModel.java @@ -24,10 +24,9 @@ package org.sonar.cxx.sslr.toolkit; // cxx: in use import com.sonar.cxx.sslr.impl.Parser; -import org.sonar.colorizer.Tokenizer; - import java.nio.charset.Charset; import java.util.List; +import org.sonar.colorizer.Tokenizer; /** * This class provides an default optimized implementation of the {@link ConfigurationModel} interface. @@ -44,7 +43,7 @@ public abstract class AbstractConfigurationModel implements ConfigurationModel { private Parser parser; private List tokenizers; - public AbstractConfigurationModel() { + protected AbstractConfigurationModel() { this.updatedFlag = true; }