From 0abc5eb0188f2f1190bf012a79c579c86d081852 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Thu, 15 Mar 2012 18:07:22 +0000 Subject: [PATCH] [libclang] A couple of enhancements to c-index-test. -When printing location avoid printing the filename if it is same as the main file, not just if it has '.h' extension. -Make sure we allocate enough bytes for storing as string a huge line number. llvm-svn: 152821 --- clang/tools/c-index-test/c-index-test.c | 49 ++++++++++++++++++------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/clang/tools/c-index-test/c-index-test.c b/clang/tools/c-index-test/c-index-test.c index 925f56a601a4..1a2303d08396 100644 --- a/clang/tools/c-index-test/c-index-test.c +++ b/clang/tools/c-index-test/c-index-test.c @@ -1555,6 +1555,7 @@ typedef struct { int first_check_printed; int fail_for_error; int abort; + const char *main_filename; } IndexData; static void printCheck(IndexData *data) { @@ -1574,13 +1575,15 @@ static void printCXIndexFile(CXIdxClientFile file) { clang_disposeString(filename); } -static void printCXIndexLoc(CXIdxLoc loc) { +static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) { + IndexData *index_data; CXString filename; - const char *cname, *end; + const char *cname; CXIdxClientFile file; unsigned line, column; - int isHeader; + int isMainFile; + index_data = (IndexData *)client_data; clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0); if (line == 0) { printf(""); @@ -1592,16 +1595,29 @@ static void printCXIndexLoc(CXIdxLoc loc) { } filename = clang_getFileName((CXFile)file); cname = clang_getCString(filename); - end = cname + strlen(cname); - isHeader = (end[-2] == '.' && end[-1] == 'h'); - - if (isHeader) { + if (strcmp(cname, index_data->main_filename) == 0) + isMainFile = 1; + else + isMainFile = 0; + clang_disposeString(filename); + + if (!isMainFile) { printCXIndexFile(file); printf(":"); } printf("%d:%d", line, column); } +static unsigned digitCount(unsigned val) { + unsigned c = 1; + while (1) { + if (val < 10) + return c; + ++c; + val /= 10; + } +} + static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info, CXIdxLoc loc) { const char *name; @@ -1615,7 +1631,8 @@ static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info, clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0); /* FIXME: free these.*/ - newStr = (char *)malloc(strlen(name) + 10); + newStr = (char *)malloc(strlen(name) + + digitCount(line) + digitCount(column) + 3); sprintf(newStr, "%s:%d:%d", name, line, column); return (CXIdxClientContainer)newStr; } @@ -1722,7 +1739,7 @@ static void printBaseClassInfo(CXClientData client_data, printf(" | cursor: "); PrintCursor(info->cursor); printf(" | loc: "); - printCXIndexLoc(info->loc); + printCXIndexLoc(info->loc, client_data); } static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo, @@ -1734,7 +1751,7 @@ static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo, printf(" | cursor: "); PrintCursor(ProtoInfo->protocols[i]->cursor); printf(" | loc: "); - printCXIndexLoc(ProtoInfo->protocols[i]->loc); + printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data); printf("\n"); } } @@ -1770,6 +1787,10 @@ static CXIdxClientFile index_enteredMainFile(CXClientData client_data, index_data = (IndexData *)client_data; printCheck(index_data); + CXString filename = clang_getFileName(file); + index_data->main_filename = clang_getCString(filename); + clang_disposeString(filename); + printf("[enteredMainFile]: "); printCXIndexFile((CXIdxClientFile)file); printf("\n"); @@ -1787,7 +1808,7 @@ static CXIdxClientFile index_ppIncludedFile(CXClientData client_data, printCXIndexFile((CXIdxClientFile)info->file); printf(" | name: \"%s\"", info->filename); printf(" | hash loc: "); - printCXIndexLoc(info->hashLoc); + printCXIndexLoc(info->hashLoc, client_data); printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled); return (CXIdxClientFile)info->file; @@ -1818,7 +1839,7 @@ static void index_indexDeclaration(CXClientData client_data, printf(" | cursor: "); PrintCursor(info->cursor); printf(" | loc: "); - printCXIndexLoc(info->loc); + printCXIndexLoc(info->loc, client_data); printf(" | semantic-container: "); printCXIndexContainer(info->semanticContainer); printf(" | lexical-container: "); @@ -1856,7 +1877,7 @@ static void index_indexDeclaration(CXClientData client_data, printf(" | cursor: "); PrintCursor(CatInfo->classCursor); printf(" | loc: "); - printCXIndexLoc(CatInfo->classLoc); + printCXIndexLoc(CatInfo->classLoc, client_data); printf("\n"); } @@ -1900,7 +1921,7 @@ static void index_indexEntityReference(CXClientData client_data, printf(" | cursor: "); PrintCursor(info->cursor); printf(" | loc: "); - printCXIndexLoc(info->loc); + printCXIndexLoc(info->loc, client_data); printEntityInfo(" | :", client_data, info->parentEntity); printf(" | container: "); printCXIndexContainer(info->container);