dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 100.0% 8 / 0 / 8
Functions: 100.0% 2 / 0 / 2
Branches: 33.3% 2 / 0 / 6

src/regexp.h
Line Branch Exec Source
1 #ifndef REGEXP_H
2 #define REGEXP_H
3
4 #include <regex.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 #include "command/error.h"
9 #include "util/macros.h"
10 #include "util/string-view.h"
11 #include "util/string.h"
12
13 #if defined(REG_ENHANCED)
14 // The REG_ENHANCED flag enables various extensions on macOS
15 // (see "enhanced features" in re_format(7)). Most of these
16 // extensions are enabled by default on Linux (in both glibc
17 // and musl) without the need for any extra flags.
18 #define DEFAULT_REGEX_FLAGS ((REG_EXTENDED) | (REG_ENHANCED))
19 #else
20 // POSIX Extended Regular Expressions (ERE) are used almost
21 // everywhere in this codebase, except where Basic Regular
22 // Expressions (BRE) are explicitly called for (most notably
23 // in search_tag(), which is used for ctags patterns).
24 #define DEFAULT_REGEX_FLAGS (REG_EXTENDED)
25 #endif
26
27 // The REG_STARTEND flag is supported by glibc and BSDs, but ASan's
28 // __interceptor_regexec() still produces a "heap-buffer-overflow"
29 // error if the buffer isn't null-terminated. This is contrary to the
30 // entire point of the flag, so we simply use the portable fallback
31 // implementation when the flag is defined (and ostensibly supported)
32 // but known to cause problems.
33 #if defined(REG_STARTEND) && ASAN_ENABLED == 0 && MSAN_ENABLED == 0
34 #define REGEXP_STARTEND_FLAG (REG_STARTEND)
35 #define HAVE_REG_STARTEND 1 // Always suitable for #if conditions
36 #else
37 #define REGEXP_STARTEND_FLAG 0
38 #define HAVE_REG_STARTEND 0
39 #endif
40
41 typedef struct {
42 const char *str; // Pattern string, interned by str_intern()
43 regex_t re; // regex(3) object, compiled with regcomp(3)
44 } InternedRegexp;
45
46 // Platform-specific patterns for matching word boundaries, as detected
47 // and initialized by regexp_get_word_boundary_tokens()
48 typedef struct {
49 char start[8];
50 char end[8];
51 uint8_t len;
52 } RegexpWordBoundaryTokens;
53
54 const regex_t *regexp_compile_or_fatal_error(const char *pattern) NONNULL_ARGS_AND_RETURN;
55 RegexpWordBoundaryTokens regexp_get_word_boundary_tokens(void);
56 bool regexp_error_msg(ErrorBuffer *ebuf, const regex_t *re, const char *pattern, int err) NONNULL_ARG(2, 3);
57 char *regexp_escape(const char *pattern, size_t len) NONNULL_ARGS WARN_UNUSED_RESULT;
58 size_t regexp_escapeb(char *buf, size_t buflen, const char *pat, size_t plen) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(3, 4);
59 size_t string_append_escaped_regex(String *s, StringView pattern) NONNULL_ARGS;
60
61 const InternedRegexp *regexp_intern(ErrorBuffer *ebuf, const char *pattern) NONNULL_ARG(2) WARN_UNUSED_RESULT;
62 bool regexp_is_interned(const char *pattern) NONNULL_ARGS;
63 void free_interned_regexps(void);
64
65 WARN_UNUSED_RESULT NONNULL_ARG(1, 2) NONNULL_ARG_IF_NONZERO_LENGTH(5, 4)
66 bool regexp_exec (
67 const regex_t *re,
68 const char *buf,
69 size_t size,
70 size_t nmatch,
71 regmatch_t *pmatch,
72 int flags
73 );
74
75 WARN_UNUSED_RESULT NONNULL_ARG(2, 3)
76 304 static inline bool regexp_compile(ErrorBuffer *ebuf, regex_t *re, const char *pattern, int flags)
77 {
78 304 int err = regcomp(re, pattern, flags | DEFAULT_REGEX_FLAGS);
79
1/4
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 7 taken 304 times.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
304 return !err || regexp_error_msg(ebuf, re, pattern, err);
80 }
81
82 WARN_UNUSED_RESULT NONNULL_ARG(2)
83 56 static inline bool regexp_is_valid(ErrorBuffer *ebuf, const char *pattern, int flags)
84 {
85 56 regex_t re;
86
1/2
✓ Branch 3 → 4 taken 56 times.
✗ Branch 3 → 6 not taken.
56 if (!regexp_compile(ebuf, &re, pattern, flags | REG_NOSUB)) {
87 return false;
88 }
89 56 regfree(&re);
90 56 return true;
91 }
92
93 #endif
94