Subject: bug#56362: closed (Re: bug#56362: CC Mode 5.35.1 (C++//l); Highlight for C++20 keywords)

From: [email-masked] (GNU bug Tracking System)
Date: Tue, 04 Oct 2022 17:33:02 +0000

Your bug report

#56362: CC Mode 5.35.1 (C++//l); Highlight for C++20 keywords

which was filed against the cc-mode package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
56362: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=56362
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#56362: CC Mode 5.35.1 (C++//l); Highlight for C++20 keywords
From: Alan Mackenzie <[email-masked]>
To: Zhiwei Chen <[email-masked]>
Cc: [email-masked], [email-masked]
Date: Tue, 4 Oct 2022 17:32:40 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello again, Zhiwei.

I've now committed the changes to standalone CC Mode and the Emacs
master branch.

As a matter of interest, I've also committed support for C++20 concepts
and modules.

I'm closing the bug with this post.

-- 
Alan Mackenzie (Nuremberg, Germany).

On Wed, Sep 07, 2022 at 13:01:24 +0000, Alan Mackenzie wrote:
> Hello, Zhiwei.

> First of all, sorry that it's taken me two months to reply.

> Secondly, thanks for taking the trouble to submit this bug.  I have fixed
> the problems, partly, but I still have a lot of work to do to bring CC
> Mode up to the C++20 standard.  In particular, "concepts" and "requires"
> will need a lot doing.  I hope to do this work in the coming weeks.

> I have included a patch for these problems below; could you please test
> this patch on your CC Mode and your real C++ source files, and let me
> know how it goes.  As I said, it is not perfect, but it should be better
> than it was.  If you want any help with the patching or byte compiling of
> CC Mode, feel free to send me private email.

> For the problems you raised:

> On Sun, Jul 03, 2022 at 03:12:28 +0800, Zhiwei Chen wrote:
> > Package: cc-mode

> > See the comments.

> > #+begin_src C++
> > #if __has_include(<bits/stdc++.h>)
> > #include <bits/stdc++.h>
> > #endif

> > #include <cstdint>
> > #include <concepts>

> > // no highlight for consteval keyword
> > consteval int sqr(int x) {
> >     return x * x;
> > }

> Now fixed.

> > // no highlight for constinit keyword
> > constinit int four = sqr(16);

> Now fixed.

> > class Foo {
> > public:
> >     // <=> highlights wrong
> >     auto operator<=>(const Foo&) const = default;

> Now fixed.

> > private:
> >     // no highlight for alignas keyword
> >     alignas(alignof(std::uint64_t)) int val_;
> > };

> Now fixed.  It's also worth noting that "alignas" in a struct/class:

>     class alignas(64) Foo { ..... }

> should now fontify correctly.

> > template <typename T>
> > requires std::integral<T> // 'requires' occasionally has a font-lock-type-face
> > T add(T a, T b) {
> >     return a + b;
> > }

> Here's where I still need to make "requires" work properly.  I've
> temporarily make "requires" and "concept" C++ keywords, but in the above
> piece of code, "T add(T a, T b) {" is not correctly fontified.  But
> "requires" should now always be fontified as a keyword.

> > int main() {
> >     const char8_t c1 = 0; // char8_t is missing in `c-primitive-type-kwds'. cc-langs.el L2205

> >     return 0;
> > }
> > #+end_src

> char8_t is now in c-primitive-type-kwds.

> > Emacs  : GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.34, cairo version 1.17.6)
> >  of 2022-07-02
> > Package: CC Mode 5.35.1 (C++//l)
> > Buffer Style: gnu
> > c-emacs-features: (pps-extended-state col-0-paren posix-char-classes gen-string-delim gen-comment-delim syntax-properties 1-bit)

> [ CC Mode configuration appreciated, but snipped ]

> Here's the patch:

> diff -r 87af98f62eec cc-langs.el
> --- a/cc-langs.el	Wed Aug 31 18:28:12 2022 +0000
> +++ b/cc-langs.el	Wed Sep 07 12:38:20 2022 +0000
> @@ -1301,6 +1301,10 @@
>  		  ,@(when (c-major-mode-is 'java-mode)
>  		      '(">>>")))
 
> +      ;; The C++ "spaceship" operator.
> +      ,@(when (c-major-mode-is 'c++-mode)
> +	  `((left-assoc "<=>")))
> +
>        ;; Relational.
>        (left-assoc "<" ">" "<=" ">="
>  		  ,@(when (c-major-mode-is 'java-mode)
> @@ -1414,7 +1418,7 @@
>  	 "^" "??'" "xor" "&" "bitand" "|" "??!" "bitor" "~" "??-" "compl"
>  	 "!" "=" "<" ">" "+=" "-=" "*=" "/=" "%=" "^="
>  	 "??'=" "xor_eq" "&=" "and_eq" "|=" "??!=" "or_eq"
> -	 "<<" ">>" ">>=" "<<=" "==" "!=" "not_eq" "<=" ">="
> +	 "<<" ">>" ">>=" "<<=" "==" "!=" "not_eq" "<=>" "<=" ">="
>  	 "&&" "and" "||" "??!??!" "or" "++" "--" "," "->*" "->"
>  	 "()" "[]" "<::>" "??(??)")
>    ;; These work like identifiers in Pike.
> @@ -1535,8 +1539,10 @@
>    "List of all arithmetic operators, including \"+=\", etc."
>    ;; Note: in the following, there are too many operators for AWK and IDL.
>    t (append (c-lang-const c-assignment-operators)
> -	    '("+" "-" "*" "/" "%"
> +	    `("+" "-" "*" "/" "%"
>  	      "<<" ">>"
> +	      ,@(if (c-major-mode-is 'c++-mode)
> +		    '("<=>"))
>  	      "<" ">" "<=" ">="
>  	      "==" "!="
>  	      "&" "^" "|"
> @@ -2187,7 +2193,7 @@
>  	'("_Bool" "_Complex" "_Imaginary") ; Conditionally defined in C99.
>  	(c-lang-const c-primitive-type-kwds))
>    c++  (append
> -	'("bool" "wchar_t" "char16_t" "char32_t")
> +	'("bool" "wchar_t" "char8_t" "char16_t" "char32_t")
>  	(c-lang-const c-primitive-type-kwds))
>    ;; Objective-C extends C, but probably not the new stuff in C99.
>    objc (append
> @@ -2580,8 +2586,8 @@
>    t    nil
>    (c c++) '("extern" "inline" "register" "static")
>    c    (append '("auto") (c-lang-const c-modifier-kwds))
> -  c++  (append '("constexpr" "explicit" "friend" "mutable" "template"
> -		 "thread_local" "virtual")
> +  c++  (append '("consteval" "constexpr" "constinit" "explicit" "friend"
> +		 "mutable" "template" "thread_local" "virtual")
>  	       ;; "using" is now handled specially (2020-09-14).
>  	       (c-lang-const c-modifier-kwds))
>    objc '("auto" "bycopy" "byref" "extern" "in" "inout" "oneway" "out" "static")
> @@ -2655,7 +2661,8 @@
>    (c c++) '(;; GCC extension.
>  	    "__attribute__"
>  	    ;; MSVC extension.
> -	    "__declspec"))
> +	    "__declspec")
> +  c++ (append (c-lang-const c-decl-hangon-kwds) '("alignas")))
 
>  (c-lang-defconst c-decl-hangon-key
>    ;; Adorned regexp matching `c-decl-hangon-kwds'.
> @@ -2880,7 +2887,7 @@
>  	    "__attribute__"
>  	    ;; MSVC extension.
>  	    "__declspec")
> -  c++ (append (c-lang-const c-paren-nontype-kwds) '("noexcept")))
> +  c++ (append (c-lang-const c-paren-nontype-kwds) '("noexcept" "alignas")))
 
>  (c-lang-defconst c-paren-nontype-key
>    t (c-make-keywords-re t (c-lang-const c-paren-nontype-kwds)))
> @@ -3256,11 +3263,17 @@
>  (c-lang-defconst c-keywords
>    ;; All keywords as a list.
>    t (c--delete-duplicates
> -     (c-lang-defconst-eval-immediately
> -      `(append ,@(mapcar (lambda (kwds-lang-const)
> -			   `(c-lang-const ,kwds-lang-const))
> -			 c-kwds-lang-consts)
> -	       nil))
> +     (append				; Temporary, 2022-09-07
> +      (c-lang-defconst-eval-immediately
> +       `(append ,@(mapcar (lambda (kwds-lang-const)
> +			    `(c-lang-const ,kwds-lang-const))
> +			  c-kwds-lang-consts)
> +		nil))
> +      ;; Temporary addition for C++ until concepts have been worked out properly.
> +      ;; 2022-09-07.
> +      `(,@(if (c-major-mode-is 'c++-mode)
> +	      '("concept" "requires")))
> +      nil)
>       :test 'string-equal))
 
>  (c-lang-defconst c-keywords-regexp

> -- 
> Alan Mackenzie (Nuremberg, Germany).

Subject: CC Mode 5.35.1 (C++//l); Highlight for C++20 keywords
From: Zhiwei Chen <[email-masked]>
To: [email-masked]
Date: Sun, 03 Jul 2022 03:12:28 +0800
List-Id: <debbugs-submit.debbugs.gnu.org>

Package: cc-mode

See the comments.

#+begin_src C++
#if __has_include(<bits/stdc++.h>)
#include <bits/stdc++.h>
#endif

#include <cstdint>
#include <concepts>

// no highlight for consteval keyword
consteval int sqr(int x) {
    return x * x;
}

// no highlight for constinit keyword
constinit int four = sqr(16);

class Foo {
public:
    // <=> highlights wrong
    auto operator<=>(const Foo&) const = default;

private:
    // no highlight for alignas keyword
    alignas(alignof(std::uint64_t)) int val_;
};

template <typename T>
requires std::integral<T> // 'requires' occasionally has a font-lock-type-face
T add(T a, T b) {
    return a + b;
}

int main() {
    const char8_t c1 = 0; // char8_t is missing in `c-primitive-type-kwds'. cc-langs.el L2205

    return 0;
}
#+end_src

Emacs  : GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.34, cairo version 1.17.6)
 of 2022-07-02
Package: CC Mode 5.35.1 (C++//l)
Buffer Style: gnu
c-emacs-features: (pps-extended-state col-0-paren posix-char-classes gen-string-delim gen-comment-delim syntax-properties 1-bit)

current state:
==============
(setq
 c-basic-offset 4
 c-comment-only-line-offset '(0 . 0)
 c-indent-comment-alist '((anchored-comment column . 0) (end-block space . 1)
                          (cpp-end-block space . 2))
 c-indent-comments-syntactically-p nil
 c-block-comment-prefix ""
 c-comment-prefix-regexp '((c-mode . "//+!?\\|\\**") (c++-mode . "//+!?\\|\\**")
                           (awk-mode . "#+") (other . "//+\\|\\**"))
 c-doc-comment-style '((c-mode . gtkdoc) (c++-mode . doxygen))
 c-cleanup-list '(scope-operator)
 c-hanging-braces-alist '((substatement-open before after) (arglist-cont-nonempty))
 c-hanging-colons-alist nil
 c-hanging-semi&comma-criteria '(c-semi&comma-inside-parenlist)
 c-backslash-column 48
 c-backslash-max-column 72
 c-special-indent-hook '(t c-gnu-impose-minimum)
 c-label-minimum-indentation 0
 c-offsets-alist '((inexpr-class . +)
                   (incomposition . +)
                   (inmodule . +)
                   (composition-close . 0)
                   (module-close . 0)
                   (composition-open . 0)
                   (module-open . 0)
                   (objc-method-call-cont
                    c-lineup-ObjC-method-call-colons
                    c-lineup-ObjC-method-call
                    +
                    )
                   (objc-method-args-cont . c-lineup-ObjC-method-args)
                   (objc-method-intro . [0])
                   (cpp-define-intro c-lineup-cpp-define +)
                   (annotation-var-cont . +)
                   (annotation-top-cont . 0)
                   (knr-argdecl . 0)
                   (knr-argdecl-intro . 5)
                   (c . c-lineup-C-comments)
                   (string . c-lineup-dont-change)
                   (defun-open . 0)
                   (defun-close . 0)
                   (defun-block-intro . +)
                   (class-open . 0)
                   (class-close . 0)
                   (access-label . -)
                   (inline-open . 0)
                   (inline-close . 0)
                   (inclass . +)
                   (friend . 0)
                   (func-decl-cont . +)
                   (brace-list-open . 0)
                   (brace-list-close . 0)
                   (brace-list-intro . +)
                   (brace-list-entry . 0)
                   (brace-entry-open . 0)
                   (namespace-open . 0)
                   (namespace-close . 0)
                   (innamespace . 0)
                   (block-open . 0)
                   (block-close . 0)
                   (topmost-intro . 0)
                   (topmost-intro-cont . c-lineup-topmost-intro-cont)
                   (member-init-intro . +)
                   (member-init-cont . c-lineup-multi-inher)
                   (inher-intro . +)
                   (inher-cont . c-lineup-multi-inher)
                   (statement . 0)
                   (statement-cont c-lineup-ternary-bodies +)
                   (statement-block-intro . +)
                   (statement-case-intro . +)
                   (statement-case-open . +)
                   (substatement . +)
                   (substatement-open . 0)
                   (substatement-label . 0)
                   (case-label . 0)
                   (label . 0)
                   (do-while-closure . 0)
                   (else-clause . 0)
                   (catch-clause . 0)
                   (comment-intro . c-lineup-comment)
                   (arglist-intro . +)
                   (arglist-cont . 0)
                   (arglist-cont-nonempty . c-lineup-arglist)
                   (arglist-close . c-lineup-close-paren)
                   (stream-op . c-lineup-streamop)
                   (cpp-macro . -1000)
                   (cpp-macro-cont . +)
                   (extern-lang-open . 0)
                   (extern-lang-close . 0)
                   (inextern-lang . 0)
                   (inlambda . 0)
                   (lambda-intro-cont . +)
                   (inexpr-statement . 0)
                   (template-args-cont c-lineup-template-args +)
                   )
 c-buffer-is-cc-mode 'c++-mode
 c-tab-always-indent t
 c-syntactic-indentation t
 c-syntactic-indentation-in-macros t
 c-ignore-auto-fill '(string cpp code)
 c-auto-align-backslashes t
 c-backspace-function 'backward-delete-char-untabify
 c-delete-function 'delete-char
 c-electric-pound-behavior nil
 c-default-style '((java-mode . "java") (awk-mode . "awk") (other . "gnu"))
 c-enable-xemacs-performance-kludge-p nil
 c-old-style-variable-behavior nil
 defun-prompt-regexp nil
 tab-width 4
 comment-column 32
 parse-sexp-ignore-comments t
 parse-sexp-lookup-properties t
 auto-fill-function nil
 comment-multi-line t
 comment-start-skip "\\(?://+\\|/\\*+\\)\\s *"
 fill-prefix nil
 fill-column 80
 paragraph-start "[ 	]*\\(//+!?\\|\\**\\)[ 	]*$\\|^\f"
 adaptive-fill-mode t
 adaptive-fill-regexp "[ 	]*\\(//+!?\\|\\**\\)[ 	]*\\([ 	]*\\([-–!|#%;>*·•‣⁃◦]+[ 	]*\\)*\\)"
 )
-- 
Zhiwei Chen