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