unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Naofumi Yasufuku <naofumi@yasufuku.dev>
To: 57318@debbugs.gnu.org
Subject: bug#57318: 29.0.50; c++-mode: Functions are shown as variables: partial template specialization
Date: Sun, 28 Aug 2022 14:33:08 +0900	[thread overview]
Message-ID: <m1v8qdx78r.fsf@yasufuku.dev> (raw)
In-Reply-To: <m1o7wd4th8.fsf@yasufuku.dev>

[-- Attachment #1: Type: text/plain, Size: 1682 bytes --]

Naofumi Yasufuku <naofumi@yasufuku.dev> writes:

> Hello,
>
> In the case of C++ partial template specialization [1], functions are
> shown as variables.  Please look at the attached screenshots.
>
>   1. emacs -Q
>   2. open attached C++ code "specialization.cc"
>
>   attachments:
>   - NG_cc-partial-specialization.png
>   - OK_cc-partial-specialization.png
>   - specialization.cc
>
[..snip..]
>
> After a brief investigation, the attached cc-engine.el patch seems to
> solve this issue at least, but it is not tested enough.
>
>   attachments:
>   - 0001-cc-mode-Fix-function-fontification-in-the-case-of-C-.patch
>

Previous patch still has a problem with sizeof() specializations.
Updated patch and screenshots are attached.

  attachments:
  - 0001-cc-mode-Fix-function-fontification-in-the-case-of-C-v2.patch
  - NG_cc-partial-specialization-v1.png
  - OK_cc-partial-specialization-v2.png
  - specialization.cc

-------------------------------------------------------------
template<typename T1, size_t>
class TwoSz {
public:
    TwoSz() {}
private:
    T1 _t1;
};

template<typename T>
class TwoSz<T, sizeof(int)> {
public:
    TwoSz() {}
private:
    T _t1;
};
-------------------------------------------------------------
$ ./specialization
One<T>: primary
One<char>: specialization
Two<T1, T2>: primary
Two<T, char>: specialization
Two<T, T*>: specialization
Two<T, T&>: specialization
Two<T, T[]>: specialization
Two<char, char*>: specialization
Two<char, char&>: specialization
Two<char, char[]>: specialization
TwoSz<T1, size_t>: primary
TwoSz<T, sizeof(int)>: specialization
$
-------------------------------------------------------------

Best regards,
  Naofumi


[-- Attachment #2: sizeof problem --]
[-- Type: image/png, Size: 638081 bytes --]

[-- Attachment #3: ok with patch v2 --]
[-- Type: image/png, Size: 637252 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: patch v2 --]
[-- Type: text/x-patch, Size: 1534 bytes --]

From fc88895a0daa9f43c789650ac7f0438cd2466811 Mon Sep 17 00:00:00 2001
From: Naofumi Yasufuku <naofumi@yasufuku.dev>
Date: Sun, 28 Aug 2022 13:33:01 +0900
Subject: [PATCH] cc-mode: Fix function fontification in the case of C++
 partial specialization

* lisp/progmodes/cc-engine.el (c-update-brace-stack): Skip "*", ",",
and ")" found in "class foo : bar, .." (base-clause of a class) or
"class foo<T, T*, T&, T[], sizeof(..), ..>" (partial template specialization)
(bug#57318).
---
 lisp/progmodes/cc-engine.el | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
index b2d1f15d39..5d1a11e9e8 100644
--- a/lisp/progmodes/cc-engine.el
+++ b/lisp/progmodes/cc-engine.el
@@ -6141,13 +6141,18 @@ c-update-brace-stack
 		(c-forward-<>-arglist nil)) ; Should always work.
 	      (when (> (point) to)
 		(setq bound-<> (point)))
-	    (forward-char)))
+	    (forward-char))
+	  (if (and s
+		   (eq (car s) 0))
+	      ;; for "class foo<T, T*, T&, T[], sizeof(..), ..>"
+	      (setq s (cons -1 (cdr s)))))
 	 ((and (equal match ":")
 	       s
 	       (eq (car s) 0))
+	  ;; for "," in "class foo : bar, .."
 	  (setq s (cons -1 (cdr s))))
-	 ((and (equal match ",")
-	       (eq (car s) -1)))	; at "," in "class foo : bar, ..."
+	 ((and (eq (car s) -1) ; in "class foo : .." or "class foo<..>"
+	       (member match '("*" "," ")"))))
 	 ((member match '(";" "*" "," ")"))
 	  (when (and s (cdr s) (<= (car s) 0))
 	    (setq s (cdr s))))
-- 
2.37.2


[-- Attachment #5: test c++ source --]
[-- Type: text/plain, Size: 2970 bytes --]

#include <iostream>

template<typename T>
class One {
public:
    One() {}
    ~One() {}
    void fn() { std::cout << "One<T>: primary" << std::endl; }
private:
    T _t;
};

template<>
class One<char> {
public:
    One() {}
    ~One() {}
    void fn() { std::cout << "One<char>: specialization" << std::endl; }
private:
    char _c;
};

template<typename T1, typename T2>
class Two {
public:
    Two() {}
    ~Two() {}
    void fn() { std::cout << "Two<T1, T2>: primary" << std::endl; }
private:
    T1 _t1;
    T2 _t2;
};

template<typename T>
class Two<T, char> {
public:
    Two() {}
    ~Two() {}
    void fn() { std::cout << "Two<T, char>: specialization" << std::endl; }
private:
    T _t;
    char _c;
};

template<typename T>
class Two<T, T*> {
public:
    Two() {}
    ~Two() {}
    void fn() { std::cout << "Two<T, T*>: specialization" << std::endl; }
private:
    T _t;
    T* _pt;
};

template<typename T>
class Two<T, T&> {
public:
    Two() {}
    ~Two() {}
    void fn() { std::cout << "Two<T, T&>: specialization" << std::endl; }
private:
    T _t;
};

template<typename T>
class Two<T, T[]> {
public:
    Two() {}
    ~Two() {}
    void fn() { std::cout << "Two<T, T[]>: specialization" << std::endl; }
private:
    T _t;
};

template<>
class Two<char, char*> {
public:
    Two() {}
    ~Two() {}
    void fn() { std::cout << "Two<char, char*>: specialization" << std::endl; }
private:
    char _c;
};

template<>
class Two<char, char&> {
public:
    Two() {}
    ~Two() {}
    void fn() { std::cout << "Two<char, char&>: specialization" << std::endl; }
private:
    char _c;
};

template<>
class Two<char, char[]> {
public:
    Two() {}
    ~Two() {}
    void fn() { std::cout << "Two<char, char[]>: specialization" << std::endl; }
private:
    char _c;
};

template<typename T1, size_t>
class TwoSz {
public:
    TwoSz() {}
    ~TwoSz() {}
    void fn() { std::cout << "TwoSz<T1, size_t>: primary" << std::endl; }
private:
    T1 _t1;
};

template<typename T>
class TwoSz<T, sizeof(int)> {
public:
    TwoSz() {}
    ~TwoSz() {}
    void fn() { std::cout << "TwoSz<T, sizeof(int)>: specialization" << std::endl; }
private:
    T _t1;
};

class Base1 {};
template<typename T1, size_t> class Base2 {};
template<typename T> class Derived : public Base1, Base2<T, sizeof(int)> {
public:
  Derived() {}
};

int main() {
    One<int> oneInt; oneInt.fn();
    One<char> oneChar; oneChar.fn();

    Two<int, int> twoIntInt;  twoIntInt.fn();
    Two<int, char> twoIntChar; twoIntChar.fn();

    Two<int, int*> twoIntIntP; twoIntIntP.fn();
    Two<int, int&> twoIntIntR; twoIntIntR.fn();
    Two<int, int[]> twoIntIntA; twoIntIntA.fn();

    Two<char, char*> twoCharCharP; twoCharCharP.fn();
    Two<char, char&> twoCharCharR; twoCharCharR.fn();
    Two<char, char[]> twoCharCharA; twoCharCharA.fn();

    TwoSz<int, 8> twoSzInt8; twoSzInt8.fn();
    TwoSz<int, sizeof(int)> twoSzIntSizeofI; twoSzIntSizeofI.fn();

    Derived<int> derived{};

    return 0;
}

  reply	other threads:[~2022-08-28  5:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-21 13:27 bug#57318: 29.0.50; c++-mode: Functions are shown as variables: partial template specialization Naofumi Yasufuku
2022-08-28  5:33 ` Naofumi Yasufuku [this message]
2022-08-30 20:00   ` Alan Mackenzie
2022-08-31 16:01     ` Naofumi Yasufuku
2022-08-31 19:26       ` Alan Mackenzie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m1v8qdx78r.fsf@yasufuku.dev \
    --to=naofumi@yasufuku.dev \
    --cc=57318@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).