From aabb844e45642d6bf80673159dae18f4ea3693e4 Mon Sep 17 00:00:00 2001 From: Alan Mackenzie Date: Fri, 26 Jul 2019 17:48:45 +0000 Subject: [PATCH] CC Mode. Create lang vars for certain skipping expressions at compile time This saves repeated calculations at run time. * lisp/progmodes/cc-langs.el (c-stmt-boundary-skip-chars) (c-stmt-boundary-skip-list, c-stmt-boundary-skip-chars-with-comma) (c-stmt-boundary-skip-list-with-comma): New lang constants/variables. * lisp/progmodes/cc-engine.el (c-commas-bound-stmts): New variable (c-beginning-of-statement-1): Set c-commas-bound-stmts rather than c-stmt-delim-chars. (c-crosses-statement-barrier-p): Remove the now unneeded calculations of c-stmt-delim-chars. Set skip chars to one of the new lang variables, and later to a substring of it. (c-at-statement-start-p): Set c-syntactic-skip-backward from the new variables. (c-at-expression-start-p): Bind c-commas-bound-stmts. Use c-stmt-delim-chars-with-comma rather than c-stmt-delim-chars in a backward scan. (c-guess-basic-syntax): Bind c-commas-bound-stmts rather than c-stmt-delim-chars to itself. Bind c-commas-bound-stmts to t at another place rather than setting c-stmt-delim-chars to c-stmt-delim-chars-with-comma. --- lisp/progmodes/cc-engine.el | 52 ++++++++++++++++++++----------------- lisp/progmodes/cc-langs.el | 44 +++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 24 deletions(-) diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el index a89528d033c..1ebacb58c7f 100644 --- a/lisp/progmodes/cc-engine.el +++ b/lisp/progmodes/cc-engine.el @@ -713,6 +713,10 @@ comment at the start of cc-engine.el for more info." ;; the byte compiler. (defvar c-maybe-labelp) +(defvar c-commas-bound-stmts nil) + ;; Set to non-nil when `c-beginning-of-statement-1' is to regard a comma as + ;; a statement terminator. + ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22 ;; Macros used internally in c-beginning-of-statement-1 for the @@ -897,9 +901,7 @@ comment at the start of cc-engine.el for more info." (start (point)) macro-start (delims (if comma-delim '(?\; ?,) '(?\;))) - (c-stmt-delim-chars (if comma-delim - c-stmt-delim-chars-with-comma - c-stmt-delim-chars)) + (c-commas-bound-stmts (or c-commas-bound-stmts comma-delim)) c-maybe-labelp after-case:-pos saved ;; Current position. pos @@ -1422,21 +1424,12 @@ the line. If this virtual semicolon is _at_ from, the function recognizes it. Note that this function might do hidden buffer changes. See the comment at the start of cc-engine.el for more info." - (let* ((skip-chars - ;; If the current language has CPP macros, insert # into skip-chars. - (if c-opt-cpp-symbol - (concat (substring c-stmt-delim-chars 0 1) ; "^" - c-opt-cpp-symbol ; usually "#" - (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:" - c-stmt-delim-chars)) - (skip-chars - (if (c-major-mode-is 'c++-mode) - (concat (substring skip-chars 0 1) ; "^" - "[" ; to catch C++ attributes - (substring skip-chars 1)) ; e.g. "#;{}?:" - skip-chars)) - (non-skip-list - (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:) + (let* ((skip-chars (if c-commas-bound-stmts + c-stmt-boundary-skip-chars-with-comma + c-stmt-boundary-skip-chars)) ; e.g. "^#;{}?:" + (non-skip-list (if c-commas-bound-stmts + c-stmt-boundary-skip-list-with-comma + c-stmt-boundary-skip-list)) ; e.g. (?# ?\; ?{ ?} ?? ?:) lit-range lit-start vsemi-pos attr-end) (save-restriction (widen) @@ -1477,7 +1470,11 @@ comment at the start of cc-engine.el for more info." ;; A question mark. Can't be a label, so stop ;; looking for more : and ?. (setq c-maybe-labelp nil - skip-chars (substring c-stmt-delim-chars 0 -2))) + skip-chars + (substring (if c-commas-bound-stmts + c-stmt-delim-chars-with-comma + c-stmt-delim-chars) + 0 -2))) ;; At a CPP construct or a "#" or "##" operator? ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol)) (if (save-excursion @@ -1513,7 +1510,13 @@ comment at the start of cc-engine.el for more info." (save-excursion (let ((end (point)) c-maybe-labelp) - (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t) + (c-syntactic-skip-backward + (substring + (if c-commas-bound-stmts + c-stmt-delim-chars-with-comma + c-stmt-delim-chars) + 1) + nil t) (or (bobp) (eq (char-before) ?}) (and (eq (char-before) ?{) @@ -1540,9 +1543,10 @@ comment at the start of cc-engine.el for more info." (save-excursion (let ((end (point)) - (c-stmt-delim-chars c-stmt-delim-chars-with-comma) + (c-commas-bound-stmts t) c-maybe-labelp) - (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t) + (c-syntactic-skip-backward (substring c-stmt-delim-chars-with-comma 1) + nil t) (or (bobp) (memq (char-before) '(?{ ?})) (save-excursion (backward-char) @@ -12587,7 +12591,7 @@ comment at the start of cc-engine.el for more info." ;; There's always at most one syntactic element which got ;; an anchor pos. It's stored in syntactic-relpos. syntactic-relpos - (c-stmt-delim-chars c-stmt-delim-chars)) + (c-commas-bound-stmts c-commas-bound-stmts)) ;; Check if we're directly inside an enclosing declaration ;; level block. @@ -12639,7 +12643,7 @@ comment at the start of cc-engine.el for more info." ;; arglists. (when (and containing-sexp (eq (char-after containing-sexp) ?\()) - (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma)) + (setq c-commas-bound-stmts t)) ;; cache char before and after indent point, and move point to ;; the most likely position to perform the majority of tests (goto-char indent-point) diff --git a/lisp/progmodes/cc-langs.el b/lisp/progmodes/cc-langs.el index a0d4559c207..8a4f8f59515 100644 --- a/lisp/progmodes/cc-langs.el +++ b/lisp/progmodes/cc-langs.el @@ -1442,12 +1442,56 @@ operators." t "^;{}?:") (c-lang-defvar c-stmt-delim-chars (c-lang-const c-stmt-delim-chars)) +(c-lang-defconst c-stmt-boundary-skip-chars + ;; Like `c-stmt-delim-chars', but augmented by "#" for languages with CPP + ;; constructs, and for C++ Mode, also by "[", to help deal with C++ + ;; attributes. + t (if (c-lang-const c-opt-cpp-symbol) + (concat (substring (c-lang-const c-stmt-delim-chars) 0 1) ; "^" + (c-lang-const c-opt-cpp-symbol) ; usually # + (substring (c-lang-const c-stmt-delim-chars) 1)) ; ";{}?:" + (c-lang-const c-stmt-delim-chars)) + c++ (concat (substring (c-lang-const c-stmt-boundary-skip-chars) 0 1) ; "^" + "[" + (substring (c-lang-const c-stmt-boundary-skip-chars) 1))) ; ";{}?:" +(c-lang-defvar c-stmt-boundary-skip-chars + (c-lang-const c-stmt-boundary-skip-chars)) + +(c-lang-defconst c-stmt-boundary-skip-list + ;; The characters (apart from the initial ^) in `c-stmt-boundary-skip-chars' + ;; as a list of characters. + t (append (substring (c-lang-const c-stmt-boundary-skip-chars) 1) nil)) +(c-lang-defvar c-stmt-boundary-skip-list + (c-lang-const c-stmt-boundary-skip-list)) + (c-lang-defconst c-stmt-delim-chars-with-comma ;; Variant of `c-stmt-delim-chars' that additionally contains ','. t "^;,{}?:") (c-lang-defvar c-stmt-delim-chars-with-comma (c-lang-const c-stmt-delim-chars-with-comma)) +(c-lang-defconst c-stmt-boundary-skip-chars-with-comma + ;; Variant of `c-stmt-boundary-skip-chars' also containing ','. + t (if (c-lang-const c-opt-cpp-symbol) + (concat (substring (c-lang-const c-stmt-delim-chars-with-comma) 0 1) + (c-lang-const c-opt-cpp-symbol) ; usually # + (substring (c-lang-const c-stmt-delim-chars-with-comma) 1)) + (c-lang-const c-stmt-delim-chars-with-comma)) + c++ (concat + (substring (c-lang-const c-stmt-boundary-skip-chars-with-comma) 0 1) ; "^" + "[" + (substring (c-lang-const c-stmt-boundary-skip-chars-with-comma) 1))) ; ";,{}?:" +(c-lang-defvar c-stmt-boundary-skip-chars-with-comma + (c-lang-const c-stmt-boundary-skip-chars-with-comma)) + +(c-lang-defconst c-stmt-boundary-skip-list-with-comma + ;; Variant of `c-stmt-boundary-skip-list' also including a comma. + t (append (substring (c-lang-const c-stmt-boundary-skip-chars-with-comma) + 1) + nil)) +(c-lang-defvar c-stmt-boundary-skip-list-with-comma + (c-lang-const c-stmt-boundary-skip-list-with-comma)) + (c-lang-defconst c-pack-ops "Ops which signal C++11's \"parameter pack\"" t nil -- 2.39.2