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 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) { 50 try (Scope s = Scope.newNativeScope()) { 51 Pointer<Pointer<Void>> file = s.allocate(NativeTypes.VOID.pointer()); 52 Pointer<Integer> line = s.allocate(NativeTypes.INT); 53 Pointer<Integer> col = s.allocate(NativeTypes.INT); 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 }