< prev index next >

test/jdk/com/sun/tools/jextract/jclang-ffi/src/jdk/internal/clang/Index.java

Print this page




  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 




  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_h.CXTranslationUnitImpl;
  34 import clang.Index_h.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_h 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_h 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 


< prev index next >