| 1 |
/** |
| 2 |
* Text macro processor for Ddoc. |
| 3 |
* |
| 4 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 5 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 6 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/dmacro.d, _dmacro.d) |
| 8 |
* Documentation: https://dlang.org/phobos/dmd_dmacro.html |
| 9 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/dmacro.d |
| 10 |
*/ |
| 11 |
|
| 12 |
module dmd.dmacro; |
| 13 |
|
| 14 |
import core.stdc.ctype; |
| 15 |
import core.stdc.string; |
| 16 |
import dmd.common.outbuffer; |
| 17 |
import dmd.root.rmem; |
| 18 |
|
| 19 |
struct MacroTable |
| 20 |
{ |
| 21 |
/********************************** |
| 22 |
* Define name=text macro. |
| 23 |
* If macro `name` already exists, replace the text for it. |
| 24 |
* Params: |
| 25 |
* name = name of macro |
| 26 |
* text = text of macro |
| 27 |
*/ |
| 28 |
void define(const(char)[] name, const(char)[] text) nothrow pure @safe |
| 29 |
{ |
| 30 |
//printf("MacroTable::define('%.*s' = '%.*s')\n", cast(int)name.length, name.ptr, text.length, text.ptr); |
| 31 |
if (auto table = name in mactab) |
| 32 |
{ |
| 33 |
(*table).text = text; |
| 34 |
return; |
| 35 |
} |
| 36 |
mactab[name] = new Macro(name, text); |
| 37 |
} |
| 38 |
|
| 39 |
alias fp_t = bool function(const(char)* p) @nogc nothrow pure; |
| 40 |
|
| 41 |
/***************************************************** |
| 42 |
* Look for macros in buf and expand them in place. |
| 43 |
* Only look at the text in buf from start to pend. |
| 44 |
* |
| 45 |
* Returns: `true` on success, `false` when the recursion limit was reached |
| 46 |
*/ |
| 47 |
bool expand(ref OutBuffer buf, size_t start, ref size_t pend, const(char)[] arg, int recursionLimit, |
| 48 |
fp_t isIdStart, fp_t isIdTail) nothrow pure |
| 49 |
{ |
| 50 |
version (none) |
| 51 |
{ |
| 52 |
printf("Macro::expand(buf[%d..%d], arg = '%.*s')\n", start, pend, cast(int)arg.length, arg.ptr); |
| 53 |
printf("Buf is: '%.*s'\n", cast(int)(pend - start), buf.data + start); |
| 54 |
} |
| 55 |
// limit recursive expansion |
| 56 |
recursionLimit--; |
| 57 |
if (recursionLimit < 0) |
| 58 |
return false; |
| 59 |
|
| 60 |
size_t end = pend; |
| 61 |
assert(start <= end); |
| 62 |
assert(end <= buf.length); |
| 63 |
/* First pass - replace $0 |
| 64 |
*/ |
| 65 |
arg = memdup(arg); |
| 66 |
for (size_t u = start; u + 1 < end;) |
| 67 |
{ |
| 68 |
char* p = cast(char*)buf[].ptr; // buf.data is not loop invariant |
| 69 |
/* Look for $0, but not $$0, and replace it with arg. |
| 70 |
*/ |
| 71 |
if (p[u] == '$' && (isdigit(p[u + 1]) || p[u + 1] == '+')) |
| 72 |
{ |
| 73 |
if (u > start && p[u - 1] == '$') |
| 74 |
{ |
| 75 |
// Don't expand $$0, but replace it with $0 |
| 76 |
buf.remove(u - 1, 1); |
| 77 |
end--; |
| 78 |
u += 1; // now u is one past the closing '1' |
| 79 |
continue; |
| 80 |
} |
| 81 |
char c = p[u + 1]; |
| 82 |
int n = (c == '+') ? -1 : c - '0'; |
| 83 |
const(char)[] marg; |
| 84 |
if (n == 0) |
| 85 |
{ |
| 86 |
marg = arg; |
| 87 |
} |
| 88 |
else |
| 89 |
extractArgN(arg, marg, n); |
| 90 |
if (marg.length == 0) |
| 91 |
{ |
| 92 |
// Just remove macro invocation |
| 93 |
//printf("Replacing '$%c' with '%.*s'\n", p[u + 1], cast(int)marg.length, marg.ptr); |
| 94 |
buf.remove(u, 2); |
| 95 |
end -= 2; |
| 96 |
} |
| 97 |
else if (c == '+') |
| 98 |
{ |
| 99 |
// Replace '$+' with 'arg' |
| 100 |
//printf("Replacing '$%c' with '%.*s'\n", p[u + 1], cast(int)marg.length, marg.ptr); |
| 101 |
buf.remove(u, 2); |
| 102 |
buf.insert(u, marg); |
| 103 |
end += marg.length - 2; |
| 104 |
// Scan replaced text for further expansion |
| 105 |
size_t mend = u + marg.length; |
| 106 |
const success = expand(buf, u, mend, null, recursionLimit, isIdStart, isIdTail); |
| 107 |
if (!success) |
| 108 |
return false; |
| 109 |
end += mend - (u + marg.length); |
| 110 |
u = mend; |
| 111 |
} |
| 112 |
else |
| 113 |
{ |
| 114 |
// Replace '$1' with '\xFF{arg\xFF}' |
| 115 |
//printf("Replacing '$%c' with '\xFF{%.*s\xFF}'\n", p[u + 1], cast(int)marg.length, marg.ptr); |
| 116 |
ubyte[] slice = cast(ubyte[])buf[]; |
| 117 |
slice[u] = 0xFF; |
| 118 |
slice[u + 1] = '{'; |
| 119 |
buf.insert(u + 2, marg); |
| 120 |
buf.insert(u + 2 + marg.length, "\xFF}"); |
| 121 |
end += -2 + 2 + marg.length + 2; |
| 122 |
// Scan replaced text for further expansion |
| 123 |
size_t mend = u + 2 + marg.length; |
| 124 |
const success = expand(buf, u + 2, mend, null, recursionLimit, isIdStart, isIdTail); |
| 125 |
if (!success) |
| 126 |
return false; |
| 127 |
end += mend - (u + 2 + marg.length); |
| 128 |
u = mend; |
| 129 |
} |
| 130 |
//printf("u = %d, end = %d\n", u, end); |
| 131 |
//printf("#%.*s#\n", cast(int)end, &buf.data[0]); |
| 132 |
continue; |
| 133 |
} |
| 134 |
u++; |
| 135 |
} |
| 136 |
/* Second pass - replace other macros |
| 137 |
*/ |
| 138 |
for (size_t u = start; u + 4 < end;) |
| 139 |
{ |
| 140 |
char* p = cast(char*)buf[].ptr; // buf.data is not loop invariant |
| 141 |
/* A valid start of macro expansion is $(c, where c is |
| 142 |
* an id start character, and not $$(c. |
| 143 |
*/ |
| 144 |
if (p[u] == '$' && p[u + 1] == '(' && isIdStart(p + u + 2)) |
| 145 |
{ |
| 146 |
//printf("\tfound macro start '%c'\n", p[u + 2]); |
| 147 |
char* name = p + u + 2; |
| 148 |
size_t namelen = 0; |
| 149 |
const(char)[] marg; |
| 150 |
size_t v; |
| 151 |
/* Scan forward to find end of macro name and |
| 152 |
* beginning of macro argument (marg). |
| 153 |
*/ |
| 154 |
for (v = u + 2; v < end; v += utfStride(p[v])) |
| 155 |
{ |
| 156 |
if (!isIdTail(p + v)) |
| 157 |
{ |
| 158 |
// We've gone past the end of the macro name. |
| 159 |
namelen = v - (u + 2); |
| 160 |
break; |
| 161 |
} |
| 162 |
} |
| 163 |
v += extractArgN(p[v .. end], marg, 0); |
| 164 |
assert(v <= end); |
| 165 |
if (v < end) |
| 166 |
{ |
| 167 |
// v is on the closing ')' |
| 168 |
if (u > start && p[u - 1] == '$') |
| 169 |
{ |
| 170 |
// Don't expand $$(NAME), but replace it with $(NAME) |
| 171 |
buf.remove(u - 1, 1); |
| 172 |
end--; |
| 173 |
u = v; // now u is one past the closing ')' |
| 174 |
continue; |
| 175 |
} |
| 176 |
Macro* m = search(name[0 .. namelen]); |
| 177 |
if (!m) |
| 178 |
{ |
| 179 |
immutable undef = "DDOC_UNDEFINED_MACRO"; |
| 180 |
m = search(undef); |
| 181 |
if (m) |
| 182 |
{ |
| 183 |
// Macro was not defined, so this is an expansion of |
| 184 |
// DDOC_UNDEFINED_MACRO. Prepend macro name to args. |
| 185 |
// marg = name[ ] ~ "," ~ marg[ ]; |
| 186 |
if (marg.length) |
| 187 |
{ |
| 188 |
char* q = cast(char*)mem.xmalloc(namelen + 1 + marg.length); |
| 189 |
assert(q); |
| 190 |
memcpy(q, name, namelen); |
| 191 |
q[namelen] = ','; |
| 192 |
memcpy(q + namelen + 1, marg.ptr, marg.length); |
| 193 |
marg = q[0 .. marg.length + namelen + 1]; |
| 194 |
} |
| 195 |
else |
| 196 |
{ |
| 197 |
marg = name[0 .. namelen]; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
if (m) |
| 202 |
{ |
| 203 |
if (m.inuse && marg.length == 0) |
| 204 |
{ |
| 205 |
// Remove macro invocation |
| 206 |
buf.remove(u, v + 1 - u); |
| 207 |
end -= v + 1 - u; |
| 208 |
} |
| 209 |
else if (m.inuse && ((arg.length == marg.length && memcmp(arg.ptr, marg.ptr, arg.length) == 0) || |
| 210 |
(arg.length + 4 == marg.length && marg[0] == 0xFF && marg[1] == '{' && memcmp(arg.ptr, marg.ptr + 2, arg.length) == 0 && marg[marg.length - 2] == 0xFF && marg[marg.length - 1] == '}'))) |
| 211 |
{ |
| 212 |
/* Recursive expansion: |
| 213 |
* marg is same as arg (with blue paint added) |
| 214 |
* Just leave in place. |
| 215 |
*/ |
| 216 |
} |
| 217 |
else |
| 218 |
{ |
| 219 |
//printf("\tmacro '%.*s'(%.*s) = '%.*s'\n", cast(int)m.namelen, m.name, cast(int)marg.length, marg.ptr, cast(int)m.textlen, m.text); |
| 220 |
marg = memdup(marg); |
| 221 |
// Insert replacement text |
| 222 |
buf.spread(v + 1, 2 + m.text.length + 2); |
| 223 |
ubyte[] slice = cast(ubyte[])buf[]; |
| 224 |
slice[v + 1] = 0xFF; |
| 225 |
slice[v + 2] = '{'; |
| 226 |
slice[v + 3 .. v + 3 + m.text.length] = cast(ubyte[])m.text[]; |
| 227 |
slice[v + 3 + m.text.length] = 0xFF; |
| 228 |
slice[v + 3 + m.text.length + 1] = '}'; |
| 229 |
end += 2 + m.text.length + 2; |
| 230 |
// Scan replaced text for further expansion |
| 231 |
m.inuse++; |
| 232 |
size_t mend = v + 1 + 2 + m.text.length + 2; |
| 233 |
const success = expand(buf, v + 1, mend, marg, recursionLimit, isIdStart, isIdTail); |
| 234 |
if (!success) |
| 235 |
return false; |
| 236 |
end += mend - (v + 1 + 2 + m.text.length + 2); |
| 237 |
m.inuse--; |
| 238 |
buf.remove(u, v + 1 - u); |
| 239 |
end -= v + 1 - u; |
| 240 |
u += mend - (v + 1); |
| 241 |
mem.xfree(cast(char*)marg.ptr); |
| 242 |
//printf("u = %d, end = %d\n", u, end); |
| 243 |
//printf("#%.*s#\n", cast(int)(end - u), &buf.data[u]); |
| 244 |
continue; |
| 245 |
} |
| 246 |
} |
| 247 |
else |
| 248 |
{ |
| 249 |
// Replace $(NAME) with nothing |
| 250 |
buf.remove(u, v + 1 - u); |
| 251 |
end -= (v + 1 - u); |
| 252 |
continue; |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
u++; |
| 257 |
} |
| 258 |
mem.xfree(cast(char*)arg); |
| 259 |
pend = end; |
| 260 |
return true; |
| 261 |
} |
| 262 |
|
| 263 |
private: |
| 264 |
|
| 265 |
Macro* search(const(char)[] name) @nogc nothrow pure @safe |
| 266 |
{ |
| 267 |
//printf("Macro::search(%.*s)\n", cast(int)name.length, name.ptr); |
| 268 |
if (auto table = name in mactab) |
| 269 |
{ |
| 270 |
//printf("\tfound %d\n", table.textlen); |
| 271 |
return *table; |
| 272 |
} |
| 273 |
return null; |
| 274 |
} |
| 275 |
|
| 276 |
private Macro*[const(char)[]] mactab; |
| 277 |
} |
| 278 |
|
| 279 |
/* ************************************************************************ */ |
| 280 |
|
| 281 |
private: |
| 282 |
|
| 283 |
struct Macro |
| 284 |
{ |
| 285 |
const(char)[] name; // macro name |
| 286 |
const(char)[] text; // macro replacement text |
| 287 |
int inuse; // macro is in use (don't expand) |
| 288 |
|
| 289 |
this(const(char)[] name, const(char)[] text) @nogc nothrow pure @safe |
| 290 |
{ |
| 291 |
this.name = name; |
| 292 |
this.text = text; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/************************ |
| 297 |
* Make mutable copy of slice p. |
| 298 |
* Params: |
| 299 |
* p = slice |
| 300 |
* Returns: |
| 301 |
* copy allocated with mem.xmalloc() |
| 302 |
*/ |
| 303 |
|
| 304 |
char[] memdup(const(char)[] p) nothrow pure @trusted |
| 305 |
{ |
| 306 |
size_t len = p.length; |
| 307 |
return (cast(char*)memcpy(mem.xmalloc(len), p.ptr, len))[0 .. len]; |
| 308 |
} |
| 309 |
|
| 310 |
/********************************************************** |
| 311 |
* Given buffer buf[], extract argument marg[]. |
| 312 |
* Params: |
| 313 |
* buf = source string |
| 314 |
* marg = set to slice of buf[] |
| 315 |
* n = 0: get entire argument |
| 316 |
* 1..9: get nth argument |
| 317 |
* -1: get 2nd through end |
| 318 |
*/ |
| 319 |
size_t extractArgN(const(char)[] buf, out const(char)[] marg, int n) @nogc nothrow pure @safe |
| 320 |
{ |
| 321 |
/* Scan forward for matching right parenthesis. |
| 322 |
* Nest parentheses. |
| 323 |
* Skip over "..." and '...' strings inside HTML tags. |
| 324 |
* Skip over <!-- ... --> comments. |
| 325 |
* Skip over previous macro insertions |
| 326 |
* Set marg. |
| 327 |
*/ |
| 328 |
uint parens = 1; |
| 329 |
ubyte instring = 0; |
| 330 |
uint incomment = 0; |
| 331 |
uint intag = 0; |
| 332 |
uint inexp = 0; |
| 333 |
uint argn = 0; |
| 334 |
size_t v = 0; |
| 335 |
const p = buf; |
| 336 |
const end = buf.length; |
| 337 |
Largstart: |
| 338 |
// Skip first space, if any, to find the start of the macro argument |
| 339 |
if (n != 1 && v < end && isspace(p[v])) |
| 340 |
v++; |
| 341 |
size_t vstart = v; |
| 342 |
for (; v < end; v++) |
| 343 |
{ |
| 344 |
char c = p[v]; |
| 345 |
switch (c) |
| 346 |
{ |
| 347 |
case ',': |
| 348 |
if (!inexp && !instring && !incomment && parens == 1) |
| 349 |
{ |
| 350 |
argn++; |
| 351 |
if (argn == 1 && n == -1) |
| 352 |
{ |
| 353 |
v++; |
| 354 |
goto Largstart; |
| 355 |
} |
| 356 |
if (argn == n) |
| 357 |
break; |
| 358 |
if (argn + 1 == n) |
| 359 |
{ |
| 360 |
v++; |
| 361 |
goto Largstart; |
| 362 |
} |
| 363 |
} |
| 364 |
continue; |
| 365 |
case '(': |
| 366 |
if (!inexp && !instring && !incomment) |
| 367 |
parens++; |
| 368 |
continue; |
| 369 |
case ')': |
| 370 |
if (!inexp && !instring && !incomment && --parens == 0) |
| 371 |
{ |
| 372 |
break; |
| 373 |
} |
| 374 |
continue; |
| 375 |
case '"': |
| 376 |
case '\'': |
| 377 |
if (!inexp && !incomment && intag) |
| 378 |
{ |
| 379 |
if (c == instring) |
| 380 |
instring = 0; |
| 381 |
else if (!instring) |
| 382 |
instring = c; |
| 383 |
} |
| 384 |
continue; |
| 385 |
case '<': |
| 386 |
if (!inexp && !instring && !incomment) |
| 387 |
{ |
| 388 |
if (v + 6 < end && p[v + 1] == '!' && p[v + 2] == '-' && p[v + 3] == '-') |
| 389 |
{ |
| 390 |
incomment = 1; |
| 391 |
v += 3; |
| 392 |
} |
| 393 |
else if (v + 2 < end && isalpha(p[v + 1])) |
| 394 |
intag = 1; |
| 395 |
} |
| 396 |
continue; |
| 397 |
case '>': |
| 398 |
if (!inexp) |
| 399 |
intag = 0; |
| 400 |
continue; |
| 401 |
case '-': |
| 402 |
if (!inexp && !instring && incomment && v + 2 < end && p[v + 1] == '-' && p[v + 2] == '>') |
| 403 |
{ |
| 404 |
incomment = 0; |
| 405 |
v += 2; |
| 406 |
} |
| 407 |
continue; |
| 408 |
case 0xFF: |
| 409 |
if (v + 1 < end) |
| 410 |
{ |
| 411 |
if (p[v + 1] == '{') |
| 412 |
inexp++; |
| 413 |
else if (p[v + 1] == '}') |
| 414 |
inexp--; |
| 415 |
} |
| 416 |
continue; |
| 417 |
default: |
| 418 |
continue; |
| 419 |
} |
| 420 |
break; |
| 421 |
} |
| 422 |
if (argn == 0 && n == -1) |
| 423 |
marg = p[v .. v]; |
| 424 |
else |
| 425 |
marg = p[vstart .. v]; |
| 426 |
//printf("extractArg%d('%.*s') = '%.*s'\n", n, cast(int)end, p, cast(int)marg.length, marg.ptr); |
| 427 |
return v; |
| 428 |
} |
| 429 |
|
| 430 |
/***************************************** |
| 431 |
* Get number of UTF-8 code units in code point that starts with `c` |
| 432 |
* Params: |
| 433 |
* c = starting code unit |
| 434 |
* Returns: number of UTF-8 code units (i.e. bytes), else 1 on invalid UTF start |
| 435 |
*/ |
| 436 |
@safe |
| 437 |
int utfStride(char c) @nogc nothrow pure |
| 438 |
{ |
| 439 |
return |
| 440 |
c < 0x80 ? 1 : |
| 441 |
c < 0xC0 ? 1 : // invalid UTF start |
| 442 |
c < 0xE0 ? 2 : |
| 443 |
c < 0xF0 ? 3 : |
| 444 |
c < 0xF8 ? 4 : |
| 445 |
c < 0xFC ? 5 : |
| 446 |
c < 0xFE ? 6 : |
| 447 |
1; // invalid UTF start |
| 448 |
} |
| 449 |
|
| 450 |
unittest |
| 451 |
{ |
| 452 |
assert(utfStride(0) == 1); |
| 453 |
assert(utfStride(0x80) == 1); |
| 454 |
assert(utfStride(0xC0) == 2); |
| 455 |
assert(utfStride(0xE0) == 3); |
| 456 |
assert(utfStride(0xF0) == 4); |
| 457 |
assert(utfStride(0xF8) == 5); |
| 458 |
assert(utfStride(0xFC) == 6); |
| 459 |
assert(utfStride(0xFE) == 1); |
| 460 |
} |