From 913d32db79178761acca75943fb8239ad635d09b Mon Sep 17 00:00:00 2001
From: Saad <57033728+akhtarmdsaad@users.noreply.github.com>
Date: Mon, 26 Feb 2024 12:04:06 +0000
Subject: [PATCH 1/4] Added list of false positiitives
---
src/languages/cpp.js | 48 +++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 47 insertions(+), 1 deletion(-)
diff --git a/src/languages/cpp.js b/src/languages/cpp.js
index 64aee1d44e..cc60170186 100644
--- a/src/languages/cpp.js
+++ b/src/languages/cpp.js
@@ -428,10 +428,56 @@ export default function(hljs) {
/(?!for)/,
/(?!switch)/,
/(?!while)/,
+ /(?!alignas)/,
+ /(?!alignof)/,
+ /(?!asm)/,
+ /(?!catch)/,
+ /(?!const_cast)/,
+ /(?!dynamic_cast)/,
+ /(?!noexcept)/,
+ /(?!reinterpret_cast)/,
+ /(?!sizeof)/,
+ /(?!static_assert)/,
+ /(?!static_cast)/,
+ /(?!typeid)/,
+ /(?!requires)/,
+ /(?!explicit)/,
+ /(?!case)/,
+ /(?!delete)/,
+
+ // compound operators
+ /(?!bitand_eq)/,
+ /(?!bitor_eq)/,
+ /(?!xor_eq)/,
+ /(?!not_eq)/,
+ /(?!or_eq)/,
+ /(?!and_eq)/,
+
+ // operators
+ /(?!and)/,
+ /(?!or)/,
+ /(?!bitand)/,
+ /(?!bitor)/,
+ /(?!xor)/,
+
+ // unary operators
+ /(?!not)/,
+ /(?!compl)/,
+ /(?!co_await)/,
+ /(?!co_return)/,
+ /(?!co_yield)/,
+
+ // Reserved types
+ /(?!int)/,
+ /(?!char)/,
+ /(?!double)/,
+ /(?!float)/,
+ /(?!bool)/,
+ /(?!auto)/,
+
hljs.IDENT_RE,
regex.lookahead(/(<[^<>]+>|)\s*\(/))
};
-
const EXPRESSION_CONTAINS = [
FUNCTION_DISPATCH,
PREPROCESSOR,
From 89853e26257e6bae230d904235484d1e7c8b82b5 Mon Sep 17 00:00:00 2001
From: Saad <57033728+akhtarmdsaad@users.noreply.github.com>
Date: Mon, 26 Feb 2024 13:10:55 +0000
Subject: [PATCH 2/4] Added markup tests
---
CHANGES.md | 2 +-
.../cpp/keywords-with-parentheses.expect.txt | 93 +++++++++++++++++++
test/markup/cpp/keywords-with-parentheses.txt | 93 +++++++++++++++++++
.../markup/cpp/template-complexity.expect.txt | 2 +-
4 files changed, 188 insertions(+), 2 deletions(-)
create mode 100644 test/markup/cpp/keywords-with-parentheses.expect.txt
create mode 100644 test/markup/cpp/keywords-with-parentheses.txt
diff --git a/CHANGES.md b/CHANGES.md
index 70d3e1db47..11505701d8 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -6,6 +6,7 @@ CAVEATS / POTENTIALLY BREAKING CHANGES
Core Grammars:
+- fix(cpp) Keywords followed by parens are treated as built_in[Md Saad Akhtar][]
- enh(perl) fix false-positive variable match at end of string [Josh Goebel][]
- fix(cpp) not all kinds of number literals are highlighted correctly [LĂȘ Duy Quang][]
- fix(css) fix overly greedy pseudo class matching [Bradley Mackey][]
@@ -96,7 +97,6 @@ New Grammars:
- added 3rd party Ballerina grammar to SUPPORTED_LANGUAGES [Yasith Deelaka][]
Core Grammars:
-
- fix(rust) added negative-lookahead for callable keywords `if` `while` `for` [Omar Hussein][]
- enh(armasm) added `x0-x30` and `w0-w30` ARMv8 registers [Nicholas Thompson][]
- enh(haxe) added `final`, `is`, `macro` keywords and `$` identifiers [Robert Borghese][]
diff --git a/test/markup/cpp/keywords-with-parentheses.expect.txt b/test/markup/cpp/keywords-with-parentheses.expect.txt
new file mode 100644
index 0000000000..a4d21c9e5f
--- /dev/null
+++ b/test/markup/cpp/keywords-with-parentheses.expect.txt
@@ -0,0 +1,93 @@
+alignas(16) char aligned_buffer[1024];
+alignof(decltype(aligned_buffer))
+asm("movl $1, %eax");
+try {
+ throw std::runtime_error("An exception occurred");
+} catch (const std::exception& e) {
+ std::cout << "Caught exception: " << e.what() << std::endl;
+}
+const int* p = nullptr;
+int* mutable_p = const_cast<int*>(p);
+int x = 5;
+decltype(x) y = 10;
+Animal* animal = new Dog();
+if (Dog* dog = dynamic_cast<Dog*>(animal)) {
+ std::cout << "Dynamic cast successful" << std::endl;
+} else {
+ std::cout << "Dynamic cast failed" << std::endl;
+}
+int add(int a, int b) noexcept {
+ return a + b;
+}
+if (noexcept(add(1, 2))) {
+
+} else {
+
+}
+int value = 10;
+double* ptr = reinterpret_cast<double*>(&value);
+std::cout << "Size of int: " << sizeof(int) << " bytes" << std::endl;
+static_assert(sizeof(int) == 4, "int must be 4 bytes");
+float z = static_cast<float>(10);
+int choice = 2;
+switch(choice) {
+ case 1:
+ std::cout << "Choice is 1" << std::endl;
+ break;
+ case 2:
+ std::cout << "Choice is 2" << std::endl;
+ break;
+ default:
+ std::cout << "Choice is not 1 or 2" << std::endl;
+}
+std::cout << "Type of x: " << typeid(x).name() << std::endl;
+int i = 0;
+while(i < 5) {
+ std::cout << "Iteration " << i << std::endl;
+ i++;
+}
+
+template<class T>
+concept dereferenceable =
+ requires { typename iter_value_t<I>; } and
+ requires(I i) {
+ *i;
+ };
+
+
+template<class T>
+struct S {
+ explicit(weakly_incrementable<T>) S();
+};
+
+
+int main()
+{
+ auto x = auto(0);
+ x bitand_eq x;
+ x bitand_eq (x);
+ x bitor_eq x;
+ x bitor_eq (x);
+ x xor x;
+ x xor (x);
+ x and x;
+ x and (x);
+ x or x;
+ x or (x);
+ x bitand x;
+ x bitand (x);
+ x bitor x;
+ x bitor (x);
+ x not_eq x;
+ x not_eq (x);
+ not x;
+ not (x);
+ compl x;
+ compl (x);
+ co_await x;
+ co_await (x);
+ co_return x;
+ co_return (x);
+ co_yield x;
+ co_yield (x);
+}
diff --git a/test/markup/cpp/keywords-with-parentheses.txt b/test/markup/cpp/keywords-with-parentheses.txt
new file mode 100644
index 0000000000..f2b30e6715
--- /dev/null
+++ b/test/markup/cpp/keywords-with-parentheses.txt
@@ -0,0 +1,93 @@
+alignas(16) char aligned_buffer[1024];
+alignof(decltype(aligned_buffer))
+asm("movl $1, %eax");
+try {
+ throw std::runtime_error("An exception occurred");
+} catch (const std::exception& e) {
+ std::cout << "Caught exception: " << e.what() << std::endl;
+}
+const int* p = nullptr;
+int* mutable_p = const_cast(p);
+int x = 5;
+decltype(x) y = 10;
+Animal* animal = new Dog();
+if (Dog* dog = dynamic_cast(animal)) {
+ std::cout << "Dynamic cast successful" << std::endl;
+} else {
+ std::cout << "Dynamic cast failed" << std::endl;
+}
+int add(int a, int b) noexcept {
+ return a + b;
+}
+if (noexcept(add(1, 2))) {
+ // The add function will not throw an exception.
+} else {
+ // The add function may throw an exception.
+}
+int value = 10;
+double* ptr = reinterpret_cast(&value);
+std::cout << "Size of int: " << sizeof(int) << " bytes" << std::endl;
+static_assert(sizeof(int) == 4, "int must be 4 bytes");
+float z = static_cast(10);
+int choice = 2;
+switch(choice) {
+ case 1:
+ std::cout << "Choice is 1" << std::endl;
+ break;
+ case 2:
+ std::cout << "Choice is 2" << std::endl;
+ break;
+ default:
+ std::cout << "Choice is not 1 or 2" << std::endl;
+}
+std::cout << "Type of x: " << typeid(x).name() << std::endl;
+int i = 0;
+while(i < 5) {
+ std::cout << "Iteration " << i << std::endl;
+ i++;
+}
+// requires
+template
+concept dereferenceable =
+ requires { typename iter_value_t; } and
+ requires(I i) {
+ *i;
+ };
+
+// explicit
+template
+struct S {
+ explicit(weakly_incrementable) S();
+};
+
+// auto, operators
+int main()
+{
+ auto x = auto(0);
+ x bitand_eq x; // needs to be added too
+ x bitand_eq (x);
+ x bitor_eq x; // needs to be added too
+ x bitor_eq (x);
+ x xor x;
+ x xor (x);
+ x and x;
+ x and (x);
+ x or x;
+ x or (x);
+ x bitand x;
+ x bitand (x);
+ x bitor x;
+ x bitor (x);
+ x not_eq x;
+ x not_eq (x);
+ not x;
+ not (x);
+ compl x;
+ compl (x);
+ co_await x;
+ co_await (x);
+ co_return x;
+ co_return (x);
+ co_yield x;
+ co_yield (x);
+}
\ No newline at end of file
diff --git a/test/markup/cpp/template-complexity.expect.txt b/test/markup/cpp/template-complexity.expect.txt
index 92b83178b6..3f2a72f10d 100644
--- a/test/markup/cpp/template-complexity.expect.txt
+++ b/test/markup/cpp/template-complexity.expect.txt
@@ -10,7 +10,7 @@
template<class T, class = std::enable_if_t<!impl::is_streamable_v<const T &> && std::is_convertible_v<const T &, std::wstring_view>>>
std::wostream &operator <<(std::wostream &stream, const T &thing)
{
- return stream << static_cast<std::wstring_view>(thing);
+ return stream << static_cast<std::wstring_view>(thing);
}
enum struct DataHolder { };
From 2b3fa7b60af7de03398db779158f708165b06168 Mon Sep 17 00:00:00 2001
From: Md Saad Akhtar
Date: Tue, 9 Jul 2024 11:38:56 +0530
Subject: [PATCH 3/4] keywords update dynamically
---
src/languages/cpp.js | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/src/languages/cpp.js b/src/languages/cpp.js
index 64aee1d44e..6b1bb4e093 100644
--- a/src/languages/cpp.js
+++ b/src/languages/cpp.js
@@ -415,6 +415,14 @@ export default function(hljs) {
_type_hints: TYPE_HINTS
};
+ const allKeywords = [
+ ...CPP_KEYWORDS.type,
+ ...CPP_KEYWORDS.keyword,
+ ...CPP_KEYWORDS.literal,
+ ...CPP_KEYWORDS.built_in,
+ ...CPP_KEYWORDS._type_hints
+ ];
+
const FUNCTION_DISPATCH = {
className: 'function.dispatch',
relevance: 0,
@@ -423,11 +431,8 @@ export default function(hljs) {
_hint: FUNCTION_HINTS },
begin: regex.concat(
/\b/,
- /(?!decltype)/,
- /(?!if)/,
- /(?!for)/,
- /(?!switch)/,
- /(?!while)/,
+ // adds all keywords dynamically
+ ...allKeywords.map(keyword => new RegExp(`(?!${keyword})`)),
hljs.IDENT_RE,
regex.lookahead(/(<[^<>]+>|)\s*\(/))
};
From 0b50faf4e0de30c261651f924161ce9e483197b6 Mon Sep 17 00:00:00 2001
From: Md Saad Akhtar
Date: Tue, 9 Jul 2024 11:49:28 +0530
Subject: [PATCH 4/4] updated changes.md
---
CHANGES.md | 1 +
1 file changed, 1 insertion(+)
diff --git a/CHANGES.md b/CHANGES.md
index a74cd3d0fa..46f109ebe6 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -6,6 +6,7 @@ CAVEATS / POTENTIALLY BREAKING CHANGES
Core Grammars:
+- fix(cpp) Keywords followed by parens are treated as built_in [Md Saad Akhtar][]
- enh(json) added jsonc as an alias [BackupMiles][]
- enh(gml) updated to latest language version (GML v2024.2) [gnysek][]
- enh(c) added more C23 keywords and preprcoessor directives [Eisenwave][]