< prev index next >

src/jdk.jextract/share/classes/com/sun/tools/jextract/EmptyNameHandler.java

Print this page

        

@@ -74,42 +74,12 @@
             collect(Collectors.toList());
         return treeMaker.createHeader(ht.cursor(), ht.path(), decls);
     }
 
     @Override
-    public Tree visitStruct(StructTree s, Void v) {
-        // Common simple case. No nested names and no anonymous field names.
-        // We just need to check struct name itself is empty or not.
-        if (s.nestedTypes().isEmpty() && !hasAnonymousFields(s)) {
-            /*
-             * Examples:
-             *
-             *   struct { int i } x; // global variable of anon. struct type
-             *   void func(struct { int x; } p); // param of anon. struct type
-             */
-            if (s.name().isEmpty()) {
-                return s.withName(generateName(s));
-            } else {
-                // all fine with this struct
-                return s;
-            }
-        } else {
-            // handle all nested types
-            return renameRecursively(s);
-        }
-    }
-
-    // does the given struct has any anonymous (bit) field?
-    private boolean hasAnonymousFields(StructTree s) {
-        return s.fields().stream().map(f -> f.name().isEmpty()).findFirst().isPresent();
-    }
-
-    private StructTree renameRecursively(StructTree s) {
-        List<Tree> newDecls = s.declarations().stream().map(decl -> {
-            if (decl instanceof StructTree) {
-                return renameRecursively((StructTree)decl);
-            } else if (decl instanceof FieldTree && decl.name().isEmpty()) {
+    public Tree visitField(FieldTree t, Void aVoid) {
+        if (t.name().isEmpty()) {
                 /*
                  * Skip anonymous fields. This happens in the following case:
                  *
                  * struct {
                  *    int  :23; // anonymous bit field

@@ -117,13 +87,20 @@
                  * }
                  */
 
                 return null;
             } else {
-                return decl;
+            return t;
             }
-        }).filter(d -> d != null).collect(Collectors.toList());
+    }
+
+    @Override
+    public Tree visitStruct(StructTree s, Void v) {
+        List<Tree> newDecls = s.declarations().stream()
+                .map(decl -> decl.accept(this, null))
+                .filter(d -> d != null)
+                .collect(Collectors.toList());
 
         return s.withNameAndDecls(generateName(s), newDecls);
     }
 
     // test main to manually check this visitor

@@ -131,11 +108,12 @@
         if (args.length == 0) {
             System.err.println("Expected a header file");
             return;
         }
 
-        Parser p = new Parser(true);
+        Context context = new Context();
+        Parser p = new Parser(context,true);
         List<Path> paths = Arrays.stream(args).map(Paths::get).collect(Collectors.toList());
         Path builtinInc = Paths.get(System.getProperty("java.home"), "conf", "jextract");
         List<String> clangArgs = List.of("-I" + builtinInc);
         List<HeaderTree> headers = p.parse(paths, clangArgs);
         TreePrinter printer = new TreePrinter();
< prev index next >