emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

emacs-27 7938713: C++ Mode: handle comma separated brace initializers.


From: Alan Mackenzie
Subject: emacs-27 7938713: C++ Mode: handle comma separated brace initializers.
Date: Fri, 4 Sep 2020 15:12:59 -0400 (EDT)

branch: emacs-27
commit 793871310599f8c75cf86bed97bdad70577cc5dd
Author: Alan Mackenzie <acm@muc.de>
Commit: Alan Mackenzie <acm@muc.de>

    C++ Mode: handle comma separated brace initializers.
    
    This includes both indentation and fontification.
    
    * lisp/progmodes/cc-engine.el (c-do-declarators): Handle brace initializers
    without = correctly.
    (c-looking-at-or-maybe-in-bracelist): Use c-do-declarators with a simple
    inline function to check that after-type-id-pos points to the start of a
    declarator.
    
    * lisp/progmodes/cc-langs.el (c-recognize-bare-brace-inits): New lang
    const/variable.
---
 lisp/progmodes/cc-engine.el | 53 ++++++++++++++++++++++++++++++---------------
 lisp/progmodes/cc-langs.el  | 14 ++++++++++++
 2 files changed, 49 insertions(+), 18 deletions(-)

diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
index 34490d1..4b14dd1 100644
--- a/lisp/progmodes/cc-engine.el
+++ b/lisp/progmodes/cc-engine.el
@@ -9091,7 +9091,7 @@ This function might do hidden buffer changes."
   (let
       ((cdd-pos (point)) cdd-next-pos cdd-id-start cdd-id-end
        cdd-decl-res cdd-got-func cdd-got-type cdd-got-init
-       c-last-identifier-range cdd-exhausted)
+       c-last-identifier-range cdd-exhausted cdd-after-block)
 
     ;; The following `while' applies `cdd-function' to a single declarator id
     ;; each time round.  It loops only when CDD-LIST is non-nil.
@@ -9144,23 +9144,25 @@ This function might do hidden buffer changes."
                 (c-forward-syntactic-ws cdd-limit)
               (setq cdd-exhausted t))) ; unbalanced parens
 
-           (cdd-got-init       ; "=" sign OR opening "(", "[", or "{"
-            ;; Skip an initializer expression.  If we're at a '='
-            ;; then accept a brace list directly after it to cope
-            ;; with array initializers.  Otherwise stop at braces
-            ;; to avoid going past full function and class blocks.
-            (if (and (if (and (eq cdd-got-init ?=)
-                              (= (c-forward-token-2 1 nil cdd-limit) 0)
-                              (looking-at "{"))
-                         (c-go-list-forward (point) cdd-limit)
-                       t)
-                     ;; FIXME: Should look for c-decl-end markers here;
-                     ;; we might go far into the following declarations
-                     ;; in e.g. ObjC mode (see e.g. methods-4.m).
-                     (c-syntactic-re-search-forward "[;,{]" cdd-limit 'move t))
+           (cdd-got-init               ; "=" sign OR opening "(", "[", or "("
+            ;; Skip an initializer expression in braces, whether or not (in
+            ;; C++ Mode) preceded by an "=".  Be careful that the brace list
+            ;; isn't a code block or a struct (etc.) block.
+            (cond
+             ((and (eq cdd-got-init ?=)
+                   (zerop (c-forward-token-2 1 nil  cdd-limit))
+                   (eq (char-after) ?{)
+                   (c-go-list-forward (point) cdd-limit)))
+             ((and (eq cdd-got-init ?{)
+                   c-recognize-bare-brace-inits
+                   (setq cdd-after-block
+                         (save-excursion
+                           (c-go-list-forward (point) cdd-limit)))
+                   (not (c-looking-at-statement-block)))
+              (goto-char cdd-after-block)))
+            (if (c-syntactic-re-search-forward "[;,{]" cdd-limit 'move t)
                 (backward-char)
-              (setq cdd-exhausted t)
-              ))
+              (setq cdd-exhausted t)))
 
            (t (c-forward-syntactic-ws cdd-limit)))
 
@@ -11729,7 +11731,22 @@ comment at the start of cc-engine.el for more info."
                           (save-excursion (c-backward-syntactic-ws) (point))
                           nil nil))
                    (and (consp res)
-                        (eq (car res) after-type-id-pos))))))
+                        (cond
+                         ((eq (car res) after-type-id-pos))
+                         ((> (car res) after-type-id-pos) nil)
+                         (t
+                          (catch 'find-decl
+                            (save-excursion
+                              (goto-char (car res))
+                              (c-do-declarators
+                               (point-max) t nil nil
+                               (lambda (id-start id-end tok not-top func init)
+                                 (cond
+                                  ((> id-start after-type-id-pos)
+                                   (throw 'find-decl nil))
+                                  ((eq id-start after-type-id-pos)
+                                   (throw 'find-decl t)))))
+                              nil)))))))))
          (cons bufpos (or in-paren inexpr-brace-list)))
         ((or (eq (char-after) ?\;)
              ;; Brace lists can't contain a semicolon, so we're done.
diff --git a/lisp/progmodes/cc-langs.el b/lisp/progmodes/cc-langs.el
index e7e7cfd..0a7f456 100644
--- a/lisp/progmodes/cc-langs.el
+++ b/lisp/progmodes/cc-langs.el
@@ -3684,6 +3684,20 @@ Foo bar = gnu;"
   c++ t)
 (c-lang-defvar c-recognize-paren-inits (c-lang-const c-recognize-paren-inits))
 
+(c-lang-defconst c-recognize-bare-brace-inits
+  "Non-nil means that brace initializers without \"=\" exist,
+i.e. constructs like
+
+int foo[] {1, 2, 3};
+
+in addition to the more classic
+
+int foo[] = {1, 2, 3};"
+  t nil
+  c++ t)
+(c-lang-defvar c-recognize-bare-brace-inits
+              (c-lang-const c-recognize-bare-brace-inits))
+
 (c-lang-defconst c-recognize-paren-inexpr-blocks
   "Non-nil to recognize gcc style in-expression blocks,
 i.e. compound statements surrounded by parentheses inside expressions."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]