Skip to content

Commit

Permalink
C/C++/OpenCL C: Improvements (#2196)
Browse files Browse the repository at this point in the history
This makes a few minor improvements to C and OpenCL C. Some of those improvements carry over to C++.

For more details, see the pull request.
  • Loading branch information
RunDevelopment committed Feb 18, 2020
1 parent 47910b5 commit 674f4b3
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 632 deletions.
5 changes: 3 additions & 2 deletions components/prism-c.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
Prism.languages.c = Prism.languages.extend('clike', {
'class-name': {
pattern: /(\b(?:enum|struct)\s+)\w+/,
pattern: /(\b(?:enum|struct)\s+(?:__attribute__\s*\(\([\s\S]*?\)\)\s*)?)\w+/,
lookbehind: true
},
'keyword': /\b(?:_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/,
'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/,
'function': /[a-z_]\w*(?=\s*\()/i,
'operator': />>=?|<<=?|->|([-+&|:])\1|[?:~]|[-+*/%&|^!=<>]=?/,
'number': /(?:\b0x(?:[\da-f]+\.?[\da-f]*|\.[\da-f]+)(?:p[+-]?\d+)?|(?:\b\d+\.?\d*|\B\.\d+)(?:e[+-]?\d+)?)[ful]*/i
});
Expand Down
2 changes: 1 addition & 1 deletion components/prism-c.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions components/prism-opencl.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/prism-opencl.min.js

Large diffs are not rendered by default.

32 changes: 29 additions & 3 deletions tests/languages/c/class-name_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,40 @@ struct foo
enum bar

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

----------------------------------------------------

[
["keyword", "struct"], ["class-name", "foo"],
["keyword", "enum"], ["class-name", "bar"],
["keyword", "struct"],
["class-name", "foo"],
["keyword", "enum"],
["class-name", "bar"],

["keyword", "struct"], ["class-name", "foo"], " var", ["punctuation", ";"]
["keyword", "struct"],
["class-name", "foo"],
" var",
["punctuation", ";"],
["keyword", "struct"],
["keyword", "__attribute__"],
["punctuation", "("],
["punctuation", "("],
["function", "aligned"],
["punctuation", "("],
["number", "8"],
["punctuation", ")"],
["punctuation", ")"],
["punctuation", ")"],
["class-name", "S"],
["punctuation", "{"],
["keyword", "short"],
" f",
["punctuation", "["],
["number", "3"],
["punctuation", "]"],
["punctuation", ";"],
["punctuation", "}"],
["punctuation", ";"]
]

----------------------------------------------------
Expand Down
24 changes: 24 additions & 0 deletions tests/languages/c/function_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
foo(void);
bar
(1, 2);

----------------------------------------------------

[
["function", "foo"],
["punctuation", "("],
["keyword", "void"],
["punctuation", ")"],
["punctuation", ";"],
["function", "bar"],
["punctuation", "("],
["number", "1"],
["punctuation", ","],
["number", "2"],
["punctuation", ")"],
["punctuation", ";"]
]

----------------------------------------------------

Checks for functions.
2 changes: 2 additions & 0 deletions tests/languages/c/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__attribute__
_Alignas _Alignof _Atomic _Bool
_Complex _Generic _Imaginary
_Noreturn _Static_assert _Thread_local
Expand All @@ -14,6 +15,7 @@ union unsigned void volatile while
----------------------------------------------------

[
["keyword", "__attribute__"],
["keyword", "_Alignas"], ["keyword", "_Alignof"], ["keyword", "_Atomic"], ["keyword", "_Bool"],
["keyword", "_Complex"], ["keyword", "_Generic"], ["keyword", "_Imaginary"],
["keyword", "_Noreturn"], ["keyword", "_Static_assert"], ["keyword", "_Thread_local"],
Expand Down
13 changes: 13 additions & 0 deletions tests/languages/opencl/boolean_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
false
true

----------------------------------------------------

[
["boolean", "false"],
["boolean", "true"]
]

----------------------------------------------------

Checks for booleans in OpenCL kernel code.
106 changes: 89 additions & 17 deletions tests/languages/opencl/constant_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@ FLT_MIN
FLT_MIN_10_EXP
FLT_MIN_EXP
FLT_RADIX
HALF_DIG
HALF_EPSILON
HALF_MANT_DIG
HALF_MAX
HALF_MAX_10_EXP
HALF_MAX_EXP
HALF_MIN
HALF_MIN_10_EXP
HALF_MIN_EXP
HUGE_VALF
HUGE_VAL
INFINITY
INT_MAX
INT_MIN
Expand All @@ -73,23 +83,49 @@ M_LN2
M_LOG10E
M_LOG2E
M_PI
M_PI2
M_PI4
M_PI_2
M_PI_4
M_SQRT1_2
M_SQRT2
M_1_PI_F
M_2_PI_F
M_2_SQRTPI_F
M_E_F
M_LN10_F
M_LN2_F
M_LOG10E_F
M_LOG2E_F
M_PI_F
M_PI_2_F
M_PI_4_F
M_SQRT1_2_F
M_SQRT2_F
M_1_PI_H
M_2_PI_H
M_2_SQRTPI_H
M_E_H
M_LN10_H
M_LN2_H
M_LOG10E_H
M_LOG2E_H
M_PI_H
M_PI_2_H
M_PI_4_H
M_SQRT1_2_H
M_SQRT2_H
NAN
SCHAR_MAX
SCHAR_MIN
SHRT_MAX
SHRT_MIN
UCHAR_MAX
UCHAR_MIN
USHRT_MAX
UINT_MAX
ULONG_MAX
----------------------------------------------------
[
ULONG_MAX

----------------------------------------------------

[
["constant-opencl-kernel", "CHAR_BIT"],
["constant-opencl-kernel", "CHAR_MAX"],
["constant-opencl-kernel", "CHAR_MIN"],
Expand Down Expand Up @@ -149,7 +185,17 @@ ULONG_MAX
["constant-opencl-kernel", "FLT_MIN_10_EXP"],
["constant-opencl-kernel", "FLT_MIN_EXP"],
["constant-opencl-kernel", "FLT_RADIX"],
["constant-opencl-kernel", "HALF_DIG"],
["constant-opencl-kernel", "HALF_EPSILON"],
["constant-opencl-kernel", "HALF_MANT_DIG"],
["constant-opencl-kernel", "HALF_MAX"],
["constant-opencl-kernel", "HALF_MAX_10_EXP"],
["constant-opencl-kernel", "HALF_MAX_EXP"],
["constant-opencl-kernel", "HALF_MIN"],
["constant-opencl-kernel", "HALF_MIN_10_EXP"],
["constant-opencl-kernel", "HALF_MIN_EXP"],
["constant-opencl-kernel", "HUGE_VALF"],
["constant-opencl-kernel", "HUGE_VAL"],
["constant-opencl-kernel", "INFINITY"],
["constant-opencl-kernel", "INT_MAX"],
["constant-opencl-kernel", "INT_MIN"],
Expand All @@ -165,21 +211,47 @@ ULONG_MAX
["constant-opencl-kernel", "M_LOG10E"],
["constant-opencl-kernel", "M_LOG2E"],
["constant-opencl-kernel", "M_PI"],
["constant-opencl-kernel", "M_PI2"],
["constant-opencl-kernel", "M_PI4"],
["constant-opencl-kernel", "M_PI_2"],
["constant-opencl-kernel", "M_PI_4"],
["constant-opencl-kernel", "M_SQRT1_2"],
["constant-opencl-kernel", "M_SQRT2"],
["constant-opencl-kernel", "M_1_PI_F"],
["constant-opencl-kernel", "M_2_PI_F"],
["constant-opencl-kernel", "M_2_SQRTPI_F"],
["constant-opencl-kernel", "M_E_F"],
["constant-opencl-kernel", "M_LN10_F"],
["constant-opencl-kernel", "M_LN2_F"],
["constant-opencl-kernel", "M_LOG10E_F"],
["constant-opencl-kernel", "M_LOG2E_F"],
["constant-opencl-kernel", "M_PI_F"],
["constant-opencl-kernel", "M_PI_2_F"],
["constant-opencl-kernel", "M_PI_4_F"],
["constant-opencl-kernel", "M_SQRT1_2_F"],
["constant-opencl-kernel", "M_SQRT2_F"],
["constant-opencl-kernel", "M_1_PI_H"],
["constant-opencl-kernel", "M_2_PI_H"],
["constant-opencl-kernel", "M_2_SQRTPI_H"],
["constant-opencl-kernel", "M_E_H"],
["constant-opencl-kernel", "M_LN10_H"],
["constant-opencl-kernel", "M_LN2_H"],
["constant-opencl-kernel", "M_LOG10E_H"],
["constant-opencl-kernel", "M_LOG2E_H"],
["constant-opencl-kernel", "M_PI_H"],
["constant-opencl-kernel", "M_PI_2_H"],
["constant-opencl-kernel", "M_PI_4_H"],
["constant-opencl-kernel", "M_SQRT1_2_H"],
["constant-opencl-kernel", "M_SQRT2_H"],
["constant-opencl-kernel", "NAN"],
["constant-opencl-kernel", "SCHAR_MAX"],
["constant-opencl-kernel", "SCHAR_MIN"],
["constant-opencl-kernel", "SHRT_MAX"],
["constant-opencl-kernel", "SHRT_MIN"],
["constant-opencl-kernel", "UCHAR_MAX"],
["constant-opencl-kernel", "UCHAR_MIN"],
["constant-opencl-kernel", "USHRT_MAX"],
["constant-opencl-kernel", "UINT_MAX"],
["constant-opencl-kernel", "ULONG_MAX"]
]
----------------------------------------------------
Checks for all constant names in OpenCL kernel code.
["constant-opencl-kernel", "ULONG_MAX"]
]

----------------------------------------------------

Checks for all constant names in OpenCL kernel code.
Loading

0 comments on commit 674f4b3

Please sign in to comment.