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

Bugfix/preprocessor #379

Closed
wants to merge 1 commit into from
Closed
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 @@ -860,6 +860,22 @@ private Token predConcatToken(List<Token> tokens) {
while (!tokens.isEmpty()) {
Token last = tokens.remove(tokens.size() - 1);
if (last.getType() != WS) {
if ( !tokens.isEmpty() ) {
Token pred = tokens.get(tokens.size() - 1);
if (pred.getType() != WS && !pred.hasTrivia()) {
// Needed to paste tokens 0 and x back together after #define N(hex) 0x ## hex
tokens.remove(tokens.size() - 1);
String replacement = pred.getValue() + last.getValue();
last = Token.builder()
.setLine(pred.getLine())
.setColumn(pred.getColumn())
.setURI(pred.getURI())
.setValueAndOriginalValue(replacement)
.setType(pred.getType())
.setGeneratedCode(true)
.build();
}
}
return last;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,15 @@ public void preprocessorDirectives() {

@Test
public void hashhash_related_parsing_problem() {
// TODO: make it run.
// this reproduces a macros expansion problem where
// whitespace handling goes wrong

// assertThat(p).matches(
// "#define CASES CASE(00)\n"
// + "#define CASE(n) case 0x##n:\n"
// + "void foo() {\n"
// + "switch (1) {\n"
// + "CASES\n"
// + "break;\n"
// + "}\n"
// + "}\n");
assertThat(p).matches(
"#define CASES CASE(00)\n"
+ "#define CASE(n) case 0x##n:\n"
+ "void foo() {\n"
+ "switch (1) {\n"
+ "CASES\n"
+ "break;\n"
+ "}\n"
+ "}\n");
}

@Test
Expand Down Expand Up @@ -261,13 +257,11 @@ public void concatenation() {
+ "macro_start"))
.equals("int main ( void ) ; EOF"));

// FIXME: this failes due to a bug in production code
// which rips apart the number '0xcf'
// assert (serialize(p.parse(
// "#define A B(cf)\n"
// + "#define B(n) 0x##n\n"
// + "i = A;"))
// .equals("i = 0xcf ; EOF"));
assert (serialize(p.parse(
"#define A B(cf)\n"
+ "#define B(n) 0x##n\n"
+ "i = A;"))
.equals("i = 0xcf ; EOF"));
}

@Test
Expand Down