1*12c85518Srobert /*===-- clang-c/CXSourceLocation.h - C Index Source Location ------*- C -*-===*\ 2*12c85518Srobert |* *| 3*12c85518Srobert |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *| 4*12c85518Srobert |* Exceptions. *| 5*12c85518Srobert |* See https://llvm.org/LICENSE.txt for license information. *| 6*12c85518Srobert |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *| 7*12c85518Srobert |* *| 8*12c85518Srobert |*===----------------------------------------------------------------------===*| 9*12c85518Srobert |* *| 10*12c85518Srobert |* This header provides the interface to C Index source locations. *| 11*12c85518Srobert |* *| 12*12c85518Srobert \*===----------------------------------------------------------------------===*/ 13*12c85518Srobert 14*12c85518Srobert #ifndef LLVM_CLANG_C_CXSOURCE_LOCATION_H 15*12c85518Srobert #define LLVM_CLANG_C_CXSOURCE_LOCATION_H 16*12c85518Srobert 17*12c85518Srobert #include "clang-c/CXFile.h" 18*12c85518Srobert #include "clang-c/CXString.h" 19*12c85518Srobert #include "clang-c/ExternC.h" 20*12c85518Srobert #include "clang-c/Platform.h" 21*12c85518Srobert 22*12c85518Srobert LLVM_CLANG_C_EXTERN_C_BEGIN 23*12c85518Srobert 24*12c85518Srobert /** 25*12c85518Srobert * \defgroup CINDEX_LOCATIONS Physical source locations 26*12c85518Srobert * 27*12c85518Srobert * Clang represents physical source locations in its abstract syntax tree in 28*12c85518Srobert * great detail, with file, line, and column information for the majority of 29*12c85518Srobert * the tokens parsed in the source code. These data types and functions are 30*12c85518Srobert * used to represent source location information, either for a particular 31*12c85518Srobert * point in the program or for a range of points in the program, and extract 32*12c85518Srobert * specific location information from those data types. 33*12c85518Srobert * 34*12c85518Srobert * @{ 35*12c85518Srobert */ 36*12c85518Srobert 37*12c85518Srobert /** 38*12c85518Srobert * Identifies a specific source location within a translation 39*12c85518Srobert * unit. 40*12c85518Srobert * 41*12c85518Srobert * Use clang_getExpansionLocation() or clang_getSpellingLocation() 42*12c85518Srobert * to map a source location to a particular file, line, and column. 43*12c85518Srobert */ 44*12c85518Srobert typedef struct { 45*12c85518Srobert const void *ptr_data[2]; 46*12c85518Srobert unsigned int_data; 47*12c85518Srobert } CXSourceLocation; 48*12c85518Srobert 49*12c85518Srobert /** 50*12c85518Srobert * Identifies a half-open character range in the source code. 51*12c85518Srobert * 52*12c85518Srobert * Use clang_getRangeStart() and clang_getRangeEnd() to retrieve the 53*12c85518Srobert * starting and end locations from a source range, respectively. 54*12c85518Srobert */ 55*12c85518Srobert typedef struct { 56*12c85518Srobert const void *ptr_data[2]; 57*12c85518Srobert unsigned begin_int_data; 58*12c85518Srobert unsigned end_int_data; 59*12c85518Srobert } CXSourceRange; 60*12c85518Srobert 61*12c85518Srobert /** 62*12c85518Srobert * Retrieve a NULL (invalid) source location. 63*12c85518Srobert */ 64*12c85518Srobert CINDEX_LINKAGE CXSourceLocation clang_getNullLocation(void); 65*12c85518Srobert 66*12c85518Srobert /** 67*12c85518Srobert * Determine whether two source locations, which must refer into 68*12c85518Srobert * the same translation unit, refer to exactly the same point in the source 69*12c85518Srobert * code. 70*12c85518Srobert * 71*12c85518Srobert * \returns non-zero if the source locations refer to the same location, zero 72*12c85518Srobert * if they refer to different locations. 73*12c85518Srobert */ 74*12c85518Srobert CINDEX_LINKAGE unsigned clang_equalLocations(CXSourceLocation loc1, 75*12c85518Srobert CXSourceLocation loc2); 76*12c85518Srobert 77*12c85518Srobert /** 78*12c85518Srobert * Returns non-zero if the given source location is in a system header. 79*12c85518Srobert */ 80*12c85518Srobert CINDEX_LINKAGE int clang_Location_isInSystemHeader(CXSourceLocation location); 81*12c85518Srobert 82*12c85518Srobert /** 83*12c85518Srobert * Returns non-zero if the given source location is in the main file of 84*12c85518Srobert * the corresponding translation unit. 85*12c85518Srobert */ 86*12c85518Srobert CINDEX_LINKAGE int clang_Location_isFromMainFile(CXSourceLocation location); 87*12c85518Srobert 88*12c85518Srobert /** 89*12c85518Srobert * Retrieve a NULL (invalid) source range. 90*12c85518Srobert */ 91*12c85518Srobert CINDEX_LINKAGE CXSourceRange clang_getNullRange(void); 92*12c85518Srobert 93*12c85518Srobert /** 94*12c85518Srobert * Retrieve a source range given the beginning and ending source 95*12c85518Srobert * locations. 96*12c85518Srobert */ 97*12c85518Srobert CINDEX_LINKAGE CXSourceRange clang_getRange(CXSourceLocation begin, 98*12c85518Srobert CXSourceLocation end); 99*12c85518Srobert 100*12c85518Srobert /** 101*12c85518Srobert * Determine whether two ranges are equivalent. 102*12c85518Srobert * 103*12c85518Srobert * \returns non-zero if the ranges are the same, zero if they differ. 104*12c85518Srobert */ 105*12c85518Srobert CINDEX_LINKAGE unsigned clang_equalRanges(CXSourceRange range1, 106*12c85518Srobert CXSourceRange range2); 107*12c85518Srobert 108*12c85518Srobert /** 109*12c85518Srobert * Returns non-zero if \p range is null. 110*12c85518Srobert */ 111*12c85518Srobert CINDEX_LINKAGE int clang_Range_isNull(CXSourceRange range); 112*12c85518Srobert 113*12c85518Srobert /** 114*12c85518Srobert * Retrieve the file, line, column, and offset represented by 115*12c85518Srobert * the given source location. 116*12c85518Srobert * 117*12c85518Srobert * If the location refers into a macro expansion, retrieves the 118*12c85518Srobert * location of the macro expansion. 119*12c85518Srobert * 120*12c85518Srobert * \param location the location within a source file that will be decomposed 121*12c85518Srobert * into its parts. 122*12c85518Srobert * 123*12c85518Srobert * \param file [out] if non-NULL, will be set to the file to which the given 124*12c85518Srobert * source location points. 125*12c85518Srobert * 126*12c85518Srobert * \param line [out] if non-NULL, will be set to the line to which the given 127*12c85518Srobert * source location points. 128*12c85518Srobert * 129*12c85518Srobert * \param column [out] if non-NULL, will be set to the column to which the given 130*12c85518Srobert * source location points. 131*12c85518Srobert * 132*12c85518Srobert * \param offset [out] if non-NULL, will be set to the offset into the 133*12c85518Srobert * buffer to which the given source location points. 134*12c85518Srobert */ 135*12c85518Srobert CINDEX_LINKAGE void clang_getExpansionLocation(CXSourceLocation location, 136*12c85518Srobert CXFile *file, unsigned *line, 137*12c85518Srobert unsigned *column, 138*12c85518Srobert unsigned *offset); 139*12c85518Srobert 140*12c85518Srobert /** 141*12c85518Srobert * Retrieve the file, line and column represented by the given source 142*12c85518Srobert * location, as specified in a # line directive. 143*12c85518Srobert * 144*12c85518Srobert * Example: given the following source code in a file somefile.c 145*12c85518Srobert * 146*12c85518Srobert * \code 147*12c85518Srobert * #123 "dummy.c" 1 148*12c85518Srobert * 149*12c85518Srobert * static int func(void) 150*12c85518Srobert * { 151*12c85518Srobert * return 0; 152*12c85518Srobert * } 153*12c85518Srobert * \endcode 154*12c85518Srobert * 155*12c85518Srobert * the location information returned by this function would be 156*12c85518Srobert * 157*12c85518Srobert * File: dummy.c Line: 124 Column: 12 158*12c85518Srobert * 159*12c85518Srobert * whereas clang_getExpansionLocation would have returned 160*12c85518Srobert * 161*12c85518Srobert * File: somefile.c Line: 3 Column: 12 162*12c85518Srobert * 163*12c85518Srobert * \param location the location within a source file that will be decomposed 164*12c85518Srobert * into its parts. 165*12c85518Srobert * 166*12c85518Srobert * \param filename [out] if non-NULL, will be set to the filename of the 167*12c85518Srobert * source location. Note that filenames returned will be for "virtual" files, 168*12c85518Srobert * which don't necessarily exist on the machine running clang - e.g. when 169*12c85518Srobert * parsing preprocessed output obtained from a different environment. If 170*12c85518Srobert * a non-NULL value is passed in, remember to dispose of the returned value 171*12c85518Srobert * using \c clang_disposeString() once you've finished with it. For an invalid 172*12c85518Srobert * source location, an empty string is returned. 173*12c85518Srobert * 174*12c85518Srobert * \param line [out] if non-NULL, will be set to the line number of the 175*12c85518Srobert * source location. For an invalid source location, zero is returned. 176*12c85518Srobert * 177*12c85518Srobert * \param column [out] if non-NULL, will be set to the column number of the 178*12c85518Srobert * source location. For an invalid source location, zero is returned. 179*12c85518Srobert */ 180*12c85518Srobert CINDEX_LINKAGE void clang_getPresumedLocation(CXSourceLocation location, 181*12c85518Srobert CXString *filename, 182*12c85518Srobert unsigned *line, unsigned *column); 183*12c85518Srobert 184*12c85518Srobert /** 185*12c85518Srobert * Legacy API to retrieve the file, line, column, and offset represented 186*12c85518Srobert * by the given source location. 187*12c85518Srobert * 188*12c85518Srobert * This interface has been replaced by the newer interface 189*12c85518Srobert * #clang_getExpansionLocation(). See that interface's documentation for 190*12c85518Srobert * details. 191*12c85518Srobert */ 192*12c85518Srobert CINDEX_LINKAGE void clang_getInstantiationLocation(CXSourceLocation location, 193*12c85518Srobert CXFile *file, unsigned *line, 194*12c85518Srobert unsigned *column, 195*12c85518Srobert unsigned *offset); 196*12c85518Srobert 197*12c85518Srobert /** 198*12c85518Srobert * Retrieve the file, line, column, and offset represented by 199*12c85518Srobert * the given source location. 200*12c85518Srobert * 201*12c85518Srobert * If the location refers into a macro instantiation, return where the 202*12c85518Srobert * location was originally spelled in the source file. 203*12c85518Srobert * 204*12c85518Srobert * \param location the location within a source file that will be decomposed 205*12c85518Srobert * into its parts. 206*12c85518Srobert * 207*12c85518Srobert * \param file [out] if non-NULL, will be set to the file to which the given 208*12c85518Srobert * source location points. 209*12c85518Srobert * 210*12c85518Srobert * \param line [out] if non-NULL, will be set to the line to which the given 211*12c85518Srobert * source location points. 212*12c85518Srobert * 213*12c85518Srobert * \param column [out] if non-NULL, will be set to the column to which the given 214*12c85518Srobert * source location points. 215*12c85518Srobert * 216*12c85518Srobert * \param offset [out] if non-NULL, will be set to the offset into the 217*12c85518Srobert * buffer to which the given source location points. 218*12c85518Srobert */ 219*12c85518Srobert CINDEX_LINKAGE void clang_getSpellingLocation(CXSourceLocation location, 220*12c85518Srobert CXFile *file, unsigned *line, 221*12c85518Srobert unsigned *column, 222*12c85518Srobert unsigned *offset); 223*12c85518Srobert 224*12c85518Srobert /** 225*12c85518Srobert * Retrieve the file, line, column, and offset represented by 226*12c85518Srobert * the given source location. 227*12c85518Srobert * 228*12c85518Srobert * If the location refers into a macro expansion, return where the macro was 229*12c85518Srobert * expanded or where the macro argument was written, if the location points at 230*12c85518Srobert * a macro argument. 231*12c85518Srobert * 232*12c85518Srobert * \param location the location within a source file that will be decomposed 233*12c85518Srobert * into its parts. 234*12c85518Srobert * 235*12c85518Srobert * \param file [out] if non-NULL, will be set to the file to which the given 236*12c85518Srobert * source location points. 237*12c85518Srobert * 238*12c85518Srobert * \param line [out] if non-NULL, will be set to the line to which the given 239*12c85518Srobert * source location points. 240*12c85518Srobert * 241*12c85518Srobert * \param column [out] if non-NULL, will be set to the column to which the given 242*12c85518Srobert * source location points. 243*12c85518Srobert * 244*12c85518Srobert * \param offset [out] if non-NULL, will be set to the offset into the 245*12c85518Srobert * buffer to which the given source location points. 246*12c85518Srobert */ 247*12c85518Srobert CINDEX_LINKAGE void clang_getFileLocation(CXSourceLocation location, 248*12c85518Srobert CXFile *file, unsigned *line, 249*12c85518Srobert unsigned *column, unsigned *offset); 250*12c85518Srobert 251*12c85518Srobert /** 252*12c85518Srobert * Retrieve a source location representing the first character within a 253*12c85518Srobert * source range. 254*12c85518Srobert */ 255*12c85518Srobert CINDEX_LINKAGE CXSourceLocation clang_getRangeStart(CXSourceRange range); 256*12c85518Srobert 257*12c85518Srobert /** 258*12c85518Srobert * Retrieve a source location representing the last character within a 259*12c85518Srobert * source range. 260*12c85518Srobert */ 261*12c85518Srobert CINDEX_LINKAGE CXSourceLocation clang_getRangeEnd(CXSourceRange range); 262*12c85518Srobert 263*12c85518Srobert /** 264*12c85518Srobert * Identifies an array of ranges. 265*12c85518Srobert */ 266*12c85518Srobert typedef struct { 267*12c85518Srobert /** The number of ranges in the \c ranges array. */ 268*12c85518Srobert unsigned count; 269*12c85518Srobert /** 270*12c85518Srobert * An array of \c CXSourceRanges. 271*12c85518Srobert */ 272*12c85518Srobert CXSourceRange *ranges; 273*12c85518Srobert } CXSourceRangeList; 274*12c85518Srobert 275*12c85518Srobert /** 276*12c85518Srobert * Destroy the given \c CXSourceRangeList. 277*12c85518Srobert */ 278*12c85518Srobert CINDEX_LINKAGE void clang_disposeSourceRangeList(CXSourceRangeList *ranges); 279*12c85518Srobert 280*12c85518Srobert /** 281*12c85518Srobert * @} 282*12c85518Srobert */ 283*12c85518Srobert 284*12c85518Srobert LLVM_CLANG_C_EXTERN_C_END 285*12c85518Srobert 286*12c85518Srobert #endif 287