Skip to content

Commit 5663bbb

Browse files
authored
Merge pull request #2528 from guwirth/fix-2505
correct handling of # in preprocessor
2 parents 825b2ec + b70efec commit 5663bbb

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

cxx-squid/src/main/java/org/sonar/cxx/channels/PreprocessorChannel.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ public boolean consume(CodeReader code, Lexer output) {
5757
int line = code.getLinePosition();
5858
int column = code.getColumnPosition();
5959

60+
// if there was already a token in the line it's not a preprocessor command
61+
var previousTokens = output.getTokens();
62+
if (!previousTokens.isEmpty()) {
63+
if (previousTokens.get(previousTokens.size() - 1).getLine() == line) {
64+
return false;
65+
}
66+
}
67+
6068
if (code.popTo(matcher, sb) <= 0) {
6169
return false;
6270
}

cxx-squid/src/test/java/org/sonar/cxx/lexer/CxxLexerWithPreprocessingTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,4 +1030,21 @@ void string_problem_1903() {
10301030
softly.assertAll();
10311031
}
10321032

1033+
@Test
1034+
void undefined_macro_with_hash_parameter() {
1035+
List<Token> tokens = lexer.lex("BOOST_PP_EXPAND(#) define BOOST_FT_config_valid 1");
1036+
assertThat(tokens).hasSize(8);
1037+
}
1038+
1039+
@Test
1040+
void defined_macro_with_hash_parameter() {
1041+
List<Token> tokens = lexer.lex("#define BOOST_PP_EXPAND(p) p\n"
1042+
+ "BOOST_PP_EXPAND(#) define BOOST_FT_config_valid 1\n"
1043+
+ "BOOST_FT_config_valid");
1044+
1045+
assertThat(tokens).anySatisfy(token -> assertThat(token)
1046+
.isValue("1")
1047+
.hasType(CxxTokenType.NUMBER));
1048+
}
1049+
10331050
}

cxx-squid/src/test/java/org/sonar/cxx/lexer/CxxLexerWithoutPreprocessorTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public void init() {
4040
@Test
4141
void preprocessor_directives() {
4242
var softly = new SoftAssertions();
43+
44+
softly.assertThat(lexer.lex("#")).anySatisfy(token -> assertThat(token).isValue(
45+
"#").hasType(CxxTokenType.PREPROCESSOR));
4346
softly.assertThat(lexer.lex("#include <iostream>")).anySatisfy(token -> assertThat(token).isValue(
4447
"#include <iostream>").hasType(CxxTokenType.PREPROCESSOR));
4548
softly.assertThat(lexer.lex("# include <iostream>")).anySatisfy(token -> assertThat(token).isValue(

0 commit comments

Comments
 (0)