1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package com.sun.tools.jextract; 24 25 import java.nio.file.Path; 26 import java.nio.file.Paths; 27 import java.util.ArrayList; 28 import java.util.List; 29 import java.util.function.Predicate; 30 import java.util.stream.Collectors; 31 import com.sun.tools.jextract.parser.Parser; 32 import com.sun.tools.jextract.tree.FunctionTree; 33 import com.sun.tools.jextract.tree.HeaderTree; 34 import com.sun.tools.jextract.tree.MacroTree; 35 import com.sun.tools.jextract.tree.SimpleTreeVisitor; 36 import com.sun.tools.jextract.tree.Tree; 37 import com.sun.tools.jextract.tree.TreePhase; 38 import com.sun.tools.jextract.tree.VarTree; 39 import com.sun.tools.jextract.tree.TreeMaker; 40 import com.sun.tools.jextract.tree.TreePrinter; 41 42 /** 43 * This visitor filters variable, function, macro trees 44 * based on a Tree Predicate initialized. 45 */ 46 abstract class TreeFilter extends SimpleTreeVisitor<Tree, Void> 47 implements TreePhase { 48 private final TreeMaker treeMaker = new TreeMaker(); 49 50 private Tree filterTree(Tree tree) { 51 return filter(tree)? tree : null; 52 } 53 54 @Override 55 public HeaderTree transform(HeaderTree ht) { 56 return (HeaderTree)ht.accept(this, null); 57 } 58 59 @Override 60 public Tree defaultAction(Tree tree, Void v) { 61 return tree; 62 } 63 64 @Override 65 public Tree visitFunction(FunctionTree ft, Void v) { 66 return filterTree(ft); 67 } 68 69 @Override 70 public Tree visitMacro(MacroTree mt, Void v) { 71 return filterTree(mt); 72 } 73 74 @Override 75 public Tree visitHeader(HeaderTree ht, Void v) { 76 List<Tree> decls = ht.declarations().stream(). 77 map(decl -> decl.accept(this, null)). 78 filter(decl -> decl != null). 79 collect(Collectors.toList()); 80 return treeMaker.createHeader(ht.cursor(), ht.path(), decls); 81 } 82 83 @Override 84 public Tree visitVar(VarTree vt, Void v) { 85 return filterTree(vt); 86 } 87 88 abstract boolean filter(Tree tree); 89 90 // test main to manually check this visitor 91 // Usage: <header-file> [<regex-for-symbols-to-include>] 92 public static void main(String[] args) { 93 if (args.length == 0) { 94 System.err.println("Expected a header file"); 95 return; 96 } 97 98 Context context = new Context(); 99 Parser p = new Parser(context, true); 100 List<Path> paths = List.of(Paths.get(args[0])); 101 Path builtinInc = Paths.get(System.getProperty("java.home"), "conf", "jextract"); 102 List<String> clangArgs = List.of("-I" + builtinInc); 103 List<HeaderTree> headers = p.parse(paths, clangArgs); 104 TreePrinter printer = new TreePrinter(); 105 Predicate<Tree> nameFilter = args.length > 1? t->t.name().matches(args[1]) : t->true; 106 TreeFilter filter = new TreeFilter() { 107 @Override 108 boolean filter(Tree tree) { 109 return nameFilter.test(tree); 110 } 111 }; 112 for (HeaderTree ht : headers) { 113 filter.transform(ht).accept(printer, null); 114 } 115 } 116 } --- EOF ---