59 60 // generate unique name for an empty name 61 private String generateName(Tree tree) { 62 return LayoutUtils.getName(tree); 63 } 64 65 @Override 66 public Tree defaultAction(Tree tree, Void v) { 67 return tree; 68 } 69 70 @Override 71 public Tree visitHeader(HeaderTree ht, Void v) { 72 List<Tree> decls = ht.declarations().stream(). 73 map(decl -> decl.accept(this, null)). 74 collect(Collectors.toList()); 75 return treeMaker.createHeader(ht.cursor(), ht.path(), decls); 76 } 77 78 @Override 79 public Tree visitStruct(StructTree s, Void v) { 80 // Common simple case. No nested names and no anonymous field names. 81 // We just need to check struct name itself is empty or not. 82 if (s.nestedTypes().isEmpty() && !hasAnonymousFields(s)) { 83 /* 84 * Examples: 85 * 86 * struct { int i } x; // global variable of anon. struct type 87 * void func(struct { int x; } p); // param of anon. struct type 88 */ 89 if (s.name().isEmpty()) { 90 return s.withName(generateName(s)); 91 } else { 92 // all fine with this struct 93 return s; 94 } 95 } else { 96 // handle all nested types 97 return renameRecursively(s); 98 } 99 } 100 101 // does the given struct has any anonymous (bit) field? 102 private boolean hasAnonymousFields(StructTree s) { 103 return s.fields().stream().map(f -> f.name().isEmpty()).findFirst().isPresent(); 104 } 105 106 private StructTree renameRecursively(StructTree s) { 107 List<Tree> newDecls = s.declarations().stream().map(decl -> { 108 if (decl instanceof StructTree) { 109 return renameRecursively((StructTree)decl); 110 } else if (decl instanceof FieldTree && decl.name().isEmpty()) { 111 /* 112 * Skip anonymous fields. This happens in the following case: 113 * 114 * struct { 115 * int :23; // anonymous bit field 116 * int x:9; 117 * } 118 */ 119 120 return null; 121 } else { 122 return decl; 123 } 124 }).filter(d -> d != null).collect(Collectors.toList()); 125 126 return s.withNameAndDecls(generateName(s), newDecls); 127 } 128 129 // test main to manually check this visitor 130 public static void main(String[] args) { 131 if (args.length == 0) { 132 System.err.println("Expected a header file"); 133 return; 134 } 135 136 Parser p = new Parser(true); 137 List<Path> paths = Arrays.stream(args).map(Paths::get).collect(Collectors.toList()); 138 Path builtinInc = Paths.get(System.getProperty("java.home"), "conf", "jextract"); 139 List<String> clangArgs = List.of("-I" + builtinInc); 140 List<HeaderTree> headers = p.parse(paths, clangArgs); 141 TreePrinter printer = new TreePrinter(); 142 EmptyNameHandler handler = new EmptyNameHandler(); 143 for (HeaderTree ht : headers) { 144 handler.transform(ht).accept(printer, null); 145 } 146 } 147 } | 59 60 // generate unique name for an empty name 61 private String generateName(Tree tree) { 62 return LayoutUtils.getName(tree); 63 } 64 65 @Override 66 public Tree defaultAction(Tree tree, Void v) { 67 return tree; 68 } 69 70 @Override 71 public Tree visitHeader(HeaderTree ht, Void v) { 72 List<Tree> decls = ht.declarations().stream(). 73 map(decl -> decl.accept(this, null)). 74 collect(Collectors.toList()); 75 return treeMaker.createHeader(ht.cursor(), ht.path(), decls); 76 } 77 78 @Override 79 public Tree visitField(FieldTree t, Void aVoid) { 80 if (t.name().isEmpty()) { 81 /* 82 * Skip anonymous fields. This happens in the following case: 83 * 84 * struct { 85 * int :23; // anonymous bit field 86 * int x:9; 87 * } 88 */ 89 90 return null; 91 } else { 92 return t; 93 } 94 } 95 96 @Override 97 public Tree visitStruct(StructTree s, Void v) { 98 List<Tree> newDecls = s.declarations().stream() 99 .map(decl -> decl.accept(this, null)) 100 .filter(d -> d != null) 101 .collect(Collectors.toList()); 102 103 return s.withNameAndDecls(generateName(s), newDecls); 104 } 105 106 // test main to manually check this visitor 107 public static void main(String[] args) { 108 if (args.length == 0) { 109 System.err.println("Expected a header file"); 110 return; 111 } 112 113 Context context = new Context(); 114 Parser p = new Parser(context,true); 115 List<Path> paths = Arrays.stream(args).map(Paths::get).collect(Collectors.toList()); 116 Path builtinInc = Paths.get(System.getProperty("java.home"), "conf", "jextract"); 117 List<String> clangArgs = List.of("-I" + builtinInc); 118 List<HeaderTree> headers = p.parse(paths, clangArgs); 119 TreePrinter printer = new TreePrinter(); 120 EmptyNameHandler handler = new EmptyNameHandler(); 121 for (HeaderTree ht : headers) { 122 handler.transform(ht).accept(printer, null); 123 } 124 } 125 } |