From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs Subject: bug#23313: 25.0.92; CC Mode not recognizing function declarations that returns pointer to custom type Date: Sat, 23 Apr 2016 14:58:32 +0000 Message-ID: <20160423145831.GA4624@acm.fritz.box> References: <20160423114830.99485.qmail@mail.muc.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1461423564 6431 80.91.229.3 (23 Apr 2016 14:59:24 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 23 Apr 2016 14:59:24 +0000 (UTC) Cc: 23313@debbugs.gnu.org To: Mohammed Sadik Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat Apr 23 16:59:13 2016 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1atz1c-0002QM-AF for geb-bug-gnu-emacs@m.gmane.org; Sat, 23 Apr 2016 16:59:12 +0200 Original-Received: from localhost ([::1]:52715 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1atz1b-0007AQ-NZ for geb-bug-gnu-emacs@m.gmane.org; Sat, 23 Apr 2016 10:59:11 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:40443) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1atz1X-00077j-SX for bug-gnu-emacs@gnu.org; Sat, 23 Apr 2016 10:59:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1atz1W-0008Dm-KK for bug-gnu-emacs@gnu.org; Sat, 23 Apr 2016 10:59:07 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:60070) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1atz1S-0008DT-Kz; Sat, 23 Apr 2016 10:59:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1atz1S-0005du-A3; Sat, 23 Apr 2016 10:59:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Alan Mackenzie Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org, bug-cc-mode@gnu.org Resent-Date: Sat, 23 Apr 2016 14:59:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 23313 X-GNU-PR-Package: emacs,cc-mode X-GNU-PR-Keywords: Original-Received: via spool by 23313-submit@debbugs.gnu.org id=B23313.146142351721657 (code B ref 23313); Sat, 23 Apr 2016 14:59:02 +0000 Original-Received: (at 23313) by debbugs.gnu.org; 23 Apr 2016 14:58:37 +0000 Original-Received: from localhost ([127.0.0.1]:44174 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1atz12-0005dF-VT for submit@debbugs.gnu.org; Sat, 23 Apr 2016 10:58:37 -0400 Original-Received: from mail.muc.de ([193.149.48.3]:22345) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1atz10-0005d4-Dn for 23313@debbugs.gnu.org; Sat, 23 Apr 2016 10:58:35 -0400 Original-Received: (qmail 37884 invoked by uid 3782); 23 Apr 2016 14:58:32 -0000 Original-Received: from acm.muc.de (p579E8F13.dip0.t-ipconnect.de [87.158.143.19]) by colin.muc.de (tmda-ofmipd) with ESMTP; Sat, 23 Apr 2016 16:58:30 +0200 Original-Received: (qmail 4808 invoked by uid 1000); 23 Apr 2016 14:58:32 -0000 Content-Disposition: inline In-Reply-To: <20160423114830.99485.qmail@mail.muc.de> User-Agent: Mutt/1.5.24 (2015-08-30) X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:116705 Archived-At: Hello again, Mohammed. On Sat, Apr 23, 2016 at 11:48:30AM -0000, Alan Mackenzie wrote: > In article you wrote: > > c-mode doesn't recognize (or at least colorize as per current rules) > > function declarations that returns a pointer (or pointer to pointer) to > > custom data types: > > Eg., If my c file contains: > > GtkWidget * > > my_window_new (GtkApplication *app); > > Its not recognized as a function declaration, while this works: > > GtkWidget > > my_window_new (GtkApplication *app); > Yes. > I think the reason for this is that the first expression is ambiguous. > It could be the function declaration you want it to be, or it could be > the arithmetic expression "GtkWidget times the result of calling > my_window_new with argument GtkApplication times app". We need to be > careful that expressions like that don't get fontified as declarations by > mistake. > My current idea for resolving this ambiguity is to examine the token > before GtkWidget; if this is a ";" or a "}" or a "{", then we have a > declaration, if it's something else, we have an arithmetic expression. > Or something like that. > Give me a little time, and I'll see what I can come up with. OK, I've written a trial patch along these lines. Could you try it out, please, and let me know whether it works OK, or still has some faults. diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el index 62bc236..d72b345 100644 --- a/lisp/progmodes/cc-engine.el +++ b/lisp/progmodes/cc-engine.el @@ -7087,6 +7087,9 @@ c-forward-decl-or-cast-1 backup-if-not-cast ;; For casts, the return position. cast-end + ;; Set when the symbol before `preceding-token-end' is known to + ;; terminate the previous construct, or when we're at point-min. + at-decl-start ;; Have we got a new-style C++11 "auto"? new-style-auto ;; Save `c-record-type-identifiers' and @@ -7096,6 +7099,15 @@ c-forward-decl-or-cast-1 (save-rec-type-ids c-record-type-identifiers) (save-rec-ref-ids c-record-ref-identifiers)) + (save-excursion + (goto-char preceding-token-end) + (setq at-decl-start + (or (bobp) + (let ((tok-end (point))) + (c-backward-token-2) + (member (buffer-substring-no-properties (point) tok-end) + c-pre-start-tokens))))) + (while (c-forward-annotation) (c-forward-syntactic-ws)) @@ -7696,12 +7708,15 @@ c-forward-decl-or-cast-1 at-type (or at-decl-end (looking-at "=[^=]")) (not context) - (not got-suffix)) - ;; Got something like "foo * bar;". Since we're not inside an - ;; arglist it would be a meaningless expression because the - ;; result isn't used. We therefore choose to recognize it as - ;; a declaration. Do not allow a suffix since it could then - ;; be a function call. + (or (not got-suffix) + at-decl-start)) + ;; Got something like "foo * bar;". Since we're not inside + ;; an arglist it would be a meaningless expression because + ;; the result isn't used. We therefore choose to recognize + ;; it as a declaration. We only allow a suffix (which makes + ;; the construct look like a function call) when + ;; `at-decl-start' provides additional evidence that we do + ;; have a declaration. (throw 'at-decl-or-cast t)) ;; CASE 17 diff --git a/lisp/progmodes/cc-langs.el b/lisp/progmodes/cc-langs.el index dd1bccf..c548394 100644 --- a/lisp/progmodes/cc-langs.el +++ b/lisp/progmodes/cc-langs.el @@ -1325,6 +1325,14 @@ 'c-opt-op-identitier-prefix (c-lang-defconst c-haskell-op-re t (c-make-keywords-re nil (c-lang-const c-haskell-op))) (c-lang-defvar c-haskell-op-re (c-lang-const c-haskell-op-re)) + +(c-lang-defconst c-pre-start-tokens + "List of operators following which an apparent declaration \(e.g. +\"t1 *fn (t2 *b);\") is most likely to be an actual declaration +\(as opposed to an arithmetic expression)." + t '(";" "{" "}")) +(c-lang-defvar c-pre-start-tokens (c-lang-const c-pre-start-tokens)) + ;;; Syntactic whitespace. > > Package: CC Mode 5.33 > > In GNU Emacs 25.0.92.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.18.9) > > of 2016-04-13 built on fedora.localdomain > > Windowing system distributor 'Fedora Project', version 11.0.11800000 > > Configured features: > > XPM JPEG TIFF GIF PNG SOUND DBUS GSETTINGS NOTIFY ACL LIBSELINUX GNUTLS > > LIBXML2 FREETYPE XFT ZLIB TOOLKIT_SCROLL_BARS GTK3 X11 -- Alan Mackenzie (Nuremberg, Germany).