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 jdk.internal.clang;
  25 
  26 import java.foreign.NativeTypes;
  27 import java.foreign.Scope;
  28 import java.foreign.memory.Pointer;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 import java.util.function.Consumer;
  32 
  33 import clang.Index.CXTranslationUnitImpl;
  34 import clang.Index.CXDiagnostic;
  35 
  36 public class Index {
  37     // Pointer to CXIndex
  38     private final Pointer<Void> ptr;
  39     // Set of TranslationUnit
  40     public final List<Pointer<CXTranslationUnitImpl>> translationUnits;
  41 
  42     Index(Pointer<Void> ptr) {
  43         this.ptr = ptr;
  44         translationUnits = new ArrayList<>();
  45     }
  46 
  47     public TranslationUnit parseTU(String file, String... args) {
  48         final clang.Index lclang = LibClang.lib;
  49 
  50         try (Scope scope = Scope.globalScope().fork()) {
  51             Pointer<Byte> src = scope.allocateCString(file);
  52             Pointer<Pointer<Byte>> cargs = toCStrArray(scope, args);
  53             Pointer<CXTranslationUnitImpl> tu = lclang.clang_parseTranslationUnit(
  54                     ptr, src, cargs, args.length, null, 0,
  55                     LibClang.lib.CXTranslationUnit_DetailedPreprocessingRecord());
  56             return new TranslationUnit(tu);
  57         }
  58     }
  59 
  60     public Cursor parse(String file, Consumer<Diagnostic> eh, boolean detailedPreprocessorRecord, String... args) {
  61         final clang.Index lclang = LibClang.lib;
  62 
  63         try (Scope scope = Scope.globalScope().fork()) {
  64             Pointer<Byte> src = scope.allocateCString(file);
  65             Pointer<Pointer<Byte>> cargs = toCStrArray(scope, args);
  66             Pointer<CXTranslationUnitImpl> tu = lclang.clang_parseTranslationUnit(
  67                     ptr, src, cargs, args.length, Pointer.ofNull(), 0,
  68                     detailedPreprocessorRecord ?
  69                             LibClang.lib.CXTranslationUnit_DetailedPreprocessingRecord() :
  70                             LibClang.lib.CXTranslationUnit_None());
  71 
  72             if (tu != null && !tu.isNull()) {
  73                 translationUnits.add(tu);
  74             }
  75 
  76             int cntDiags = lclang.clang_getNumDiagnostics(tu);
  77             for (int i = 0; i < cntDiags; i++) {
  78                 @CXDiagnostic Pointer<Void> diag = lclang.clang_getDiagnostic(tu, i);
  79                 eh.accept(new Diagnostic(diag));
  80             }
  81 
  82             return new Cursor(lclang.clang_getTranslationUnitCursor(tu));
  83         }
  84     }
  85 
  86     public void dispose() {
  87         for (Pointer<CXTranslationUnitImpl> tu: translationUnits) {
  88             LibClang.lib.clang_disposeTranslationUnit(tu);
  89         }
  90         LibClang.lib.clang_disposeIndex(ptr);
  91     }
  92 
  93     private static Pointer<Pointer<Byte>> toCStrArray(Scope sc, String[] ar) {
  94         if (ar.length == 0) {
  95             return Pointer.ofNull();
  96         }
  97 
  98         Pointer<Pointer<Byte>> ptr = sc.allocate(NativeTypes.UINT8.pointer(), ar.length);
  99         for (int i = 0; i < ar.length; i++) {
 100             Pointer<Byte> s = sc.allocateCString(ar[i]);
 101             ptr.offset(i).set(s);
 102         }
 103 
 104         return ptr;
 105     }
 106 }