From 21be03cccb611ea9e6c73fb04e578c48edf49a25 Mon Sep 17 00:00:00 2001 From: Alan Mackenzie Date: Thu, 19 Jan 2023 10:59:10 +0000 Subject: [PATCH] CC Mode: Prevent two classes of "type" prematurely entering c-found-types This fixes bug #60769. The two classes of "type" are foo and bar in "foo d(bar () ...)", where the d could be a mistyped C-M-d. * list/progmodes/cc-engine.el (c-forward-decl-or-cast-1): New local variable got-arglist. Refactor a sequence of enclosed `if' forms into a `cond' form. Set got-arglist when needed. In CASE 2, set unsafe-maybe to inhibit foo being entered into c-found-types. In CASE 19, likewise set unsafe-maybe, to inhibit bar entering c-found-types. --- lisp/progmodes/cc-engine.el | 42 +++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el index 3fa407dd338..ebcb20f0f8c 100644 --- a/lisp/progmodes/cc-engine.el +++ b/lisp/progmodes/cc-engine.el @@ -10475,6 +10475,8 @@ This function might do hidden buffer changes." got-prefix ;; True if the declarator is surrounded by a parenthesis pair. got-parens + ;; True if there is a terminated argument list. + got-arglist ;; True if there is an identifier in the declarator. got-identifier ;; True if we find a number where an identifier was expected. @@ -10622,13 +10624,17 @@ This function might do hidden buffer changes." (when (> paren-depth 0) (setq paren-depth (1- paren-depth)) (forward-char) + (when (and (not got-parens) + (eq paren-depth 0)) + (setq got-arglist t)) t) - (when (if (save-match-data (looking-at "\\s(")) - (c-safe (c-forward-sexp 1) t) - (if (save-match-data - (looking-at c-fun-name-substitute-key)) ; requires - (c-forward-c++-requires-clause) - (goto-char (match-end 1)) + (when (cond + ((save-match-data (looking-at "\\s(")) + (c-safe (c-forward-sexp 1) t)) + ((save-match-data + (looking-at c-fun-name-substitute-key)) ; C++ requires + (c-forward-c++-requires-clause)) + (t (goto-char (match-end 1)) t)) (when (and (not got-suffix-after-parens) (= paren-depth 0)) @@ -10690,8 +10696,11 @@ This function might do hidden buffer changes." (goto-char pos) (setq pd (1- pd))) t))) - (c-fdoc-shift-type-backward) - t))) + (c-fdoc-shift-type-backward) + (when (and (not got-parens) + (eq paren-depth 0)) + (setq got-arglist t)) + t))) (c-forward-syntactic-ws)) @@ -10759,6 +10768,9 @@ This function might do hidden buffer changes." (not (or got-prefix got-parens))) ;; Got another identifier directly after the type, so it's a ;; declaration. + (when (and got-arglist + (eq at-type 'maybe)) + (setq unsafe-maybe t)) (throw 'at-decl-or-cast t)) (when (and got-parens @@ -11147,9 +11159,17 @@ This function might do hidden buffer changes." ;; inside an arglist that contains declarations. Update (2017-09): We ;; now recognize a top-level "foo(bar);" as a declaration in C. ;; CASE 19 - (or (eq context 'decl) - (and (c-major-mode-is 'c-mode) - (or (eq context 'top) make-top)))))) + (when + (or (eq context 'decl) + (and (c-major-mode-is 'c-mode) + (or (eq context 'top) make-top))) + (when (and (eq at-type 'maybe) + got-parens) + ;; If we've got "foo d(bar () ...)", the d could be a typing + ;; mistake, so we don't promote the 'maybe type "bar" to a 'found + ;; type. + (setq unsafe-maybe t)) + t)))) ;; The point is now after the type decl expression. -- 2.39.2