1 /*
   2  * Copyright (c) 2016, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.clang;
  27 
  28 import java.foreign.NativeTypes;
  29 import java.foreign.Scope;
  30 import java.foreign.memory.LayoutType;
  31 import java.foreign.memory.Pointer;
  32 import java.io.IOException;
  33 import java.nio.file.Path;
  34 
  35 import clang.CXString.CXString;
  36 import clang.Index.CXDiagnostic;
  37 import clang.Index.CXToken;
  38 import clang.Index.CXTokenKind;
  39 import clang.Index.CXTranslationUnitImpl;
  40 
  41 public class TranslationUnit {
  42     private final Pointer<CXTranslationUnitImpl> tu;
  43     private final Scope scope = Scope.globalScope().fork();
  44 
  45     TranslationUnit(Pointer<CXTranslationUnitImpl> tu) {
  46         this.tu = tu;
  47     }
  48 
  49     public Cursor getCursor() {
  50         return new Cursor(LibClang.lib.clang_getTranslationUnitCursor(tu));
  51     }
  52 
  53     public Diagnostic[] getDiagnostics() {
  54         final clang.Index lclang = LibClang.lib;
  55 
  56         int cntDiags = lclang.clang_getNumDiagnostics(tu);
  57         if (cntDiags == 0) {
  58             return null;
  59         }
  60 
  61         Diagnostic[] rv = new Diagnostic[cntDiags];
  62         for (int i = 0; i < cntDiags; i++) {
  63             @CXDiagnostic Pointer<Void> diag = lclang.clang_getDiagnostic(tu, i);
  64             rv[i] = new Diagnostic(diag);
  65         }
  66 
  67         return rv;
  68     }
  69 
  70     public final void save(Path path) throws TranslationUnit.TranslationUnitSaveException {
  71         try (Scope sc = Scope.globalScope().fork()) {
  72             int res = LibClang.lib.clang_saveTranslationUnit(tu,
  73                     sc.allocateCString(path.toAbsolutePath().toString()), 0);
  74             if (res != 0) {
  75                 throw new TranslationUnit.TranslationUnitSaveException(path);
  76             }
  77         }
  78     }
  79 
  80     public String[] tokens(SourceRange range) {
  81         Tokens tokens = tokenize(range);
  82         String rv[] = new String[tokens.size()];
  83         for (int i = 0; i < rv.length; i++) {
  84             rv[i] = tokens.getToken(i).spelling();
  85         }
  86         return rv;
  87     }
  88 
  89     public Tokens tokenize(SourceRange range) {
  90         // FIXME: Use a shared scope now, we really want to use is StackScope
  91         Pointer<Pointer<CXToken>> p = scope.allocate(LayoutType.ofStruct(CXToken.class).pointer());
  92         Pointer<Integer> pCnt = scope.allocate(NativeTypes.UINT);
  93         LibClang.lib.clang_tokenize(tu, range.range, p, pCnt);
  94         Tokens rv = new Tokens(p.get().cast(LayoutType.ofStruct(CXToken.class)), pCnt.get());
  95         return rv;
  96     }
  97 
  98     public void dispose() {
  99         LibClang.lib.clang_disposeTranslationUnit(tu);
 100     }
 101 
 102     public class Tokens {
 103         private final Pointer<CXToken> ar;
 104         private final int size;
 105 
 106         Tokens(Pointer<CXToken> ar, int size) {
 107             this.ar = ar;
 108             this.size = size;
 109         }
 110 
 111         public void dispose() {
 112             LibClang.lib.clang_disposeTokens(tu, ar, size);
 113         }
 114 
 115         public int size() {
 116             return size;
 117         }
 118 
 119         public Token getToken(int idx) {
 120             Pointer<CXToken> p = ar.offset(idx);
 121             return new Token(p.get());
 122         }
 123 
 124         @Override
 125         public String toString() {
 126             StringBuilder sb = new StringBuilder();
 127             for (int i = 0; i < size; i++) {
 128                 CXString s = LibClang.lib.clang_getTokenSpelling(tu, ar.offset(i).get());
 129                 sb.append("Token[");
 130                 sb.append(i);
 131                 sb.append("]=");
 132                 sb.append(LibClang.CXStrToString(s));
 133                 sb.append("\n");
 134             }
 135             return sb.toString();
 136         }
 137     }
 138 
 139     public class Token {
 140         final CXToken token;
 141 
 142         Token(CXToken token) {
 143             this.token = token;
 144         }
 145 
 146         public @CXTokenKind int kind() {
 147             return LibClang.lib.clang_getTokenKind(token);
 148         }
 149 
 150         public String spelling() {
 151             CXString s = LibClang.lib.clang_getTokenSpelling(
 152                     tu, token);
 153             return LibClang.CXStrToString(s);
 154         }
 155 
 156         public SourceLocation getLocation() {
 157             return new SourceLocation(LibClang.lib.clang_getTokenLocation(
 158                         tu, token));
 159         }
 160 
 161         public SourceRange getExtent() {
 162             return new SourceRange(LibClang.lib.clang_getTokenExtent(
 163                         tu, token));
 164         }
 165     }
 166 
 167     public static class TranslationUnitSaveException extends IOException {
 168 
 169         static final long serialVersionUID = 1L;
 170 
 171         TranslationUnitSaveException(Path path) {
 172             super("Cannot save translation unit to: " + path.toAbsolutePath());
 173         }
 174     }
 175 }