Skip to content

Commit 129faf5

Browse files
C: Better class name and macro name detection (#2585)
1 parent 8e1f38f commit 129faf5

File tree

8 files changed

+138
-86
lines changed

8 files changed

+138
-86
lines changed

components/prism-c.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Prism.languages.c = Prism.languages.extend('clike', {
44
greedy: true
55
},
66
'class-name': {
7-
pattern: /(\b(?:enum|struct)\s+(?:__attribute__\s*\(\([\s\S]*?\)\)\s*)?)\w+/,
7+
pattern: /(\b(?:enum|struct)\s+(?:__attribute__\s*\(\([\s\S]*?\)\)\s*)?)\w+|\b[a-z]\w*_t\b/,
88
lookbehind: true
99
},
1010
'keyword': /\b(?:__attribute__|_Alignas|_Alignof|_Atomic|_Bool|_Complex|_Generic|_Imaginary|_Noreturn|_Static_assert|_Thread_local|asm|typeof|inline|auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while)\b/,
@@ -31,6 +31,17 @@ Prism.languages.insertBefore('c', 'string', {
3131
Prism.languages.c['string']
3232
],
3333
'comment': Prism.languages.c['comment'],
34+
'macro-name': [
35+
{
36+
pattern: /(^#\s*define\s+)\w+\b(?!\()/i,
37+
lookbehind: true
38+
},
39+
{
40+
pattern: /(^#\s*define\s+)\w+\b(?=\()/i,
41+
lookbehind: true,
42+
alias: 'function'
43+
}
44+
],
3445
// highlight macro directives as keywords
3546
'directive': {
3647
pattern: /^(#\s*)[a-z]+/,

components/prism-c.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/prism-opencl.js

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/prism-opencl.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/c/class-name_feature.test

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
struct foo
2-
enum bar
1+
struct foo;
2+
enum bar;
33

44
struct foo var;
55
struct __attribute__ ((aligned (8))) S { short f[3]; };
66

7+
// by name
8+
uint32_t foo;
9+
static dtrace_helptrace_t *bar;
10+
711
----------------------------------------------------
812

913
[
1014
["keyword", "struct"],
1115
["class-name", "foo"],
16+
["punctuation", ";"],
1217
["keyword", "enum"],
1318
["class-name", "bar"],
19+
["punctuation", ";"],
1420

1521
["keyword", "struct"],
1622
["class-name", "foo"],
@@ -35,9 +41,19 @@ struct __attribute__ ((aligned (8))) S { short f[3]; };
3541
["punctuation", "]"],
3642
["punctuation", ";"],
3743
["punctuation", "}"],
44+
["punctuation", ";"],
45+
46+
["comment", "// by name"],
47+
["class-name", "uint32_t"],
48+
" foo",
49+
["punctuation", ";"],
50+
["keyword", "static"],
51+
["class-name", "dtrace_helptrace_t"],
52+
["operator", "*"],
53+
"bar",
3854
["punctuation", ";"]
3955
]
4056

4157
----------------------------------------------------
4258

43-
Checks for structs and enums.
59+
Checks for structs and enums.

tests/languages/c/macro_feature.test

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/ 1
2525

2626
#define FOO 1 // trailing comment
27+
#define FOO (1 + 1)
2728

2829
#define MAX(a, b) \
2930
((a) < (b) ? (b) : (a))
@@ -46,8 +47,8 @@
4647
["macro", [
4748
["directive-hash", "#"],
4849
["directive", "define"],
50+
["macro-name", "PG_locked"],
4951
["expression", [
50-
"PG_locked ",
5152
["number", "0"]
5253
]]
5354
]],
@@ -117,9 +118,7 @@
117118
["macro", [
118119
["directive-hash", "#"],
119120
["directive", "define"],
120-
["expression", [
121-
"FOO "
122-
]],
121+
["macro-name", "FOO"],
123122
["comment", "/*\r\n comment\r\n*/"],
124123
["expression", [
125124
["number", "1"]
@@ -129,18 +128,30 @@
129128
["macro", [
130129
["directive-hash", "#"],
131130
["directive", "define"],
131+
["macro-name", "FOO"],
132132
["expression", [
133-
"FOO ",
134133
["number", "1"]
135134
]],
136135
["comment", "// trailing comment"]
137136
]],
137+
["macro", [
138+
["directive-hash", "#"],
139+
["directive", "define"],
140+
["macro-name", "FOO"],
141+
["expression", [
142+
["punctuation", "("],
143+
["number", "1"],
144+
["operator", "+"],
145+
["number", "1"],
146+
["punctuation", ")"]
147+
]]
148+
]],
138149

139150
["macro", [
140151
["directive-hash", "#"],
141152
["directive", "define"],
153+
["macro-name", "MAX"],
142154
["expression", [
143-
["function", "MAX"],
144155
["punctuation", "("],
145156
"a",
146157
["punctuation", ","],
@@ -172,8 +183,8 @@
172183
["macro", [
173184
["directive-hash", "#"],
174185
["directive", "define"],
186+
["macro-name", "BAR"],
175187
["expression", [
176-
["function", "BAR"],
177188
["punctuation", "("],
178189
"s",
179190
["punctuation", ")"],
@@ -188,4 +199,4 @@
188199

189200
----------------------------------------------------
190201

191-
Checks for macros and paths inside include statements.
202+
Checks for macros and paths inside include statements.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
_cl_command_queue
2+
_cl_context
3+
_cl_device_id
4+
_cl_event
5+
_cl_kernel
6+
_cl_mem
7+
_cl_platform_id
8+
_cl_program
9+
_cl_sampler
10+
cl_image_format
11+
cl_mem_fence_flags
12+
clk_event_t
13+
event_t
14+
image1d_array_t
15+
image1d_buffer_t
16+
image1d_t
17+
image2d_array_depth_t
18+
image2d_array_msaa_depth_t
19+
image2d_array_msaa_t
20+
image2d_array_t
21+
image2d_depth_t
22+
image2d_msaa_depth_t
23+
image2d_msaa_t
24+
image2d_t
25+
image3d_t
26+
intptr_t
27+
ndrange_t
28+
ptrdiff_t
29+
queue_t
30+
reserve_id_t
31+
sampler_t
32+
size_t
33+
uintptr_t
34+
35+
----------------------------------------------------
36+
37+
[
38+
["builtin-type", "_cl_command_queue"],
39+
["builtin-type", "_cl_context"],
40+
["builtin-type", "_cl_device_id"],
41+
["builtin-type", "_cl_event"],
42+
["builtin-type", "_cl_kernel"],
43+
["builtin-type", "_cl_mem"],
44+
["builtin-type", "_cl_platform_id"],
45+
["builtin-type", "_cl_program"],
46+
["builtin-type", "_cl_sampler"],
47+
["builtin-type", "cl_image_format"],
48+
["builtin-type", "cl_mem_fence_flags"],
49+
["builtin-type", "clk_event_t"],
50+
["builtin-type", "event_t"],
51+
["builtin-type", "image1d_array_t"],
52+
["builtin-type", "image1d_buffer_t"],
53+
["builtin-type", "image1d_t"],
54+
["builtin-type", "image2d_array_depth_t"],
55+
["builtin-type", "image2d_array_msaa_depth_t"],
56+
["builtin-type", "image2d_array_msaa_t"],
57+
["builtin-type", "image2d_array_t"],
58+
["builtin-type", "image2d_depth_t"],
59+
["builtin-type", "image2d_msaa_depth_t"],
60+
["builtin-type", "image2d_msaa_t"],
61+
["builtin-type", "image2d_t"],
62+
["builtin-type", "image3d_t"],
63+
["builtin-type", "intptr_t"],
64+
["builtin-type", "ndrange_t"],
65+
["builtin-type", "ptrdiff_t"],
66+
["builtin-type", "queue_t"],
67+
["builtin-type", "reserve_id_t"],
68+
["builtin-type", "sampler_t"],
69+
["builtin-type", "size_t"],
70+
["builtin-type", "uintptr_t"]
71+
]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy