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.VarTree;
  38 import com.sun.tools.jextract.tree.TreeMaker;
  39 import com.sun.tools.jextract.tree.TreePrinter;
  40 
  41 /**
  42  * This visitor filters variable, function, macro trees
  43  * based on a Tree Predicate initialized.
  44  */
  45 final class TreeFilter extends SimpleTreeVisitor<Tree, Void> {
  46     private final TreeMaker treeMaker = new TreeMaker();
  47     private final Predicate<Tree> filter;
  48 
  49     TreeFilter(Predicate<Tree> filter) {
  50         this.filter = filter;
  51     }
  52 
  53     private Tree filterTree(Tree tree) {
  54         return filter.test(tree)? tree : null;
  55     }
  56 
  57     HeaderTree transform(HeaderTree ht) {
  58         return (HeaderTree)ht.accept(this, null);
  59     }
  60 
  61     @Override
  62     public Tree defaultAction(Tree tree, Void v) {
  63         return tree;
  64     }
  65 
  66     @Override
  67     public Tree visitFunction(FunctionTree ft, Void v) {
  68         return filterTree(ft);
  69     }
  70 
  71     @Override
  72     public Tree visitMacro(MacroTree mt, Void v) {
  73         return filterTree(mt);
  74     }
  75 
  76     @Override
  77     public Tree visitHeader(HeaderTree ht, Void v) {
  78         List<Tree> decls =  ht.declarations().stream().
  79             map(decl -> decl.accept(this, null)).
  80             filter(decl -> decl != null).
  81             collect(Collectors.toList());
  82         return treeMaker.createHeader(ht.cursor(), ht.path(), decls);
  83     }
  84 
  85     @Override
  86     public Tree visitVar(VarTree vt, Void v) {
  87         return filterTree(vt);
  88     }
  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         Parser p = new Parser(true);
  99         List<Path> paths = List.of(Paths.get(args[0]));
 100         Path builtinInc = Paths.get(System.getProperty("java.home"), "conf", "jextract");
 101         List<String> clangArgs = List.of("-I" + builtinInc);
 102         List<HeaderTree> headers = p.parse(paths, clangArgs);
 103         TreePrinter printer = new TreePrinter();
 104         Predicate<Tree> nameFilter =  args.length > 1? t->t.name().matches(args[1]) : t->true;
 105         TreeFilter filter = new TreeFilter(nameFilter);
 106         for (HeaderTree ht : headers) {
 107             filter.transform(ht).accept(printer, null);
 108         }
 109     }
 110 }