Skip to content

Commit 1a6abcf

Browse files
committed
Replace restrict with __restrict, except for __declspec(restrict)
1 parent 441760d commit 1a6abcf

File tree

1 file changed

+79
-1
lines changed

1 file changed

+79
-1
lines changed

convert.c

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ static EndScope *end_scopes = NULL;
183183
static unsigned n_end_scopes = 0;
184184
static unsigned n_allocated_end_scopes = 0;
185185

186+
typedef struct {
187+
int n;
188+
const char *replacement;
189+
} ReplacedToken;
190+
static ReplacedToken *replaced_tokens = NULL;
191+
static unsigned n_replaced_tokens = 0;
192+
static unsigned n_allocated_replaced_tokens = 0;
193+
186194
static FILE *out;
187195

188196
static CXTranslationUnit TU;
@@ -1634,6 +1642,54 @@ static enum CXChildVisitResult callback(CXCursor cursor, CXCursor parent,
16341642
return CXChildVisit_Continue;
16351643
}
16361644

1645+
void find_tokens_to_replace(CXToken *tokens, unsigned n_tokens)
1646+
{
1647+
unsigned n;
1648+
for (n = 0; n < n_tokens; n++) {
1649+
CXString s;
1650+
const char *str;
1651+
s = clang_getTokenSpelling(TU, tokens[n]);
1652+
str = clang_getCString(s);
1653+
if (!strcmp(str, "restrict")) {
1654+
int replace = 1;
1655+
if (n > 1 && n + 1 < n_tokens) {
1656+
CXString pre1, pre2, post;
1657+
const char *str_pre1, *str_pre2, *str_post;
1658+
pre1 = clang_getTokenSpelling(TU, tokens[n - 2]);
1659+
pre2 = clang_getTokenSpelling(TU, tokens[n - 1]);
1660+
post = clang_getTokenSpelling(TU, tokens[n + 1]);
1661+
str_pre1 = clang_getCString(pre1);
1662+
str_pre2 = clang_getCString(pre2);
1663+
str_post = clang_getCString(post);
1664+
if (!strcmp(str_pre1, "__declspec") &&
1665+
!strcmp(str_pre2, "(") && !strcmp(str_post, ")"))
1666+
replace = 0;
1667+
clang_disposeString(pre1);
1668+
clang_disposeString(pre2);
1669+
clang_disposeString(post);
1670+
}
1671+
if (replace) {
1672+
ReplacedToken *rt;
1673+
if (n_replaced_tokens == n_allocated_replaced_tokens) {
1674+
unsigned num = n_allocated_replaced_tokens + 16;
1675+
void *mem = realloc(replaced_tokens,
1676+
sizeof(*replaced_tokens) * num);
1677+
if (!mem) {
1678+
fprintf(stderr, "Failed to allocate memory for str/arr\n");
1679+
exit(1);
1680+
}
1681+
replaced_tokens = (ReplacedToken *) mem;
1682+
n_allocated_replaced_tokens = num;
1683+
}
1684+
rt = &replaced_tokens[n_replaced_tokens++];
1685+
rt->n = n;
1686+
rt->replacement = "__restrict";
1687+
}
1688+
}
1689+
clang_disposeString(s);
1690+
}
1691+
}
1692+
16371693
static double eval_expr(CXToken *tokens, unsigned *n, unsigned last);
16381694

16391695
static double eval_prim(CXToken *tokens, unsigned *n, unsigned last)
@@ -2217,7 +2273,21 @@ static void print_token_wrapper(CXToken *tokens, unsigned n_tokens,
22172273
comp_literal_lists[*clidx].type == TYPE_UNKNOWN)
22182274
(*clidx)++;
22192275
} else {
2220-
print_token(tokens[*n], lnum, cpos);
2276+
// todo: Use binary search
2277+
unsigned i;
2278+
for (i = 0; i < n_replaced_tokens; i++) {
2279+
if (replaced_tokens[i].n == *n) {
2280+
CXString spelling;
2281+
print_literal_text(replaced_tokens[i].replacement, lnum, cpos);
2282+
get_token_position(tokens[*n], lnum, cpos, &off);
2283+
spelling = clang_getTokenSpelling(TU, tokens[*n]);
2284+
(*cpos) += strlen(clang_getCString(spelling));
2285+
clang_disposeString(spelling);
2286+
break;
2287+
}
2288+
}
2289+
if (i >= n_replaced_tokens)
2290+
print_token(tokens[*n], lnum, cpos);
22212291
}
22222292

22232293
while (*esidx < n_end_scopes && off >= end_scopes[*esidx].end - 1) {
@@ -2294,6 +2364,13 @@ static void cleanup(void)
22942364
}
22952365
free(end_scopes);
22962366

2367+
dprintf("N replaced tokens: %d\n", n_replaced_tokens);
2368+
for (n = 0; n < n_replaced_tokens; n++) {
2369+
dprintf("[%d]: n=%u replacement=%s\n",
2370+
n, replaced_tokens[n].n, replaced_tokens[n].replacement);
2371+
}
2372+
free(replaced_tokens);
2373+
22972374
dprintf("N typedef entries: %d\n", n_typedefs);
22982375
for (n = 0; n < n_typedefs; n++) {
22992376
if (typedefs[n].struct_decl_idx != (unsigned) -1) {
@@ -2391,6 +2468,7 @@ int convert(const char *infile, const char *outfile)
23912468
cursor = clang_getTranslationUnitCursor(TU);
23922469
range = clang_getCursorExtent(cursor);
23932470
clang_tokenize(TU, range, &tokens, &n_tokens);
2471+
find_tokens_to_replace(tokens, n_tokens);
23942472

23952473
memset(&rec, 0, sizeof(rec));
23962474
rec.tokens = tokens;

0 commit comments

Comments
 (0)