summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorNico Weber <nicolasweber@gmx.de>2014-05-07 21:09:42 +0000
committerNico Weber <nicolasweber@gmx.de>2014-05-07 21:09:42 +0000
commit6d7447d5141456e4a1d6e335af38ed0758d90f47 (patch)
tree6c3e97101101c2479801a840ba5964b83d689990 /tools
parentba22e70e6b8fedc8684f78a626314df081e21ad2 (diff)
downloadclang-6d7447d5141456e4a1d6e335af38ed0758d90f47.tar.gz
clang-6d7447d5141456e4a1d6e335af38ed0758d90f47.tar.bz2
clang-6d7447d5141456e4a1d6e335af38ed0758d90f47.tar.xz
c-index-test: Don't leak the strings returned by makeClientContainer().
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@208249 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r--tools/c-index-test/c-index-test.c43
1 files changed, 37 insertions, 6 deletions
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index b3b482a614..fd2b120f41 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -1824,7 +1824,7 @@ static void print_completion_string(CXCompletionString completion_string,
}
static void print_completion_result(CXCompletionResult *completion_result,
- FILE *file) {
+ FILE *file) {
CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
unsigned annotationCount;
enum CXCursorKind ParentKind;
@@ -2532,6 +2532,11 @@ static void importedASTS_insert(ImportedASTFilesData *p, const char *file) {
p->filenames[p->num_files++] = strdup(file);
}
+typedef struct IndexDataStringList_ {
+ struct IndexDataStringList_ *next;
+ char data[1]; /* Dynamically sized. */
+} IndexDataStringList;
+
typedef struct {
const char *check_prefix;
int first_check_printed;
@@ -2539,8 +2544,19 @@ typedef struct {
int abort;
const char *main_filename;
ImportedASTFilesData *importedASTs;
+ IndexDataStringList *strings;
} IndexData;
+static void free_client_data(IndexData *index_data) {
+ IndexDataStringList *node = index_data->strings;
+ while (node) {
+ IndexDataStringList *next = node->next;
+ free(node);
+ node = next;
+ }
+ index_data->strings = NULL;
+}
+
static void printCheck(IndexData *data) {
if (data->check_prefix) {
if (data->first_check_printed) {
@@ -2601,8 +2617,11 @@ static unsigned digitCount(unsigned val) {
}
}
-static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
+static CXIdxClientContainer makeClientContainer(CXClientData *client_data,
+ const CXIdxEntityInfo *info,
CXIdxLoc loc) {
+ IndexData *index_data;
+ IndexDataStringList *node;
const char *name;
char *newStr;
CXIdxClientFile file;
@@ -2613,10 +2632,18 @@ static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
name = "<anon-tag>";
clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
- /* FIXME: free these.*/
- newStr = (char *)malloc(strlen(name) +
- digitCount(line) + digitCount(column) + 3);
+
+ node =
+ (IndexDataStringList *)malloc(sizeof(IndexDataStringList) + strlen(name) +
+ digitCount(line) + digitCount(column) + 2);
+ newStr = node->data;
sprintf(newStr, "%s:%d:%d", name, line, column);
+
+ /* Remember string so it can be freed later. */
+ index_data = (IndexData *)client_data;
+ node->next = index_data->strings;
+ index_data->strings = node;
+
return (CXIdxClientContainer)newStr;
}
@@ -2934,7 +2961,7 @@ static void index_indexDeclaration(CXClientData client_data,
if (info->declAsContainer)
clang_index_setClientContainer(
info->declAsContainer,
- makeClientContainer(info->entityInfo, info->loc));
+ makeClientContainer(client_data, info->entityInfo, info->loc));
}
static void index_indexEntityReference(CXClientData client_data,
@@ -3005,6 +3032,7 @@ static int index_compile_args(int num_args, const char **args,
index_data.abort = 0;
index_data.main_filename = "";
index_data.importedASTs = importedASTs;
+ index_data.strings = NULL;
index_opts = getIndexOptions();
result = clang_indexSourceFile(idxAction, &index_data,
@@ -3017,6 +3045,7 @@ static int index_compile_args(int num_args, const char **args,
if (index_data.fail_for_error)
result = -1;
+ free_client_data(&index_data);
return result;
}
@@ -3039,6 +3068,7 @@ static int index_ast_file(const char *ast_file,
index_data.abort = 0;
index_data.main_filename = "";
index_data.importedASTs = importedASTs;
+ index_data.strings = NULL;
index_opts = getIndexOptions();
result = clang_indexTranslationUnit(idxAction, &index_data,
@@ -3048,6 +3078,7 @@ static int index_ast_file(const char *ast_file,
result = -1;
clang_disposeTranslationUnit(TU);
+ free_client_data(&index_data);
return result;
}