* \brief Identifies a specific source location within a translation
* unit.
*
- * Use clang_getInstantiationLocation() to map a source location to a
- * particular file, line, and column.
+ * Use clang_getInstantiationLocation() or clang_getSpellingLocation()
+ * to map a source location to a particular file, line, and column.
*/
typedef struct {
void *ptr_data[2];
* \brief Retrieve the file, line, column, and offset represented by
* the given source location.
*
+ * If the location refers into a macro instantiation, retrieves the
+ * location of the macro instantiation.
+ *
* \param location the location within a source file that will be decomposed
* into its parts.
*
unsigned *column,
unsigned *offset);
+/**
+ * \brief Retrieve the file, line, column, and offset represented by
+ * the given source location.
+ *
+ * If the location refers into a macro instantiation, return where the
+ * location was originally spelled in the source file.
+ *
+ * \param location the location within a source file that will be decomposed
+ * into its parts.
+ *
+ * \param file [out] if non-NULL, will be set to the file to which the given
+ * source location points.
+ *
+ * \param line [out] if non-NULL, will be set to the line to which the given
+ * source location points.
+ *
+ * \param column [out] if non-NULL, will be set to the column to which the given
+ * source location points.
+ *
+ * \param offset [out] if non-NULL, will be set to the offset into the
+ * buffer to which the given source location points.
+ */
+CINDEX_LINKAGE void clang_getSpellingLocation(CXSourceLocation location,
+ CXFile *file,
+ unsigned *line,
+ unsigned *column,
+ unsigned *offset);
+
/**
* \brief Retrieve a source location representing the first character within a
* source range.
printf(", ");
Loc = clang_getCursorLocation(Ovl);
- clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
+ clang_getSpellingLocation(Loc, 0, &line, &column, 0);
printf("%d:%d", line, column);
}
printf("]");
} else {
CXSourceLocation Loc = clang_getCursorLocation(Referenced);
- clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
+ clang_getSpellingLocation(Loc, 0, &line, &column, 0);
printf(":%d:%d", line, column);
}
}
if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
CXString Name = clang_getCursorSpelling(SpecializationOf);
- clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
+ clang_getSpellingLocation(Loc, 0, &line, &column, 0);
printf(" [Specialization of %s:%d:%d]",
clang_getCString(Name), line, column);
clang_disposeString(Name);
printf(" [Overrides ");
for (I = 0; I != num_overridden; ++I) {
CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
- clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
+ clang_getSpellingLocation(Loc, 0, &line, &column, 0);
if (I)
printf(", ");
printf("@%d:%d", line, column);
CXSourceLocation Loc = clang_getCursorLocation(Cursor);
CXString source;
CXFile file;
- clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
+ clang_getSpellingLocation(Loc, &file, 0, 0, 0);
source = clang_getFileName(file);
if (!clang_getCString(source)) {
clang_disposeString(source);
fprintf(stderr, "%s\n", clang_getCString(Msg));
clang_disposeString(Msg);
- clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
- &file, 0, 0, 0);
+ clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
+ &file, 0, 0, 0);
if (!file)
return;
CXSourceLocation end = clang_getRangeEnd(range);
unsigned start_line, start_column, end_line, end_column;
CXFile start_file, end_file;
- clang_getInstantiationLocation(start, &start_file, &start_line,
- &start_column, 0);
- clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0);
+ clang_getSpellingLocation(start, &start_file, &start_line,
+ &start_column, 0);
+ clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
if (clang_equalLocations(start, end)) {
/* Insertion. */
if (start_file == file)
CXFile begin_file, end_file;
unsigned begin_line, begin_column, end_line, end_column;
- clang_getInstantiationLocation(clang_getRangeStart(extent),
- &begin_file, &begin_line, &begin_column, 0);
- clang_getInstantiationLocation(clang_getRangeEnd(extent),
- &end_file, &end_line, &end_column, 0);
+ clang_getSpellingLocation(clang_getRangeStart(extent),
+ &begin_file, &begin_line, &begin_column, 0);
+ clang_getSpellingLocation(clang_getRangeEnd(extent),
+ &end_file, &end_line, &end_column, 0);
if (!begin_file || !end_file)
return;
if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
CXSourceLocation Loc = clang_getCursorLocation(Cursor);
unsigned line, column;
- clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
+ clang_getSpellingLocation(Loc, 0, &line, &column, 0);
printf("// %s: %s:%d:%d: ", FileCheckPrefix,
GetCursorSource(Cursor), line, column);
PrintCursor(Cursor);
curColumn++;
Loc = clang_getCursorLocation(Cursor);
- clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
+ clang_getSpellingLocation(Loc, &file, 0, 0, 0);
source = clang_getFileName(file);
if (clang_getCString(source)) {
for (i = 0; i < includeStackLen; ++i) {
CXFile includingFile;
unsigned line, column;
- clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
- &column, 0);
+ clang_getSpellingLocation(includeStack[i], &includingFile, &line,
+ &column, 0);
fname = clang_getFileName(includingFile);
printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
clang_disposeString(fname);
case CXToken_Literal: kind = "Literal"; break;
case CXToken_Comment: kind = "Comment"; break;
}
- clang_getInstantiationLocation(clang_getRangeStart(extent),
- 0, &start_line, &start_column, 0);
- clang_getInstantiationLocation(clang_getRangeEnd(extent),
- 0, &end_line, &end_column, 0);
+ clang_getSpellingLocation(clang_getRangeStart(extent),
+ 0, &start_line, &start_column, 0);
+ clang_getSpellingLocation(clang_getRangeEnd(extent),
+ 0, &end_line, &end_column, 0);
printf("%s: \"%s\" ", kind, clang_getCString(spelling));
PrintExtent(stdout, start_line, start_column, end_line, end_column);
if (!clang_isInvalid(cursors[i].kind)) {
const CharSourceRange &R) {
// We want the last character in this location, so we will adjust the
// location accordingly.
- // FIXME: How do do this with a macro instantiation location?
SourceLocation EndLoc = R.getEnd();
+ if (EndLoc.isValid() && EndLoc.isMacroID())
+ EndLoc = SM.getSpellingLoc(EndLoc);
if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) {
unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts);
EndLoc = EndLoc.getFileLocWithOffset(Length);
*offset = SM.getDecomposedLoc(InstLoc).second;
}
+void clang_getSpellingLocation(CXSourceLocation location,
+ CXFile *file,
+ unsigned *line,
+ unsigned *column,
+ unsigned *offset) {
+ SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
+
+ if (!location.ptr_data[0] || Loc.isInvalid()) {
+ if (file)
+ *file = 0;
+ if (line)
+ *line = 0;
+ if (column)
+ *column = 0;
+ if (offset)
+ *offset = 0;
+ return;
+ }
+
+ const SourceManager &SM =
+ *static_cast<const SourceManager*>(location.ptr_data[0]);
+ SourceLocation SpellLoc = SM.getSpellingLoc(Loc);
+ std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc);
+ FileID FID = LocInfo.first;
+ unsigned FileOffset = LocInfo.second;
+
+ if (file)
+ *file = (void *)SM.getFileEntryForID(FID);
+ if (line)
+ *line = SM.getLineNumber(FID, FileOffset);
+ if (column)
+ *column = SM.getColumnNumber(FID, FileOffset);
+ if (offset)
+ *offset = FileOffset;
+}
+
CXSourceLocation clang_getRangeStart(CXSourceRange range) {
CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
range.begin_int_data };
// and source ranges.
CXFile File;
unsigned Line, Column;
- clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
- &File, &Line, &Column, 0);
+ clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
+ &File, &Line, &Column, 0);
if (File) {
CXString FName = clang_getFileName(File);
Out << clang_getCString(FName) << ":" << Line << ":";
CXSourceRange Range = clang_getDiagnosticRange(Diagnostic, I);
unsigned StartLine, StartColumn, EndLine, EndColumn;
- clang_getInstantiationLocation(clang_getRangeStart(Range),
- &StartFile, &StartLine, &StartColumn,
- 0);
- clang_getInstantiationLocation(clang_getRangeEnd(Range),
- &EndFile, &EndLine, &EndColumn, 0);
+ clang_getSpellingLocation(clang_getRangeStart(Range),
+ &StartFile, &StartLine, &StartColumn,
+ 0);
+ clang_getSpellingLocation(clang_getRangeEnd(Range),
+ &EndFile, &EndLine, &EndColumn, 0);
if (StartFile != EndFile || StartFile != File)
continue;