Yet more CIndex API cleanup:
- Added more routines to manipulate/compare source locations and ranges
- Switched clang_getCursor() over to take a CXSourceLocation rather
than file/line/column.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94226 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp
index 78fdace..8ce3db6 100644
--- a/tools/CIndex/CIndex.cpp
+++ b/tools/CIndex/CIndex.cpp
@@ -134,6 +134,10 @@
return Result;
}
+static SourceLocation translateSourceLocation(CXSourceLocation L) {
+ return SourceLocation::getFromRawEncoding(L.int_data);
+}
+
static SourceRange translateSourceRange(CXSourceRange R) {
return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data),
SourceLocation::getFromRawEncoding(R.end_int_data));
@@ -996,6 +1000,42 @@
// CXSourceLocation and CXSourceRange Operations.
//===----------------------------------------------------------------------===//
+extern "C" {
+CXSourceLocation clang_getNullLocation() {
+ CXSourceLocation Result = { 0, 0 };
+ return Result;
+}
+
+unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
+ return loc1.ptr_data == loc2.ptr_data && loc1.int_data == loc2.int_data;
+}
+
+CXSourceLocation clang_getLocation(CXTranslationUnit tu,
+ CXFile file,
+ unsigned line,
+ unsigned column) {
+ if (!tu)
+ return clang_getNullLocation();
+
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
+ SourceLocation SLoc
+ = CXXUnit->getSourceManager().getLocation(
+ static_cast<const FileEntry *>(file),
+ line, column);
+
+ return translateSourceLocation(CXXUnit->getASTContext(), SLoc, false);
+}
+
+CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
+ if (begin.ptr_data != end.ptr_data) {
+ CXSourceRange Result = { 0, 0, 0 };
+ return Result;
+ }
+
+ CXSourceRange Result = { begin.ptr_data, begin.int_data, end.int_data };
+ return Result;
+}
+
void clang_getInstantiationLocation(CXSourceLocation location,
CXFile *file,
unsigned *line,
@@ -1066,6 +1106,8 @@
return Result;
}
+} // end: extern "C"
+
//===----------------------------------------------------------------------===//
// CXFile Operations.
//===----------------------------------------------------------------------===//
@@ -1088,6 +1130,18 @@
FileEntry *FEnt = static_cast<FileEntry *>(SFile);
return FEnt->getModificationTime();
}
+
+CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
+ if (!tu)
+ return 0;
+
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
+
+ FileManager &FMgr = CXXUnit->getFileManager();
+ const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
+ return const_cast<FileEntry *>(File);
+}
+
} // end: extern "C"
//===----------------------------------------------------------------------===//
@@ -1249,20 +1303,13 @@
return CXChildVisit_Recurse;
}
-CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
- unsigned line, unsigned column) {
- assert(CTUnit && "Passed null CXTranslationUnit");
- ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
-
- FileManager &FMgr = CXXUnit->getFileManager();
- const FileEntry *File = FMgr.getFile(source_name,
- source_name+strlen(source_name));
- if (!File)
+CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
+ if (!TU)
return clang_getNullCursor();
-
- SourceLocation SLoc =
- CXXUnit->getSourceManager().getLocation(File, line, column);
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
+
+ SourceLocation SLoc = translateSourceLocation(Loc);
CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
if (SLoc.isValid()) {
SourceRange RegionOfInterest(SLoc,
diff --git a/tools/CIndex/CIndex.exports b/tools/CIndex/CIndex.exports
index 7151dbe..ebeab8e 100644
--- a/tools/CIndex/CIndex.exports
+++ b/tools/CIndex/CIndex.exports
@@ -7,6 +7,7 @@
_clang_disposeString
_clang_disposeTranslationUnit
_clang_equalCursors
+_clang_equalLocations
_clang_getCString
_clang_getCompletionChunkCompletionString
_clang_getCompletionChunkKind
@@ -21,11 +22,15 @@
_clang_getCursorSpelling
_clang_getCursorUSR
_clang_getDefinitionSpellingAndExtent
+_clang_getFile
_clang_getFileName
_clang_getFileTime
_clang_getInstantiationLocation
+_clang_getLocation
_clang_getNullCursor
+_clang_getNullLocation
_clang_getNumCompletionChunks
+_clang_getRange
_clang_getRangeEnd
_clang_getRangeStart
_clang_getTranslationUnitCursor
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index 473773e..98ed772 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -161,7 +161,9 @@
clang_getInstantiationLocation(Loc, &file, 0, 0);
source = clang_getFileName(file);
if (source) {
- Ref = clang_getCursor(Data->TU, source, curLine, curColumn);
+ CXSourceLocation RefLoc
+ = clang_getLocation(Data->TU, file, curLine, curColumn);
+ Ref = clang_getCursor(Data->TU, RefLoc);
if (Ref.kind == CXCursor_NoDeclFound) {
/* Nothing found here; that's fine. */
} else if (Ref.kind != CXCursor_FunctionDecl) {
@@ -296,6 +298,7 @@
FILE *fp;
unsigned line;
CXCursor prevCursor;
+ CXFile file;
unsigned printed;
unsigned start_line, start_col, last_line, last_col;
size_t i;
@@ -320,6 +323,7 @@
start_line = last_line = 1;
start_col = last_col = 1;
+ file = clang_getFile(TU, source_file);
while (!feof(fp)) {
size_t len = 0;
int c;
@@ -334,7 +338,7 @@
for (i = 0; i < len ; ++i) {
CXCursor cursor;
- cursor = clang_getCursor(TU, source_file, line, i+1);
+ cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, i+1));
if (!clang_equalCursors(cursor, prevCursor) &&
prevCursor.kind != CXCursor_InvalidFile) {
@@ -656,8 +660,13 @@
}
for (Loc = 0; Loc < NumLocations; ++Loc) {
- Cursor = clang_getCursor(TU, Locations[Loc].filename,
- Locations[Loc].line, Locations[Loc].column);
+ CXFile file = clang_getFile(TU, Locations[Loc].filename);
+ if (!file)
+ continue;
+
+ Cursor = clang_getCursor(TU,
+ clang_getLocation(TU, file, Locations[Loc].line,
+ Locations[Loc].column));
PrintCursor(Cursor);
printf("\n");
free(Locations[Loc].filename);