diff options
| author | Michal Nazarewicz | 2020-05-04 19:08:10 +0100 |
|---|---|---|
| committer | Michal Nazarewicz | 2020-05-09 11:30:32 +0100 |
| commit | ae3c510696f02f01d03052f070e5ce65b4018a45 (patch) | |
| tree | 7ffda57aed8788d8b0cbfbb16d486c0d0db15b97 | |
| parent | fab23328512e47a50caced8d074e86e583cc8a9f (diff) | |
| download | emacs-ae3c510696f02f01d03052f070e5ce65b4018a45.tar.gz emacs-ae3c510696f02f01d03052f070e5ce65b4018a45.zip | |
cc-mode: extend regexp used by ‘c-or-c++-mode’
* lisp/progmodes/cc-mode (c-or-c++-mode--regexp): Expand the regexp to
match some more C++-only constructs and recognise a few more standard
C++ header files. Also make sure identifiers start with non-digit.
(c-or-c++-mode): Add ‘(interactive)’ declaration.
* test/lisp/progmodes/cc-mode-tests.el (c-or-c++-mode): Add test case
for the newly recognised constructs.
| -rw-r--r-- | lisp/progmodes/cc-mode.el | 21 | ||||
| -rw-r--r-- | test/lisp/progmodes/cc-mode-tests.el | 12 |
2 files changed, 25 insertions, 8 deletions
diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el index f92d3efdeb7..e3a924efb06 100644 --- a/lisp/progmodes/cc-mode.el +++ b/lisp/progmodes/cc-mode.el | |||
| @@ -2541,13 +2541,21 @@ Key bindings: | |||
| 2541 | 2541 | ||
| 2542 | (defconst c-or-c++-mode--regexp | 2542 | (defconst c-or-c++-mode--regexp |
| 2543 | (eval-when-compile | 2543 | (eval-when-compile |
| 2544 | (let ((id "[a-zA-Z0-9_]+") (ws "[ \t\r]+") (ws-maybe "[ \t\r]*")) | 2544 | (let ((id "[a-zA-Z_][a-zA-Z0-9_]*") (ws "[ \t\r]+") (ws-maybe "[ \t\r]*") |
| 2545 | (headers '("string" "string_view" "iostream" "map" "unordered_map" | ||
| 2546 | "set" "unordered_set" "vector" "tuple"))) | ||
| 2545 | (concat "^" ws-maybe "\\(?:" | 2547 | (concat "^" ws-maybe "\\(?:" |
| 2546 | "using" ws "\\(?:namespace" ws "std;\\|std::\\)" | 2548 | "using" ws "\\(?:namespace" ws |
| 2547 | "\\|" "namespace" "\\(:?" ws id "\\)?" ws-maybe "{" | 2549 | "\\|" id "::" |
| 2548 | "\\|" "class" ws id ws-maybe "[:{\n]" | 2550 | "\\|" id ws-maybe "=\\)" |
| 2549 | "\\|" "template" ws-maybe "<.*>" | 2551 | "\\|" "\\(?:inline" ws "\\)?namespace" |
| 2550 | "\\|" "#include" ws-maybe "<\\(?:string\\|iostream\\|map\\)>" | 2552 | "\\(:?" ws "\\(?:" id "::\\)*" id "\\)?" ws-maybe "{" |
| 2553 | "\\|" "class" ws id | ||
| 2554 | "\\(?:" ws "final" "\\)?" ws-maybe "[:{;\n]" | ||
| 2555 | "\\|" "struct" ws id "\\(?:" ws "final" ws-maybe "[:{\n]" | ||
| 2556 | "\\|" ws-maybe ":\\)" | ||
| 2557 | "\\|" "template" ws-maybe "<.*?>" | ||
| 2558 | "\\|" "#include" ws-maybe "<" (regexp-opt headers) ">" | ||
| 2551 | "\\)"))) | 2559 | "\\)"))) |
| 2552 | "A regexp applied to C header files to check if they are really C++.") | 2560 | "A regexp applied to C header files to check if they are really C++.") |
| 2553 | 2561 | ||
| @@ -2563,6 +2571,7 @@ should be used. | |||
| 2563 | This function attempts to use file contents to determine whether | 2571 | This function attempts to use file contents to determine whether |
| 2564 | the code is C or C++ and based on that chooses whether to enable | 2572 | the code is C or C++ and based on that chooses whether to enable |
| 2565 | `c-mode' or `c++-mode'." | 2573 | `c-mode' or `c++-mode'." |
| 2574 | (interactive) | ||
| 2566 | (if (save-excursion | 2575 | (if (save-excursion |
| 2567 | (save-restriction | 2576 | (save-restriction |
| 2568 | (save-match-data | 2577 | (save-match-data |
diff --git a/test/lisp/progmodes/cc-mode-tests.el b/test/lisp/progmodes/cc-mode-tests.el index ad7a52b40d9..64d52a952b6 100644 --- a/test/lisp/progmodes/cc-mode-tests.el +++ b/test/lisp/progmodes/cc-mode-tests.el | |||
| @@ -40,7 +40,7 @@ | |||
| 40 | (insert content) | 40 | (insert content) |
| 41 | (setq mode nil) | 41 | (setq mode nil) |
| 42 | (c-or-c++-mode) | 42 | (c-or-c++-mode) |
| 43 | (unless(eq expected mode) | 43 | (unless (eq expected mode) |
| 44 | (ert-fail | 44 | (ert-fail |
| 45 | (format "expected %s but got %s when testing '%s'" | 45 | (format "expected %s but got %s when testing '%s'" |
| 46 | expected mode content))))) | 46 | expected mode content))))) |
| @@ -53,11 +53,18 @@ | |||
| 53 | (funcall do-test (concat " * " content) 'c-mode)) | 53 | (funcall do-test (concat " * " content) 'c-mode)) |
| 54 | '("using \t namespace \t std;" | 54 | '("using \t namespace \t std;" |
| 55 | "using \t std::string;" | 55 | "using \t std::string;" |
| 56 | "using Foo = Bar;" | ||
| 56 | "namespace \t {" | 57 | "namespace \t {" |
| 57 | "namespace \t foo \t {" | 58 | "namespace \t foo \t {" |
| 58 | "class \t Blah_42 \t {" | 59 | "namespace \t foo::bar \t {" |
| 60 | "inline namespace \t foo \t {" | ||
| 61 | "inline namespace \t foo::bar \t {" | ||
| 59 | "class \t Blah_42 \t \n" | 62 | "class \t Blah_42 \t \n" |
| 63 | "class \t Blah_42;" | ||
| 64 | "class \t Blah_42 \t final {" | ||
| 65 | "struct \t Blah_42 \t final {" | ||
| 60 | "class \t _42_Blah:public Foo {" | 66 | "class \t _42_Blah:public Foo {" |
| 67 | "struct \t _42_Blah:public Foo {" | ||
| 61 | "template \t < class T >" | 68 | "template \t < class T >" |
| 62 | "template< class T >" | 69 | "template< class T >" |
| 63 | "#include <string>" | 70 | "#include <string>" |
| @@ -67,6 +74,7 @@ | |||
| 67 | (mapc (lambda (content) (funcall do-test content 'c-mode)) | 74 | (mapc (lambda (content) (funcall do-test content 'c-mode)) |
| 68 | '("struct \t Blah_42 \t {" | 75 | '("struct \t Blah_42 \t {" |
| 69 | "struct template {" | 76 | "struct template {" |
| 77 | "struct Blah;" | ||
| 70 | "#include <string.h>"))))) | 78 | "#include <string.h>"))))) |
| 71 | 79 | ||
| 72 | (ert-deftest c-mode-macro-comment () | 80 | (ert-deftest c-mode-macro-comment () |