1 /*
   2  * Copyright (c) 2014, 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 
  24 package com.sun.tools.jextract.parser;
  25 
  26 import java.nio.file.Path;
  27 import java.nio.file.Paths;
  28 import java.util.Arrays;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 import java.util.Collection;
  32 import java.util.function.Predicate;
  33 
  34 import com.sun.tools.jextract.Context;
  35 import com.sun.tools.jextract.tree.Tree;
  36 import com.sun.tools.jextract.tree.HeaderTree;
  37 import com.sun.tools.jextract.tree.Printer;
  38 
  39 public class FindSymbol {
  40     public static void main(String[] args) {
  41         if (args.length == 0) {
  42             System.err.println("Expected a header file");
  43             return;
  44         }
  45 
  46         final List<Path> paths = List.of(Paths.get(args[0]));
  47         final Path builtinInc = Paths.get(System.getProperty("java.home"), "conf", "jextract");
  48         final List<String> clangArgs = List.of("-I" + builtinInc);
  49 
  50         Context context = new Context();
  51         final Parser parser = new Parser(context, true);
  52         final List<HeaderTree> headers = parser.parse(paths, clangArgs);
  53         final Printer p = new Printer();
  54         final HeaderTree tu = headers.get(0);
  55 
  56         if (args.length == 1) {
  57             p.printRecursive(tu, Integer.MAX_VALUE);
  58             return;
  59         }
  60 
  61         final Collection<String> set = Arrays.asList(Arrays.copyOfRange(args, 1, args.length));
  62         final Predicate<Tree> matchName = tree -> tree.isDeclaration() && set.contains(tree.name());
  63         tu.declarations().stream()
  64                 .filter(matchName)
  65                 .forEach(declTree -> p.printRecursive(declTree, Integer.MAX_VALUE));
  66     }
  67 }