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