Skip to content

Commit

Permalink
Merge pull request #2528 from guwirth/fix-2505
Browse files Browse the repository at this point in the history
correct handling of # in preprocessor
  • Loading branch information
guwirth authored May 20, 2023
2 parents 825b2ec + b70efec commit 5663bbb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public boolean consume(CodeReader code, Lexer output) {
int line = code.getLinePosition();
int column = code.getColumnPosition();

// if there was already a token in the line it's not a preprocessor command
var previousTokens = output.getTokens();
if (!previousTokens.isEmpty()) {
if (previousTokens.get(previousTokens.size() - 1).getLine() == line) {
return false;
}
}

if (code.popTo(matcher, sb) <= 0) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1030,4 +1030,21 @@ void string_problem_1903() {
softly.assertAll();
}

@Test
void undefined_macro_with_hash_parameter() {
List<Token> tokens = lexer.lex("BOOST_PP_EXPAND(#) define BOOST_FT_config_valid 1");
assertThat(tokens).hasSize(8);
}

@Test
void defined_macro_with_hash_parameter() {
List<Token> tokens = lexer.lex("#define BOOST_PP_EXPAND(p) p\n"
+ "BOOST_PP_EXPAND(#) define BOOST_FT_config_valid 1\n"
+ "BOOST_FT_config_valid");

assertThat(tokens).anySatisfy(token -> assertThat(token)
.isValue("1")
.hasType(CxxTokenType.NUMBER));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public void init() {
@Test
void preprocessor_directives() {
var softly = new SoftAssertions();

softly.assertThat(lexer.lex("#")).anySatisfy(token -> assertThat(token).isValue(
"#").hasType(CxxTokenType.PREPROCESSOR));
softly.assertThat(lexer.lex("#include <iostream>")).anySatisfy(token -> assertThat(token).isValue(
"#include <iostream>").hasType(CxxTokenType.PREPROCESSOR));
softly.assertThat(lexer.lex("# include <iostream>")).anySatisfy(token -> assertThat(token).isValue(
Expand Down

0 comments on commit 5663bbb

Please sign in to comment.