[cfe-commits] r152821 - /cfe/trunk/tools/c-index-test/c-index-test.c

Argyrios Kyrtzidis akyrtzi at gmail.com
Thu Mar 15 11:07:22 PDT 2012


Author: akirtzidis
Date: Thu Mar 15 13:07:22 2012
New Revision: 152821

URL: http://llvm.org/viewvc/llvm-project?rev=152821&view=rev
Log:
[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.

Modified:
    cfe/trunk/tools/c-index-test/c-index-test.c

Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=152821&r1=152820&r2=152821&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Thu Mar 15 13:07:22 2012
@@ -1555,6 +1555,7 @@
   int first_check_printed;
   int fail_for_error;
   int abort;
+  const char *main_filename;
 } IndexData;
 
 static void printCheck(IndexData *data) {
@@ -1574,13 +1575,15 @@
   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("<null loc>");
@@ -1592,16 +1595,29 @@
   }
   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 @@
 
   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 @@
   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 @@
     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 @@
   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 @@
   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 @@
   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 @@
     printf(" | cursor: ");
     PrintCursor(CatInfo->classCursor);
     printf(" | loc: ");
-    printCXIndexLoc(CatInfo->classLoc);
+    printCXIndexLoc(CatInfo->classLoc, client_data);
     printf("\n");
   }
 
@@ -1900,7 +1921,7 @@
   printf(" | cursor: ");
   PrintCursor(info->cursor);
   printf(" | loc: ");
-  printCXIndexLoc(info->loc);
+  printCXIndexLoc(info->loc, client_data);
   printEntityInfo(" | <parent>:", client_data, info->parentEntity);
   printf(" | container: ");
   printCXIndexContainer(info->container);





More information about the cfe-commits mailing list