Skip to content

Commit c2f1359

Browse files
committed
7903198: Refine filtering for enums
Reviewed-by: sundar
1 parent ac00cb5 commit c2f1359

File tree

6 files changed

+42
-22
lines changed

6 files changed

+42
-22
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,18 @@ In other words, the `jextract` tool has generated all the required supporting co
126126

127127
The `jextract` tool includes several customization options. Users can select in which package the generated code should be emitted, and what the name of the main extracted class should be. A complete list of all the supported options is given below:
128128

129-
| Option | Meaning |
130-
| :----------------------------------------------------------- | ------------------------------------------------------------ |
131-
| `-D <macro>` | define a C preprocessor macro |
132-
| `--header-class-name <name>` | specify the name of the main header class |
133-
| `-t, --target-package <package>` | specify target package for the generated bindings |
134-
| `-I <path>` | specify include files path for the clang parser |
135-
| `-l <library>` | specify a library that will be loaded by the generated bindings |
136-
| `--output <path>` | specify where to place generated files |
137-
| `--source` | generate java sources instead of classfiles |
138-
| `--dump-includes <String>` | dump included symbols into specified file (see below) |
139-
| `--include-[function,macro,struct,union,typedef,var]<String>` | Include a symbol of the given name and kind in the generated bindings (see below). When one of these options is specified, any symbol that is not matched by any specified filters is omitted from the generated bindings. |
140-
| `--version` | print version information and exit |
129+
| Option | Meaning |
130+
|:-------------------------------------------------------------------| ------------------------------------------------------------ |
131+
| `-D <macro>` | define a C preprocessor macro |
132+
| `--header-class-name <name>` | specify the name of the main header class |
133+
| `-t, --target-package <package>` | specify target package for the generated bindings |
134+
| `-I <path>` | specify include files path for the clang parser |
135+
| `-l <library>` | specify a library that will be loaded by the generated bindings |
136+
| `--output <path>` | specify where to place generated files |
137+
| `--source` | generate java sources instead of classfiles |
138+
| `--dump-includes <String>` | dump included symbols into specified file (see below) |
139+
| `--include-[function,enum,macro,struct,union,typedef,var]<String>` | Include a symbol of the given name and kind in the generated bindings (see below). When one of these options is specified, any symbol that is not matched by any specified filters is omitted from the generated bindings. |
140+
| `--version` | print version information and exit |
141141

142142

143143
#### Additional clang options

src/main/java/org/openjdk/jextract/impl/IncludeHelper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public enum IncludeKind {
5151
FUNCTION,
5252
TYPEDEF,
5353
STRUCT,
54-
UNION;
54+
UNION,
55+
ENUM;
5556

5657
public String optionName() {
5758
return "include-" + name().toLowerCase();
@@ -77,6 +78,7 @@ static IncludeKind fromScoped(Declaration.Scoped scoped) {
7778
return switch (scoped.kind()) {
7879
case STRUCT -> IncludeKind.STRUCT;
7980
case UNION -> IncludeKind.UNION;
81+
case ENUM -> IncludeKind.ENUM;
8082
default -> throw new IllegalStateException("Cannot get here!");
8183
};
8284
}

src/main/java/org/openjdk/jextract/impl/OutputFactory.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,11 @@ private JavaFileObject jfoFromString(String pkgName, String clsName, String cont
170170

171171
@Override
172172
public Void visitConstant(Declaration.Constant constant, Declaration parent) {
173-
if (!constants.add(constant.name()) || !includeHelper.isIncluded(constant)) {
174-
//skip
173+
boolean isEnumConstant = parent instanceof Declaration.Scoped scoped &&
174+
scoped.kind() == Declaration.Scoped.Kind.ENUM;
175+
176+
if ((!isEnumConstant && !includeHelper.isIncluded(constant)) ||
177+
!constants.add(constant.name())) {
175178
return null;
176179
}
177180

@@ -196,16 +199,16 @@ public Void visitScoped(Declaration.Scoped d, Declaration parent) {
196199
case STRUCT, UNION -> true;
197200
default -> false;
198201
};
202+
String scopedName = d.name();
203+
if (!scopedName.isEmpty() && !includeHelper.isIncluded(d)) {
204+
return null;
205+
}
199206
StructBuilder structBuilder = null;
200207
if (isStructKind) {
201-
String className = d.name();
202-
if (!className.isEmpty() && !includeHelper.isIncluded(d)) {
203-
return null;
204-
}
205208
GroupLayout layout = (GroupLayout) layoutFor(d);
206-
currentBuilder = structBuilder = currentBuilder.addStruct(className, parent, layout, Type.declared(d));
209+
currentBuilder = structBuilder = currentBuilder.addStruct(scopedName, parent, layout, Type.declared(d));
207210
structBuilder.classBegin();
208-
if (!className.isEmpty()) {
211+
if (!scopedName.isEmpty()) {
209212
addStructDefinition(d, structBuilder.fullName());
210213
}
211214
}

src/main/resources/org/openjdk/jextract/impl/resources/Messages.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ help.include-function=name of function to include
3535
help.include-typedef=name of type definition to include
3636
help.include-struct=name of struct definition to include
3737
help.include-union=name of union definition to include
38+
help.include-enum=name of enum definition to include
3839
help.D=define a C preprocessor macro
3940
help.dump-includes=dump included symbols into specified file
4041
help.h=print help
@@ -56,6 +57,7 @@ Option Description \n\
5657
--dump-includes <file> dump included symbols into specified file \n\
5758
--header-class-name <name> name of the header class \n\
5859
--include-function <name> name of function to include \n\
60+
--include-enum <name> name of enum definition to include \n\
5961
--include-macro <name> name of constant macro to include \n\
6062
--include-struct <name> name of struct definition to include \n\
6163
--include-typedef <name> name of type definition to include \n\

test/testng/org/openjdk/jextract/test/toolprovider/TestFilters.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import testlib.JextractToolRunner;
2929

3030
import java.io.IOException;
31+
import java.lang.reflect.Method;
3132
import java.nio.file.Files;
3233
import java.nio.file.Path;
3334
import java.util.Iterator;
@@ -92,7 +93,8 @@ enum FilterKind {
9293
CONSTANT("_constant", "--include-macro"),
9394
TYPEDEF("_typedef", "--include-typedef"),
9495
STRUCT("_struct", "--include-struct"),
95-
UNION("_union", "--include-union");
96+
UNION("_union", "--include-union"),
97+
ENUM("_enum", "--include-enum");
9698

9799
final String symbolName;
98100
final String filterOption;
@@ -114,6 +116,15 @@ Object get(Class<?> headerClass) {
114116
yield null;
115117
}
116118
}
119+
case ENUM -> {
120+
String[] constantNames = { "one", "two", "three" };
121+
Method method = null;
122+
for (String c : constantNames) {
123+
method = findMethod(headerClass, c);
124+
if (method == null) break;
125+
}
126+
yield method;
127+
}
117128
};
118129
}
119130
}

test/testng/org/openjdk/jextract/test/toolprovider/filters.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ struct _struct { int x; };
4343

4444
union _union { int y; };
4545

46+
enum _enum { one = 1, two = 2, three = 3 };
47+
4648
#ifdef __cplusplus
4749
}
4850
#endif // __cplusplus

0 commit comments

Comments
 (0)