| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "build-defs.h" | ||
| 2 | #include <errno.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | #include <sys/stat.h> | ||
| 6 | #include <sys/types.h> | ||
| 7 | #include <unistd.h> | ||
| 8 | #include "tag.h" | ||
| 9 | #include "command/error.h" | ||
| 10 | #include "util/debug.h" | ||
| 11 | #include "util/numtostr.h" | ||
| 12 | #include "util/path.h" | ||
| 13 | #include "util/time-util.h" | ||
| 14 | #include "util/xmalloc.h" | ||
| 15 | #include "util/xreadwrite.h" | ||
| 16 | #include "util/xstring.h" | ||
| 17 | |||
| 18 | ✗ | static bool tag_is_local_to_file(const Tag *tag, const char *path) | |
| 19 | { | ||
| 20 | ✗ | return tag->local && !!path && strview_equal_cstring(tag->filename, path); | |
| 21 | } | ||
| 22 | |||
| 23 | ✗ | static int visibility_cmp(const Tag *a, const Tag *b, const char *filename) | |
| 24 | { | ||
| 25 | ✗ | if (!a->local && !b->local) { | |
| 26 | return 0; | ||
| 27 | } | ||
| 28 | |||
| 29 | // Is tag visibility limited to the current file? | ||
| 30 | ✗ | bool a_this_file = tag_is_local_to_file(a, filename); | |
| 31 | ✗ | bool b_this_file = tag_is_local_to_file(b, filename); | |
| 32 | |||
| 33 | // Tags local to other file than current are not interesting | ||
| 34 | ✗ | if (a->local && !a_this_file) { | |
| 35 | // a is not interesting | ||
| 36 | ✗ | if (b->local && !b_this_file) { | |
| 37 | // b is equally uninteresting | ||
| 38 | return 0; | ||
| 39 | } | ||
| 40 | // b is more interesting, sort it before a | ||
| 41 | ✗ | return 1; | |
| 42 | } | ||
| 43 | ✗ | if (b->local && !b_this_file) { | |
| 44 | // b is not interesting | ||
| 45 | return -1; | ||
| 46 | } | ||
| 47 | |||
| 48 | // Both are NOT UNinteresting | ||
| 49 | |||
| 50 | ✗ | if (a->local && a_this_file) { | |
| 51 | ✗ | if (b->local && b_this_file) { | |
| 52 | return 0; | ||
| 53 | } | ||
| 54 | // a is more interesting because it's a local symbol | ||
| 55 | ✗ | return -1; | |
| 56 | } | ||
| 57 | ✗ | if (b->local && b_this_file) { | |
| 58 | // b is more interesting because it's a local symbol | ||
| 59 | ✗ | return 1; | |
| 60 | } | ||
| 61 | return 0; | ||
| 62 | } | ||
| 63 | |||
| 64 | ✗ | static int kind_cmp(const Tag *a, const Tag *b) | |
| 65 | { | ||
| 66 | ✗ | if (a->kind == b->kind) { | |
| 67 | return 0; | ||
| 68 | } | ||
| 69 | |||
| 70 | // Struct member (m) is not very interesting | ||
| 71 | ✗ | if (a->kind == 'm') { | |
| 72 | return 1; | ||
| 73 | } | ||
| 74 | ✗ | if (b->kind == 'm') { | |
| 75 | return -1; | ||
| 76 | } | ||
| 77 | |||
| 78 | // Global variable (v) is not very interesting | ||
| 79 | ✗ | if (a->kind == 'v') { | |
| 80 | return 1; | ||
| 81 | } | ||
| 82 | ✗ | if (b->kind == 'v') { | |
| 83 | ✗ | return -1; | |
| 84 | } | ||
| 85 | |||
| 86 | // Struct (s), union (u) | ||
| 87 | return 0; | ||
| 88 | } | ||
| 89 | |||
| 90 | ✗ | static int tag_cmp_r(const void *ap, const void *bp, void *userdata) | |
| 91 | { | ||
| 92 | ✗ | const Tag *const *a = ap; | |
| 93 | ✗ | const Tag *const *b = bp; | |
| 94 | ✗ | const char *filename = userdata; | |
| 95 | ✗ | int r = visibility_cmp(*a, *b, filename); | |
| 96 | ✗ | return r ? r : kind_cmp(*a, *b); | |
| 97 | } | ||
| 98 | |||
| 99 | // Find "tags" file from directory path and its parent directories | ||
| 100 | 1 | static int open_tag_file(ErrorBuffer *ebuf, char *path) | |
| 101 | { | ||
| 102 | 1 | static const char tags[] = "tags"; | |
| 103 |
1/2✓ Branch 12 → 3 taken 1 time.
✗ Branch 12 → 13 not taken.
|
1 | while (*path) { |
| 104 | 1 | size_t len = strlen(path); | |
| 105 | 1 | char *slash = strrchr(path, '/'); | |
| 106 |
1/2✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 5 not taken.
|
1 | if (slash != path + len - 1) { |
| 107 | 1 | path[len++] = '/'; | |
| 108 | } | ||
| 109 | 1 | memcpy(path + len, tags, sizeof(tags)); | |
| 110 | 1 | int fd = xopen(path, O_RDONLY | O_CLOEXEC, 0); | |
| 111 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 15 taken 1 time.
|
1 | if (fd >= 0) { |
| 112 | return fd; | ||
| 113 | } | ||
| 114 | ✗ | if (errno != ENOENT) { | |
| 115 | ✗ | error_msg(ebuf, "failed to open '%s': %s", path, strerror(errno)); | |
| 116 | ✗ | return -1; | |
| 117 | } | ||
| 118 | ✗ | *slash = '\0'; | |
| 119 | } | ||
| 120 | |||
| 121 | ✗ | error_msg(ebuf, "no tags file"); | |
| 122 | ✗ | return -1; | |
| 123 | } | ||
| 124 | |||
| 125 | ✗ | static bool tag_file_changed ( | |
| 126 | const TagFile *tf, | ||
| 127 | const char *filename, | ||
| 128 | const struct stat *st | ||
| 129 | ) { | ||
| 130 | ✗ | return tf->mtime != st->st_mtime || !streq(tf->filename, filename); | |
| 131 | } | ||
| 132 | |||
| 133 | // Note: does not free `tf` itself | ||
| 134 | 11 | void tag_file_free(TagFile *tf) | |
| 135 | { | ||
| 136 | 11 | free(tf->filename); | |
| 137 | 11 | free(tf->buf); | |
| 138 | 11 | *tf = (TagFile){.filename = NULL}; | |
| 139 | 11 | } | |
| 140 | |||
| 141 | 1 | bool load_tag_file(TagFile *tf, ErrorBuffer *ebuf) | |
| 142 | { | ||
| 143 | 1 | char path[8192]; | |
| 144 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1 time.
|
1 | if (unlikely(!getcwd(path, sizeof(path) - STRLEN("/tags")))) { |
| 145 | ✗ | return error_msg_errno(ebuf, "getcwd"); | |
| 146 | } | ||
| 147 | |||
| 148 | 1 | int fd = open_tag_file(ebuf, path); | |
| 149 |
1/2✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 34 not taken.
|
1 | if (fd < 0) { |
| 150 | return false; | ||
| 151 | } | ||
| 152 | |||
| 153 | 1 | struct stat st; | |
| 154 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 12 taken 1 time.
|
1 | if (unlikely(fstat(fd, &st) != 0)) { |
| 155 | ✗ | const char *str = strerror(errno); | |
| 156 | ✗ | xclose(fd); | |
| 157 | ✗ | return error_msg(ebuf, "fstat: %s", str); | |
| 158 | } | ||
| 159 | |||
| 160 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 15 taken 1 time.
|
1 | if (unlikely(st.st_size <= 0)) { |
| 161 | ✗ | xclose(fd); | |
| 162 | ✗ | return error_msg(ebuf, "empty tags file"); | |
| 163 | } | ||
| 164 | |||
| 165 |
1/2✗ Branch 15 → 16 not taken.
✓ Branch 15 → 22 taken 1 time.
|
1 | if (tf->filename) { |
| 166 | ✗ | if (!tag_file_changed(tf, path, &st)) { | |
| 167 | ✗ | xclose(fd); | |
| 168 | ✗ | return true; | |
| 169 | } | ||
| 170 | ✗ | tag_file_free(tf); | |
| 171 | ✗ | BUG_ON(tf->filename); | |
| 172 | } | ||
| 173 | |||
| 174 | 1 | char *buf = malloc(st.st_size); | |
| 175 |
1/2✗ Branch 22 → 23 not taken.
✓ Branch 22 → 26 taken 1 time.
|
1 | if (unlikely(!buf)) { |
| 176 | ✗ | xclose(fd); | |
| 177 | ✗ | return error_msg(ebuf, "malloc: %s", strerror(ENOMEM)); | |
| 178 | } | ||
| 179 | |||
| 180 | 1 | ssize_t size = xread_all(fd, buf, st.st_size); | |
| 181 | 1 | int err = errno; | |
| 182 | 1 | xclose(fd); | |
| 183 |
1/2✗ Branch 28 → 29 not taken.
✓ Branch 28 → 31 taken 1 time.
|
1 | if (size < 0) { |
| 184 | ✗ | free(buf); | |
| 185 | ✗ | return error_msg(ebuf, "read: %s", strerror(err)); | |
| 186 | } | ||
| 187 | |||
| 188 | 2 | *tf = (TagFile) { | |
| 189 | 1 | .filename = xstrdup(path), | |
| 190 | 1 | .dirname_len = (xstrrchr(path, '/') - path) + 1, // Includes last slash | |
| 191 | .buf = buf, | ||
| 192 | .size = size, | ||
| 193 | 1 | .mtime = st.st_mtime, | |
| 194 | }; | ||
| 195 | |||
| 196 | 1 | return true; | |
| 197 | } | ||
| 198 | |||
| 199 | 1 | static void free_tags_cb(Tag *tag) | |
| 200 | { | ||
| 201 | 1 | free_tag(tag); | |
| 202 | 1 | free(tag); | |
| 203 | 1 | } | |
| 204 | |||
| 205 | 1 | static void free_tags(PointerArray *tags) | |
| 206 | { | ||
| 207 | 1 | ptr_array_free_cb(tags, FREE_FUNC(free_tags_cb)); | |
| 208 | 1 | } | |
| 209 | |||
| 210 | #if !HAVE_QSORT_R | ||
| 211 | static const char *current_filename_global; // NOLINT(*-non-const-global-variables) | ||
| 212 | static int tag_cmp(const void *t1, const void *t2) | ||
| 213 | { | ||
| 214 | return tag_cmp_r(t1, t2, (char*)current_filename_global); | ||
| 215 | } | ||
| 216 | #endif | ||
| 217 | |||
| 218 | 1 | static void tag_file_find_tags ( | |
| 219 | const TagFile *tf, | ||
| 220 | const char *filename, | ||
| 221 | StringView name, | ||
| 222 | PointerArray *tags | ||
| 223 | ) { | ||
| 224 | 1 | Tag *tag = xmalloc(sizeof(*tag)); | |
| 225 | 1 | size_t pos = 0; | |
| 226 |
2/2✓ Branch 7 → 4 taken 1 time.
✓ Branch 7 → 8 taken 1 time.
|
2 | while (next_tag(tf->buf, tf->size, &pos, name, true, tag)) { |
| 227 | 1 | ptr_array_append(tags, tag); | |
| 228 | 1 | tag = xmalloc(sizeof(*tag)); | |
| 229 | } | ||
| 230 | 1 | free(tag); | |
| 231 | |||
| 232 |
1/2✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 10 not taken.
|
1 | if (tags->count < 2) { |
| 233 | 1 | return; | |
| 234 | } | ||
| 235 | |||
| 236 | ✗ | if (filename) { | |
| 237 | ✗ | BUG_ON(!path_is_absolute(filename)); | |
| 238 | ✗ | size_t n = tf->dirname_len; | |
| 239 | ✗ | BUG_ON(n == 0); | |
| 240 | ✗ | if (strncmp(filename, tf->filename, n) == 0) { | |
| 241 | ✗ | filename += n; | |
| 242 | } else { | ||
| 243 | // Filename doesn't start with directory prefix of tag file | ||
| 244 | filename = NULL; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | ✗ | void **ptrs = tags->ptrs; | |
| 249 | ✗ | BUG_ON(!ptrs); | |
| 250 | |||
| 251 | #if HAVE_QSORT_R | ||
| 252 | ✗ | qsort_r(ptrs, tags->count, sizeof(*ptrs), tag_cmp_r, (char*)filename); | |
| 253 | #else | ||
| 254 | current_filename_global = filename; | ||
| 255 | qsort(ptrs, tags->count, sizeof(*ptrs), tag_cmp); | ||
| 256 | current_filename_global = NULL; | ||
| 257 | #endif | ||
| 258 | } | ||
| 259 | |||
| 260 | // Note: this moves ownership of tag->pattern to the generated Message | ||
| 261 | // and assigns NULL to the old pointer | ||
| 262 | 1 | void add_message_for_tag(MessageList *messages, Tag *tag, StringView dir) | |
| 263 | { | ||
| 264 | 1 | BUG_ON(dir.length == 0); | |
| 265 | 1 | BUG_ON(dir.data[0] != '/'); | |
| 266 | |||
| 267 | 1 | static const char prefix[] = "Tag "; | |
| 268 | 1 | StringView name = tag->name; | |
| 269 | 1 | size_t prefix_len = sizeof(prefix) - 1; | |
| 270 | 1 | Message *m = xmalloc(xadd3(sizeof(*m), prefix_len, name.length + 1)); | |
| 271 | 1 | xmempcpy3(m->msg, prefix, prefix_len, name.data, name.length, "", 1); | |
| 272 | |||
| 273 | 1 | char *filename = path_join_sv(dir, tag->filename, false); | |
| 274 | 1 | m->loc = new_file_location(filename, 0, 0, 0); | |
| 275 | |||
| 276 |
1/2✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 13 not taken.
|
1 | if (tag->pattern) { |
| 277 | 1 | m->loc->pattern = tag->pattern; // Message takes ownership | |
| 278 | 1 | tag->pattern = NULL; | |
| 279 | } else { | ||
| 280 | ✗ | m->loc->line = tag->lineno; | |
| 281 | } | ||
| 282 | |||
| 283 | 1 | add_message(messages, m); | |
| 284 | 1 | } | |
| 285 | |||
| 286 | 1 | size_t tag_lookup ( | |
| 287 | TagFile *tf, | ||
| 288 | MessageList *messages, | ||
| 289 | ErrorBuffer *ebuf, | ||
| 290 | StringView name, | ||
| 291 | const char *filename | ||
| 292 | ) { | ||
| 293 | 1 | BUG_ON(!tf->filename); | |
| 294 | |||
| 295 | // Filename helps to find correct tags | ||
| 296 | 1 | PointerArray tags = PTR_ARRAY_INIT; | |
| 297 | 1 | tag_file_find_tags(tf, filename, name, &tags); | |
| 298 | |||
| 299 | 1 | size_t ntags = tags.count; | |
| 300 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 1 time.
|
1 | if (ntags == 0) { |
| 301 | ✗ | error_msg(ebuf, "Tag '%.*s' not found", (int)name.length, name.data); | |
| 302 | ✗ | return 0; | |
| 303 | } | ||
| 304 | |||
| 305 | // Note that `dirname_len` always includes a trailing slash, but the | ||
| 306 | // call to path_join_sv() in add_message_for_tag() handles that fine | ||
| 307 | 1 | BUG_ON(tf->dirname_len == 0); | |
| 308 | 1 | StringView tagfile_dir = string_view(tf->filename, tf->dirname_len); | |
| 309 | |||
| 310 |
2/2✓ Branch 13 → 11 taken 1 time.
✓ Branch 13 → 14 taken 1 time.
|
2 | for (size_t i = 0; i < ntags; i++) { |
| 311 | 1 | Tag *tag = tags.ptrs[i]; | |
| 312 | 1 | add_message_for_tag(messages, tag, tagfile_dir); | |
| 313 | } | ||
| 314 | |||
| 315 | 1 | free_tags(&tags); | |
| 316 | 1 | return ntags; | |
| 317 | } | ||
| 318 | |||
| 319 | ✗ | void collect_tags(TagFile *tf, PointerArray *a, StringView prefix) | |
| 320 | { | ||
| 321 | ✗ | ErrorBuffer ebuf; | |
| 322 | ✗ | ebuf.print_to_stderr = false; | |
| 323 | ✗ | if (!load_tag_file(tf, &ebuf)) { | |
| 324 | ✗ | return; | |
| 325 | } | ||
| 326 | |||
| 327 | ✗ | Tag tag; | |
| 328 | ✗ | size_t pos = 0; | |
| 329 | ✗ | StringView prev = STRING_VIEW_INIT; | |
| 330 | ✗ | while (next_tag(tf->buf, tf->size, &pos, prefix, false, &tag)) { | |
| 331 | ✗ | BUG_ON(tag.name.length == 0); | |
| 332 | ✗ | if (prev.length == 0 || !strview_equal(tag.name, prev)) { | |
| 333 | ✗ | ptr_array_append(a, xstrcut(tag.name.data, tag.name.length)); | |
| 334 | ✗ | prev = tag.name; | |
| 335 | } | ||
| 336 | ✗ | free_tag(&tag); | |
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | ✗ | String dump_tags(TagFile *tf, ErrorBuffer *ebuf) | |
| 341 | { | ||
| 342 | ✗ | if (!load_tag_file(tf, ebuf)) { | |
| 343 | ✗ | return string_new(0); | |
| 344 | } | ||
| 345 | |||
| 346 | ✗ | const struct timespec ts = {.tv_sec = tf->mtime}; | |
| 347 | ✗ | char sizestr[FILESIZE_STR_MAX]; | |
| 348 | ✗ | char tstr[TIME_STR_BUFSIZE]; | |
| 349 | ✗ | String buf = string_new(tf->size); | |
| 350 | |||
| 351 | ✗ | string_sprintf ( | |
| 352 | &buf, | ||
| 353 | "Tags file\n---------\n\n" | ||
| 354 | "%s %s\n%s %s\n%s %s\n\n" | ||
| 355 | "Tag entries\n-----------\n\n", | ||
| 356 | " Path:", tf->filename, | ||
| 357 | ✗ | " Modified:", timespec_to_str(&ts, tstr) ? tstr : "-", | |
| 358 | ✗ | " Size:", filesize_to_str(tf->size, sizestr) | |
| 359 | ); | ||
| 360 | |||
| 361 | ✗ | const StringView prefix = STRING_VIEW_INIT; | |
| 362 | ✗ | size_t pos = 0; | |
| 363 | ✗ | Tag tag; | |
| 364 | |||
| 365 | ✗ | while (next_tag(tf->buf, tf->size, &pos, prefix, false, &tag)) { | |
| 366 | ✗ | string_append_buf(&buf, tag.name.data, tag.name.length); | |
| 367 | ✗ | string_append_cstring(&buf, " "); | |
| 368 | ✗ | string_append_buf(&buf, tag.filename.data, tag.filename.length); | |
| 369 | ✗ | if (tag.kind) { | |
| 370 | ✗ | string_sprintf(&buf, " kind:%c", tag.kind); | |
| 371 | } | ||
| 372 | ✗ | if (tag.local) { | |
| 373 | ✗ | string_append_cstring(&buf, " LOCAL"); | |
| 374 | } | ||
| 375 | ✗ | if (tag.pattern) { | |
| 376 | ✗ | string_sprintf(&buf, " /%s/", tag.pattern); | |
| 377 | } else { | ||
| 378 | ✗ | string_sprintf(&buf, " lineno:%lu", tag.lineno); | |
| 379 | } | ||
| 380 | ✗ | string_append_byte(&buf, '\n'); | |
| 381 | ✗ | free_tag(&tag); | |
| 382 | } | ||
| 383 | |||
| 384 | ✗ | return buf; | |
| 385 | } | ||
| 386 |