emacs-diffs
[Top][All Lists]
Advanced

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

master fb8276a17c: CC Mode: Cease adding types to found-types too eagerl


From: Alan Mackenzie
Subject: master fb8276a17c: CC Mode: Cease adding types to found-types too eagerly
Date: Wed, 19 Oct 2022 10:54:56 -0400 (EDT)

branch: master
commit fb8276a17c26b4c2bf8281210ede114ff9e24958
Author: Alan Mackenzie <acm@muc.de>
Commit: Alan Mackenzie <acm@muc.de>

    CC Mode: Cease adding types to found-types too eagerly
    
    This fixes bug #58537 and bug #58539.
    
    * lisp/progmodes/cc-engine.el (c-forward-type): Remove trailing whitespace
    from an identifier before passing it to c-add-type.
    (c-forward-decl-or-cast-1): CASE 3: Do not recognize two consecutive
    identifiers as type + variable/function unless certain conditions are met.
    CASE 10: Do not recognize the "type" as a found type unless certain 
condtions
    are met.  (Near end): Do not recognize the identifier in a cast as a type
    unless certain conditions are met.
    
    * lisp/progmodes/cc-fonts.el (c-get-fontification-context): Recognize being
    in declaration parens when there is a syntactially wrong "foo ((bar))"
    preceding the match position.
    
    * lisp/progmodes/cc-mode.el (c-update-new-id): Set c-new-id-is-type
    unconditionally to nil to prevent a second identifier being wrongly marked 
as
    a type.
---
 lisp/progmodes/cc-engine.el | 35 ++++++++++++++++++++++++++++-------
 lisp/progmodes/cc-fonts.el  | 17 +++++++++++++++--
 lisp/progmodes/cc-mode.el   |  5 +++--
 3 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
index 596cccdf48..e71560fa25 100644
--- a/lisp/progmodes/cc-engine.el
+++ b/lisp/progmodes/cc-engine.el
@@ -9106,7 +9106,9 @@ multi-line strings (but not C++, for example)."
        (when (save-excursion
                (goto-char post-prefix-pos)
                (looking-at c-self-contained-typename-key))
-         (c-add-type pos (point)))
+         (c-add-type pos (save-excursion
+                           (c-backward-syntactic-ws)
+                           (point))))
        (when (and c-record-type-identifiers
                   c-last-identifier-range)
          (c-record-type-id c-last-identifier-range)))
@@ -9191,7 +9193,10 @@ multi-line strings (but not C++, for example)."
             (goto-char id-end)
             (if (or res c-promote-possible-types)
                 (progn
-                  (c-add-type id-start id-end)
+                  (c-add-type id-start (save-excursion
+                                         (goto-char id-end)
+                                         (c-backward-syntactic-ws)
+                                         (point)))
                   (when (and c-record-type-identifiers id-range)
                     (c-record-type-id id-range))
                   (unless res
@@ -10762,8 +10767,16 @@ This function might do hidden buffer changes."
                         (setq backup-if-not-cast t)
                         (throw 'at-decl-or-cast t)))
 
-                    (setq backup-if-not-cast t)
-                    (throw 'at-decl-or-cast t)))
+                    ;; If we're in declaration or template delimiters, or one
+                    ;; of a certain set of characters follows, we've got a
+                    ;; type and variable.
+                    (if (or (memq context '(decl <>))
+                            (memq (char-after) '(?\; ?, ?= ?\( ?{ ?:)))
+                        (progn
+                          (setq backup-if-not-cast t)
+                          (throw 'at-decl-or-cast t))
+                      ;; We're probably just typing a statement.
+                      (throw 'at-decl-or-cast nil))))
 
                 ;; CASE 4
                 (when (and got-suffix
@@ -10879,8 +10892,13 @@ This function might do hidden buffer changes."
 
         ;; CASE 10
         (when at-decl-or-cast
-          ;; By now we've located the type in the declaration that we know
-          ;; we're in.
+          ;; By now we've located the type in the declaration that we think
+          ;; we're in.  Do we have enough evidence to promote the putative
+          ;; type to a found type?  The user may be halfway through typing
+          ;; a statement beginning with an identifier.
+          (when (and (eq at-type 'maybe)
+                     (not (eq context 'top)))
+            (setq c-record-type-identifiers nil))
           (throw 'at-decl-or-cast t))
 
         ;; CASE 11
@@ -11123,7 +11141,10 @@ This function might do hidden buffer changes."
                    (not (c-on-identifier)))))))))
 
       ;; Handle the cast.
-      (when (and c-record-type-identifiers at-type (not (eq at-type t)))
+      (when (and c-record-type-identifiers
+                at-type
+                (not (memq at-type '(t maybe)))) ; 'maybe isn't strong enough
+                                       ; evidence to promote the type.
        (let ((c-promote-possible-types t))
          (goto-char type-start)
          (c-forward-type)))
diff --git a/lisp/progmodes/cc-fonts.el b/lisp/progmodes/cc-fonts.el
index b4ff32b907..aa16da7070 100644
--- a/lisp/progmodes/cc-fonts.el
+++ b/lisp/progmodes/cc-fonts.el
@@ -1197,8 +1197,21 @@ casts and declarations are fontified.  Used on level 2 
and higher."
   ;; arguments lists (i.e. lists enclosed by <...>) is more strict about what
   ;; characters it allows within the list.
   (let ((type (and (> match-pos (point-min))
-                  (c-get-char-property (1- match-pos) 'c-type))))
-    (cond ((not (memq (char-before match-pos) '(?\( ?, ?\[ ?< ?{)))
+                  (c-get-char-property (1- match-pos) 'c-type)))
+       id-pos)
+    (cond
+     ;; Are we just after something like "(foo((bar))" ?
+     ((and (eq (char-before match-pos) ?\))
+          (c-go-list-backward match-pos)
+          (progn
+            (c-backward-syntactic-ws)
+            (and (setq id-pos (c-on-identifier))
+                 (goto-char id-pos)
+                 (progn
+                   (c-backward-syntactic-ws)
+                   (eq (char-before) ?\()))))
+      (c-get-fontification-context (point) not-front-decl toplev))
+         ((not (memq (char-before match-pos) '(?\( ?, ?\[ ?< ?{)))
           (cons (and toplev 'top) nil))
          ;; A control flow expression or a decltype
          ((and (eq (char-before match-pos) ?\()
diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el
index dce300f33c..2aa6b90dea 100644
--- a/lisp/progmodes/cc-mode.el
+++ b/lisp/progmodes/cc-mode.el
@@ -2080,13 +2080,14 @@ with // and /*, not more generic line and block 
comments."
 (defun c-update-new-id (end)
   ;; Note the bounds of any identifier that END is in or just after, in
   ;; `c-new-id-start' and `c-new-id-end'.  Otherwise set these variables to
-  ;; nil.
+  ;; nil.  Set `c-new-id-is-type' unconditionally to nil.
   (save-excursion
     (goto-char end)
     (let ((id-beg (c-on-identifier)))
       (setq c-new-id-start id-beg
            c-new-id-end (and id-beg
-                             (progn (c-end-of-current-token) (point)))))))
+                             (progn (c-end-of-current-token) (point)))
+           c-new-id-is-type nil))))
 
 (defun c-post-command ()
   ;; If point was inside of a new identifier and no longer is, record that



reply via email to

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