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 final class TreeFilter extends SimpleTreeVisitor<Tree, Void>
  47         implements TreePhase {
  48     private final TreeMaker treeMaker = new TreeMaker();
  49     private final Predicate<Tree> filter;
  50 
  51     TreeFilter(Predicate<Tree> filter) {
  52         this.filter = filter;
  53     }
  54 
  55     private Tree filterTree(Tree tree) {
  56         return filter.test(tree)? tree : null;
  57     }
  58 
  59     @Override
  60     public HeaderTree transform(HeaderTree ht) {
  61         return (HeaderTree)ht.accept(this, null);
  62     }
  63 
  64     @Override
  65     public Tree defaultAction(Tree tree, Void v) {
  66         return tree;
  67     }
  68 
  69     @Override
  70     public Tree visitFunction(FunctionTree ft, Void v) {
  71         return filterTree(ft);
  72     }
  73 
  74     @Override
  75     public Tree visitMacro(MacroTree mt, Void v) {
  76         return filterTree(mt);
  77     }
  78 
  79     @Override
  80     public Tree visitHeader(HeaderTree ht, Void v) {
  81         List<Tree> decls =  ht.declarations().stream().
  82             map(decl -> decl.accept(this, null)).
  83             filter(decl -> decl != null).
  84             collect(Collectors.toList());
  85         return treeMaker.createHeader(ht.cursor(), ht.path(), decls);
  86     }
  87 
  88     @Override
  89     public Tree visitVar(VarTree vt, Void v) {
  90         return filterTree(vt);
  91     }
  92 
  93     // test main to manually check this visitor
  94     // Usage: <header-file> [<regex-for-symbols-to-include>]
  95     public static void main(String[] args) {
  96         if (args.length == 0) {
  97             System.err.println("Expected a header file");
  98             return;
  99         }
 100 
 101         Parser p = new Parser(true);
 102         List<Path> paths = List.of(Paths.get(args[0]));
 103         Path builtinInc = Paths.get(System.getProperty("java.home"), "conf", "jextract");
 104         List<String> clangArgs = List.of("-I" + builtinInc);
 105         List<HeaderTree> headers = p.parse(paths, clangArgs);
 106         TreePrinter printer = new TreePrinter();
 107         Predicate<Tree> nameFilter =  args.length > 1? t->t.name().matches(args[1]) : t->true;
 108         TreeFilter filter = new TreeFilter(nameFilter);
 109         for (HeaderTree ht : headers) {
 110             filter.transform(ht).accept(printer, null);
 111         }
 112     }
 113 }