Skip to content

Commit

Permalink
* Fix Parser prematurely expanding macros defined in class, `str…
Browse files Browse the repository at this point in the history
…uct` or `union` (issue #674)
  • Loading branch information
saudet committed Apr 23, 2023
1 parent bc24d34 commit be4df66
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Fix `Parser` prematurely expanding macros defined in `class`, `struct` or `union` ([issue #674](https://github.com/bytedeco/javacpp/issues/674))
* Add `Info.upcast` to support class hierarchies with virtual inheritance ([pull #671](https://github.com/bytedeco/javacpp/pull/671))
* Pick up `@Adapter`, `@SharedPtr`, etc annotations on `allocate()` as well ([pull #668](https://github.com/bytedeco/javacpp/pull/668))
* Provide `@Virtual(subclasses=false)` to prevent `Generator` from subclassing subclasses ([pull #660](https://github.com/bytedeco/javacpp/pull/660))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
@Documented @Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface NoException {
/** On override, indicates whether to use the C++ noexcept operator or not. */
/** On override, indicates whether to use the C++ noexcept specifier or not. */
boolean value() default false;
}
28 changes: 21 additions & 7 deletions src/main/java/org/bytedeco/javacpp/tools/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1969,7 +1969,7 @@ Attribute attribute(boolean explicit) {
return attr;
}

String body() {
String body() throws ParserException {
String text = "";
if (!tokens.get().match('{')) {
return null;
Expand All @@ -1978,6 +1978,11 @@ String body() {
int count = 1;
boolean catchBlock = false;
for (Token token = tokens.next(); !token.match(Token.EOF) && count > 0; token = tokens.next()) {
while (tokens.get().match('#')) {
// prevent premature macro expansion
macro(null, null);
token = tokens.get();
}
if (token.match('{')) {
if (catchBlock) {
catchBlock = false;
Expand Down Expand Up @@ -2888,9 +2893,11 @@ boolean macro(Context context, DeclarationList declList) throws ParserException
Token keyword = tokens.next();
if (keyword.spacing.indexOf('\n') >= 0) {
// empty macro?
Declaration decl = new Declaration();
decl.text = spacing + "// #";
declList.add(decl);
if (declList != null) {
Declaration decl = new Declaration();
decl.text = spacing + "// #";
declList.add(decl);
}
tokens.raw = false;
return true;
}
Expand All @@ -2903,6 +2910,11 @@ boolean macro(Context context, DeclarationList declList) throws ParserException
break;
}
}
if (context == null) {
// we just want to skip over the macro in this case
tokens.raw = false;
return true;
}
int endIndex = tokens.index;
while (tokens.get(-1).match(Token.COMMENT)) {
tokens.index--;
Expand Down Expand Up @@ -3119,9 +3131,11 @@ boolean macro(Context context, DeclarationList declList) throws ParserException
decl.text = comment + decl.text;
}
tokens.raw = false;
declList.spacing = spacing;
declList.add(decl);
declList.spacing = null;
if (declList != null) {
declList.spacing = spacing;
declList.add(decl);
declList.spacing = null;
}
return true;
}

Expand Down

0 comments on commit be4df66

Please sign in to comment.