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

fix PreprocessorChannel: a multiline comment results in a blank #2812

Merged
merged 1 commit into from
Nov 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ private void read(CodeReader code) {
} else {
len = multiLineCommentChannel.isComment(code);
if (len != 0) {
// multi line comment
// multi line comment: replace it with a blank
code.skip(len);
multiLineCommentChannel.read(code, dummy);
sb.append(' ');
dummy.delete(0, dummy.length());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ int main() {
O 10\\
20
""")).as("preprocessor directive with line splicing").anySatisfy(token
-> assertThat(token).isValue("EOF").hasTrivia().isTrivia("# define FOO 1020").isTriviaLine(3));
-> assertThat(token).isValue("EOF").hasTrivia().isTrivia("# define FOO 1020").isTriviaLine(3));
softly.assertAll();

softly.assertThat(lexer.lex("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,53 +66,53 @@ void preprocessorDirectives() {
@Test
void preprocessorContinuedDefine() {
assertThat(lexer.lex("""
#define M\\
0
""")).anySatisfy(token -> assertThat(token).isValue("#define M0").hasType(
#define M\\
0
""")).anySatisfy(token -> assertThat(token).isValue("#define M0").hasType(
CxxTokenType.PREPROCESSOR));
}

@Test
void preprocessorDirectiveWithComment() {
var softly = new SoftAssertions();
softly.assertThat(lexer.lex("""
#define A B*/
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A B*/")
#define A1 B*/
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A1 B*/")
.hasType(CxxTokenType.PREPROCESSOR));
softly.assertThat(lexer.lex("""
#define A B/*CCC*/
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A B")
#define A2 B/*CCC*/
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A2 B ")
.hasType(CxxTokenType.PREPROCESSOR));
softly.assertThat(lexer.lex("""
#define A B/**/C
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A BC")
#define A3 B/**/C
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A3 B C")
.hasType(CxxTokenType.PREPROCESSOR));
softly.assertThat(lexer.lex("""
#define A B/*C
#define A4 B/*C


C*/D
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A BD").hasType(CxxTokenType.PREPROCESSOR));
C*/D
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A4 B D").hasType(CxxTokenType.PREPROCESSOR));
softly.assertThat(lexer.lex("""
#define A "a/*" B
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A \"a/*\" B").hasType(CxxTokenType.PREPROCESSOR));
#define A5 "a/*" B
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A5 \"a/*\" B").hasType(CxxTokenType.PREPROCESSOR));
softly.assertThat(lexer.lex("""
#define A "-str/*"-/*CCC*/
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A \"-str/*\"-").hasType(CxxTokenType.PREPROCESSOR));
#define A6 "-str/*"-/*CCC*/
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A6 \"-str/*\"- ").hasType(CxxTokenType.PREPROCESSOR));
softly.assertThat(lexer.lex("""
#define A B/*-"str"-*/C
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A BC").hasType(CxxTokenType.PREPROCESSOR));
#define A7 B/*-"str"-*/C
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A7 B C").hasType(CxxTokenType.PREPROCESSOR));
softly.assertThat(lexer.lex("""
#define A B//-/*-"str"-*/
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A B").hasType(CxxTokenType.PREPROCESSOR));
#define A8 B//-/*-"str"-*/
""")).anySatisfy(token -> assertThat(token)
.isValue("#define A8 B").hasType(CxxTokenType.PREPROCESSOR));
softly.assertAll();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ public class ParserBaseTestHelper {

public ParserBaseTestHelper() {
squidConfig = new CxxSquidConfiguration();
squidConfig.add(CxxSquidConfiguration.SONAR_PROJECT_PROPERTIES, CxxSquidConfiguration.ERROR_RECOVERY_ENABLED, "false");
squidConfig.add(
CxxSquidConfiguration.SONAR_PROJECT_PROPERTIES, CxxSquidConfiguration.ERROR_RECOVERY_ENABLED, "false"
);

var file = new File("snippet.cpp").getAbsoluteFile();
SquidAstVisitorContextImpl<Grammar> context = mock(SquidAstVisitorContextImpl.class);
when(context.getFile()).thenReturn(file);

p = CxxParser.create(context, squidConfig);
//var builder = AstScanner.<Grammar>builder(context).setBaseParser(p);
g = p.getGrammar();
}

Expand Down
Loading