include/eshu_c.h

filter: Statementstmt 94.8% 257 / 271 Branchbran 79.1% 293 / 370 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.8% 550 / 641 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/eshu_json.h ↑ Summary include/eshu_xml.h »
1 /*
2 * eshu_c.h — C language indentation scanner
3 *
4 * Tracks {} () [] nesting depth while skipping strings, comments,
5 * and preprocessor directives. Rewrites leading whitespace only.
6 */
7  
8 #ifndef ESHU_C_H
9 #define ESHU_C_H
10  
11 #include "eshu.h"
12  
13 /* ══════════════════════════════════════════════════════════════════
14 * Scanner context — persists across lines
15 * ══════════════════════════════════════════════════════════════════ */
16  
17 typedef struct {
18 int depth; /* brace nesting depth */
19 int pp_depth; /* preprocessor #if depth */
20 int case_depth; /* depth at which last case/default was seen */
21 int case_extra; /* 1 = add extra indent to case body lines */
22 enum eshu_state state; /* current scanner state */
23 eshu_config_t cfg;
24 } eshu_ctx_t;
25  
26 262 static void eshu_ctx_init(eshu_ctx_t *ctx, const eshu_config_t *cfg) {
27 262 ctx->depth = 0;
28 262 ctx->pp_depth = 0;
29 262 ctx->case_depth = 0;
30 262 ctx->case_extra = 0;
31 262 ctx->state = ESHU_CODE;
32 262 ctx->cfg = *cfg;
33 262 }
34  
35 /* ══════════════════════════════════════════════════════════════════
36 * Classify first non-ws char for pre-indent adjustment
37 * ══════════════════════════════════════════════════════════════════ */
38  
39 /* Does the line's content (after stripping ws) start with a
40 * closing brace/bracket/paren? If so we dedent before emitting. */
41 5812 static int eshu_c_is_closing(char c) {
42 5812 ▶ return c == '}' || c == ')' || c == ']';
Branch: 3 branches
BranchTrueFalseTotal
return c == '}' || c == ')' || c == ']'; 4855 957 5812
return c == '}' || c == ')' || c == ']'; 4854 1 4855
return c == '}' || c == ')' || c == ']'; 0 4854 4854
43 }
44  
45 /* Is this a preprocessor line? (first non-ws char is '#') */
46 5974 static int eshu_c_is_pp(const char *content) {
47 5974 return *content == '#';
48 }
49  
50 /* Is this line a switch case/default label?
51 * Matches "case <expr>:" or "default:" (ignoring trailing // comments).
52 * Case labels stay at brace depth; their body content gets +1 via case_extra. */
53 4854 static int eshu_c_is_case_label(const char *content, int len) {
54 const char *p, *end;
55 4854 int is_kw = 0;
56  
57 4854 ▶ if (len < 2) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 2) return 0; 13 4841 4854
58  
59 4841 ▶ if (len >= 5 && strncmp(content, "case ", 5) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (len >= 5 && strncmp(content, "case ", 5) == 0) 4692 149 4841
if (len >= 5 && strncmp(content, "case ", 5) == 0) 92 4600 4692
60 92 is_kw = 1;
61 4749 ▶ else if (len >= 7 && strncmp(content, "default", 7) == 0 &&
Branch: 3 branches
BranchTrueFalseTotal
else if (len >= 7 && strncmp(content, "default", 7) == 0 && 4484 265 4749
else if (len >= 7 && strncmp(content, "default", 7) == 0 && 18 4466 4484
else if (len >= 7 && strncmp(content, "default", 7) == 0 && 18 0 18
62 18 ▶ (len == 7 || content[7] == ':' || content[7] == ' ' ||
Branch: 2 branches
BranchTrueFalseTotal
(len == 7 || content[7] == ':' || content[7] == ' ' || 0 18 18
(len == 7 || content[7] == ':' || content[7] == ' ' || 0 0 0
63 0 ▶ content[7] == '\t' || content[7] == '/'))
Branch: 2 branches
BranchTrueFalseTotal
content[7] == '\t' || content[7] == '/')) 0 0 0
content[7] == '\t' || content[7] == '/')) 0 0 0
64 18 is_kw = 1;
65  
66 4841 ▶ if (!is_kw) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_kw) return 0; 4731 110 4841
67  
68 /* Trimmed content must end with ':' (ignore // comments) */
69 110 p = content;
70 110 end = content + len;
71 2589 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 2479 110 2589
72 2479 ▶ if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; }
Branch: 3 branches
BranchTrueFalseTotal
if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } 4 2475 2479
if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } 2 2 4
if (*p == '/' && p + 1 < end && *(p + 1) == '/') { end = p; break; } 0 2 2
73 2479 ▶ if (*p == '"' || *p == '\'') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '"' || *p == '\'') { 2479 0 2479
if (*p == '"' || *p == '\'') { 104 2375 2479
74 104 char d = *p++;
75 208 ▶ while (p < end && *p != d) {
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != d) { 208 0 208
while (p < end && *p != d) { 104 104 208
76 104 ▶ if (*p == '\\') p++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\\') p++; 4 100 104
77 104 p++;
78 }
79 }
80 2479 p++;
81 }
82 110 ▶ while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--;
Branch: 3 branches
BranchTrueFalseTotal
while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; 110 0 110
while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; 0 110 110
while (end > content && (*(end - 1) == ' ' || *(end - 1) == '\t')) end--; 0 110 110
83 110 ▶ return end > content && *(end - 1) == ':';
Branch: 2 branches
BranchTrueFalseTotal
return end > content && *(end - 1) == ':'; 110 0 110
return end > content && *(end - 1) == ':'; 74 36 110
84 }
85  
86 /* Is this line a bare goto label (identifier followed by ':' alone)?
87 * Excludes case/default. These are emitted at column 0 in C style. */
88 4780 static int eshu_c_is_goto_label(const char *content, int len) {
89 const char *p;
90 int ident_len;
91  
92 4780 ▶ if (len < 2) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 2) return 0; 13 4767 4780
93 4767 ▶ if (!isalpha((unsigned char)*content) && *content != '_') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!isalpha((unsigned char)*content) && *content != '_') return 0; 793 3974 4767
if (!isalpha((unsigned char)*content) && *content != '_') return 0; 793 0 793
94  
95 3974 p = content;
96 26349 ▶ while (p < content + len && (isalnum((unsigned char)*p) || *p == '_')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < content + len && (isalnum((unsigned char)*p) || *p == '_')) p++; 26337 12 26349
while (p < content + len && (isalnum((unsigned char)*p) || *p == '_')) p++; 20826 5511 26337
while (p < content + len && (isalnum((unsigned char)*p) || *p == '_')) p++; 1549 3962 5511
97 3974 ident_len = (int)(p - content);
98  
99 /* exclude case and default */
100 3974 ▶ if (ident_len == 4 && strncmp(content, "case", 4) == 0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (ident_len == 4 && strncmp(content, "case", 4) == 0) return 0; 356 3618 3974
if (ident_len == 4 && strncmp(content, "case", 4) == 0) return 0; 32 324 356
101 3942 ▶ if (ident_len == 7 && strncmp(content, "default", 7) == 0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (ident_len == 7 && strncmp(content, "default", 7) == 0) return 0; 131 3811 3942
if (ident_len == 7 && strncmp(content, "default", 7) == 0) return 0; 4 127 131
102  
103 7889 ▶ while (p < content + len && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < content + len && (*p == ' ' || *p == '\t')) p++; 7877 12 7889
while (p < content + len && (*p == ' ' || *p == '\t')) p++; 3951 3926 7877
while (p < content + len && (*p == ' ' || *p == '\t')) p++; 0 3926 3926
104 3938 ▶ if (p >= content + len || *p != ':') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= content + len || *p != ':') return 0; 3926 12 3938
if (p >= content + len || *p != ':') return 0; 3918 8 3926
105 8 p++;
106  
107 10 ▶ while (p < content + len && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < content + len && (*p == ' ' || *p == '\t')) p++; 6 4 10
while (p < content + len && (*p == ' ' || *p == '\t')) p++; 2 4 6
while (p < content + len && (*p == ' ' || *p == '\t')) p++; 0 4 4
108 8 ▶ if (p >= content + len) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= content + len) return 1; 4 4 8
109 4 ▶ return *p == '/' && p + 1 < content + len && *(p + 1) == '/';
Branch: 3 branches
BranchTrueFalseTotal
return *p == '/' && p + 1 < content + len && *(p + 1) == '/'; 0 4 4
return *p == '/' && p + 1 < content + len && *(p + 1) == '/'; 0 0 0
return *p == '/' && p + 1 < content + len && *(p + 1) == '/'; 0 0 0
110 }
111  
112 /* ══════════════════════════════════════════════════════════════════
113 * Classify preprocessor directive for pp_depth tracking
114 * Returns: +1 for #if/#ifdef/#ifndef, -1 for #endif, 0 otherwise
115 * Sets *is_else = 1 for #else/#elif
116 * ══════════════════════════════════════════════════════════════════ */
117  
118 162 static int eshu_c_pp_classify(const char *content, int *is_else) {
119 162 const char *p = content + 1; /* skip '#' */
120 162 *is_else = 0;
121  
122 162 ▶ while (*p == ' ' || *p == '\t') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (*p == ' ' || *p == '\t') p++; 0 162 162
while (*p == ' ' || *p == '\t') p++; 0 162 162
123  
124 162 ▶ if (strncmp(p, "if", 2) == 0 && !isalnum((unsigned char)p[2]) && p[2] != '_')
Branch: 3 branches
BranchTrueFalseTotal
if (strncmp(p, "if", 2) == 0 && !isalnum((unsigned char)p[2]) && p[2] != '_') 32 130 162
if (strncmp(p, "if", 2) == 0 && !isalnum((unsigned char)p[2]) && p[2] != '_') 4 28 32
if (strncmp(p, "if", 2) == 0 && !isalnum((unsigned char)p[2]) && p[2] != '_') 4 0 4
125 4 return 1;
126 158 ▶ if (strncmp(p, "ifdef", 5) == 0 && !isalnum((unsigned char)p[5]))
Branch: 2 branches
BranchTrueFalseTotal
if (strncmp(p, "ifdef", 5) == 0 && !isalnum((unsigned char)p[5])) 15 143 158
if (strncmp(p, "ifdef", 5) == 0 && !isalnum((unsigned char)p[5])) 15 0 15
127 15 return 1;
128 143 ▶ if (strncmp(p, "ifndef", 6) == 0 && !isalnum((unsigned char)p[6]))
Branch: 2 branches
BranchTrueFalseTotal
if (strncmp(p, "ifndef", 6) == 0 && !isalnum((unsigned char)p[6])) 13 130 143
if (strncmp(p, "ifndef", 6) == 0 && !isalnum((unsigned char)p[6])) 13 0 13
129 13 return 1;
130 130 ▶ if (strncmp(p, "endif", 5) == 0 && !isalnum((unsigned char)p[5]))
Branch: 2 branches
BranchTrueFalseTotal
if (strncmp(p, "endif", 5) == 0 && !isalnum((unsigned char)p[5])) 32 98 130
if (strncmp(p, "endif", 5) == 0 && !isalnum((unsigned char)p[5])) 32 0 32
131 32 return -1;
132 98 ▶ if (strncmp(p, "else", 4) == 0 && !isalnum((unsigned char)p[4])) {
Branch: 2 branches
BranchTrueFalseTotal
if (strncmp(p, "else", 4) == 0 && !isalnum((unsigned char)p[4])) { 6 92 98
if (strncmp(p, "else", 4) == 0 && !isalnum((unsigned char)p[4])) { 6 0 6
133 6 *is_else = 1;
134 6 return 0;
135 }
136 92 ▶ if (strncmp(p, "elif", 4) == 0 && !isalnum((unsigned char)p[4])) {
Branch: 2 branches
BranchTrueFalseTotal
if (strncmp(p, "elif", 4) == 0 && !isalnum((unsigned char)p[4])) { 1 91 92
if (strncmp(p, "elif", 4) == 0 && !isalnum((unsigned char)p[4])) { 1 0 1
137 1 *is_else = 1;
138 1 return 0;
139 }
140  
141 91 return 0;
142 }
143  
144 /* ══════════════════════════════════════════════════════════════════
145 * Scan a line for nesting changes (in CODE state)
146 *
147 * Called AFTER the line has been emitted. Updates ctx->state
148 * and ctx->depth for the next line.
149 * ══════════════════════════════════════════════════════════════════ */
150  
151 6118 static void eshu_c_scan_line(eshu_ctx_t *ctx, const char *p, const char *end) {
152 191392 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 185277 6115 191392
153 185277 char c = *p;
154  
155 185277 switch (ctx->state) {
156 111729 case ESHU_CODE:
157 111729 ▶ if (c == '{' || c == '(' || c == '[') {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '{' || c == '(' || c == '[') { 110720 1009 111729
if (c == '{' || c == '(' || c == '[') { 108112 2608 110720
if (c == '{' || c == '(' || c == '[') { 163 107949 108112
158 3780 ctx->depth++;
159 107949 ▶ } else if (c == '}' || c == ')' || c == ']') {
Branch: 3 branches
BranchTrueFalseTotal
} else if (c == '}' || c == ')' || c == ']') { 106936 1013 107949
} else if (c == '}' || c == ')' || c == ']') { 104328 2608 106936
} else if (c == '}' || c == ')' || c == ']') { 163 104165 104328
160 3784 ctx->depth--;
161 3784 ▶ if (ctx->depth < 0) ctx->depth = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->depth < 0) ctx->depth = 0; 4 3780 3784
162 104165 ▶ } else if (c == '"') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '"') { 720 103445 104165
163 720 ctx->state = ESHU_STRING_DQ;
164 103445 ▶ } else if (c == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '\'') { 851 102594 103445
165 851 ctx->state = ESHU_CHAR_LIT;
166 102594 ▶ } else if (c == '/' && p + 1 < end && *(p + 1) == '/') {
Branch: 3 branches
BranchTrueFalseTotal
} else if (c == '/' && p + 1 < end && *(p + 1) == '/') { 803 101791 102594
} else if (c == '/' && p + 1 < end && *(p + 1) == '/') { 803 0 803
} else if (c == '/' && p + 1 < end && *(p + 1) == '/') { 3 800 803
167 /* line comment — skip rest of line */
168 3 return;
169 102591 ▶ } else if (c == '/' && p + 1 < end && *(p + 1) == '*') {
Branch: 3 branches
BranchTrueFalseTotal
} else if (c == '/' && p + 1 < end && *(p + 1) == '*') { 800 101791 102591
} else if (c == '/' && p + 1 < end && *(p + 1) == '*') { 800 0 800
} else if (c == '/' && p + 1 < end && *(p + 1) == '*') { 791 9 800
170 791 ctx->state = ESHU_COMMENT_BLOCK;
171 791 p++; /* skip the '*' */
172 }
173 111726 break;
174  
175 5110 case ESHU_STRING_DQ:
176 5110 ▶ if (c == '\\' && p + 1 < end) {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '\\' && p + 1 < end) { 22 5088 5110
if (c == '\\' && p + 1 < end) { 22 0 22
177 22 p++; /* skip escaped char */
178 5088 ▶ } else if (c == '"') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '"') { 720 4368 5088
179 720 ctx->state = ESHU_CODE;
180 }
181 5110 break;
182  
183 1702 case ESHU_CHAR_LIT:
184 1702 ▶ if (c == '\\' && p + 1 < end) {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '\\' && p + 1 < end) { 258 1444 1702
if (c == '\\' && p + 1 < end) { 258 0 258
185 258 p++; /* skip escaped char */
186 1444 ▶ } else if (c == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '\'') { 851 593 1444
187 851 ctx->state = ESHU_CODE;
188 }
189 1702 break;
190  
191 0 case ESHU_STRING_SQ:
192 /* C doesn't use SQ strings, but keep state complete */
193 0 ▶ if (c == '\\' && p + 1 < end) {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '\\' && p + 1 < end) { 0 0 0
if (c == '\\' && p + 1 < end) { 0 0 0
194 0 p++;
195 0 ▶ } else if (c == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '\'') { 0 0 0
196 0 ctx->state = ESHU_CODE;
197 }
198 0 break;
199  
200 66736 case ESHU_COMMENT_BLOCK:
201 66736 ▶ if (c == '*' && p + 1 < end && *(p + 1) == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '*' && p + 1 < end && *(p + 1) == '/') { 1084 65652 66736
if (c == '*' && p + 1 < end && *(p + 1) == '/') { 1064 20 1084
if (c == '*' && p + 1 < end && *(p + 1) == '/') { 791 273 1064
202 791 ctx->state = ESHU_CODE;
203 791 p++; /* skip the '/' */
204 }
205 66736 break;
206  
207 0 case ESHU_COMMENT_LINE:
208 /* shouldn't happen — we return early above */
209 0 return;
210  
211 0 case ESHU_PREPROCESSOR:
212 /* handled separately */
213 0 break;
214  
215 /* States not used by the C scanner — ignore */
216 0 default:
217 0 break;
218 }
219 185274 p++;
220 }
221 }
222  
223 /* ══════════════════════════════════════════════════════════════════
224 * Process a single line — decide indent, emit, scan
225 * ══════════════════════════════════════════════════════════════════ */
226  
227 6962 static void eshu_c_process_line(eshu_ctx_t *ctx, eshu_buf_t *out,
228 const char *line_start, const char *eol) {
229 6962 const char *content = eshu_skip_leading_ws(line_start);
230 int line_len;
231 int indent_depth;
232  
233 /* empty line — preserve it */
234 6962 ▶ if (content >= eol) {
Branch: 1 branch
BranchTrueFalseTotal
if (content >= eol) { 682 6280 6962
235 682 eshu_buf_putc(out, '\n');
236 682 return;
237 }
238  
239 6280 line_len = (int)(eol - content);
240  
241 /* If we're in a block comment, pass through with current depth */
242 6280 ▶ if (ctx->state == ESHU_COMMENT_BLOCK) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_COMMENT_BLOCK) { 306 5974 6280
243 /* inside block comment: indent at current depth + 1
244 * (the comment was opened inside a code block) */
245 306 eshu_emit_indent(out, ctx->depth, &ctx->cfg);
246 306 eshu_buf_write_trimmed(out, content, line_len);
247 306 eshu_buf_putc(out, '\n');
248 306 eshu_c_scan_line(ctx, content, eol);
249 306 return;
250 }
251  
252 /* Preprocessor line */
253 5974 ▶ if (eshu_c_is_pp(content)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_c_is_pp(content)) { 162 5812 5974
254 162 int is_else = 0;
255 162 int pp_dir = eshu_c_pp_classify(content, &is_else);
256  
257 162 ▶ if (ctx->cfg.indent_pp) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cfg.indent_pp) { 7 155 162
258 7 ▶ if (pp_dir < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pp_dir < 0) { 3 4 7
259 /* #endif — dedent first */
260 3 ctx->pp_depth--;
261 3 ▶ if (ctx->pp_depth < 0) ctx->pp_depth = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->pp_depth < 0) ctx->pp_depth = 0; 0 3 3
262 }
263 7 ▶ if (is_else) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_else) { 1 6 7
264 1 eshu_emit_indent(out, ctx->pp_depth - 1, &ctx->cfg);
265 } else {
266 6 eshu_emit_indent(out, ctx->pp_depth, &ctx->cfg);
267 }
268 7 ▶ if (pp_dir > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (pp_dir > 0) 3 4 7
269 3 ctx->pp_depth++;
270 } else {
271 /* no pp indent — emit at column 0 */
272 155 ▶ if (pp_dir > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (pp_dir > 0) 29 126 155
273 29 ctx->pp_depth++;
274 126 ▶ else if (pp_dir < 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (pp_dir < 0) { 29 97 126
275 29 ctx->pp_depth--;
276 29 ▶ if (ctx->pp_depth < 0) ctx->pp_depth = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->pp_depth < 0) ctx->pp_depth = 0; 0 29 29
277 }
278 }
279  
280 162 eshu_buf_write_trimmed(out, content, line_len);
281 162 eshu_buf_putc(out, '\n');
282 162 return;
283 }
284  
285 /* Normal code line */
286 5812 indent_depth = ctx->depth;
287  
288 /* Leaving a case block: clear case_extra once depth drops below case_depth */
289 5812 ▶ if (ctx->case_extra && ctx->depth < ctx->case_depth)
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->case_extra && ctx->depth < ctx->case_depth) 730 5082 5812
if (ctx->case_extra && ctx->depth < ctx->case_depth) 15 715 730
290 15 ctx->case_extra = 0;
291  
292 5812 ▶ if (eshu_c_is_closing(*content)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_c_is_closing(*content)) { 958 4854 5812
293 /* closing brace/paren — dedent this line */
294 958 indent_depth--;
295 958 ▶ if (indent_depth < 0) indent_depth = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (indent_depth < 0) indent_depth = 0; 4 954 958
296 /* still inside a case block at deeper nesting */
297 958 ▶ if (ctx->case_extra && ctx->depth > ctx->case_depth)
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->case_extra && ctx->depth > ctx->case_depth) 146 812 958
if (ctx->case_extra && ctx->depth > ctx->case_depth) 130 16 146
298 130 indent_depth++;
299 4854 ▶ } else if (eshu_c_is_case_label(content, line_len)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (eshu_c_is_case_label(content, line_len)) { 74 4780 4854
300 /* case/default label: stays at brace depth; body lines get +1 */
301 74 ctx->case_depth = ctx->depth;
302 74 ctx->case_extra = 1;
303 4780 ▶ } else if (eshu_c_is_goto_label(content, line_len)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (eshu_c_is_goto_label(content, line_len)) { 4 4776 4780
304 /* goto label: always at column 0 */
305 4 indent_depth = 0;
306 4776 ▶ } else if (ctx->case_extra && ctx->depth >= ctx->case_depth) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (ctx->case_extra && ctx->depth >= ctx->case_depth) { 511 4265 4776
} else if (ctx->case_extra && ctx->depth >= ctx->case_depth) { 511 0 511
307 /* body line inside a case block: add one extra indent level */
308 511 indent_depth++;
309 }
310  
311 5812 eshu_emit_indent(out, indent_depth, &ctx->cfg);
312 5812 eshu_buf_write_trimmed(out, content, line_len);
313 5812 eshu_buf_putc(out, '\n');
314  
315 /* Scan for nesting changes */
316 5812 eshu_c_scan_line(ctx, content, eol);
317 }
318  
319 /* ══════════════════════════════════════════════════════════════════
320 * Public API — indent a C source string
321 * ══════════════════════════════════════════════════════════════════ */
322  
323 166 static char * eshu_indent_c(const char *src, size_t src_len,
324 const eshu_config_t *cfg, size_t *out_len) {
325 eshu_ctx_t ctx;
326 eshu_buf_t out;
327 166 const char *p = src;
328 166 const char *end = src + src_len;
329 char *result;
330  
331 166 eshu_ctx_init(&ctx, cfg);
332 166 eshu_buf_init(&out, src_len + 256);
333  
334 {
335 166 int line_num = 1;
336 6999 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 6833 166 6999
337 6833 const char *eol = eshu_find_eol(p);
338  
339 6833 ▶ if (eshu_in_range(cfg, line_num)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_in_range(cfg, line_num)) { 6830 3 6833
340 6830 eshu_c_process_line(&ctx, &out, p, eol);
341 } else {
342 /* Outside range: scan for state, emit verbatim */
343 3 size_t saved = out.len;
344 3 eshu_c_process_line(&ctx, &out, p, eol);
345 3 out.len = saved;
346 3 eshu_buf_write_trimmed(&out, p, (int)(eol - p));
347 3 eshu_buf_putc(&out, '\n');
348 }
349  
350 6833 p = eol;
351 6833 ▶ if (*p == '\n') p++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\n') p++; 6833 0 6833
352 6833 line_num++;
353 }
354 }
355  
356 /* NUL-terminate */
357 166 eshu_buf_putc(&out, '\0');
358 166 out.len--; /* don't count NUL in length */
359  
360 166 *out_len = out.len;
361 166 result = out.data;
362 /* caller owns result, must free() it */
363 166 return result;
364 }
365  
366  
367 #include "eshu_hl_util.h"
368  
369 /* ══════════════════════════════════════════════════════════════════
370 * C / XS keyword list
371 * ══════════════════════════════════════════════════════════════════ */
372  
373 static const char * const eshu_hl_c_kw[] = {
374 "auto", "break", "case", "char", "const", "continue", "default",
375 "do", "double", "else", "enum", "extern", "float", "for", "goto",
376 "if", "inline", "int", "long", "register", "restrict", "return",
377 "short", "signed", "sizeof", "static", "struct", "switch", "typedef",
378 "union", "unsigned", "void", "volatile", "while",
379 /* C99/C11 */
380 "_Bool", "_Complex", "_Thread_local", "_Atomic", "_Noreturn",
381 "_Alignas", "_Alignof", "_Static_assert",
382 /* common macros treated as keywords */
383 "NULL", "true", "false",
384 NULL
385 };
386  
387 /* ══════════════════════════════════════════════════════════════════
388 * C / XS highlighter
389 * ══════════════════════════════════════════════════════════════════ */
390  
391 57 static char *eshu_highlight_c(const char *src, size_t src_len, size_t *out_len) {
392 eshu_buf_t out;
393 57 const char *p = src;
394 57 const char *end = src + src_len;
395 57 const char *plain = p;
396 57 int at_bol = 1; /* at beginning of line */
397  
398 57 eshu_buf_init(&out, src_len * 2 + 64);
399  
400 /* emit plain text up to tok_start, then span, advance p */
401 #define C_SPAN(cls, ts, te) do { \
402 eshu_hl_flush(&out, plain, (ts)); \
403 eshu_hl_span(&out, (cls), (ts), (te)); \
404 p = (te); plain = p; \
405 } while(0)
406  
407 505 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 448 57 505
408 448 char c = *p;
409  
410 /* preprocessor: '#' at beginning of logical line */
411 448 ▶ if (at_bol && c == '#') {
Branch: 2 branches
BranchTrueFalseTotal
if (at_bol && c == '#') { 98 350 448
if (at_bol && c == '#') { 11 87 98
412 11 const char *ts = p++;
413 140 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 140 0 140
414 140 ▶ if (*p == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\n') { 11 129 140
415 11 ▶ if (p > ts && *(p - 1) == '\\') { p++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (p > ts && *(p - 1) == '\\') { p++; continue; } 11 0 11
if (p > ts && *(p - 1) == '\\') { p++; continue; } 0 11 11
416 11 break;
417 }
418 129 p++;
419 }
420 11 C_SPAN("esh-p", ts, p);
421 /* leave '\n' to be handled as next char */
422 11 continue;
423 }
424  
425 /* line comment */
426 437 ▶ if (c == '/' && p + 1 < end && *(p + 1) == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '/' && p + 1 < end && *(p + 1) == '/') { 7 430 437
if (c == '/' && p + 1 < end && *(p + 1) == '/') { 7 0 7
if (c == '/' && p + 1 < end && *(p + 1) == '/') { 2 5 7
427 2 const char *ts = p;
428 2 p += 2;
429 26 ▶ while (p < end && *p != '\n') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != '\n') p++; 26 0 26
while (p < end && *p != '\n') p++; 24 2 26
430 2 C_SPAN("esh-c", ts, p);
431 2 at_bol = 0;
432 2 continue;
433 }
434  
435 /* block comment */
436 435 ▶ if (c == '/' && p + 1 < end && *(p + 1) == '*') {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '/' && p + 1 < end && *(p + 1) == '*') { 5 430 435
if (c == '/' && p + 1 < end && *(p + 1) == '*') { 5 0 5
if (c == '/' && p + 1 < end && *(p + 1) == '*') { 4 1 5
437 4 const char *ts = p;
438 4 p += 2;
439 75 ▶ while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; 75 0 75
while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; 71 4 75
while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; 0 4 4
440 4 ▶ if (p + 1 < end) p += 2;
Branch: 1 branch
BranchTrueFalseTotal
if (p + 1 < end) p += 2; 4 0 4
441 4 C_SPAN("esh-c", ts, p);
442 4 at_bol = 0;
443 4 continue;
444 }
445  
446 /* string or char literal */
447 431 ▶ if (c == '"' || c == '\'') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '"' || c == '\'') { 426 5 431
if (c == '"' || c == '\'') { 2 424 426
448 7 const char *ts = p++;
449 43 ▶ while (p < end && *p != c) {
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != c) { 43 0 43
while (p < end && *p != c) { 36 7 43
450 36 ▶ if (*p == '\\') { p++; if (p < end) p++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '\\') { p++; if (p < end) p++; continue; } 3 33 36
if (*p == '\\') { p++; if (p < end) p++; continue; } 3 0 3
451 33 ▶ if (*p == '\n') break; /* unterminated */
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\n') break; /* unterminated */ 0 33 33
452 33 p++;
453 }
454 7 ▶ if (p < end && *p == c) p++;
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == c) p++; 7 0 7
if (p < end && *p == c) p++; 7 0 7
455 7 C_SPAN("esh-s", ts, p);
456 7 at_bol = 0;
457 7 continue;
458 }
459  
460 /* number: digit, or '.digit' */
461 424 ▶ if (isdigit((unsigned char)c) ||
Branch: 2 branches
BranchTrueFalseTotal
if (isdigit((unsigned char)c) || 408 16 424
if (isdigit((unsigned char)c) || 0 408 408
462 0 ▶ (c == '.' && p + 1 < end && isdigit((unsigned char)*(p + 1)))) {
Branch: 2 branches
BranchTrueFalseTotal
(c == '.' && p + 1 < end && isdigit((unsigned char)*(p + 1)))) { 0 0 0
(c == '.' && p + 1 < end && isdigit((unsigned char)*(p + 1)))) { 0 0 0
463 16 const char *ts = p;
464 16 ▶ if (c == '0' && p + 1 < end && (*(p + 1) == 'x' || *(p + 1) == 'X')) {
Branch: 4 branches
BranchTrueFalseTotal
if (c == '0' && p + 1 < end && (*(p + 1) == 'x' || *(p + 1) == 'X')) { 6 10 16
if (c == '0' && p + 1 < end && (*(p + 1) == 'x' || *(p + 1) == 'X')) { 6 0 6
if (c == '0' && p + 1 < end && (*(p + 1) == 'x' || *(p + 1) == 'X')) { 5 1 6
if (c == '0' && p + 1 < end && (*(p + 1) == 'x' || *(p + 1) == 'X')) { 0 5 5
465 1 p += 2;
466 3 ▶ while (p < end && isxdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isxdigit((unsigned char)*p)) p++; 2 1 3
while (p < end && isxdigit((unsigned char)*p)) p++; 2 0 2
467 15 ▶ } else if (c == '0' && p + 1 < end && (*(p + 1) == 'b' || *(p + 1) == 'B')) {
Branch: 4 branches
BranchTrueFalseTotal
} else if (c == '0' && p + 1 < end && (*(p + 1) == 'b' || *(p + 1) == 'B')) { 5 10 15
} else if (c == '0' && p + 1 < end && (*(p + 1) == 'b' || *(p + 1) == 'B')) { 5 0 5
} else if (c == '0' && p + 1 < end && (*(p + 1) == 'b' || *(p + 1) == 'B')) { 4 1 5
} else if (c == '0' && p + 1 < end && (*(p + 1) == 'b' || *(p + 1) == 'B')) { 0 4 4
468 1 p += 2;
469 5 ▶ while (p < end && (*p == '0' || *p == '1')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == '0' || *p == '1')) p++; 4 1 5
while (p < end && (*p == '0' || *p == '1')) p++; 2 2 4
while (p < end && (*p == '0' || *p == '1')) p++; 2 0 2
470 } else {
471 34 ▶ while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isdigit((unsigned char)*p)) p++; 33 1 34
while (p < end && isdigit((unsigned char)*p)) p++; 20 13 33
472 14 ▶ if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') { 13 1 14
if (p < end && *p == '.') { 1 12 13
473 1 p++;
474 3 ▶ while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isdigit((unsigned char)*p)) p++; 3 0 3
while (p < end && isdigit((unsigned char)*p)) p++; 2 1 3
475 }
476 14 ▶ if (p < end && (*p == 'e' || *p == 'E')) {
Branch: 3 branches
BranchTrueFalseTotal
if (p < end && (*p == 'e' || *p == 'E')) { 13 1 14
if (p < end && (*p == 'e' || *p == 'E')) { 11 2 13
if (p < end && (*p == 'e' || *p == 'E')) { 0 11 11
477 2 p++;
478 2 ▶ if (p < end && (*p == '+' || *p == '-')) p++;
Branch: 3 branches
BranchTrueFalseTotal
if (p < end && (*p == '+' || *p == '-')) p++; 2 0 2
if (p < end && (*p == '+' || *p == '-')) p++; 2 0 2
if (p < end && (*p == '+' || *p == '-')) p++; 1 1 2
479 5 ▶ while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isdigit((unsigned char)*p)) p++; 3 2 5
while (p < end && isdigit((unsigned char)*p)) p++; 3 0 3
480 }
481 }
482 /* suffixes: u, l, f, ul, ll, etc. */
483 24 ▶ while (p < end && isalpha((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isalpha((unsigned char)*p)) p++; 15 9 24
while (p < end && isalpha((unsigned char)*p)) p++; 8 7 15
484 16 C_SPAN("esh-n", ts, p);
485 16 at_bol = 0;
486 16 continue;
487 }
488  
489 /* identifier or keyword */
490 408 ▶ if (eshu_hl_isalpha_(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_hl_isalpha_(c)) { 119 289 408
491 119 const char *ts = p;
492 535 ▶ while (p < end && eshu_hl_isalnum_(*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && eshu_hl_isalnum_(*p)) p++; 534 1 535
while (p < end && eshu_hl_isalnum_(*p)) p++; 416 118 534
493 119 eshu_hl_flush(&out, plain, ts);
494 119 plain = p;
495 119 ▶ if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_c_kw)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_hl_kw(ts, (size_t)(p - ts), eshu_hl_c_kw)) { 63 56 119
496 63 eshu_hl_span(&out, "esh-k", ts, p);
497 } else {
498 56 eshu_hl_write_html(&out, ts, (size_t)(p - ts));
499 }
500 119 at_bol = 0;
501 119 continue;
502 }
503  
504 /* track line starts */
505 289 ▶ if (c == '\n') { at_bol = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\n') { at_bol = 1; } 21 268 289
506 268 ▶ else if (c != ' ' && c != '\t') { at_bol = 0; }
Branch: 2 branches
BranchTrueFalseTotal
else if (c != ' ' && c != '\t') { at_bol = 0; } 119 149 268
else if (c != ' ' && c != '\t') { at_bol = 0; } 119 0 119
507 289 p++;
508 }
509  
510 57 eshu_hl_flush(&out, plain, end);
511 57 eshu_buf_putc(&out, '\0');
512 57 *out_len = out.len - 1;
513 57 return out.data;
514  
515 #undef C_SPAN
516 }
517  
518 #endif /* ESHU_C_H */
« include/eshu_json.h ↑ Summary include/eshu_xml.h »