< prev index next >

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

Print this page




  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 jdk.internal.clang;
  24 
  25 import java.foreign.NativeTypes;
  26 import java.foreign.Scope;
  27 import java.foreign.memory.Pointer;
  28 import java.nio.file.Path;
  29 import java.nio.file.Paths;

  30 
  31 import static clang.CXString.CXString;
  32 import static clang.Index.CXSourceLocation;
  33 
  34 public class SourceLocation {
  35 
  36     private final CXSourceLocation loc;
  37 
  38     SourceLocation(CXSourceLocation loc) {
  39         this.loc = loc;
  40     }
  41 
  42     @FunctionalInterface
  43     private interface LocationFactory {
  44         void get(CXSourceLocation loc, Pointer<Pointer<Void>> file,
  45                  Pointer<Integer> line, Pointer<Integer> column, Pointer<Integer> offset);
  46     }
  47 
  48     @SuppressWarnings("unchecked")
  49     private Location getLocation(LocationFactory fn) {


  54             Pointer<Integer> offset = s.allocate(NativeTypes.INT);
  55 
  56             fn.get(loc, file, line, col, offset);
  57             CXString fname = LibClang.lib.clang_getFileName(file.get());
  58             return new Location(LibClang.CXStrToString(fname), line.get(),
  59                 col.get(), offset.get());
  60         }
  61     }
  62 
  63     public Location getFileLocation() { return getLocation(LibClang.lib::clang_getFileLocation); }
  64     public Location getExpansionLocation() { return getLocation(LibClang.lib::clang_getExpansionLocation); }
  65     public Location getSpellingLocation() { return getLocation(LibClang.lib::clang_getSpellingLocation); }
  66     public boolean isInSystemHeader() {
  67         return LibClang.lib.clang_Location_isInSystemHeader(loc) != 0;
  68     }
  69 
  70     public boolean isFromMainFile() {
  71         return LibClang.lib.clang_Location_isFromMainFile(loc) != 0;
  72     }
  73 

















  74     public final static class Location {
  75         private final Path path;
  76         private final int line;
  77         private final int column;
  78         private final int offset;
  79 
  80         private Location(String filename, int line, int column, int offset) {
  81             if (filename == null || filename.isEmpty()) {
  82                 this.path = null;
  83             } else {
  84                 this.path = Paths.get(filename);
  85             }
  86 
  87             this.line = line;
  88             this.column = column;
  89             this.offset = offset;
  90         }
  91 
  92         public Path path() {
  93             return path;
  94         }
  95 
  96         public int line() {
  97             return line;
  98         }
  99 
 100         public int column() {
 101             return column;
 102         }
 103 
 104         public int offset() {
 105             return offset;
 106         }
























 107     }
 108 }


  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 jdk.internal.clang;
  24 
  25 import java.foreign.NativeTypes;
  26 import java.foreign.Scope;
  27 import java.foreign.memory.Pointer;
  28 import java.nio.file.Path;
  29 import java.nio.file.Paths;
  30 import java.util.Objects;
  31 
  32 import static clang.CXString.CXString;
  33 import static clang.Index.CXSourceLocation;
  34 
  35 public class SourceLocation {
  36 
  37     private final CXSourceLocation loc;
  38 
  39     SourceLocation(CXSourceLocation loc) {
  40         this.loc = loc;
  41     }
  42 
  43     @FunctionalInterface
  44     private interface LocationFactory {
  45         void get(CXSourceLocation loc, Pointer<Pointer<Void>> file,
  46                  Pointer<Integer> line, Pointer<Integer> column, Pointer<Integer> offset);
  47     }
  48 
  49     @SuppressWarnings("unchecked")
  50     private Location getLocation(LocationFactory fn) {


  55             Pointer<Integer> offset = s.allocate(NativeTypes.INT);
  56 
  57             fn.get(loc, file, line, col, offset);
  58             CXString fname = LibClang.lib.clang_getFileName(file.get());
  59             return new Location(LibClang.CXStrToString(fname), line.get(),
  60                 col.get(), offset.get());
  61         }
  62     }
  63 
  64     public Location getFileLocation() { return getLocation(LibClang.lib::clang_getFileLocation); }
  65     public Location getExpansionLocation() { return getLocation(LibClang.lib::clang_getExpansionLocation); }
  66     public Location getSpellingLocation() { return getLocation(LibClang.lib::clang_getSpellingLocation); }
  67     public boolean isInSystemHeader() {
  68         return LibClang.lib.clang_Location_isInSystemHeader(loc) != 0;
  69     }
  70 
  71     public boolean isFromMainFile() {
  72         return LibClang.lib.clang_Location_isFromMainFile(loc) != 0;
  73     }
  74 
  75     @Override
  76     public boolean equals(Object other) {
  77         if (this == other) {
  78             return true;
  79         }
  80         if (!(other instanceof SourceLocation)) {
  81             return false;
  82         }
  83         SourceLocation sloc = (SourceLocation)other;
  84         return Objects.equals(getFileLocation(), sloc.getFileLocation());
  85     }
  86 
  87     @Override
  88     public int hashCode() {
  89         return getFileLocation().hashCode();
  90     }
  91 
  92     public final static class Location {
  93         private final Path path;
  94         private final int line;
  95         private final int column;
  96         private final int offset;
  97 
  98         private Location(String filename, int line, int column, int offset) {
  99             if (filename == null || filename.isEmpty()) {
 100                 this.path = null;
 101             } else {
 102                 this.path = Paths.get(filename);
 103             }
 104 
 105             this.line = line;
 106             this.column = column;
 107             this.offset = offset;
 108         }
 109 
 110         public Path path() {
 111             return path;
 112         }
 113 
 114         public int line() {
 115             return line;
 116         }
 117 
 118         public int column() {
 119             return column;
 120         }
 121 
 122         public int offset() {
 123             return offset;
 124         }
 125 
 126         @Override
 127         public boolean equals(Object other) {
 128             if (this == other) {
 129                 return true;
 130             }
 131             if (!(other instanceof Location)) {
 132                 return false;
 133             }
 134             Location loc = (Location)other;
 135             return Objects.equals(path, loc.path) &&
 136                 line == loc.line && column == loc.column &&
 137                 offset == loc.offset;
 138         }
 139 
 140         @Override
 141         public int hashCode() {
 142             return Objects.hashCode(path) ^ line ^ column ^ offset;
 143         }
 144 
 145         @Override
 146         public String toString() {
 147             return Objects.toString(path) + ":" + line + ":" + column + ":" + offset;
 148         }
 149     }
 150 }
< prev index next >