CXSourceLocation.cpp revision ad0d5707463cd7875e1ffd50dacd3f900d185aa1
1//===- CXSourceLocation.cpp - CXSourceLocations APIs ------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines routines for manipulating CXSourceLocations. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Frontend/ASTUnit.h" 15#include "CIndexer.h" 16#include "CLog.h" 17#include "CXLoadedDiagnostic.h" 18#include "CXSourceLocation.h" 19#include "CXString.h" 20#include "CXTranslationUnit.h" 21#include "llvm/Support/Compiler.h" 22#include "llvm/Support/Format.h" 23 24using namespace clang; 25using namespace clang::cxindex; 26 27//===----------------------------------------------------------------------===// 28// Internal predicates on CXSourceLocations. 29//===----------------------------------------------------------------------===// 30 31static bool isASTUnitSourceLocation(const CXSourceLocation &L) { 32 // If the lowest bit is clear then the first ptr_data entry is a SourceManager 33 // pointer, or the CXSourceLocation is a null location. 34 return ((uintptr_t)L.ptr_data[0] & 0x1) == 0; 35} 36 37//===----------------------------------------------------------------------===// 38// Basic construction and comparison of CXSourceLocations and CXSourceRanges. 39//===----------------------------------------------------------------------===// 40 41extern "C" { 42 43CXSourceLocation clang_getNullLocation() { 44 CXSourceLocation Result = { { 0, 0 }, 0 }; 45 return Result; 46} 47 48unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) { 49 return (loc1.ptr_data[0] == loc2.ptr_data[0] && 50 loc1.ptr_data[1] == loc2.ptr_data[1] && 51 loc1.int_data == loc2.int_data); 52} 53 54CXSourceRange clang_getNullRange() { 55 CXSourceRange Result = { { 0, 0 }, 0, 0 }; 56 return Result; 57} 58 59CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) { 60 if (!isASTUnitSourceLocation(begin)) { 61 if (isASTUnitSourceLocation(end)) 62 return clang_getNullRange(); 63 CXSourceRange Result = { { begin.ptr_data[0], end.ptr_data[0] }, 0, 0 }; 64 return Result; 65 } 66 67 if (begin.ptr_data[0] != end.ptr_data[0] || 68 begin.ptr_data[1] != end.ptr_data[1]) 69 return clang_getNullRange(); 70 71 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] }, 72 begin.int_data, end.int_data }; 73 74 return Result; 75} 76 77unsigned clang_equalRanges(CXSourceRange range1, CXSourceRange range2) { 78 return range1.ptr_data[0] == range2.ptr_data[0] 79 && range1.ptr_data[1] == range2.ptr_data[1] 80 && range1.begin_int_data == range2.begin_int_data 81 && range1.end_int_data == range2.end_int_data; 82} 83 84int clang_Range_isNull(CXSourceRange range) { 85 return clang_equalRanges(range, clang_getNullRange()); 86} 87 88 89CXSourceLocation clang_getRangeStart(CXSourceRange range) { 90 // Special decoding for CXSourceLocations for CXLoadedDiagnostics. 91 if ((uintptr_t)range.ptr_data[0] & 0x1) { 92 CXSourceLocation Result = { { range.ptr_data[0], 0 }, 0 }; 93 return Result; 94 } 95 96 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, 97 range.begin_int_data }; 98 return Result; 99} 100 101CXSourceLocation clang_getRangeEnd(CXSourceRange range) { 102 // Special decoding for CXSourceLocations for CXLoadedDiagnostics. 103 if ((uintptr_t)range.ptr_data[0] & 0x1) { 104 CXSourceLocation Result = { { range.ptr_data[1], 0 }, 0 }; 105 return Result; 106 } 107 108 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, 109 range.end_int_data }; 110 return Result; 111} 112 113} // end extern "C" 114 115//===----------------------------------------------------------------------===// 116// Getting CXSourceLocations and CXSourceRanges from a translation unit. 117//===----------------------------------------------------------------------===// 118 119extern "C" { 120 121CXSourceLocation clang_getLocation(CXTranslationUnit TU, 122 CXFile file, 123 unsigned line, 124 unsigned column) { 125 if (!TU || !file) 126 return clang_getNullLocation(); 127 128 LogRef Log = Logger::make(LLVM_FUNCTION_NAME); 129 ASTUnit *CXXUnit = cxtu::getASTUnit(TU); 130 ASTUnit::ConcurrencyCheck Check(*CXXUnit); 131 const FileEntry *File = static_cast<const FileEntry *>(file); 132 SourceLocation SLoc = CXXUnit->getLocation(File, line, column); 133 if (SLoc.isInvalid()) { 134 if (Log) 135 *Log << llvm::format("(\"%s\", %d, %d) = invalid", 136 File->getName(), line, column); 137 return clang_getNullLocation(); 138 } 139 140 CXSourceLocation CXLoc = 141 cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); 142 if (Log) 143 *Log << llvm::format("(\"%s\", %d, %d) = ", File->getName(), line, column) 144 << CXLoc; 145 146 return CXLoc; 147} 148 149CXSourceLocation clang_getLocationForOffset(CXTranslationUnit TU, 150 CXFile file, 151 unsigned offset) { 152 if (!TU || !file) 153 return clang_getNullLocation(); 154 155 ASTUnit *CXXUnit = cxtu::getASTUnit(TU); 156 157 SourceLocation SLoc 158 = CXXUnit->getLocation(static_cast<const FileEntry *>(file), offset); 159 160 if (SLoc.isInvalid()) 161 return clang_getNullLocation(); 162 163 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc); 164} 165 166} // end extern "C" 167 168//===----------------------------------------------------------------------===// 169// Routines for expanding and manipulating CXSourceLocations, regardless 170// of their origin. 171//===----------------------------------------------------------------------===// 172 173static void createNullLocation(CXFile *file, unsigned *line, 174 unsigned *column, unsigned *offset) { 175 if (file) 176 *file = 0; 177 if (line) 178 *line = 0; 179 if (column) 180 *column = 0; 181 if (offset) 182 *offset = 0; 183 return; 184} 185 186static void createNullLocation(CXString *filename, unsigned *line, 187 unsigned *column, unsigned *offset = 0) { 188 if (filename) 189 *filename = cxstring::createEmpty(); 190 if (line) 191 *line = 0; 192 if (column) 193 *column = 0; 194 if (offset) 195 *offset = 0; 196 return; 197} 198 199extern "C" { 200 201int clang_Location_isInSystemHeader(CXSourceLocation location) { 202 const SourceLocation Loc = 203 SourceLocation::getFromRawEncoding(location.int_data); 204 if (Loc.isInvalid()) 205 return 0; 206 207 const SourceManager &SM = 208 *static_cast<const SourceManager*>(location.ptr_data[0]); 209 return SM.isInSystemHeader(Loc); 210} 211 212int clang_Location_isFromMainFile(CXSourceLocation location) { 213 const SourceLocation Loc = 214 SourceLocation::getFromRawEncoding(location.int_data); 215 if (Loc.isInvalid()) 216 return 0; 217 218 const SourceManager &SM = 219 *static_cast<const SourceManager*>(location.ptr_data[0]); 220 return SM.isFromMainFile(Loc); 221} 222 223void clang_getExpansionLocation(CXSourceLocation location, 224 CXFile *file, 225 unsigned *line, 226 unsigned *column, 227 unsigned *offset) { 228 229 if (!isASTUnitSourceLocation(location)) { 230 CXLoadedDiagnostic::decodeLocation(location, file, line, column, offset); 231 return; 232 } 233 234 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); 235 236 if (!location.ptr_data[0] || Loc.isInvalid()) { 237 createNullLocation(file, line, column, offset); 238 return; 239 } 240 241 const SourceManager &SM = 242 *static_cast<const SourceManager*>(location.ptr_data[0]); 243 SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc); 244 245 // Check that the FileID is invalid on the expansion location. 246 // This can manifest in invalid code. 247 FileID fileID = SM.getFileID(ExpansionLoc); 248 bool Invalid = false; 249 const SrcMgr::SLocEntry &sloc = SM.getSLocEntry(fileID, &Invalid); 250 if (Invalid || !sloc.isFile()) { 251 createNullLocation(file, line, column, offset); 252 return; 253 } 254 255 if (file) 256 *file = const_cast<FileEntry *>(SM.getFileEntryForSLocEntry(sloc)); 257 if (line) 258 *line = SM.getExpansionLineNumber(ExpansionLoc); 259 if (column) 260 *column = SM.getExpansionColumnNumber(ExpansionLoc); 261 if (offset) 262 *offset = SM.getDecomposedLoc(ExpansionLoc).second; 263} 264 265void clang_getPresumedLocation(CXSourceLocation location, 266 CXString *filename, 267 unsigned *line, 268 unsigned *column) { 269 270 if (!isASTUnitSourceLocation(location)) { 271 // Other SourceLocation implementations do not support presumed locations 272 // at this time. 273 createNullLocation(filename, line, column); 274 return; 275 } 276 277 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); 278 279 if (!location.ptr_data[0] || Loc.isInvalid()) 280 createNullLocation(filename, line, column); 281 else { 282 const SourceManager &SM = 283 *static_cast<const SourceManager*>(location.ptr_data[0]); 284 PresumedLoc PreLoc = SM.getPresumedLoc(Loc); 285 286 if (filename) 287 *filename = cxstring::createRef(PreLoc.getFilename()); 288 if (line) 289 *line = PreLoc.getLine(); 290 if (column) 291 *column = PreLoc.getColumn(); 292 } 293} 294 295void clang_getInstantiationLocation(CXSourceLocation location, 296 CXFile *file, 297 unsigned *line, 298 unsigned *column, 299 unsigned *offset) { 300 // Redirect to new API. 301 clang_getExpansionLocation(location, file, line, column, offset); 302} 303 304void clang_getSpellingLocation(CXSourceLocation location, 305 CXFile *file, 306 unsigned *line, 307 unsigned *column, 308 unsigned *offset) { 309 310 if (!isASTUnitSourceLocation(location)) { 311 CXLoadedDiagnostic::decodeLocation(location, file, line, 312 column, offset); 313 return; 314 } 315 316 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); 317 318 if (!location.ptr_data[0] || Loc.isInvalid()) 319 return createNullLocation(file, line, column, offset); 320 321 const SourceManager &SM = 322 *static_cast<const SourceManager*>(location.ptr_data[0]); 323 // FIXME: This should call SourceManager::getSpellingLoc(). 324 SourceLocation SpellLoc = SM.getFileLoc(Loc); 325 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc); 326 FileID FID = LocInfo.first; 327 unsigned FileOffset = LocInfo.second; 328 329 if (FID.isInvalid()) 330 return createNullLocation(file, line, column, offset); 331 332 if (file) 333 *file = const_cast<FileEntry *>(SM.getFileEntryForID(FID)); 334 if (line) 335 *line = SM.getLineNumber(FID, FileOffset); 336 if (column) 337 *column = SM.getColumnNumber(FID, FileOffset); 338 if (offset) 339 *offset = FileOffset; 340} 341 342void clang_getFileLocation(CXSourceLocation location, 343 CXFile *file, 344 unsigned *line, 345 unsigned *column, 346 unsigned *offset) { 347 348 if (!isASTUnitSourceLocation(location)) { 349 CXLoadedDiagnostic::decodeLocation(location, file, line, 350 column, offset); 351 return; 352 } 353 354 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); 355 356 if (!location.ptr_data[0] || Loc.isInvalid()) 357 return createNullLocation(file, line, column, offset); 358 359 const SourceManager &SM = 360 *static_cast<const SourceManager*>(location.ptr_data[0]); 361 SourceLocation FileLoc = SM.getFileLoc(Loc); 362 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(FileLoc); 363 FileID FID = LocInfo.first; 364 unsigned FileOffset = LocInfo.second; 365 366 if (FID.isInvalid()) 367 return createNullLocation(file, line, column, offset); 368 369 if (file) 370 *file = const_cast<FileEntry *>(SM.getFileEntryForID(FID)); 371 if (line) 372 *line = SM.getLineNumber(FID, FileOffset); 373 if (column) 374 *column = SM.getColumnNumber(FID, FileOffset); 375 if (offset) 376 *offset = FileOffset; 377} 378 379} // end extern "C" 380