]> git.eshelyaron.com Git - emacs.git/commitdiff
Correctly analyze C++ list initialization in member init areas.
authorAlan Mackenzie <acm@muc.de>
Tue, 30 Aug 2016 15:36:15 +0000 (15:36 +0000)
committerAlan Mackenzie <acm@muc.de>
Tue, 30 Aug 2016 15:36:15 +0000 (15:36 +0000)
* lisp/progmodes/cc-engine.el (c-back-over-list-of-member-inits): Remove a
superfluous call to c-backward-syntactic-ws.
(c-looking-at-or-maybe-in-bracelist): Change calling convention, so that it
reports encountering the Java "new" keyword.  Add, as an alternative, a check
that we can move back over member initializations, and handle this as finding
a brace block.
(c-looking-at-special-brace-list, c-guess-continued-construct): Adapt to the
new calling convention of c-looking-at-or-maybe-in-bracelist.
(c-guess-basic-syntax, CASE 5A.3): Replace lots of inline ad-hoc code with
calls to c-backward-over-enum-header and c-looking-at-or-maybe-in-bracelist,
using the extra info from the value of that function to generate a
topmost-into-cont element where needed (in Java).

* lisp/progmodes/cc-fonts.el (c-font-lock-declarations): Adapt to the new
calling convention of c-looking-at-or-maybe-in-bracelist.

* lisp/progmodes/cc-mode.el (c-fl-decl-start): Adapt to the new calling
convention of c-looking-at-or-maybe-in-bracelist.

lisp/progmodes/cc-engine.el
lisp/progmodes/cc-fonts.el
lisp/progmodes/cc-mode.el

index 28d6618d8a042fdfbf8f99a2ebbd473592d0edb9..83ed308e10790f80dbb50bdee009d4d6e8751a3e 100644 (file)
@@ -7434,7 +7434,6 @@ comment at the start of cc-engine.el for more info."
        (when (not (c-backward-<>-arglist nil))
         (throw 'done nil))
        (c-backward-syntactic-ws))
-     (c-backward-syntactic-ws)
      (when (not (c-back-over-compound-identifier))
        (throw 'level nil))
      (c-backward-syntactic-ws)))
@@ -9831,15 +9830,18 @@ comment at the start of cc-engine.el for more info."
     (or (looking-at c-brace-list-key)
        (progn (goto-char here) nil))))
 
-(defun c-looking-at-or-maybe-in-bracelist (containing-sexp &optional lim)
-  ;; Point is at an open brace.  If this starts a brace list, return the
-  ;; buffer position of the start of the construct which introduces the list.
-  ;; Otherwise, if point might be inside an enclosing brace list, return t.
-  ;; If point is definitely neither at nor in a brace list, return nil.
+(defun c-looking-at-or-maybe-in-bracelist (&optional containing-sexp lim)
+  ;; Point is at an open brace.  If this starts a brace list, return a list
+  ;; whose car is the buffer position of the start of the construct which
+  ;; introduces the list, and whose cdr is t if we have parsed a keyword
+  ;; matching `c-opt-inexpr-brace-list-key' (e.g. Java's "new"), nil
+  ;; otherwise.  Otherwise, if point might be inside an enclosing brace list,
+  ;; return t.  If point is definitely neither at nor in a brace list, return
+  ;; nil.
   ;;
   ;; CONTAINING-SEXP is the position of the brace/paren/braacket enclosing
-  ;; POINT, or nil if there is no such position.  LIM is a backward search
-  ;; limit.
+  ;; POINT, or nil if there is no such position, or we do not know it.  LIM is
+  ;; a backward search limit.
   ;;
   ;; Here, "brace list" does not include the body of an enum.
   (save-excursion
@@ -9850,7 +9852,7 @@ comment at the start of cc-engine.el for more info."
           (and (c-major-mode-is 'pike-mode)
                c-decl-block-key))
          (braceassignp 'dontknow)
-         bufpos macro-start res after-type-id-pos)
+         inexpr-brace-list bufpos macro-start res pos after-type-id-pos)
 
       (setq res (c-backward-token-2 1 t lim))
       ;; Checks to do only on the first sexp before the brace.
@@ -9886,7 +9888,9 @@ comment at the start of cc-engine.el for more info."
        (while (eq braceassignp 'dontknow)
          (setq braceassignp
                (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
-                     ((looking-at c-opt-inexpr-brace-list-key) t)
+                     ((looking-at c-opt-inexpr-brace-list-key)
+                      (setq inexpr-brace-list t)
+                      t)
                      ((looking-at "\\sw\\|\\s_\\|[.[]")
                       ;; Carry on looking if this is an
                       ;; identifier (may contain "." in Java)
@@ -9894,104 +9898,116 @@ comment at the start of cc-engine.el for more info."
                       'dontknow)
                      (t nil)))))
 
-      ;; Checks to do on all sexps before the brace, up to the
-      ;; beginning of the statement.
-      (while (eq braceassignp 'dontknow)
-       (cond ((eq (char-after) ?\;)
-              (setq braceassignp nil))
-             ((and class-key
-                   (looking-at class-key))
-              (setq braceassignp nil))
-             ((eq (char-after) ?=)
-              ;; We've seen a =, but must check earlier tokens so
-              ;; that it isn't something that should be ignored.
-              (setq braceassignp 'maybe)
-              (while (and (eq braceassignp 'maybe)
-                          (zerop (c-backward-token-2 1 t lim)))
-                (setq braceassignp
-                      (cond
-                       ;; Check for operator =
-                       ((and c-opt-op-identifier-prefix
-                             (looking-at c-opt-op-identifier-prefix))
-                        nil)
-                       ;; Check for `<opchar>= in Pike.
-                       ((and (c-major-mode-is 'pike-mode)
-                             (or (eq (char-after) ?`)
-                                 ;; Special case for Pikes
-                                 ;; `[]=, since '[' is not in
-                                 ;; the punctuation class.
-                                 (and (eq (char-after) ?\[)
-                                      (eq (char-before) ?`))))
-                        nil)
-                       ((looking-at "\\s.") 'maybe)
-                       ;; make sure we're not in a C++ template
-                       ;; argument assignment
-                       ((and
-                         (c-major-mode-is 'c++-mode)
-                         (save-excursion
-                           (let ((here (point))
-                                 (pos< (progn
-                                         (skip-chars-backward "^<>")
-                                         (point))))
-                             (and (eq (char-before) ?<)
-                                  (not (c-crosses-statement-barrier-p
-                                        pos< here))
-                                  (not (c-in-literal))
-                                  ))))
-                        nil)
-                       (t t))))))
-       (if (and (eq braceassignp 'dontknow)
-                (/= (c-backward-token-2 1 t lim) 0))
-           (setq braceassignp nil)))
+      (setq pos (point))
+      (if (and after-type-id-pos
+              (goto-char after-type-id-pos)
+              (setq res (c-back-over-member-initializers))
+              (goto-char res)
+              (eq (car (c-beginning-of-decl-1 lim)) 'same))
+         (cons (point) nil)            ; Return value.
+      
+       (goto-char pos)
+       ;; Checks to do on all sexps before the brace, up to the
+       ;; beginning of the statement.
+       (while (eq braceassignp 'dontknow)
+         (cond ((eq (char-after) ?\;)
+                (setq braceassignp nil))
+               ((and class-key
+                     (looking-at class-key))
+                (setq braceassignp nil))
+               ((eq (char-after) ?=)
+                ;; We've seen a =, but must check earlier tokens so
+                ;; that it isn't something that should be ignored.
+                (setq braceassignp 'maybe)
+                (while (and (eq braceassignp 'maybe)
+                            (zerop (c-backward-token-2 1 t lim)))
+                  (setq braceassignp
+                        (cond
+                         ;; Check for operator =
+                         ((and c-opt-op-identifier-prefix
+                               (looking-at c-opt-op-identifier-prefix))
+                          nil)
+                         ;; Check for `<opchar>= in Pike.
+                         ((and (c-major-mode-is 'pike-mode)
+                               (or (eq (char-after) ?`)
+                                   ;; Special case for Pikes
+                                   ;; `[]=, since '[' is not in
+                                   ;; the punctuation class.
+                                   (and (eq (char-after) ?\[)
+                                        (eq (char-before) ?`))))
+                          nil)
+                         ((looking-at "\\s.") 'maybe)
+                         ;; make sure we're not in a C++ template
+                         ;; argument assignment
+                         ((and
+                           (c-major-mode-is 'c++-mode)
+                           (save-excursion
+                             (let ((here (point))
+                                   (pos< (progn
+                                           (skip-chars-backward "^<>")
+                                           (point))))
+                               (and (eq (char-before) ?<)
+                                    (not (c-crosses-statement-barrier-p
+                                          pos< here))
+                                    (not (c-in-literal))
+                                    ))))
+                          nil)
+                         (t t))))))
+         (if (and (eq braceassignp 'dontknow)
+                  (/= (c-backward-token-2 1 t lim) 0))
+             (setq braceassignp nil)))
 
-      (cond
-       (braceassignp
-       ;; We've hit the beginning of the aggregate list.
-       (c-beginning-of-statement-1 containing-sexp)
-       (point))
-       ((and after-type-id-pos
-            (save-excursion
-              (when (eq (char-after) ?\;)
-                (c-forward-token-2 1 t))
-              (setq bufpos (point))
-              (when (looking-at c-opt-<>-sexp-key)
-                (c-forward-token-2)
-                (when (and (eq (char-after) ?<)
-                           (c-get-char-property (point) 'syntax-table))
-                  (c-go-list-forward nil after-type-id-pos)
-                  (c-forward-syntactic-ws)))
-              (and
-               (or (not (looking-at c-class-key))
-                   (save-excursion
-                     (goto-char (match-end 1))
-                     (c-forward-syntactic-ws)
-                     (not (eq (point) after-type-id-pos))))
-               (progn
-                 (setq res
-                       (c-forward-decl-or-cast-1
-                        (save-excursion (c-backward-syntactic-ws) (point))
-                        nil nil))
-                 (and (consp res)
-                      (eq (car res) after-type-id-pos))))))
-       bufpos)
-       ((eq (char-after) ?\;)
-       ;; Brace lists can't contain a semicolon, so we're done.
-       ;; (setq containing-sexp nil)
-       nil)
-       ((and (setq macro-start (point))
-            (c-forward-to-cpp-define-body)
-            (eq (point) start))
-       ;; We've a macro whose expansion starts with the '{'.
-       ;; Heuristically, if we have a ';' in it we've not got a
-       ;; brace list, otherwise we have.
-       (let ((macro-end (progn (c-end-of-macro) (point))))
-         (goto-char start)
-         (forward-char)
-         (if (and (c-syntactic-re-search-forward "[;,]" macro-end t t)
-                  (eq (char-before) ?\;))
-             nil
-           macro-start)))
-       (t t)) ;; The caller can go up one level.
+       (cond
+        (braceassignp
+         ;; We've hit the beginning of the aggregate list.
+         (c-beginning-of-statement-1 containing-sexp)
+         (cons (point) inexpr-brace-list))
+        ((and after-type-id-pos
+              (save-excursion
+                (when (eq (char-after) ?\;)
+                  (c-forward-token-2 1 t))
+                (setq bufpos (point))
+                (when (looking-at c-opt-<>-sexp-key)
+                  (c-forward-token-2)
+                  (when (and (eq (char-after) ?<)
+                             (c-get-char-property (point) 'syntax-table))
+                    (c-go-list-forward nil after-type-id-pos)
+                    (c-forward-syntactic-ws)))
+                (and
+                 (or (not (looking-at c-class-key))
+                     (save-excursion
+                       (goto-char (match-end 1))
+                       (c-forward-syntactic-ws)
+                       (not (eq (point) after-type-id-pos))))
+                 (progn
+                   (setq res
+                         (c-forward-decl-or-cast-1
+                          (save-excursion (c-backward-syntactic-ws) (point))
+                          nil nil))
+                   (and (consp res)
+                        (eq (car res) after-type-id-pos))))))
+         (cons bufpos inexpr-brace-list))
+        ((eq (char-after) ?\;)
+         ;; Brace lists can't contain a semicolon, so we're done.
+         ;; (setq containing-sexp nil)
+         nil)
+        ((and (setq macro-start (point))
+              (c-forward-to-cpp-define-body)
+              (eq (point) start))
+         ;; We've a macro whose expansion starts with the '{'.
+         ;; Heuristically, if we have a ';' in it we've not got a
+         ;; brace list, otherwise we have.
+         (let ((macro-end (progn (c-end-of-macro) (point))))
+           (goto-char start)
+           (forward-char)
+           (if (and (c-syntactic-re-search-forward "[;,]" macro-end t t)
+                    (eq (char-before) ?\;))
+               nil
+             (cons macro-start nil)))) ; (2016-08-30): Lazy! We have no
+                                       ; languages where
+                                       ; `c-opt-inexpr-brace-list-key' is
+                                       ; non-nil and we have macros.
+        (t t)))                        ;; The caller can go up one level.
       )))
 
 (defun c-inside-bracelist-p (containing-sexp paren-state)
@@ -10040,7 +10056,7 @@ comment at the start of cc-engine.el for more info."
             (setq containing-sexp next-containing
                   lim nil
                   next-containing nil))))
-       (and (numberp bufpos) bufpos)))))
+       (and (consp bufpos) (car bufpos))))))
 
 (defun c-looking-at-special-brace-list (&optional lim)
   ;; If we're looking at the start of a pike-style list, i.e., `({ })',
@@ -10688,7 +10704,7 @@ comment at the start of cc-engine.el for more info."
 
        ;; CASE B.2: brace-list-open
        ((or (consp special-brace-list)
-           (numberp
+           (consp
             (c-looking-at-or-maybe-in-bracelist
              containing-sexp beg-of-same-or-containing-stmt))
            )
@@ -11231,42 +11247,18 @@ comment at the start of cc-engine.el for more info."
 
           ;; CASE 5A.3: brace list open
           ((save-excursion
-             (c-beginning-of-decl-1 lim)
-             (while (cond
-                     ((looking-at c-specifier-key)
-                      (c-forward-keyword-clause 1))
-                     ((and c-opt-cpp-prefix
-                           (looking-at c-noise-macro-with-parens-name-re))
-                      (c-forward-noise-clause))))
-             (setq placeholder (c-point 'boi))
-             (or (consp special-brace-list)
-                 (and (or (save-excursion
-                            (goto-char indent-point)
-                            (setq tmpsymbol nil)
-                            (while (and (> (point) placeholder)
-                                        (zerop (c-backward-token-2 1 t))
-                                        (not (looking-at "=\\([^=]\\|$\\)")))
-                              (and c-opt-inexpr-brace-list-key
-                                   (not tmpsymbol)
-                                   (looking-at c-opt-inexpr-brace-list-key)
-                                   (setq tmpsymbol 'topmost-intro-cont)))
-                            (looking-at "=\\([^=]\\|$\\)"))
-                          (looking-at c-brace-list-key)
-                          (looking-at c-return-key)
-                          (save-excursion
-                            (and (c-forward-type)
-                                 (looking-at c-identifier-start)
-                                 (not (looking-at c-keywords-regexp))
-                                 (c-forward-token-2)
-                                 (eq (point) (c-point 'boi indent-point)))))
-                      (save-excursion
-                        (while (and (< (point) indent-point)
-                                    (zerop (c-forward-token-2 1 t))
-                                    (not (memq (char-after) '(?\; ?\()))))
-                        (not (memq (char-after) '(?\; ?\()))
-                        ))))
+             (goto-char indent-point)
+             (skip-chars-forward " \t")
+             (cond
+              ((c-backward-over-enum-header)
+               (setq placeholder (c-point 'boi)))
+              ((consp (setq placeholder 
+                            (c-looking-at-or-maybe-in-bracelist
+                             containing-sexp lim)))
+               (setq tmpsymbol (and (cdr placeholder) 'topmost-intro-cont))
+               (setq placeholder (c-point 'boi (car placeholder))))))
            (if (and (not c-auto-newline-analysis)
-                    (c-major-mode-is 'java-mode)
+                    ;(c-major-mode-is 'java-mode)  ; Not needed anymore (2016-08-30).
                     (eq tmpsymbol 'topmost-intro-cont))
                ;; We're in Java and have found that the open brace
                ;; belongs to a "new Foo[]" initialization list,
index bf8b857ca6ea4ad4095c44aa32e437a203221919..a23dddf214e7adbb92d2975b34fbd52e3da2d4ad 100644 (file)
@@ -1261,12 +1261,12 @@ casts and declarations are fontified.  Used on level 2 and higher."
                           c-restricted-<>-arglists nil)
                     (c-put-char-property (1- match-pos) 'c-type
                                          'c-decl-arg-start))
-                   ;; We're inside an brace list.
+                   ;; We're inside a brace list.
                    ((and (eq (char-before match-pos) ?{)
                          (save-excursion
                            (goto-char (1- match-pos))
-                           (numberp
-                            (c-looking-at-or-maybe-in-bracelist nil))))
+                           (consp
+                            (c-looking-at-or-maybe-in-bracelist))))
                     (setq context 'not-decl
                           c-restricted-<>-arglists nil)
                     (c-put-char-property (1- match-pos) 'c-type
index 5b324d6d24c2c4bf0a479c0ae1bb15b564046de3..f2c62563c5f314313b89f545ea89feca72e833d1 100644 (file)
@@ -1406,7 +1406,7 @@ Note that the style variables are always made local to the buffer."
                    (and (eq (char-before) ?{)
                         (save-excursion
                           (backward-char)
-                          (numberp (c-looking-at-or-maybe-in-bracelist nil))))
+                          (consp (c-looking-at-or-maybe-in-bracelist))))
                    )))
         (not (bobp)))
       (backward-char))                 ; back over (, [, <.