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.Arrays;
  29 import java.util.HashMap;
  30 import java.util.List;
  31 import java.util.Map;
  32 import java.util.Objects;
  33 import java.util.Optional;
  34 import java.util.stream.Collectors;
  35 import com.sun.tools.jextract.parser.Parser;
  36 import com.sun.tools.jextract.tree.EnumTree;
  37 import com.sun.tools.jextract.tree.HeaderTree;
  38 import com.sun.tools.jextract.tree.SimpleTreeVisitor;
  39 import com.sun.tools.jextract.tree.StructTree;
  40 import com.sun.tools.jextract.tree.Tree;
  41 import com.sun.tools.jextract.tree.TreeMaker;
  42 import com.sun.tools.jextract.tree.TreePhase;
  43 import com.sun.tools.jextract.tree.TreePrinter;
  44 import jdk.internal.clang.Cursor;
  45 import jdk.internal.clang.SourceLocation;
  46 
  47 /**
  48  * Removes redundant declarations.
  49  */
  50 final class DuplicateDeclarationHandler extends SimpleTreeVisitor<Void, Void>
  51         implements TreePhase {
  52     private final TreeMaker treeMaker = new TreeMaker();
  53 
  54     // Potential Tree instances that will go into transformed HeaderTree
  55     // are collected in this list.
  56     private List<Tree> decls = new ArrayList<>();
  57 
  58     // declarations seen already
  59     private final Map<String, Tree> declarationsSeen = new HashMap<>();
  60     private void saveDeclaration(Tree t) {
  61         if (!t.name().isEmpty()) {
  62             declarationsSeen.put(t.name(), t);
  63         }
  64     }
  65 
  66     @Override
  67     public HeaderTree transform(HeaderTree ht) {
  68         // Process all header declarations are collect potential
  69         // declarations that will go into transformed HeaderTree
  70         // into the this.decls field.
  71         ht.accept(this, null);
  72 
  73         return treeMaker.createHeader(ht.cursor(), ht.path(), decls);
  74     }
  75 
  76     @Override
  77     public Void defaultAction(Tree tree, Void v) {
  78         if (!declarationsSeen.containsKey(tree.name())) {
  79             decls.add(tree);
  80         }
  81         return null;
  82     }
  83 
  84     @Override
  85     public Void visitEnum(EnumTree e, Void v) {
  86         defaultAction(e, v);
  87         if (!e.isDefinition()) {
  88             saveDeclaration(e); 
  89         }
  90         return null;
  91     }
  92 
  93     @Override
  94     public Void visitHeader(HeaderTree ht, Void v) {
  95         ht.declarations().forEach(decl -> decl.accept(this, null));
  96         return null;
  97     }
  98 
  99     @Override
 100     public Void visitStruct(StructTree s, Void v) {
 101         defaultAction(s, v);
 102         if (!s.isDefinition()) {
 103             saveDeclaration(s); 
 104         }
 105         return null;
 106     }
 107 
 108     // test main to manually check this visitor
 109     public static void main(String[] args) {
 110         if (args.length == 0) {
 111             System.err.println("Expected a header file");
 112             return;
 113         }
 114 
 115         Context context = new Context();
 116         Parser p = new Parser(context, true);
 117         List<Path> paths = Arrays.stream(args).map(Paths::get).collect(Collectors.toList());
 118         Path builtinInc = Paths.get(System.getProperty("java.home"), "conf", "jextract");
 119         List<String> clangArgs = List.of("-I" + builtinInc);
 120         List<HeaderTree> headers = p.parse(paths, clangArgs);
 121         TreePrinter printer = new TreePrinter();
 122         DuplicateDeclarationHandler handler = new DuplicateDeclarationHandler();
 123         for (HeaderTree ht : headers) {
 124             handler.transform(ht).accept(printer, null);
 125         }
 126     }
 127 }