Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Generate a usable TAGS file

  • Loading branch information...
commit 1c7d063de8d7ff67bed2e6d669a1db3bd9a9c202 1 parent 836e661
Nick Gasson authored

Showing 1 changed file with 48 additions and 1 deletion. Show diff stats Hide diff stats

  1. +48 1 clang-tags.c
49 clang-tags.c
@@ -9,6 +9,8 @@
9 9 #include <string.h>
10 10 #include <stdlib.h>
11 11 #include <assert.h>
  12 +#include <stdbool.h>
  13 +#include <ctype.h>
12 14
13 15 typedef struct {
14 16 CXFile *source_file;
@@ -52,7 +54,42 @@ static void emit_tag(const char *name, const char *text,
52 54
53 55 etags_wr_ptr += snprintf(etags_wr_ptr, remain, "%s\x7f%s\x01%u,%u\n",
54 56 text, name, line, offset);
  57 +}
  58 +
  59 +static bool end_search_char(char ch)
  60 +{
  61 + return ch == '(' || ch == '{' || ch == '\n' || ch == '\r'
  62 + || ch == ';' || ch == '=';
  63 +}
  64 +
  65 +static char *find_search_text(const char *content,
  66 + unsigned offset,
  67 + unsigned max)
  68 +{
  69 + const char *start, *end;
  70 +
  71 + start = end = content + offset;
  72 +
  73 + // Search backwards to the beginning of the line
  74 + while (start > content && start[-1] != '\n')
  75 + start--;
  76 +
  77 + // Search forwards to the next brace or parenthesis
  78 + while (end <= content + offset + max && !end_search_char(*end))
  79 + end++;
  80 +
  81 + // Drop any trailing whitespace
  82 + while (isspace(*end))
  83 + end--;
  84 +
  85 + const size_t nchars = end - start;
  86 + char *buf = malloc(nchars + 1);
  87 + assert(buf != NULL);
  88 +
  89 + memcpy(buf, start, nchars);
  90 + buf[nchars] = '\0';
55 91
  92 + return buf;
56 93 }
57 94
58 95 static enum CXChildVisitResult cursor_visitor(CXCursor cursor,
@@ -69,14 +106,24 @@ static enum CXChildVisitResult cursor_visitor(CXCursor cursor,
69 106 clang_getSpellingLocation(loc, &file, &line, &column, &offset);
70 107
71 108 if (file == args->source_file) {
  109 + CXSourceRange extent = clang_getCursorExtent(cursor);
  110 + unsigned end;
  111 + clang_getSpellingLocation(clang_getRangeEnd(extent),
  112 + NULL, NULL, NULL, &end);
  113 +
  114 + char *search = find_search_text(args->file_contents,
  115 + offset, end - offset);
  116 +
72 117 CXString file_str = clang_getFileName(file);
73 118 CXString str = clang_getCursorSpelling(cursor);
74 119 printf("visit: %s: line %u col %u off %u: %s\n",
75 120 clang_getCString(file_str), line, column, offset,
76 121 clang_getCString(str));
77   - emit_tag(clang_getCString(str), "test", line, offset);
  122 + emit_tag(clang_getCString(str), search, line, offset);
  123 +
78 124 clang_disposeString(str);
79 125 clang_disposeString(file_str);
  126 + free(search);
80 127 }
81 128 }
82 129

0 comments on commit 1c7d063

Please sign in to comment.
Something went wrong with that request. Please try again.