Document reserved keys
[emacs.git] / lisp / progmodes / cc-engine.el
blob317968aafd3fac6aeba656b5721eb099d90b7be5
1 ;;; cc-engine.el --- core syntax guessing engine for CC mode -*- coding: utf-8 -*-
3 ;; Copyright (C) 1985, 1987, 1992-2018 Free Software Foundation, Inc.
5 ;; Authors: 2001- Alan Mackenzie
6 ;; 1998- Martin Stjernholm
7 ;; 1992-1999 Barry A. Warsaw
8 ;; 1987 Dave Detlefs
9 ;; 1987 Stewart Clamen
10 ;; 1985 Richard M. Stallman
11 ;; Maintainer: bug-cc-mode@gnu.org
12 ;; Created: 22-Apr-1997 (split from cc-mode.el)
13 ;; Keywords: c languages
14 ;; Package: cc-mode
16 ;; This file is part of GNU Emacs.
18 ;; GNU Emacs is free software: you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation, either version 3 of the License, or
21 ;; (at your option) any later version.
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
31 ;;; Commentary:
33 ;; The functions which have docstring documentation can be considered
34 ;; part of an API which other packages can use in CC Mode buffers.
35 ;; Otoh, undocumented functions and functions with the documentation
36 ;; in comments are considered purely internal and can change semantics
37 ;; or even disappear in the future.
39 ;; (This policy applies to CC Mode as a whole, not just this file. It
40 ;; probably also applies to many other Emacs packages, but here it's
41 ;; clearly spelled out.)
43 ;; Hidden buffer changes
45 ;; Various functions in CC Mode use text properties for caching and
46 ;; syntactic markup purposes, and those of them that might modify such
47 ;; properties but still don't modify the buffer in a visible way are
48 ;; said to do "hidden buffer changes". They should be used within
49 ;; `c-save-buffer-state' or a similar function that saves and restores
50 ;; buffer modifiedness, disables buffer change hooks, etc.
52 ;; Interactive functions are assumed to not do hidden buffer changes,
53 ;; except in the specific parts of them that do real changes.
55 ;; Lineup functions are assumed to do hidden buffer changes. They
56 ;; must not do real changes, though.
58 ;; All other functions that do hidden buffer changes have that noted
59 ;; in their doc string or comment.
61 ;; The intention with this system is to avoid wrapping every leaf
62 ;; function that do hidden buffer changes inside
63 ;; `c-save-buffer-state'. It should be used as near the top of the
64 ;; interactive functions as possible.
66 ;; Functions called during font locking are allowed to do hidden
67 ;; buffer changes since the font-lock package run them in a context
68 ;; similar to `c-save-buffer-state' (in fact, that function is heavily
69 ;; inspired by `save-buffer-state' in the font-lock package).
71 ;; Use of text properties
73 ;; CC Mode uses several text properties internally to mark up various
74 ;; positions, e.g. to improve speed and to eliminate glitches in
75 ;; interactive refontification.
77 ;; Note: This doc is for internal use only. Other packages should not
78 ;; assume that these text properties are used as described here.
80 ;; 'category
81 ;; Used for "indirection". With its help, some other property can
82 ;; be cheaply and easily switched on or off everywhere it occurs.
84 ;; 'syntax-table
85 ;; Used to modify the syntax of some characters. It is used to
86 ;; mark the "<" and ">" of angle bracket parens with paren syntax, to
87 ;; "hide" obtrusive characters in preprocessor lines, and to mark C++
88 ;; raw strings to enable their fontification.
90 ;; This property is used on single characters and is therefore
91 ;; always treated as front and rear nonsticky (or start and end open
92 ;; in XEmacs vocabulary). It's therefore installed on
93 ;; `text-property-default-nonsticky' if that variable exists (Emacs
94 ;; >= 21).
96 ;; 'c-is-sws and 'c-in-sws
97 ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
98 ;; speed them up. See the comment blurb before `c-put-is-sws'
99 ;; below for further details.
101 ;; 'c-type
102 ;; This property is used on single characters to mark positions with
103 ;; special syntactic relevance of various sorts. Its primary use is
104 ;; to avoid glitches when multiline constructs are refontified
105 ;; interactively (on font lock decoration level 3). It's cleared in
106 ;; a region before it's fontified and is then put on relevant chars
107 ;; in that region as they are encountered during the fontification.
108 ;; The value specifies the kind of position:
110 ;; 'c-decl-arg-start
111 ;; Put on the last char of the token preceding each declaration
112 ;; inside a declaration style arglist (typically in a function
113 ;; prototype).
115 ;; 'c-decl-end
116 ;; Put on the last char of the token preceding a declaration.
117 ;; This is used in cases where declaration boundaries can't be
118 ;; recognized simply by looking for a token like ";" or "}".
119 ;; `c-type-decl-end-used' must be set if this is used (see also
120 ;; `c-find-decl-spots').
122 ;; 'c-<>-arg-sep
123 ;; Put on the commas that separate arguments in angle bracket
124 ;; arglists like C++ template arglists.
126 ;; 'c-decl-id-start and 'c-decl-type-start
127 ;; Put on the last char of the token preceding each declarator
128 ;; in the declarator list of a declaration. They are also used
129 ;; between the identifiers cases like enum declarations.
130 ;; 'c-decl-type-start is used when the declarators are types,
131 ;; 'c-decl-id-start otherwise.
133 ;; 'c-not-decl
134 ;; Put on the brace which introduces a brace list and on the commas
135 ;; which separate the elements within it.
137 ;; 'c-awk-NL-prop
138 ;; Used in AWK mode to mark the various kinds of newlines. See
139 ;; cc-awk.el.
141 ;;; Code:
143 (eval-when-compile
144 (let ((load-path
145 (if (and (boundp 'byte-compile-dest-file)
146 (stringp byte-compile-dest-file))
147 (cons (file-name-directory byte-compile-dest-file) load-path)
148 load-path)))
149 (load "cc-bytecomp" nil t)))
151 (cc-require 'cc-defs)
152 (cc-require-when-compile 'cc-langs)
153 (cc-require 'cc-vars)
155 (eval-when-compile (require 'cl))
158 ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
160 (defmacro c-declare-lang-variables ()
161 `(progn
162 ,@(c--mapcan (lambda (init)
163 `(,(if (elt init 2)
164 `(defvar ,(car init) nil ,(elt init 2))
165 `(defvar ,(car init) nil))
166 (make-variable-buffer-local ',(car init))))
167 (cdr c-lang-variable-inits))))
168 (c-declare-lang-variables)
171 ;;; Internal state variables.
173 ;; Internal state of hungry delete key feature
174 (defvar c-hungry-delete-key nil)
175 (make-variable-buffer-local 'c-hungry-delete-key)
177 ;; The electric flag (toggled by `c-toggle-electric-state').
178 ;; If t, electric actions (like automatic reindentation, and (if
179 ;; c-auto-newline is also set) auto newlining) will happen when an electric
180 ;; key like `{' is pressed (or an electric keyword like `else').
181 (defvar c-electric-flag t)
182 (make-variable-buffer-local 'c-electric-flag)
184 ;; Internal state of auto newline feature.
185 (defvar c-auto-newline nil)
186 (make-variable-buffer-local 'c-auto-newline)
188 ;; Included in the mode line to indicate the active submodes.
189 ;; (defvar c-submode-indicators nil)
190 ;; (make-variable-buffer-local 'c-submode-indicators)
192 (defun c-calculate-state (arg prevstate)
193 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
194 ;; arg is nil or zero, toggle the state. If arg is negative, turn
195 ;; the state off, and if arg is positive, turn the state on
196 (if (or (not arg)
197 (zerop (setq arg (prefix-numeric-value arg))))
198 (not prevstate)
199 (> arg 0)))
202 ;; Basic handling of preprocessor directives.
204 ;; This is a dynamically bound cache used together with
205 ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
206 ;; works as long as point doesn't cross a macro boundary.
207 (defvar c-macro-start 'unknown)
209 (defsubst c-query-and-set-macro-start ()
210 (if (symbolp c-macro-start)
211 (setq c-macro-start (save-excursion
212 (c-save-buffer-state ()
213 (and (c-beginning-of-macro)
214 (point)))))
215 c-macro-start))
217 (defsubst c-query-macro-start ()
218 (if (symbolp c-macro-start)
219 (save-excursion
220 (c-save-buffer-state ()
221 (and (c-beginning-of-macro)
222 (point))))
223 c-macro-start))
225 ;; One element macro cache to cope with continual movement within very large
226 ;; CPP macros.
227 (defvar c-macro-cache nil)
228 (make-variable-buffer-local 'c-macro-cache)
229 ;; Nil or cons of the bounds of the most recent CPP form probed by
230 ;; `c-beginning-of-macro', `c-end-of-macro' or `c-syntactic-end-of-macro'.
231 ;; The cdr will be nil if we know only the start of the CPP form.
232 (defvar c-macro-cache-start-pos nil)
233 (make-variable-buffer-local 'c-macro-cache-start-pos)
234 ;; The starting position from where we determined `c-macro-cache'.
235 (defvar c-macro-cache-syntactic nil)
236 (make-variable-buffer-local 'c-macro-cache-syntactic)
237 ;; Either nil, or the syntactic end of the macro currently represented by
238 ;; `c-macro-cache'.
239 (defvar c-macro-cache-no-comment nil)
240 (make-variable-buffer-local 'c-macro-cache-no-comment)
241 ;; Either nil, or the position of a comment which is open at the end of the
242 ;; macro represented by `c-macro-cache'.
244 (defun c-invalidate-macro-cache (beg _end)
245 ;; Called from a before-change function. If the change region is before or
246 ;; in the macro characterized by `c-macro-cache' etc., nullify it
247 ;; appropriately. BEG and END are the standard before-change-functions
248 ;; parameters. END isn't used.
249 (cond
250 ((null c-macro-cache))
251 ((<= beg (car c-macro-cache))
252 (setq c-macro-cache nil
253 c-macro-cache-start-pos nil
254 c-macro-cache-syntactic nil
255 c-macro-cache-no-comment nil))
256 ((and (cdr c-macro-cache)
257 (< beg (cdr c-macro-cache)))
258 (setcdr c-macro-cache nil)
259 (setq c-macro-cache-start-pos beg
260 c-macro-cache-syntactic nil
261 c-macro-cache-no-comment nil))))
263 (defun c-macro-is-genuine-p ()
264 ;; Check that the ostensible CPP construct at point is a real one. In
265 ;; particular, if point is on the first line of a narrowed buffer, make sure
266 ;; that the "#" isn't, say, the second character of a "##" operator. Return
267 ;; t when the macro is real, nil otherwise.
268 (let ((here (point)))
269 (beginning-of-line)
270 (prog1
271 (if (and (eq (point) (point-min))
272 (/= (point) 1))
273 (save-restriction
274 (widen)
275 (beginning-of-line)
276 (and (looking-at c-anchored-cpp-prefix)
277 (eq (match-beginning 1) here)))
279 (goto-char here))))
281 (defun c-beginning-of-macro (&optional lim)
282 "Go to the beginning of a preprocessor directive.
283 Leave point at the beginning of the directive and return t if in one,
284 otherwise return nil and leave point unchanged.
286 Note that this function might do hidden buffer changes. See the
287 comment at the start of cc-engine.el for more info."
288 (let ((here (point)))
289 (when c-opt-cpp-prefix
290 (if (and (car c-macro-cache)
291 (>= (point) (car c-macro-cache))
292 (or (and (cdr c-macro-cache)
293 (<= (point) (cdr c-macro-cache)))
294 (<= (point) c-macro-cache-start-pos)))
295 (unless (< (car c-macro-cache) (or lim (point-min)))
296 (progn (goto-char (max (or lim (point-min)) (car c-macro-cache)))
297 (setq c-macro-cache-start-pos
298 (max c-macro-cache-start-pos here))
300 (setq c-macro-cache nil
301 c-macro-cache-start-pos nil
302 c-macro-cache-syntactic nil
303 c-macro-cache-no-comment nil)
305 (save-restriction
306 (if lim (narrow-to-region lim (point-max)))
307 (beginning-of-line)
308 (while (eq (char-before (1- (point))) ?\\)
309 (forward-line -1))
310 (back-to-indentation)
311 (if (and (<= (point) here)
312 (save-match-data (looking-at c-opt-cpp-start))
313 (c-macro-is-genuine-p))
314 (progn
315 (setq c-macro-cache (cons (point) nil)
316 c-macro-cache-start-pos here
317 c-macro-cache-syntactic nil)
319 (goto-char here)
320 nil))))))
322 (defun c-end-of-macro (&optional lim)
323 "Go to the end of a preprocessor directive.
324 More accurately, move the point to the end of the closest following
325 line that doesn't end with a line continuation backslash - no check is
326 done that the point is inside a cpp directive to begin with.
328 If LIM is provided, it is a limit position at which point is left
329 if the end of the macro doesn't occur earlier.
331 Note that this function might do hidden buffer changes. See the
332 comment at the start of cc-engine.el for more info."
333 (save-restriction
334 (if lim (narrow-to-region (point-min) lim))
335 (if (and (cdr c-macro-cache)
336 (<= (point) (cdr c-macro-cache))
337 (>= (point) (car c-macro-cache)))
338 (goto-char (cdr c-macro-cache))
339 (unless (and (car c-macro-cache)
340 (<= (point) c-macro-cache-start-pos)
341 (>= (point) (car c-macro-cache)))
342 (setq c-macro-cache nil
343 c-macro-cache-start-pos nil
344 c-macro-cache-syntactic nil
345 c-macro-cache-no-comment nil))
346 (while (progn
347 (end-of-line)
348 (when (and (eq (char-before) ?\\)
349 (not (eobp)))
350 (forward-char)
351 t)))
352 (when (and (car c-macro-cache)
353 (bolp)
354 (not (eq (char-before (1- (point))) ?\\)))
355 (setcdr c-macro-cache (point))
356 (setq c-macro-cache-syntactic nil)))))
358 (defun c-syntactic-end-of-macro ()
359 ;; Go to the end of a CPP directive, or a "safe" pos just before.
361 ;; This is normally the end of the next non-escaped line. A "safe"
362 ;; position is one not within a string or comment. (The EOL on a line
363 ;; comment is NOT "safe").
365 ;; This function must only be called from the beginning of a CPP construct.
367 ;; Note that this function might do hidden buffer changes. See the comment
368 ;; at the start of cc-engine.el for more info.
369 (let* ((here (point))
370 (there (progn (c-end-of-macro) (point)))
372 (if c-macro-cache-syntactic
373 (goto-char c-macro-cache-syntactic)
374 (setq s (parse-partial-sexp here there))
375 (while (and (or (nth 3 s) ; in a string
376 (and (nth 4 s) ; in a comment (maybe at end of line comment)
377 (not (eq (nth 7 s) 'syntax-table)))) ; Not a pseudo comment
378 (> there here)) ; No infinite loops, please.
379 (setq there (1- (nth 8 s)))
380 (setq s (parse-partial-sexp here there)))
381 (setq c-macro-cache-syntactic (point)))
382 (point)))
384 (defun c-no-comment-end-of-macro ()
385 ;; Go to the start of the comment which is open at the end of the current
386 ;; CPP directive, or to the end of that directive. For this purpose, open
387 ;; strings are ignored.
389 ;; This function must only be called from the beginning of a CPP construct.
391 ;; Note that this function might do hidden buffer changes. See the comment
392 ;; at the start of cc-engine.el for more info.
393 (let* ((here (point))
394 (there (progn (c-end-of-macro) (point)))
396 (if c-macro-cache-no-comment
397 (goto-char c-macro-cache-no-comment)
398 (setq s (parse-partial-sexp here there))
399 (while (and (nth 3 s) ; in a string
400 (> there here)) ; No infinite loops, please.
401 (setq here (1+ (nth 8 s)))
402 (setq s (parse-partial-sexp here there)))
403 (when (and (nth 4 s)
404 (not (eq (nth 7 s) 'syntax-table))) ; no pseudo comments.
405 (goto-char (nth 8 s)))
406 (setq c-macro-cache-no-comment (point)))
407 (point)))
409 (defun c-forward-over-cpp-define-id ()
410 ;; Assuming point is at the "#" that introduces a preprocessor
411 ;; directive, it's moved forward to the end of the identifier which is
412 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
413 ;; is returned in this case, in all other cases nil is returned and
414 ;; point isn't moved.
416 ;; This function might do hidden buffer changes.
417 (when (and c-opt-cpp-macro-define-id
418 (looking-at c-opt-cpp-macro-define-id))
419 (goto-char (match-end 0))))
421 (defun c-forward-to-cpp-define-body ()
422 ;; Assuming point is at the "#" that introduces a preprocessor
423 ;; directive, it's moved forward to the start of the definition body
424 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
425 ;; specifies). Non-nil is returned in this case, in all other cases
426 ;; nil is returned and point isn't moved.
428 ;; This function might do hidden buffer changes.
429 (when (and c-opt-cpp-macro-define-start
430 (looking-at c-opt-cpp-macro-define-start)
431 (not (= (match-end 0) (c-point 'eol))))
432 (goto-char (match-end 0))))
435 ;;; Basic utility functions.
437 (defun c-delq-from-dotted-list (elt dlist)
438 ;; If ELT is a member of the (possibly dotted) list DLIST, remove all
439 ;; occurrences of it (except for any in the last cdr of DLIST).
441 ;; Call this as (setq DLIST (c-delq-from-dotted-list ELT DLIST)), as
442 ;; sometimes the original structure is changed, sometimes it's not.
444 ;; This function is needed in Emacs < 24.5, and possibly XEmacs, because
445 ;; `delq' throws an error in these versions when given a dotted list.
446 (let ((tail dlist) prev)
447 (while (consp tail)
448 (if (eq (car tail) elt)
449 (if prev
450 (setcdr prev (cdr tail))
451 (setq dlist (cdr dlist)))
452 (setq prev tail))
453 (setq tail (cdr tail)))
454 dlist))
456 (defun c-syntactic-content (from to paren-level)
457 ;; Return the given region as a string where all syntactic
458 ;; whitespace is removed or, where necessary, replaced with a single
459 ;; space. If PAREN-LEVEL is given then all parens in the region are
460 ;; collapsed to "()", "[]" etc.
462 ;; This function might do hidden buffer changes.
464 (save-excursion
465 (save-restriction
466 (narrow-to-region from to)
467 (goto-char from)
468 (let* ((parts (list nil)) (tail parts) pos in-paren)
470 (while (re-search-forward c-syntactic-ws-start to t)
471 (goto-char (setq pos (match-beginning 0)))
472 (c-forward-syntactic-ws)
473 (if (= (point) pos)
474 (forward-char)
476 (when paren-level
477 (save-excursion
478 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
479 pos (point))))
481 (if (and (> pos from)
482 (< (point) to)
483 (looking-at "\\w\\|\\s_")
484 (save-excursion
485 (goto-char (1- pos))
486 (looking-at "\\w\\|\\s_")))
487 (progn
488 (setcdr tail (list (buffer-substring-no-properties from pos)
489 " "))
490 (setq tail (cddr tail)))
491 (setcdr tail (list (buffer-substring-no-properties from pos)))
492 (setq tail (cdr tail)))
494 (when in-paren
495 (when (= (car (parse-partial-sexp pos to -1)) -1)
496 (setcdr tail (list (buffer-substring-no-properties
497 (1- (point)) (point))))
498 (setq tail (cdr tail))))
500 (setq from (point))))
502 (setcdr tail (list (buffer-substring-no-properties from to)))
503 (apply 'concat (cdr parts))))))
505 (defun c-shift-line-indentation (shift-amt)
506 ;; Shift the indentation of the current line with the specified
507 ;; amount (positive inwards). The buffer is modified only if
508 ;; SHIFT-AMT isn't equal to zero.
509 (let ((pos (- (point-max) (point)))
510 (c-macro-start c-macro-start)
511 tmp-char-inserted)
512 (if (zerop shift-amt)
514 ;; If we're on an empty line inside a macro, we take the point
515 ;; to be at the current indentation and shift it to the
516 ;; appropriate column. This way we don't treat the extra
517 ;; whitespace out to the line continuation as indentation.
518 (when (and (c-query-and-set-macro-start)
519 (looking-at "[ \t]*\\\\$")
520 (save-excursion
521 (skip-chars-backward " \t")
522 (bolp)))
523 (insert ?x)
524 (backward-char)
525 (setq tmp-char-inserted t))
526 (unwind-protect
527 (let ((col (current-indentation)))
528 (delete-region (c-point 'bol) (c-point 'boi))
529 (beginning-of-line)
530 (indent-to (+ col shift-amt)))
531 (when tmp-char-inserted
532 (delete-char 1))))
533 ;; If initial point was within line's indentation and we're not on
534 ;; a line with a line continuation in a macro, position after the
535 ;; indentation. Else stay at same point in text.
536 (if (and (< (point) (c-point 'boi))
537 (not tmp-char-inserted))
538 (back-to-indentation)
539 (if (> (- (point-max) pos) (point))
540 (goto-char (- (point-max) pos))))))
542 (defsubst c-keyword-sym (keyword)
543 ;; Return non-nil if the string KEYWORD is a known keyword. More
544 ;; precisely, the value is the symbol for the keyword in
545 ;; `c-keywords-obarray'.
546 (intern-soft keyword c-keywords-obarray))
548 (defsubst c-keyword-member (keyword-sym lang-constant)
549 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
550 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
551 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
552 ;; nil then the result is nil.
553 (get keyword-sym lang-constant))
555 ;; String syntax chars, suitable for skip-syntax-(forward|backward).
556 (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
557 "\"|"
558 "\""))
560 ;; Regexp matching string limit syntax.
561 (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
562 "\\s\"\\|\\s|"
563 "\\s\""))
565 ;; Regexp matching WS followed by string limit syntax.
566 (defconst c-ws*-string-limit-regexp
567 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
569 ;; Holds formatted error strings for the few cases where parse errors
570 ;; are reported.
571 (defvar c-parsing-error nil)
572 (make-variable-buffer-local 'c-parsing-error)
574 (defun c-echo-parsing-error (&optional quiet)
575 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
576 (c-benign-error "%s" c-parsing-error))
577 c-parsing-error)
579 ;; Faces given to comments and string literals. This is used in some
580 ;; situations to speed up recognition; it isn't mandatory that font
581 ;; locking is in use. This variable is extended with the face in
582 ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
583 (defvar c-literal-faces
584 (append '(font-lock-comment-face font-lock-string-face)
585 (when (facep 'font-lock-comment-delimiter-face)
586 ;; New in Emacs 22.
587 '(font-lock-comment-delimiter-face))))
589 (defsubst c-put-c-type-property (pos value)
590 ;; Put a c-type property with the given value at POS.
591 (c-put-char-property pos 'c-type value))
593 (defun c-clear-c-type-property (from to value)
594 ;; Remove all occurrences of the c-type property that has the given
595 ;; value in the region between FROM and TO. VALUE is assumed to not
596 ;; be nil.
598 ;; Note: This assumes that c-type is put on single chars only; it's
599 ;; very inefficient if matching properties cover large regions.
600 (save-excursion
601 (goto-char from)
602 (while (progn
603 (when (eq (get-text-property (point) 'c-type) value)
604 (c-clear-char-property (point) 'c-type))
605 (goto-char (c-next-single-property-change (point) 'c-type nil to))
606 (< (point) to)))))
609 ;; Some debug tools to visualize various special positions. This
610 ;; debug code isn't as portable as the rest of CC Mode.
612 (cc-bytecomp-defun overlays-in)
613 (cc-bytecomp-defun overlay-get)
614 (cc-bytecomp-defun overlay-start)
615 (cc-bytecomp-defun overlay-end)
616 (cc-bytecomp-defun delete-overlay)
617 (cc-bytecomp-defun overlay-put)
618 (cc-bytecomp-defun make-overlay)
620 (defun c-debug-add-face (beg end face)
621 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
622 (while overlays
623 (setq overlay (car overlays)
624 overlays (cdr overlays))
625 (when (eq (overlay-get overlay 'face) face)
626 (setq beg (min beg (overlay-start overlay))
627 end (max end (overlay-end overlay)))
628 (delete-overlay overlay)))
629 (overlay-put (make-overlay beg end) 'face face)))
631 (defun c-debug-remove-face (beg end face)
632 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
633 (ol-beg beg) (ol-end end))
634 (while overlays
635 (setq overlay (car overlays)
636 overlays (cdr overlays))
637 (when (eq (overlay-get overlay 'face) face)
638 (setq ol-beg (min ol-beg (overlay-start overlay))
639 ol-end (max ol-end (overlay-end overlay)))
640 (delete-overlay overlay)))
641 (when (< ol-beg beg)
642 (overlay-put (make-overlay ol-beg beg) 'face face))
643 (when (> ol-end end)
644 (overlay-put (make-overlay end ol-end) 'face face))))
647 ;; `c-beginning-of-statement-1' and accompanying stuff.
649 ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
650 ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
651 ;; better way should be implemented, but this will at least shut up
652 ;; the byte compiler.
653 (defvar c-maybe-labelp)
655 ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
657 ;; Macros used internally in c-beginning-of-statement-1 for the
658 ;; automaton actions.
659 (defmacro c-bos-push-state ()
660 '(setq stack (cons (cons state saved-pos)
661 stack)))
662 (defmacro c-bos-pop-state (&optional do-if-done)
663 `(if (setq state (car (car stack))
664 saved-pos (cdr (car stack))
665 stack (cdr stack))
667 ,do-if-done
668 (throw 'loop nil)))
669 (defmacro c-bos-pop-state-and-retry ()
670 '(throw 'loop (setq state (car (car stack))
671 saved-pos (cdr (car stack))
672 ;; Throw nil if stack is empty, else throw non-nil.
673 stack (cdr stack))))
674 (defmacro c-bos-save-pos ()
675 '(setq saved-pos (vector pos tok ptok pptok)))
676 (defmacro c-bos-restore-pos ()
677 '(unless (eq (elt saved-pos 0) start)
678 (setq pos (elt saved-pos 0)
679 tok (elt saved-pos 1)
680 ptok (elt saved-pos 2)
681 pptok (elt saved-pos 3))
682 (goto-char pos)
683 (setq sym nil)))
684 (defmacro c-bos-save-error-info (missing got)
685 `(setq saved-pos (vector pos ,missing ,got)))
686 (defmacro c-bos-report-error ()
687 '(unless noerror
688 (setq c-parsing-error
689 (format-message
690 "No matching `%s' found for `%s' on line %d"
691 (elt saved-pos 1)
692 (elt saved-pos 2)
693 (1+ (count-lines (point-min)
694 (c-point 'bol (elt saved-pos 0))))))))
696 (defun c-beginning-of-statement-1 (&optional lim ignore-labels
697 noerror comma-delim)
698 "Move to the start of the current statement or declaration, or to
699 the previous one if already at the beginning of one. Only
700 statements/declarations on the same level are considered, i.e. don't
701 move into or out of sexps (not even normal expression parentheses).
703 If point is already at the earliest statement within braces or parens,
704 this function doesn't move back into any whitespace preceding it; it
705 returns `same' in this case.
707 Stop at statement continuation tokens like \"else\", \"catch\",
708 \"finally\" and the \"while\" in \"do ... while\" if the start point
709 is within the continuation. If starting at such a token, move to the
710 corresponding statement start. If at the beginning of a statement,
711 move to the closest containing statement if there is any. This might
712 also stop at a continuation clause.
714 Labels are treated as part of the following statements if
715 IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
716 statement start keyword.) Otherwise, each label is treated as a
717 separate statement.
719 Macros are ignored \(i.e. skipped over) unless point is within one, in
720 which case the content of the macro is treated as normal code. Aside
721 from any normal statement starts found in it, stop at the first token
722 of the content in the macro, i.e. the expression of an \"#if\" or the
723 start of the definition in a \"#define\". Also stop at start of
724 macros before leaving them.
726 Return:
727 `label' if stopped at a label or \"case...:\" or \"default:\";
728 `same' if stopped at the beginning of the current statement;
729 `up' if stepped to a containing statement;
730 `previous' if stepped to a preceding statement;
731 `beginning' if stepped from a statement continuation clause to
732 its start clause; or
733 `macro' if stepped to a macro start.
734 Note that `same' and not `label' is returned if stopped at the same
735 label without crossing the colon character.
737 LIM may be given to limit the search. If the search hits the limit,
738 point will be left at the closest following token, or at the start
739 position if that is less (`same' is returned in this case).
741 NOERROR turns off error logging to `c-parsing-error'.
743 Normally only `;' and virtual semicolons are considered to delimit
744 statements, but if COMMA-DELIM is non-nil then `,' is treated
745 as a delimiter too.
747 Note that this function might do hidden buffer changes. See the
748 comment at the start of cc-engine.el for more info."
750 ;; The bulk of this function is a pushdown automaton that looks at statement
751 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
752 ;; purpose is to keep track of nested statements, ensuring that such
753 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
754 ;; does with nested braces/brackets/parentheses).
756 ;; Note: The position of a boundary is the following token.
758 ;; Beginning with the current token (the one following point), move back one
759 ;; sexp at a time (where a sexp is, more or less, either a token or the
760 ;; entire contents of a brace/bracket/paren pair). Each time a statement
761 ;; boundary is crossed or a "while"-like token is found, update the state of
762 ;; the PDA. Stop at the beginning of a statement when the stack (holding
763 ;; nested statement info) is empty and the position has been moved.
765 ;; The following variables constitute the PDA:
767 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
768 ;; scanned back over, 'boundary if we've just gone back over a
769 ;; statement boundary, or nil otherwise.
770 ;; state: takes one of the values (nil else else-boundary while
771 ;; while-boundary catch catch-boundary).
772 ;; nil means "no "while"-like token yet scanned".
773 ;; 'else, for example, means "just gone back over an else".
774 ;; 'else-boundary means "just gone back over a statement boundary
775 ;; immediately after having gone back over an else".
776 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
777 ;; of error reporting information.
778 ;; stack: The stack onto which the PDA pushes its state. Each entry
779 ;; consists of a saved value of state and saved-pos. An entry is
780 ;; pushed when we move back over a "continuation" token (e.g. else)
781 ;; and popped when we encounter the corresponding opening token
782 ;; (e.g. if).
785 ;; The following diagram briefly outlines the PDA.
787 ;; Common state:
788 ;; "else": Push state, goto state `else'.
789 ;; "while": Push state, goto state `while'.
790 ;; "catch" or "finally": Push state, goto state `catch'.
791 ;; boundary: Pop state.
792 ;; other: Do nothing special.
794 ;; State `else':
795 ;; boundary: Goto state `else-boundary'.
796 ;; other: Error, pop state, retry token.
798 ;; State `else-boundary':
799 ;; "if": Pop state.
800 ;; boundary: Error, pop state.
801 ;; other: See common state.
803 ;; State `while':
804 ;; boundary: Save position, goto state `while-boundary'.
805 ;; other: Pop state, retry token.
807 ;; State `while-boundary':
808 ;; "do": Pop state.
809 ;; boundary: Restore position if it's not at start, pop state. [*see below]
810 ;; other: See common state.
812 ;; State `catch':
813 ;; boundary: Goto state `catch-boundary'.
814 ;; other: Error, pop state, retry token.
816 ;; State `catch-boundary':
817 ;; "try": Pop state.
818 ;; "catch": Goto state `catch'.
819 ;; boundary: Error, pop state.
820 ;; other: See common state.
822 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
823 ;; searching for a "do" which would have opened a do-while. If we didn't
824 ;; find it, we discard the analysis done since the "while", go back to this
825 ;; token in the buffer and restart the scanning there, this time WITHOUT
826 ;; pushing the 'while state onto the stack.
828 ;; In addition to the above there is some special handling of labels
829 ;; and macros.
831 (let ((case-fold-search nil)
832 (start (point))
833 macro-start
834 (delims (if comma-delim '(?\; ?,) '(?\;)))
835 (c-stmt-delim-chars (if comma-delim
836 c-stmt-delim-chars-with-comma
837 c-stmt-delim-chars))
838 c-maybe-labelp after-case:-pos saved
839 ;; Current position.
841 ;; Position of last stmt boundary character (e.g. ;).
842 boundary-pos
843 ;; The position of the last sexp or bound that follows the
844 ;; first found colon, i.e. the start of the nonlabel part of
845 ;; the statement. It's `start' if a colon is found just after
846 ;; the start.
847 after-labels-pos
848 ;; Like `after-labels-pos', but the first such position inside
849 ;; a label, i.e. the start of the last label before the start
850 ;; of the nonlabel part of the statement.
851 last-label-pos
852 ;; The last position where a label is possible provided the
853 ;; statement started there. It's nil as long as no invalid
854 ;; label content has been found (according to
855 ;; `c-nonlabel-token-key'). It's `start' if no valid label
856 ;; content was found in the label. Note that we might still
857 ;; regard it a label if it starts with `c-label-kwds'.
858 label-good-pos
859 ;; Putative positions of the components of a bitfield declaration,
860 ;; e.g. "int foo : NUM_FOO_BITS ;"
861 bitfield-type-pos bitfield-id-pos bitfield-size-pos
862 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
863 ;; See above.
865 ;; Current state in the automaton. See above.
866 state
867 ;; Current saved positions. See above.
868 saved-pos
869 ;; Stack of conses (state . saved-pos).
870 stack
871 ;; Regexp which matches "for", "if", etc.
872 (cond-key (or c-opt-block-stmt-key
873 "\\<\\>")) ; Matches nothing.
874 ;; Return value.
875 (ret 'same)
876 ;; Positions of the last three sexps or bounds we've stopped at.
877 tok ptok pptok)
879 (save-restriction
880 (if lim (narrow-to-region lim (point-max)))
882 (if (save-excursion
883 (and (c-beginning-of-macro)
884 (/= (point) start)))
885 (setq macro-start (point)))
887 ;; Try to skip back over unary operator characters, to register
888 ;; that we've moved.
889 (while (progn
890 (setq pos (point))
891 (c-backward-syntactic-ws)
892 ;; Protect post-++/-- operators just before a virtual semicolon.
893 (and (not (c-at-vsemi-p))
894 (/= (skip-chars-backward "-+!*&~@`#") 0))))
896 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
897 ;; done. Later on we ignore the boundaries for statements that don't
898 ;; contain any sexp. The only thing that is affected is that the error
899 ;; checking is a little less strict, and we really don't bother.
900 (if (and (memq (char-before) delims)
901 (progn (forward-char -1)
902 (setq saved (point))
903 (c-backward-syntactic-ws)
904 (or (memq (char-before) delims)
905 (memq (char-before) '(?: nil))
906 (eq (char-syntax (char-before)) ?\()
907 (c-at-vsemi-p))))
908 (setq ret 'previous
909 pos saved)
911 ;; Begin at start and not pos to detect macros if we stand
912 ;; directly after the #.
913 (goto-char start)
914 (if (looking-at "\\<\\|\\W")
915 ;; Record this as the first token if not starting inside it.
916 (setq tok start))
918 ;; The following while loop goes back one sexp (balanced parens,
919 ;; etc. with contents, or symbol or suchlike) each iteration. This
920 ;; movement is accomplished with a call to c-backward-sexp approx 170
921 ;; lines below.
923 ;; The loop is exited only by throwing nil to the (catch 'loop ...):
924 ;; 1. On reaching the start of a macro;
925 ;; 2. On having passed a stmt boundary with the PDA stack empty;
926 ;; 3. On reaching the start of an Objective C method def;
927 ;; 4. From macro `c-bos-pop-state'; when the stack is empty;
928 ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty.
929 (while
930 (catch 'loop ;; Throw nil to break, non-nil to continue.
931 (cond
932 ;; Are we in a macro, just after the opening #?
933 ((save-excursion
934 (and macro-start ; Always NIL for AWK.
935 (progn (skip-chars-backward " \t")
936 (eq (char-before) ?#))
937 (progn (setq saved (1- (point)))
938 (beginning-of-line)
939 (not (eq (char-before (1- (point))) ?\\)))
940 (looking-at c-opt-cpp-start)
941 (progn (skip-chars-forward " \t")
942 (eq (point) saved))))
943 (goto-char saved)
944 (if (and (c-forward-to-cpp-define-body)
945 (progn (c-forward-syntactic-ws start)
946 (< (point) start)))
947 ;; Stop at the first token in the content of the macro.
948 (setq pos (point)
949 ignore-labels t) ; Avoid the label check on exit.
950 (setq pos saved
951 ret 'macro
952 ignore-labels t))
953 (throw 'loop nil)) ; 1. Start of macro.
955 ;; Do a round through the automaton if we've just passed a
956 ;; statement boundary or passed a "while"-like token.
957 ((or sym
958 (and (looking-at cond-key)
959 (setq sym (intern (match-string 1)))))
961 (when (and (< pos start) (null stack))
962 (throw 'loop nil)) ; 2. Statement boundary.
964 ;; The PDA state handling.
966 ;; Refer to the description of the PDA in the opening
967 ;; comments. In the following OR form, the first leaf
968 ;; attempts to handles one of the specific actions detailed
969 ;; (e.g., finding token "if" whilst in state `else-boundary').
970 ;; We drop through to the second leaf (which handles common
971 ;; state) if no specific handler is found in the first cond.
972 ;; If a parsing error is detected (e.g. an "else" with no
973 ;; preceding "if"), we throw to the enclosing catch.
975 ;; Note that the (eq state 'else) means
976 ;; "we've just passed an else", NOT "we're looking for an
977 ;; else".
978 (or (cond
979 ((eq state 'else)
980 (if (eq sym 'boundary)
981 (setq state 'else-boundary)
982 (c-bos-report-error)
983 (c-bos-pop-state-and-retry)))
985 ((eq state 'else-boundary)
986 (cond ((eq sym 'if)
987 (c-bos-pop-state (setq ret 'beginning)))
988 ((eq sym 'boundary)
989 (c-bos-report-error)
990 (c-bos-pop-state))))
992 ((eq state 'while)
993 (if (and (eq sym 'boundary)
994 ;; Since this can cause backtracking we do a
995 ;; little more careful analysis to avoid it:
996 ;; If there's a label in front of the while
997 ;; it can't be part of a do-while.
998 (not after-labels-pos))
999 (progn (c-bos-save-pos)
1000 (setq state 'while-boundary))
1001 (c-bos-pop-state-and-retry))) ; Can't be a do-while
1003 ((eq state 'while-boundary)
1004 (cond ((eq sym 'do)
1005 (c-bos-pop-state (setq ret 'beginning)))
1006 ((eq sym 'boundary) ; isn't a do-while
1007 (c-bos-restore-pos) ; the position of the while
1008 (c-bos-pop-state)))) ; no longer searching for do.
1010 ((eq state 'catch)
1011 (if (eq sym 'boundary)
1012 (setq state 'catch-boundary)
1013 (c-bos-report-error)
1014 (c-bos-pop-state-and-retry)))
1016 ((eq state 'catch-boundary)
1017 (cond
1018 ((eq sym 'try)
1019 (c-bos-pop-state (setq ret 'beginning)))
1020 ((eq sym 'catch)
1021 (setq state 'catch))
1022 ((eq sym 'boundary)
1023 (c-bos-report-error)
1024 (c-bos-pop-state)))))
1026 ;; This is state common. We get here when the previous
1027 ;; cond statement found no particular state handler.
1028 (cond ((eq sym 'boundary)
1029 ;; If we have a boundary at the start
1030 ;; position we push a frame to go to the
1031 ;; previous statement.
1032 (if (>= pos start)
1033 (c-bos-push-state)
1034 (c-bos-pop-state)))
1035 ((eq sym 'else)
1036 (c-bos-push-state)
1037 (c-bos-save-error-info 'if 'else)
1038 (setq state 'else))
1039 ((eq sym 'while)
1040 ;; Is this a real while, or a do-while?
1041 ;; The next `when' triggers unless we are SURE that
1042 ;; the `while' is not the tail end of a `do-while'.
1043 (when (or (not pptok)
1044 (memq (char-after pptok) delims)
1045 ;; The following kludge is to prevent
1046 ;; infinite recursion when called from
1047 ;; c-awk-after-if-for-while-condition-p,
1048 ;; or the like.
1049 (and (eq (point) start)
1050 (c-vsemi-status-unknown-p))
1051 (c-at-vsemi-p pptok))
1052 ;; Since this can cause backtracking we do a
1053 ;; little more careful analysis to avoid it: If
1054 ;; the while isn't followed by a (possibly
1055 ;; virtual) semicolon it can't be a do-while.
1056 (c-bos-push-state)
1057 (setq state 'while)))
1058 ((memq sym '(catch finally))
1059 (c-bos-push-state)
1060 (c-bos-save-error-info 'try sym)
1061 (setq state 'catch))))
1063 (when c-maybe-labelp
1064 ;; We're either past a statement boundary or at the
1065 ;; start of a statement, so throw away any label data
1066 ;; for the previous one.
1067 (setq after-labels-pos nil
1068 last-label-pos nil
1069 c-maybe-labelp nil))))
1071 ;; Step to the previous sexp, but not if we crossed a
1072 ;; boundary, since that doesn't consume an sexp.
1073 (if (eq sym 'boundary)
1074 (setq ret 'previous)
1076 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
1077 ;; BACKWARDS THROUGH THE SOURCE.
1079 (c-backward-syntactic-ws)
1080 (let ((before-sws-pos (point))
1081 ;; The end position of the area to search for statement
1082 ;; barriers in this round.
1083 (maybe-after-boundary-pos pos))
1085 ;; Go back over exactly one logical sexp, taking proper
1086 ;; account of macros and escaped EOLs.
1087 (while
1088 (progn
1089 (unless (c-safe (c-backward-sexp) t)
1090 ;; Give up if we hit an unbalanced block. Since the
1091 ;; stack won't be empty the code below will report a
1092 ;; suitable error.
1093 (throw 'loop nil))
1094 (cond
1095 ;; Have we moved into a macro?
1096 ((and (not macro-start)
1097 (c-beginning-of-macro))
1098 ;; Have we crossed a statement boundary? If not,
1099 ;; keep going back until we find one or a "real" sexp.
1100 (and
1101 (save-excursion
1102 (c-end-of-macro)
1103 (not (c-crosses-statement-barrier-p
1104 (point) maybe-after-boundary-pos)))
1105 (setq maybe-after-boundary-pos (point))))
1106 ;; Have we just gone back over an escaped NL? This
1107 ;; doesn't count as a sexp.
1108 ((looking-at "\\\\$")))))
1110 ;; Have we crossed a statement boundary?
1111 (setq boundary-pos
1112 (cond
1113 ;; Are we at a macro beginning?
1114 ((and (not macro-start)
1115 c-opt-cpp-prefix
1116 (looking-at c-opt-cpp-prefix))
1117 (save-excursion
1118 (c-end-of-macro)
1119 (c-crosses-statement-barrier-p
1120 (point) maybe-after-boundary-pos)))
1121 ;; Just gone back over a brace block?
1122 ((and
1123 (eq (char-after) ?{)
1124 (not (c-looking-at-inexpr-block lim nil t))
1125 (save-excursion
1126 (c-backward-token-2 1 t nil)
1127 (not (looking-at "=\\([^=]\\|$\\)"))))
1128 (save-excursion
1129 (c-forward-sexp) (point)))
1130 ;; Just gone back over some paren block?
1131 ((looking-at "\\s(")
1132 (save-excursion
1133 (goto-char (1+ (c-down-list-backward
1134 before-sws-pos)))
1135 (c-crosses-statement-barrier-p
1136 (point) maybe-after-boundary-pos)))
1137 ;; Just gone back over an ordinary symbol of some sort?
1138 (t (c-crosses-statement-barrier-p
1139 (point) maybe-after-boundary-pos))))
1141 (when boundary-pos
1142 (setq pptok ptok
1143 ptok tok
1144 tok boundary-pos
1145 sym 'boundary)
1146 ;; Like a C "continue". Analyze the next sexp.
1147 (throw 'loop t))))
1149 ;; ObjC method def?
1150 (when (and c-opt-method-key
1151 (setq saved (c-in-method-def-p)))
1152 (setq pos saved
1153 ignore-labels t) ; Avoid the label check on exit.
1154 (throw 'loop nil)) ; 3. ObjC method def.
1156 ;; Might we have a bitfield declaration, "<type> <id> : <size>"?
1157 (if c-has-bitfields
1158 (cond
1159 ;; The : <size> and <id> fields?
1160 ((and (numberp c-maybe-labelp)
1161 (not bitfield-size-pos)
1162 (save-excursion
1163 (goto-char (or tok start))
1164 (not (looking-at c-keywords-regexp)))
1165 (not (looking-at c-keywords-regexp))
1166 (not (c-punctuation-in (point) c-maybe-labelp)))
1167 (setq bitfield-size-pos (or tok start)
1168 bitfield-id-pos (point)))
1169 ;; The <type> field?
1170 ((and bitfield-id-pos
1171 (not bitfield-type-pos))
1172 (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-)
1173 (not (looking-at c-not-primitive-type-keywords-regexp))
1174 (not (c-punctuation-in (point) tok)))
1175 (setq bitfield-type-pos (point))
1176 (setq bitfield-size-pos nil
1177 bitfield-id-pos nil)))))
1179 ;; Handle labels.
1180 (unless (eq ignore-labels t)
1181 (when (numberp c-maybe-labelp)
1182 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1183 ;; might be in a label now. Have we got a real label
1184 ;; (including a case label) or something like C++'s "public:"?
1185 ;; A case label might use an expression rather than a token.
1186 (setq after-case:-pos (or tok start))
1187 (if (or (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1188 ;; Catch C++'s inheritance construct "class foo : bar".
1189 (save-excursion
1190 (and
1191 (c-safe (c-backward-sexp) t)
1192 (looking-at c-nonlabel-token-2-key))))
1193 (setq c-maybe-labelp nil)
1194 (if after-labels-pos ; Have we already encountered a label?
1195 (if (not last-label-pos)
1196 (setq last-label-pos (or tok start)))
1197 (setq after-labels-pos (or tok start)))
1198 (setq c-maybe-labelp t
1199 label-good-pos nil))) ; bogus "label"
1201 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1202 ; been found.
1203 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
1204 ;; We're in a potential label and it's the first
1205 ;; time we've found something that isn't allowed in
1206 ;; one.
1207 (setq label-good-pos (or tok start))))
1209 ;; We've moved back by a sexp, so update the token positions.
1210 (setq sym nil
1211 pptok ptok
1212 ptok tok
1213 tok (point)
1214 pos tok) ; always non-nil
1215 ) ; end of (catch loop ....)
1216 ) ; end of sexp-at-a-time (while ....)
1218 ;; If the stack isn't empty there might be errors to report.
1219 (while stack
1220 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1221 (c-bos-report-error))
1222 (setq saved-pos (cdr (car stack))
1223 stack (cdr stack)))
1225 (when (and (eq ret 'same)
1226 (not (memq sym '(boundary ignore nil))))
1227 ;; Need to investigate closer whether we've crossed
1228 ;; between a substatement and its containing statement.
1229 (if (setq saved
1230 (cond ((and (looking-at c-block-stmt-1-2-key)
1231 (eq (char-after ptok) ?\())
1232 pptok)
1233 ((looking-at c-block-stmt-1-key)
1234 ptok)
1235 (t pptok)))
1236 (cond ((> start saved) (setq pos saved))
1237 ((= start saved) (setq ret 'up)))))
1239 (when (and (not ignore-labels)
1240 (eq c-maybe-labelp t)
1241 (not (eq ret 'beginning))
1242 after-labels-pos
1243 (not bitfield-type-pos) ; Bitfields take precedence over labels.
1244 (or (not label-good-pos)
1245 (<= label-good-pos pos)
1246 (progn
1247 (goto-char (if (and last-label-pos
1248 (< last-label-pos start))
1249 last-label-pos
1250 pos))
1251 (looking-at c-label-kwds-regexp))))
1252 ;; We're in a label. Maybe we should step to the statement
1253 ;; after it.
1254 (if (< after-labels-pos start)
1255 (setq pos after-labels-pos)
1256 (setq ret 'label)
1257 (if (and last-label-pos (< last-label-pos start))
1258 ;; Might have jumped over several labels. Go to the last one.
1259 (setq pos last-label-pos)))))
1261 ;; Have we got "case <expression>:"?
1262 (goto-char pos)
1263 (when (and after-case:-pos
1264 (not (eq ret 'beginning))
1265 (looking-at c-case-kwds-regexp))
1266 (if (< after-case:-pos start)
1267 (setq pos after-case:-pos))
1268 (if (eq ret 'same)
1269 (setq ret 'label)))
1271 ;; Skip over the unary operators that can start the statement.
1272 (while (progn
1273 (c-backward-syntactic-ws)
1274 ;; protect AWK post-inc/decrement operators, etc.
1275 (and (not (c-at-vsemi-p (point)))
1276 (/= (skip-chars-backward "-+!*&~@`#") 0)))
1277 (setq pos (point)))
1278 (goto-char pos)
1279 ret)))
1281 (defun c-punctuation-in (from to)
1282 "Return non-nil if there is a non-comment non-macro punctuation character
1283 between FROM and TO. FROM must not be in a string or comment. The returned
1284 value is the position of the first such character."
1285 (save-excursion
1286 (goto-char from)
1287 (let ((pos (point)))
1288 (while (progn (skip-chars-forward c-symbol-chars to)
1289 (c-forward-syntactic-ws to)
1290 (> (point) pos))
1291 (setq pos (point))))
1292 (and (< (point) to) (point))))
1294 (defun c-crosses-statement-barrier-p (from to)
1295 "Return non-nil if buffer positions FROM to TO cross one or more
1296 statement or declaration boundaries. The returned value is actually
1297 the position of the earliest boundary char. FROM must not be within
1298 a string or comment.
1300 The variable `c-maybe-labelp' is set to the position of the first `:' that
1301 might start a label (i.e. not part of `::' and not preceded by `?'). If a
1302 single `?' is found, then `c-maybe-labelp' is cleared.
1304 For AWK, a statement which is terminated by an EOL (not a ; or a }) is
1305 regarded as having a \"virtual semicolon\" immediately after the last token on
1306 the line. If this virtual semicolon is _at_ from, the function recognizes it.
1308 Note that this function might do hidden buffer changes. See the
1309 comment at the start of cc-engine.el for more info."
1310 (let* ((skip-chars
1311 ;; If the current language has CPP macros, insert # into skip-chars.
1312 (if c-opt-cpp-symbol
1313 (concat (substring c-stmt-delim-chars 0 1) ; "^"
1314 c-opt-cpp-symbol ; usually "#"
1315 (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:"
1316 c-stmt-delim-chars))
1317 (non-skip-list
1318 (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:)
1319 lit-range lit-start vsemi-pos)
1320 (save-restriction
1321 (widen)
1322 (save-excursion
1323 (catch 'done
1324 (goto-char from)
1325 (while (progn (skip-chars-forward
1326 skip-chars
1327 (min to (c-point 'bonl)))
1328 (< (point) to))
1329 (cond
1330 ;; Virtual semicolon?
1331 ((and (bolp)
1332 (save-excursion
1333 (progn
1334 (if (setq lit-start (c-literal-start from)) ; Have we landed in a string/comment?
1335 (goto-char lit-start))
1336 (c-backward-syntactic-ws) ; ? put a limit here, maybe?
1337 (setq vsemi-pos (point))
1338 (c-at-vsemi-p))))
1339 (throw 'done vsemi-pos))
1340 ;; In a string/comment?
1341 ((setq lit-range (c-literal-limits from))
1342 (goto-char (cdr lit-range)))
1343 ((eq (char-after) ?:)
1344 (forward-char)
1345 (if (and (eq (char-after) ?:)
1346 (< (point) to))
1347 ;; Ignore scope operators.
1348 (forward-char)
1349 (setq c-maybe-labelp (1- (point)))))
1350 ((eq (char-after) ??)
1351 ;; A question mark. Can't be a label, so stop
1352 ;; looking for more : and ?.
1353 (setq c-maybe-labelp nil
1354 skip-chars (substring c-stmt-delim-chars 0 -2)))
1355 ;; At a CPP construct or a "#" or "##" operator?
1356 ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol))
1357 (if (save-excursion
1358 (skip-chars-backward " \t")
1359 (and (bolp)
1360 (or (bobp)
1361 (not (eq (char-before (1- (point))) ?\\)))))
1362 (c-end-of-macro)
1363 (skip-chars-forward c-opt-cpp-symbol)))
1364 ((memq (char-after) non-skip-list)
1365 (throw 'done (point)))))
1366 ;; In trailing space after an as yet undetected virtual semicolon?
1367 (c-backward-syntactic-ws from)
1368 (when (and (bolp) (not (bobp))) ; Can happen in AWK Mode with an
1369 ; unterminated string/regexp.
1370 (backward-char))
1371 (if (and (< (point) to)
1372 (c-at-vsemi-p))
1373 (point)
1374 nil))))))
1376 (defun c-at-statement-start-p ()
1377 "Return non-nil if the point is at the first token in a statement
1378 or somewhere in the syntactic whitespace before it.
1380 A \"statement\" here is not restricted to those inside code blocks.
1381 Any kind of declaration-like construct that occur outside function
1382 bodies is also considered a \"statement\".
1384 Note that this function might do hidden buffer changes. See the
1385 comment at the start of cc-engine.el for more info."
1387 (save-excursion
1388 (let ((end (point))
1389 c-maybe-labelp)
1390 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1391 (or (bobp)
1392 (eq (char-before) ?})
1393 (and (eq (char-before) ?{)
1394 (not (and c-special-brace-lists
1395 (progn (backward-char)
1396 (c-looking-at-special-brace-list)))))
1397 (c-crosses-statement-barrier-p (point) end)))))
1399 (defun c-at-expression-start-p ()
1400 "Return non-nil if the point is at the first token in an expression or
1401 statement, or somewhere in the syntactic whitespace before it.
1403 An \"expression\" here is a bit different from the normal language
1404 grammar sense: It's any sequence of expression tokens except commas,
1405 unless they are enclosed inside parentheses of some kind. Also, an
1406 expression never continues past an enclosing parenthesis, but it might
1407 contain parenthesis pairs of any sort except braces.
1409 Since expressions never cross statement boundaries, this function also
1410 recognizes statement beginnings, just like `c-at-statement-start-p'.
1412 Note that this function might do hidden buffer changes. See the
1413 comment at the start of cc-engine.el for more info."
1415 (save-excursion
1416 (let ((end (point))
1417 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1418 c-maybe-labelp)
1419 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1420 (or (bobp)
1421 (memq (char-before) '(?{ ?}))
1422 (save-excursion (backward-char)
1423 (looking-at "\\s("))
1424 (c-crosses-statement-barrier-p (point) end)))))
1427 ;; A set of functions that covers various idiosyncrasies in
1428 ;; implementations of `forward-comment'.
1430 ;; Note: Some emacsen considers incorrectly that any line comment
1431 ;; ending with a backslash continues to the next line. I can't think
1432 ;; of any way to work around that in a reliable way without changing
1433 ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1434 ;; changing the syntax for backslash doesn't work since we must treat
1435 ;; escapes in string literals correctly.)
1437 (defun c-forward-single-comment ()
1438 "Move forward past whitespace and the closest following comment, if any.
1439 Return t if a comment was found, nil otherwise. In either case, the
1440 point is moved past the following whitespace. Line continuations,
1441 i.e. a backslashes followed by line breaks, are treated as whitespace.
1442 The line breaks that end line comments are considered to be the
1443 comment enders, so the point will be put on the beginning of the next
1444 line if it moved past a line comment.
1446 This function does not do any hidden buffer changes."
1448 (let ((start (point)))
1449 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1450 (goto-char (match-end 0)))
1452 (when (forward-comment 1)
1453 (if (eobp)
1454 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1455 ;; forwards at eob.
1458 ;; Emacs includes the ending newline in a b-style (c++)
1459 ;; comment, but XEmacs doesn't. We depend on the Emacs
1460 ;; behavior (which also is symmetric).
1461 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1462 (condition-case nil (forward-char 1)))
1464 t))))
1466 (defsubst c-forward-comments ()
1467 "Move forward past all following whitespace and comments.
1468 Line continuations, i.e. a backslashes followed by line breaks, are
1469 treated as whitespace.
1471 Note that this function might do hidden buffer changes. See the
1472 comment at the start of cc-engine.el for more info."
1474 (while (or
1475 ;; If forward-comment in at least XEmacs 21 is given a large
1476 ;; positive value, it'll loop all the way through if it hits
1477 ;; eob.
1478 (and (forward-comment 5)
1479 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1480 ;; forwards at eob.
1481 (not (eobp)))
1483 (when (looking-at "\\\\[\n\r]")
1484 (forward-char 2)
1485 t))))
1487 (defun c-backward-single-comment ()
1488 "Move backward past whitespace and the closest preceding comment, if any.
1489 Return t if a comment was found, nil otherwise. In either case, the
1490 point is moved past the preceding whitespace. Line continuations,
1491 i.e. a backslashes followed by line breaks, are treated as whitespace.
1492 The line breaks that end line comments are considered to be the
1493 comment enders, so the point cannot be at the end of the same line to
1494 move over a line comment.
1496 This function does not do any hidden buffer changes."
1498 (let ((start (point)))
1499 ;; When we got newline terminated comments, forward-comment in all
1500 ;; supported emacsen so far will stop at eol of each line not
1501 ;; ending with a comment when moving backwards. This corrects for
1502 ;; that, and at the same time handles line continuations.
1503 (while (progn
1504 (skip-chars-backward " \t\n\r\f\v")
1505 (and (looking-at "[\n\r]")
1506 (eq (char-before) ?\\)))
1507 (backward-char))
1509 (if (bobp)
1510 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1511 ;; backwards at bob.
1514 ;; Leave point after the closest following newline if we've
1515 ;; backed up over any above, since forward-comment won't move
1516 ;; backward over a line comment if point is at the end of the
1517 ;; same line.
1518 (re-search-forward "\\=\\s *[\n\r]" start t)
1520 (if (if (forward-comment -1)
1521 (if (eolp)
1522 ;; If forward-comment above succeeded and we're at eol
1523 ;; then the newline we moved over above didn't end a
1524 ;; line comment, so we give it another go.
1525 (forward-comment -1)
1528 ;; Emacs <= 20 and XEmacs move back over the closer of a
1529 ;; block comment that lacks an opener.
1530 (if (looking-at "\\*/")
1531 (progn (forward-char 2) nil)
1532 t)))))
1534 (defsubst c-backward-comments ()
1535 "Move backward past all preceding whitespace and comments.
1536 Line continuations, i.e. a backslashes followed by line breaks, are
1537 treated as whitespace. The line breaks that end line comments are
1538 considered to be the comment enders, so the point cannot be at the end
1539 of the same line to move over a line comment. Unlike
1540 c-backward-syntactic-ws, this function doesn't move back over
1541 preprocessor directives.
1543 Note that this function might do hidden buffer changes. See the
1544 comment at the start of cc-engine.el for more info."
1546 (let ((start (point)))
1547 (while (and
1548 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
1549 ;; return t when moving backwards at bob.
1550 (not (bobp))
1552 (if (let (moved-comment)
1553 (while
1554 (and (not (setq moved-comment (forward-comment -1)))
1555 ;; Cope specifically with ^M^J here -
1556 ;; forward-comment sometimes gets stuck after ^Ms,
1557 ;; sometimes after ^M^J.
1559 (when (eq (char-before) ?\r)
1560 (backward-char)
1562 (when (and (eq (char-before) ?\n)
1563 (eq (char-before (1- (point))) ?\r))
1564 (backward-char 2)
1565 t))))
1566 moved-comment)
1567 (if (looking-at "\\*/")
1568 ;; Emacs <= 20 and XEmacs move back over the
1569 ;; closer of a block comment that lacks an opener.
1570 (progn (forward-char 2) nil)
1573 ;; XEmacs treats line continuations as whitespace but
1574 ;; only in the backward direction, which seems a bit
1575 ;; odd. Anyway, this is necessary for Emacs.
1576 (when (and (looking-at "[\n\r]")
1577 (eq (char-before) ?\\)
1578 (< (point) start))
1579 (backward-char)
1580 t))))))
1583 ;; Tools for skipping over syntactic whitespace.
1585 ;; The following functions use text properties to cache searches over
1586 ;; large regions of syntactic whitespace. It works as follows:
1588 ;; o If a syntactic whitespace region contains anything but simple
1589 ;; whitespace (i.e. space, tab and line breaks), the text property
1590 ;; `c-in-sws' is put over it. At places where we have stopped
1591 ;; within that region there's also a `c-is-sws' text property.
1592 ;; That since there typically are nested whitespace inside that
1593 ;; must be handled separately, e.g. whitespace inside a comment or
1594 ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1595 ;; to jump to another point with that property within the same
1596 ;; `c-in-sws' region. It can be likened to a ladder where
1597 ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1599 ;; o The `c-is-sws' property is put on the simple whitespace chars at
1600 ;; a "rung position" and also maybe on the first following char.
1601 ;; As many characters as can be conveniently found in this range
1602 ;; are marked, but no assumption can be made that the whole range
1603 ;; is marked (it could be clobbered by later changes, for
1604 ;; instance).
1606 ;; Note that some part of the beginning of a sequence of simple
1607 ;; whitespace might be part of the end of a preceding line comment
1608 ;; or cpp directive and must not be considered part of the "rung".
1609 ;; Such whitespace is some amount of horizontal whitespace followed
1610 ;; by a newline. In the case of cpp directives it could also be
1611 ;; two newlines with horizontal whitespace between them.
1613 ;; The reason to include the first following char is to cope with
1614 ;; "rung positions" that don't have any ordinary whitespace. If
1615 ;; `c-is-sws' is put on a token character it does not have
1616 ;; `c-in-sws' set simultaneously. That's the only case when that
1617 ;; can occur, and the reason for not extending the `c-in-sws'
1618 ;; region to cover it is that the `c-in-sws' region could then be
1619 ;; accidentally merged with a following one if the token is only
1620 ;; one character long.
1622 ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1623 ;; removed in the changed region. If the change was inside
1624 ;; syntactic whitespace that means that the "ladder" is broken, but
1625 ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1626 ;; parts on either side and use an ordinary search only to "repair"
1627 ;; the gap.
1629 ;; Special care needs to be taken if a region is removed: If there
1630 ;; are `c-in-sws' on both sides of it which do not connect inside
1631 ;; the region then they can't be joined. If e.g. a marked macro is
1632 ;; broken, syntactic whitespace inside the new text might be
1633 ;; marked. If those marks would become connected with the old
1634 ;; `c-in-sws' range around the macro then we could get a ladder
1635 ;; with one end outside the macro and the other at some whitespace
1636 ;; within it.
1638 ;; The main motivation for this system is to increase the speed in
1639 ;; skipping over the large whitespace regions that can occur at the
1640 ;; top level in e.g. header files that contain a lot of comments and
1641 ;; cpp directives. For small comments inside code it's probably
1642 ;; slower than using `forward-comment' straightforwardly, but speed is
1643 ;; not a significant factor there anyway.
1645 ; (defface c-debug-is-sws-face
1646 ; '((t (:background "GreenYellow")))
1647 ; "Debug face to mark the `c-is-sws' property.")
1648 ; (defface c-debug-in-sws-face
1649 ; '((t (:underline t)))
1650 ; "Debug face to mark the `c-in-sws' property.")
1652 ; (defun c-debug-put-sws-faces ()
1653 ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1654 ; ;; properties in the buffer.
1655 ; (interactive)
1656 ; (save-excursion
1657 ; (c-save-buffer-state (in-face)
1658 ; (goto-char (point-min))
1659 ; (setq in-face (if (get-text-property (point) 'c-is-sws)
1660 ; (point)))
1661 ; (while (progn
1662 ; (goto-char (next-single-property-change
1663 ; (point) 'c-is-sws nil (point-max)))
1664 ; (if in-face
1665 ; (progn
1666 ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1667 ; (setq in-face nil))
1668 ; (setq in-face (point)))
1669 ; (not (eobp))))
1670 ; (goto-char (point-min))
1671 ; (setq in-face (if (get-text-property (point) 'c-in-sws)
1672 ; (point)))
1673 ; (while (progn
1674 ; (goto-char (next-single-property-change
1675 ; (point) 'c-in-sws nil (point-max)))
1676 ; (if in-face
1677 ; (progn
1678 ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1679 ; (setq in-face nil))
1680 ; (setq in-face (point)))
1681 ; (not (eobp)))))))
1683 (defmacro c-debug-sws-msg (&rest args)
1684 (ignore args)
1685 ;;`(message ,@args)
1688 (defmacro c-put-is-sws (beg end)
1689 ;; This macro does a hidden buffer change.
1690 `(let ((beg ,beg) (end ,end))
1691 (put-text-property beg end 'c-is-sws t)
1692 ,@(when (facep 'c-debug-is-sws-face)
1693 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1695 (defmacro c-put-in-sws (beg end)
1696 ;; This macro does a hidden buffer change.
1697 `(let ((beg ,beg) (end ,end))
1698 (put-text-property beg end 'c-in-sws t)
1699 ,@(when (facep 'c-debug-is-sws-face)
1700 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1702 (defmacro c-remove-is-sws (beg end)
1703 ;; This macro does a hidden buffer change.
1704 `(let ((beg ,beg) (end ,end))
1705 (remove-text-properties beg end '(c-is-sws nil))
1706 ,@(when (facep 'c-debug-is-sws-face)
1707 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1709 (defmacro c-remove-in-sws (beg end)
1710 ;; This macro does a hidden buffer change.
1711 `(let ((beg ,beg) (end ,end))
1712 (remove-text-properties beg end '(c-in-sws nil))
1713 ,@(when (facep 'c-debug-is-sws-face)
1714 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1716 (defmacro c-remove-is-and-in-sws (beg end)
1717 ;; This macro does a hidden buffer change.
1718 `(let ((beg ,beg) (end ,end))
1719 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1720 ,@(when (facep 'c-debug-is-sws-face)
1721 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1722 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1724 ;; The type of literal position `end' is in a `before-change-functions'
1725 ;; function - one of `c', `c++', `pound', or nil (but NOT `string').
1726 (defvar c-sws-lit-type nil)
1727 ;; A cons (START . STOP) of the bounds of the comment or CPP construct
1728 ;; enclosing END, if any, else nil.
1729 (defvar c-sws-lit-limits nil)
1731 (defun c-invalidate-sws-region-before (end)
1732 ;; Called from c-before-change. END is the end of the change region, the
1733 ;; standard parameter given to all before-change-functions.
1735 ;; Note whether END is inside a comment or CPP construct, and if so note its
1736 ;; bounds in `c-sws-lit-limits' and type in `c-sws-lit-type'.
1737 (save-excursion
1738 (goto-char end)
1739 (let* ((limits (c-literal-limits))
1740 (lit-type (c-literal-type limits)))
1741 (cond
1742 ((memq lit-type '(c c++))
1743 (setq c-sws-lit-type lit-type
1744 c-sws-lit-limits limits))
1745 ((c-beginning-of-macro)
1746 (setq c-sws-lit-type 'pound
1747 c-sws-lit-limits (cons (point)
1748 (progn (c-end-of-macro) (point)))))
1749 (t (setq c-sws-lit-type nil
1750 c-sws-lit-limits nil))))))
1752 (defun c-invalidate-sws-region-after-del (beg end old-len)
1753 ;; Text has been deleted, OLD-LEN characters of it starting from position
1754 ;; BEG. END is typically eq to BEG. Should there have been a comment or
1755 ;; CPP construct open at END before the deletion, check whether this
1756 ;; deletion deleted or "damaged" its opening delimiter. If so, return the
1757 ;; current position of where the construct ended, otherwise return nil.
1758 (when c-sws-lit-limits
1759 (setcdr c-sws-lit-limits (- (cdr c-sws-lit-limits) old-len))
1760 (if (and (< beg (+ (car c-sws-lit-limits) 2)) ; A lazy assumption that
1761 ; comment delimiters are 2
1762 ; chars long.
1763 (or (get-text-property end 'c-in-sws)
1764 (next-single-property-change end 'c-in-sws nil
1765 (cdr c-sws-lit-limits))
1766 (get-text-property end 'c-is-sws)
1767 (next-single-property-change end 'c-is-sws nil
1768 (cdr c-sws-lit-limits))))
1769 (cdr c-sws-lit-limits))))
1771 (defun c-invalidate-sws-region-after-ins (end)
1772 ;; Text has been inserted, ending at buffer position END. Should there be a
1773 ;; literal or CPP construct open at END, check whether there are `c-in-sws'
1774 ;; or `c-is-sws' text properties inside this literal. If there are, return
1775 ;; the buffer position of the end of the literal, else return nil.
1776 (save-excursion
1777 (let* ((limits (c-literal-limits))
1778 (lit-type (c-literal-type limits)))
1779 (goto-char end)
1780 (when (and (not (memq lit-type '(c c++)))
1781 (c-beginning-of-macro))
1782 (setq lit-type 'pound
1783 limits (cons (point)
1784 (progn (c-end-of-macro) (point)))))
1785 (when (memq lit-type '(c c++ pound))
1786 (let ((next-in (next-single-property-change (car limits) 'c-in-sws
1787 nil (cdr limits)))
1788 (next-is (next-single-property-change (car limits) 'c-is-sws
1789 nil (cdr limits))))
1790 (and (or next-in next-is)
1791 (cdr limits)))))))
1793 (defun c-invalidate-sws-region-after (beg end old-len)
1794 ;; Called from `after-change-functions'. Remove any stale `c-in-sws' or
1795 ;; `c-is-sws' text properties from the vicinity of the change. BEG, END,
1796 ;; and OLD-LEN are the standard arguments given to after-change functions.
1798 ;; Note that if `c-forward-sws' or `c-backward-sws' are used outside
1799 ;; `c-save-buffer-state' or similar then this will remove the cache
1800 ;; properties right after they're added.
1802 ;; This function does hidden buffer changes.
1803 (let ((del-end
1804 (and (> old-len 0)
1805 (c-invalidate-sws-region-after-del beg end old-len)))
1806 (ins-end
1807 (and (> end beg)
1808 (c-invalidate-sws-region-after-ins end))))
1809 (save-excursion
1810 ;; Adjust the end to remove the properties in any following simple
1811 ;; ws up to and including the next line break, if there is any
1812 ;; after the changed region. This is necessary e.g. when a rung
1813 ;; marked empty line is converted to a line comment by inserting
1814 ;; "//" before the line break. In that case the line break would
1815 ;; keep the rung mark which could make a later `c-backward-sws'
1816 ;; move into the line comment instead of over it.
1817 (goto-char end)
1818 (skip-chars-forward " \t\f\v")
1819 (when (and (eolp) (not (eobp)))
1820 (setq end (1+ (point)))))
1822 (when (and (= beg end)
1823 (get-text-property beg 'c-in-sws)
1824 (> beg (point-min))
1825 (get-text-property (1- beg) 'c-in-sws))
1826 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1827 ;; safe to keep a range that was continuous before the change. E.g:
1829 ;; #define foo
1830 ;; \
1831 ;; bar
1833 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1834 ;; after "foo" is removed then "bar" will become part of the cpp
1835 ;; directive instead of a syntactically relevant token. In that
1836 ;; case there's no longer syntactic ws from "#" to "b".
1837 (setq beg (1- beg)))
1839 (setq end (max (or del-end end)
1840 (or ins-end end)
1841 end))
1843 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1844 (c-remove-is-and-in-sws beg end)))
1846 (defun c-forward-sws ()
1847 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
1849 ;; This function might do hidden buffer changes.
1851 (let (;; `rung-pos' is set to a position as early as possible in the
1852 ;; unmarked part of the simple ws region.
1853 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1854 rung-is-marked next-rung-is-marked simple-ws-end macro-start macro-end
1855 ;; `safe-start' is set when it's safe to cache the start position.
1856 ;; This is the case except when we have an unterminated block comment
1857 ;; within a macro.
1858 safe-start)
1860 ;; Skip simple ws and do a quick check on the following character to see
1861 ;; if it's anything that can't start syntactic ws, so we can bail out
1862 ;; early in the majority of cases when there just are a few ws chars.
1863 (skip-chars-forward " \t\n\r\f\v")
1864 (when (or (looking-at c-syntactic-ws-start)
1865 (and c-opt-cpp-prefix
1866 (looking-at c-noise-macro-name-re)))
1868 (setq rung-end-pos (min (1+ (point)) (point-max)))
1869 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1870 'c-is-sws t))
1871 ;; Find the last rung position to avoid setting properties in all
1872 ;; the cases when the marked rung is complete.
1873 ;; (`next-single-property-change' is certain to move at least one
1874 ;; step forward.)
1875 (setq rung-pos (1- (c-next-single-property-change
1876 rung-is-marked 'c-is-sws nil rung-end-pos)))
1877 ;; Got no marked rung here. Since the simple ws might have started
1878 ;; inside a line comment or cpp directive we must set `rung-pos' as
1879 ;; high as possible.
1880 (setq rung-pos (point)))
1882 (with-silent-modifications
1883 (while
1884 (progn
1885 ;; In the following while form, we move over a "ladder" and
1886 ;; following simple WS each time round the loop, appending the WS
1887 ;; onto the ladder, joining adjacent ladders, and terminating when
1888 ;; there is no more WS or we reach EOB.
1889 (while
1890 (when (and rung-is-marked
1891 (get-text-property (point) 'c-in-sws))
1893 ;; The following search is the main reason that `c-in-sws'
1894 ;; and `c-is-sws' aren't combined to one property.
1895 (goto-char (c-next-single-property-change
1896 (point) 'c-in-sws nil (point-max)))
1897 (unless (get-text-property (point) 'c-is-sws)
1898 ;; If the `c-in-sws' region extended past the last
1899 ;; `c-is-sws' char we have to go back a bit.
1900 (or (get-text-property (1- (point)) 'c-is-sws)
1901 (goto-char (previous-single-property-change
1902 (point) 'c-is-sws)))
1903 (backward-char))
1905 (c-debug-sws-msg
1906 "c-forward-sws cached move %s -> %s (max %s)"
1907 rung-pos (point) (point-max))
1909 (setq rung-pos (point))
1910 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1911 (not (eobp))))
1913 ;; We'll loop here if there is simple ws after the last rung.
1914 ;; That means that there's been some change in it and it's
1915 ;; possible that we've stepped into another ladder, so extend
1916 ;; the previous one to join with it if there is one, and try to
1917 ;; use the cache again.
1918 (c-debug-sws-msg
1919 "c-forward-sws extending rung with [%s..%s] (max %s)"
1920 (1+ rung-pos) (1+ (point)) (point-max))
1921 (unless (get-text-property (point) 'c-is-sws)
1922 ;; Remove any `c-in-sws' property from the last char of
1923 ;; the rung before we mark it with `c-is-sws', so that we
1924 ;; won't connect with the remains of a broken "ladder".
1925 (c-remove-in-sws (point) (1+ (point))))
1926 (c-put-is-sws (1+ rung-pos)
1927 (1+ (point)))
1928 (c-put-in-sws rung-pos
1929 (setq rung-pos (point)
1930 last-put-in-sws-pos rung-pos)))
1932 ;; Now move over any comments (x)or a CPP construct.
1933 (setq simple-ws-end (point))
1934 (setq safe-start t)
1935 ;; Take elaborate precautions to detect an open block comment at
1936 ;; the end of a macro. If we find one, we set `safe-start' to nil
1937 ;; and break off any further scanning of comments.
1938 (let ((com-begin (point)) com-end in-macro)
1939 (when (and (c-forward-single-comment)
1940 (setq com-end (point))
1941 (save-excursion
1942 (goto-char com-begin)
1943 (c-beginning-of-macro)))
1944 (setq in-macro t)
1945 (goto-char com-begin)
1946 (if (progn (c-end-of-macro com-end)
1947 (< (point) com-end))
1948 (setq safe-start nil)))
1949 (if in-macro
1950 (while (and safe-start
1951 com-end (> com-end com-begin)
1952 (setq com-begin (point))
1953 (when (and (c-forward-single-comment)
1954 (setq com-end (point)))
1955 (goto-char com-begin)
1956 (if (progn (c-end-of-macro com-end)
1957 (< (point) com-end))
1958 (setq safe-start nil))
1959 safe-start)))
1960 (c-forward-comments)))
1962 (cond
1963 ((/= (point) simple-ws-end)
1964 ;; Skipped over comments. Don't cache at eob in case the buffer
1965 ;; is narrowed.
1966 (not (eobp)))
1968 ((save-excursion
1969 (and c-opt-cpp-prefix
1970 (looking-at c-opt-cpp-start)
1971 (setq macro-start (point))
1972 (progn (skip-chars-backward " \t")
1973 (bolp))
1974 (or (bobp)
1975 (progn (backward-char)
1976 (not (eq (char-before) ?\\))))))
1977 ;; Skip a preprocessor directive.
1978 (end-of-line)
1979 (while (and (eq (char-before) ?\\)
1980 (= (forward-line 1) 0))
1981 (end-of-line))
1982 (setq macro-end (point))
1983 ;; Check for an open block comment at the end of the macro.
1984 (let ((s (parse-partial-sexp macro-start macro-end)))
1985 (if (and (elt s 4) ; in a comment
1986 (null (elt s 7))) ; a block comment
1987 (setq safe-start nil)))
1988 (forward-line 1)
1989 ;; Don't cache at eob in case the buffer is narrowed.
1990 (not (eobp)))
1992 ((and c-opt-cpp-prefix
1993 (looking-at c-noise-macro-name-re))
1994 ;; Skip over a noise macro.
1995 (goto-char (match-end 1))
1996 (not (eobp)))))
1998 ;; We've searched over a piece of non-white syntactic ws. See if this
1999 ;; can be cached.
2000 (setq next-rung-pos (point))
2001 (skip-chars-forward " \t\n\r\f\v")
2002 (setq rung-end-pos (min (1+ (point)) (point-max)))
2004 (if (or
2005 ;; Cache if we haven't skipped comments only, and if we started
2006 ;; either from a marked rung or from a completely uncached
2007 ;; position.
2008 (and safe-start
2009 (or rung-is-marked
2010 (not (get-text-property simple-ws-end 'c-in-sws))))
2012 ;; See if there's a marked rung in the encountered simple ws. If
2013 ;; so then we can cache, unless `safe-start' is nil. Even then
2014 ;; we need to do this to check if the cache can be used for the
2015 ;; next step.
2016 (and (setq next-rung-is-marked
2017 (text-property-any next-rung-pos rung-end-pos
2018 'c-is-sws t))
2019 safe-start))
2021 (progn
2022 (c-debug-sws-msg
2023 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
2024 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
2025 (point-max))
2027 ;; Remove the properties for any nested ws that might be cached.
2028 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
2029 ;; anyway.
2030 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
2031 (unless (and rung-is-marked (= rung-pos simple-ws-end))
2032 (c-put-is-sws rung-pos
2033 (1+ simple-ws-end))
2034 (setq rung-is-marked t))
2035 (c-put-in-sws rung-pos
2036 (setq rung-pos (point)
2037 last-put-in-sws-pos rung-pos))
2038 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
2039 ;; Remove any `c-in-sws' property from the last char of
2040 ;; the rung before we mark it with `c-is-sws', so that we
2041 ;; won't connect with the remains of a broken "ladder".
2042 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
2043 (c-put-is-sws next-rung-pos
2044 rung-end-pos))
2046 (c-debug-sws-msg
2047 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
2048 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
2049 (point-max))
2051 ;; Set `rung-pos' for the next rung. It's the same thing here as
2052 ;; initially, except that the rung position is set as early as
2053 ;; possible since we can't be in the ending ws of a line comment or
2054 ;; cpp directive now.
2055 (if (setq rung-is-marked next-rung-is-marked)
2056 (setq rung-pos (1- (c-next-single-property-change
2057 rung-is-marked 'c-is-sws nil rung-end-pos)))
2058 (setq rung-pos next-rung-pos))))
2060 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
2061 ;; another one after the point (which might occur when editing inside a
2062 ;; comment or macro).
2063 (when (eq last-put-in-sws-pos (point))
2064 (cond ((< last-put-in-sws-pos (point-max))
2065 (c-debug-sws-msg
2066 "c-forward-sws clearing at %s for cache separation"
2067 last-put-in-sws-pos)
2068 (c-remove-in-sws last-put-in-sws-pos
2069 (1+ last-put-in-sws-pos)))
2071 ;; If at eob we have to clear the last character before the end
2072 ;; instead since the buffer might be narrowed and there might
2073 ;; be a `c-in-sws' after (point-max). In this case it's
2074 ;; necessary to clear both properties.
2075 (c-debug-sws-msg
2076 "c-forward-sws clearing thoroughly at %s for cache separation"
2077 (1- last-put-in-sws-pos))
2078 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
2079 last-put-in-sws-pos))))
2080 ))))
2082 (defun c-backward-sws ()
2083 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
2085 ;; This function might do hidden buffer changes.
2087 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
2088 ;; part of the simple ws region.
2089 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
2090 rung-is-marked simple-ws-beg cmt-skip-pos)
2092 ;; Skip simple horizontal ws and do a quick check on the preceding
2093 ;; character to see if it's anything that can't end syntactic ws, so we can
2094 ;; bail out early in the majority of cases when there just are a few ws
2095 ;; chars. Newlines are complicated in the backward direction, so we can't
2096 ;; skip over them.
2097 (skip-chars-backward " \t\f")
2098 (when (and (not (bobp))
2099 (save-excursion
2100 (backward-char)
2101 (or (looking-at c-syntactic-ws-end)
2102 (and c-opt-cpp-prefix
2103 (looking-at c-symbol-char-key)
2104 (progn (c-beginning-of-current-token)
2105 (looking-at c-noise-macro-name-re))))))
2106 ;; Try to find a rung position in the simple ws preceding point, so that
2107 ;; we can get a cache hit even if the last bit of the simple ws has
2108 ;; changed recently.
2109 (setq simple-ws-beg (point))
2110 (skip-chars-backward " \t\n\r\f\v")
2111 (if (setq rung-is-marked (text-property-any
2112 (point) (min (1+ rung-pos) (point-max))
2113 'c-is-sws t))
2114 ;; `rung-pos' will be the earliest marked position, which means that
2115 ;; there might be later unmarked parts in the simple ws region.
2116 ;; It's not worth the effort to fix that; the last part of the
2117 ;; simple ws is also typically edited often, so it could be wasted.
2118 (goto-char (setq rung-pos rung-is-marked))
2119 (goto-char simple-ws-beg))
2121 (with-silent-modifications
2122 (while
2123 (progn
2124 ;; Each time round the next while form, we move back over a ladder
2125 ;; and append any simple WS preceding it, if possible joining with
2126 ;; the previous ladder.
2127 (while
2128 (when (and rung-is-marked
2129 (not (bobp))
2130 (get-text-property (1- (point)) 'c-in-sws))
2132 ;; The following search is the main reason that `c-in-sws'
2133 ;; and `c-is-sws' aren't combined to one property.
2134 (goto-char (previous-single-property-change
2135 (point) 'c-in-sws nil (point-min)))
2136 (unless (get-text-property (point) 'c-is-sws)
2137 ;; If the `c-in-sws' region extended past the first
2138 ;; `c-is-sws' char we have to go forward a bit.
2139 (goto-char (c-next-single-property-change
2140 (point) 'c-is-sws)))
2142 (c-debug-sws-msg
2143 "c-backward-sws cached move %s <- %s (min %s)"
2144 (point) rung-pos (point-min))
2146 (setq rung-pos (point))
2147 (if (and (< (min (skip-chars-backward " \t\f\v")
2148 (progn
2149 (setq simple-ws-beg (point))
2150 (skip-chars-backward " \t\n\r\f\v")))
2152 (setq rung-is-marked
2153 (text-property-any (point) rung-pos
2154 'c-is-sws t)))
2156 (goto-char simple-ws-beg)
2157 nil))
2159 ;; We'll loop here if there is simple ws before the first rung.
2160 ;; That means that there's been some change in it and it's
2161 ;; possible that we've stepped into another ladder, so extend
2162 ;; the previous one to join with it if there is one, and try to
2163 ;; use the cache again.
2164 (c-debug-sws-msg
2165 "c-backward-sws extending rung with [%s..%s] (min %s)"
2166 rung-is-marked rung-pos (point-min))
2167 (unless (get-text-property (1- rung-pos) 'c-is-sws)
2168 ;; Remove any `c-in-sws' property from the last char of
2169 ;; the rung before we mark it with `c-is-sws', so that we
2170 ;; won't connect with the remains of a broken "ladder".
2171 (c-remove-in-sws (1- rung-pos) rung-pos))
2172 (c-put-is-sws rung-is-marked
2173 rung-pos)
2174 (c-put-in-sws rung-is-marked
2175 (1- rung-pos))
2176 (setq rung-pos rung-is-marked
2177 last-put-in-sws-pos rung-pos))
2179 (c-backward-comments)
2180 (setq cmt-skip-pos (point))
2182 (cond
2183 ((and c-opt-cpp-prefix
2184 (/= cmt-skip-pos simple-ws-beg)
2185 (c-beginning-of-macro))
2186 ;; Inside a cpp directive. See if it should be skipped over.
2187 (let ((cpp-beg (point)))
2189 ;; Move back over all line continuations in the region skipped
2190 ;; over by `c-backward-comments'. If we go past it then we
2191 ;; started inside the cpp directive.
2192 (goto-char simple-ws-beg)
2193 (beginning-of-line)
2194 (while (and (> (point) cmt-skip-pos)
2195 (progn (backward-char)
2196 (eq (char-before) ?\\)))
2197 (beginning-of-line))
2199 (if (< (point) cmt-skip-pos)
2200 ;; Don't move past the cpp directive if we began inside
2201 ;; it. Note that the position at the end of the last line
2202 ;; of the macro is also considered to be within it.
2203 (progn (goto-char cmt-skip-pos)
2204 nil)
2206 ;; It's worthwhile to spend a little bit of effort on finding
2207 ;; the end of the macro, to get a good `simple-ws-beg'
2208 ;; position for the cache. Note that `c-backward-comments'
2209 ;; could have stepped over some comments before going into
2210 ;; the macro, and then `simple-ws-beg' must be kept on the
2211 ;; same side of those comments.
2212 (goto-char simple-ws-beg)
2213 (skip-chars-backward " \t\n\r\f\v")
2214 (if (eq (char-before) ?\\)
2215 (forward-char))
2216 (forward-line 1)
2217 (if (< (point) simple-ws-beg)
2218 ;; Might happen if comments after the macro were skipped
2219 ;; over.
2220 (setq simple-ws-beg (point)))
2222 (goto-char cpp-beg)
2223 t)))
2225 ((/= (save-excursion
2226 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
2227 (setq next-rung-pos (point)))
2228 simple-ws-beg)
2229 ;; Skipped over comments. Must put point at the end of
2230 ;; the simple ws at point since we might be after a line
2231 ;; comment or cpp directive that's been partially
2232 ;; narrowed out, and we can't risk marking the simple ws
2233 ;; at the end of it.
2234 (goto-char next-rung-pos)
2237 ((and c-opt-cpp-prefix
2238 (save-excursion
2239 (and (< (skip-syntax-backward "w_") 0)
2240 (progn (setq next-rung-pos (point))
2241 (looking-at c-noise-macro-name-re)))))
2242 ;; Skipped over a noise macro
2243 (goto-char next-rung-pos)
2244 t)))
2246 ;; We've searched over a piece of non-white syntactic ws. See if this
2247 ;; can be cached.
2248 (setq next-rung-pos (point))
2249 (skip-chars-backward " \t\f\v")
2251 (if (or
2252 ;; Cache if we started either from a marked rung or from a
2253 ;; completely uncached position.
2254 rung-is-marked
2255 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
2257 ;; Cache if there's a marked rung in the encountered simple ws.
2258 (save-excursion
2259 (skip-chars-backward " \t\n\r\f\v")
2260 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
2261 'c-is-sws t)))
2263 (progn
2264 (c-debug-sws-msg
2265 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
2266 (point) (1+ next-rung-pos)
2267 simple-ws-beg (min (1+ rung-pos) (point-max))
2268 (point-min))
2270 ;; Remove the properties for any nested ws that might be cached.
2271 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
2272 ;; anyway.
2273 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
2274 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
2275 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
2276 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
2277 ;; Remove any `c-in-sws' property from the last char of
2278 ;; the rung before we mark it with `c-is-sws', so that we
2279 ;; won't connect with the remains of a broken "ladder".
2280 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
2281 (c-put-is-sws simple-ws-beg
2282 rung-end-pos)
2283 (setq rung-is-marked t)))
2284 (c-put-in-sws (setq simple-ws-beg (point)
2285 last-put-in-sws-pos simple-ws-beg)
2286 rung-pos)
2287 (c-put-is-sws (setq rung-pos simple-ws-beg)
2288 (1+ next-rung-pos)))
2290 (c-debug-sws-msg
2291 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
2292 (point) (1+ next-rung-pos)
2293 simple-ws-beg (min (1+ rung-pos) (point-max))
2294 (point-min))
2295 (setq rung-pos next-rung-pos
2296 simple-ws-beg (point))
2299 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
2300 ;; another one before the point (which might occur when editing inside a
2301 ;; comment or macro).
2302 (when (eq last-put-in-sws-pos (point))
2303 (cond ((< (point-min) last-put-in-sws-pos)
2304 (c-debug-sws-msg
2305 "c-backward-sws clearing at %s for cache separation"
2306 (1- last-put-in-sws-pos))
2307 (c-remove-in-sws (1- last-put-in-sws-pos)
2308 last-put-in-sws-pos))
2309 ((> (point-min) 1)
2310 ;; If at bob and the buffer is narrowed, we have to clear the
2311 ;; character we're standing on instead since there might be a
2312 ;; `c-in-sws' before (point-min). In this case it's necessary
2313 ;; to clear both properties.
2314 (c-debug-sws-msg
2315 "c-backward-sws clearing thoroughly at %s for cache separation"
2316 last-put-in-sws-pos)
2317 (c-remove-is-and-in-sws last-put-in-sws-pos
2318 (1+ last-put-in-sws-pos)))))
2319 ))))
2322 ;; Other whitespace tools
2323 (defun c-partial-ws-p (beg end)
2324 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
2325 ;; region? This is a "heuristic" function. .....
2327 ;; The motivation for the second bit is to check whether removing this
2328 ;; region would coalesce two symbols.
2330 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
2331 ;; careful about using this function for, e.g. AWK. (2007/3/7)
2332 (save-excursion
2333 (let ((end+1 (min (1+ end) (point-max))))
2334 (or (progn (goto-char (max (point-min) (1- beg)))
2335 (c-skip-ws-forward end)
2336 (eq (point) end))
2337 (progn (goto-char beg)
2338 (c-skip-ws-forward end+1)
2339 (eq (point) end+1))))))
2341 ;; A system for finding noteworthy parens before the point.
2343 (defconst c-state-cache-too-far 5000)
2344 ;; A maximum comfortable scanning distance, e.g. between
2345 ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2346 ;; this distance is exceeded, we take "emergency measures", e.g. by clearing
2347 ;; the cache and starting again from point-min or a beginning of defun. This
2348 ;; value can be tuned for efficiency or set to a lower value for testing.
2350 (defvar c-state-cache nil)
2351 (make-variable-buffer-local 'c-state-cache)
2352 ;; The state cache used by `c-parse-state' to cut down the amount of
2353 ;; searching. It's the result from some earlier `c-parse-state' call. See
2354 ;; `c-parse-state''s doc string for details of its structure.
2356 ;; The use of the cached info is more effective if the next
2357 ;; `c-parse-state' call is on a line close by the one the cached state
2358 ;; was made at; the cache can actually slow down a little if the
2359 ;; cached state was made very far back in the buffer. The cache is
2360 ;; most effective if `c-parse-state' is used on each line while moving
2361 ;; forward.
2363 (defvar c-state-cache-good-pos 1)
2364 (make-variable-buffer-local 'c-state-cache-good-pos)
2365 ;; This is a position where `c-state-cache' is known to be correct, or
2366 ;; nil (see below). It's a position inside one of the recorded unclosed
2367 ;; parens or the top level, but not further nested inside any literal or
2368 ;; subparen that is closed before the last recorded position.
2370 ;; The exact position is chosen to try to be close to yet earlier than
2371 ;; the position where `c-state-cache' will be called next. Right now
2372 ;; the heuristic is to set it to the position after the last found
2373 ;; closing paren (of any type) before the line on which
2374 ;; `c-parse-state' was called. That is chosen primarily to work well
2375 ;; with refontification of the current line.
2377 ;; 2009-07-28: When `c-state-point-min' and the last position where
2378 ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2379 ;; both in the same literal, there is no such "good position", and
2380 ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2381 ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2383 ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2384 ;; the middle of the desert, as long as it is not within a brace pair
2385 ;; recorded in `c-state-cache' or a paren/bracket pair.
2387 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2388 ;; We maintain a simple cache of positions which aren't in a literal, so as to
2389 ;; speed up testing for non-literality.
2390 (defconst c-state-nonlit-pos-interval 3000)
2391 ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2393 (defvar c-state-nonlit-pos-cache nil)
2394 (make-variable-buffer-local 'c-state-nonlit-pos-cache)
2395 ;; A list of buffer positions which are known not to be in a literal or a cpp
2396 ;; construct. This is ordered with higher positions at the front of the list.
2397 ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
2399 (defvar c-state-nonlit-pos-cache-limit 1)
2400 (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2401 ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2402 ;; reduced by buffer changes, and increased by invocations of
2403 ;; `c-state-literal-at'.
2405 (defvar c-state-semi-nonlit-pos-cache nil)
2406 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache)
2407 ;; A list of elements which are either buffer positions (when such positions
2408 ;; are not in literals) or lists of the form (POS TYPE START), where POS is
2409 ;; a buffer position inside a literal, TYPE is the type of the literal
2410 ;; ('string, 'c, or 'c++) and START is the start of the literal.
2412 (defvar c-state-semi-nonlit-pos-cache-limit 1)
2413 (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache-limit)
2414 ;; An upper limit on valid entries in `c-state-semi-nonlit-pos-cache'. This
2415 ;; is reduced by buffer changes, and increased by invocations of
2416 ;; `c-parse-ps-state-below'.
2418 (defsubst c-truncate-semi-nonlit-pos-cache (pos)
2419 ;; Truncate the upper bound of the cache `c-state-semi-nonlit-pos-cache' to
2420 ;; POS, if it is higher than that position.
2421 (setq c-state-semi-nonlit-pos-cache-limit
2422 (min c-state-semi-nonlit-pos-cache-limit pos)))
2424 (defun c-state-semi-pp-to-literal (here &optional not-in-delimiter)
2425 ;; Do a parse-partial-sexp from a position in the buffer before HERE which
2426 ;; isn't in a literal, and return information about HERE, either:
2427 ;; (STATE TYPE BEG) if HERE is in a literal; or
2428 ;; (STATE) otherwise,
2429 ;; where STATE is the parsing state at HERE, TYPE is the type of the literal
2430 ;; enclosing HERE, (one of 'string, 'c, 'c++) and BEG is the starting
2431 ;; position of that literal (including the delimiter).
2433 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2434 ;; comment opener, this is recognized as being in a comment literal.
2436 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), 7
2437 ;; (comment type), and 8 (start of comment/string), and possibly 10 (in
2438 ;; newer Emacsen only, the syntax of a position after a potential first char
2439 ;; of a two char construct) of STATE are valid.
2440 (save-excursion
2441 (save-restriction
2442 (widen)
2443 (save-match-data
2444 (let* ((base-and-state (c-parse-ps-state-below here))
2445 (base (car base-and-state))
2446 (s (cdr base-and-state))
2447 (s (parse-partial-sexp base here nil nil s))
2449 (cond
2450 ((or (nth 3 s)
2451 (and (nth 4 s)
2452 (not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
2453 (setq ty (cond
2454 ((nth 3 s) 'string)
2455 ((nth 7 s) 'c++)
2456 (t 'c)))
2457 (list s ty (nth 8 s)))
2459 ((and (not not-in-delimiter) ; inside a comment starter
2460 (not (bobp))
2461 (progn (backward-char)
2462 (and (not (and (memq 'category-properties c-emacs-features)
2463 (looking-at "\\s!")))
2464 (looking-at c-comment-start-regexp))))
2465 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++))
2466 (list s ty (point)))
2468 (t (list s))))))))
2470 (defun c-state-full-pp-to-literal (here &optional not-in-delimiter)
2471 ;; This function will supersede c-state-pp-to-literal.
2473 ;; Do a parse-partial-sexp from a position in the buffer before HERE which
2474 ;; isn't in a literal, and return information about HERE, either:
2475 ;; (STATE TYPE (BEG . END)) if HERE is in a literal; or
2476 ;; (STATE) otherwise,
2477 ;; where STATE is the parsing state at HERE, TYPE is the type of the literal
2478 ;; enclosing HERE, (one of 'string, 'c, 'c++) and (BEG . END) is the
2479 ;; boundaries of that literal (including the delimiters).
2481 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2482 ;; comment opener, this is recognized as being in a comment literal.
2484 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), 7
2485 ;; (comment type), and 8 (start of comment/string), and possibly 10 (in
2486 ;; newer Emacsen only, the syntax of a position after a potential first char
2487 ;; of a two char construct) of STATE are valid.
2488 (save-excursion
2489 (save-restriction
2490 (widen)
2491 (save-match-data
2492 (let* ((base-and-state (c-parse-ps-state-below here))
2493 (base (car base-and-state))
2494 (s (cdr base-and-state))
2495 (s (parse-partial-sexp base here nil nil s))
2496 ty start)
2497 (cond
2498 ((or (nth 3 s)
2499 (and (nth 4 s)
2500 (not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
2501 (setq ty (cond
2502 ((nth 3 s) 'string)
2503 ((nth 7 s) 'c++)
2504 (t 'c)))
2505 (setq start (nth 8 s))
2506 (parse-partial-sexp here (point-max)
2507 nil ; TARGETDEPTH
2508 nil ; STOPBEFORE
2509 s ; OLDSTATE
2510 'syntax-table) ; stop at end of literal
2511 (list s ty (cons start (point))))
2513 ((and (not not-in-delimiter) ; inside a comment starter
2514 (not (bobp))
2515 (progn (backward-char)
2516 (and (not (and (memq 'category-properties c-emacs-features)
2517 (looking-at "\\s!")))
2518 (looking-at c-comment-start-regexp))))
2519 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
2520 start (point))
2521 (forward-comment 1)
2522 (list s ty (cons start (point))))
2524 (t (list s))))))))
2526 (defun c-state-pp-to-literal (from to &optional not-in-delimiter)
2527 ;; Do a parse-partial-sexp from FROM to TO, returning either
2528 ;; (STATE TYPE (BEG . END)) if TO is in a literal; or
2529 ;; (STATE) otherwise,
2530 ;; where STATE is the parsing state at TO, TYPE is the type of the literal
2531 ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal,
2532 ;; including the delimiters.
2534 ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
2535 ;; comment opener, this is recognized as being in a comment literal.
2537 ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
2538 ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
2539 ;; STATE are valid.
2540 (save-excursion
2541 (save-match-data
2542 (let ((s (parse-partial-sexp from to))
2543 ty co-st)
2544 (cond
2545 ((or (nth 3 s)
2546 (and (nth 4 s)
2547 (not (eq (nth 7 s) 'syntax-table)))) ; in a string or comment
2548 (setq ty (cond
2549 ((nth 3 s) 'string)
2550 ((nth 7 s) 'c++)
2551 (t 'c)))
2552 (parse-partial-sexp (point) (point-max)
2553 nil ; TARGETDEPTH
2554 nil ; STOPBEFORE
2555 s ; OLDSTATE
2556 'syntax-table) ; stop at end of literal
2557 `(,s ,ty (,(nth 8 s) . ,(point))))
2559 ((and (not not-in-delimiter) ; inside a comment starter
2560 (not (bobp))
2561 (progn (backward-char)
2562 (and (not (looking-at "\\s!"))
2563 (looking-at c-comment-start-regexp))))
2564 (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
2565 co-st (point))
2566 (forward-comment 1)
2567 `(,s ,ty (,co-st . ,(point))))
2569 (t `(,s)))))))
2571 (defun c-cache-to-parse-ps-state (elt)
2572 ;; Create a list suitable to use as the old-state parameter to
2573 ;; `parse-partial-sexp', out of ELT, a member of
2574 ;; `c-state-semi-nonlit-pos-cache'. ELT is either just a number, or a list
2575 ;; with 2, 3, or 4 members (See `c-parse-ps-state-to-cache'). That number
2576 ;; or the car of the list is the "position element" of ELT, the position
2577 ;; where ELT is valid.
2579 ;; POINT is left at the position for which the returned state is valid. It
2580 ;; will be either the position element of ELT, or one character before
2581 ;; that. (The latter happens in Emacs <= 25 and XEmacs, when ELT indicates
2582 ;; its position element directly follows a potential first character of a
2583 ;; two char construct (such as a comment opener or an escaped character).)
2584 (if (and (consp elt) (>= (length elt) 3))
2585 ;; Inside a string or comment
2586 (let ((depth 0) (containing nil) (last nil)
2587 in-string in-comment (after-quote nil)
2588 (min-depth 0) com-style com-str-start (intermediate nil)
2589 (char-1 (nth 3 elt)) ; first char of poss. 2-char construct
2590 (pos (car elt))
2591 (type (cadr elt)))
2592 (setq com-str-start (car (cddr elt)))
2593 (cond
2594 ((or (numberp type) (eq type t)) ; A string
2595 (setq in-string type))
2596 ((memq type '(c c++)) ; A comment
2597 (setq in-comment t
2598 com-style (if (eq type 'c++) 1 nil)))
2599 (t (c-benign-error "Invalid type %s in c-cache-to-parse-ps-state"
2600 elt)))
2601 (if (memq 'pps-extended-state c-emacs-features)
2602 (progn
2603 (goto-char pos)
2604 (list depth containing last
2605 in-string in-comment after-quote
2606 min-depth com-style com-str-start
2607 intermediate char-1))
2608 (goto-char (if char-1
2609 (1- pos)
2610 pos))
2611 (list depth containing last
2612 in-string in-comment nil
2613 min-depth com-style com-str-start
2614 intermediate)))
2616 ;; Not in a string or comment.
2617 (if (memq 'pps-extended-state c-emacs-features)
2618 (progn
2619 (goto-char (if (consp elt) (car elt) elt))
2620 (list 0 nil nil nil nil
2621 (and (consp elt) (eq (nth 1 elt) 9)) ; 9 is syntax code for "escape".
2622 0 nil nil nil
2623 (and (consp elt) (nth 1 elt))))
2624 (goto-char (if (consp elt) (car elt) elt))
2625 (if (and (consp elt) (cdr elt)) (backward-char))
2626 (copy-tree '(0 nil nil nil nil
2628 0 nil nil nil)))))
2630 (defun c-parse-ps-state-to-cache (state)
2631 ;; Convert STATE, a `parse-partial-sexp' state valid at POINT, to an element
2632 ;; for the `c-state-semi-nonlit-pos-cache' cache. This is one of
2633 ;; o - POINT (when point is not in a literal);
2634 ;; o - (POINT CHAR-1) (when the last character before point is potentially
2635 ;; the first of a two-character construct
2636 ;; o - (POINT TYPE STARTING-POS) (when in a literal);
2637 ;; o - (POINT TYPE STARTING-POS CHAR-1) (Combination of the previous two),
2638 ;; where TYPE is the type of the literal (either 'c, or 'c++, or the
2639 ;; character which closes the string), STARTING-POS is the starting
2640 ;; position of the comment or string. CHAR-1 is either the character
2641 ;; potentially forming the first half of a two-char construct (in Emacs <=
2642 ;; 25 and XEmacs) or the syntax of the character (in Emacs >= 26).
2643 (if (memq 'pps-extended-state c-emacs-features)
2644 ;; Emacs >= 26.
2645 (let ((basic
2646 (cond
2647 ((nth 3 state) ; A string
2648 (list (point) (nth 3 state) (nth 8 state)))
2649 ((and (nth 4 state) ; A comment
2650 (not (eq (nth 7 state) 'syntax-table))) ; but not a pseudo comment.
2651 (list (point)
2652 (if (eq (nth 7 state) 1) 'c++ 'c)
2653 (nth 8 state)))
2654 (t ; Neither string nor comment.
2655 (point)))))
2656 (if (nth 10 state)
2657 (append (if (consp basic)
2658 basic
2659 (list basic))
2660 (list (nth 10 state)))
2661 basic))
2663 ;; Emacs <= 25, XEmacs.
2664 (cond
2665 ((nth 3 state) ; A string
2666 (if (eq (char-before) ?\\)
2667 (list (point) (nth 3 state) (nth 8 state) ?\\)
2668 (list (point) (nth 3 state) (nth 8 state))))
2669 ((and (nth 4 state) ; comment
2670 (not (eq (nth 7 state) 'syntax-table)))
2671 (if (and (eq (char-before) ?*)
2672 (> (- (point) (nth 8 state)) 2)) ; not "/*/".
2673 (list (point)
2674 (if (eq (nth 7 state) 1) 'c++ 'c)
2675 (nth 8 state)
2677 (list (point)
2678 (if (eq (nth 7 state) 1) 'c++ 'c)
2679 (nth 8 state))))
2680 (t (if (memq (char-before) '(?/ ?\\))
2681 (list (point) (char-before))
2682 (point))))))
2684 (defsubst c-ps-state-cache-pos (elt)
2685 ;; Get the buffer position from ELT, an element from the cache
2686 ;; `c-state-semi-nonlit-pos-cache'.
2687 (if (atom elt)
2689 (car elt)))
2691 (defun c-parse-ps-state-below (here)
2692 ;; Given a buffer position HERE, Return a cons (CACHE-POS . STATE), where
2693 ;; CACHE-POS is a position not very far before HERE for which the
2694 ;; parse-partial-sexp STATE is valid. Note that the only valid elements of
2695 ;; STATE are those concerning comments and strings; STATE is the state of a
2696 ;; null `parse-partial-sexp' scan when CACHE-POS is not in a comment or
2697 ;; string.
2698 (save-excursion
2699 (save-restriction
2700 (widen)
2701 (let ((c c-state-semi-nonlit-pos-cache)
2702 elt state npos high-elt)
2703 ;; Trim the cache to take account of buffer changes.
2704 (while (and c (> (c-ps-state-cache-pos (car c))
2705 c-state-semi-nonlit-pos-cache-limit))
2706 (setq c (cdr c)))
2707 (setq c-state-semi-nonlit-pos-cache c)
2709 (while (and c (> (c-ps-state-cache-pos (car c)) here))
2710 (setq high-elt (car c))
2711 (setq c (cdr c)))
2712 (goto-char (or (and c (c-ps-state-cache-pos (car c)))
2713 (point-min)))
2714 (setq state
2715 (if c
2716 (c-cache-to-parse-ps-state (car c))
2717 (copy-tree '(0 nil nil nil nil nil 0 nil nil nil nil))))
2719 (when (not high-elt)
2720 ;; We need to extend the cache. Add an element to
2721 ;; `c-state-semi-nonlit-pos-cache' each iteration of the following.
2722 (while
2723 (<= (setq npos (+ (point) c-state-nonlit-pos-interval)) here)
2724 (setq state (parse-partial-sexp (point) npos nil nil state))
2725 (setq elt (c-parse-ps-state-to-cache state))
2726 (setq c-state-semi-nonlit-pos-cache
2727 (cons elt c-state-semi-nonlit-pos-cache))))
2729 (if (> (point) c-state-semi-nonlit-pos-cache-limit)
2730 (setq c-state-semi-nonlit-pos-cache-limit (point)))
2732 (cons (point) state)))))
2734 (defun c-state-safe-place (here)
2735 ;; Return a buffer position before HERE which is "safe", i.e. outside any
2736 ;; string, comment, or macro.
2738 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2739 ;; MAY NOT contain any positions within macros, since macros are frequently
2740 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2741 ;; We cannot rely on this mechanism whilst determining a cache pos since
2742 ;; this function is also called from outwith `c-parse-state'.
2743 (save-restriction
2744 (widen)
2745 (save-excursion
2746 (let ((c c-state-nonlit-pos-cache)
2747 pos npos high-pos lit macro-beg macro-end)
2748 ;; Trim the cache to take account of buffer changes.
2749 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2750 (setq c (cdr c)))
2751 (setq c-state-nonlit-pos-cache c)
2753 (while (and c (> (car c) here))
2754 (setq high-pos (car c))
2755 (setq c (cdr c)))
2756 (setq pos (or (car c) (point-min)))
2758 (unless high-pos
2759 (while
2760 ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
2761 (and
2762 (setq npos
2763 (when (<= (+ pos c-state-nonlit-pos-interval) here)
2764 (+ pos c-state-nonlit-pos-interval)))
2766 ;; Test for being in a literal. If so, go to after it.
2767 (progn
2768 (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
2769 (or (null lit)
2770 (prog1 (<= (cdr lit) here)
2771 (setq npos (cdr lit)))))
2773 ;; Test for being in a macro. If so, go to after it.
2774 (progn
2775 (goto-char npos)
2776 (setq macro-beg
2777 (and (c-beginning-of-macro) (/= (point) npos) (point)))
2778 (when macro-beg
2779 (c-syntactic-end-of-macro)
2780 (or (eobp) (forward-char))
2781 (setq macro-end (point)))
2782 (or (null macro-beg)
2783 (prog1 (<= macro-end here)
2784 (setq npos macro-end)))))
2786 (setq pos npos)
2787 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2788 ;; Add one extra element above HERE so as to avoid the previous
2789 ;; expensive calculation when the next call is close to the current
2790 ;; one. This is especially useful when inside a large macro.
2791 (when npos
2792 (setq c-state-nonlit-pos-cache
2793 (cons npos c-state-nonlit-pos-cache))))
2795 (if (> pos c-state-nonlit-pos-cache-limit)
2796 (setq c-state-nonlit-pos-cache-limit pos))
2797 pos))))
2799 (defun c-state-literal-at (here)
2800 ;; If position HERE is inside a literal, return (START . END), the
2801 ;; boundaries of the literal (which may be outside the accessible bit of the
2802 ;; buffer). Otherwise, return nil.
2804 ;; This function is almost the same as `c-literal-limits'. Previously, it
2805 ;; differed in that it was a lower level function, and that it rigorously
2806 ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
2807 ;; virtually identical to this function.
2808 (save-restriction
2809 (widen)
2810 (save-excursion
2811 (let ((pos (c-state-safe-place here)))
2812 (car (cddr (c-state-pp-to-literal pos here)))))))
2814 (defsubst c-state-lit-beg (pos)
2815 ;; Return the start of the literal containing POS, or POS itself.
2816 (or (car (c-state-literal-at pos))
2817 pos))
2819 (defsubst c-state-cache-non-literal-place (pos state)
2820 ;; Return a position outside of a string/comment/macro at or before POS.
2821 ;; STATE is the parse-partial-sexp state at POS.
2822 (let ((res (if (or (nth 3 state) ; in a string?
2823 (and (nth 4 state)
2824 (not (eq (nth 7 state) 'syntax-table)))) ; in a comment?
2825 (nth 8 state)
2826 pos)))
2827 (save-excursion
2828 (goto-char res)
2829 (if (c-beginning-of-macro)
2830 (point)
2831 res))))
2833 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2834 ;; Stuff to do with point-min, and coping with any literal there.
2835 (defvar c-state-point-min 1)
2836 (make-variable-buffer-local 'c-state-point-min)
2837 ;; This is (point-min) when `c-state-cache' was last calculated. A change of
2838 ;; narrowing is likely to affect the parens that are visible before the point.
2840 (defvar c-state-point-min-lit-type nil)
2841 (make-variable-buffer-local 'c-state-point-min-lit-type)
2842 (defvar c-state-point-min-lit-start nil)
2843 (make-variable-buffer-local 'c-state-point-min-lit-start)
2844 ;; These two variables define the literal, if any, containing point-min.
2845 ;; Their values are, respectively, 'string, c, or c++, and the start of the
2846 ;; literal. If there's no literal there, they're both nil.
2848 (defvar c-state-min-scan-pos 1)
2849 (make-variable-buffer-local 'c-state-min-scan-pos)
2850 ;; This is the earliest buffer-pos from which scanning can be done. It is
2851 ;; either the end of the literal containing point-min, or point-min itself.
2852 ;; It becomes nil if the buffer is changed earlier than this point.
2853 (defun c-state-get-min-scan-pos ()
2854 ;; Return the lowest valid scanning pos. This will be the end of the
2855 ;; literal enclosing point-min, or point-min itself.
2856 (or c-state-min-scan-pos
2857 (save-restriction
2858 (save-excursion
2859 (widen)
2860 (goto-char c-state-point-min-lit-start)
2861 (if (eq c-state-point-min-lit-type 'string)
2862 (forward-sexp)
2863 (forward-comment 1))
2864 (setq c-state-min-scan-pos (point))))))
2866 (defun c-state-mark-point-min-literal ()
2867 ;; Determine the properties of any literal containing POINT-MIN, setting the
2868 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2869 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2870 (let ((p-min (point-min))
2871 lit)
2872 (save-restriction
2873 (widen)
2874 (setq lit (c-state-literal-at p-min))
2875 (if lit
2876 (setq c-state-point-min-lit-type
2877 (save-excursion
2878 (goto-char (car lit))
2879 (cond
2880 ((looking-at c-block-comment-start-regexp) 'c)
2881 ((looking-at c-line-comment-starter) 'c++)
2882 (t 'string)))
2883 c-state-point-min-lit-start (car lit)
2884 c-state-min-scan-pos (cdr lit))
2885 (setq c-state-point-min-lit-type nil
2886 c-state-point-min-lit-start nil
2887 c-state-min-scan-pos p-min)))))
2890 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2891 ;; A variable which signals a brace dessert - helpful for reducing the number
2892 ;; of fruitless backward scans.
2893 (defvar c-state-brace-pair-desert nil)
2894 (make-variable-buffer-local 'c-state-brace-pair-desert)
2895 ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
2896 ;; that defun has searched backwards for a brace pair and not found one. Its
2897 ;; value is either nil or a cons (PA . FROM), where PA is the position of the
2898 ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2899 ;; nil when at top level) and FROM is where the backward search started. It
2900 ;; is reset to nil in `c-invalidate-state-cache'.
2903 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2904 ;; Lowish level functions/macros which work directly on `c-state-cache', or a
2905 ;; list of like structure.
2906 (defmacro c-state-cache-top-lparen (&optional cache)
2907 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2908 ;; (default `c-state-cache') (or nil).
2909 (let ((cash (or cache 'c-state-cache)))
2910 `(if (consp (car ,cash))
2911 (caar ,cash)
2912 (car ,cash))))
2914 (defmacro c-state-cache-top-paren (&optional cache)
2915 ;; Return the address of the latest brace/bracket/paren (whether left or
2916 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2917 (let ((cash (or cache 'c-state-cache)))
2918 `(if (consp (car ,cash))
2919 (cdar ,cash)
2920 (car ,cash))))
2922 (defmacro c-state-cache-after-top-paren (&optional cache)
2923 ;; Return the position just after the latest brace/bracket/paren (whether
2924 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2925 (let ((cash (or cache 'c-state-cache)))
2926 `(if (consp (car ,cash))
2927 (cdar ,cash)
2928 (and (car ,cash)
2929 (1+ (car ,cash))))))
2931 (defun c-get-cache-scan-pos (here)
2932 ;; From the state-cache, determine the buffer position from which we might
2933 ;; scan forward to HERE to update this cache. This position will be just
2934 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2935 ;; return the earliest position in the accessible region which isn't within
2936 ;; a literal. If the visible portion of the buffer is entirely within a
2937 ;; literal, return NIL.
2938 (let ((c c-state-cache) elt)
2939 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2940 (while (and c
2941 (>= (c-state-cache-top-lparen c) here))
2942 (setq c (cdr c)))
2944 (setq elt (car c))
2945 (cond
2946 ((consp elt)
2947 (if (> (cdr elt) here)
2948 (1+ (car elt))
2949 (cdr elt)))
2950 (elt (1+ elt))
2951 ((<= (c-state-get-min-scan-pos) here)
2952 (c-state-get-min-scan-pos))
2953 (t nil))))
2955 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2956 ;; Variables which keep track of preprocessor constructs.
2957 (defvar c-state-old-cpp-beg-marker nil)
2958 (make-variable-buffer-local 'c-state-old-cpp-beg-marker)
2959 (defvar c-state-old-cpp-beg nil)
2960 (make-variable-buffer-local 'c-state-old-cpp-beg)
2961 (defvar c-state-old-cpp-end-marker nil)
2962 (make-variable-buffer-local 'c-state-old-cpp-end-marker)
2963 (defvar c-state-old-cpp-end nil)
2964 (make-variable-buffer-local 'c-state-old-cpp-end)
2965 ;; These are the limits of the macro containing point at the previous call of
2966 ;; `c-parse-state', or nil.
2968 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2969 ;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
2970 (defun c-get-fallback-scan-pos (here)
2971 ;; Return a start position for building `c-state-cache' from
2972 ;; scratch. This will be at the top level, 2 defuns back.
2973 (save-excursion
2974 ;; Go back 2 bods, but ignore any bogus positions returned by
2975 ;; beginning-of-defun (i.e. open paren in column zero).
2976 (goto-char here)
2977 (let ((cnt 2))
2978 (while (not (or (bobp) (zerop cnt)))
2979 (c-beginning-of-defun-1) ; Pure elisp BOD.
2980 (if (eq (char-after) ?\{)
2981 (setq cnt (1- cnt)))))
2982 (point)))
2984 (defun c-state-balance-parens-backwards (here- here+ top)
2985 ;; Return the position of the opening paren/brace/bracket before HERE- which
2986 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2987 ;; there's a macro, HERE- and HERE+ are the same. Like this:
2989 ;; ............................................
2990 ;; | |
2991 ;; ( [ ( .........#macro.. ) ( ) ] )
2992 ;; ^ ^ ^ ^
2993 ;; | | | |
2994 ;; return HERE- HERE+ TOP
2996 ;; If there aren't enough opening paren/brace/brackets, return the position
2997 ;; of the outermost one found, or HERE- if there are none. If there are no
2998 ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2999 ;; must not be inside literals. Only the accessible portion of the buffer
3000 ;; will be scanned.
3002 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
3003 ;; `here'. Go round the next loop each time we pass over such a ")". These
3004 ;; probably match "("s before `here-'.
3005 (let (pos pa ren+1 lonely-rens)
3006 (save-excursion
3007 (save-restriction
3008 (narrow-to-region (point-min) top) ; This can move point, sometimes.
3009 (setq pos here+)
3010 (c-safe
3011 (while
3012 (setq ren+1 (c-sc-scan-lists pos 1 1)) ; might signal
3013 (setq lonely-rens (cons ren+1 lonely-rens)
3014 pos ren+1)))))
3016 ;; PART 2: Scan back before `here-' searching for the "("s
3017 ;; matching/mismatching the ")"s found above. We only need to direct the
3018 ;; caller to scan when we've encountered unmatched right parens.
3019 (setq pos here-)
3020 (when lonely-rens
3021 (c-safe
3022 (while
3023 (and lonely-rens ; actual values aren't used.
3024 (setq pa (c-sc-scan-lists pos -1 1)))
3025 (setq pos pa)
3026 (setq lonely-rens (cdr lonely-rens)))))
3027 pos))
3029 (defun c-parse-state-get-strategy (here good-pos)
3030 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
3031 ;; to minimize the amount of scanning. HERE is the pertinent position in
3032 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
3033 ;; its head trimmed) is known to be good, or nil if there is no such
3034 ;; position.
3036 ;; The return value is a list, one of the following:
3038 ;; o - ('forward START-POINT) - scan forward from START-POINT,
3039 ;; which is not less than the highest position in `c-state-cache' below HERE,
3040 ;; which is after GOOD-POS.
3041 ;; o - ('backward nil) - scan backwards (from HERE).
3042 ;; o - ('back-and-forward START-POINT) - like 'forward, but when HERE is earlier
3043 ;; than GOOD-POS.
3044 ;; o - ('BOD START-POINT) - scan forwards from START-POINT, which is at the
3045 ;; top level.
3046 ;; o - ('IN-LIT nil) - point is inside the literal containing point-min.
3047 (let* ((in-macro-start ; start of macro containing HERE or nil.
3048 (save-excursion
3049 (goto-char here)
3050 (and (c-beginning-of-macro)
3051 (point))))
3052 (changed-macro-start
3053 (and in-macro-start
3054 (not (and c-state-old-cpp-beg
3055 (= in-macro-start c-state-old-cpp-beg)))
3056 in-macro-start))
3057 (cache-pos (c-get-cache-scan-pos (if changed-macro-start
3058 (min changed-macro-start here)
3059 here))) ; highest suitable position in cache (or 1)
3060 BOD-pos ; position of 2nd BOD before HERE.
3061 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
3062 start-point
3063 how-far) ; putative scanning distance.
3064 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
3065 (cond
3066 ((< here (c-state-get-min-scan-pos))
3067 (setq strategy 'IN-LIT
3068 start-point nil
3069 cache-pos nil
3070 how-far 0))
3071 ((<= good-pos here)
3072 (setq strategy 'forward
3073 start-point (if changed-macro-start
3074 cache-pos
3075 (max good-pos cache-pos))
3076 how-far (- here start-point)))
3077 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
3078 (setq strategy 'backward
3079 how-far (- good-pos here)))
3081 (setq strategy 'back-and-forward
3082 start-point cache-pos
3083 how-far (- here start-point))))
3085 ;; Might we be better off starting from the top level, two defuns back,
3086 ;; instead? This heuristic no longer works well in C++, where
3087 ;; declarations inside namespace brace blocks are frequently placed at
3088 ;; column zero. (2015-11-10): Remove the condition on C++ Mode.
3089 (when (and (or (not (memq 'col-0-paren c-emacs-features))
3090 open-paren-in-column-0-is-defun-start)
3091 ;; (not (c-major-mode-is 'c++-mode))
3092 (> how-far c-state-cache-too-far))
3093 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
3094 (if (< (- here BOD-pos) how-far)
3095 (setq strategy 'BOD
3096 start-point BOD-pos)))
3098 (list strategy start-point)))
3101 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3102 ;; Routines which change `c-state-cache' and associated values.
3103 (defun c-renarrow-state-cache ()
3104 ;; The region (more precisely, point-min) has changed since we
3105 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
3106 (if (< (point-min) c-state-point-min)
3107 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
3108 ;; It would be possible to do a better job here and recalculate the top
3109 ;; only.
3110 (progn
3111 (c-state-mark-point-min-literal)
3112 (setq c-state-cache nil
3113 c-state-cache-good-pos c-state-min-scan-pos
3114 c-state-brace-pair-desert nil))
3116 ;; point-min has MOVED FORWARD.
3118 ;; Is the new point-min inside a (different) literal?
3119 (unless (and c-state-point-min-lit-start ; at prev. point-min
3120 (< (point-min) (c-state-get-min-scan-pos)))
3121 (c-state-mark-point-min-literal))
3123 ;; Cut off a bit of the tail from `c-state-cache'.
3124 (let ((ptr (cons nil c-state-cache))
3126 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
3127 (>= pa (point-min)))
3128 (setq ptr (cdr ptr)))
3130 (when (consp ptr)
3131 (if (or (eq (cdr ptr) c-state-cache)
3132 (and (consp (cadr ptr))
3133 (> (cdr (cadr ptr)) (point-min)))) ; Our new point-min is
3134 ; inside a recorded
3135 ; brace pair.
3136 (setq c-state-cache nil
3137 c-state-cache-good-pos c-state-min-scan-pos)
3138 (setcdr ptr nil)
3139 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
3142 (setq c-state-point-min (point-min)))
3144 (defun c-append-lower-brace-pair-to-state-cache (from here &optional upper-lim)
3145 ;; If there is a brace pair preceding FROM in the buffer, at the same level
3146 ;; of nesting (not necessarily immediately preceding), push a cons onto
3147 ;; `c-state-cache' to represent it. FROM must not be inside a literal. If
3148 ;; UPPER-LIM is non-nil, we append the highest brace pair whose "}" is below
3149 ;; UPPER-LIM.
3151 ;; Return non-nil when this has been done.
3153 ;; The situation it copes with is this transformation:
3155 ;; OLD: { (.) {...........}
3156 ;; ^ ^
3157 ;; FROM HERE
3159 ;; NEW: { {....} (.) {.........
3160 ;; ^ ^ ^
3161 ;; LOWER BRACE PAIR HERE or HERE
3163 ;; This routine should be fast. Since it can get called a LOT, we maintain
3164 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
3165 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
3166 (save-excursion
3167 (save-restriction
3168 (let* (new-cons
3169 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
3170 (macro-start-or-from
3171 (progn (goto-char from)
3172 (c-beginning-of-macro)
3173 (point)))
3174 (bra ; Position of "{".
3175 ;; Don't start scanning in the middle of a CPP construct unless
3176 ;; it contains HERE - these constructs, in Emacs, are "commented
3177 ;; out" with category properties.
3178 (if (eq (c-get-char-property macro-start-or-from 'category)
3179 'c-cpp-delimiter)
3180 macro-start-or-from
3181 from))
3182 ce) ; Position of "}"
3183 (or upper-lim (setq upper-lim from))
3185 ;; If we're essentially repeating a fruitless search, just give up.
3186 (unless (and c-state-brace-pair-desert
3187 (eq cache-pos (car c-state-brace-pair-desert))
3188 (or (null (car c-state-brace-pair-desert))
3189 (> from (car c-state-brace-pair-desert)))
3190 (<= from (cdr c-state-brace-pair-desert)))
3191 ;; DESERT-LIM. Avoid repeated searching through the cached desert.
3192 (let ((desert-lim
3193 (and c-state-brace-pair-desert
3194 (eq cache-pos (car c-state-brace-pair-desert))
3195 (>= from (cdr c-state-brace-pair-desert))
3196 (cdr c-state-brace-pair-desert)))
3197 ;; CACHE-LIM. This limit will be necessary when an opening
3198 ;; paren at `cache-pos' has just had its matching close paren
3199 ;; inserted into the buffer. `cache-pos' continues to be a
3200 ;; search bound, even though the algorithm below would skip
3201 ;; over the new paren pair.
3202 (cache-lim (and cache-pos (< cache-pos from) cache-pos)))
3203 (narrow-to-region
3204 (cond
3205 ((and desert-lim cache-lim)
3206 (max desert-lim cache-lim))
3207 (desert-lim)
3208 (cache-lim)
3209 ((point-min)))
3210 ;; The top limit is EOB to ensure that `bra' is inside the
3211 ;; accessible part of the buffer at the next scan operation.
3212 (1+ (buffer-size))))
3214 ;; In the next pair of nested loops, the inner one moves back past a
3215 ;; pair of (mis-)matching parens or brackets; the outer one moves
3216 ;; back over a sequence of unmatched close brace/paren/bracket each
3217 ;; time round.
3218 (while
3219 (progn
3220 (c-safe
3221 (while
3222 (and (setq ce (c-sc-scan-lists bra -1 -1)) ; back past )/]/}; might signal
3223 (setq bra (c-sc-scan-lists ce -1 1)) ; back past (/[/{; might signal
3224 (or (> bra here) ;(> ce here)
3225 (and
3226 (< ce here)
3227 (or (not (eq (char-after bra) ?\{))
3228 (and (goto-char bra)
3229 (c-beginning-of-macro)
3230 (< (point) macro-start-or-from))))))))
3231 (and ce (< ce bra)))
3232 (setq bra ce)) ; If we just backed over an unbalanced closing
3233 ; brace, ignore it.
3235 (if (and ce (< ce here) (< bra ce) (eq (char-after bra) ?\{))
3236 ;; We've found the desired brace-pair.
3237 (progn
3238 (setq new-cons (cons bra (1+ ce)))
3239 (cond
3240 ((consp (car c-state-cache))
3241 (setcar c-state-cache new-cons))
3242 ((and (numberp (car c-state-cache)) ; probably never happens
3243 (< ce (car c-state-cache)))
3244 (setcdr c-state-cache
3245 (cons new-cons (cdr c-state-cache))))
3246 (t (setq c-state-cache (cons new-cons c-state-cache)))))
3248 ;; We haven't found a brace pair. Record this in the cache.
3249 (setq c-state-brace-pair-desert
3250 (cons (if (and ce (< bra ce) (> ce here)) ; {..} straddling HERE?
3252 (point-min))
3253 (min here from)))))))))
3255 (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
3256 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
3257 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
3258 ;; "push" "a" brace pair onto `c-state-cache'.
3260 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
3261 ;; otherwise push it normally.
3263 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
3264 ;; latter is inside a macro, not being a macro containing
3265 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
3266 ;; base pair. This latter case is assumed to be rare.
3268 ;; Note: POINT is not preserved in this routine.
3269 (if bra+1
3270 (if (or (> bra+1 macro-start-or-here)
3271 (progn (goto-char bra+1)
3272 (not (c-beginning-of-macro))))
3273 (setq c-state-cache
3274 (cons (cons (1- bra+1)
3275 (c-sc-scan-lists bra+1 1 1))
3276 (if (consp (car c-state-cache))
3277 (cdr c-state-cache)
3278 c-state-cache)))
3279 ;; N.B. This defsubst codes one method for the simple, normal case,
3280 ;; and a more sophisticated, slower way for the general case. Don't
3281 ;; eliminate this defsubst - it's a speed optimization.
3282 (c-append-lower-brace-pair-to-state-cache (1- bra+1) (point-max)))))
3284 (defun c-append-to-state-cache (from here)
3285 ;; Scan the buffer from FROM to HERE, adding elements into `c-state-cache'
3286 ;; for braces etc. Return a candidate for `c-state-cache-good-pos'.
3288 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
3289 ;; any. Typically, it is immediately after it. It must not be inside a
3290 ;; literal.
3291 (let ((here-bol (c-point 'bol here))
3292 (macro-start-or-here
3293 (save-excursion (goto-char here)
3294 (if (c-beginning-of-macro)
3295 (point)
3296 here)))
3297 pa+1 ; pos just after an opening PAren (or brace).
3298 (ren+1 from) ; usually a pos just after a closing paREN etc.
3299 ; Is actually the pos. to scan for a (/{/[ from,
3300 ; which sometimes is after a silly )/}/].
3301 paren+1 ; Pos after some opening or closing paren.
3302 paren+1s ; A list of `paren+1's; used to determine a
3303 ; good-pos.
3304 bra+1 ; just after L bra-ce.
3305 mstart) ; start of a macro.
3307 (save-excursion
3308 (save-restriction
3309 (narrow-to-region (point-min) here)
3310 ;; Each time round the following loop, we enter a successively deeper
3311 ;; level of brace/paren nesting. (Except sometimes we "continue at
3312 ;; the existing level".) `pa+1' is a pos inside an opening
3313 ;; brace/paren/bracket, usually just after it.
3314 (while
3315 (progn
3316 ;; Each time round the next loop moves forward over an opening then
3317 ;; a closing brace/bracket/paren. This loop is white hot, so it
3318 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
3319 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
3320 ;; call of `scan-lists' signals an error, which happens when there
3321 ;; are no more b/b/p's to scan.
3322 (c-safe
3323 (while t
3324 (setq pa+1 (c-sc-scan-lists ren+1 1 -1) ; Into (/{/[; might signal
3325 paren+1s (cons pa+1 paren+1s))
3326 (setq ren+1 (c-sc-scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
3327 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
3328 (setq bra+1 pa+1))
3329 (setcar paren+1s ren+1)))
3331 (if (and pa+1 (> pa+1 ren+1))
3332 ;; We've just entered a deeper nesting level.
3333 (progn
3334 ;; Insert the brace pair (if present) and the single open
3335 ;; paren/brace/bracket into `c-state-cache' It cannot be
3336 ;; inside a macro, except one around point, because of what
3337 ;; `c-neutralize-syntax-in-CPP' has done.
3338 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
3339 ;; Insert the opening brace/bracket/paren position.
3340 (setq c-state-cache (cons (1- pa+1) c-state-cache))
3341 ;; Clear admin stuff for the next more nested part of the scan.
3342 (setq ren+1 pa+1 pa+1 nil bra+1 nil)
3343 t) ; Carry on the loop
3345 ;; All open p/b/b's at this nesting level, if any, have probably
3346 ;; been closed by matching/mismatching ones. We're probably
3347 ;; finished - we just need to check for having found an
3348 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
3349 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
3350 (c-safe (setq ren+1 (c-sc-scan-lists ren+1 1 1)))))) ; acts as loop control.
3352 ;; Record the final, innermost, brace-pair if there is one.
3353 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
3355 ;; Determine a good pos
3356 (while (and (setq paren+1 (car paren+1s))
3357 (> (if (> paren+1 macro-start-or-here)
3358 paren+1
3359 (goto-char paren+1)
3360 (setq mstart (and (c-beginning-of-macro)
3361 (point)))
3362 (or mstart paren+1))
3363 here-bol))
3364 (setq paren+1s (cdr paren+1s)))
3365 (cond
3366 ((and paren+1 mstart)
3367 (min paren+1 mstart))
3368 (paren+1)
3369 (t from))))))
3371 (defun c-remove-stale-state-cache (start-point here pps-point)
3372 ;; Remove stale entries from the `c-cache-state', i.e. those which will
3373 ;; not be in it when it is amended for position HERE. This may involve
3374 ;; replacing a CONS element for a brace pair containing HERE with its car.
3375 ;; Additionally, the "outermost" open-brace entry before HERE will be
3376 ;; converted to a cons if the matching close-brace is below HERE.
3378 ;; START-POINT is a "maximal" "safe position" - there must be no open
3379 ;; parens/braces/brackets between START-POINT and HERE.
3381 ;; As a second thing, calculate the result of parse-partial-sexp at
3382 ;; PPS-POINT, w.r.t. START-POINT. The motivation here is that
3383 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
3384 ;; adjust it to get outside a string/comment. (Sorry about this! The code
3385 ;; needs to be FAST).
3387 ;; Return a list (GOOD-POS SCAN-BACK-POS CONS-SEPARATED PPS-STATE), where
3388 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
3389 ;; to be good (we aim for this to be as high as possible);
3390 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
3391 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
3392 ;; position to scan backwards from. It is the position of the "{" of the
3393 ;; last element to be removed from `c-state-cache', when that elt is a
3394 ;; cons, otherwise nil.
3395 ;; o - CONS-SEPARATED is t when a cons element in `c-state-cache' has been
3396 ;; replaced by its car because HERE lies inside the brace pair represented
3397 ;; by the cons.
3398 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
3399 (save-excursion
3400 (save-restriction
3401 (narrow-to-region 1 (point-max))
3402 (let* ((in-macro-start ; start of macro containing HERE or nil.
3403 (save-excursion
3404 (goto-char here)
3405 (and (c-beginning-of-macro)
3406 (point))))
3407 (start-point-actual-macro-start ; Start of macro containing
3408 ; start-point or nil
3409 (and (< start-point here)
3410 (save-excursion
3411 (goto-char start-point)
3412 (and (c-beginning-of-macro)
3413 (point)))))
3414 (start-point-actual-macro-end ; End of this macro, (maybe
3415 ; HERE), or nil.
3416 (and start-point-actual-macro-start
3417 (save-excursion
3418 (goto-char start-point-actual-macro-start)
3419 (c-end-of-macro)
3420 (point))))
3421 pps-state ; Will be 9 or 10 elements long.
3423 upper-lim ; ,beyond which `c-state-cache' entries are removed
3424 scan-back-pos
3425 cons-separated
3426 pair-beg target-depth)
3428 ;; Remove entries beyond HERE. Also remove any entries inside
3429 ;; a macro, unless HERE is in the same macro.
3430 (setq upper-lim
3431 (if (or (null c-state-old-cpp-beg)
3432 (and (> here c-state-old-cpp-beg)
3433 (< here c-state-old-cpp-end)))
3434 here
3435 (min here c-state-old-cpp-beg)))
3436 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
3437 (setq scan-back-pos (car-safe (car c-state-cache)))
3438 (setq c-state-cache (cdr c-state-cache)))
3440 ;; If `upper-lim' is inside the last recorded brace pair, remove its
3441 ;; RBrace and indicate we'll need to search backwards for a previous
3442 ;; brace pair.
3443 (when (and c-state-cache
3444 (consp (car c-state-cache))
3445 (> (cdar c-state-cache) upper-lim))
3446 (setcar c-state-cache (caar c-state-cache))
3447 (setq scan-back-pos (car c-state-cache)
3448 cons-separated t))
3450 ;; The next loop jumps forward out of a nested level of parens each
3451 ;; time round; the corresponding elements in `c-state-cache' are
3452 ;; removed. `pos' is just after the brace-pair or the open paren at
3453 ;; (car c-state-cache). There can be no open parens/braces/brackets
3454 ;; between `start-point'/`start-point-actual-macro-start' and HERE,
3455 ;; due to the interface spec to this function.
3456 (setq pos (if (and start-point-actual-macro-end
3457 (not (eq start-point-actual-macro-start
3458 in-macro-start)))
3459 (1+ start-point-actual-macro-end) ; get outside the macro as
3460 ; marked by a `category' text property.
3461 start-point))
3462 (goto-char pos)
3463 (while (and c-state-cache
3464 (or (numberp (car c-state-cache)) ; Have we a { at all?
3465 (cdr c-state-cache))
3466 (< (point) here))
3467 (cond
3468 ((null pps-state) ; first time through
3469 (setq target-depth -1))
3470 ((eq (car pps-state) target-depth) ; found closing ),},]
3471 (setq target-depth (1- (car pps-state))))
3472 ;; Do nothing when we've merely reached pps-point.
3475 ;; Scan!
3476 (setq pps-state
3477 (c-sc-parse-partial-sexp
3478 (point) (if (< (point) pps-point) pps-point here)
3479 target-depth
3480 nil pps-state))
3482 (when (eq (car pps-state) target-depth)
3483 (setq pos (point)) ; POS is now just after an R-paren/brace.
3484 (cond
3485 ((and (consp (car c-state-cache))
3486 (eq (point) (cdar c-state-cache)))
3487 ;; We've just moved out of the paren pair containing the brace-pair
3488 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
3489 ;; and is potentially where the open brace of a cons in
3490 ;; c-state-cache will be.
3491 (setq pair-beg (car-safe (cdr c-state-cache))
3492 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
3493 ((numberp (car c-state-cache))
3494 (setq pair-beg (car c-state-cache)
3495 c-state-cache (cdr c-state-cache))) ; remove this
3496 ; containing Lparen
3497 ((numberp (cadr c-state-cache))
3498 (setq pair-beg (cadr c-state-cache)
3499 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
3500 ; together with enclosed brace pair.
3501 ;; (t nil) ; Ignore an unmated Rparen.
3504 (if (< (point) pps-point)
3505 (setq pps-state (c-sc-parse-partial-sexp
3506 (point) pps-point
3507 nil nil ; TARGETDEPTH, STOPBEFORE
3508 pps-state)))
3510 ;; If the last paren pair we moved out of was actually a brace pair,
3511 ;; insert it into `c-state-cache'.
3512 (when (and pair-beg (eq (char-after pair-beg) ?{))
3513 (if (consp (car-safe c-state-cache))
3514 (setq c-state-cache (cdr c-state-cache)))
3515 (setq c-state-cache (cons (cons pair-beg pos)
3516 c-state-cache)))
3518 (list pos scan-back-pos cons-separated pps-state)))))
3520 (defun c-remove-stale-state-cache-backwards (here)
3521 ;; Strip stale elements of `c-state-cache' by moving backwards through the
3522 ;; buffer, and inform the caller of the scenario detected.
3524 ;; HERE is the position we're setting `c-state-cache' for.
3525 ;; CACHE-POS (a locally bound variable) is just after the latest recorded
3526 ;; position in `c-state-cache' before HERE, or a position at or near
3527 ;; point-min which isn't in a literal.
3529 ;; This function must only be called only when (> `c-state-cache-good-pos'
3530 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
3531 ;; optimized to eliminate (or minimize) scanning between these two
3532 ;; positions.
3534 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
3535 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
3536 ;; could become so after missing elements are inserted into
3537 ;; `c-state-cache'. This is JUST AFTER an opening or closing
3538 ;; brace/paren/bracket which is already in `c-state-cache' or just before
3539 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
3540 ;; before `here''s line, or the start of the literal containing it.
3541 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
3542 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
3543 ;; to scan backwards from.
3544 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
3545 ;; POS and HERE which aren't recorded in `c-state-cache'.
3547 ;; The comments in this defun use "paren" to mean parenthesis or square
3548 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
3550 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
3551 ;; | | | | | |
3552 ;; CP E here D C good
3553 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
3554 (pos c-state-cache-good-pos)
3555 pa ren ; positions of "(" and ")"
3556 dropped-cons ; whether the last element dropped from `c-state-cache'
3557 ; was a cons (representing a brace-pair)
3558 good-pos ; see above.
3559 lit ; (START . END) of a literal containing some point.
3560 here-lit-start here-lit-end ; bounds of literal containing `here'
3561 ; or `here' itself.
3562 here- here+ ; start/end of macro around HERE, or HERE
3563 (here-bol (c-point 'bol here))
3564 (too-far-back (max (- here c-state-cache-too-far) (point-min))))
3566 ;; Remove completely irrelevant entries from `c-state-cache'.
3567 (while (and c-state-cache
3568 (>= (setq pa (c-state-cache-top-lparen)) here))
3569 (setq dropped-cons (consp (car c-state-cache)))
3570 (setq c-state-cache (cdr c-state-cache))
3571 (setq pos pa))
3572 ;; At this stage, (>= pos here);
3573 ;; (< (c-state-cache-top-lparen) here) (or is nil).
3575 (cond
3576 ((and (consp (car c-state-cache))
3577 (> (cdar c-state-cache) here))
3578 ;; CASE 1: The top of the cache is a brace pair which now encloses
3579 ;; `here'. As good-pos, return the address of the "{". Since we've no
3580 ;; knowledge of what's inside these braces, we have no alternative but
3581 ;; to direct the caller to scan the buffer from the opening brace.
3582 (setq pos (caar c-state-cache))
3583 (setcar c-state-cache pos)
3584 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
3585 ; entry into a { entry, so the caller needs to
3586 ; search for a brace pair before the {.
3588 ;; `here' might be inside a literal. Check for this.
3589 ((progn
3590 (setq lit (c-state-literal-at here)
3591 here-lit-start (or (car lit) here)
3592 here-lit-end (or (cdr lit) here))
3593 ;; Has `here' just "newly entered" a macro?
3594 (save-excursion
3595 (goto-char here-lit-start)
3596 (if (and (c-beginning-of-macro)
3597 (or (null c-state-old-cpp-beg)
3598 (not (= (point) c-state-old-cpp-beg))))
3599 (progn
3600 (setq here- (point))
3601 (c-end-of-macro)
3602 (setq here+ (point)))
3603 (setq here- here-lit-start
3604 here+ here-lit-end)))
3606 ;; `here' might be nested inside any depth of parens (or brackets but
3607 ;; not braces). Scan backwards to find the outermost such opening
3608 ;; paren, if there is one. This will be the scan position to return.
3609 (save-restriction
3610 (narrow-to-region cache-pos (point-max))
3611 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
3612 nil)) ; for the cond
3614 ((< pos here-lit-start)
3615 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
3616 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
3617 ;; a brace pair preceding this, it will already be in `c-state-cache',
3618 ;; unless there was a brace pair after it, i.e. there'll only be one to
3619 ;; scan for if we've just deleted one.
3620 (list pos (and dropped-cons pos) t)) ; Return value.
3622 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
3623 ;; Further forward scanning isn't needed, but we still need to find a
3624 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
3625 ((progn
3626 (save-restriction
3627 (narrow-to-region here-bol (point-max))
3628 (setq pos here-lit-start)
3629 (c-safe (while (setq pa (c-sc-scan-lists pos -1 1))
3630 (setq pos pa)))) ; might signal
3631 nil)) ; for the cond
3633 ((save-restriction
3634 (narrow-to-region too-far-back (point-max))
3635 (setq ren (c-safe (c-sc-scan-lists pos -1 -1))))
3636 ;; CASE 3: After a }/)/] before `here''s BOL.
3637 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
3639 ((progn (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
3640 (>= cache-pos good-pos))
3641 ;; CASE 3.5: Just after an existing entry in `c-state-cache' on `here''s
3642 ;; line or the previous line.
3643 (list cache-pos nil nil))
3646 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
3647 ;; literal containing it.
3648 (list good-pos (and dropped-cons good-pos) nil)))))
3651 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3652 ;; Externally visible routines.
3654 (defun c-state-cache-init ()
3655 (setq c-state-cache nil
3656 c-state-cache-good-pos 1
3657 c-state-nonlit-pos-cache nil
3658 c-state-nonlit-pos-cache-limit 1
3659 c-state-semi-nonlit-pos-cache nil
3660 c-state-semi-nonlit-pos-cache-limit 1
3661 c-state-brace-pair-desert nil
3662 c-state-point-min 1
3663 c-state-point-min-lit-type nil
3664 c-state-point-min-lit-start nil
3665 c-state-min-scan-pos 1
3666 c-state-old-cpp-beg nil
3667 c-state-old-cpp-end nil)
3668 (c-state-mark-point-min-literal))
3670 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3671 ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
3672 ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
3673 ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
3674 ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
3675 ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
3676 ;; (defun c-state-dump ()
3677 ;; ;; For debugging.
3678 ;; ;(message
3679 ;; (concat
3680 ;; (c-sc-qde c-state-cache)
3681 ;; (c-sc-de c-state-cache-good-pos)
3682 ;; (c-sc-qde c-state-nonlit-pos-cache)
3683 ;; (c-sc-de c-state-nonlit-pos-cache-limit)
3684 ;; (c-sc-qde c-state-brace-pair-desert)
3685 ;; (c-sc-de c-state-point-min)
3686 ;; (c-sc-de c-state-point-min-lit-type)
3687 ;; (c-sc-de c-state-point-min-lit-start)
3688 ;; (c-sc-de c-state-min-scan-pos)
3689 ;; (c-sc-de c-state-old-cpp-beg)
3690 ;; (c-sc-de c-state-old-cpp-end)))
3691 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3693 (defun c-invalidate-state-cache-1 (here)
3694 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
3695 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
3696 ;; left in a consistent state.
3698 ;; This is much like `c-whack-state-after', but it never changes a paren
3699 ;; pair element into an open paren element. Doing that would mean that the
3700 ;; new open paren wouldn't have the required preceding paren pair element.
3702 ;; This function is called from c-before-change.
3704 ;; The caches of non-literals:
3705 ;; Note that we use "<=" for the possibility of the second char of a two-char
3706 ;; comment opener being typed; this would invalidate any cache position at
3707 ;; HERE.
3708 (if (<= here c-state-nonlit-pos-cache-limit)
3709 (setq c-state-nonlit-pos-cache-limit (1- here)))
3710 (c-truncate-semi-nonlit-pos-cache here)
3712 ;; `c-state-cache':
3713 ;; Case 1: if `here' is in a literal containing point-min, everything
3714 ;; becomes (or is already) nil.
3715 (if (or (null c-state-cache-good-pos)
3716 (< here (c-state-get-min-scan-pos)))
3717 (setq c-state-cache nil
3718 c-state-cache-good-pos nil
3719 c-state-min-scan-pos nil)
3721 ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
3722 ;; below `here'. To maintain its consistency, we may need to insert a new
3723 ;; brace pair.
3724 (let ((here-bol (c-point 'bol here))
3725 too-high-pa ; recorded {/(/[ next above or just below here, or nil.
3726 dropped-cons) ; was the last removed element a brace pair?
3727 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
3728 (while (and c-state-cache
3729 (>= (c-state-cache-top-paren) here))
3730 (setq dropped-cons (consp (car c-state-cache))
3731 too-high-pa (c-state-cache-top-lparen)
3732 c-state-cache (cdr c-state-cache)))
3734 ;; Do we need to add in an earlier brace pair, having lopped one off?
3735 (if (and dropped-cons
3736 (<= too-high-pa here))
3737 (c-append-lower-brace-pair-to-state-cache too-high-pa here here-bol))
3738 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
3739 (c-state-get-min-scan-pos)))))
3741 ;; The brace-pair desert marker:
3742 (when (car c-state-brace-pair-desert)
3743 (if (< here (car c-state-brace-pair-desert))
3744 (setq c-state-brace-pair-desert nil)
3745 (if (< here (cdr c-state-brace-pair-desert))
3746 (setcdr c-state-brace-pair-desert here)))))
3748 (defun c-parse-state-1 ()
3749 ;; Find and record all noteworthy parens between some good point earlier in
3750 ;; the file and point. That good point is at least the beginning of the
3751 ;; top-level construct we are in, or the beginning of the preceding
3752 ;; top-level construct if we aren't in one.
3754 ;; The returned value is a list of the noteworthy parens with the last one
3755 ;; first. If an element in the list is an integer, it's the position of an
3756 ;; open paren (of any type) which has not been closed before the point. If
3757 ;; an element is a cons, it gives the position of a closed BRACE paren
3758 ;; pair[*]; the car is the start brace position and the cdr is the position
3759 ;; following the closing brace. Only the last closed brace paren pair
3760 ;; before each open paren and before the point is recorded, and thus the
3761 ;; state never contains two cons elements in succession. When a close brace
3762 ;; has no matching open brace (e.g., the matching brace is outside the
3763 ;; visible region), it is not represented in the returned value.
3765 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
3766 ;; This defun explicitly treats mismatching parens/braces/brackets as
3767 ;; matching. It is the open brace which makes it a "brace" pair.
3769 ;; If POINT is within a macro, open parens and brace pairs within
3770 ;; THIS macro MIGHT be recorded. This depends on whether their
3771 ;; syntactic properties have been suppressed by
3772 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
3774 ;; Currently no characters which are given paren syntax with the
3775 ;; syntax-table property are recorded, i.e. angle bracket arglist
3776 ;; parens are never present here. Note that this might change.
3778 ;; BUG: This function doesn't cope entirely well with unbalanced
3779 ;; parens in macros. (2008-12-11: this has probably been resolved
3780 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3781 ;; following case the brace before the macro isn't balanced with the
3782 ;; one after it:
3784 ;; {
3785 ;; #define X {
3786 ;; }
3788 ;; Note to maintainers: this function DOES get called with point
3789 ;; within comments and strings, so don't assume it doesn't!
3791 ;; This function might do hidden buffer changes.
3792 (let* ((here (point))
3793 (here-bopl (c-point 'bopl))
3794 strategy ; 'forward, 'backward etc..
3795 ;; Candidate positions to start scanning from:
3796 cache-pos ; highest position below HERE already existing in
3797 ; cache (or 1).
3798 good-pos
3799 start-point ; (when scanning forward) a place below HERE where there
3800 ; are no open parens/braces between it and HERE.
3801 bopl-state
3803 cons-separated
3804 scan-backward-pos scan-forward-p) ; used for 'backward.
3805 ;; If POINT-MIN has changed, adjust the cache
3806 (unless (= (point-min) c-state-point-min)
3807 (c-renarrow-state-cache))
3809 ;; Strategy?
3810 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3811 strategy (car res)
3812 start-point (cadr res))
3814 (when (eq strategy 'BOD)
3815 (setq c-state-cache nil
3816 c-state-cache-good-pos start-point))
3818 ;; SCAN!
3819 (cond
3820 ((memq strategy '(forward back-and-forward BOD))
3821 (setq res (c-remove-stale-state-cache start-point here here-bopl))
3822 (setq cache-pos (car res)
3823 scan-backward-pos (cadr res)
3824 cons-separated (car (cddr res))
3825 bopl-state (cadr (cddr res))) ; will be nil if (< here-bopl
3826 ; start-point)
3827 (if (and scan-backward-pos
3828 (or cons-separated (eq strategy 'forward))) ;scan-backward-pos
3829 (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
3830 (setq good-pos
3831 (c-append-to-state-cache cache-pos here))
3832 (setq c-state-cache-good-pos
3833 (if (and bopl-state
3834 (< good-pos (- here c-state-cache-too-far)))
3835 (c-state-cache-non-literal-place here-bopl bopl-state)
3836 good-pos)))
3838 ((eq strategy 'backward)
3839 (setq res (c-remove-stale-state-cache-backwards here)
3840 good-pos (car res)
3841 scan-backward-pos (cadr res)
3842 scan-forward-p (car (cddr res)))
3843 (if scan-backward-pos
3844 (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
3845 (setq c-state-cache-good-pos
3846 (if scan-forward-p
3847 (c-append-to-state-cache good-pos here)
3848 good-pos)))
3850 (t ; (eq strategy 'IN-LIT)
3851 (setq c-state-cache nil
3852 c-state-cache-good-pos nil))))
3854 c-state-cache)
3856 (defun c-invalidate-state-cache (here)
3857 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3859 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3860 ;; of all parens in preprocessor constructs, except for any such construct
3861 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3862 ;; worrying further about macros and template delimiters.
3863 (if (eval-when-compile (memq 'category-properties c-emacs-features))
3864 ;; Emacs
3865 (c-with-<->-as-parens-suppressed
3866 (c-invalidate-state-cache-1 here))
3867 ;; XEmacs
3868 (c-invalidate-state-cache-1 here)))
3870 (defmacro c-state-maybe-marker (place marker)
3871 ;; If PLACE is non-nil, return a marker marking it, otherwise nil.
3872 ;; We (re)use MARKER.
3873 `(and ,place
3874 (or ,marker (setq ,marker (make-marker)))
3875 (set-marker ,marker ,place)))
3877 (defun c-parse-state ()
3878 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3879 ;; description of the functionality and return value.
3881 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3882 ;; of all parens in preprocessor constructs, except for any such construct
3883 ;; containing point. We can then call `c-parse-state-1' without worrying
3884 ;; further about macros and template delimiters.
3885 (let (here-cpp-beg here-cpp-end)
3886 (save-excursion
3887 (when (c-beginning-of-macro)
3888 (setq here-cpp-beg (point))
3889 (unless
3890 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3891 here-cpp-beg)
3892 (setq here-cpp-beg nil here-cpp-end nil))))
3893 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3894 ;; subsystem.
3895 (prog1
3896 (if (eval-when-compile (memq 'category-properties c-emacs-features))
3897 ;; Emacs
3898 (c-with-<->-as-parens-suppressed
3899 (c-parse-state-1))
3900 ;; XEmacs
3901 (c-parse-state-1))
3902 (setq c-state-old-cpp-beg
3903 (c-state-maybe-marker here-cpp-beg c-state-old-cpp-beg-marker)
3904 c-state-old-cpp-end
3905 (c-state-maybe-marker here-cpp-end c-state-old-cpp-end-marker)))))
3907 ;; Debug tool to catch cache inconsistencies. This is called from
3908 ;; 000tests.el.
3909 (defvar c-debug-parse-state nil)
3910 (unless (fboundp 'c-real-parse-state)
3911 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3912 (cc-bytecomp-defun c-real-parse-state)
3914 (defvar c-parse-state-point nil)
3915 (defvar c-parse-state-state nil)
3916 (make-variable-buffer-local 'c-parse-state-state)
3917 (defun c-record-parse-state-state ()
3918 (setq c-parse-state-point (point))
3919 (when (markerp (cdr (assq 'c-state-old-cpp-beg c-parse-state-state)))
3920 (move-marker (cdr (assq 'c-state-old-cpp-beg c-parse-state-state)) nil)
3921 (move-marker (cdr (assq 'c-state-old-cpp-end c-parse-state-state)) nil))
3922 (setq c-parse-state-state
3923 (mapcar
3924 (lambda (arg)
3925 (let ((val (symbol-value arg)))
3926 (cons arg
3927 (cond ((consp val) (copy-tree val))
3928 ((markerp val) (copy-marker val))
3929 (t val)))))
3930 '(c-state-cache
3931 c-state-cache-good-pos
3932 c-state-nonlit-pos-cache
3933 c-state-nonlit-pos-cache-limit
3934 c-state-semi-nonlit-pos-cache
3935 c-state-semi-nonlit-pos-cache-limit
3936 c-state-brace-pair-desert
3937 c-state-point-min
3938 c-state-point-min-lit-type
3939 c-state-point-min-lit-start
3940 c-state-min-scan-pos
3941 c-state-old-cpp-beg
3942 c-state-old-cpp-end
3943 c-parse-state-point))))
3944 (defun c-replay-parse-state-state ()
3945 (message "%s"
3946 (concat "(setq "
3947 (mapconcat
3948 (lambda (arg)
3949 (format "%s %s%s" (car arg)
3950 (if (atom (cdr arg)) "" "'")
3951 (if (markerp (cdr arg))
3952 (format "(copy-marker %s)" (marker-position (cdr arg)))
3953 (cdr arg))))
3954 c-parse-state-state " ")
3955 ")")))
3957 (defun c-debug-parse-state-double-cons (state)
3958 (let (state-car conses-not-ok)
3959 (while state
3960 (setq state-car (car state)
3961 state (cdr state))
3962 (if (and (consp state-car)
3963 (consp (car state)))
3964 (setq conses-not-ok t)))
3965 conses-not-ok))
3967 (defun c-debug-parse-state ()
3968 (let ((here (point)) (min-point (point-min)) (res1 (c-real-parse-state)) res2)
3969 (let ((c-state-cache nil)
3970 (c-state-cache-good-pos 1)
3971 (c-state-nonlit-pos-cache nil)
3972 (c-state-nonlit-pos-cache-limit 1)
3973 (c-state-brace-pair-desert nil)
3974 (c-state-point-min 1)
3975 (c-state-point-min-lit-type nil)
3976 (c-state-point-min-lit-start nil)
3977 (c-state-min-scan-pos 1)
3978 (c-state-old-cpp-beg nil)
3979 (c-state-old-cpp-end nil))
3980 (setq res2 (c-real-parse-state)))
3981 (unless (equal res1 res2)
3982 ;; The cache can actually go further back due to the ad-hoc way
3983 ;; the first paren is found, so try to whack off a bit of its
3984 ;; start before complaining.
3985 ;; (save-excursion
3986 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3987 ;; (c-beginning-of-defun-1)
3988 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3989 ;; (c-beginning-of-defun-1))
3990 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3991 ;; (message (concat "c-parse-state inconsistency at %s: "
3992 ;; "using cache: %s, from scratch: %s")
3993 ;; here res1 res2)))
3994 (message (concat "c-parse-state inconsistency at %s: "
3995 "using cache: %s, from scratch: %s. POINT-MIN: %s")
3996 here res1 res2 min-point)
3997 (message "Old state:")
3998 (c-replay-parse-state-state))
4000 (when (c-debug-parse-state-double-cons res1)
4001 (message "c-parse-state INVALIDITY at %s: %s"
4002 here res1)
4003 (message "Old state:")
4004 (c-replay-parse-state-state))
4006 (c-record-parse-state-state)
4007 res2 ; res1 correct a cascading series of errors ASAP
4010 (defun c-toggle-parse-state-debug (&optional arg)
4011 (interactive "P")
4012 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
4013 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
4014 'c-debug-parse-state
4015 'c-real-parse-state)))
4016 (c-keep-region-active)
4017 (message "c-debug-parse-state %sabled"
4018 (if c-debug-parse-state "en" "dis")))
4019 (when c-debug-parse-state
4020 (c-toggle-parse-state-debug 1))
4023 (defun c-whack-state-before (bufpos paren-state)
4024 ;; Whack off any state information from PAREN-STATE which lies
4025 ;; before BUFPOS. Not destructive on PAREN-STATE.
4026 (let* ((newstate (list nil))
4027 (ptr newstate)
4028 car)
4029 (while paren-state
4030 (setq car (car paren-state)
4031 paren-state (cdr paren-state))
4032 (if (< (if (consp car) (car car) car) bufpos)
4033 (setq paren-state nil)
4034 (setcdr ptr (list car))
4035 (setq ptr (cdr ptr))))
4036 (cdr newstate)))
4038 (defun c-whack-state-after (bufpos paren-state)
4039 ;; Whack off any state information from PAREN-STATE which lies at or
4040 ;; after BUFPOS. Not destructive on PAREN-STATE.
4041 (catch 'done
4042 (while paren-state
4043 (let ((car (car paren-state)))
4044 (if (consp car)
4045 ;; just check the car, because in a balanced brace
4046 ;; expression, it must be impossible for the corresponding
4047 ;; close brace to be before point, but the open brace to
4048 ;; be after.
4049 (if (<= bufpos (car car))
4050 nil ; whack it off
4051 (if (< bufpos (cdr car))
4052 ;; its possible that the open brace is before
4053 ;; bufpos, but the close brace is after. In that
4054 ;; case, convert this to a non-cons element. The
4055 ;; rest of the state is before bufpos, so we're
4056 ;; done.
4057 (throw 'done (cons (car car) (cdr paren-state)))
4058 ;; we know that both the open and close braces are
4059 ;; before bufpos, so we also know that everything else
4060 ;; on state is before bufpos.
4061 (throw 'done paren-state)))
4062 (if (<= bufpos car)
4063 nil ; whack it off
4064 ;; it's before bufpos, so everything else should too.
4065 (throw 'done paren-state)))
4066 (setq paren-state (cdr paren-state)))
4067 nil)))
4069 (defun c-most-enclosing-brace (paren-state &optional bufpos)
4070 ;; Return the bufpos of the innermost enclosing open paren before
4071 ;; bufpos, or nil if none was found.
4072 (let (enclosingp)
4073 (or bufpos (setq bufpos 134217727))
4074 (while paren-state
4075 (setq enclosingp (car paren-state)
4076 paren-state (cdr paren-state))
4077 (if (or (consp enclosingp)
4078 (>= enclosingp bufpos))
4079 (setq enclosingp nil)
4080 (setq paren-state nil)))
4081 enclosingp))
4083 (defun c-least-enclosing-brace (paren-state)
4084 ;; Return the bufpos of the outermost enclosing open paren, or nil
4085 ;; if none was found.
4086 (let (pos elem)
4087 (while paren-state
4088 (setq elem (car paren-state)
4089 paren-state (cdr paren-state))
4090 (if (integerp elem)
4091 (setq pos elem)))
4092 pos))
4094 (defun c-safe-position (bufpos paren-state)
4095 ;; Return the closest "safe" position recorded on PAREN-STATE that
4096 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
4097 ;; contain any. Return nil if BUFPOS is nil, which is useful to
4098 ;; find the closest limit before a given limit that might be nil.
4100 ;; A "safe" position is a position at or after a recorded open
4101 ;; paren, or after a recorded close paren. The returned position is
4102 ;; thus either the first position after a close brace, or the first
4103 ;; position after an enclosing paren, or at the enclosing paren in
4104 ;; case BUFPOS is immediately after it.
4105 (when bufpos
4106 (let (elem)
4107 (catch 'done
4108 (while paren-state
4109 (setq elem (car paren-state))
4110 (if (consp elem)
4111 (cond ((< (cdr elem) bufpos)
4112 (throw 'done (cdr elem)))
4113 ((< (car elem) bufpos)
4114 ;; See below.
4115 (throw 'done (min (1+ (car elem)) bufpos))))
4116 (if (< elem bufpos)
4117 ;; elem is the position at and not after the opening paren, so
4118 ;; we can go forward one more step unless it's equal to
4119 ;; bufpos. This is useful in some cases avoid an extra paren
4120 ;; level between the safe position and bufpos.
4121 (throw 'done (min (1+ elem) bufpos))))
4122 (setq paren-state (cdr paren-state)))))))
4124 (defun c-beginning-of-syntax ()
4125 ;; This is used for `font-lock-beginning-of-syntax-function'. It
4126 ;; goes to the closest previous point that is known to be outside
4127 ;; any string literal or comment. `c-state-cache' is used if it has
4128 ;; a position in the vicinity.
4129 (let* ((paren-state c-state-cache)
4130 elem
4132 (pos (catch 'done
4133 ;; Note: Similar code in `c-safe-position'. The
4134 ;; difference is that we accept a safe position at
4135 ;; the point and don't bother to go forward past open
4136 ;; parens.
4137 (while paren-state
4138 (setq elem (car paren-state))
4139 (if (consp elem)
4140 (cond ((<= (cdr elem) (point))
4141 (throw 'done (cdr elem)))
4142 ((<= (car elem) (point))
4143 (throw 'done (car elem))))
4144 (if (<= elem (point))
4145 (throw 'done elem)))
4146 (setq paren-state (cdr paren-state)))
4147 (point-min))))
4149 (if (> pos (- (point) 4000))
4150 (goto-char pos)
4151 ;; The position is far back. Try `c-beginning-of-defun-1'
4152 ;; (although we can't be entirely sure it will go to a position
4153 ;; outside a comment or string in current emacsen). FIXME:
4154 ;; Consult `syntax-ppss' here.
4155 (c-beginning-of-defun-1)
4156 (if (< (point) pos)
4157 (goto-char pos)))))
4160 ;; Tools for scanning identifiers and other tokens.
4162 (defun c-on-identifier ()
4163 "Return non-nil if the point is on or directly after an identifier.
4164 Keywords are recognized and not considered identifiers. If an
4165 identifier is detected, the returned value is its starting position.
4166 If an identifier ends at the point and another begins at it \(can only
4167 happen in Pike) then the point for the preceding one is returned.
4169 Note that this function might do hidden buffer changes. See the
4170 comment at the start of cc-engine.el for more info."
4172 ;; FIXME: Shouldn't this function handle "operator" in C++?
4174 (save-excursion
4175 (skip-syntax-backward "w_")
4179 ;; Check for a normal (non-keyword) identifier.
4180 (and (looking-at c-symbol-start)
4181 (not (looking-at c-keywords-regexp))
4182 (point))
4184 (when (c-major-mode-is 'pike-mode)
4185 ;; Handle the `<operator> syntax in Pike.
4186 (let ((pos (point)))
4187 (skip-chars-backward "-!%&*+/<=>^|~[]()")
4188 (and (if (< (skip-chars-backward "`") 0)
4190 (goto-char pos)
4191 (eq (char-after) ?\`))
4192 (looking-at c-symbol-key)
4193 (>= (match-end 0) pos)
4194 (point))))
4196 ;; Handle the "operator +" syntax in C++.
4197 (when (and c-overloadable-operators-regexp
4198 (= (c-backward-token-2 0) 0))
4200 (cond ((and (looking-at c-overloadable-operators-regexp)
4201 (or (not c-opt-op-identifier-prefix)
4202 (and (= (c-backward-token-2 1) 0)
4203 (looking-at c-opt-op-identifier-prefix))))
4204 (point))
4206 ((save-excursion
4207 (and c-opt-op-identifier-prefix
4208 (looking-at c-opt-op-identifier-prefix)
4209 (= (c-forward-token-2 1) 0)
4210 (looking-at c-overloadable-operators-regexp)))
4211 (point))))
4215 (defsubst c-simple-skip-symbol-backward ()
4216 ;; If the point is at the end of a symbol then skip backward to the
4217 ;; beginning of it. Don't move otherwise. Return non-nil if point
4218 ;; moved.
4220 ;; This function might do hidden buffer changes.
4221 (or (< (skip-syntax-backward "w_") 0)
4222 (and (c-major-mode-is 'pike-mode)
4223 ;; Handle the `<operator> syntax in Pike.
4224 (let ((pos (point)))
4225 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
4226 (< (skip-chars-backward "`") 0)
4227 (looking-at c-symbol-key)
4228 (>= (match-end 0) pos))
4230 (goto-char pos)
4231 nil)))))
4233 (defun c-beginning-of-current-token (&optional back-limit)
4234 ;; Move to the beginning of the current token. Do not move if not
4235 ;; in the middle of one. BACK-LIMIT may be used to bound the
4236 ;; backward search; if given it's assumed to be at the boundary
4237 ;; between two tokens. Return non-nil if the point is moved, nil
4238 ;; otherwise.
4240 ;; This function might do hidden buffer changes.
4241 (let ((start (point)))
4242 (if (looking-at "\\w\\|\\s_")
4243 (skip-syntax-backward "w_" back-limit)
4244 (when (< (skip-syntax-backward ".()" back-limit) 0)
4245 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
4246 (match-end 0))
4247 ;; `c-nonsymbol-token-regexp' should always match
4248 ;; since we've skipped backward over punctuation
4249 ;; or paren syntax, but consume one char in case
4250 ;; it doesn't so that we don't leave point before
4251 ;; some earlier incorrect token.
4252 (1+ (point)))))
4253 (if (<= pos start)
4254 (goto-char pos))))))
4255 (< (point) start)))
4257 (defun c-end-of-current-token (&optional back-limit)
4258 ;; Move to the end of the current token. Do not move if not in the
4259 ;; middle of one. BACK-LIMIT may be used to bound the backward
4260 ;; search; if given it's assumed to be at the boundary between two
4261 ;; tokens. Return non-nil if the point is moved, nil otherwise.
4263 ;; This function might do hidden buffer changes.
4264 (let ((start (point)))
4265 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
4266 (skip-syntax-forward "w_"))
4267 ((< (skip-syntax-backward ".()" back-limit) 0)
4268 (while (progn
4269 (if (looking-at c-nonsymbol-token-regexp)
4270 (goto-char (match-end 0))
4271 ;; `c-nonsymbol-token-regexp' should always match since
4272 ;; we've skipped backward over punctuation or paren
4273 ;; syntax, but move forward in case it doesn't so that
4274 ;; we don't leave point earlier than we started with.
4275 (forward-char))
4276 (< (point) start)))))
4277 (> (point) start)))
4279 (defconst c-jump-syntax-balanced
4280 (if (memq 'gen-string-delim c-emacs-features)
4281 "\\w\\|\\s_\\|\\s(\\|\\s)\\|\\s\"\\|\\s|"
4282 "\\w\\|\\s_\\|\\s(\\|\\s)\\|\\s\""))
4284 (defconst c-jump-syntax-unbalanced
4285 (if (memq 'gen-string-delim c-emacs-features)
4286 "\\w\\|\\s_\\|\\s\"\\|\\s|"
4287 "\\w\\|\\s_\\|\\s\""))
4289 (defun c-forward-over-token-and-ws (&optional balanced)
4290 "Move forward over a token and any following whitespace
4291 Return t if we moved, nil otherwise (i.e. we were at EOB, or a
4292 non-token or BALANCED is non-nil and we can't move). If we
4293 are at syntactic whitespace, move over this in place of a token.
4295 If BALANCED is non-nil move over any balanced parens we are at, and never move
4296 out of an enclosing paren.
4298 This function differs from `c-forward-token-2' in that it will move forward
4299 over the final token in a buffer, up to EOB."
4300 (let ((jump-syntax (if balanced
4301 c-jump-syntax-balanced
4302 c-jump-syntax-unbalanced))
4303 (here (point)))
4304 (when
4305 (condition-case nil
4306 (cond
4307 ((/= (point)
4308 (progn (c-forward-syntactic-ws) (point)))
4309 ;; If we're at whitespace, count this as the token.
4311 ((eobp) nil)
4312 ((looking-at jump-syntax)
4313 (goto-char (scan-sexps (point) 1))
4315 ((looking-at c-nonsymbol-token-regexp)
4316 (goto-char (match-end 0))
4318 ((save-restriction
4319 (widen)
4320 (looking-at c-nonsymbol-token-regexp))
4321 nil)
4323 (forward-char)
4325 (error (goto-char here)
4326 nil))
4327 (c-forward-syntactic-ws)
4328 t)))
4330 (defun c-forward-token-2 (&optional count balanced limit)
4331 "Move forward by tokens.
4332 A token is defined as all symbols and identifiers which aren't
4333 syntactic whitespace \(note that multicharacter tokens like \"==\" are
4334 treated properly). Point is always either left at the beginning of a
4335 token or not moved at all. COUNT specifies the number of tokens to
4336 move; a negative COUNT moves in the opposite direction. A COUNT of 0
4337 moves to the next token beginning only if not already at one. If
4338 BALANCED is true, move over balanced parens, otherwise move into them.
4339 Also, if BALANCED is true, never move out of an enclosing paren.
4341 LIMIT sets the limit for the movement and defaults to the point limit.
4342 The case when LIMIT is set in the middle of a token, comment or macro
4343 is handled correctly, i.e. the point won't be left there.
4345 Return the number of tokens left to move \(positive or negative). If
4346 BALANCED is true, a move over a balanced paren counts as one. Note
4347 that if COUNT is 0 and no appropriate token beginning is found, 1 will
4348 be returned. Thus, a return value of 0 guarantees that point is at
4349 the requested position and a return value less \(without signs) than
4350 COUNT guarantees that point is at the beginning of some token.
4352 Note that this function might do hidden buffer changes. See the
4353 comment at the start of cc-engine.el for more info."
4355 (or count (setq count 1))
4356 (if (< count 0)
4357 (- (c-backward-token-2 (- count) balanced limit))
4359 (let ((here (point))
4360 (last (point)))
4361 (when (zerop count)
4362 ;; If count is zero we should jump if in the middle of a token.
4363 (c-end-of-current-token))
4365 (save-restriction
4366 (if limit (narrow-to-region (point-min) limit))
4367 (if (/= (point)
4368 (progn (c-forward-syntactic-ws) (point)))
4369 ;; Skip whitespace. Count this as a move if we did in
4370 ;; fact move.
4371 (setq count (max (1- count) 0)))
4373 (if (eobp)
4374 ;; Moved out of bounds. Make sure the returned count isn't zero.
4375 (progn
4376 (if (zerop count) (setq count 1))
4377 (goto-char here))
4378 (while (and
4379 (> count 0)
4380 (c-forward-over-token-and-ws balanced)
4381 (not (eobp)))
4382 (setq last (point)
4383 count (1- count)))
4384 (if (eobp)
4385 (goto-char last))))
4386 count)))
4388 (defun c-backward-token-2 (&optional count balanced limit)
4389 "Move backward by tokens.
4390 See `c-forward-token-2' for details."
4392 (or count (setq count 1))
4393 (if (< count 0)
4394 (- (c-forward-token-2 (- count) balanced limit))
4396 (or limit (setq limit (point-min)))
4397 (let ((jump-syntax (if balanced
4398 c-jump-syntax-balanced
4399 c-jump-syntax-unbalanced))
4400 (last (point)))
4402 (if (zerop count)
4403 ;; The count is zero so try to skip to the beginning of the
4404 ;; current token.
4405 (if (> (point)
4406 (progn (c-beginning-of-current-token) (point)))
4407 (if (< (point) limit)
4408 ;; The limit is inside the same token, so return 1.
4409 (setq count 1))
4411 ;; We're not in the middle of a token. If there's
4412 ;; whitespace after the point then we must move backward,
4413 ;; so set count to 1 in that case.
4414 (and (looking-at c-syntactic-ws-start)
4415 ;; If we're looking at a '#' that might start a cpp
4416 ;; directive then we have to do a more elaborate check.
4417 (or (/= (char-after) ?#)
4418 (not c-opt-cpp-prefix)
4419 (save-excursion
4420 (and (= (point)
4421 (progn (beginning-of-line)
4422 (looking-at "[ \t]*")
4423 (match-end 0)))
4424 (or (bobp)
4425 (progn (backward-char)
4426 (not (eq (char-before) ?\\)))))))
4427 (setq count 1))))
4429 ;; Use `condition-case' to avoid having to check for buffer
4430 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
4431 (condition-case nil
4432 (while (and
4433 (> count 0)
4434 (progn
4435 (c-backward-syntactic-ws)
4436 (backward-char)
4437 (if (looking-at jump-syntax)
4438 (goto-char (scan-sexps (1+ (point)) -1))
4439 ;; This can be very inefficient if there's a long
4440 ;; sequence of operator tokens without any separation.
4441 ;; That doesn't happen in practice, anyway.
4442 (c-beginning-of-current-token))
4443 (>= (point) limit)))
4444 (setq last (point)
4445 count (1- count)))
4446 (error (goto-char last)))
4448 (if (< (point) limit)
4449 (goto-char last))
4451 count)))
4453 (defun c-forward-token-1 (&optional count balanced limit)
4454 "Like `c-forward-token-2' but doesn't treat multicharacter operator
4455 tokens like \"==\" as single tokens, i.e. all sequences of symbol
4456 characters are jumped over character by character. This function is
4457 for compatibility only; it's only a wrapper over `c-forward-token-2'."
4458 (let ((c-nonsymbol-token-regexp "\\s."))
4459 (c-forward-token-2 count balanced limit)))
4461 (defun c-backward-token-1 (&optional count balanced limit)
4462 "Like `c-backward-token-2' but doesn't treat multicharacter operator
4463 tokens like \"==\" as single tokens, i.e. all sequences of symbol
4464 characters are jumped over character by character. This function is
4465 for compatibility only; it's only a wrapper over `c-backward-token-2'."
4466 (let ((c-nonsymbol-token-regexp "\\s."))
4467 (c-backward-token-2 count balanced limit)))
4470 ;; Tools for doing searches restricted to syntactically relevant text.
4472 (defun c-syntactic-re-search-forward (regexp &optional bound noerror
4473 paren-level not-inside-token
4474 lookbehind-submatch)
4475 "Like `re-search-forward', but only report matches that are found
4476 in syntactically significant text. I.e. matches in comments, macros
4477 or string literals are ignored. The start point is assumed to be
4478 outside any comment, macro or string literal, or else the content of
4479 that region is taken as syntactically significant text.
4481 NOERROR, in addition to the values nil, t, and <anything else>
4482 used in `re-search-forward' can also take the values
4483 'before-literal and 'after-literal. In these cases, when BOUND
4484 is also given and is inside a literal, and a search fails, point
4485 will be left, respectively before or after the literal. Be aware
4486 that with 'after-literal, if a string or comment is unclosed at
4487 the end of the buffer, point may be left there, even though it is
4488 inside a literal there.
4490 If PAREN-LEVEL is non-nil, an additional restriction is added to
4491 ignore matches in nested paren sexps. The search will also not go
4492 outside the current list sexp, which has the effect that if the point
4493 should be moved to BOUND when no match is found \(i.e. NOERROR is
4494 neither nil nor t), then it will be at the closing paren if the end of
4495 the current list sexp is encountered first.
4497 If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
4498 ignored. Things like multicharacter operators and special symbols
4499 \(e.g. \"`()\" in Pike) are handled but currently not floating point
4500 constants.
4502 If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
4503 subexpression in REGEXP. The end of that submatch is used as the
4504 position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
4505 isn't used or if that subexpression didn't match then the start
4506 position of the whole match is used instead. The \"look behind\"
4507 subexpression is never tested before the starting position, so it
4508 might be a good idea to include \\=\\= as a match alternative in it.
4510 Optimization note: Matches might be missed if the \"look behind\"
4511 subexpression can match the end of nonwhite syntactic whitespace,
4512 i.e. the end of comments or cpp directives. This since the function
4513 skips over such things before resuming the search. It's on the other
4514 hand not safe to assume that the \"look behind\" subexpression never
4515 matches syntactic whitespace.
4517 Bug: Unbalanced parens inside cpp directives are currently not handled
4518 correctly \(i.e. they don't get ignored as they should) when
4519 PAREN-LEVEL is set.
4521 Note that this function might do hidden buffer changes. See the
4522 comment at the start of cc-engine.el for more info."
4524 (or bound (setq bound (point-max)))
4525 (if paren-level (setq paren-level -1))
4527 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
4529 (let ((start (point))
4531 ;; Start position for the last search.
4532 search-pos
4533 ;; The `parse-partial-sexp' state between the start position
4534 ;; and the point.
4535 state
4536 ;; The current position after the last state update. The next
4537 ;; `parse-partial-sexp' continues from here.
4538 (state-pos (point))
4539 ;; The position at which to check the state and the state
4540 ;; there. This is separate from `state-pos' since we might
4541 ;; need to back up before doing the next search round.
4542 check-pos check-state
4543 ;; Last position known to end a token.
4544 (last-token-end-pos (point-min))
4545 ;; Set when a valid match is found.
4546 found)
4548 (condition-case err
4549 (while
4550 (and
4551 (progn
4552 (setq search-pos (point))
4553 (if (re-search-forward regexp bound noerror)
4555 ;; Without the following, when PAREN-LEVEL is non-nil, and
4556 ;; NOERROR is not nil or t, and the very first search above
4557 ;; has just failed, point would end up at BOUND rather than
4558 ;; just before the next close paren.
4559 (when (and (eq search-pos start)
4560 paren-level
4561 (not (memq noerror '(nil t))))
4562 (setq state (parse-partial-sexp start bound -1))
4563 (if (eq (car state) -1)
4564 (setq bound (1- (point)))))
4565 nil))
4567 (progn
4568 (setq state (parse-partial-sexp
4569 state-pos (match-beginning 0) paren-level nil state)
4570 state-pos (point))
4571 (if (setq check-pos (and lookbehind-submatch
4572 (or (not paren-level)
4573 (>= (car state) 0))
4574 (match-end lookbehind-submatch)))
4575 (setq check-state (parse-partial-sexp
4576 state-pos check-pos paren-level nil state))
4577 (setq check-pos state-pos
4578 check-state state))
4580 ;; NOTE: If we got a look behind subexpression and get
4581 ;; an insignificant match in something that isn't
4582 ;; syntactic whitespace (i.e. strings or in nested
4583 ;; parentheses), then we can never skip more than a
4584 ;; single character from the match start position
4585 ;; (i.e. `state-pos' here) before continuing the
4586 ;; search. That since the look behind subexpression
4587 ;; might match the end of the insignificant region in
4588 ;; the next search.
4590 (cond
4591 ((elt check-state 7)
4592 ;; Match inside a line comment. Skip to eol. Use
4593 ;; `re-search-forward' instead of `skip-chars-forward' to get
4594 ;; the right bound behavior.
4595 (re-search-forward "[\n\r]" bound noerror))
4597 ((elt check-state 4)
4598 ;; Match inside a block comment. Skip to the '*/'.
4599 (search-forward "*/" bound noerror))
4601 ((and (not (elt check-state 5))
4602 (eq (char-before check-pos) ?/)
4603 (not (c-get-char-property (1- check-pos) 'syntax-table))
4604 (memq (char-after check-pos) '(?/ ?*)))
4605 ;; Match in the middle of the opener of a block or line
4606 ;; comment.
4607 (if (= (char-after check-pos) ?/)
4608 (re-search-forward "[\n\r]" bound noerror)
4609 (search-forward "*/" bound noerror)))
4611 ;; The last `parse-partial-sexp' above might have
4612 ;; stopped short of the real check position if the end
4613 ;; of the current sexp was encountered in paren-level
4614 ;; mode. The checks above are always false in that
4615 ;; case, and since they can do better skipping in
4616 ;; lookbehind-submatch mode, we do them before
4617 ;; checking the paren level.
4619 ((and paren-level
4620 (/= (setq tmp (car check-state)) 0))
4621 ;; Check the paren level first since we're short of the
4622 ;; syntactic checking position if the end of the
4623 ;; current sexp was encountered by `parse-partial-sexp'.
4624 (if (> tmp 0)
4626 ;; Inside a nested paren sexp.
4627 (if lookbehind-submatch
4628 ;; See the NOTE above.
4629 (progn (goto-char state-pos) t)
4630 ;; Skip out of the paren quickly.
4631 (setq state (parse-partial-sexp state-pos bound 0 nil state)
4632 state-pos (point)))
4634 ;; Have exited the current paren sexp.
4635 (if noerror
4636 (progn
4637 ;; The last `parse-partial-sexp' call above
4638 ;; has left us just after the closing paren
4639 ;; in this case, so we can modify the bound
4640 ;; to leave the point at the right position
4641 ;; upon return.
4642 (setq bound (1- (point)))
4643 nil)
4644 (signal 'search-failed (list regexp)))))
4646 ((setq tmp (elt check-state 3))
4647 ;; Match inside a string.
4648 (if (or lookbehind-submatch
4649 (not (integerp tmp)))
4650 ;; See the NOTE above.
4651 (progn (goto-char state-pos) t)
4652 ;; Skip to the end of the string before continuing.
4653 (let ((ender (make-string 1 tmp)) (continue t))
4654 (while (if (search-forward ender bound noerror)
4655 (progn
4656 (setq state (parse-partial-sexp
4657 state-pos (point) nil nil state)
4658 state-pos (point))
4659 (elt state 3))
4660 (setq continue nil)))
4661 continue)))
4663 ((save-excursion
4664 (save-match-data
4665 (c-beginning-of-macro start)))
4666 ;; Match inside a macro. Skip to the end of it.
4667 (c-end-of-macro)
4668 (cond ((<= (point) bound) t)
4669 (noerror nil)
4670 (t (signal 'search-failed (list regexp)))))
4672 ((and not-inside-token
4673 (or (< check-pos last-token-end-pos)
4674 (< check-pos
4675 (save-excursion
4676 (goto-char check-pos)
4677 (save-match-data
4678 (c-end-of-current-token last-token-end-pos))
4679 (setq last-token-end-pos (point))))))
4680 ;; Inside a token.
4681 (if lookbehind-submatch
4682 ;; See the NOTE above.
4683 (goto-char state-pos)
4684 (goto-char (min last-token-end-pos bound))))
4687 ;; A real match.
4688 (setq found t)
4689 nil)))
4691 ;; Should loop to search again, but take care to avoid
4692 ;; looping on the same spot.
4693 (or (/= search-pos (point))
4694 (if (= (point) bound)
4695 (if noerror
4697 (signal 'search-failed (list regexp)))
4698 (forward-char)
4699 t))))
4701 (error
4702 (goto-char start)
4703 (signal (car err) (cdr err))))
4705 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
4707 (if found
4708 (progn
4709 (goto-char (match-end 0))
4710 (match-end 0))
4712 ;; Search failed. Set point as appropriate.
4713 (cond
4714 ((eq noerror t)
4715 (goto-char start))
4716 ((not (memq noerror '(before-literal after-literal)))
4717 (goto-char bound))
4718 (t (setq state (parse-partial-sexp state-pos bound nil nil state))
4719 (if (or (elt state 3) (elt state 4))
4720 (if (eq noerror 'before-literal)
4721 (goto-char (elt state 8))
4722 (parse-partial-sexp bound (point-max) nil nil
4723 state 'syntax-table))
4724 (goto-char bound))))
4726 nil)))
4728 (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
4730 (defsubst c-ssb-lit-begin ()
4731 ;; Return the start of the literal point is in, or nil.
4732 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
4733 ;; bound in the caller.
4735 ;; Use `parse-partial-sexp' from a safe position down to the point to check
4736 ;; if it's outside comments and strings.
4737 (save-excursion
4738 (let ((pos (point)) safe-pos state)
4739 ;; Pick a safe position as close to the point as possible.
4741 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
4742 ;; position.
4744 (while (and safe-pos-list
4745 (> (car safe-pos-list) (point)))
4746 (setq safe-pos-list (cdr safe-pos-list)))
4747 (unless (setq safe-pos (car-safe safe-pos-list))
4748 (setq safe-pos (max (or (c-safe-position
4749 (point) (c-parse-state))
4751 (point-min))
4752 safe-pos-list (list safe-pos)))
4754 ;; Cache positions along the way to use if we have to back up more. We
4755 ;; cache every closing paren on the same level. If the paren cache is
4756 ;; relevant in this region then we're typically already on the same
4757 ;; level as the target position. Note that we might cache positions
4758 ;; after opening parens in case safe-pos is in a nested list. That's
4759 ;; both uncommon and harmless.
4760 (while (progn
4761 (setq state (parse-partial-sexp
4762 safe-pos pos 0))
4763 (< (point) pos))
4764 (setq safe-pos (point)
4765 safe-pos-list (cons safe-pos safe-pos-list)))
4767 ;; If the state contains the start of the containing sexp we cache that
4768 ;; position too, so that parse-partial-sexp in the next run has a bigger
4769 ;; chance of starting at the same level as the target position and thus
4770 ;; will get more good safe positions into the list.
4771 (if (elt state 1)
4772 (setq safe-pos (1+ (elt state 1))
4773 safe-pos-list (cons safe-pos safe-pos-list)))
4775 (if (or (elt state 3) (elt state 4))
4776 ;; Inside string or comment. Continue search at the
4777 ;; beginning of it.
4778 (elt state 8)))))
4780 (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4781 "Like `skip-chars-backward' but only look at syntactically relevant chars,
4782 i.e. don't stop at positions inside syntactic whitespace or string
4783 literals. Preprocessor directives are also ignored, with the exception
4784 of the one that the point starts within, if any. If LIMIT is given,
4785 it's assumed to be at a syntactically relevant position.
4787 If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4788 sexps, and the search will also not go outside the current paren sexp.
4789 However, if LIMIT or the buffer limit is reached inside a nested paren
4790 then the point will be left at the limit.
4792 Non-nil is returned if the point moved, nil otherwise.
4794 Note that this function might do hidden buffer changes. See the
4795 comment at the start of cc-engine.el for more info."
4797 (c-self-bind-state-cache
4798 (let ((start (point))
4799 ;; A list of syntactically relevant positions in descending
4800 ;; order. It's used to avoid scanning repeatedly over
4801 ;; potentially large regions with `parse-partial-sexp' to verify
4802 ;; each position. Used in `c-ssb-lit-begin'
4803 safe-pos-list
4804 ;; The result from `c-beginning-of-macro' at the start position or the
4805 ;; start position itself if it isn't within a macro. Evaluated on
4806 ;; demand.
4807 start-macro-beg
4808 ;; The earliest position after the current one with the same paren
4809 ;; level. Used only when `paren-level' is set.
4810 lit-beg
4811 (paren-level-pos (point)))
4813 (while
4814 (progn
4815 ;; The next loop "tries" to find the end point each time round,
4816 ;; loops when it hasn't succeeded.
4817 (while
4818 (and
4819 (let ((pos (point)))
4820 (while (and
4821 (< (skip-chars-backward skip-chars limit) 0)
4822 ;; Don't stop inside a literal.
4823 (when (setq lit-beg (c-ssb-lit-begin))
4824 (goto-char lit-beg)
4825 t)))
4826 (< (point) pos))
4828 (let ((pos (point)) state-2 pps-end-pos)
4830 (cond
4831 ((and paren-level
4832 (save-excursion
4833 (setq state-2 (parse-partial-sexp
4834 pos paren-level-pos -1)
4835 pps-end-pos (point))
4836 (/= (car state-2) 0)))
4837 ;; Not at the right level.
4839 (if (and (< (car state-2) 0)
4840 ;; We stop above if we go out of a paren.
4841 ;; Now check whether it precedes or is
4842 ;; nested in the starting sexp.
4843 (save-excursion
4844 (setq state-2
4845 (parse-partial-sexp
4846 pps-end-pos paren-level-pos
4847 nil nil state-2))
4848 (< (car state-2) 0)))
4850 ;; We've stopped short of the starting position
4851 ;; so the hit was inside a nested list. Go up
4852 ;; until we are at the right level.
4853 (condition-case nil
4854 (progn
4855 (goto-char (scan-lists pos -1
4856 (- (car state-2))))
4857 (setq paren-level-pos (point))
4858 (if (and limit (>= limit paren-level-pos))
4859 (progn
4860 (goto-char limit)
4861 nil)
4863 (error
4864 (goto-char (or limit (point-min)))
4865 nil))
4867 ;; The hit was outside the list at the start
4868 ;; position. Go to the start of the list and exit.
4869 (goto-char (1+ (elt state-2 1)))
4870 nil))
4872 ((c-beginning-of-macro limit)
4873 ;; Inside a macro.
4874 (if (< (point)
4875 (or start-macro-beg
4876 (setq start-macro-beg
4877 (save-excursion
4878 (goto-char start)
4879 (c-beginning-of-macro limit)
4880 (point)))))
4883 ;; It's inside the same macro we started in so it's
4884 ;; a relevant match.
4885 (goto-char pos)
4886 nil))))))
4888 (> (point)
4889 (progn
4890 ;; Skip syntactic ws afterwards so that we don't stop at the
4891 ;; end of a comment if `skip-chars' is something like "^/".
4892 (c-backward-syntactic-ws)
4893 (point)))))
4895 ;; We might want to extend this with more useful return values in
4896 ;; the future.
4897 (/= (point) start))))
4899 ;; The following is an alternative implementation of
4900 ;; `c-syntactic-skip-backward' that uses backward movement to keep
4901 ;; track of the syntactic context. It turned out to be generally
4902 ;; slower than the one above which uses forward checks from earlier
4903 ;; safe positions.
4905 ;;(defconst c-ssb-stop-re
4906 ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4907 ;; ;; stop at to avoid going into comments and literals.
4908 ;; (concat
4909 ;; ;; Match comment end syntax and string literal syntax. Also match
4910 ;; ;; '/' for block comment endings (not covered by comment end
4911 ;; ;; syntax).
4912 ;; "\\s>\\|/\\|\\s\""
4913 ;; (if (memq 'gen-string-delim c-emacs-features)
4914 ;; "\\|\\s|"
4915 ;; "")
4916 ;; (if (memq 'gen-comment-delim c-emacs-features)
4917 ;; "\\|\\s!"
4918 ;; "")))
4920 ;;(defconst c-ssb-stop-paren-re
4921 ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4922 ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4924 ;;(defconst c-ssb-sexp-end-re
4925 ;; ;; Regexp matching the ending syntax of a complex sexp.
4926 ;; (concat c-string-limit-regexp "\\|\\s)"))
4928 ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4929 ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4930 ;;i.e. don't stop at positions inside syntactic whitespace or string
4931 ;;literals. Preprocessor directives are also ignored. However, if the
4932 ;;point is within a comment, string literal or preprocessor directory to
4933 ;;begin with, its contents is treated as syntactically relevant chars.
4934 ;;If LIMIT is given, it limits the backward search and the point will be
4935 ;;left there if no earlier position is found.
4937 ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4938 ;;sexps, and the search will also not go outside the current paren sexp.
4939 ;;However, if LIMIT or the buffer limit is reached inside a nested paren
4940 ;;then the point will be left at the limit.
4942 ;;Non-nil is returned if the point moved, nil otherwise.
4944 ;;Note that this function might do hidden buffer changes. See the
4945 ;;comment at the start of cc-engine.el for more info."
4947 ;; (save-restriction
4948 ;; (when limit
4949 ;; (narrow-to-region limit (point-max)))
4951 ;; (let ((start (point)))
4952 ;; (catch 'done
4953 ;; (while (let ((last-pos (point))
4954 ;; (stop-pos (progn
4955 ;; (skip-chars-backward skip-chars)
4956 ;; (point))))
4958 ;; ;; Skip back over the same region as
4959 ;; ;; `skip-chars-backward' above, but keep to
4960 ;; ;; syntactically relevant positions.
4961 ;; (goto-char last-pos)
4962 ;; (while (and
4963 ;; ;; `re-search-backward' with a single char regexp
4964 ;; ;; should be fast.
4965 ;; (re-search-backward
4966 ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4967 ;; stop-pos 'move)
4969 ;; (progn
4970 ;; (cond
4971 ;; ((looking-at "\\s(")
4972 ;; ;; `paren-level' is set and we've found the
4973 ;; ;; start of the containing paren.
4974 ;; (forward-char)
4975 ;; (throw 'done t))
4977 ;; ((looking-at c-ssb-sexp-end-re)
4978 ;; ;; We're at the end of a string literal or paren
4979 ;; ;; sexp (if `paren-level' is set).
4980 ;; (forward-char)
4981 ;; (condition-case nil
4982 ;; (c-backward-sexp)
4983 ;; (error
4984 ;; (goto-char limit)
4985 ;; (throw 'done t))))
4987 ;; (t
4988 ;; (forward-char)
4989 ;; ;; At the end of some syntactic ws or possibly
4990 ;; ;; after a plain '/' operator.
4991 ;; (let ((pos (point)))
4992 ;; (c-backward-syntactic-ws)
4993 ;; (if (= pos (point))
4994 ;; ;; Was a plain '/' operator. Go past it.
4995 ;; (backward-char)))))
4997 ;; (> (point) stop-pos))))
4999 ;; ;; Now the point is either at `stop-pos' or at some
5000 ;; ;; position further back if `stop-pos' was at a
5001 ;; ;; syntactically irrelevant place.
5003 ;; ;; Skip additional syntactic ws so that we don't stop
5004 ;; ;; at the end of a comment if `skip-chars' is
5005 ;; ;; something like "^/".
5006 ;; (c-backward-syntactic-ws)
5008 ;; (< (point) stop-pos))))
5010 ;; ;; We might want to extend this with more useful return values
5011 ;; ;; in the future.
5012 ;; (/= (point) start))))
5015 ;; Tools for handling comments and string literals.
5017 (defun c-in-literal (&optional _lim detect-cpp)
5018 "Return the type of literal point is in, if any.
5019 The return value is `c' if in a C-style comment, `c++' if in a C++
5020 style comment, `string' if in a string literal, `pound' if DETECT-CPP
5021 is non-nil and in a preprocessor line, or nil if somewhere else.
5022 Optional LIM is used as the backward limit of the search. If omitted,
5023 or nil, `c-beginning-of-defun' is used.
5025 Note that this function might do hidden buffer changes. See the
5026 comment at the start of cc-engine.el for more info."
5027 (save-restriction
5028 (widen)
5029 (let ((lit (c-state-semi-pp-to-literal (point))))
5030 (or (cadr lit)
5031 (and detect-cpp
5032 (save-excursion (c-beginning-of-macro))
5033 'pound)))))
5035 (defun c-literal-limits (&optional lim near not-in-delimiter)
5036 "Return a cons of the beginning and end positions of the comment or
5037 string surrounding point (including both delimiters), or nil if point
5038 isn't in one. If LIM is non-nil, it's used as the \"safe\" position
5039 to start parsing from. If NEAR is non-nil, then the limits of any
5040 literal next to point is returned. \"Next to\" means there's only
5041 spaces and tabs between point and the literal. The search for such a
5042 literal is done first in forward direction. If NOT-IN-DELIMITER is
5043 non-nil, the case when point is inside a starting delimiter won't be
5044 recognized. This only has effect for comments which have starting
5045 delimiters with more than one character.
5047 Note that this function might do hidden buffer changes. See the
5048 comment at the start of cc-engine.el for more info."
5050 (save-excursion
5051 (let*
5052 ((pos (point))
5053 (lit-limits
5054 (if lim
5055 (let ((s (parse-partial-sexp lim (point))))
5056 (when (or (nth 3 s)
5057 (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table))))
5058 (cons (nth 8 s)
5059 (progn (parse-partial-sexp (point) (point-max)
5060 nil nil
5062 'syntax-table)
5063 (point)))))
5064 (let ((pp-to-lit (c-state-full-pp-to-literal pos not-in-delimiter)))
5065 (car (cddr pp-to-lit))))))
5066 (cond
5067 (lit-limits)
5069 (near
5070 (goto-char pos)
5071 ;; Search forward for a literal.
5072 (skip-chars-forward " \t")
5073 (cond
5074 ((looking-at c-string-limit-regexp) ; String.
5075 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
5076 (point-max))))
5078 ((looking-at c-comment-start-regexp) ; Line or block comment.
5079 (cons (point) (progn (c-forward-single-comment) (point))))
5082 ;; Search backward.
5083 (skip-chars-backward " \t")
5085 (let ((end (point)) beg)
5086 (cond
5087 ((save-excursion
5088 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
5089 (setq beg (c-safe (c-backward-sexp 1) (point))))
5091 ((and (c-safe (forward-char -2) t)
5092 (looking-at "*/"))
5093 ;; Block comment. Due to the nature of line
5094 ;; comments, they will always be covered by the
5095 ;; normal case above.
5096 (goto-char end)
5097 (c-backward-single-comment)
5098 ;; If LIM is bogus, beg will be bogus.
5099 (setq beg (point))))
5101 (if beg (cons beg end))))))
5102 ))))
5104 (defun c-literal-start (&optional safe-pos)
5105 "Return the start of the string or comment surrounding point, or nil if
5106 point isn't in one. SAFE-POS, if non-nil, is a position before point which is
5107 a known \"safe position\", i.e. outside of any string or comment."
5108 (if safe-pos
5109 (let ((s (parse-partial-sexp safe-pos (point))))
5110 (and (or (nth 3 s)
5111 (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table))))
5112 (nth 8 s)))
5113 (car (cddr (c-state-semi-pp-to-literal (point))))))
5115 ;; In case external callers use this; it did have a docstring.
5116 (defalias 'c-literal-limits-fast 'c-literal-limits)
5118 (defun c-collect-line-comments (range)
5119 "If the argument is a cons of two buffer positions (such as returned by
5120 `c-literal-limits'), and that range contains a C++ style line comment,
5121 then an extended range is returned that contains all adjacent line
5122 comments (i.e. all comments that starts in the same column with no
5123 empty lines or non-whitespace characters between them). Otherwise the
5124 argument is returned.
5126 Note that this function might do hidden buffer changes. See the
5127 comment at the start of cc-engine.el for more info."
5129 (save-excursion
5130 (condition-case nil
5131 (if (and (consp range) (progn
5132 (goto-char (car range))
5133 (looking-at c-line-comment-starter)))
5134 (let ((col (current-column))
5135 (beg (point))
5136 (bopl (c-point 'bopl))
5137 (end (cdr range)))
5138 ;; Got to take care in the backward direction to handle
5139 ;; comments which are preceded by code.
5140 (while (and (c-backward-single-comment)
5141 (>= (point) bopl)
5142 (looking-at c-line-comment-starter)
5143 (= col (current-column)))
5144 (setq beg (point)
5145 bopl (c-point 'bopl)))
5146 (goto-char end)
5147 (while (and (progn (skip-chars-forward " \t")
5148 (looking-at c-line-comment-starter))
5149 (= col (current-column))
5150 (prog1 (zerop (forward-line 1))
5151 (setq end (point)))))
5152 (cons beg end))
5153 range)
5154 (error range))))
5156 (defun c-literal-type (range)
5157 "Convenience function that given the result of `c-literal-limits',
5158 returns nil or the type of literal that the range surrounds, one
5159 of the symbols `c', `c++' or `string'. It's much faster than using
5160 `c-in-literal' and is intended to be used when you need both the
5161 type of a literal and its limits.
5163 Note that this function might do hidden buffer changes. See the
5164 comment at the start of cc-engine.el for more info."
5166 (if (consp range)
5167 (save-excursion
5168 (goto-char (car range))
5169 (cond ((looking-at c-string-limit-regexp) 'string)
5170 ((or (looking-at "//") ; c++ line comment
5171 (and (looking-at "\\s<") ; comment starter
5172 (looking-at "#"))) ; awk comment.
5173 'c++)
5174 (t 'c))) ; Assuming the range is valid.
5175 range))
5177 (defsubst c-determine-limit-get-base (start try-size)
5178 ;; Get a "safe place" approximately TRY-SIZE characters before START.
5179 ;; This defsubst doesn't preserve point.
5180 (let* ((pos (max (- start try-size) (point-min)))
5181 (s (c-state-semi-pp-to-literal pos))
5182 (cand (or (car (cddr s)) pos)))
5183 (if (>= cand (point-min))
5184 cand
5185 (parse-partial-sexp pos start nil nil (car s) 'syntax-table)
5186 (point))))
5188 (defun c-determine-limit (how-far-back &optional start try-size)
5189 ;; Return a buffer position HOW-FAR-BACK non-literal characters from
5190 ;; START (default point). The starting position, either point or
5191 ;; START may not be in a comment or string.
5193 ;; The position found will not be before POINT-MIN and won't be in a
5194 ;; literal.
5196 ;; We start searching for the sought position TRY-SIZE (default
5197 ;; twice HOW-FAR-BACK) bytes back from START.
5199 ;; This function must be fast. :-)
5200 (save-excursion
5201 (let* ((start (or start (point)))
5202 (try-size (or try-size (* 2 how-far-back)))
5203 (base (c-determine-limit-get-base start try-size))
5204 (pos base)
5206 (s (parse-partial-sexp pos pos)) ; null state.
5207 stack elt size
5208 (count 0))
5209 (while (< pos start)
5210 ;; Move forward one literal each time round this loop.
5211 ;; Move forward to the start of a comment or string.
5212 (setq s (parse-partial-sexp
5214 start
5215 nil ; target-depth
5216 nil ; stop-before
5217 s ; state
5218 'syntax-table)) ; stop-comment
5220 ;; Gather details of the non-literal-bit - starting pos and size.
5221 (setq size (- (if (or (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table)))
5222 (nth 3 s))
5223 (nth 8 s)
5224 (point))
5225 pos))
5226 (if (> size 0)
5227 (setq stack (cons (cons pos size) stack)))
5229 ;; Move forward to the end of the comment/string.
5230 (if (or (and (nth 4 s) (not (eq (nth 7 s) 'syntax-table)))
5231 (nth 3 s))
5232 (setq s (parse-partial-sexp
5233 (point)
5234 start
5235 nil ; target-depth
5236 nil ; stop-before
5237 s ; state
5238 'syntax-table))) ; stop-comment
5239 (setq pos (point)))
5241 ;; Now try and find enough non-literal characters recorded on the stack.
5242 ;; Go back one recorded literal each time round this loop.
5243 (while (and (< count how-far-back)
5244 stack)
5245 (setq elt (car stack)
5246 stack (cdr stack))
5247 (setq count (+ count (cdr elt))))
5249 ;; Have we found enough yet?
5250 (cond
5251 ((>= count how-far-back)
5252 (+ (car elt) (- count how-far-back)))
5253 ((eq base (point-min))
5254 (point-min))
5255 ((> base (- start try-size)) ; Can only happen if we hit point-min.
5256 (car elt))
5258 (c-determine-limit (- how-far-back count) base try-size))))))
5260 (defun c-determine-+ve-limit (how-far &optional start-pos)
5261 ;; Return a buffer position about HOW-FAR non-literal characters forward
5262 ;; from START-POS (default point), which must not be inside a literal.
5263 (save-excursion
5264 (let ((pos (or start-pos (point)))
5265 (count how-far)
5266 (s (parse-partial-sexp (point) (point)))) ; null state
5267 (while (and (not (eobp))
5268 (> count 0))
5269 ;; Scan over counted characters.
5270 (setq s (parse-partial-sexp
5272 (min (+ pos count) (point-max))
5273 nil ; target-depth
5274 nil ; stop-before
5275 s ; state
5276 'syntax-table)) ; stop-comment
5277 (setq count (- count (- (point) pos) 1)
5278 pos (point))
5279 ;; Scan over literal characters.
5280 (if (nth 8 s)
5281 (setq s (parse-partial-sexp
5283 (point-max)
5284 nil ; target-depth
5285 nil ; stop-before
5286 s ; state
5287 'syntax-table) ; stop-comment
5288 pos (point))))
5289 (point))))
5292 ;; `c-find-decl-spots' and accompanying stuff.
5294 ;; Variables used in `c-find-decl-spots' to cache the search done for
5295 ;; the first declaration in the last call. When that function starts,
5296 ;; it needs to back up over syntactic whitespace to look at the last
5297 ;; token before the region being searched. That can sometimes cause
5298 ;; moves back and forth over a quite large region of comments and
5299 ;; macros, which would be repeated for each changed character when
5300 ;; we're called during fontification, since font-lock refontifies the
5301 ;; current line for each change. Thus it's worthwhile to cache the
5302 ;; first match.
5304 ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
5305 ;; the syntactic whitespace less or equal to some start position.
5306 ;; There's no cached value if it's nil.
5308 ;; `c-find-decl-match-pos' is the match position if
5309 ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
5310 ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
5311 (defvar c-find-decl-syntactic-pos nil)
5312 (make-variable-buffer-local 'c-find-decl-syntactic-pos)
5313 (defvar c-find-decl-match-pos nil)
5314 (make-variable-buffer-local 'c-find-decl-match-pos)
5316 (defsubst c-invalidate-find-decl-cache (change-min-pos)
5317 (and c-find-decl-syntactic-pos
5318 (< change-min-pos c-find-decl-syntactic-pos)
5319 (setq c-find-decl-syntactic-pos nil)))
5321 ; (defface c-debug-decl-spot-face
5322 ; '((t (:background "Turquoise")))
5323 ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
5324 ; (defface c-debug-decl-sws-face
5325 ; '((t (:background "Khaki")))
5326 ; "Debug face to mark the syntactic whitespace between the declaration
5327 ; spots and the preceding token end.")
5329 (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
5330 (when (facep 'c-debug-decl-spot-face)
5331 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
5332 (c-debug-add-face (max match-pos (point-min)) decl-pos
5333 'c-debug-decl-sws-face)
5334 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
5335 'c-debug-decl-spot-face))))
5336 (defmacro c-debug-remove-decl-spot-faces (beg end)
5337 (when (facep 'c-debug-decl-spot-face)
5338 `(c-save-buffer-state ()
5339 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
5340 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
5342 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5343 ;; Machinery for determining when we're at top level (this including being
5344 ;; directly inside a class or namespace, etc.)
5346 ;; We maintain a stack of brace depths in structures like classes and
5347 ;; namespaces. The car of this structure, when non-nil, indicates that the
5348 ;; associated position is within a template (etc.) structure, and the value is
5349 ;; the position where the (outermost) template ends. The other elements in
5350 ;; the structure are stacked elements, one each for each enclosing "top level"
5351 ;; structure.
5353 ;; At the very outermost level, the value of the stack would be (nil 1), the
5354 ;; "1" indicating an enclosure in a notional all-enclosing block. After
5355 ;; passing a keyword such as "namespace", the value would become (nil 0 1).
5356 ;; At this point, passing a semicolon would cause the 0 to be dropped from the
5357 ;; stack (at any other time, a semicolon is ignored). Alternatively, on
5358 ;; passing an opening brace, the stack would become (nil 1 1). Each opening
5359 ;; brace passed causes the cadr to be incremented, and passing closing braces
5360 ;; causes it to be decremented until it reaches 1. On passing a closing brace
5361 ;; when the cadr of the stack is at 1, this causes it to be removed from the
5362 ;; stack, the corresponding namespace (etc.) structure having been closed.
5364 ;; There is a special stack value -1 which means the C++ colon operator
5365 ;; introducing a list of inherited classes has just been parsed. The value
5366 ;; persists on the stack until the next open brace or semicolon.
5368 ;; When the car of the stack is non-nil, i.e. when we're in a template (etc.)
5369 ;; structure, braces are not counted. The counting resumes only after passing
5370 ;; the template's closing position, which is recorded in the car of the stack.
5372 ;; The test for being at top level consists of the cadr being 0 or 1.
5374 ;; The values of this stack throughout a buffer are cached in a simple linear
5375 ;; cache, every 5000 characters.
5377 ;; Note to maintainers: This cache mechanism is MUCH faster than recalculating
5378 ;; the stack at every entry to `c-find-decl-spots' using `c-at-toplevel-p' or
5379 ;; the like.
5380 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5381 ;; The approximate interval at which we cache the value of the brace stack.
5382 (defconst c-bs-interval 5000)
5383 ;; The list of cached values of the brace stack. Each value in the list is a
5384 ;; cons of the position it is valid for and the value of the stack as
5385 ;; described above.
5386 (defvar c-bs-cache nil)
5387 (make-variable-buffer-local 'c-bs-cache)
5388 ;; The position of the buffer at and below which entries in `c-bs-cache' are
5389 ;; valid.
5390 (defvar c-bs-cache-limit 1)
5391 (make-variable-buffer-local 'c-bs-cache-limit)
5392 ;; The previous buffer position for which the brace stack value was
5393 ;; determined.
5394 (defvar c-bs-prev-pos most-positive-fixnum)
5395 (make-variable-buffer-local 'c-bs-prev-pos)
5396 ;; The value of the brace stack at `c-bs-prev-pos'.
5397 (defvar c-bs-prev-stack nil)
5398 (make-variable-buffer-local 'c-bs-prev-stack)
5400 (defun c-init-bs-cache ()
5401 ;; Initialize the cache in `c-bs-cache' and related variables.
5402 (setq c-bs-cache nil
5403 c-bs-cache-limit 1
5404 c-bs-prev-pos most-positive-fixnum
5405 c-bs-prev-stack nil))
5407 (defun c-truncate-bs-cache (pos &rest _ignore)
5408 ;; Truncate the upper bound of the cache `c-bs-cache' to POS, if it is
5409 ;; higher than that position. This is called as either a before- or
5410 ;; after-change-function.
5411 (setq c-bs-cache-limit
5412 (min c-bs-cache-limit pos)))
5414 (defun c-update-brace-stack (stack from to)
5415 ;; Given a brace-stack which has the value STACK at position FROM, update it
5416 ;; to its value at position TO, where TO is after (or equal to) FROM.
5417 ;; Return a cons of either TO (if it is outside a literal) and this new
5418 ;; value, or of the next position after TO outside a literal and the new
5419 ;; value.
5420 (let (match kwd-sym (prev-match-pos 1)
5421 (s (cdr stack))
5422 (bound-<> (car stack)))
5423 (save-excursion
5424 (cond
5425 ((and bound-<> (<= to bound-<>))
5426 (goto-char to)) ; Nothing to do.
5427 (bound-<>
5428 (goto-char bound-<>)
5429 (setq bound-<> nil))
5430 (t (goto-char from)))
5431 (while (and (< (point) to)
5432 (c-syntactic-re-search-forward
5433 (if (<= (car s) 0)
5434 c-brace-stack-thing-key
5435 c-brace-stack-no-semi-key)
5436 to 'after-literal)
5437 (> (point) prev-match-pos)) ; prevent infinite loop.
5438 (setq prev-match-pos (point))
5439 (setq match (match-string-no-properties 1)
5440 kwd-sym (c-keyword-sym match))
5441 (cond
5442 ((and (equal match "{")
5443 (progn (backward-char)
5444 (prog1 (looking-at "\\s(")
5445 (forward-char))))
5446 (setq s (if s
5447 (cons (if (<= (car s) 0)
5449 (1+ (car s)))
5450 (cdr s))
5451 (list 1))))
5452 ((and (equal match "}")
5453 (progn (backward-char)
5454 (prog1 (looking-at "\\s)")
5455 (forward-char))))
5456 (setq s
5457 (cond
5458 ((and s (> (car s) 1))
5459 (cons (1- (car s)) (cdr s)))
5460 ((and (cdr s) (eq (car s) 1))
5461 (cdr s))
5462 (t s))))
5463 ((and (equal match "<")
5464 (progn (backward-char)
5465 (prog1 (looking-at "\\s(")
5466 (forward-char))))
5467 (backward-char)
5468 (if (c-forward-<>-arglist nil) ; Should always work.
5469 (when (> (point) to)
5470 (setq bound-<> (point)))
5471 (forward-char)))
5472 ((and (equal match ":")
5474 (eq (car s) 0))
5475 (setq s (cons -1 (cdr s))))
5476 ((and (equal match ",")
5477 (eq (car s) -1))) ; at "," in "class foo : bar, ..."
5478 ((member match '(";" "," ")"))
5479 (when (and s (cdr s) (<= (car s) 0))
5480 (setq s (cdr s))))
5481 ((c-keyword-member kwd-sym 'c-flat-decl-block-kwds)
5482 (push 0 s))))
5483 ;; The failing `c-syntactic-re-search-forward' may have left us in the
5484 ;; middle of a token, which might be a significant token. Fix this!
5485 (c-beginning-of-current-token)
5486 (cons (point)
5487 (cons bound-<> s)))))
5489 (defun c-brace-stack-at (here)
5490 ;; Given a buffer position HERE, Return the value of the brace stack there.
5491 (save-excursion
5492 (save-restriction
5493 (widen)
5494 (let ((c c-bs-cache)
5495 (can-use-prev (<= c-bs-prev-pos c-bs-cache-limit))
5496 elt stack pos npos high-elt)
5497 ;; Trim the cache to take account of buffer changes.
5498 (while (and c
5499 (> (caar c) c-bs-cache-limit))
5500 (setq c (cdr c)))
5501 (setq c-bs-cache c)
5503 (while (and c
5504 (> (caar c) here))
5505 (setq high-elt (car c))
5506 (setq c (cdr c)))
5507 (setq pos (or (and c (caar c))
5508 (point-min)))
5510 (setq elt (if c
5511 (car c)
5512 (cons (point-min)
5513 (cons nil (list 1)))))
5514 (when (not high-elt)
5515 (setq stack (cdr elt))
5516 (while
5517 ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration.
5518 (<= (setq npos (+ pos c-bs-interval)) here)
5519 (setq elt (c-update-brace-stack stack pos npos))
5520 (setq npos (car elt))
5521 (setq stack (cdr elt))
5522 (unless (eq npos (point-max)) ; NPOS could be in a literal at EOB.
5523 (setq c-bs-cache (cons elt c-bs-cache)))
5524 (setq pos npos)))
5526 (if (> pos c-bs-cache-limit)
5527 (setq c-bs-cache-limit pos))
5529 ;; Can we just use the previous value?
5530 (if (and can-use-prev
5531 (<= c-bs-prev-pos here)
5532 (> c-bs-prev-pos (car elt)))
5533 (setq pos c-bs-prev-pos
5534 stack c-bs-prev-stack)
5535 (setq pos (car elt)
5536 stack (cdr elt)))
5537 (if (> here c-bs-cache-limit)
5538 (setq c-bs-cache-limit here))
5539 (setq elt (c-update-brace-stack stack pos here)
5540 c-bs-prev-pos (car elt)
5541 c-bs-prev-stack (cdr elt))))))
5543 (defun c-bs-at-toplevel-p (here)
5544 ;; Is position HERE at the top level, as indicated by the brace stack?
5545 (let ((stack (c-brace-stack-at here)))
5546 (or (null stack) ; Probably unnecessary.
5547 (<= (cadr stack) 1))))
5549 (defmacro c-find-decl-prefix-search ()
5550 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
5551 ;; but it contains lots of free variables that refer to things
5552 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
5553 ;; if there is a match, otherwise at `cfd-limit'.
5555 ;; The macro moves point forward to the next putative start of a declaration
5556 ;; or cfd-limit. This decl start is the next token after a "declaration
5557 ;; prefix". The declaration prefix is the earlier of `cfd-prop-match' and
5558 ;; `cfd-re-match'. `cfd-match-pos' is set to the decl prefix.
5560 ;; This macro might do hidden buffer changes.
5562 '(progn
5563 ;; Find the next property match position if we haven't got one already.
5564 (unless cfd-prop-match
5565 (save-excursion
5566 (while (progn
5567 (goto-char (c-next-single-property-change
5568 (point) 'c-type nil cfd-limit))
5569 (and (< (point) cfd-limit)
5570 (not (eq (c-get-char-property (1- (point)) 'c-type)
5571 'c-decl-end)))))
5572 (setq cfd-prop-match (point))))
5574 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
5575 ;; got one already.
5576 (unless cfd-re-match
5578 (if (> cfd-re-match-end (point))
5579 (goto-char cfd-re-match-end))
5581 ;; Each time round, the next `while' moves forward over a pseudo match
5582 ;; of `c-decl-prefix-or-start-re' which is either inside a literal, or
5583 ;; is a ":" not preceded by "public", etc.. `cfd-re-match' and
5584 ;; `cfd-re-match-end' get set.
5585 (while
5586 (progn
5587 (setq cfd-re-match-end (re-search-forward c-decl-prefix-or-start-re
5588 cfd-limit 'move))
5589 (cond
5590 ((null cfd-re-match-end)
5591 ;; No match. Finish up and exit the loop.
5592 (setq cfd-re-match cfd-limit)
5593 nil)
5594 ((c-got-face-at
5595 (if (setq cfd-re-match (match-end 1))
5596 ;; Matched the end of a token preceding a decl spot.
5597 (progn
5598 (goto-char cfd-re-match)
5599 (1- cfd-re-match))
5600 ;; Matched a token that start a decl spot.
5601 (goto-char (match-beginning 0))
5602 (point))
5603 c-literal-faces)
5604 ;; Pseudo match inside a comment or string literal. Skip out
5605 ;; of comments and string literals.
5606 (while (progn
5607 (goto-char (c-next-single-property-change
5608 (point) 'face nil cfd-limit))
5609 (and (< (point) cfd-limit)
5610 (c-got-face-at (point) c-literal-faces))))
5611 t) ; Continue the loop over pseudo matches.
5612 ((and c-opt-identifier-concat-key
5613 (match-string 1)
5614 (save-excursion
5615 (goto-char (match-beginning 1))
5616 (save-match-data
5617 (looking-at c-opt-identifier-concat-key))))
5618 ;; Found, e.g., "::" in C++
5620 ((and (match-string 1)
5621 (string= (match-string 1) ":")
5622 (save-excursion
5623 (or (/= (c-backward-token-2 2) 0) ; no search limit. :-(
5624 (not (looking-at c-decl-start-colon-kwd-re)))))
5625 ;; Found a ":" which isn't part of "public:", etc.
5627 (t nil)))) ;; Found a real match. Exit the pseudo-match loop.
5629 ;; If our match was at the decl start, we have to back up over the
5630 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
5631 ;; any decl spots in the syntactic ws.
5632 (unless cfd-re-match
5633 (c-backward-syntactic-ws)
5634 (setq cfd-re-match (point))))
5636 ;; Choose whichever match is closer to the start.
5637 (if (< cfd-re-match cfd-prop-match)
5638 (setq cfd-match-pos cfd-re-match
5639 cfd-re-match nil)
5640 (setq cfd-match-pos cfd-prop-match
5641 cfd-prop-match nil))
5642 (setq cfd-top-level (c-bs-at-toplevel-p cfd-match-pos))
5644 (goto-char cfd-match-pos)
5646 (when (< cfd-match-pos cfd-limit)
5647 ;; Skip forward past comments only so we don't skip macros.
5648 (c-forward-comments)
5649 ;; Set the position to continue at. We can avoid going over
5650 ;; the comments skipped above a second time, but it's possible
5651 ;; that the comment skipping has taken us past `cfd-prop-match'
5652 ;; since the property might be used inside comments.
5653 (setq cfd-continue-pos (if cfd-prop-match
5654 (min cfd-prop-match (point))
5655 (point))))))
5657 (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
5658 ;; Call CFD-FUN for each possible spot for a declaration, cast or
5659 ;; label from the point to CFD-LIMIT.
5661 ;; CFD-FUN is called with point at the start of the spot. It's passed three
5662 ;; arguments: The first is the end position of the token preceding the spot,
5663 ;; or 0 for the implicit match at bob. The second is a flag that is t when
5664 ;; the match is inside a macro. The third is a flag that is t when the
5665 ;; match is at "top level", i.e. outside any brace block, or directly inside
5666 ;; a class or namespace, etc. Point should be moved forward by at least one
5667 ;; token.
5669 ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
5670 ;; it should return non-nil to ensure that the next search will find them.
5672 ;; Such a spot is:
5673 ;; o The first token after bob.
5674 ;; o The first token after the end of submatch 1 in
5675 ;; `c-decl-prefix-or-start-re' when that submatch matches. This
5676 ;; submatch is typically a (L or R) brace or paren, a ;, or a ,.
5677 ;; o The start of each `c-decl-prefix-or-start-re' match when
5678 ;; submatch 1 doesn't match. This is, for example, the keyword
5679 ;; "class" in Pike.
5680 ;; o The start of a previously recognized declaration; "recognized"
5681 ;; means that the last char of the previous token has a `c-type'
5682 ;; text property with the value `c-decl-end'; this only holds
5683 ;; when `c-type-decl-end-used' is set.
5685 ;; Only a spot that match CFD-DECL-RE and whose face is in the
5686 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
5687 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
5689 ;; If the match is inside a macro then the buffer is narrowed to the
5690 ;; end of it, so that CFD-FUN can investigate the following tokens
5691 ;; without matching something that begins inside a macro and ends
5692 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
5693 ;; CFD-FACE-CHECKLIST checks exist.
5695 ;; The spots are visited approximately in order from top to bottom.
5696 ;; It's however the positions where `c-decl-prefix-or-start-re'
5697 ;; matches and where `c-decl-end' properties are found that are in
5698 ;; order. Since the spots often are at the following token, they
5699 ;; might be visited out of order insofar as more spots are reported
5700 ;; later on within the syntactic whitespace between the match
5701 ;; positions and their spots.
5703 ;; It's assumed that comments and strings are fontified in the
5704 ;; searched range.
5706 ;; This is mainly used in fontification, and so has an elaborate
5707 ;; cache to handle repeated calls from the same start position; see
5708 ;; the variables above.
5710 ;; All variables in this function begin with `cfd-' to avoid name
5711 ;; collision with the (dynamically bound) variables used in CFD-FUN.
5713 ;; This function might do hidden buffer changes.
5715 (let ((cfd-start-pos (point)) ; never changed
5716 (cfd-buffer-end (point-max))
5717 ;; The end of the token preceding the decl spot last found
5718 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
5719 ;; no match.
5720 cfd-re-match
5721 ;; The end position of the last `c-decl-prefix-or-start-re'
5722 ;; match. If this is greater than `cfd-continue-pos', the
5723 ;; next regexp search is started here instead.
5724 (cfd-re-match-end (point-min))
5725 ;; The end of the last `c-decl-end' found by
5726 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
5727 ;; match. If searching for the property isn't needed then we
5728 ;; disable it by setting it to `cfd-limit' directly.
5729 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
5730 ;; The end of the token preceding the decl spot last found by
5731 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
5732 ;; bob. `cfd-limit' if there's no match. In other words,
5733 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
5734 (cfd-match-pos cfd-limit)
5735 ;; The position to continue searching at.
5736 cfd-continue-pos
5737 ;; The position of the last "real" token we've stopped at.
5738 ;; This can be greater than `cfd-continue-pos' when we get
5739 ;; hits inside macros or at `c-decl-end' positions inside
5740 ;; comments.
5741 (cfd-token-pos 0)
5742 ;; The end position of the last entered macro.
5743 (cfd-macro-end 0)
5744 ;; Whether the last position returned from `c-find-decl-prefix-search'
5745 ;; is at the top-level (including directly in a class or namespace,
5746 ;; etc.).
5747 (cfd-top-level (c-bs-at-toplevel-p (point))))
5749 ;; Initialize by finding a syntactically relevant start position
5750 ;; before the point, and do the first `c-decl-prefix-or-start-re'
5751 ;; search unless we're at bob.
5753 (let (start-in-literal start-in-macro syntactic-pos)
5754 ;; Must back up a bit since we look for the end of the previous
5755 ;; statement or declaration, which is earlier than the first
5756 ;; returned match.
5758 ;; This `cond' moves back over any literals or macros. It has special
5759 ;; handling for when the region being searched is entirely within a
5760 ;; macro. It sets `cfd-continue-pos' (unless we've reached
5761 ;; `cfd-limit').
5762 (cond
5763 ;; First we need to move to a syntactically relevant position.
5764 ;; Begin by backing out of comment or string literals.
5766 ;; This arm of the cond actually triggers if we're in a literal,
5767 ;; and cfd-limit is at most at BONL.
5768 ((and
5769 ;; This arm of the `and' moves backwards out of a literal when
5770 ;; the face at point is a literal face. In this case, its value
5771 ;; is always non-nil.
5772 (when (c-got-face-at (point) c-literal-faces)
5773 ;; Try to use the faces to back up to the start of the
5774 ;; literal. FIXME: What if the point is on a declaration
5775 ;; inside a comment?
5776 (while (and (not (bobp))
5777 (c-got-face-at (1- (point)) c-literal-faces))
5778 (goto-char (previous-single-property-change
5779 (point) 'face nil (point-min))))
5781 ;; XEmacs doesn't fontify the quotes surrounding string
5782 ;; literals.
5783 (and (featurep 'xemacs)
5784 (eq (get-text-property (point) 'face)
5785 'font-lock-string-face)
5786 (not (bobp))
5787 (progn (backward-char)
5788 (not (looking-at c-string-limit-regexp)))
5789 (forward-char))
5791 ;; Don't trust the literal to contain only literal faces
5792 ;; (the font lock package might not have fontified the
5793 ;; start of it at all, for instance) so check that we have
5794 ;; arrived at something that looks like a start or else
5795 ;; resort to `c-literal-limits'.
5796 (unless (looking-at c-literal-start-regexp)
5797 (let ((lit-start (c-literal-start)))
5798 (if lit-start (goto-char lit-start)))
5801 (setq start-in-literal (point))) ; end of `and' arm.
5803 ;; The start is in a literal. If the limit is in the same
5804 ;; one we don't have to find a syntactic position etc. We
5805 ;; only check that if the limit is at or before bonl to save
5806 ;; time; it covers the by far most common case when font-lock
5807 ;; refontifies the current line only.
5808 (<= cfd-limit (c-point 'bonl cfd-start-pos))
5809 (save-excursion
5810 (goto-char cfd-start-pos)
5811 (while (progn
5812 (goto-char (c-next-single-property-change
5813 (point) 'face nil cfd-limit))
5814 (and (< (point) cfd-limit)
5815 (c-got-face-at (point) c-literal-faces))))
5816 (= (point) cfd-limit))) ; end of `cond' arm condition
5818 ;; Completely inside a literal. Set up variables to trig the
5819 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
5820 ;; find a suitable start position.
5821 (setq cfd-continue-pos start-in-literal)) ; end of `cond' arm
5823 ;; Check if the region might be completely inside a macro, to
5824 ;; optimize that like the completely-inside-literal above.
5825 ((save-excursion
5826 (and (= (forward-line 1) 0)
5827 (bolp) ; forward-line has funny behavior at eob.
5828 (>= (point) cfd-limit)
5829 (progn (backward-char)
5830 (eq (char-before) ?\\))))
5831 ;; (Maybe) completely inside a macro. Only need to trig the
5832 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
5833 ;; set things up.
5834 (setq cfd-continue-pos (1- cfd-start-pos)
5835 start-in-macro t))
5837 ;; The default arm of the `cond' moves back over any macro we're in
5838 ;; and over any syntactic WS. It sets `c-find-decl-syntactic-pos'.
5840 ;; Back out of any macro so we don't miss any declaration
5841 ;; that could follow after it.
5842 (when (c-beginning-of-macro)
5843 (setq start-in-macro t))
5845 ;; Now we're at a proper syntactically relevant position so we
5846 ;; can use the cache. But first clear it if it applied
5847 ;; further down.
5848 (c-invalidate-find-decl-cache cfd-start-pos)
5850 (setq syntactic-pos (point))
5851 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
5852 ;; Don't have to do this if the cache is relevant here,
5853 ;; typically if the same line is refontified again. If
5854 ;; we're just some syntactic whitespace further down we can
5855 ;; still use the cache to limit the skipping.
5856 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
5858 ;; If we hit `c-find-decl-syntactic-pos' and
5859 ;; `c-find-decl-match-pos' is set then we install the cached
5860 ;; values. If we hit `c-find-decl-syntactic-pos' and
5861 ;; `c-find-decl-match-pos' is nil then we know there's no decl
5862 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
5863 ;; and so we can continue the search from this point. If we
5864 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
5865 ;; the right spot to begin searching anyway.
5866 (if (and (eq (point) c-find-decl-syntactic-pos)
5867 c-find-decl-match-pos)
5868 (setq cfd-match-pos c-find-decl-match-pos
5869 cfd-continue-pos syntactic-pos)
5871 (setq c-find-decl-syntactic-pos syntactic-pos)
5873 (when (if (bobp)
5874 ;; Always consider bob a match to get the first
5875 ;; declaration in the file. Do this separately instead of
5876 ;; letting `c-decl-prefix-or-start-re' match bob, so that
5877 ;; regexp always can consume at least one character to
5878 ;; ensure that we won't get stuck in an infinite loop.
5879 (setq cfd-re-match 0)
5880 (backward-char)
5881 (c-beginning-of-current-token)
5882 (< (point) cfd-limit))
5883 ;; Do an initial search now. In the bob case above it's
5884 ;; only done to search for a `c-decl-end' spot.
5885 (c-find-decl-prefix-search)) ; sets cfd-continue-pos
5887 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
5888 cfd-match-pos))))) ; end of `cond'
5890 ;; Advance `cfd-continue-pos' if it's before the start position.
5891 ;; The closest continue position that might have effect at or
5892 ;; after the start depends on what we started in. This also
5893 ;; finds a suitable start position in the special cases when the
5894 ;; region is completely within a literal or macro.
5895 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
5897 (cond
5898 (start-in-macro
5899 ;; If we're in a macro then it's the closest preceding token
5900 ;; in the macro. Check this before `start-in-literal',
5901 ;; since if we're inside a literal in a macro, the preceding
5902 ;; token is earlier than any `c-decl-end' spot inside the
5903 ;; literal (comment).
5904 (goto-char (or start-in-literal cfd-start-pos))
5905 ;; The only syntactic ws in macros are comments.
5906 (c-backward-comments)
5907 (backward-char)
5908 (c-beginning-of-current-token))
5910 (start-in-literal
5911 ;; If we're in a comment it can only be the closest
5912 ;; preceding `c-decl-end' position within that comment, if
5913 ;; any. Go back to the beginning of such a property so that
5914 ;; `c-find-decl-prefix-search' will find the end of it.
5915 ;; (Can't stop at the end and install it directly on
5916 ;; `cfd-prop-match' since that variable might be cleared
5917 ;; after `cfd-fun' below.)
5919 ;; Note that if the literal is a string then the property
5920 ;; search will simply skip to the beginning of it right
5921 ;; away.
5922 (if (not c-type-decl-end-used)
5923 (goto-char start-in-literal)
5924 (goto-char cfd-start-pos)
5925 (while (progn
5926 (goto-char (previous-single-property-change
5927 (point) 'c-type nil start-in-literal))
5928 (and (> (point) start-in-literal)
5929 (not (eq (c-get-char-property (point) 'c-type)
5930 'c-decl-end))))))
5932 (when (= (point) start-in-literal)
5933 ;; Didn't find any property inside the comment, so we can
5934 ;; skip it entirely. (This won't skip past a string, but
5935 ;; that'll be handled quickly by the next
5936 ;; `c-find-decl-prefix-search' anyway.)
5937 (c-forward-single-comment)
5938 (if (> (point) cfd-limit)
5939 (goto-char cfd-limit))))
5942 ;; If we started in normal code, the only match that might
5943 ;; apply before the start is what we already got in
5944 ;; `cfd-match-pos' so we can continue at the start position.
5945 ;; (Note that we don't get here if the first match is below
5946 ;; it.)
5947 (goto-char cfd-start-pos))) ; end of `cond'
5949 ;; Delete found matches if they are before our new continue
5950 ;; position, so that `c-find-decl-prefix-search' won't back up
5951 ;; to them later on.
5952 (setq cfd-continue-pos (point))
5953 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
5954 (setq cfd-re-match nil))
5955 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
5956 (setq cfd-prop-match nil))) ; end of `when'
5958 (if syntactic-pos
5959 ;; This is the normal case and we got a proper syntactic
5960 ;; position. If there's a match then it's always outside
5961 ;; macros and comments, so advance to the next token and set
5962 ;; `cfd-token-pos'. The loop below will later go back using
5963 ;; `cfd-continue-pos' to fix declarations inside the
5964 ;; syntactic ws.
5965 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
5966 (goto-char syntactic-pos)
5967 (c-forward-syntactic-ws)
5968 (and cfd-continue-pos
5969 (< cfd-continue-pos (point))
5970 (setq cfd-token-pos (point))))
5972 ;; Have one of the special cases when the region is completely
5973 ;; within a literal or macro. `cfd-continue-pos' is set to a
5974 ;; good start position for the search, so do it.
5975 (c-find-decl-prefix-search)))
5977 ;; Now loop, one decl spot per iteration. We already have the first
5978 ;; match in `cfd-match-pos'.
5979 (while (progn
5980 ;; Go forward over "false matches", one per iteration.
5981 (while (and
5982 (< cfd-match-pos cfd-limit)
5985 ;; Kludge to filter out matches on the "<" that
5986 ;; aren't open parens, for the sake of languages
5987 ;; that got `c-recognize-<>-arglists' set.
5988 (and (eq (char-before cfd-match-pos) ?<)
5989 (not (c-get-char-property (1- cfd-match-pos)
5990 'syntax-table)))
5992 ;; If `cfd-continue-pos' is less or equal to
5993 ;; `cfd-token-pos', we've got a hit inside a macro
5994 ;; that's in the syntactic whitespace before the last
5995 ;; "real" declaration we've checked. If they're equal
5996 ;; we've arrived at the declaration a second time, so
5997 ;; there's nothing to do.
5998 (= cfd-continue-pos cfd-token-pos)
6000 (progn
6001 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
6002 ;; we're still searching for declarations embedded in
6003 ;; the syntactic whitespace. In that case we need
6004 ;; only to skip comments and not macros, since they
6005 ;; can't be nested, and that's already been done in
6006 ;; `c-find-decl-prefix-search'.
6007 (when (> cfd-continue-pos cfd-token-pos)
6008 (c-forward-syntactic-ws)
6009 (setq cfd-token-pos (point)))
6011 ;; Continue if the following token fails the
6012 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
6013 (when (or (>= (point) cfd-limit)
6014 (not (looking-at cfd-decl-re))
6015 (and cfd-face-checklist
6016 (not (c-got-face-at
6017 (point) cfd-face-checklist))))
6018 (goto-char cfd-continue-pos)
6019 t)))
6021 (< (point) cfd-limit)) ; end of "false matches" condition
6022 (c-find-decl-prefix-search)) ; end of "false matches" loop
6024 (< (point) cfd-limit)) ; end of condition for "decl-spot" while
6026 (when (and
6027 (>= (point) cfd-start-pos)
6029 (progn
6030 ;; Narrow to the end of the macro if we got a hit inside
6031 ;; one, to avoid recognizing things that start inside the
6032 ;; macro and end outside it.
6033 (when (> cfd-match-pos cfd-macro-end)
6034 ;; Not in the same macro as in the previous round.
6035 (save-excursion
6036 (goto-char cfd-match-pos)
6037 (setq cfd-macro-end
6038 (if (save-excursion (and (c-beginning-of-macro)
6039 (< (point) cfd-match-pos)))
6040 (progn (c-end-of-macro)
6041 (point))
6042 0))))
6044 (if (zerop cfd-macro-end)
6046 (if (> cfd-macro-end (point))
6047 (progn (narrow-to-region (point-min) cfd-macro-end)
6049 ;; The matched token was the last thing in the macro,
6050 ;; so the whole match is bogus.
6051 (setq cfd-macro-end 0)
6052 nil)))) ; end of when condition
6054 (when (> cfd-macro-end 0)
6055 (setq cfd-top-level nil)) ; In a macro is "never" at top level.
6056 (c-debug-put-decl-spot-faces cfd-match-pos (point))
6057 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0) cfd-top-level)
6058 (setq cfd-prop-match nil))
6060 (when (/= cfd-macro-end 0)
6061 ;; Restore limits if we did macro narrowing above.
6062 (narrow-to-region (point-min) cfd-buffer-end)))
6064 (goto-char cfd-continue-pos)
6065 (if (= cfd-continue-pos cfd-limit)
6066 (setq cfd-match-pos cfd-limit)
6067 (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
6068 ; cfd-match-pos, etc.
6071 ;; A cache for found types.
6073 ;; Buffer local variable that contains an obarray with the types we've
6074 ;; found. If a declaration is recognized somewhere we record the
6075 ;; fully qualified identifier in it to recognize it as a type
6076 ;; elsewhere in the file too. This is not accurate since we do not
6077 ;; bother with the scoping rules of the languages, but in practice the
6078 ;; same name is seldom used as both a type and something else in a
6079 ;; file, and we only use this as a last resort in ambiguous cases (see
6080 ;; `c-forward-decl-or-cast-1').
6082 ;; Not every type need be in this cache. However, things which have
6083 ;; ceased to be types must be removed from it.
6085 ;; Template types in C++ are added here too but with the template
6086 ;; arglist replaced with "<>" in references or "<" for the one in the
6087 ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
6088 ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
6089 ;; template specs can be fairly sized programs in themselves) and
6090 ;; improves the hit ratio (it's a type regardless of the template
6091 ;; args; it's just not the same type, but we're only interested in
6092 ;; recognizing types, not telling distinct types apart). Note that
6093 ;; template types in references are added here too; from the example
6094 ;; above there will also be an entry "Foo<".
6095 (defvar c-found-types nil)
6096 (make-variable-buffer-local 'c-found-types)
6098 (defsubst c-clear-found-types ()
6099 ;; Clears `c-found-types'.
6100 (setq c-found-types
6101 (make-hash-table :test #'equal :weakness nil)))
6103 (defun c-add-type (from to)
6104 ;; Add the given region as a type in `c-found-types'. If the region
6105 ;; doesn't match an existing type but there is a type which is equal
6106 ;; to the given one except that the last character is missing, then
6107 ;; the shorter type is removed. That's done to avoid adding all
6108 ;; prefixes of a type as it's being entered and font locked. This
6109 ;; doesn't cover cases like when characters are removed from a type
6110 ;; or added in the middle. We'd need the position of point when the
6111 ;; font locking is invoked to solve this well.
6113 ;; This function might do hidden buffer changes.
6114 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
6115 (unless (gethash type c-found-types)
6116 (remhash (substring type 0 -1) c-found-types)
6117 (puthash type t c-found-types))))
6119 (defun c-unfind-type (name)
6120 ;; Remove the "NAME" from c-found-types, if present.
6121 (remhash name c-found-types))
6123 (defsubst c-check-type (from to)
6124 ;; Return non-nil if the given region contains a type in
6125 ;; `c-found-types'.
6127 ;; This function might do hidden buffer changes.
6128 (gethash (c-syntactic-content from to c-recognize-<>-arglists) c-found-types))
6130 (defun c-list-found-types ()
6131 ;; Return all the types in `c-found-types' as a sorted list of
6132 ;; strings.
6133 (let (type-list)
6134 (maphash (lambda (type _)
6135 (setq type-list (cons type type-list)))
6136 c-found-types)
6137 (sort type-list 'string-lessp)))
6139 ;; Shut up the byte compiler.
6140 (defvar c-maybe-stale-found-type)
6142 (defun c-trim-found-types (beg end _old-len)
6143 ;; An after change function which, in conjunction with the info in
6144 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
6145 ;; from `c-found-types', should this type have become stale. For
6146 ;; example, this happens to "foo" when "foo \n bar();" becomes
6147 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
6148 ;; the fontification.
6150 ;; Have we, perhaps, added non-ws characters to the front/back of a found
6151 ;; type?
6152 (when (> end beg)
6153 (save-excursion
6154 (when (< end (point-max))
6155 (goto-char end)
6156 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
6157 (progn (goto-char end)
6158 (c-end-of-current-token)))
6159 (c-unfind-type (buffer-substring-no-properties
6160 end (point)))))
6161 (when (> beg (point-min))
6162 (goto-char beg)
6163 (if (and (c-end-of-current-token) ; only moves when we started in the middle
6164 (progn (goto-char beg)
6165 (c-beginning-of-current-token)))
6166 (c-unfind-type (buffer-substring-no-properties
6167 (point) beg))))))
6169 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
6170 (cond
6171 ;; Changing the amount of (already existing) whitespace - don't do anything.
6172 ((and (c-partial-ws-p beg end)
6173 (or (= beg end) ; removal of WS
6174 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
6176 ;; The syntactic relationship which defined a "found type" has been
6177 ;; destroyed.
6178 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
6179 (c-unfind-type (cadr c-maybe-stale-found-type)))
6180 ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
6184 ;; Setting and removing syntax properties on < and > in languages (C++
6185 ;; and Java) where they can be template/generic delimiters as well as
6186 ;; their normal meaning of "less/greater than".
6188 ;; Normally, < and > have syntax 'punctuation'. When they are found to
6189 ;; be delimiters, they are marked as such with the category properties
6190 ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
6192 ;; STRATEGY:
6194 ;; It is impossible to determine with certainty whether a <..> pair in
6195 ;; C++ is two comparison operators or is template delimiters, unless
6196 ;; one duplicates a lot of a C++ compiler. For example, the following
6197 ;; code fragment:
6199 ;; foo (a < b, c > d) ;
6201 ;; could be a function call with two integer parameters (each a
6202 ;; relational expression), or it could be a constructor for class foo
6203 ;; taking one parameter d of templated type "a < b, c >". They are
6204 ;; somewhat easier to distinguish in Java.
6206 ;; The strategy now (2010-01) adopted is to mark and unmark < and
6207 ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
6208 ;; individually when their context so indicated. This gave rise to
6209 ;; intractable problems when one of a matching pair was deleted, or
6210 ;; pulled into a literal.]
6212 ;; At each buffer change, the syntax-table properties are removed in a
6213 ;; before-change function and reapplied, when needed, in an
6214 ;; after-change function. It is far more important that the
6215 ;; properties get removed when they they are spurious than that they
6216 ;; be present when wanted.
6217 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6218 (defun c-clear-<-pair-props (&optional pos)
6219 ;; POS (default point) is at a < character. If it is marked with
6220 ;; open paren syntax-table text property, remove the property,
6221 ;; together with the close paren property on the matching > (if
6222 ;; any).
6223 (save-excursion
6224 (if pos
6225 (goto-char pos)
6226 (setq pos (point)))
6227 (when (equal (c-get-char-property (point) 'syntax-table)
6228 c-<-as-paren-syntax)
6229 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6230 (c-go-list-forward))
6231 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
6232 c->-as-paren-syntax) ; should always be true.
6233 (c-unmark-<->-as-paren (1- (point))))
6234 (c-unmark-<->-as-paren pos))))
6236 (defun c-clear->-pair-props (&optional pos)
6237 ;; POS (default point) is at a > character. If it is marked with
6238 ;; close paren syntax-table property, remove the property, together
6239 ;; with the open paren property on the matching < (if any).
6240 (save-excursion
6241 (if pos
6242 (goto-char pos)
6243 (setq pos (point)))
6244 (when (equal (c-get-char-property (point) 'syntax-table)
6245 c->-as-paren-syntax)
6246 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6247 (c-go-up-list-backward))
6248 (when (equal (c-get-char-property (point) 'syntax-table)
6249 c-<-as-paren-syntax) ; should always be true.
6250 (c-unmark-<->-as-paren (point)))
6251 (c-unmark-<->-as-paren pos))))
6253 (defun c-clear-<>-pair-props (&optional pos)
6254 ;; POS (default point) is at a < or > character. If it has an
6255 ;; open/close paren syntax-table property, remove this property both
6256 ;; from the current character and its partner (which will also be
6257 ;; thusly marked).
6258 (cond
6259 ((eq (char-after) ?\<)
6260 (c-clear-<-pair-props pos))
6261 ((eq (char-after) ?\>)
6262 (c-clear->-pair-props pos))
6263 (t (c-benign-error
6264 "c-clear-<>-pair-props called from wrong position"))))
6266 (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
6267 ;; POS (default point) is at a < character. If it is both marked
6268 ;; with open/close paren syntax-table property, and has a matching >
6269 ;; (also marked) which is after LIM, remove the property both from
6270 ;; the current > and its partner. Return t when this happens, nil
6271 ;; when it doesn't.
6272 (save-excursion
6273 (if pos
6274 (goto-char pos)
6275 (setq pos (point)))
6276 (when (equal (c-get-char-property (point) 'syntax-table)
6277 c-<-as-paren-syntax)
6278 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6279 (c-go-list-forward))
6280 (when (and (>= (point) lim)
6281 (equal (c-get-char-property (1- (point)) 'syntax-table)
6282 c->-as-paren-syntax)) ; should always be true.
6283 (c-unmark-<->-as-paren (1- (point)))
6284 (c-unmark-<->-as-paren pos))
6285 t)))
6287 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
6288 ;; POS (default point) is at a > character. If it is both marked
6289 ;; with open/close paren syntax-table property, and has a matching <
6290 ;; (also marked) which is before LIM, remove the property both from
6291 ;; the current < and its partner. Return t when this happens, nil
6292 ;; when it doesn't.
6293 (save-excursion
6294 (if pos
6295 (goto-char pos)
6296 (setq pos (point)))
6297 (when (equal (c-get-char-property (point) 'syntax-table)
6298 c->-as-paren-syntax)
6299 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
6300 (c-go-up-list-backward))
6301 (when (and (<= (point) lim)
6302 (equal (c-get-char-property (point) 'syntax-table)
6303 c-<-as-paren-syntax)) ; should always be true.
6304 (c-unmark-<->-as-paren (point))
6305 (c-unmark-<->-as-paren pos))
6306 t)))
6308 ;; Set by c-common-init in cc-mode.el.
6309 (defvar c-new-BEG)
6310 (defvar c-new-END)
6311 ;; Set by c-after-change in cc-mode.el.
6312 (defvar c-old-BEG)
6313 (defvar c-old-END)
6315 (defun c-before-change-check-<>-operators (beg end)
6316 ;; Unmark certain pairs of "< .... >" which are currently marked as
6317 ;; template/generic delimiters. (This marking is via syntax-table text
6318 ;; properties), and expand the (c-new-BEG c-new-END) region to include all
6319 ;; unmarked < and > operators within the certain bounds (see below).
6321 ;; These pairs are those which are in the current "statement" (i.e.,
6322 ;; the region between the {, }, or ; before BEG and the one after
6323 ;; END), and which enclose any part of the interval (BEG END).
6325 ;; Note that in C++ (?and Java), template/generic parens cannot
6326 ;; enclose a brace or semicolon, so we use these as bounds on the
6327 ;; region we must work on.
6329 ;; This function is called from before-change-functions (via
6330 ;; c-get-state-before-change-functions). Thus the buffer is widened,
6331 ;; and point is undefined, both at entry and exit.
6333 ;; FIXME!!! This routine ignores the possibility of macros entirely.
6334 ;; 2010-01-29.
6335 (save-excursion
6336 (c-save-buffer-state
6337 ((beg-lit-start (progn (goto-char beg) (c-literal-start)))
6338 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
6339 new-beg new-end beg-limit end-limit)
6340 ;; Locate the earliest < after the barrier before the changed region,
6341 ;; which isn't already marked as a paren.
6342 (goto-char (or beg-lit-start beg))
6343 (setq beg-limit (c-determine-limit 512))
6345 ;; Remove the syntax-table/category properties from each pertinent <...>
6346 ;; pair. Firstly, the ones with the < before beg and > after beg....
6347 (while (progn (c-syntactic-skip-backward "^;{}<" beg-limit)
6348 (eq (char-before) ?<))
6349 (c-backward-token-2)
6350 (when (eq (char-after) ?<)
6351 (c-clear-<-pair-props-if-match-after beg)
6352 (setq new-beg (point))))
6353 (c-forward-syntactic-ws)
6355 ;; ...Then the ones with < before end and > after end.
6356 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
6357 (setq end-limit (c-determine-+ve-limit 512))
6358 (while (and (c-syntactic-re-search-forward "[;{}>]" end-limit 'end)
6359 (eq (char-before) ?>))
6360 (c-end-of-current-token)
6361 (when (eq (char-before) ?>)
6362 (c-clear->-pair-props-if-match-before end (1- (point)))
6363 (setq new-end (point))))
6364 (c-backward-syntactic-ws)
6366 ;; Extend the fontification region, if needed.
6367 (and new-beg
6368 (< new-beg c-new-BEG)
6369 (setq c-new-BEG new-beg))
6370 (and new-end
6371 (> new-end c-new-END)
6372 (setq c-new-END new-end)))))
6374 (defun c-after-change-check-<>-operators (beg end)
6375 ;; This is called from `after-change-functions' when
6376 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
6377 ;; chars with paren syntax become part of another operator like "<<"
6378 ;; or ">=".
6380 ;; This function might do hidden buffer changes.
6382 (save-excursion
6383 (goto-char beg)
6384 (when (or (looking-at "[<>]")
6385 (< (skip-chars-backward "<>") 0))
6387 (goto-char beg)
6388 (c-beginning-of-current-token)
6389 (when (and (< (point) beg)
6390 (looking-at c-<>-multichar-token-regexp)
6391 (< beg (setq beg (match-end 0))))
6392 (while (progn (skip-chars-forward "^<>" beg)
6393 (< (point) beg))
6394 (c-clear-<>-pair-props)
6395 (forward-char))))
6397 (when (< beg end)
6398 (goto-char end)
6399 (when (or (looking-at "[<>]")
6400 (< (skip-chars-backward "<>") 0))
6402 (goto-char end)
6403 (c-beginning-of-current-token)
6404 (when (and (< (point) end)
6405 (looking-at c-<>-multichar-token-regexp)
6406 (< end (setq end (match-end 0))))
6407 (while (progn (skip-chars-forward "^<>" end)
6408 (< (point) end))
6409 (c-clear-<>-pair-props)
6410 (forward-char)))))))
6412 (defvar c-restricted-<>-arglists) ;FIXME: Move definition here?
6413 (defvar c-parse-and-markup-<>-arglists) ;FIXME: Move definition here?
6415 (defun c-restore-<>-properties (_beg _end _old-len)
6416 ;; This function is called as an after-change function. It restores the
6417 ;; category/syntax-table properties on template/generic <..> pairs between
6418 ;; c-new-BEG and c-new-END. It may do hidden buffer changes.
6419 (c-save-buffer-state ((c-parse-and-markup-<>-arglists t)
6420 c-restricted-<>-arglists lit-limits)
6421 (goto-char c-new-BEG)
6422 (if (setq lit-limits (c-literal-limits))
6423 (goto-char (cdr lit-limits)))
6424 (while (and (< (point) c-new-END)
6425 (c-syntactic-re-search-forward "<" c-new-END 'bound))
6426 (backward-char)
6427 (save-excursion
6428 (c-backward-token-2)
6429 (setq c-restricted-<>-arglists
6430 (and (not (looking-at c-opt-<>-sexp-key))
6431 (progn (c-backward-syntactic-ws) ; to ( or ,
6432 (and (memq (char-before) '(?\( ?,)) ; what about <?
6433 (not (eq (c-get-char-property (point) 'c-type)
6434 'c-decl-arg-start)))))))
6435 (or (c-forward-<>-arglist nil)
6436 (c-forward-over-token-and-ws)
6437 (goto-char c-new-END)))))
6440 ;; Functions to handle C++ raw strings.
6442 ;; A valid C++ raw string looks like
6443 ;; R"<id>(<contents>)<id>"
6444 ;; , where <id> is an identifier from 0 to 16 characters long, not containing
6445 ;; spaces, control characters, double quote or left/right paren. <contents>
6446 ;; can include anything which isn't the terminating )<id>", including new
6447 ;; lines, "s, parentheses, etc.
6449 ;; CC Mode handles C++ raw strings by the use of `syntax-table' text
6450 ;; properties as follows:
6452 ;; (i) On a validly terminated raw string, no `syntax-table' text properties
6453 ;; are applied to the opening and closing delimiters, but any " in the
6454 ;; contents is given the property value "punctuation" (`(1)') to prevent it
6455 ;; interacting with the "s in the delimiters.
6457 ;; The font locking routine `c-font-lock-c++-raw-strings' (in cc-fonts.el)
6458 ;; recognizes valid raw strings, and fontifies the delimiters (apart from
6459 ;; the parentheses) with the default face and the parentheses and the
6460 ;; <contents> with font-lock-string-face.
6462 ;; (ii) A valid, but unterminated, raw string opening delimiter gets the
6463 ;; "punctuation" value (`(1)') of the `syntax-table' text property, and the
6464 ;; open parenthesis gets the "string fence" value (`(15)').
6466 ;; `c-font-lock-c++-raw-strings' puts c-font-lock-warning-face on the entire
6467 ;; unmatched opening delimiter (from the R up to the open paren), and allows
6468 ;; the rest of the buffer to get font-lock-string-face, caused by the
6469 ;; unmatched "string fence" `syntax-table' text property value.
6471 ;; (iii) Inside a macro, a valid raw string is handled as in (i). An
6472 ;; unmatched opening delimiter is handled slightly differently. In addition
6473 ;; to the "punctuation" and "string fence" properties on the delimiter,
6474 ;; another "string fence" `syntax-table' property is applied to the last
6475 ;; possible character of the macro before the terminating linefeed (if there
6476 ;; is such a character after the "("). This "last possible" character is
6477 ;; never a backslash escaping the end of line. If the character preceding
6478 ;; this "last possible" character is itself a backslash, this preceding
6479 ;; character gets a "punctuation" `syntax-table' value. If the "(" is
6480 ;; already at the end of the macro, it gets the "punctuation" value, and no
6481 ;; "string fence"s are used.
6483 ;; The effect on the fontification of either of these tactics is that rest of
6484 ;; the macro (if any) after the "(" gets font-lock-string-face, but the rest
6485 ;; of the file is fontified normally.
6488 (defun c-raw-string-pos ()
6489 ;; Get POINT's relationship to any containing raw string.
6490 ;; If point isn't in a raw string, return nil.
6491 ;; Otherwise, return the following list:
6493 ;; (POS B\" B\( E\) E\")
6495 ;; , where POS is the symbol `open-delim' if point is in the opening
6496 ;; delimiter, the symbol `close-delim' if it's in the closing delimiter, and
6497 ;; nil if it's in the string body. B\", B\(, E\), E\" are the positions of
6498 ;; the opening and closing quotes and parentheses of a correctly terminated
6499 ;; raw string. (N.B.: E\) and E\" are NOT on the "outside" of these
6500 ;; characters.) If the raw string is not terminated, E\) and E\" are set to
6501 ;; nil.
6503 ;; Note: this routine is dependant upon the correct syntax-table text
6504 ;; properties being set.
6505 (let ((state (c-state-semi-pp-to-literal (point)))
6506 open-quote-pos open-paren-pos close-paren-pos close-quote-pos id)
6507 (save-excursion
6508 (when
6509 (and
6510 (cond
6511 ((null (cadr state))
6512 (or (eq (char-after) ?\")
6513 (search-backward "\"" (max (- (point) 17) (point-min)) t)))
6514 ((and (eq (cadr state) 'string)
6515 (goto-char (nth 2 state))
6516 (or (eq (char-after) ?\")
6517 (search-backward "\"" (max (- (point) 17) (point-min)) t))
6518 (not (bobp)))))
6519 (eq (char-before) ?R)
6520 (looking-at "\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)("))
6521 (setq open-quote-pos (point)
6522 open-paren-pos (match-end 1)
6523 id (match-string-no-properties 1))
6524 (goto-char (1+ open-paren-pos))
6525 (when (and (not (c-get-char-property open-paren-pos 'syntax-table))
6526 (search-forward (concat ")" id "\"") nil t))
6527 (setq close-paren-pos (match-beginning 0)
6528 close-quote-pos (1- (point))))))
6529 (and open-quote-pos
6530 (list
6531 (cond
6532 ((<= (point) open-paren-pos)
6533 'open-delim)
6534 ((and close-paren-pos
6535 (> (point) close-paren-pos))
6536 'close-delim)
6537 (t nil))
6538 open-quote-pos open-paren-pos close-paren-pos close-quote-pos))))
6540 (defun c-depropertize-raw-string (id open-quote open-paren bound)
6541 ;; Point is immediately after a raw string opening delimiter. Remove any
6542 ;; `syntax-table' text properties associated with the delimiter (if it's
6543 ;; unmatched) or the raw string.
6545 ;; ID, a string, is the delimiter's identifier. OPEN-QUOTE and OPEN-PAREN
6546 ;; are the buffer positions of the delimiter's components. BOUND is the
6547 ;; bound for searching for a matching closing delimiter; it is usually nil,
6548 ;; but if we're inside a macro, it's the end of the macro.
6550 ;; Point is moved to after the (terminated) raw string, or left after the
6551 ;; unmatched opening delimiter, as the case may be. The return value is of
6552 ;; no significance.
6553 (let ((open-paren-prop (c-get-char-property open-paren 'syntax-table)))
6554 (cond
6555 ((null open-paren-prop)
6556 ;; A terminated raw string
6557 (when (search-forward (concat ")" id "\"") nil t)
6558 (let* ((closing-paren (match-beginning 0))
6559 (first-punctuation
6560 (save-match-data
6561 (goto-char (1+ open-paren))
6562 (and (c-search-forward-char-property 'syntax-table '(1)
6563 closing-paren)
6564 (1- (point)))))
6566 (when first-punctuation
6567 (c-clear-char-property-with-value
6568 first-punctuation (match-beginning 0) 'syntax-table '(1))
6569 (c-truncate-semi-nonlit-pos-cache first-punctuation)
6570 ))))
6571 ((or (and (equal open-paren-prop '(15)) (null bound))
6572 (equal open-paren-prop '(1)))
6573 ;; An unterminated raw string either not in a macro, or in a macro with
6574 ;; the open parenthesis right up against the end of macro
6575 (c-clear-char-property open-quote 'syntax-table)
6576 (c-truncate-semi-nonlit-pos-cache open-quote)
6577 (c-clear-char-property open-paren 'syntax-table))
6579 ;; An unterminated string in a macro, with at least one char after the
6580 ;; open paren
6581 (c-clear-char-property open-quote 'syntax-table)
6582 (c-truncate-semi-nonlit-pos-cache open-quote)
6583 (c-clear-char-property open-paren 'syntax-table)
6584 (let ((after-string-fence-pos
6585 (save-excursion
6586 (goto-char (1+ open-paren))
6587 (c-search-forward-char-property 'syntax-table '(15) bound))))
6588 (when after-string-fence-pos
6589 (c-clear-char-property (1- after-string-fence-pos) 'syntax-table)))
6590 ))))
6592 (defun c-depropertize-raw-strings-in-region (start finish)
6593 ;; Remove any `syntax-table' text properties associated with C++ raw strings
6594 ;; contained in the region (START FINISH). Point is undefined at entry and
6595 ;; exit, and the return value has no significance.
6596 (goto-char start)
6597 (while (and (< (point) finish)
6598 (re-search-forward
6599 (concat "\\(" ; 1
6600 c-anchored-cpp-prefix ; 2
6601 "\\)\\|\\(" ; 3
6602 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" ; 4
6603 "\\)")
6604 finish t))
6605 (when (save-excursion
6606 (goto-char (match-beginning 0)) (not (c-in-literal)))
6607 (if (match-beginning 4) ; the id
6608 ;; We've found a raw string
6609 (c-depropertize-raw-string
6610 (match-string-no-properties 4) ; id
6611 (1+ (match-beginning 3)) ; open quote
6612 (match-end 4) ; open paren
6613 nil) ; bound
6614 ;; We've found a CPP construct. Search for raw strings within it.
6615 (goto-char (match-beginning 2)) ; the "#"
6616 (c-end-of-macro)
6617 (let ((eom (point)))
6618 (goto-char (match-end 2)) ; after the "#".
6619 (while (and (< (point) eom)
6620 (c-syntactic-re-search-forward
6621 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" eom t))
6622 (c-depropertize-raw-string
6623 (match-string-no-properties 1) ; id
6624 (1+ (match-beginning 0)) ; open quote
6625 (match-end 1) ; open paren
6626 eom))))))) ; bound.
6628 (defun c-before-change-check-raw-strings (beg end)
6629 ;; This function clears `syntax-table' text properties from C++ raw strings
6630 ;; in the region (c-new-BEG c-new-END). BEG and END are the standard
6631 ;; arguments supplied to any before-change function.
6633 ;; Point is undefined on both entry and exit, and the return value has no
6634 ;; significance.
6636 ;; This function is called as a before-change function solely due to its
6637 ;; membership of the C++ value of `c-get-state-before-change-functions'.
6638 (c-save-buffer-state
6639 ((beg-rs (progn (goto-char beg) (c-raw-string-pos)))
6640 (beg-plus (if (null beg-rs)
6642 (max beg
6643 (1+ (or (nth 4 beg-rs) (nth 2 beg-rs))))))
6644 (end-rs (progn (goto-char end) (c-raw-string-pos))) ; FIXME!!!
6645 ; Optimize this so that we don't call
6646 ; `c-raw-string-pos' twice when once
6647 ; will do. (2016-06-02).
6648 (end-minus (if (null end-rs)
6650 (min end (cadr end-rs))))
6652 (when beg-rs
6653 (setq c-new-BEG (min c-new-BEG (1- (cadr beg-rs)))))
6654 (c-depropertize-raw-strings-in-region c-new-BEG beg-plus)
6656 (when end-rs
6657 (setq c-new-END (max c-new-END
6658 (1+ (or (nth 4 end-rs)
6659 (nth 2 end-rs))))))
6660 (c-depropertize-raw-strings-in-region end-minus c-new-END)))
6662 (defun c-propertize-raw-string-opener (id open-quote open-paren bound)
6663 ;; Point is immediately after a raw string opening delimiter. Apply any
6664 ;; pertinent `syntax-table' text properties to the delimiter and also the
6665 ;; raw string, should there be a valid matching closing delimiter.
6667 ;; ID, a string, is the delimiter's identifier. OPEN-QUOTE and OPEN-PAREN
6668 ;; are the buffer positions of the delimiter's components. BOUND is the
6669 ;; bound for searching for a matching closing delimiter; it is usually nil,
6670 ;; but if we're inside a macro, it's the end of the macro.
6672 ;; Point is moved to after the (terminated) raw string, or left after the
6673 ;; unmatched opening delimiter, as the case may be. The return value is of
6674 ;; no significance.
6675 (if (search-forward (concat ")" id "\"") bound t)
6676 (let ((end-string (match-beginning 0))
6677 (after-quote (match-end 0)))
6678 (goto-char open-paren)
6679 (while (progn (skip-syntax-forward "^\"" end-string)
6680 (< (point) end-string))
6681 (c-put-char-property (point) 'syntax-table '(1)) ; punctuation
6682 (c-truncate-semi-nonlit-pos-cache (point))
6683 (forward-char))
6684 (goto-char after-quote))
6685 (c-put-char-property open-quote 'syntax-table '(1)) ; punctuation
6686 (c-truncate-semi-nonlit-pos-cache open-quote)
6687 (c-put-char-property open-paren 'syntax-table '(15)) ; generic string
6688 (when bound
6689 ;; In a CPP construct, we try to apply a generic-string `syntax-table'
6690 ;; text property to the last possible character in the string, so that
6691 ;; only characters within the macro get "stringed out".
6692 (goto-char bound)
6693 (if (save-restriction
6694 (narrow-to-region (1+ open-paren) (point-max))
6695 (re-search-backward
6696 (eval-when-compile
6697 ;; This regular expression matches either an escape pair (which
6698 ;; isn't an escaped NL) (submatch 5) or a non-escaped character
6699 ;; (which isn't itself a backslash) (submatch 10). The long
6700 ;; preambles to these (respectively submatches 2-4 and 6-9)
6701 ;; ensure that we have the correct parity for sequences of
6702 ;; backslashes, etc..
6703 (concat "\\(" ; 1
6704 "\\(\\`[^\\]?\\|[^\\][^\\]\\)\\(\\\\\\(.\\|\n\\)\\)*" ; 2-4
6705 "\\(\\\\.\\)" ; 5
6706 "\\|"
6707 "\\(\\`\\|[^\\]\\|\\(\\`[^\\]?\\|[^\\][^\\]\\)\\(\\\\\\(.\\|\n\\)\\)+\\)" ; 6-9
6708 "\\([^\\]\\)" ; 10
6709 "\\)"
6710 "\\(\\\\\n\\)*\\=")) ; 11
6711 (1+ open-paren) t))
6712 (if (match-beginning 10)
6713 (progn
6714 (c-put-char-property (match-beginning 10) 'syntax-table '(15))
6715 (c-truncate-semi-nonlit-pos-cache (match-beginning 10)))
6716 (c-put-char-property (match-beginning 5) 'syntax-table '(1))
6717 (c-put-char-property (1+ (match-beginning 5)) 'syntax-table '(15))
6718 (c-truncate-semi-nonlit-pos-cache (1+ (match-beginning 5))))
6719 (c-put-char-property open-paren 'syntax-table '(1)))
6720 (goto-char bound))))
6722 (defun c-after-change-re-mark-raw-strings (_beg _end _old-len)
6723 ;; This function applies `syntax-table' text properties to C++ raw strings
6724 ;; beginning in the region (c-new-BEG c-new-END). BEG, END, and OLD-LEN are
6725 ;; the standard arguments supplied to any after-change function.
6727 ;; Point is undefined on both entry and exit, and the return value has no
6728 ;; significance.
6730 ;; This function is called as an after-change function solely due to its
6731 ;; membership of the C++ value of `c-before-font-lock-functions'.
6732 (c-save-buffer-state ()
6733 ;; If the region (c-new-BEG c-new-END) has expanded, remove
6734 ;; `syntax-table' text-properties from the new piece(s).
6735 (when (< c-new-BEG c-old-BEG)
6736 (let ((beg-rs (progn (goto-char c-old-BEG) (c-raw-string-pos))))
6737 (c-depropertize-raw-strings-in-region
6738 c-new-BEG
6739 (if beg-rs
6740 (1+ (or (nth 4 beg-rs) (nth 2 beg-rs)))
6741 c-old-BEG))))
6742 (when (> c-new-END c-old-END)
6743 (let ((end-rs (progn (goto-char c-old-END) (c-raw-string-pos))))
6744 (c-depropertize-raw-strings-in-region
6745 (if end-rs
6746 (cadr end-rs)
6747 c-old-END)
6748 c-new-END)))
6750 (goto-char c-new-BEG)
6751 (while (and (< (point) c-new-END)
6752 (re-search-forward
6753 (concat "\\(" ; 1
6754 c-anchored-cpp-prefix ; 2
6755 "\\)\\|\\(" ; 3
6756 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" ; 4
6757 "\\)")
6758 c-new-END t))
6759 (when (save-excursion
6760 (goto-char (match-beginning 0)) (not (c-in-literal)))
6761 (if (match-beginning 4) ; the id
6762 ;; We've found a raw string.
6763 (c-propertize-raw-string-opener
6764 (match-string-no-properties 4) ; id
6765 (1+ (match-beginning 3)) ; open quote
6766 (match-end 4) ; open paren
6767 nil) ; bound
6768 ;; We've found a CPP construct. Search for raw strings within it.
6769 (goto-char (match-beginning 2)) ; the "#"
6770 (c-end-of-macro)
6771 (let ((eom (point)))
6772 (goto-char (match-end 2)) ; after the "#".
6773 (while (and (< (point) eom)
6774 (c-syntactic-re-search-forward
6775 "R\"\\([^ ()\\\n\r\t]\\{0,16\\}\\)(" eom t))
6776 (c-propertize-raw-string-opener
6777 (match-string-no-properties 1) ; id
6778 (1+ (match-beginning 0)) ; open quote
6779 (match-end 1) ; open paren
6780 eom)))))))) ; bound
6783 ;; Handling of small scale constructs like types and names.
6785 ;; Dynamically bound variable that instructs `c-forward-type' to also
6786 ;; treat possible types (i.e. those that it normally returns 'maybe or
6787 ;; 'found for) as actual types (and always return 'found for them).
6788 ;; This means that it records them in `c-record-type-identifiers' if
6789 ;; that is set, and that it adds them to `c-found-types'.
6790 (defvar c-promote-possible-types nil)
6792 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
6793 ;; mark up successfully parsed arglists with paren syntax properties on
6794 ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
6795 ;; `c-type' property of each argument separating comma.
6797 ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
6798 ;; all arglists for side effects (i.e. recording types), otherwise it
6799 ;; exploits any existing paren syntax properties to quickly jump to the
6800 ;; end of already parsed arglists.
6802 ;; Marking up the arglists is not the default since doing that correctly
6803 ;; depends on a proper value for `c-restricted-<>-arglists'.
6804 (defvar c-parse-and-markup-<>-arglists nil)
6806 ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
6807 ;; not accept arglists that contain binary operators.
6809 ;; This is primarily used to handle C++ template arglists. C++
6810 ;; disambiguates them by checking whether the preceding name is a
6811 ;; template or not. We can't do that, so we assume it is a template
6812 ;; if it can be parsed as one. That usually works well since
6813 ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
6814 ;; in almost all cases would be pointless.
6816 ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
6817 ;; should let the comma separate the function arguments instead. And
6818 ;; in a context where the value of the expression is taken, e.g. in
6819 ;; "if (a < b || c > d)", it's probably not a template.
6820 (defvar c-restricted-<>-arglists nil)
6822 ;; Dynamically bound variables that instructs
6823 ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
6824 ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
6825 ;; `c-forward-label' to record the ranges of all the type and
6826 ;; reference identifiers they encounter. They will build lists on
6827 ;; these variables where each element is a cons of the buffer
6828 ;; positions surrounding each identifier. This recording is only
6829 ;; activated when `c-record-type-identifiers' is non-nil.
6831 ;; All known types that can't be identifiers are recorded, and also
6832 ;; other possible types if `c-promote-possible-types' is set.
6833 ;; Recording is however disabled inside angle bracket arglists that
6834 ;; are encountered inside names and other angle bracket arglists.
6835 ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
6836 ;; instead.
6838 ;; Only the names in C++ template style references (e.g. "tmpl" in
6839 ;; "tmpl<a,b>::foo") are recorded as references, other references
6840 ;; aren't handled here.
6842 ;; `c-forward-label' records the label identifier(s) on
6843 ;; `c-record-ref-identifiers'.
6844 (defvar c-record-type-identifiers nil)
6845 (defvar c-record-ref-identifiers nil)
6847 ;; This variable will receive a cons cell of the range of the last
6848 ;; single identifier symbol stepped over by `c-forward-name' if it's
6849 ;; successful. This is the range that should be put on one of the
6850 ;; record lists above by the caller. It's assigned nil if there's no
6851 ;; such symbol in the name.
6852 (defvar c-last-identifier-range nil)
6854 (defmacro c-record-type-id (range)
6855 (if (eq (car-safe range) 'cons)
6856 ;; Always true.
6857 `(setq c-record-type-identifiers
6858 (cons ,range c-record-type-identifiers))
6859 `(let ((range ,range))
6860 (if range
6861 (setq c-record-type-identifiers
6862 (cons range c-record-type-identifiers))))))
6864 (defmacro c-record-ref-id (range)
6865 (if (eq (car-safe range) 'cons)
6866 ;; Always true.
6867 `(setq c-record-ref-identifiers
6868 (cons ,range c-record-ref-identifiers))
6869 `(let ((range ,range))
6870 (if range
6871 (setq c-record-ref-identifiers
6872 (cons range c-record-ref-identifiers))))))
6874 ;; Dynamically bound variable that instructs `c-forward-type' to
6875 ;; record the ranges of types that only are found. Behaves otherwise
6876 ;; like `c-record-type-identifiers'.
6877 (defvar c-record-found-types nil)
6879 (defmacro c-forward-keyword-prefixed-id (type)
6880 ;; Used internally in `c-forward-keyword-clause' to move forward
6881 ;; over a type (if TYPE is 'type) or a name (otherwise) which
6882 ;; possibly is prefixed by keywords and their associated clauses.
6883 ;; Try with a type/name first to not trip up on those that begin
6884 ;; with a keyword. Return t if a known or found type is moved
6885 ;; over. The point is clobbered if nil is returned. If range
6886 ;; recording is enabled, the identifier is recorded on as a type
6887 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
6889 ;; This macro might do hidden buffer changes.
6890 `(let (res)
6891 (setq c-last-identifier-range nil)
6892 (while (if (setq res ,(if (eq type 'type)
6893 `(c-forward-type)
6894 `(c-forward-name)))
6896 (cond ((looking-at c-keywords-regexp)
6897 (c-forward-keyword-clause 1))
6898 ((and c-opt-cpp-prefix
6899 (looking-at c-noise-macro-with-parens-name-re))
6900 (c-forward-noise-clause)))))
6901 (when (memq res '(t known found prefix maybe))
6902 (when c-record-type-identifiers
6903 ,(if (eq type 'type)
6904 `(c-record-type-id c-last-identifier-range)
6905 `(c-record-ref-id c-last-identifier-range)))
6906 t)))
6908 (defmacro c-forward-id-comma-list (type update-safe-pos)
6909 ;; Used internally in `c-forward-keyword-clause' to move forward
6910 ;; over a comma separated list of types or names using
6911 ;; `c-forward-keyword-prefixed-id'.
6913 ;; This macro might do hidden buffer changes.
6914 `(while (and (progn
6915 ,(when update-safe-pos
6916 `(setq safe-pos (point)))
6917 (eq (char-after) ?,))
6918 (progn
6919 (forward-char)
6920 (c-forward-syntactic-ws)
6921 (c-forward-keyword-prefixed-id ,type)))))
6923 (defun c-forward-noise-clause ()
6924 ;; Point is at a c-noise-macro-with-parens-names macro identifier. Go
6925 ;; forward over this name, any parenthesis expression which follows it, and
6926 ;; any syntactic WS, ending up at the next token. If there is an unbalanced
6927 ;; paren expression, leave point at it. Always Return t.
6928 (c-forward-token-2)
6929 (if (and (eq (char-after) ?\()
6930 (c-go-list-forward))
6931 (c-forward-syntactic-ws))
6934 (defun c-forward-keyword-clause (match)
6935 ;; Submatch MATCH in the current match data is assumed to surround a
6936 ;; token. If it's a keyword, move over it and any immediately
6937 ;; following clauses associated with it, stopping at the start of
6938 ;; the next token. t is returned in that case, otherwise the point
6939 ;; stays and nil is returned. The kind of clauses that are
6940 ;; recognized are those specified by `c-type-list-kwds',
6941 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
6942 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
6943 ;; `c-<>-arglist-kwds', and `c-protection-kwds'.
6945 ;; This function records identifier ranges on
6946 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6947 ;; `c-record-type-identifiers' is non-nil.
6949 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
6950 ;; apply directly after the keyword, the type list is moved over
6951 ;; only when there is no unaccounted token before it (i.e. a token
6952 ;; that isn't moved over due to some other keyword list). The
6953 ;; identifier ranges in the list are still recorded if that should
6954 ;; be done, though.
6956 ;; This function might do hidden buffer changes.
6958 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
6959 ;; The call to `c-forward-<>-arglist' below is made after
6960 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
6961 ;; are angle bracket arglists and `c-restricted-<>-arglists'
6962 ;; should therefore be nil.
6963 (c-parse-and-markup-<>-arglists t)
6964 c-restricted-<>-arglists)
6966 (when kwd-sym
6967 (goto-char (match-end match))
6968 (c-forward-syntactic-ws)
6969 (setq safe-pos (point))
6971 (cond
6972 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
6973 (c-forward-keyword-prefixed-id type))
6974 ;; There's a type directly after a keyword in `c-type-list-kwds'.
6975 (c-forward-id-comma-list type t))
6977 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
6978 (c-forward-keyword-prefixed-id ref))
6979 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
6980 (c-forward-id-comma-list ref t))
6982 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
6983 (eq (char-after) ?\())
6984 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
6986 (forward-char)
6987 (when (and (setq pos (c-up-list-forward))
6988 (eq (char-before pos) ?\)))
6989 (when (and c-record-type-identifiers
6990 (c-keyword-member kwd-sym 'c-paren-type-kwds))
6991 ;; Use `c-forward-type' on every identifier we can find
6992 ;; inside the paren, to record the types.
6993 (while (c-syntactic-re-search-forward c-symbol-start pos t)
6994 (goto-char (match-beginning 0))
6995 (unless (c-forward-type)
6996 (looking-at c-symbol-key) ; Always matches.
6997 (goto-char (match-end 0)))))
6999 (goto-char pos)
7000 (c-forward-syntactic-ws)
7001 (setq safe-pos (point))))
7003 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
7004 (eq (char-after) ?<)
7005 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
7006 (c-forward-syntactic-ws)
7007 (setq safe-pos (point)))
7009 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
7010 (not (looking-at c-symbol-start))
7011 (c-safe (c-forward-sexp) t))
7012 (c-forward-syntactic-ws)
7013 (setq safe-pos (point)))
7015 ((and (c-keyword-member kwd-sym 'c-protection-kwds)
7016 (or (null c-post-protection-token)
7017 (and (looking-at c-post-protection-token)
7018 (save-excursion
7019 (goto-char (match-end 0))
7020 (not (c-end-of-current-token))))))
7021 (if c-post-protection-token
7022 (goto-char (match-end 0)))
7023 (c-forward-syntactic-ws)
7024 (setq safe-pos (point))))
7026 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
7027 (if (eq (char-after) ?:)
7028 ;; If we are at the colon already, we move over the type
7029 ;; list after it.
7030 (progn
7031 (forward-char)
7032 (c-forward-syntactic-ws)
7033 (when (c-forward-keyword-prefixed-id type)
7034 (c-forward-id-comma-list type t)))
7035 ;; Not at the colon, so stop here. But the identifier
7036 ;; ranges in the type list later on should still be
7037 ;; recorded.
7038 (and c-record-type-identifiers
7039 (progn
7040 ;; If a keyword matched both one of the types above and
7041 ;; this one, we match `c-colon-type-list-re' after the
7042 ;; clause matched above.
7043 (goto-char safe-pos)
7044 (looking-at c-colon-type-list-re))
7045 (progn
7046 (goto-char (match-end 0))
7047 (c-forward-syntactic-ws)
7048 (c-forward-keyword-prefixed-id type))
7049 ;; There's a type after the `c-colon-type-list-re' match
7050 ;; after a keyword in `c-colon-type-list-kwds'.
7051 (c-forward-id-comma-list type nil))))
7053 (goto-char safe-pos)
7054 t)))
7056 ;; cc-mode requires cc-fonts.
7057 (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
7059 (defun c-forward-<>-arglist (all-types)
7060 ;; The point is assumed to be at a "<". Try to treat it as the open
7061 ;; paren of an angle bracket arglist and move forward to the
7062 ;; corresponding ">". If successful, the point is left after the
7063 ;; ">" and t is returned, otherwise the point isn't moved and nil is
7064 ;; returned. If ALL-TYPES is t then all encountered arguments in
7065 ;; the arglist that might be types are treated as found types.
7067 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
7068 ;; function handles text properties on the angle brackets and argument
7069 ;; separating commas.
7071 ;; `c-restricted-<>-arglists' controls how lenient the template
7072 ;; arglist recognition should be.
7074 ;; This function records identifier ranges on
7075 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7076 ;; `c-record-type-identifiers' is non-nil.
7078 ;; This function might do hidden buffer changes.
7080 (let ((start (point))
7081 (old-found-types (copy-hash-table c-found-types))
7082 ;; If `c-record-type-identifiers' is set then activate
7083 ;; recording of any found types that constitute an argument in
7084 ;; the arglist.
7085 (c-record-found-types (if c-record-type-identifiers t)))
7086 (if (catch 'angle-bracket-arglist-escape
7087 (setq c-record-found-types
7088 (c-forward-<>-arglist-recur all-types)))
7089 (progn
7090 (when (consp c-record-found-types)
7091 (setq c-record-type-identifiers
7092 ;; `nconc' doesn't mind that the tail of
7093 ;; `c-record-found-types' is t.
7094 (nconc c-record-found-types c-record-type-identifiers)))
7097 (setq c-found-types old-found-types)
7098 (goto-char start)
7099 nil)))
7101 (defun c-forward-<>-arglist-recur (all-types)
7102 ;; Recursive part of `c-forward-<>-arglist'.
7104 ;; This function might do hidden buffer changes.
7105 (let ((start (point)) res pos
7106 ;; Cover this so that any recorded found type ranges are
7107 ;; automatically lost if it turns out to not be an angle
7108 ;; bracket arglist. It's propagated through the return value
7109 ;; on successful completion.
7110 (c-record-found-types c-record-found-types)
7111 ;; List that collects the positions after the argument
7112 ;; separating ',' in the arglist.
7113 arg-start-pos)
7114 ;; If the '<' has paren open syntax then we've marked it as an angle
7115 ;; bracket arglist before, so skip to the end.
7116 (if (and (not c-parse-and-markup-<>-arglists)
7117 (c-get-char-property (point) 'syntax-table))
7119 (progn
7120 (forward-char)
7121 (if (and (c-go-up-list-forward)
7122 (eq (char-before) ?>))
7124 ;; Got unmatched paren angle brackets. We don't clear the paren
7125 ;; syntax properties and retry, on the basis that it's very
7126 ;; unlikely that paren angle brackets become operators by code
7127 ;; manipulation. It's far more likely that it doesn't match due
7128 ;; to narrowing or some temporary change.
7129 (goto-char start)
7130 nil))
7132 (forward-char) ; Forward over the opening '<'.
7134 (unless (looking-at c-<-op-cont-regexp)
7135 ;; go forward one non-alphanumeric character (group) per iteration of
7136 ;; this loop.
7137 (while (and
7138 (progn
7139 (c-forward-syntactic-ws)
7140 (when (or (and c-record-type-identifiers all-types)
7141 (not (equal c-inside-<>-type-key "\\(\\<\\>\\)")))
7142 (c-forward-syntactic-ws)
7143 (cond
7144 ((eq (char-after) ??)
7145 (forward-char))
7146 ((and (looking-at c-identifier-start)
7147 (not (looking-at c-keywords-regexp)))
7148 (if (or (and all-types c-record-type-identifiers)
7149 (c-major-mode-is 'java-mode))
7150 ;; All encountered identifiers are types, so set the
7151 ;; promote flag and parse the type.
7152 (let ((c-promote-possible-types t)
7153 (c-record-found-types t))
7154 (c-forward-type))
7155 (c-forward-over-token-and-ws))))
7157 (c-forward-syntactic-ws)
7159 (when (looking-at c-inside-<>-type-key)
7160 (goto-char (match-end 1))
7161 (c-forward-syntactic-ws)
7162 (let ((c-promote-possible-types t)
7163 (c-record-found-types t))
7164 (c-forward-type))
7165 (c-forward-syntactic-ws)))
7167 (setq pos (point)) ; e.g. first token inside the '<'
7169 ;; Note: These regexps exploit the match order in \| so
7170 ;; that "<>" is matched by "<" rather than "[^>:-]>".
7171 (c-syntactic-re-search-forward
7172 ;; Stop on ',', '|', '&', '+' and '-' to catch
7173 ;; common binary operators that could be between
7174 ;; two comparison expressions "a<b" and "c>d".
7175 ;; 2016-02-11: C++11 templates can now contain arithmetic
7176 ;; expressions, so template detection in C++ is now less
7177 ;; robust than it was.
7178 c-<>-notable-chars-re
7179 nil t t))
7181 (cond
7182 ((eq (char-before) ?>)
7183 ;; Either an operator starting with '>' or the end of
7184 ;; the angle bracket arglist.
7186 (if (save-excursion
7187 (c-backward-token-2)
7188 (looking-at c-multichar->-op-not->>-regexp))
7189 (progn
7190 (goto-char (match-end 0))
7191 t) ; Continue the loop.
7193 ;; The angle bracket arglist is finished.
7194 (when c-parse-and-markup-<>-arglists
7195 (while arg-start-pos
7196 (c-put-c-type-property (1- (car arg-start-pos))
7197 'c-<>-arg-sep)
7198 (setq arg-start-pos (cdr arg-start-pos)))
7199 (c-mark-<-as-paren start)
7200 (c-mark->-as-paren (1- (point))))
7201 (setq res t)
7202 nil)) ; Exit the loop.
7204 ((eq (char-before) ?<)
7205 ;; Either an operator starting with '<' or a nested arglist.
7206 (setq pos (point))
7207 (let (id-start id-end subres keyword-match)
7208 (cond
7209 ;; The '<' begins a multi-char operator.
7210 ((looking-at c-<-op-cont-regexp)
7211 (goto-char (match-end 0)))
7212 ;; We're at a nested <.....>
7213 ((progn
7214 (backward-char) ; to the '<'
7215 (and
7216 (save-excursion
7217 ;; There's always an identifier before an angle
7218 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
7219 ;; or `c-<>-arglist-kwds'.
7220 (c-backward-syntactic-ws)
7221 (setq id-end (point))
7222 (c-simple-skip-symbol-backward)
7223 (when (or (setq keyword-match
7224 (looking-at c-opt-<>-sexp-key))
7225 (not (looking-at c-keywords-regexp)))
7226 (setq id-start (point))))
7227 (setq subres
7228 (let ((c-promote-possible-types t)
7229 (c-record-found-types t))
7230 (c-forward-<>-arglist-recur
7231 (and keyword-match
7232 (c-keyword-member
7233 (c-keyword-sym (match-string 1))
7234 'c-<>-type-kwds))))))
7235 (or subres (goto-char pos))
7236 subres)
7237 ;; It was an angle bracket arglist.
7238 (setq c-record-found-types subres)
7240 ;; Record the identifier before the template as a type
7241 ;; or reference depending on whether the arglist is last
7242 ;; in a qualified identifier.
7243 (when (and c-record-type-identifiers
7244 (not keyword-match))
7245 (if (and c-opt-identifier-concat-key
7246 (progn
7247 (c-forward-syntactic-ws)
7248 (looking-at c-opt-identifier-concat-key)))
7249 (c-record-ref-id (cons id-start id-end))
7250 (c-record-type-id (cons id-start id-end)))))
7252 ;; At a "less than" operator.
7254 ;; (forward-char) ; NO! We've already gone over the <.
7256 t) ; carry on looping.
7258 ((and
7259 (eq (char-before) ?\()
7260 (c-go-up-list-forward)
7261 (eq (char-before) ?\))))
7263 ((and (not c-restricted-<>-arglists)
7264 (or (and (eq (char-before) ?&)
7265 (not (eq (char-after) ?&)))
7266 (eq (char-before) ?,)))
7267 ;; Just another argument. Record the position. The
7268 ;; type check stuff that made us stop at it is at
7269 ;; the top of the loop.
7270 (setq arg-start-pos (cons (point) arg-start-pos)))
7273 ;; Got a character that can't be in an angle bracket
7274 ;; arglist argument. Abort using `throw', since
7275 ;; it's useless to try to find a surrounding arglist
7276 ;; if we're nested.
7277 (throw 'angle-bracket-arglist-escape nil))))))
7278 (if res
7279 (or c-record-found-types t)))))
7281 (defun c-backward-<>-arglist (all-types &optional limit)
7282 ;; The point is assumed to be directly after a ">". Try to treat it
7283 ;; as the close paren of an angle bracket arglist and move back to
7284 ;; the corresponding "<". If successful, the point is left at
7285 ;; the "<" and t is returned, otherwise the point isn't moved and
7286 ;; nil is returned. ALL-TYPES is passed on to
7287 ;; `c-forward-<>-arglist'.
7289 ;; If the optional LIMIT is given, it bounds the backward search.
7290 ;; It's then assumed to be at a syntactically relevant position.
7292 ;; This is a wrapper around `c-forward-<>-arglist'. See that
7293 ;; function for more details.
7295 (let ((start (point)))
7296 (backward-char)
7297 (if (and (not c-parse-and-markup-<>-arglists)
7298 (c-get-char-property (point) 'syntax-table))
7300 (if (and (c-go-up-list-backward)
7301 (eq (char-after) ?<))
7303 ;; See corresponding note in `c-forward-<>-arglist'.
7304 (goto-char start)
7305 nil)
7307 (while (progn
7308 (c-syntactic-skip-backward "^<;{}" limit t)
7310 (and
7311 (if (eq (char-before) ?<)
7313 ;; Stopped at bob or a char that isn't allowed in an
7314 ;; arglist, so we've failed.
7315 (goto-char start)
7316 nil)
7318 (if (> (point)
7319 (progn (c-beginning-of-current-token)
7320 (point)))
7321 ;; If we moved then the "<" was part of some
7322 ;; multicharacter token.
7325 (backward-char)
7326 (let ((beg-pos (point)))
7327 (if (c-forward-<>-arglist all-types)
7328 (cond ((= (point) start)
7329 ;; Matched the arglist. Break the while.
7330 (goto-char beg-pos)
7331 nil)
7332 ((> (point) start)
7333 ;; We started from a non-paren ">" inside an
7334 ;; arglist.
7335 (goto-char start)
7336 nil)
7338 ;; Matched a shorter arglist. Can be a nested
7339 ;; one so continue looking.
7340 (goto-char beg-pos)
7342 t))))))
7344 (/= (point) start))))
7346 (defun c-forward-name ()
7347 ;; Move forward over a complete name if at the beginning of one,
7348 ;; stopping at the next following token. A keyword, as such,
7349 ;; doesn't count as a name. If the point is not at something that
7350 ;; is recognized as a name then it stays put.
7352 ;; A name could be something as simple as "foo" in C or something as
7353 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
7354 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
7355 ;; int>::*volatile const" in C++ (this function is actually little
7356 ;; more than a `looking-at' call in all modes except those that,
7357 ;; like C++, have `c-recognize-<>-arglists' set).
7359 ;; Return
7360 ;; o - nil if no name is found;
7361 ;; o - 'template if it's an identifier ending with an angle bracket
7362 ;; arglist;
7363 ;; o - 'operator of it's an operator identifier;
7364 ;; o - t if it's some other kind of name.
7366 ;; This function records identifier ranges on
7367 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7368 ;; `c-record-type-identifiers' is non-nil.
7370 ;; This function might do hidden buffer changes.
7372 (let ((pos (point)) (start (point)) res id-start id-end
7373 ;; Turn off `c-promote-possible-types' here since we might
7374 ;; call `c-forward-<>-arglist' and we don't want it to promote
7375 ;; every suspect thing in the arglist to a type. We're
7376 ;; typically called from `c-forward-type' in this case, and
7377 ;; the caller only wants the top level type that it finds to
7378 ;; be promoted.
7379 c-promote-possible-types)
7380 (while
7381 (and
7382 (looking-at c-identifier-key)
7384 (progn
7385 ;; Check for keyword. We go to the last symbol in
7386 ;; `c-identifier-key' first.
7387 (goto-char (setq id-end (match-end 0)))
7388 (c-simple-skip-symbol-backward)
7389 (setq id-start (point))
7391 (if (looking-at c-keywords-regexp)
7392 (when (and (c-major-mode-is 'c++-mode)
7393 (looking-at
7394 (cc-eval-when-compile
7395 (concat "\\(operator\\|\\(template\\)\\)"
7396 "\\(" (c-lang-const c-nonsymbol-key c++)
7397 "\\|$\\)")))
7398 (if (match-beginning 2)
7399 ;; "template" is only valid inside an
7400 ;; identifier if preceded by "::".
7401 (save-excursion
7402 (c-backward-syntactic-ws)
7403 (and (c-safe (backward-char 2) t)
7404 (looking-at "::")))
7407 ;; Handle a C++ operator or template identifier.
7408 (goto-char id-end)
7409 (c-forward-syntactic-ws)
7410 (cond ((eq (char-before id-end) ?e)
7411 ;; Got "... ::template".
7412 (let ((subres (c-forward-name)))
7413 (when subres
7414 (setq pos (point)
7415 res subres))))
7417 ((and (looking-at c-identifier-start)
7418 (or (not (looking-at
7419 c-ambiguous-overloadable-or-identifier-prefix-re))
7420 (save-excursion
7421 (and (eq (c-forward-token-2) 0)
7422 (not (eq (char-after) ?\())))))
7423 ;; Got a cast operator.
7424 (when (c-forward-type)
7425 (setq pos (point)
7426 res 'operator)
7427 ;; Now we should match a sequence of either
7428 ;; '*', '&' or a name followed by ":: *",
7429 ;; where each can be followed by a sequence
7430 ;; of `c-opt-type-modifier-key'.
7431 (while (cond ((looking-at "[*&]")
7432 (goto-char (match-end 0))
7434 ((looking-at c-identifier-start)
7435 (and (c-forward-name)
7436 (looking-at "::")
7437 (progn
7438 (goto-char (match-end 0))
7439 (c-forward-syntactic-ws)
7440 (eq (char-after) ?*))
7441 (progn
7442 (forward-char)
7443 t))))
7444 (while (progn
7445 (c-forward-syntactic-ws)
7446 (setq pos (point))
7447 (looking-at c-opt-type-modifier-key))
7448 (goto-char (match-end 1))))))
7450 ((looking-at c-overloadable-operators-regexp)
7451 ;; Got some other operator.
7452 (setq c-last-identifier-range
7453 (cons (point) (match-end 0)))
7454 (goto-char (match-end 0))
7455 (c-forward-syntactic-ws)
7456 (setq pos (point)
7457 res 'operator)))
7459 nil)
7461 ;; `id-start' is equal to `id-end' if we've jumped over
7462 ;; an identifier that doesn't end with a symbol token.
7463 ;; That can occur e.g. for Java import directives on the
7464 ;; form "foo.bar.*".
7465 (when (and id-start (/= id-start id-end))
7466 (setq c-last-identifier-range
7467 (cons id-start id-end)))
7468 (goto-char id-end)
7469 (c-forward-syntactic-ws)
7470 (setq pos (point)
7471 res t)))
7473 (progn
7474 (goto-char pos)
7475 (when (or c-opt-identifier-concat-key
7476 c-recognize-<>-arglists)
7478 (cond
7479 ((and c-opt-identifier-concat-key
7480 (looking-at c-opt-identifier-concat-key))
7481 ;; Got a concatenated identifier. This handles the
7482 ;; cases with tricky syntactic whitespace that aren't
7483 ;; covered in `c-identifier-key'.
7484 (goto-char (match-end 0))
7485 (c-forward-syntactic-ws)
7488 ((and c-recognize-<>-arglists
7489 (eq (char-after) ?<))
7490 ;; Maybe an angle bracket arglist.
7491 (when (let (c-last-identifier-range)
7492 (c-forward-<>-arglist nil))
7494 (c-forward-syntactic-ws)
7495 (unless (eq (char-after) ?\()
7496 (setq c-last-identifier-range nil)
7497 (c-add-type start (1+ pos)))
7498 (setq pos (point))
7500 (if (and c-opt-identifier-concat-key
7501 (looking-at c-opt-identifier-concat-key))
7503 ;; Continue if there's an identifier concatenation
7504 ;; operator after the template argument.
7505 (progn
7506 (when (and c-record-type-identifiers id-start)
7507 (c-record-ref-id (cons id-start id-end)))
7508 (forward-char 2)
7509 (c-forward-syntactic-ws)
7512 (when (and c-record-type-identifiers id-start
7513 (not (eq (char-after) ?\()))
7514 (c-record-type-id (cons id-start id-end)))
7515 (setq res 'template)
7516 nil)))
7517 )))))
7519 (goto-char pos)
7520 res))
7522 (defun c-forward-type (&optional brace-block-too)
7523 ;; Move forward over a type spec if at the beginning of one,
7524 ;; stopping at the next following token. The keyword "typedef"
7525 ;; isn't part of a type spec here.
7527 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
7528 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
7529 ;; The current (2009-03-10) intention is to convert all uses of
7530 ;; `c-forward-type' to call with this parameter set, then to
7531 ;; eliminate it.
7533 ;; Return
7534 ;; o - t if it's a known type that can't be a name or other
7535 ;; expression;
7536 ;; o - 'known if it's an otherwise known type (according to
7537 ;; `*-font-lock-extra-types');
7538 ;; o - 'prefix if it's a known prefix of a type;
7539 ;; o - 'found if it's a type that matches one in `c-found-types';
7540 ;; o - 'maybe if it's an identifier that might be a type;
7541 ;; o - 'decltype if it's a decltype(variable) declaration; - or
7542 ;; o - nil if it can't be a type (the point isn't moved then).
7544 ;; The point is assumed to be at the beginning of a token.
7546 ;; Note that this function doesn't skip past the brace definition
7547 ;; that might be considered part of the type, e.g.
7548 ;; "enum {a, b, c} foo".
7550 ;; This function records identifier ranges on
7551 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7552 ;; `c-record-type-identifiers' is non-nil.
7554 ;; This function might do hidden buffer changes.
7555 (when (and c-recognize-<>-arglists
7556 (looking-at "<"))
7557 (c-forward-<>-arglist t)
7558 (c-forward-syntactic-ws))
7560 (let ((start (point)) pos res name-res id-start id-end id-range)
7562 ;; Skip leading type modifiers. If any are found we know it's a
7563 ;; prefix of a type.
7564 (when c-opt-type-modifier-prefix-key ; e.g. "const" "volatile", but NOT "typedef"
7565 (while (looking-at c-opt-type-modifier-prefix-key)
7566 (goto-char (match-end 1))
7567 (c-forward-syntactic-ws)
7568 (setq res 'prefix)))
7570 (cond
7571 ((looking-at c-typeof-key) ; e.g. C++'s "decltype".
7572 (goto-char (match-end 1))
7573 (c-forward-syntactic-ws)
7574 (setq res (and (eq (char-after) ?\()
7575 (c-safe (c-forward-sexp))
7576 'decltype))
7577 (if res
7578 (c-forward-syntactic-ws)
7579 (goto-char start)))
7581 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
7582 ; "typedef".
7583 (goto-char (match-end 1))
7584 (c-forward-syntactic-ws)
7586 (while (cond
7587 ((looking-at c-decl-hangon-key)
7588 (c-forward-keyword-clause 1))
7589 ((looking-at c-pack-key)
7590 (goto-char (match-end 1))
7591 (c-forward-syntactic-ws))
7592 ((and c-opt-cpp-prefix
7593 (looking-at c-noise-macro-with-parens-name-re))
7594 (c-forward-noise-clause))))
7596 (setq pos (point))
7598 (setq name-res (c-forward-name))
7599 (setq res (not (null name-res)))
7600 (when (eq name-res t)
7601 ;; In many languages the name can be used without the
7602 ;; prefix, so we add it to `c-found-types'.
7603 (c-add-type pos (point))
7604 (when (and c-record-type-identifiers
7605 c-last-identifier-range)
7606 (c-record-type-id c-last-identifier-range)))
7607 (when (and brace-block-too
7608 (memq res '(t nil))
7609 (eq (char-after) ?\{)
7610 (save-excursion
7611 (c-safe
7612 (progn (c-forward-sexp)
7613 (c-forward-syntactic-ws)
7614 (setq pos (point))))))
7615 (goto-char pos)
7616 (setq res t))
7617 (unless res (goto-char start))) ; invalid syntax
7619 ((progn
7620 (setq pos nil)
7621 (if (looking-at c-identifier-start)
7622 (save-excursion
7623 (setq id-start (point)
7624 name-res (c-forward-name))
7625 (when name-res
7626 (setq id-end (point)
7627 id-range c-last-identifier-range))))
7628 (and (cond ((looking-at c-primitive-type-key)
7629 (setq res t))
7630 ((c-with-syntax-table c-identifier-syntax-table
7631 (looking-at c-known-type-key))
7632 (setq res 'known)))
7633 (or (not id-end)
7634 (>= (save-excursion
7635 (save-match-data
7636 (goto-char (match-end 1))
7637 (c-forward-syntactic-ws)
7638 (setq pos (point))))
7639 id-end)
7640 (setq res nil))))
7641 ;; Looking at a primitive or known type identifier. We've
7642 ;; checked for a name first so that we don't go here if the
7643 ;; known type match only is a prefix of another name.
7645 (setq id-end (match-end 1))
7647 (when (and c-record-type-identifiers
7648 (or c-promote-possible-types (eq res t)))
7649 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
7651 (if (and c-opt-type-component-key
7652 (save-match-data
7653 (looking-at c-opt-type-component-key)))
7654 ;; There might be more keywords for the type.
7655 (let (safe-pos)
7656 (c-forward-keyword-clause 1)
7657 (while (progn
7658 (setq safe-pos (point))
7659 (looking-at c-opt-type-component-key))
7660 (when (and c-record-type-identifiers
7661 (looking-at c-primitive-type-key))
7662 (c-record-type-id (cons (match-beginning 1)
7663 (match-end 1))))
7664 (c-forward-keyword-clause 1))
7665 (if (looking-at c-primitive-type-key)
7666 (progn
7667 (when c-record-type-identifiers
7668 (c-record-type-id (cons (match-beginning 1)
7669 (match-end 1))))
7670 (c-forward-keyword-clause 1)
7671 (setq res t))
7672 (goto-char safe-pos)
7673 (setq res 'prefix)))
7674 (unless (save-match-data (c-forward-keyword-clause 1))
7675 (if pos
7676 (goto-char pos)
7677 (goto-char (match-end 1))
7678 (c-forward-syntactic-ws)))))
7680 (name-res
7681 (cond ((eq name-res t)
7682 ;; A normal identifier.
7683 (goto-char id-end)
7684 (if (or res c-promote-possible-types)
7685 (progn
7686 (c-add-type id-start id-end)
7687 (when (and c-record-type-identifiers id-range)
7688 (c-record-type-id id-range))
7689 (unless res
7690 (setq res 'found)))
7691 (setq res (if (c-check-type id-start id-end)
7692 ;; It's an identifier that has been used as
7693 ;; a type somewhere else.
7694 'found
7695 ;; It's an identifier that might be a type.
7696 'maybe))))
7697 ((eq name-res 'template)
7698 ;; A template is sometimes a type.
7699 (goto-char id-end)
7700 (c-forward-syntactic-ws)
7701 (setq res
7702 (if (eq (char-after) ?\()
7703 (if (c-check-type id-start id-end)
7704 ;; It's an identifier that has been used as
7705 ;; a type somewhere else.
7706 'found
7707 ;; It's an identifier that might be a type.
7708 'maybe)
7709 t)))
7711 ;; Otherwise it's an operator identifier, which is not a type.
7712 (goto-char start)
7713 (setq res nil)))))
7715 (when res
7716 ;; Skip trailing type modifiers. If any are found we know it's
7717 ;; a type.
7718 (when c-opt-type-modifier-key
7719 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
7720 (goto-char (match-end 1))
7721 (c-forward-syntactic-ws)
7722 (setq res t)))
7724 ;; Step over any type suffix operator. Do not let the existence
7725 ;; of these alter the classification of the found type, since
7726 ;; these operators typically are allowed in normal expressions
7727 ;; too.
7728 (when c-opt-type-suffix-key ; e.g. "..."
7729 (while (looking-at c-opt-type-suffix-key)
7730 (goto-char (match-end 1))
7731 (c-forward-syntactic-ws)))
7733 ;; Skip any "WS" identifiers (e.g. "final" or "override" in C++)
7734 (while (looking-at c-type-decl-suffix-ws-ids-key)
7735 (goto-char (match-end 1))
7736 (c-forward-syntactic-ws)
7737 (setq res t))
7739 (when c-opt-type-concat-key ; Only/mainly for pike.
7740 ;; Look for a trailing operator that concatenates the type
7741 ;; with a following one, and if so step past that one through
7742 ;; a recursive call. Note that we don't record concatenated
7743 ;; types in `c-found-types' - it's the component types that
7744 ;; are recorded when appropriate.
7745 (setq pos (point))
7746 (let* ((c-promote-possible-types (or (memq res '(t known))
7747 c-promote-possible-types))
7748 ;; If we can't promote then set `c-record-found-types' so that
7749 ;; we can merge in the types from the second part afterwards if
7750 ;; it turns out to be a known type there.
7751 (c-record-found-types (and c-record-type-identifiers
7752 (not c-promote-possible-types)))
7753 subres)
7754 (if (and (looking-at c-opt-type-concat-key)
7756 (progn
7757 (goto-char (match-end 1))
7758 (c-forward-syntactic-ws)
7759 (setq subres (c-forward-type))))
7761 (progn
7762 ;; If either operand certainly is a type then both are, but we
7763 ;; don't let the existence of the operator itself promote two
7764 ;; uncertain types to a certain one.
7765 (cond ((eq res t))
7766 ((eq subres t)
7767 (unless (eq name-res 'template)
7768 (c-add-type id-start id-end))
7769 (when (and c-record-type-identifiers id-range)
7770 (c-record-type-id id-range))
7771 (setq res t))
7772 ((eq res 'known))
7773 ((eq subres 'known)
7774 (setq res 'known))
7775 ((eq res 'found))
7776 ((eq subres 'found)
7777 (setq res 'found))
7779 (setq res 'maybe)))
7781 (when (and (eq res t)
7782 (consp c-record-found-types))
7783 ;; Merge in the ranges of any types found by the second
7784 ;; `c-forward-type'.
7785 (setq c-record-type-identifiers
7786 ;; `nconc' doesn't mind that the tail of
7787 ;; `c-record-found-types' is t.
7788 (nconc c-record-found-types
7789 c-record-type-identifiers))))
7791 (goto-char pos))))
7793 (when (and c-record-found-types (memq res '(known found)) id-range)
7794 (setq c-record-found-types
7795 (cons id-range c-record-found-types))))
7797 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
7799 res))
7801 (defun c-forward-annotation ()
7802 ;; Used for Java code only at the moment. Assumes point is on the @, moves
7803 ;; forward an annotation and returns t. Leaves point unmoved and returns
7804 ;; nil if there is no annotation at point.
7805 (let ((pos (point)))
7807 (and (looking-at "@")
7808 (not (looking-at c-keywords-regexp))
7809 (progn (forward-char) t)
7810 (looking-at c-symbol-key)
7811 (progn (goto-char (match-end 0))
7812 (c-forward-syntactic-ws)
7814 (if (looking-at "(")
7815 (c-go-list-forward)
7817 (progn (goto-char pos) nil))))
7819 (defmacro c-pull-open-brace (ps)
7820 ;; Pull the next open brace from PS (which has the form of paren-state),
7821 ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
7822 `(progn
7823 (while (consp (car ,ps))
7824 (setq ,ps (cdr ,ps)))
7825 (prog1 (car ,ps)
7826 (setq ,ps (cdr ,ps)))))
7828 (defun c-back-over-compound-identifier ()
7829 ;; Point is putatively just after a "compound identifier", i.e. something
7830 ;; looking (in C++) like this "FQN::of::base::Class". Move to the start of
7831 ;; this construct and return t. If the parsing fails, return nil, leaving
7832 ;; point unchanged.
7833 (let (end)
7834 (if (not (c-on-identifier))
7836 (c-simple-skip-symbol-backward)
7837 (while
7838 (progn
7839 (setq end (point))
7840 (c-backward-syntactic-ws)
7841 (c-backward-token-2)
7842 (and
7843 c-opt-identifier-concat-key
7844 (looking-at c-opt-identifier-concat-key)
7845 (progn
7846 (c-backward-syntactic-ws)
7847 (c-simple-skip-symbol-backward))))
7848 (setq end (point)))
7849 (goto-char end)
7850 t)))
7852 (defun c-back-over-member-initializer-braces ()
7853 ;; Point is just after a closing brace/parenthesis. Try to parse this as a
7854 ;; C++ member initializer list, going back to just after the introducing ":"
7855 ;; and returning t. Otherwise return nil, leaving point unchanged.
7856 (let ((here (point)) res)
7857 (setq res
7858 (catch 'done
7859 (when (not (c-go-list-backward))
7860 (throw 'done nil))
7861 (c-backward-syntactic-ws)
7862 (when (not (c-back-over-compound-identifier))
7863 (throw 'done nil))
7864 (c-backward-syntactic-ws)
7866 (while (eq (char-before) ?,)
7867 (backward-char)
7868 (c-backward-syntactic-ws)
7869 (when (not (memq (char-before) '(?\) ?})))
7870 (throw 'done nil))
7871 (when (not (c-go-list-backward))
7872 (throw 'done nil))
7873 (c-backward-syntactic-ws)
7874 (when (not (c-back-over-compound-identifier))
7875 (throw 'done nil))
7876 (c-backward-syntactic-ws))
7878 (eq (char-before) ?:)))
7879 (or res (goto-char here))
7880 res))
7882 (defmacro c-back-over-list-of-member-inits ()
7883 ;; Go back over a list of elements, each looking like:
7884 ;; <symbol> (<expression>) ,
7885 ;; or <symbol> {<expression>} , (with possibly a <....> expressions
7886 ;; following the <symbol>).
7887 ;; when we are putatively immediately after a comma. Stop when we don't see
7888 ;; a comma. If either of <symbol> or bracketed <expression> is missing,
7889 ;; throw nil to 'level. If the terminating } or ) is unmatched, throw nil
7890 ;; to 'done. This is not a general purpose macro!
7891 `(while (eq (char-before) ?,)
7892 (backward-char)
7893 (c-backward-syntactic-ws)
7894 (when (not (memq (char-before) '(?\) ?})))
7895 (throw 'level nil))
7896 (when (not (c-go-list-backward))
7897 (throw 'done nil))
7898 (c-backward-syntactic-ws)
7899 (while (eq (char-before) ?>)
7900 (when (not (c-backward-<>-arglist nil))
7901 (throw 'done nil))
7902 (c-backward-syntactic-ws))
7903 (when (not (c-back-over-compound-identifier))
7904 (throw 'level nil))
7905 (c-backward-syntactic-ws)))
7907 (defun c-back-over-member-initializers ()
7908 ;; Test whether we are in a C++ member initializer list, and if so, go back
7909 ;; to the introducing ":", returning the position of the opening paren of
7910 ;; the function's arglist. Otherwise return nil, leaving point unchanged.
7911 (let ((here (point))
7912 (paren-state (c-parse-state))
7913 pos level-plausible at-top-level res)
7914 ;; Assume tentatively that we're at the top level. Try to go back to the
7915 ;; colon we seek.
7916 (setq res
7917 (catch 'done
7918 (setq level-plausible
7919 (catch 'level
7920 (c-backward-syntactic-ws)
7921 (when (memq (char-before) '(?\) ?}))
7922 (when (not (c-go-list-backward))
7923 (throw 'done nil))
7924 (c-backward-syntactic-ws))
7925 (when (c-back-over-compound-identifier)
7926 (c-backward-syntactic-ws))
7927 (c-back-over-list-of-member-inits)
7928 (and (eq (char-before) ?:)
7929 (save-excursion
7930 (c-backward-token-2)
7931 (not (looking-at c-:$-multichar-token-regexp)))
7932 (c-just-after-func-arglist-p))))
7934 (while (and (not (and level-plausible
7935 (setq at-top-level (c-at-toplevel-p))))
7936 (setq pos (c-pull-open-brace paren-state))) ; might be a paren.
7937 (setq level-plausible
7938 (catch 'level
7939 (goto-char pos)
7940 (c-backward-syntactic-ws)
7941 (when (not (c-back-over-compound-identifier))
7942 (throw 'level nil))
7943 (c-backward-syntactic-ws)
7944 (c-back-over-list-of-member-inits)
7945 (and (eq (char-before) ?:)
7946 (save-excursion
7947 (c-backward-token-2)
7948 (not (looking-at c-:$-multichar-token-regexp)))
7949 (c-just-after-func-arglist-p)))))
7951 (and at-top-level level-plausible)))
7952 (or res (goto-char here))
7953 res))
7956 ;; Handling of large scale constructs like statements and declarations.
7958 ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
7959 ;; defsubst or perhaps even a defun, but it contains lots of free
7960 ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
7961 (defmacro c-fdoc-shift-type-backward (&optional short)
7962 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
7963 ;; of types when parsing a declaration, which means that it
7964 ;; sometimes consumes the identifier in the declaration as a type.
7965 ;; This is used to "backtrack" and make the last type be treated as
7966 ;; an identifier instead.
7967 `(progn
7968 ,(unless short
7969 ;; These identifiers are bound only in the inner let.
7970 '(setq identifier-type at-type
7971 identifier-start type-start
7972 got-parens nil
7973 got-identifier t
7974 got-suffix t
7975 got-suffix-after-parens id-start
7976 paren-depth 0))
7978 (if (setq at-type (if (eq backup-at-type 'prefix)
7980 backup-at-type))
7981 (setq type-start backup-type-start
7982 id-start backup-id-start)
7983 (setq type-start start-pos
7984 id-start start-pos))
7986 ;; When these flags already are set we've found specifiers that
7987 ;; unconditionally signal these attributes - backtracking doesn't
7988 ;; change that. So keep them set in that case.
7989 (or at-type-decl
7990 (setq at-type-decl backup-at-type-decl))
7991 (or maybe-typeless
7992 (setq maybe-typeless backup-maybe-typeless))
7994 ,(unless short
7995 ;; This identifier is bound only in the inner let.
7996 '(setq start id-start))))
7998 (defun c-forward-declarator (&optional limit accept-anon)
7999 ;; Assuming point is at the start of a declarator, move forward over it,
8000 ;; leaving point at the next token after it (e.g. a ) or a ; or a ,).
8002 ;; Return a list (ID-START ID-END BRACKETS-AFTER-ID GOT-INIT DECORATED),
8003 ;; where ID-START and ID-END are the bounds of the declarator's identifier,
8004 ;; and BRACKETS-AFTER-ID is non-nil if a [...] pair is present after the id.
8005 ;; GOT-INIT is non-nil when the declarator is followed by "=" or "(",
8006 ;; DECORATED is non-nil when the identifier is embellished by an operator,
8007 ;; like "*x", or "(*x)".
8009 ;; If ACCEPT-ANON is non-nil, move forward over any "anonymous declarator",
8010 ;; i.e. something like the (*) in int (*), such as might be found in a
8011 ;; declaration. In such a case ID-START and ID-END in the return value are
8012 ;; both set to nil. A "null" "anonymous declarator" gives a non-nil result.
8014 ;; If no declarator is found, leave point unmoved and return nil. LIMIT is
8015 ;; an optional limit for forward searching.
8017 ;; Note that the global variable `c-last-identifier-range' is written to, so
8018 ;; the caller should bind it if necessary.
8020 ;; Inside the following "condition form", we move forward over the
8021 ;; declarator's identifier up as far as any opening bracket (for array
8022 ;; size) or paren (for parameters of function-type) or brace (for
8023 ;; array/struct initialization) or "=" or terminating delimiter
8024 ;; (e.g. "," or ";" or "}").
8025 (let ((here (point))
8026 id-start id-end brackets-after-id paren-depth decorated)
8027 (or limit (setq limit (point-max)))
8028 (if (and
8029 (< (point) limit)
8031 ;; The following form moves forward over the declarator's
8032 ;; identifier (and what precedes it), returning t. If there
8033 ;; wasn't one, it returns nil.
8034 (let (got-identifier)
8035 (setq paren-depth 0)
8036 ;; Skip over type decl prefix operators, one for each iteration
8037 ;; of the while. These are, e.g. "*" in "int *foo" or "(" and
8038 ;; "*" in "int (*foo) (void)" (Note similar code in
8039 ;; `c-forward-decl-or-cast-1'.)
8040 (while
8041 (cond
8042 ((looking-at c-decl-hangon-key)
8043 (c-forward-keyword-clause 1))
8044 ((and c-opt-cpp-prefix
8045 (looking-at c-noise-macro-with-parens-name-re))
8046 (c-forward-noise-clause))
8047 ((and (looking-at c-type-decl-prefix-key)
8048 (if (and (c-major-mode-is 'c++-mode)
8049 (match-beginning 3))
8050 ;; If the third submatch matches in C++ then
8051 ;; we're looking at an identifier that's a
8052 ;; prefix only if it specifies a member pointer.
8053 (progn
8054 (setq id-start (point))
8055 (c-forward-name)
8056 (if (looking-at "\\(::\\)")
8057 ;; We only check for a trailing "::" and
8058 ;; let the "*" that should follow be
8059 ;; matched in the next round.
8061 ;; It turned out to be the real identifier,
8062 ;; so flag that and stop.
8063 (setq got-identifier t)
8064 nil))
8066 (if (looking-at c-type-decl-operator-prefix-key)
8067 (setq decorated t))
8068 (if (eq (char-after) ?\()
8069 (progn
8070 (setq paren-depth (1+ paren-depth))
8071 (forward-char))
8072 (goto-char (match-end 1)))
8073 (c-forward-syntactic-ws)
8074 t)))
8076 ;; If we haven't passed the identifier already, do it now.
8077 (unless got-identifier
8078 (setq id-start (point)))
8079 (cond
8080 ((or got-identifier
8081 (c-forward-name))
8082 (save-excursion
8083 (c-backward-syntactic-ws)
8084 (setq id-end (point))))
8085 (accept-anon
8086 (setq id-start nil id-end nil)
8088 (t (/= (point) here))))
8090 ;; Skip out of the parens surrounding the identifier. If closing
8091 ;; parens are missing, this form returns nil.
8092 (or (= paren-depth 0)
8093 (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
8095 (<= (point) limit)
8097 ;; Skip over any trailing bit, such as "__attribute__".
8098 (progn
8099 (while (cond
8100 ((looking-at c-decl-hangon-key)
8101 (c-forward-keyword-clause 1))
8102 ((and c-opt-cpp-prefix
8103 (looking-at c-noise-macro-with-parens-name-re))
8104 (c-forward-noise-clause))))
8105 (<= (point) limit))
8107 ;; Search syntactically to the end of the declarator (";",
8108 ;; ",", a closing paren, eob etc) or to the beginning of an
8109 ;; initializer or function prototype ("=" or "\\s\(").
8110 ;; Note that square brackets are now not also treated as
8111 ;; initializers, since this broke when there were also
8112 ;; initializing brace lists.
8113 (let (found)
8114 (while
8115 (and (< (point) limit)
8116 (progn
8117 ;; In the next loop, we keep searching forward whilst
8118 ;; we find ":"s which aren't single colons inside C++
8119 ;; "for" statements.
8120 (while
8121 (and
8122 (< (point) limit)
8123 (setq found
8124 (c-syntactic-re-search-forward
8125 "[;:,]\\|\\s)\\|\\(=\\|\\s(\\)"
8126 limit t t))
8127 (eq (char-before) ?:)
8128 (if (looking-at c-:-op-cont-regexp)
8129 (progn (goto-char (match-end 0)) t)
8130 (not
8131 (and (c-major-mode-is 'c++-mode)
8132 (save-excursion
8133 (and
8134 (c-go-up-list-backward)
8135 (eq (char-after) ?\()
8136 (progn (c-backward-syntactic-ws)
8137 (c-simple-skip-symbol-backward))
8138 (looking-at c-paren-stmt-key))))))))
8139 found)
8140 (eq (char-before) ?\[)
8141 (c-go-up-list-forward))
8142 (setq brackets-after-id t))
8143 (when found (backward-char))
8144 (<= (point) limit)))
8145 (list id-start id-end brackets-after-id (match-beginning 1) decorated)
8147 (goto-char here)
8148 nil)))
8150 (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
8151 ;; Move forward over a declaration or a cast if at the start of one.
8152 ;; The point is assumed to be at the start of some token. Nil is
8153 ;; returned if no declaration or cast is recognized, and the point
8154 ;; is clobbered in that case.
8156 ;; If a declaration is parsed:
8158 ;; The point is left at the first token after the first complete
8159 ;; declarator, if there is one. The return value is a list of 5 elements,
8160 ;; where the first is the position of the first token in the declarator.
8161 ;; (See below for the other four.)
8162 ;; Some examples:
8164 ;; void foo (int a, char *b) stuff ...
8165 ;; car ^ ^ point
8166 ;; float (*a)[], b;
8167 ;; car ^ ^ point
8168 ;; unsigned int a = c_style_initializer, b;
8169 ;; car ^ ^ point
8170 ;; unsigned int a (cplusplus_style_initializer), b;
8171 ;; car ^ ^ point (might change)
8172 ;; class Foo : public Bar {}
8173 ;; car ^ ^ point
8174 ;; class PikeClass (int a, string b) stuff ...
8175 ;; car ^ ^ point
8176 ;; enum bool;
8177 ;; car ^ ^ point
8178 ;; enum bool flag;
8179 ;; car ^ ^ point
8180 ;; void cplusplus_function (int x) throw (Bad);
8181 ;; car ^ ^ point
8182 ;; Foo::Foo (int b) : Base (b) {}
8183 ;; car ^ ^ point
8185 ;; auto foo = 5;
8186 ;; car ^ ^ point
8187 ;; auto cplusplus_11 (int a, char *b) -> decltype (bar):
8188 ;; car ^ ^ point
8192 ;; The second element of the return value is non-nil when a
8193 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
8194 ;; Specifically it is a dotted pair (A . B) where B is t when a
8195 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
8196 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
8197 ;; specifier is present. I.e., (some of) the declared
8198 ;; identifier(s) are types.
8200 ;; The third element of the return value is non-nil when the declaration
8201 ;; parsed might be an expression. The fourth element is the position of
8202 ;; the start of the type identifier. The fifth element is t if either
8203 ;; CONTEXT was 'top, or the declaration is detected to be treated as top
8204 ;; level (e.g. with the keyword "extern").
8206 ;; If a cast is parsed:
8208 ;; The point is left at the first token after the closing paren of
8209 ;; the cast. The return value is `cast'. Note that the start
8210 ;; position must be at the first token inside the cast parenthesis
8211 ;; to recognize it.
8213 ;; PRECEDING-TOKEN-END is the first position after the preceding
8214 ;; token, i.e. on the other side of the syntactic ws from the point.
8215 ;; Use a value less than or equal to (point-min) if the point is at
8216 ;; the first token in (the visible part of) the buffer.
8218 ;; CONTEXT is a symbol that describes the context at the point:
8219 ;; 'decl In a comma-separated declaration context (typically
8220 ;; inside a function declaration arglist).
8221 ;; '<> In an angle bracket arglist.
8222 ;; 'arglist Some other type of arglist.
8223 ;; 'top Some other context and point is at the top-level (either
8224 ;; outside any braces or directly inside a class or namespace,
8225 ;; etc.)
8226 ;; nil Some other context or unknown context. Includes
8227 ;; within the parens of an if, for, ... construct.
8228 ;; 'not-decl This value is never supplied to this function. It
8229 ;; would mean we're definitely not in a declaration.
8231 ;; LAST-CAST-END is the first token after the closing paren of a
8232 ;; preceding cast, or nil if none is known. If
8233 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
8234 ;; the position after the closest preceding call where a cast was
8235 ;; matched. In that case it's used to discover chains of casts like
8236 ;; "(a) (b) c".
8238 ;; This function records identifier ranges on
8239 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
8240 ;; `c-record-type-identifiers' is non-nil.
8242 ;; This function might do hidden buffer changes.
8244 (let (;; `start-pos' is used below to point to the start of the
8245 ;; first type, i.e. after any leading specifiers. It might
8246 ;; also point at the beginning of the preceding syntactic
8247 ;; whitespace.
8248 (start-pos (point))
8249 ;; Set to the result of `c-forward-type'.
8250 at-type
8251 ;; The position of the first token in what we currently
8252 ;; believe is the type in the declaration or cast, after any
8253 ;; specifiers and their associated clauses.
8254 type-start
8255 ;; The position of the first token in what we currently
8256 ;; believe is the declarator for the first identifier. Set
8257 ;; when the type is found, and moved forward over any
8258 ;; `c-decl-hangon-kwds' and their associated clauses that
8259 ;; occurs after the type.
8260 id-start
8261 ;; These store `at-type', `type-start' and `id-start' of the
8262 ;; identifier before the one in those variables. The previous
8263 ;; identifier might turn out to be the real type in a
8264 ;; declaration if the last one has to be the declarator in it.
8265 ;; If `backup-at-type' is nil then the other variables have
8266 ;; undefined values.
8267 backup-at-type backup-type-start backup-id-start
8268 ;; Set if we've found a specifier (apart from "typedef") that makes
8269 ;; the defined identifier(s) types.
8270 at-type-decl
8271 ;; Set if we've a "typedef" keyword.
8272 at-typedef
8273 ;; Set if we've found a specifier that can start a declaration
8274 ;; where there's no type.
8275 maybe-typeless
8276 ;; Save the value of kwd-sym between loops of the "Check for a
8277 ;; type" loop. Needed to distinguish a C++11 "auto" from a pre
8278 ;; C++11 one.
8279 prev-kwd-sym
8280 ;; If a specifier is found that also can be a type prefix,
8281 ;; these flags are set instead of those above. If we need to
8282 ;; back up an identifier, they are copied to the real flag
8283 ;; variables. Thus they only take effect if we fail to
8284 ;; interpret it as a type.
8285 backup-at-type-decl backup-maybe-typeless
8286 ;; Whether we've found a declaration or a cast. We might know
8287 ;; this before we've found the type in it. It's 'ids if we've
8288 ;; found two consecutive identifiers (usually a sure sign, but
8289 ;; we should allow that in labels too), and t if we've found a
8290 ;; specifier keyword (a 100% sure sign).
8291 at-decl-or-cast
8292 ;; Set when we need to back up to parse this as a declaration
8293 ;; but not as a cast.
8294 backup-if-not-cast
8295 ;; For casts, the return position.
8296 cast-end
8297 ;; Have we got a new-style C++11 "auto"?
8298 new-style-auto
8299 ;; Set when the symbol before `preceding-token-end' is known to
8300 ;; terminate the previous construct, or when we're at point-min.
8301 at-decl-start
8302 ;; Set when we have encountered a keyword (e.g. "extern") which
8303 ;; causes the following declaration to be treated as though top-level.
8304 make-top
8305 ;; Save `c-record-type-identifiers' and
8306 ;; `c-record-ref-identifiers' since ranges are recorded
8307 ;; speculatively and should be thrown away if it turns out
8308 ;; that it isn't a declaration or cast.
8309 (save-rec-type-ids c-record-type-identifiers)
8310 (save-rec-ref-ids c-record-ref-identifiers)
8311 ;; Set when we parse a declaration which might also be an expression,
8312 ;; such as "a *b". See CASE 16 and CASE 17.
8313 maybe-expression)
8315 (save-excursion
8316 (goto-char preceding-token-end)
8317 (setq at-decl-start
8318 (or (bobp)
8319 (let ((tok-end (point)))
8320 (c-backward-token-2)
8321 (member (buffer-substring-no-properties (point) tok-end)
8322 c-pre-start-tokens)))))
8324 (while (c-forward-annotation)
8325 (c-forward-syntactic-ws))
8327 ;; Check for a type. Unknown symbols are treated as possible
8328 ;; types, but they could also be specifiers disguised through
8329 ;; macros like __INLINE__, so we recognize both types and known
8330 ;; specifiers after them too.
8331 (while
8332 (let* ((start (point)) kwd-sym kwd-clause-end found-type noise-start)
8334 (cond
8335 ;; Look for a specifier keyword clause.
8336 ((or (and (looking-at c-make-top-level-key)
8337 (setq make-top t))
8338 (looking-at c-prefix-spec-kwds-re)
8339 (and (c-major-mode-is 'java-mode)
8340 (looking-at "@[A-Za-z0-9]+")))
8341 (save-match-data
8342 (if (looking-at c-typedef-key)
8343 (setq at-typedef t)))
8344 (setq kwd-sym (c-keyword-sym (match-string 1)))
8345 (save-excursion
8346 (c-forward-keyword-clause 1)
8347 (setq kwd-clause-end (point))))
8348 ((and c-opt-cpp-prefix
8349 (looking-at c-noise-macro-with-parens-name-re))
8350 (setq noise-start (point))
8351 (c-forward-noise-clause)
8352 (setq kwd-clause-end (point))))
8354 (when (setq found-type (c-forward-type t)) ; brace-block-too
8355 ;; Found a known or possible type or a prefix of a known type.
8356 (when (and (c-major-mode-is 'c++-mode) ; C++11 style "auto"?
8357 (eq prev-kwd-sym (c-keyword-sym "auto"))
8358 (looking-at "[=(]")) ; FIXME!!! proper regexp.
8359 (setq new-style-auto t)
8360 (setq found-type nil)
8361 (goto-char start)) ; position of foo in "auto foo"
8363 (when at-type
8364 ;; Got two identifiers with nothing but whitespace
8365 ;; between them. That can only happen in declarations.
8366 (setq at-decl-or-cast 'ids)
8368 (when (eq at-type 'found)
8369 ;; If the previous identifier is a found type we
8370 ;; record it as a real one; it might be some sort of
8371 ;; alias for a prefix like "unsigned".
8372 (save-excursion
8373 (goto-char type-start)
8374 (let ((c-promote-possible-types t))
8375 (c-forward-type)))))
8377 (setq backup-at-type at-type
8378 backup-type-start type-start
8379 backup-id-start id-start
8380 at-type found-type
8381 type-start start
8382 id-start (point)
8383 ;; The previous ambiguous specifier/type turned out
8384 ;; to be a type since we've parsed another one after
8385 ;; it, so clear these backup flags.
8386 backup-at-type-decl nil
8387 backup-maybe-typeless nil))
8389 (if (or kwd-sym noise-start)
8390 (progn
8391 ;; Handle known specifier keywords and
8392 ;; `c-decl-hangon-kwds' which can occur after known
8393 ;; types.
8395 (if (or (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
8396 noise-start)
8397 ;; It's a hang-on keyword or noise clause that can occur
8398 ;; anywhere.
8399 (progn
8400 (if at-type
8401 ;; Move the identifier start position if
8402 ;; we've passed a type.
8403 (setq id-start kwd-clause-end)
8404 ;; Otherwise treat this as a specifier and
8405 ;; move the fallback position.
8406 (setq start-pos kwd-clause-end))
8407 (goto-char kwd-clause-end))
8409 ;; It's an ordinary specifier so we know that
8410 ;; anything before this can't be the type.
8411 (setq backup-at-type nil
8412 start-pos kwd-clause-end)
8414 (if found-type
8415 ;; It's ambiguous whether this keyword is a
8416 ;; specifier or a type prefix, so set the backup
8417 ;; flags. (It's assumed that `c-forward-type'
8418 ;; moved further than `c-forward-keyword-clause'.)
8419 (progn
8420 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
8421 (setq backup-at-type-decl t))
8422 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
8423 (setq backup-maybe-typeless t)))
8425 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
8426 ;; This test only happens after we've scanned a type.
8427 ;; So, with valid syntax, kwd-sym can't be 'typedef.
8428 (setq at-type-decl t))
8429 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
8430 (setq maybe-typeless t))
8432 ;; Haven't matched a type so it's an unambiguous
8433 ;; specifier keyword and we know we're in a
8434 ;; declaration.
8435 (setq at-decl-or-cast t)
8436 (setq prev-kwd-sym kwd-sym)
8438 (goto-char kwd-clause-end))))
8440 ;; If the type isn't known we continue so that we'll jump
8441 ;; over all specifiers and type identifiers. The reason
8442 ;; to do this for a known type prefix is to make things
8443 ;; like "unsigned INT16" work.
8444 (and found-type (not (eq found-type t))))))
8446 (cond
8447 ((eq at-type t)
8448 ;; If a known type was found, we still need to skip over any
8449 ;; hangon keyword clauses after it. Otherwise it has already
8450 ;; been done in the loop above.
8451 (while
8452 (cond ((looking-at c-decl-hangon-key)
8453 (c-forward-keyword-clause 1))
8454 ((and c-opt-cpp-prefix
8455 (looking-at c-noise-macro-with-parens-name-re))
8456 (c-forward-noise-clause))))
8457 (setq id-start (point)))
8459 ((eq at-type 'prefix)
8460 ;; A prefix type is itself a primitive type when it's not
8461 ;; followed by another type.
8462 (setq at-type t))
8464 ((not at-type)
8465 ;; Got no type but set things up to continue anyway to handle
8466 ;; the various cases when a declaration doesn't start with a
8467 ;; type.
8468 (setq id-start start-pos))
8470 ((and (eq at-type 'maybe)
8471 (c-major-mode-is 'c++-mode))
8472 ;; If it's C++ then check if the last "type" ends on the form
8473 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
8474 ;; (con|de)structor.
8475 (save-excursion
8476 (let (name end-2 end-1)
8477 (goto-char id-start)
8478 (c-backward-syntactic-ws)
8479 (setq end-2 (point))
8480 (when (and
8481 (c-simple-skip-symbol-backward)
8482 (progn
8483 (setq name
8484 (buffer-substring-no-properties (point) end-2))
8485 ;; Cheating in the handling of syntactic ws below.
8486 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
8487 (progn
8488 (setq end-1 (point))
8489 (c-simple-skip-symbol-backward))
8490 (>= (point) type-start)
8491 (equal (buffer-substring-no-properties (point) end-1)
8492 name)
8493 (goto-char end-2)
8494 (progn
8495 (c-forward-syntactic-ws)
8496 (eq (char-after) ?\()))
8497 ;; It is a (con|de)structor name. In that case the
8498 ;; declaration is typeless so zap out any preceding
8499 ;; identifier(s) that we might have taken as types.
8500 (goto-char type-start)
8501 (setq at-type nil
8502 backup-at-type nil
8503 id-start type-start))))))
8505 ;; Check for and step over a type decl expression after the thing
8506 ;; that is or might be a type. This can't be skipped since we
8507 ;; need the correct end position of the declarator for
8508 ;; `max-type-decl-end-*'.
8509 (let ((start (point)) (paren-depth 0) pos
8510 ;; True if there's a non-open-paren match of
8511 ;; `c-type-decl-prefix-key'.
8512 got-prefix
8513 ;; True if the declarator is surrounded by a parenthesis pair.
8514 got-parens
8515 ;; True if there is an identifier in the declarator.
8516 got-identifier
8517 ;; True if there's a non-close-paren match of
8518 ;; `c-type-decl-suffix-key'.
8519 got-suffix
8520 ;; True if there's a prefix match outside the outermost
8521 ;; paren pair that surrounds the declarator.
8522 got-prefix-before-parens
8523 ;; True if there's a suffix match outside the outermost
8524 ;; paren pair that surrounds the declarator. The value is
8525 ;; the position of the first suffix match.
8526 got-suffix-after-parens
8527 ;; True if we've parsed the type decl to a token that is
8528 ;; known to end declarations in this context.
8529 at-decl-end
8530 ;; The earlier values of `at-type' and `type-start' if we've
8531 ;; shifted the type backwards.
8532 identifier-type identifier-start
8533 ;; If `c-parse-and-markup-<>-arglists' is set we need to
8534 ;; turn it off during the name skipping below to avoid
8535 ;; getting `c-type' properties that might be bogus. That
8536 ;; can happen since we don't know if
8537 ;; `c-restricted-<>-arglists' will be correct inside the
8538 ;; arglist paren that gets entered.
8539 c-parse-and-markup-<>-arglists
8540 ;; Start of the identifier for which `got-identifier' was set.
8541 name-start
8542 ;; Position after (innermost) open parenthesis encountered in the
8543 ;; prefix operators.
8544 after-paren-pos)
8546 (goto-char id-start)
8548 ;; Skip over type decl prefix operators. (Note similar code in
8549 ;; `c-forward-declarator'.)
8550 (if (and c-recognize-typeless-decls
8551 (equal c-type-decl-prefix-key "\\<\\>"))
8552 (when (eq (char-after) ?\()
8553 (progn
8554 (setq paren-depth (1+ paren-depth))
8555 (forward-char)
8556 (setq after-paren-pos (point))))
8557 (while (and (looking-at c-type-decl-prefix-key)
8558 (if (and (c-major-mode-is 'c++-mode)
8559 (match-beginning 3))
8560 ;; If the third submatch matches in C++ then
8561 ;; we're looking at an identifier that's a
8562 ;; prefix only if it specifies a member pointer.
8563 (when (progn (setq pos (point))
8564 (setq got-identifier (c-forward-name)))
8565 (setq name-start pos)
8566 (if (looking-at "\\(::\\)")
8567 ;; We only check for a trailing "::" and
8568 ;; let the "*" that should follow be
8569 ;; matched in the next round.
8570 (progn (setq got-identifier nil) t)
8571 ;; It turned out to be the real identifier,
8572 ;; so stop.
8573 nil))
8576 (if (eq (char-after) ?\()
8577 (progn
8578 (setq paren-depth (1+ paren-depth))
8579 (forward-char)
8580 (setq after-paren-pos (point)))
8581 (unless got-prefix-before-parens
8582 (setq got-prefix-before-parens (= paren-depth 0)))
8583 (setq got-prefix t)
8584 (goto-char (match-end 1)))
8585 (c-forward-syntactic-ws)))
8587 (setq got-parens (> paren-depth 0))
8589 ;; Try to skip over an identifier.
8590 (or got-identifier
8591 (and (looking-at c-identifier-start)
8592 (setq pos (point))
8593 (setq got-identifier (c-forward-name))
8594 (setq name-start pos)))
8596 ;; Skip over type decl suffix operators and trailing noise macros.
8597 (while
8598 (cond
8599 ((and c-opt-cpp-prefix
8600 (looking-at c-noise-macro-with-parens-name-re))
8601 (c-forward-noise-clause))
8603 ((and (looking-at c-type-decl-suffix-key)
8604 ;; We avoid recognizing foo(bar) or foo() at top level as a
8605 ;; construct here in C, since we want to recognize this as a
8606 ;; typeless function declaration.
8607 (not (and (c-major-mode-is 'c-mode)
8608 (or (eq context 'top) make-top)
8609 (eq (char-after) ?\)))))
8610 (if (eq (char-after) ?\))
8611 (when (> paren-depth 0)
8612 (setq paren-depth (1- paren-depth))
8613 (forward-char)
8615 (when (if (save-match-data (looking-at "\\s("))
8616 (c-safe (c-forward-sexp 1) t)
8617 (goto-char (match-end 1))
8619 (when (and (not got-suffix-after-parens)
8620 (= paren-depth 0))
8621 (setq got-suffix-after-parens (match-beginning 0)))
8622 (setq got-suffix t))))
8625 ;; No suffix matched. We might have matched the
8626 ;; identifier as a type and the open paren of a
8627 ;; function arglist as a type decl prefix. In that
8628 ;; case we should "backtrack": Reinterpret the last
8629 ;; type as the identifier, move out of the arglist and
8630 ;; continue searching for suffix operators.
8632 ;; Do this even if there's no preceding type, to cope
8633 ;; with old style function declarations in K&R C,
8634 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
8635 ;; style declarations. That isn't applicable in an
8636 ;; arglist context, though.
8637 (when (and (= paren-depth 1)
8638 (not got-prefix-before-parens)
8639 (not (eq at-type t))
8640 (or backup-at-type
8641 maybe-typeless
8642 backup-maybe-typeless
8643 (when c-recognize-typeless-decls
8644 (and (memq context '(nil top))
8645 ;; Deal with C++11's "copy-initialization"
8646 ;; where we have <type>(<constant>), by
8647 ;; contrasting with a typeless
8648 ;; <name>(<type><parameter>, ...).
8649 (save-excursion
8650 (goto-char after-paren-pos)
8651 (c-forward-syntactic-ws)
8652 (or (c-forward-type)
8653 ;; Recognize a top-level typeless
8654 ;; function declaration in C.
8655 (and (c-major-mode-is 'c-mode)
8656 (or (eq context 'top) make-top)
8657 (eq (char-after) ?\))))))))
8658 (setq pos (c-up-list-forward (point)))
8659 (eq (char-before pos) ?\)))
8660 (c-fdoc-shift-type-backward)
8661 (goto-char pos)
8662 t)))
8664 (c-forward-syntactic-ws))
8666 (when (or (and new-style-auto
8667 (looking-at c-auto-ops-re))
8668 (and (or maybe-typeless backup-maybe-typeless)
8669 (not got-identifier)
8670 (not got-prefix)
8671 at-type))
8672 ;; Have found no identifier but `c-typeless-decl-kwds' has
8673 ;; matched so we know we're inside a declaration. The
8674 ;; preceding type must be the identifier instead.
8675 (c-fdoc-shift-type-backward))
8677 ;; Prepare the "-> type;" for fontification later on.
8678 (when (and new-style-auto
8679 (looking-at c-haskell-op-re))
8680 (save-excursion
8681 (goto-char (match-end 0))
8682 (c-forward-syntactic-ws)
8683 (setq type-start (point))
8684 (setq at-type (c-forward-type))))
8686 ;; Move forward over any "WS" ids (like "final" or "override" in C++)
8687 (while (looking-at c-type-decl-suffix-ws-ids-key)
8688 (goto-char (match-end 1))
8689 (c-forward-syntactic-ws))
8691 (setq
8692 at-decl-or-cast
8693 (catch 'at-decl-or-cast
8695 ;; CASE 1
8696 (when (> paren-depth 0)
8697 ;; Encountered something inside parens that isn't matched by
8698 ;; the `c-type-decl-*' regexps, so it's not a type decl
8699 ;; expression. Try to skip out to the same paren depth to
8700 ;; not confuse the cast check below. If we don't manage this and
8701 ;; `at-decl-or-cast' is 'ids we might have an expression like
8702 ;; "foo bar ({ ..." which is a valid C++11 initialization.
8703 (if (and (not (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
8704 (eq at-decl-or-cast 'ids))
8705 (c-fdoc-shift-type-backward))
8706 ;; If we've found a specifier keyword then it's a
8707 ;; declaration regardless.
8708 (throw 'at-decl-or-cast (memq at-decl-or-cast '(t ids))))
8710 (setq at-decl-end
8711 (looking-at (cond ((eq context '<>) "[,>]")
8712 ((not (memq context '(nil top))) "[,\)]")
8713 (t "[,;]"))))
8715 ;; Now we've collected info about various characteristics of
8716 ;; the construct we're looking at. Below follows a decision
8717 ;; tree based on that. It's ordered to check more certain
8718 ;; signs before less certain ones.
8720 (if got-identifier
8721 (progn
8723 ;; CASE 2
8724 (when (and (or at-type maybe-typeless)
8725 (not (or got-prefix got-parens)))
8726 ;; Got another identifier directly after the type, so it's a
8727 ;; declaration.
8728 (throw 'at-decl-or-cast t))
8730 (when (and got-parens
8731 (not got-prefix)
8732 ;; (not got-suffix-after-parens)
8733 (or backup-at-type
8734 maybe-typeless
8735 backup-maybe-typeless
8736 (eq at-decl-or-cast t)
8737 ;; Check whether we have "bar (gnu);" where we
8738 ;; are directly inside a class (etc.) called "bar".
8739 (save-excursion
8740 (and
8741 (progn
8742 (goto-char name-start)
8743 (not (memq (c-forward-type) '(nil maybe))))
8744 (progn
8745 (goto-char id-start)
8746 (c-directly-in-class-called-p
8747 (buffer-substring
8748 type-start
8749 (progn
8750 (goto-char type-start)
8751 (c-forward-type)
8752 (c-backward-syntactic-ws)
8753 (point)))))))))
8754 ;; Got a declaration of the form "foo bar (gnu);" or "bar
8755 ;; (gnu);" where we've recognized "bar" as the type and "gnu"
8756 ;; as the declarator, and in the latter case, checked that
8757 ;; "bar (gnu)" appears directly inside the class "bar". In
8758 ;; this case it's however more likely that "bar" is the
8759 ;; declarator and "gnu" a function argument or initializer
8760 ;; (if `c-recognize-paren-inits' is set), since the parens
8761 ;; around "gnu" would be superfluous if it's a declarator.
8762 ;; Shift the type one step backward.
8763 (c-fdoc-shift-type-backward)))
8765 ;; Found no identifier.
8767 (if backup-at-type
8768 (progn
8770 ;; CASE 3
8771 (when (= (point) start)
8772 ;; Got a plain list of identifiers. If a colon follows it's
8773 ;; a valid label, or maybe a bitfield. Otherwise the last
8774 ;; one probably is the declared identifier and we should
8775 ;; back up to the previous type, providing it isn't a cast.
8776 (if (and (eq (char-after) ?:)
8777 (not (c-major-mode-is 'java-mode)))
8778 (cond
8779 ;; If we've found a specifier keyword then it's a
8780 ;; declaration regardless.
8781 ((eq at-decl-or-cast t)
8782 (throw 'at-decl-or-cast t))
8783 ((and c-has-bitfields
8784 (eq at-decl-or-cast 'ids)) ; bitfield.
8785 (setq backup-if-not-cast t)
8786 (throw 'at-decl-or-cast t)))
8788 (setq backup-if-not-cast t)
8789 (throw 'at-decl-or-cast t)))
8791 ;; CASE 4
8792 (when (and got-suffix
8793 (not got-prefix)
8794 (not got-parens))
8795 ;; Got a plain list of identifiers followed by some suffix.
8796 ;; If this isn't a cast then the last identifier probably is
8797 ;; the declared one and we should back up to the previous
8798 ;; type.
8799 (setq backup-if-not-cast t)
8800 (throw 'at-decl-or-cast t)))
8802 ;; CASE 5
8803 (when (eq at-type t)
8804 ;; If the type is known we know that there can't be any
8805 ;; identifier somewhere else, and it's only in declarations in
8806 ;; e.g. function prototypes and in casts that the identifier may
8807 ;; be left out.
8808 (throw 'at-decl-or-cast t))
8810 (when (= (point) start)
8811 ;; Only got a single identifier (parsed as a type so far).
8812 ;; CASE 6
8813 (if (and
8814 ;; Check that the identifier isn't at the start of an
8815 ;; expression.
8816 at-decl-end
8817 (cond
8818 ((eq context 'decl)
8819 ;; Inside an arglist that contains declarations. If K&R
8820 ;; style declarations and parenthesis style initializers
8821 ;; aren't allowed then the single identifier must be a
8822 ;; type, else we require that it's known or found
8823 ;; (primitive types are handled above).
8824 (or (and (not c-recognize-knr-p)
8825 (not c-recognize-paren-inits))
8826 (memq at-type '(known found))))
8827 ((eq context '<>)
8828 ;; Inside a template arglist. Accept known and found
8829 ;; types; other identifiers could just as well be
8830 ;; constants in C++.
8831 (memq at-type '(known found)))))
8832 (throw 'at-decl-or-cast t)
8833 ;; CASE 7
8834 ;; Can't be a valid declaration or cast, but if we've found a
8835 ;; specifier it can't be anything else either, so treat it as
8836 ;; an invalid/unfinished declaration or cast.
8837 (throw 'at-decl-or-cast at-decl-or-cast))))
8839 (if (and got-parens
8840 (not got-prefix)
8841 (memq context '(nil top))
8842 (not (eq at-type t))
8843 (or backup-at-type
8844 maybe-typeless
8845 backup-maybe-typeless
8846 (when c-recognize-typeless-decls
8847 (or (not got-suffix)
8848 (not (looking-at
8849 c-after-suffixed-type-maybe-decl-key))))))
8850 ;; Got an empty paren pair and a preceding type that probably
8851 ;; really is the identifier. Shift the type backwards to make
8852 ;; the last one the identifier. This is analogous to the
8853 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
8854 ;; above.
8856 ;; Exception: In addition to the conditions in that
8857 ;; "backtracking" code, do not shift backward if we're not
8858 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
8859 ;; Since there's no preceding type, the shift would mean that
8860 ;; the declaration is typeless. But if the regexp doesn't match
8861 ;; then we will simply fall through in the tests below and not
8862 ;; recognize it at all, so it's better to try it as an abstract
8863 ;; declarator instead.
8864 (c-fdoc-shift-type-backward)
8866 ;; Still no identifier.
8867 ;; CASE 8
8868 (when (and got-prefix (or got-parens got-suffix))
8869 ;; Require `got-prefix' together with either `got-parens' or
8870 ;; `got-suffix' to recognize it as an abstract declarator:
8871 ;; `got-parens' only is probably an empty function call.
8872 ;; `got-suffix' only can build an ordinary expression together
8873 ;; with the preceding identifier which we've taken as a type.
8874 ;; We could actually accept on `got-prefix' only, but that can
8875 ;; easily occur temporarily while writing an expression so we
8876 ;; avoid that case anyway. We could do a better job if we knew
8877 ;; the point when the fontification was invoked.
8878 (throw 'at-decl-or-cast t))
8880 ;; CASE 9
8881 (when (and at-type
8882 (not got-prefix)
8883 (not got-parens)
8884 got-suffix-after-parens
8885 (eq (char-after got-suffix-after-parens) ?\())
8886 ;; Got a type, no declarator but a paren suffix. I.e. it's a
8887 ;; normal function call after all (or perhaps a C++ style object
8888 ;; instantiation expression).
8889 (throw 'at-decl-or-cast nil))))
8891 ;; CASE 9.5
8892 (when (and (not context) ; i.e. not at top level.
8893 (c-major-mode-is 'c++-mode)
8894 (eq at-decl-or-cast 'ids)
8895 after-paren-pos)
8896 ;; We've got something like "foo bar (...)" in C++ which isn't at
8897 ;; the top level. This is probably a uniform initialization of bar
8898 ;; to the contents of the parens. In this case the declarator ends
8899 ;; at the open paren.
8900 (goto-char (1- after-paren-pos))
8901 (throw 'at-decl-or-cast t))
8903 ;; CASE 10
8904 (when at-decl-or-cast
8905 ;; By now we've located the type in the declaration that we know
8906 ;; we're in.
8907 (throw 'at-decl-or-cast t))
8909 ;; CASE 11
8910 (when (and got-identifier
8911 (looking-at c-after-suffixed-type-decl-key)
8912 (or (eq context 'top)
8913 make-top
8914 (and (eq context nil)
8915 (match-beginning 1)))
8916 (if (and got-parens
8917 (not got-prefix)
8918 (not got-suffix)
8919 (not (eq at-type t)))
8920 ;; Shift the type backward in the case that there's a
8921 ;; single identifier inside parens. That can only
8922 ;; occur in K&R style function declarations so it's
8923 ;; more likely that it really is a function call.
8924 ;; Therefore we only do this after
8925 ;; `c-after-suffixed-type-decl-key' has matched.
8926 (progn (c-fdoc-shift-type-backward) t)
8927 got-suffix-after-parens))
8928 ;; A declaration according to `c-after-suffixed-type-decl-key'.
8929 (throw 'at-decl-or-cast t))
8931 ;; CASE 12
8932 (when (and (or got-prefix (not got-parens))
8933 (memq at-type '(t known)))
8934 ;; It's a declaration if a known type precedes it and it can't be a
8935 ;; function call.
8936 (throw 'at-decl-or-cast t))
8938 ;; If we get here we can't tell if this is a type decl or a normal
8939 ;; expression by looking at it alone. (That's under the assumption
8940 ;; that normal expressions always can look like type decl expressions,
8941 ;; which isn't really true but the cases where it doesn't hold are so
8942 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
8943 ;; the effort to look for them.)
8945 ;;; 2008-04-16: commented out the next form, to allow the function to recognize
8946 ;;; "foo (int bar)" in CC (an implicit type (in class foo) without a semicolon)
8947 ;;; as a(n almost complete) declaration, enabling it to be fontified.
8948 ;; CASE 13
8949 ;; (unless (or at-decl-end (looking-at "=[^=]"))
8950 ;; If this is a declaration it should end here or its initializer(*)
8951 ;; should start here, so check for allowed separation tokens. Note
8952 ;; that this rule doesn't work e.g. with a K&R arglist after a
8953 ;; function header.
8955 ;; *) Don't check for C++ style initializers using parens
8956 ;; since those already have been matched as suffixes.
8958 ;; If `at-decl-or-cast' is then we've found some other sign that
8959 ;; it's a declaration or cast, so then it's probably an
8960 ;; invalid/unfinished one.
8961 ;; (throw 'at-decl-or-cast at-decl-or-cast))
8963 ;; Below are tests that only should be applied when we're certain to
8964 ;; not have parsed halfway through an expression.
8966 ;; CASE 14
8967 (when (memq at-type '(t known))
8968 ;; The expression starts with a known type so treat it as a
8969 ;; declaration.
8970 (throw 'at-decl-or-cast t))
8972 ;; CASE 15
8973 (when (and (c-major-mode-is 'c++-mode)
8974 ;; In C++ we check if the identifier is a known type, since
8975 ;; (con|de)structors use the class name as identifier.
8976 ;; We've always shifted over the identifier as a type and
8977 ;; then backed up again in this case.
8978 identifier-type
8979 (or (memq identifier-type '(found known))
8980 (and (eq (char-after identifier-start) ?~)
8981 ;; `at-type' probably won't be 'found for
8982 ;; destructors since the "~" is then part of the
8983 ;; type name being checked against the list of
8984 ;; known types, so do a check without that
8985 ;; operator.
8986 (or (save-excursion
8987 (goto-char (1+ identifier-start))
8988 (c-forward-syntactic-ws)
8989 (c-with-syntax-table
8990 c-identifier-syntax-table
8991 (looking-at c-known-type-key)))
8992 (save-excursion
8993 (goto-char (1+ identifier-start))
8994 ;; We have already parsed the type earlier,
8995 ;; so it'd be possible to cache the end
8996 ;; position instead of redoing it here, but
8997 ;; then we'd need to keep track of another
8998 ;; position everywhere.
8999 (c-check-type (point)
9000 (progn (c-forward-type)
9001 (point))))))))
9002 (throw 'at-decl-or-cast t))
9004 (if got-identifier
9005 (progn
9006 ;; CASE 16
9007 (when (and got-prefix-before-parens
9008 at-type
9009 (or at-decl-end (looking-at "=[^=]"))
9010 (memq context '(nil top))
9011 (or (not got-suffix)
9012 at-decl-start))
9013 ;; Got something like "foo * bar;". Since we're not inside
9014 ;; an arglist it would be a meaningless expression because
9015 ;; the result isn't used. We therefore choose to recognize
9016 ;; it as a declaration. We only allow a suffix (which makes
9017 ;; the construct look like a function call) when
9018 ;; `at-decl-start' provides additional evidence that we do
9019 ;; have a declaration.
9020 (setq maybe-expression t)
9021 (throw 'at-decl-or-cast t))
9023 ;; CASE 17
9024 (when (and (or got-suffix-after-parens
9025 (looking-at "=[^=]"))
9026 (eq at-type 'found)
9027 (not (eq context 'arglist)))
9028 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
9029 ;; be an odd expression or it could be a declaration. Treat
9030 ;; it as a declaration if "a" has been used as a type
9031 ;; somewhere else (if it's a known type we won't get here).
9032 (setq maybe-expression t)
9033 (throw 'at-decl-or-cast t))
9035 ;; CASE 17.5
9036 (when (and c-asymmetry-fontification-flag
9037 got-prefix-before-parens
9038 at-type
9039 (or (not got-suffix)
9040 at-decl-start))
9041 (let ((space-before-id
9042 (save-excursion
9043 (goto-char name-start)
9044 (or (bolp) (memq (char-before) '(?\ ?\t)))))
9045 (space-after-type
9046 (save-excursion
9047 (goto-char type-start)
9048 (and (c-forward-type)
9049 (progn (c-backward-syntactic-ws) t)
9050 (or (eolp)
9051 (memq (char-after) '(?\ ?\t)))))))
9052 (when (not (eq (not space-before-id)
9053 (not space-after-type)))
9054 (setq maybe-expression t)
9055 (throw 'at-decl-or-cast t)))))
9057 ;; CASE 18
9058 (when (and (not (memq context '(nil top)))
9059 (or got-prefix
9060 (and (eq context 'decl)
9061 (not c-recognize-paren-inits)
9062 (or got-parens got-suffix))))
9063 ;; Got a type followed by an abstract declarator. If `got-prefix'
9064 ;; is set it's something like "a *" without anything after it. If
9065 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
9066 ;; or similar, which we accept only if the context rules out
9067 ;; expressions.
9068 (throw 'at-decl-or-cast t)))
9070 ;; If we had a complete symbol table here (which rules out
9071 ;; `c-found-types') we should return t due to the disambiguation rule
9072 ;; (in at least C++) that anything that can be parsed as a declaration
9073 ;; is a declaration. Now we're being more defensive and prefer to
9074 ;; highlight things like "foo (bar);" as a declaration only if we're
9075 ;; inside an arglist that contains declarations. Update (2017-09): We
9076 ;; now recognize a top-level "foo(bar);" as a declaration in C.
9077 ;; CASE 19
9078 (or (eq context 'decl)
9079 (and (c-major-mode-is 'c-mode)
9080 (or (eq context 'top) make-top))))))
9082 ;; The point is now after the type decl expression.
9084 (cond
9085 ;; Check for a cast.
9086 ((save-excursion
9087 (and
9088 c-cast-parens
9090 ;; Should be the first type/identifier in a cast paren.
9091 (> preceding-token-end (point-min))
9092 (memq (char-before preceding-token-end) c-cast-parens)
9094 ;; The closing paren should follow.
9095 (progn
9096 (c-forward-syntactic-ws)
9097 (looking-at "\\s)"))
9099 ;; There should be a primary expression after it.
9100 (let (pos)
9101 (forward-char)
9102 (c-forward-syntactic-ws)
9103 (setq cast-end (point))
9104 (and (looking-at c-primary-expr-regexp)
9105 (progn
9106 (setq pos (match-end 0))
9108 ;; Check if the expression begins with a prefix keyword.
9109 (match-beginning 2)
9110 (if (match-beginning 1)
9111 ;; Expression begins with an ambiguous operator. Treat
9112 ;; it as a cast if it's a type decl or if we've
9113 ;; recognized the type somewhere else.
9114 (or at-decl-or-cast
9115 (memq at-type '(t known found)))
9116 ;; Unless it's a keyword, it's the beginning of a primary
9117 ;; expression.
9118 (not (looking-at c-keywords-regexp)))))
9119 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
9120 ;; that it matched a whole one so that we don't e.g. confuse
9121 ;; the operator '-' with '->'. It's ok if it matches further,
9122 ;; though, since it e.g. can match the float '.5' while the
9123 ;; operator regexp only matches '.'.
9124 (or (not (looking-at c-nonsymbol-token-regexp))
9125 (<= (match-end 0) pos))))
9127 ;; There should either be a cast before it or something that isn't an
9128 ;; identifier or close paren.
9129 (> preceding-token-end (point-min))
9130 (progn
9131 (goto-char (1- preceding-token-end))
9132 (or (eq (point) last-cast-end)
9133 (progn
9134 (c-backward-syntactic-ws)
9135 (if (< (skip-syntax-backward "w_") 0)
9136 ;; It's a symbol. Accept it only if it's one of the
9137 ;; keywords that can precede an expression (without
9138 ;; surrounding parens).
9139 (looking-at c-simple-stmt-key)
9140 (and
9141 ;; Check that it isn't a close paren (block close is ok,
9142 ;; though).
9143 (not (memq (char-before) '(?\) ?\])))
9144 ;; Check that it isn't a nonsymbol identifier.
9145 (not (c-on-identifier)))))))))
9147 ;; Handle the cast.
9148 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
9149 (let ((c-promote-possible-types t))
9150 (goto-char type-start)
9151 (c-forward-type)))
9153 (goto-char cast-end)
9154 'cast)
9156 (at-decl-or-cast
9157 ;; We're at a declaration. Highlight the type and the following
9158 ;; declarators.
9160 (when backup-if-not-cast
9161 (c-fdoc-shift-type-backward t))
9163 (when (and (eq context 'decl) (looking-at ","))
9164 ;; Make sure to propagate the `c-decl-arg-start' property to
9165 ;; the next argument if it's set in this one, to cope with
9166 ;; interactive refontification.
9167 (c-put-c-type-property (point) 'c-decl-arg-start))
9169 ;; Record the type's coordinates in `c-record-type-identifiers' for
9170 ;; later fontification.
9171 (when (and c-record-type-identifiers at-type ;; (not (eq at-type t))
9172 ;; There seems no reason to exclude a token from
9173 ;; fontification just because it's "a known type that can't
9174 ;; be a name or other expression". 2013-09-18.
9176 (let ((c-promote-possible-types t))
9177 (save-excursion
9178 (goto-char type-start)
9179 (c-forward-type))))
9181 (list id-start
9182 (and (or at-type-decl at-typedef)
9183 (cons at-type-decl at-typedef))
9184 maybe-expression
9185 type-start
9186 (or (eq context 'top) make-top)))
9189 ;; False alarm. Restore the recorded ranges.
9190 (setq c-record-type-identifiers save-rec-type-ids
9191 c-record-ref-identifiers save-rec-ref-ids)
9192 nil))))
9194 (defun c-forward-label (&optional assume-markup preceding-token-end limit)
9195 ;; Assuming that point is at the beginning of a token, check if it starts a
9196 ;; label and if so move over it and return non-nil (t in default situations,
9197 ;; specific symbols (see below) for interesting situations), otherwise don't
9198 ;; move and return nil. "Label" here means "most things with a colon".
9200 ;; More precisely, a "label" is regarded as one of:
9201 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
9202 ;; (ii) A case label - either the entire construct "case FOO:", or just the
9203 ;; bare "case", should the colon be missing. We return t;
9204 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
9205 ;; return t;
9206 ;; (iv) One of QT's "extended" C++ variants of
9207 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
9208 ;; Returns the symbol `qt-2kwds-colon'.
9209 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
9210 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
9211 ;; colon). Currently (2006-03), this applies only to Objective C's
9212 ;; keywords "@private", "@protected", and "@public". Returns t.
9214 ;; One of the things which will NOT be recognized as a label is a bit-field
9215 ;; element of a struct, something like "int foo:5".
9217 ;; The end of the label is taken to be just after the colon, or the end of
9218 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
9219 ;; after the end on return. The terminating char gets marked with
9220 ;; `c-decl-end' to improve recognition of the following declaration or
9221 ;; statement.
9223 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
9224 ;; label, if any, has already been marked up like that.
9226 ;; If PRECEDING-TOKEN-END is given, it should be the first position
9227 ;; after the preceding token, i.e. on the other side of the
9228 ;; syntactic ws from the point. Use a value less than or equal to
9229 ;; (point-min) if the point is at the first token in (the visible
9230 ;; part of) the buffer.
9232 ;; The optional LIMIT limits the forward scan for the colon.
9234 ;; This function records the ranges of the label symbols on
9235 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
9236 ;; non-nil.
9238 ;; This function might do hidden buffer changes.
9240 (let ((start (point))
9241 label-end
9242 qt-symbol-idx
9243 macro-start ; if we're in one.
9244 label-type
9245 kwd)
9246 (cond
9247 ;; "case" or "default" (Doesn't apply to AWK).
9248 ((looking-at c-label-kwds-regexp)
9249 (let ((kwd-end (match-end 1)))
9250 ;; Record only the keyword itself for fontification, since in
9251 ;; case labels the following is a constant expression and not
9252 ;; a label.
9253 (when c-record-type-identifiers
9254 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
9256 ;; Find the label end.
9257 (goto-char kwd-end)
9258 (setq label-type
9259 (if (and (c-syntactic-re-search-forward
9260 ;; Stop on chars that aren't allowed in expressions,
9261 ;; and on operator chars that would be meaningless
9262 ;; there. FIXME: This doesn't cope with ?: operators.
9263 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
9264 limit t t nil 1)
9265 (match-beginning 2))
9267 (progn ; there's a proper :
9268 (goto-char (match-beginning 2)) ; just after the :
9269 (c-put-c-type-property (1- (point)) 'c-decl-end)
9272 ;; It's an unfinished label. We consider the keyword enough
9273 ;; to recognize it as a label, so that it gets fontified.
9274 ;; Leave the point at the end of it, but don't put any
9275 ;; `c-decl-end' marker.
9276 (goto-char kwd-end)
9277 t))))
9279 ;; @private, @protected, @public, in Objective C, or similar.
9280 ((and c-opt-extra-label-key
9281 (looking-at c-opt-extra-label-key))
9282 ;; For a `c-opt-extra-label-key' match, we record the whole
9283 ;; thing for fontification. That's to get the leading '@' in
9284 ;; Objective-C protection labels fontified.
9285 (goto-char (match-end 1))
9286 (when c-record-type-identifiers
9287 (c-record-ref-id (cons (match-beginning 1) (point))))
9288 (c-put-c-type-property (1- (point)) 'c-decl-end)
9289 (setq label-type t))
9291 ;; All other cases of labels.
9292 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
9294 ;; A colon label must have something before the colon.
9295 (not (eq (char-after) ?:))
9297 ;; Check that we're not after a token that can't precede a label.
9299 ;; Trivially succeeds when there's no preceding token.
9300 ;; Succeeds when we're at a virtual semicolon.
9301 (if preceding-token-end
9302 (<= preceding-token-end (point-min))
9303 (save-excursion
9304 (c-backward-syntactic-ws)
9305 (setq preceding-token-end (point))
9306 (or (bobp)
9307 (c-at-vsemi-p))))
9309 ;; Check if we're after a label, if we're after a closing
9310 ;; paren that belong to statement, and with
9311 ;; `c-label-prefix-re'. It's done in different order
9312 ;; depending on `assume-markup' since the checks have
9313 ;; different expensiveness.
9314 (if assume-markup
9316 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
9317 'c-decl-end)
9319 (save-excursion
9320 (goto-char (1- preceding-token-end))
9321 (c-beginning-of-current-token)
9322 (or (looking-at c-label-prefix-re)
9323 (looking-at c-block-stmt-1-key)))
9325 (and (eq (char-before preceding-token-end) ?\))
9326 (c-after-conditional)))
9329 (save-excursion
9330 (goto-char (1- preceding-token-end))
9331 (c-beginning-of-current-token)
9332 (or (looking-at c-label-prefix-re)
9333 (looking-at c-block-stmt-1-key)))
9335 (cond
9336 ((eq (char-before preceding-token-end) ?\))
9337 (c-after-conditional))
9339 ((eq (char-before preceding-token-end) ?:)
9340 ;; Might be after another label, so check it recursively.
9341 (save-restriction
9342 (save-excursion
9343 (goto-char (1- preceding-token-end))
9344 ;; Essentially the same as the
9345 ;; `c-syntactic-re-search-forward' regexp below.
9346 (setq macro-start
9347 (save-excursion (and (c-beginning-of-macro)
9348 (point))))
9349 (if macro-start (narrow-to-region macro-start (point-max)))
9350 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
9351 ;; Note: the following should work instead of the
9352 ;; narrow-to-region above. Investigate why not,
9353 ;; sometime. ACM, 2006-03-31.
9354 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
9355 ;; macro-start t)
9356 (let ((pte (point))
9357 ;; If the caller turned on recording for us,
9358 ;; it shouldn't apply when we check the
9359 ;; preceding label.
9360 c-record-type-identifiers)
9361 ;; A label can't start at a cpp directive. Check for
9362 ;; this, since c-forward-syntactic-ws would foul up on it.
9363 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
9364 (c-forward-syntactic-ws)
9365 (c-forward-label nil pte start))))))))))
9367 ;; Point is still at the beginning of the possible label construct.
9369 ;; Check that the next nonsymbol token is ":", or that we're in one
9370 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
9371 ;; arguments. FIXME: Should build this regexp from the language
9372 ;; constants.
9373 (cond
9374 ;; public: protected: private:
9375 ((and
9376 (c-major-mode-is 'c++-mode)
9377 (search-forward-regexp
9378 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
9379 (progn (backward-char)
9380 (c-forward-syntactic-ws limit)
9381 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
9382 (forward-char)
9383 (setq label-type t))
9384 ;; QT double keyword like "protected slots:" or goto target.
9385 ((progn (goto-char start) nil))
9386 ((when (c-syntactic-re-search-forward
9387 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
9388 (backward-char)
9389 (setq label-end (point))
9390 (setq qt-symbol-idx
9391 (and (c-major-mode-is 'c++-mode)
9392 (string-match
9393 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
9394 (buffer-substring start (point)))))
9395 (c-forward-syntactic-ws limit)
9396 (cond
9397 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
9398 (forward-char)
9399 (setq label-type
9400 (if (or (string= "signals" ; Special QT macro
9401 (setq kwd (buffer-substring-no-properties start label-end)))
9402 (string= "Q_SIGNALS" kwd))
9403 'qt-1kwd-colon
9404 'goto-target)))
9405 ((and qt-symbol-idx
9406 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
9407 (progn (c-forward-syntactic-ws limit)
9408 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
9409 (forward-char)
9410 (setq label-type 'qt-2kwds-colon)))))))
9412 (save-restriction
9413 (narrow-to-region start (point))
9415 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
9416 (catch 'check-label
9417 (goto-char start)
9418 (while (progn
9419 (when (looking-at c-nonlabel-token-key)
9420 (goto-char start)
9421 (setq label-type nil)
9422 (throw 'check-label nil))
9423 (and (c-safe (c-forward-sexp)
9424 (c-forward-syntactic-ws)
9426 (not (eobp)))))
9428 ;; Record the identifiers in the label for fontification, unless
9429 ;; it begins with `c-label-kwds' in which case the following
9430 ;; identifiers are part of a (constant) expression that
9431 ;; shouldn't be fontified.
9432 (when (and c-record-type-identifiers
9433 (progn (goto-char start)
9434 (not (looking-at c-label-kwds-regexp))))
9435 (while (c-syntactic-re-search-forward c-symbol-key nil t)
9436 (c-record-ref-id (cons (match-beginning 0)
9437 (match-end 0)))))
9439 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
9440 (goto-char (point-max)))))
9443 ;; Not a label.
9444 (goto-char start)))
9445 label-type))
9447 (defun c-forward-objc-directive ()
9448 ;; Assuming the point is at the beginning of a token, try to move
9449 ;; forward to the end of the Objective-C directive that starts
9450 ;; there. Return t if a directive was fully recognized, otherwise
9451 ;; the point is moved as far as one could be successfully parsed and
9452 ;; nil is returned.
9454 ;; This function records identifier ranges on
9455 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
9456 ;; `c-record-type-identifiers' is non-nil.
9458 ;; This function might do hidden buffer changes.
9460 (let ((start (point))
9461 start-char
9462 (c-promote-possible-types t)
9464 ;; Turn off recognition of angle bracket arglists while parsing
9465 ;; types here since the protocol reference list might then be
9466 ;; considered part of the preceding name or superclass-name.
9467 c-recognize-<>-arglists)
9469 (if (or
9470 (when (looking-at
9471 (eval-when-compile
9472 (c-make-keywords-re t
9473 (append (c-lang-const c-protection-kwds objc)
9474 '("@end"))
9475 'objc-mode)))
9476 (goto-char (match-end 1))
9479 (and
9480 (looking-at
9481 (eval-when-compile
9482 (c-make-keywords-re t
9483 '("@interface" "@implementation" "@protocol")
9484 'objc-mode)))
9486 ;; Handle the name of the class itself.
9487 (progn
9488 ;; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
9489 ;; at EOB.
9490 (goto-char (match-end 0))
9491 (setq lim (point))
9492 (c-skip-ws-forward)
9493 (c-forward-type))
9495 (catch 'break
9496 ;; Look for ": superclass-name" or "( category-name )".
9497 (when (looking-at "[:(]")
9498 (setq start-char (char-after))
9499 (forward-char)
9500 (c-forward-syntactic-ws)
9501 (unless (c-forward-type) (throw 'break nil))
9502 (when (eq start-char ?\()
9503 (unless (eq (char-after) ?\)) (throw 'break nil))
9504 (forward-char)
9505 (c-forward-syntactic-ws)))
9507 ;; Look for a protocol reference list.
9508 (if (eq (char-after) ?<)
9509 (let ((c-recognize-<>-arglists t)
9510 (c-parse-and-markup-<>-arglists t)
9511 c-restricted-<>-arglists)
9512 (c-forward-<>-arglist t))
9513 t))))
9515 (progn
9516 (c-backward-syntactic-ws lim)
9517 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
9518 (c-put-c-type-property (1- (point)) 'c-decl-end)
9521 (c-clear-c-type-property start (point) 'c-decl-end)
9522 nil)))
9524 (defun c-beginning-of-inheritance-list (&optional lim)
9525 ;; Go to the first non-whitespace after the colon that starts a
9526 ;; multiple inheritance introduction. Optional LIM is the farthest
9527 ;; back we should search.
9529 ;; This function might do hidden buffer changes.
9530 (c-with-syntax-table c++-template-syntax-table
9531 (c-backward-token-2 0 t lim)
9532 (while (and (or (looking-at c-symbol-start)
9533 (looking-at "[<,]\\|::"))
9534 (zerop (c-backward-token-2 1 t lim))))))
9536 (defun c-in-method-def-p ()
9537 ;; Return nil if we aren't in a method definition, otherwise the
9538 ;; position of the initial [+-].
9540 ;; This function might do hidden buffer changes.
9541 (save-excursion
9542 (beginning-of-line)
9543 (and c-opt-method-key
9544 (looking-at c-opt-method-key)
9545 (point))
9548 ;; Contributed by Kevin Ryde <user42@zip.com.au>.
9549 (defun c-in-gcc-asm-p ()
9550 ;; Return non-nil if point is within a gcc \"asm\" block.
9552 ;; This should be called with point inside an argument list.
9554 ;; Only one level of enclosing parentheses is considered, so for
9555 ;; instance nil is returned when in a function call within an asm
9556 ;; operand.
9558 ;; This function might do hidden buffer changes.
9560 (and c-opt-asm-stmt-key
9561 (save-excursion
9562 (beginning-of-line)
9563 (backward-up-list 1)
9564 (c-beginning-of-statement-1 (point-min) nil t)
9565 (looking-at c-opt-asm-stmt-key))))
9567 (defun c-at-toplevel-p ()
9568 "Return a determination as to whether point is \"at the top level\".
9569 Informally, \"at the top level\" is anywhere where you can write
9570 a function.
9572 More precisely, being at the top-level means that point is either
9573 outside any enclosing block (such as a function definition), or
9574 directly inside a class, namespace or other block that contains
9575 another declaration level.
9577 If point is not at the top-level (e.g. it is inside a method
9578 definition), then nil is returned. Otherwise, if point is at a
9579 top-level not enclosed within a class definition, t is returned.
9580 Otherwise, a 2-vector is returned where the zeroth element is the
9581 buffer position of the start of the class declaration, and the first
9582 element is the buffer position of the enclosing class's opening
9583 brace.
9585 Note that this function might do hidden buffer changes. See the
9586 comment at the start of cc-engine.el for more info."
9587 ;; Note to maintainers: this function consumes a great mass of CPU cycles.
9588 ;; Its use should thus be minimized as far as possible.
9589 ;; Consider instead using `c-bs-at-toplevel-p'.
9590 (let ((paren-state (c-parse-state)))
9591 (or (not (c-most-enclosing-brace paren-state))
9592 (c-search-uplist-for-classkey paren-state))))
9594 (defun c-just-after-func-arglist-p (&optional lim)
9595 ;; Return non-nil if the point is in the region after the argument
9596 ;; list of a function and its opening brace (or semicolon in case it
9597 ;; got no body). If there are K&R style argument declarations in
9598 ;; that region, the point has to be inside the first one for this
9599 ;; function to recognize it.
9601 ;; If successful, the point is moved to the first token after the
9602 ;; function header (see `c-forward-decl-or-cast-1' for details) and
9603 ;; the position of the opening paren of the function arglist is
9604 ;; returned.
9606 ;; The point is clobbered if not successful.
9608 ;; LIM is used as bound for backward buffer searches.
9610 ;; This function might do hidden buffer changes.
9612 (let ((beg (point)) id-start)
9613 (and
9614 (eq (c-beginning-of-statement-1 lim) 'same)
9616 (not (and (c-major-mode-is 'objc-mode)
9617 (c-forward-objc-directive)))
9619 ;; Don't confuse #if .... defined(foo) for a function arglist.
9620 (not (and (looking-at c-cpp-expr-functions-key)
9621 (save-excursion
9622 (save-restriction
9623 (widen)
9624 (c-beginning-of-macro lim)))))
9625 (setq id-start
9626 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) 'top nil)))
9627 (numberp id-start)
9628 (< id-start beg)
9630 ;; There should not be a '=' or ',' between beg and the
9631 ;; start of the declaration since that means we were in the
9632 ;; "expression part" of the declaration.
9633 (or (> (point) beg)
9634 (not (looking-at "[=,]")))
9636 (save-excursion
9637 ;; Check that there's an arglist paren in the
9638 ;; declaration.
9639 (goto-char id-start)
9640 (cond ((eq (char-after) ?\()
9641 ;; The declarator is a paren expression, so skip past it
9642 ;; so that we don't get stuck on that instead of the
9643 ;; function arglist.
9644 (c-forward-sexp))
9645 ((and c-opt-op-identifier-prefix
9646 (looking-at c-opt-op-identifier-prefix))
9647 ;; Don't trip up on "operator ()".
9648 (c-forward-token-2 2 t)))
9649 (and (< (point) beg)
9650 (c-syntactic-re-search-forward "(" beg t t)
9651 (1- (point)))))))
9653 (defun c-in-knr-argdecl (&optional lim)
9654 ;; Return the position of the first argument declaration if point is
9655 ;; inside a K&R style argument declaration list, nil otherwise.
9656 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
9657 ;; position that bounds the backward search for the argument list. This
9658 ;; function doesn't move point.
9660 ;; Point must be within a possible K&R region, e.g. just before a top-level
9661 ;; "{". It must be outside of parens and brackets. The test can return
9662 ;; false positives otherwise.
9664 ;; This function might do hidden buffer changes.
9665 (save-excursion
9666 (save-restriction
9667 ;; If we're in a macro, our search range is restricted to it. Narrow to
9668 ;; the searchable range.
9669 (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
9670 (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
9671 (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
9672 before-lparen after-rparen
9673 (here (point))
9674 (pp-count-out 20) ; Max number of paren/brace constructs before
9675 ; we give up.
9676 ids ; List of identifiers in the parenthesized list.
9677 id-start after-prec-token decl-or-cast decl-res
9678 c-last-identifier-range identifier-ok)
9679 (narrow-to-region low-lim (or macro-end (point-max)))
9681 ;; Search backwards for the defun's argument list. We give up if we
9682 ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
9683 ;; a knr region) or BOB.
9685 ;; The criterion for a paren structure being the arg list is:
9686 ;; o - there is non-WS stuff after it but before any "{"; AND
9687 ;; o - the token after it isn't a ";" AND
9688 ;; o - it is preceded by either an identifier (the function name) or
9689 ;; a macro expansion like "DEFUN (...)"; AND
9690 ;; o - its content is a non-empty comma-separated list of identifiers
9691 ;; (an empty arg list won't have a knr region).
9693 ;; The following snippet illustrates these rules:
9694 ;; int foo (bar, baz, yuk)
9695 ;; int bar [] ;
9696 ;; int (*baz) (my_type) ;
9697 ;; int (*(* yuk) (void)) (void) ;
9698 ;; {
9700 ;; Additionally, for a knr list to be recognized:
9701 ;; o - The identifier of each declarator up to and including the
9702 ;; one "near" point must be contained in the arg list.
9704 (catch 'knr
9705 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
9706 (setq pp-count-out (1- pp-count-out))
9707 (c-syntactic-skip-backward "^)]}=")
9708 (cond ((eq (char-before) ?\))
9709 (setq after-rparen (point)))
9710 ((eq (char-before) ?\])
9711 (setq after-rparen nil))
9712 (t ; either } (hit previous defun) or = or no more
9713 ; parens/brackets.
9714 (throw 'knr nil)))
9716 (if after-rparen
9717 ;; We're inside a paren. Could it be our argument list....?
9719 (and
9720 (progn
9721 (goto-char after-rparen)
9722 (unless (c-go-list-backward) (throw 'knr nil)) ;
9723 ;; FIXME!!! What about macros between the parens? 2007/01/20
9724 (setq before-lparen (point)))
9726 ;; It can't be the arg list if next token is ; or {
9727 (progn (goto-char after-rparen)
9728 (c-forward-syntactic-ws)
9729 (not (memq (char-after) '(?\; ?\{ ?\=))))
9731 ;; Is the thing preceding the list an identifier (the
9732 ;; function name), or a macro expansion?
9733 (progn
9734 (goto-char before-lparen)
9735 (eq (c-backward-token-2) 0)
9736 (or (eq (c-on-identifier) (point))
9737 (and (eq (char-after) ?\))
9738 (c-go-up-list-backward)
9739 (eq (c-backward-token-2) 0)
9740 (eq (c-on-identifier) (point)))))
9742 ;; Have we got a non-empty list of comma-separated
9743 ;; identifiers?
9744 (progn
9745 (goto-char before-lparen)
9746 (and
9747 (c-forward-over-token-and-ws) ; to first token inside parens
9748 (setq id-start (c-on-identifier)) ; Must be at least one.
9749 (catch 'id-list
9750 (while
9751 (progn
9752 (forward-char)
9753 (c-end-of-current-token)
9754 (push (buffer-substring-no-properties id-start
9755 (point))
9756 ids)
9757 (c-forward-syntactic-ws)
9758 (eq (char-after) ?\,))
9759 (c-forward-over-token-and-ws)
9760 (unless (setq id-start (c-on-identifier))
9761 (throw 'id-list nil)))
9762 (eq (char-after) ?\)))))
9764 ;; Are all the identifiers in the k&r list up to the
9765 ;; current one also in the argument list?
9766 (progn
9767 (forward-char) ; over the )
9768 (setq after-prec-token after-rparen)
9769 (c-forward-syntactic-ws)
9770 (while (and
9771 (or (consp (setq decl-or-cast
9772 (c-forward-decl-or-cast-1
9773 after-prec-token
9774 nil ; Or 'arglist ???
9775 nil)))
9776 (progn
9777 (goto-char after-prec-token)
9778 (c-forward-syntactic-ws)
9779 (setq identifier-ok (eq (char-after) ?{))
9780 nil))
9781 (eq (char-after) ?\;)
9782 (setq after-prec-token (1+ (point)))
9783 (goto-char (car decl-or-cast))
9784 (setq decl-res (c-forward-declarator))
9785 (setq identifier-ok
9786 (member (buffer-substring-no-properties
9787 (car decl-res) (cadr decl-res))
9788 ids))
9789 (progn
9790 (goto-char after-prec-token)
9791 (prog1 (< (point) here)
9792 (c-forward-syntactic-ws))))
9793 (setq identifier-ok nil))
9794 identifier-ok))
9795 ;; ...Yes. We've identified the function's argument list.
9796 (throw 'knr
9797 (progn (goto-char after-rparen)
9798 (c-forward-syntactic-ws)
9799 (point)))
9800 ;; ...No. The current parens aren't the function's arg list.
9801 (goto-char before-lparen))
9803 (or (c-go-list-backward) ; backwards over [ .... ]
9804 (throw 'knr nil)))))))))
9806 (defun c-skip-conditional ()
9807 ;; skip forward over conditional at point, including any predicate
9808 ;; statements in parentheses. No error checking is performed.
9810 ;; This function might do hidden buffer changes.
9811 (c-forward-sexp (cond
9812 ;; else if()
9813 ((looking-at (concat "\\<else"
9814 "\\([ \t\n]\\|\\\\\n\\)+"
9815 "if\\>\\([^_]\\|$\\)"))
9817 ;; do, else, try, finally
9818 ((looking-at (concat "\\<\\("
9819 "do\\|else\\|try\\|finally"
9820 "\\)\\>\\([^_]\\|$\\)"))
9822 ;; for, if, while, switch, catch, synchronized, foreach
9823 (t 2))))
9825 (defun c-after-conditional (&optional lim)
9826 ;; If looking at the token after a conditional then return the
9827 ;; position of its start, otherwise return nil.
9829 ;; This function might do hidden buffer changes.
9830 (save-excursion
9831 (and (zerop (c-backward-token-2 1 t lim))
9832 (or (looking-at c-block-stmt-1-key)
9833 (and (eq (char-after) ?\()
9834 (zerop (c-backward-token-2 1 t lim))
9835 (or (looking-at c-block-stmt-2-key)
9836 (looking-at c-block-stmt-1-2-key))))
9837 (point))))
9839 (defun c-after-special-operator-id (&optional lim)
9840 ;; If the point is after an operator identifier that isn't handled
9841 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
9842 ;; position of the start of that identifier is returned. nil is
9843 ;; returned otherwise. The point may be anywhere in the syntactic
9844 ;; whitespace after the last token of the operator identifier.
9846 ;; This function might do hidden buffer changes.
9847 (save-excursion
9848 (and c-overloadable-operators-regexp
9849 (zerop (c-backward-token-2 1 nil lim))
9850 (looking-at c-overloadable-operators-regexp)
9851 (or (not c-opt-op-identifier-prefix)
9852 (and
9853 (zerop (c-backward-token-2 1 nil lim))
9854 (looking-at c-opt-op-identifier-prefix)))
9855 (point))))
9857 (defsubst c-backward-to-block-anchor (&optional lim)
9858 ;; Assuming point is at a brace that opens a statement block of some
9859 ;; kind, move to the proper anchor point for that block. It might
9860 ;; need to be adjusted further by c-add-stmt-syntax, but the
9861 ;; position at return is suitable as start position for that
9862 ;; function.
9864 ;; This function might do hidden buffer changes.
9865 (unless (= (point) (c-point 'boi))
9866 (let ((start (c-after-conditional lim)))
9867 (if start
9868 (goto-char start)))))
9870 (defsubst c-backward-to-decl-anchor (&optional lim)
9871 ;; Assuming point is at a brace that opens the block of a top level
9872 ;; declaration of some kind, move to the proper anchor point for
9873 ;; that block.
9875 ;; This function might do hidden buffer changes.
9876 (unless (= (point) (c-point 'boi))
9877 (c-beginning-of-statement-1 lim)))
9879 (defun c-search-decl-header-end ()
9880 ;; Search forward for the end of the "header" of the current
9881 ;; declaration. That's the position where the definition body
9882 ;; starts, or the first variable initializer, or the ending
9883 ;; semicolon. I.e. search forward for the closest following
9884 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
9885 ;; _after_ the first found token, or at point-max if none is found.
9887 ;; This function might do hidden buffer changes.
9889 (let ((base (point)))
9890 (if (c-major-mode-is 'c++-mode)
9892 ;; In C++ we need to take special care to handle operator
9893 ;; tokens and those pesky template brackets.
9894 (while (and
9895 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
9897 (c-end-of-current-token base)
9898 ;; Handle operator identifiers, i.e. ignore any
9899 ;; operator token preceded by "operator".
9900 (save-excursion
9901 (and (c-safe (c-backward-sexp) t)
9902 (looking-at c-opt-op-identifier-prefix)))
9903 (and (eq (char-before) ?<)
9904 (c-with-syntax-table c++-template-syntax-table
9905 (if (c-safe (goto-char (c-up-list-forward (point))))
9907 (goto-char (point-max))
9908 nil)))))
9909 (setq base (point)))
9911 (while (and
9912 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
9913 (c-end-of-current-token base))
9914 (setq base (point))))))
9916 (defun c-beginning-of-decl-1 (&optional lim)
9917 ;; Go to the beginning of the current declaration, or the beginning
9918 ;; of the previous one if already at the start of it. Point won't
9919 ;; be moved out of any surrounding paren. Return a cons cell of the
9920 ;; form (MOVE . KNR-POS). MOVE is like the return value from
9921 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
9922 ;; style argument declarations (and they are to be recognized) then
9923 ;; KNR-POS is set to the start of the first such argument
9924 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
9925 ;; position that bounds the backward search.
9927 ;; NB: Cases where the declaration continues after the block, as in
9928 ;; "struct foo { ... } bar;", are currently recognized as two
9929 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
9931 ;; This function might do hidden buffer changes.
9932 (catch 'return
9933 (let* ((start (point))
9934 (last-stmt-start (point))
9935 (move (c-beginning-of-statement-1 lim nil t)))
9937 ;; `c-beginning-of-statement-1' stops at a block start, but we
9938 ;; want to continue if the block doesn't begin a top level
9939 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
9940 ;; or an open paren.
9941 (let ((beg (point)) tentative-move)
9942 ;; Go back one "statement" each time round the loop until we're just
9943 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
9944 ;; an ObjC method. This will move over a multiple declaration whose
9945 ;; components are comma separated.
9946 (while (and
9947 ;; Must check with c-opt-method-key in ObjC mode.
9948 (not (and c-opt-method-key
9949 (looking-at c-opt-method-key)))
9950 (/= last-stmt-start (point))
9951 (progn
9952 (c-backward-syntactic-ws lim)
9953 (not (or (memq (char-before) '(?\; ?} ?: nil))
9954 (c-at-vsemi-p))))
9955 (save-excursion
9956 (backward-char)
9957 (not (looking-at "\\s(")))
9958 ;; Check that we don't move from the first thing in a
9959 ;; macro to its header.
9960 (not (eq (setq tentative-move
9961 (c-beginning-of-statement-1 lim nil t))
9962 'macro)))
9963 (setq last-stmt-start beg
9964 beg (point)
9965 move tentative-move))
9966 (goto-char beg))
9968 (when c-recognize-knr-p
9969 (let ((fallback-pos (point)) knr-argdecl-start)
9970 ;; Handle K&R argdecls. Back up after the "statement" jumped
9971 ;; over by `c-beginning-of-statement-1', unless it was the
9972 ;; function body, in which case we're sitting on the opening
9973 ;; brace now. Then test if we're in a K&R argdecl region and
9974 ;; that we started at the other side of the first argdecl in
9975 ;; it.
9976 (unless (eq (char-after) ?{)
9977 (goto-char last-stmt-start))
9978 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
9979 (< knr-argdecl-start start)
9980 (progn
9981 (goto-char knr-argdecl-start)
9982 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
9983 (throw 'return
9984 (cons (if (eq (char-after fallback-pos) ?{)
9985 'previous
9986 'same)
9987 knr-argdecl-start))
9988 (goto-char fallback-pos))))
9990 ;; `c-beginning-of-statement-1' counts each brace block as a separate
9991 ;; statement, so the result will be 'previous if we've moved over any.
9992 ;; So change our result back to 'same if necessary.
9994 ;; If they were brace list initializers we might not have moved over a
9995 ;; declaration boundary though, so change it to 'same if we've moved
9996 ;; past a '=' before '{', but not ';'. (This ought to be integrated
9997 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
9998 ;; potentially can search over a large amount of text.). Take special
9999 ;; pains not to get mislead by C++'s "operator=", and the like.
10000 (if (and (eq move 'previous)
10001 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
10002 c++-template-syntax-table
10003 (syntax-table))
10004 (save-excursion
10005 (and
10006 (progn
10007 (while ; keep going back to "[;={"s until we either find
10008 ; no more, or get to one which isn't an "operator ="
10009 (and (c-syntactic-re-search-forward "[;={]" start t t t)
10010 (eq (char-before) ?=)
10011 c-overloadable-operators-regexp
10012 c-opt-op-identifier-prefix
10013 (save-excursion
10014 (eq (c-backward-token-2) 0)
10015 (looking-at c-overloadable-operators-regexp)
10016 (eq (c-backward-token-2) 0)
10017 (looking-at c-opt-op-identifier-prefix))))
10018 (eq (char-before) ?=))
10019 (c-syntactic-re-search-forward "[;{]" start t t)
10020 (eq (char-before) ?{)
10021 (c-safe (goto-char (c-up-list-forward (point))) t)
10022 (not (c-syntactic-re-search-forward ";" start t t))))))
10023 (cons 'same nil)
10024 (cons move nil)))))
10026 (defun c-end-of-decl-1 ()
10027 ;; Assuming point is at the start of a declaration (as detected by
10028 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
10029 ;; `c-beginning-of-decl-1', this function handles the case when a
10030 ;; block is followed by identifiers in e.g. struct declarations in C
10031 ;; or C++. If a proper end was found then t is returned, otherwise
10032 ;; point is moved as far as possible within the current sexp and nil
10033 ;; is returned. This function doesn't handle macros; use
10034 ;; `c-end-of-macro' instead in those cases.
10036 ;; This function might do hidden buffer changes.
10037 (let ((start (point))
10038 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
10039 c++-template-syntax-table
10040 (syntax-table))))
10041 (catch 'return
10042 (c-search-decl-header-end)
10044 (when (and c-recognize-knr-p
10045 (eq (char-before) ?\;)
10046 (c-in-knr-argdecl start))
10047 ;; Stopped at the ';' in a K&R argdecl section which is
10048 ;; detected using the same criteria as in
10049 ;; `c-beginning-of-decl-1'. Move to the following block
10050 ;; start.
10051 (c-syntactic-re-search-forward "{" nil 'move t))
10053 (when (eq (char-before) ?{)
10054 ;; Encountered a block in the declaration. Jump over it.
10055 (condition-case nil
10056 (goto-char (c-up-list-forward (point)))
10057 (error (goto-char (point-max))
10058 (throw 'return nil)))
10059 (if (or (not c-opt-block-decls-with-vars-key)
10060 (save-excursion
10061 (c-with-syntax-table decl-syntax-table
10062 (let ((lim (point)))
10063 (goto-char start)
10064 (not (and
10065 ;; Check for `c-opt-block-decls-with-vars-key'
10066 ;; before the first paren.
10067 (c-syntactic-re-search-forward
10068 (concat "[;=([{]\\|\\("
10069 c-opt-block-decls-with-vars-key
10070 "\\)")
10071 lim t t t)
10072 (match-beginning 1)
10073 (not (eq (char-before) ?_))
10074 ;; Check that the first following paren is
10075 ;; the block.
10076 (c-syntactic-re-search-forward "[;=([{]"
10077 lim t t t)
10078 (eq (char-before) ?{)))))))
10079 ;; The declaration doesn't have any of the
10080 ;; `c-opt-block-decls-with-vars' keywords in the
10081 ;; beginning, so it ends here at the end of the block.
10082 (throw 'return t)))
10084 (c-with-syntax-table decl-syntax-table
10085 (while (progn
10086 (if (eq (char-before) ?\;)
10087 (throw 'return t))
10088 (c-syntactic-re-search-forward ";" nil 'move t))))
10089 nil)))
10091 (defun c-looking-at-decl-block (_containing-sexp goto-start &optional limit)
10092 ;; Assuming the point is at an open brace, check if it starts a
10093 ;; block that contains another declaration level, i.e. that isn't a
10094 ;; statement block or a brace list, and if so return non-nil.
10096 ;; If the check is successful, the return value is the start of the
10097 ;; keyword that tells what kind of construct it is, i.e. typically
10098 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
10099 ;; the point will be at the start of the construct, before any
10100 ;; leading specifiers, otherwise it's at the returned position.
10102 ;; The point is clobbered if the check is unsuccessful.
10104 ;; CONTAINING-SEXP is the position of the open of the surrounding
10105 ;; paren, or nil if none.
10107 ;; The optional LIMIT limits the backward search for the start of
10108 ;; the construct. It's assumed to be at a syntactically relevant
10109 ;; position.
10111 ;; If any template arglists are found in the searched region before
10112 ;; the open brace, they get marked with paren syntax.
10114 ;; This function might do hidden buffer changes.
10116 (let ((open-brace (point)) kwd-start first-specifier-pos)
10117 (c-syntactic-skip-backward c-block-prefix-charset limit t)
10119 (when (and c-recognize-<>-arglists
10120 (eq (char-before) ?>))
10121 ;; Could be at the end of a template arglist.
10122 (let ((c-parse-and-markup-<>-arglists t))
10123 (while (and
10124 (c-backward-<>-arglist nil limit)
10125 (progn
10126 (c-syntactic-skip-backward c-block-prefix-charset limit t)
10127 (eq (char-before) ?>))))))
10129 ;; Skip back over noise clauses.
10130 (while (and
10131 c-opt-cpp-prefix
10132 (eq (char-before) ?\))
10133 (let ((after-paren (point)))
10134 (if (and (c-go-list-backward)
10135 (progn (c-backward-syntactic-ws)
10136 (c-simple-skip-symbol-backward))
10137 (or (looking-at c-paren-nontype-key)
10138 (looking-at c-noise-macro-with-parens-name-re)))
10139 (progn
10140 (c-syntactic-skip-backward c-block-prefix-charset limit t)
10142 (goto-char after-paren)
10143 nil))))
10145 ;; Note: Can't get bogus hits inside template arglists below since they
10146 ;; have gotten paren syntax above.
10147 (when (and
10148 ;; If `goto-start' is set we begin by searching for the
10149 ;; first possible position of a leading specifier list.
10150 ;; The `c-decl-block-key' search continues from there since
10151 ;; we know it can't match earlier.
10152 (if goto-start
10153 (when (c-syntactic-re-search-forward c-symbol-start
10154 open-brace t t)
10155 (goto-char (setq first-specifier-pos (match-beginning 0)))
10159 (cond
10160 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
10161 (goto-char (setq kwd-start (match-beginning 0)))
10162 (and
10163 ;; Exclude cases where we matched what would ordinarily
10164 ;; be a block declaration keyword, except where it's not
10165 ;; legal because it's part of a "compound keyword" like
10166 ;; "enum class". Of course, if c-after-brace-list-key
10167 ;; is nil, we can skip the test.
10168 (or (equal c-after-brace-list-key "\\<\\>")
10169 (save-match-data
10170 (save-excursion
10171 (not
10172 (and
10173 (looking-at c-after-brace-list-key)
10174 (= (c-backward-token-2 1 t) 0)
10175 (looking-at c-brace-list-key))))))
10177 ;; Found a keyword that can't be a type?
10178 (match-beginning 1)
10180 ;; Can be a type too, in which case it's the return type of a
10181 ;; function (under the assumption that no declaration level
10182 ;; block construct starts with a type).
10183 (not (c-forward-type))
10185 ;; Jumped over a type, but it could be a declaration keyword
10186 ;; followed by the declared identifier that we've jumped over
10187 ;; instead (e.g. in "class Foo {"). If it indeed is a type
10188 ;; then we should be at the declarator now, so check for a
10189 ;; valid declarator start.
10191 ;; Note: This doesn't cope with the case when a declared
10192 ;; identifier is followed by e.g. '(' in a language where '('
10193 ;; also might be part of a declarator expression. Currently
10194 ;; there's no such language.
10195 (not (or (looking-at c-symbol-start)
10196 (looking-at c-type-decl-prefix-key))))))
10198 ;; In Pike a list of modifiers may be followed by a brace
10199 ;; to make them apply to many identifiers. Note that the
10200 ;; match data will be empty on return in this case.
10201 ((and (c-major-mode-is 'pike-mode)
10202 (progn
10203 (goto-char open-brace)
10204 (= (c-backward-token-2) 0))
10205 (looking-at c-specifier-key)
10206 ;; Use this variant to avoid yet another special regexp.
10207 (c-keyword-member (c-keyword-sym (match-string 1))
10208 'c-modifier-kwds))
10209 (setq kwd-start (point))
10210 t)))
10212 ;; Got a match.
10214 (if goto-start
10215 ;; Back up over any preceding specifiers and their clauses
10216 ;; by going forward from `first-specifier-pos', which is the
10217 ;; earliest possible position where the specifier list can
10218 ;; start.
10219 (progn
10220 (goto-char first-specifier-pos)
10222 (while (< (point) kwd-start)
10223 (if (looking-at c-symbol-key)
10224 ;; Accept any plain symbol token on the ground that
10225 ;; it's a specifier masked through a macro (just
10226 ;; like `c-forward-decl-or-cast-1' skip forward over
10227 ;; such tokens).
10229 ;; Could be more restrictive wrt invalid keywords,
10230 ;; but that'd only occur in invalid code so there's
10231 ;; no use spending effort on it.
10232 (let ((end (match-end 0))
10233 (kwd-sym (c-keyword-sym (match-string 0))))
10234 (unless
10235 (and kwd-sym
10236 ;; Moving over a protection kwd and the following
10237 ;; ":" (in C++ Mode) to the next token could take
10238 ;; us all the way up to `kwd-start', leaving us
10239 ;; no chance to update `first-specifier-pos'.
10240 (not (c-keyword-member kwd-sym 'c-protection-kwds))
10241 (c-forward-keyword-clause 0))
10242 (goto-char end)
10243 (c-forward-syntactic-ws)))
10245 ;; Can't parse a declaration preamble and is still
10246 ;; before `kwd-start'. That means `first-specifier-pos'
10247 ;; was in some earlier construct. Search again.
10248 (if (c-syntactic-re-search-forward c-symbol-start
10249 kwd-start 'move t)
10250 (goto-char (setq first-specifier-pos (match-beginning 0)))
10251 ;; Got no preamble before the block declaration keyword.
10252 (setq first-specifier-pos kwd-start))))
10254 (goto-char first-specifier-pos))
10255 (goto-char kwd-start))
10257 kwd-start)))
10259 (defun c-directly-in-class-called-p (name)
10260 ;; Check whether point is directly inside a brace block which is the brace
10261 ;; block of a class, struct, or union which is called NAME, a string.
10262 (let* ((paren-state (c-parse-state))
10263 (brace-pos (c-pull-open-brace paren-state))
10265 (when (eq (char-after brace-pos) ?{)
10266 (goto-char brace-pos)
10267 (save-excursion
10268 ; *c-looking-at-decl-block
10269 ; containing-sexp goto-start &optional
10270 ; limit)
10271 (when (and (c-looking-at-decl-block
10272 (c-pull-open-brace paren-state)
10273 nil)
10274 (looking-at c-class-key))
10275 (goto-char (match-end 1))
10276 (c-forward-syntactic-ws)
10277 (looking-at name))))))
10279 (defun c-search-uplist-for-classkey (paren-state)
10280 ;; Check if the closest containing paren sexp is a declaration
10281 ;; block, returning a 2 element vector in that case. Aref 0
10282 ;; contains the bufpos at boi of the class key line, and aref 1
10283 ;; contains the bufpos of the open brace. This function is an
10284 ;; obsolete wrapper for `c-looking-at-decl-block'.
10286 ;; This function might do hidden buffer changes.
10287 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
10288 (when open-paren-pos
10289 (save-excursion
10290 (goto-char open-paren-pos)
10291 (when (and (eq (char-after) ?{)
10292 (c-looking-at-decl-block
10293 (c-safe-position open-paren-pos paren-state)
10294 nil))
10295 (back-to-indentation)
10296 (vector (point) open-paren-pos))))))
10298 (defun c-most-enclosing-decl-block (paren-state)
10299 ;; Return the buffer position of the most enclosing decl-block brace (in the
10300 ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
10301 ;; none was found.
10302 (let* ((open-brace (c-pull-open-brace paren-state))
10303 (next-open-brace (c-pull-open-brace paren-state)))
10304 (while (and open-brace
10305 (save-excursion
10306 (goto-char open-brace)
10307 (not (c-looking-at-decl-block next-open-brace nil))))
10308 (setq open-brace next-open-brace
10309 next-open-brace (c-pull-open-brace paren-state)))
10310 open-brace))
10312 (defun c-cheap-inside-bracelist-p (paren-state)
10313 ;; Return the position of the L-brace if point is inside a brace list
10314 ;; initialization of an array, etc. This is an approximate function,
10315 ;; designed for speed over accuracy. It will not find every bracelist, but
10316 ;; a non-nil result is reliable. We simply search for "= {" (naturally with
10317 ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
10318 ;; is everywhere else.
10319 (let (b-pos)
10320 (save-excursion
10321 (while
10322 (and (setq b-pos (c-pull-open-brace paren-state))
10323 (progn (goto-char b-pos)
10324 (c-backward-sws)
10325 (c-backward-token-2)
10326 (not (looking-at "=")))))
10327 b-pos)))
10329 (defun c-backward-typed-enum-colon ()
10330 ;; We're at a "{" which might be the opening brace of an enum which is
10331 ;; strongly typed (by a ":" followed by a type). If this is the case, leave
10332 ;; point before the colon and return t. Otherwise leave point unchanged and return nil.
10333 ;; Match data will be clobbered.
10334 (let ((here (point))
10335 (colon-pos nil))
10336 (save-excursion
10337 (while
10338 (and (eql (c-backward-token-2) 0)
10339 (or (not (looking-at "\\s)"))
10340 (c-go-up-list-backward))
10341 (cond
10342 ((looking-at "::")
10344 ((and (eql (char-after) ?:)
10345 (save-excursion
10346 (c-backward-syntactic-ws)
10347 (or (c-on-identifier)
10348 (progn
10349 (c-backward-token-2)
10350 (looking-at c-brace-list-key)))))
10351 (setq colon-pos (point))
10352 (forward-char)
10353 (c-forward-syntactic-ws)
10354 (or (and (c-forward-type)
10355 (progn (c-forward-syntactic-ws)
10356 (eq (point) here)))
10357 (setq colon-pos nil))
10358 nil)
10359 ((eql (char-after) ?\()
10361 ((looking-at c-symbol-key)
10363 (t nil)))))
10364 (when colon-pos
10365 (goto-char colon-pos)
10366 t)))
10368 (defun c-backward-over-enum-header ()
10369 ;; We're at a "{". Move back to the enum-like keyword that starts this
10370 ;; declaration and return t, otherwise don't move and return nil.
10371 (let ((here (point))
10372 before-identifier)
10373 (when c-recognize-post-brace-list-type-p
10374 (c-backward-typed-enum-colon))
10375 (while
10376 (and
10377 (eq (c-backward-token-2) 0)
10378 (or (not (looking-at "\\s)"))
10379 (c-go-up-list-backward))
10380 (cond
10381 ((and (looking-at c-symbol-key) (c-on-identifier)
10382 (not before-identifier))
10383 (setq before-identifier t))
10384 ((and before-identifier
10385 (or (eql (char-after) ?,)
10386 (looking-at c-postfix-decl-spec-key)))
10387 (setq before-identifier nil)
10389 ((looking-at c-after-brace-list-key) t)
10390 ((looking-at c-brace-list-key) nil)
10391 ((eq (char-after) ?\()
10392 (and (eq (c-backward-token-2) 0)
10393 (or (looking-at c-decl-hangon-key)
10394 (and c-opt-cpp-prefix
10395 (looking-at c-noise-macro-with-parens-name-re)))))
10397 ((and c-recognize-<>-arglists
10398 (eq (char-after) ?<)
10399 (looking-at "\\s("))
10401 (t nil))))
10402 (or (looking-at c-brace-list-key)
10403 (progn (goto-char here) nil))))
10405 (defun c-looking-at-or-maybe-in-bracelist (&optional containing-sexp lim)
10406 ;; Point is at an open brace. If this starts a brace list, return a list
10407 ;; whose car is the buffer position of the start of the construct which
10408 ;; introduces the list, and whose cdr is the symbol `in-paren' if the brace
10409 ;; is directly enclosed in a parenthesis form (i.e. an arglist), t if we
10410 ;; have parsed a keyword matching `c-opt-inexpr-brace-list-key' (e.g. Java's
10411 ;; "new"), nil otherwise. Otherwise, if point might be inside an enclosing
10412 ;; brace list, return t. If point is definitely neither at nor in a brace
10413 ;; list, return nil.
10415 ;; CONTAINING-SEXP is the position of the brace/paren/bracket enclosing
10416 ;; POINT, or nil if there is no such position, or we do not know it. LIM is
10417 ;; a backward search limit.
10419 ;; The determination of whether the brace starts a brace list is solely by
10420 ;; the context of the brace, not by its contents.
10422 ;; Here, "brace list" does not include the body of an enum.
10423 (save-excursion
10424 (let ((start (point))
10425 (class-key
10426 ;; Pike can have class definitions anywhere, so we must
10427 ;; check for the class key here.
10428 (and (c-major-mode-is 'pike-mode)
10429 c-decl-block-key))
10430 (braceassignp 'dontknow)
10431 inexpr-brace-list bufpos macro-start res pos after-type-id-pos
10432 in-paren parens-before-brace)
10434 (setq res (c-backward-token-2 1 t lim))
10435 ;; Checks to do only on the first sexp before the brace.
10436 ;; Have we a C++ initialization, without an "="?
10437 (if (and (c-major-mode-is 'c++-mode)
10438 (cond
10439 ((and (or (not (eq res 0))
10440 (eq (char-after) ?,))
10441 (c-go-up-list-backward nil lim) ; FIXME!!! Check ; `lim' 2016-07-12.
10442 (eq (char-after) ?\())
10443 (setq braceassignp 'c++-noassign
10444 in-paren 'in-paren))
10445 ((looking-at c-pre-id-bracelist-key))
10446 ((looking-at c-return-key))
10447 ((and (looking-at c-symbol-start)
10448 (not (looking-at c-keywords-regexp)))
10449 (setq after-type-id-pos (point)))
10450 ((eq (char-after) ?\()
10451 (setq parens-before-brace t)
10452 nil)
10453 (t nil))
10454 (save-excursion
10455 (cond
10456 ((or (not (eq res 0))
10457 (eq (char-after) ?,))
10458 (and (c-go-up-list-backward nil lim) ; FIXME!!! Check `lim' 2016-07-12.
10459 (eq (char-after) ?\()
10460 (setq in-paren 'in-paren)))
10461 ((looking-at c-pre-id-bracelist-key))
10462 ((looking-at c-return-key))
10463 (t (setq after-type-id-pos (point))
10464 nil))))
10465 (setq braceassignp 'c++-noassign))
10467 (when (and c-opt-inexpr-brace-list-key
10468 (eq (char-after) ?\[))
10469 ;; In Java, an initialization brace list may follow
10470 ;; directly after "new Foo[]", so check for a "new"
10471 ;; earlier.
10472 (while (eq braceassignp 'dontknow)
10473 (setq braceassignp
10474 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
10475 ((looking-at c-opt-inexpr-brace-list-key)
10476 (setq inexpr-brace-list t)
10478 ((looking-at "\\sw\\|\\s_\\|[.[]")
10479 ;; Carry on looking if this is an
10480 ;; identifier (may contain "." in Java)
10481 ;; or another "[]" sexp.
10482 'dontknow)
10483 (t nil)))))
10485 (setq pos (point))
10486 (cond
10487 ((and after-type-id-pos
10488 (goto-char after-type-id-pos)
10489 (setq res (c-back-over-member-initializers))
10490 (goto-char res)
10491 (eq (car (c-beginning-of-decl-1 lim)) 'same))
10492 (cons (point) nil)) ; Return value.
10494 ((and after-type-id-pos
10495 (progn
10496 (c-backward-syntactic-ws)
10497 (eq (char-before) ?\()))
10498 ;; Single identifier between '(' and '{'. We have a bracelist.
10499 (cons after-type-id-pos 'in-paren))
10501 ;; Are we at the parens of a C++ lambda expression?
10502 ((and parens-before-brace
10503 (save-excursion
10504 (and
10505 (zerop (c-backward-token-2 1 t lim))
10506 (c-looking-at-c++-lambda-capture-list))))
10507 nil) ; a lambda expression isn't a brace list.
10510 (goto-char pos)
10511 ;; Checks to do on all sexps before the brace, up to the
10512 ;; beginning of the statement.
10513 (while (eq braceassignp 'dontknow)
10514 (cond ((eq (char-after) ?\;)
10515 (setq braceassignp nil))
10516 ((and class-key
10517 (looking-at class-key))
10518 (setq braceassignp nil))
10519 ((eq (char-after) ?=)
10520 ;; We've seen a =, but must check earlier tokens so
10521 ;; that it isn't something that should be ignored.
10522 (setq braceassignp 'maybe)
10523 (while (and (eq braceassignp 'maybe)
10524 (zerop (c-backward-token-2 1 t lim)))
10525 (setq braceassignp
10526 (cond
10527 ;; Check for operator =
10528 ((and c-opt-op-identifier-prefix
10529 (looking-at c-opt-op-identifier-prefix))
10530 nil)
10531 ;; Check for `<opchar>= in Pike.
10532 ((and (c-major-mode-is 'pike-mode)
10533 (or (eq (char-after) ?`)
10534 ;; Special case for Pikes
10535 ;; `[]=, since '[' is not in
10536 ;; the punctuation class.
10537 (and (eq (char-after) ?\[)
10538 (eq (char-before) ?`))))
10539 nil)
10540 ((looking-at "\\s.") 'maybe)
10541 ;; make sure we're not in a C++ template
10542 ;; argument assignment
10543 ((and
10544 (c-major-mode-is 'c++-mode)
10545 (save-excursion
10546 (let ((here (point))
10547 (pos< (progn
10548 (skip-chars-backward "^<>")
10549 (point))))
10550 (and (eq (char-before) ?<)
10551 (not (c-crosses-statement-barrier-p
10552 pos< here))
10553 (not (c-in-literal))
10554 ))))
10555 nil)
10556 (t t))))))
10557 (if (and (eq braceassignp 'dontknow)
10558 (/= (c-backward-token-2 1 t lim) 0))
10559 (setq braceassignp nil)))
10561 (cond
10562 (braceassignp
10563 ;; We've hit the beginning of the aggregate list.
10564 (c-beginning-of-statement-1 containing-sexp)
10565 (cons (point) (or in-paren inexpr-brace-list)))
10566 ((and after-type-id-pos
10567 (save-excursion
10568 (when (eq (char-after) ?\;)
10569 (c-forward-over-token-and-ws t))
10570 (setq bufpos (point))
10571 (when (looking-at c-opt-<>-sexp-key)
10572 (c-forward-over-token-and-ws)
10573 (when (and (eq (char-after) ?<)
10574 (c-get-char-property (point) 'syntax-table))
10575 (c-go-list-forward nil after-type-id-pos)
10576 (c-forward-syntactic-ws)))
10577 (and
10578 (or (not (looking-at c-class-key))
10579 (save-excursion
10580 (goto-char (match-end 1))
10581 (c-forward-syntactic-ws)
10582 (not (eq (point) after-type-id-pos))))
10583 (progn
10584 (setq res
10585 (c-forward-decl-or-cast-1
10586 (save-excursion (c-backward-syntactic-ws) (point))
10587 nil nil))
10588 (and (consp res)
10589 (eq (car res) after-type-id-pos))))))
10590 (cons bufpos (or in-paren inexpr-brace-list)))
10591 ((eq (char-after) ?\;)
10592 ;; Brace lists can't contain a semicolon, so we're done.
10593 ;; (setq containing-sexp nil)
10594 nil)
10595 ((and (setq macro-start (point))
10596 (c-forward-to-cpp-define-body)
10597 (eq (point) start))
10598 ;; We've a macro whose expansion starts with the '{'.
10599 ;; Heuristically, if we have a ';' in it we've not got a
10600 ;; brace list, otherwise we have.
10601 (let ((macro-end (progn (c-end-of-macro) (point))))
10602 (goto-char start)
10603 (forward-char)
10604 (if (and (c-syntactic-re-search-forward "[;,]" macro-end t t)
10605 (eq (char-before) ?\;))
10607 (cons macro-start nil)))) ; (2016-08-30): Lazy! We have no
10608 ; languages where
10609 ; `c-opt-inexpr-brace-list-key' is
10610 ; non-nil and we have macros.
10611 (t t)))) ;; The caller can go up one level.
10614 (defun c-inside-bracelist-p (containing-sexp paren-state accept-in-paren)
10615 ;; return the buffer position of the beginning of the brace list
10616 ;; statement if we're inside a brace list, otherwise return nil.
10617 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
10618 ;; paren. PAREN-STATE is the remainder of the state of enclosing
10619 ;; braces. ACCEPT-IN-PAREN is non-nil iff we will accept as a brace
10620 ;; list a brace directly enclosed in a parenthesis.
10622 ;; The "brace list" here is recognized solely by its context, not by
10623 ;; its contents.
10625 ;; N.B.: This algorithm can potentially get confused by cpp macros
10626 ;; placed in inconvenient locations. It's a trade-off we make for
10627 ;; speed.
10629 ;; This function might do hidden buffer changes.
10631 ;; This will pick up brace list declarations.
10632 (save-excursion
10633 (goto-char containing-sexp)
10634 (c-backward-over-enum-header))
10635 ;; this will pick up array/aggregate init lists, even if they are nested.
10636 (save-excursion
10637 (let ((bufpos t)
10638 next-containing)
10639 (while (and (eq bufpos t)
10640 containing-sexp)
10641 (when paren-state
10642 (setq next-containing (c-pull-open-brace paren-state)))
10644 (goto-char containing-sexp)
10645 (if (c-looking-at-inexpr-block next-containing next-containing)
10646 ;; We're in an in-expression block of some kind. Do not
10647 ;; check nesting. We deliberately set the limit to the
10648 ;; containing sexp, so that c-looking-at-inexpr-block
10649 ;; doesn't check for an identifier before it.
10650 (setq bufpos nil)
10651 (if (not (eq (char-after) ?{))
10652 (setq bufpos nil)
10653 (when (eq (setq bufpos (c-looking-at-or-maybe-in-bracelist
10654 next-containing next-containing))
10656 (setq containing-sexp next-containing
10657 next-containing nil)))))
10658 (and (consp bufpos)
10659 (or accept-in-paren (not (eq (cdr bufpos) 'in-paren)))
10660 (car bufpos))))))
10662 (defun c-looking-at-special-brace-list (&optional _lim)
10663 ;; If we're looking at the start of a pike-style list, i.e., `({ })',
10664 ;; `([ ])', `(< >)', etc., a cons of a cons of its starting and ending
10665 ;; positions and its entry in c-special-brace-lists is returned, nil
10666 ;; otherwise. The ending position is nil if the list is still open.
10667 ;; LIM is the limit for forward search. The point may either be at
10668 ;; the `(' or at the following paren character. Tries to check the
10669 ;; matching closer, but assumes it's correct if no balanced paren is
10670 ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
10671 ;; a special brace list).
10673 ;; This function might do hidden buffer changes.
10674 (if c-special-brace-lists
10675 (condition-case ()
10676 (save-excursion
10677 (let ((beg (point))
10678 inner-beg end type)
10679 (c-forward-syntactic-ws)
10680 (if (eq (char-after) ?\()
10681 (progn
10682 (forward-char 1)
10683 (c-forward-syntactic-ws)
10684 (setq inner-beg (point))
10685 (setq type (assq (char-after) c-special-brace-lists)))
10686 (if (setq type (assq (char-after) c-special-brace-lists))
10687 (progn
10688 (setq inner-beg (point))
10689 (c-backward-syntactic-ws)
10690 (forward-char -1)
10691 (setq beg (if (eq (char-after) ?\()
10692 (point)
10693 nil)))))
10694 (if (and beg type)
10695 (if (and (c-safe
10696 (goto-char beg)
10697 (c-forward-sexp 1)
10698 (setq end (point))
10699 (= (char-before) ?\)))
10700 (c-safe
10701 (goto-char inner-beg)
10702 (if (looking-at "\\s(")
10703 ;; Check balancing of the inner paren
10704 ;; below.
10705 (progn
10706 (c-forward-sexp 1)
10708 ;; If the inner char isn't a paren then
10709 ;; we can't check balancing, so just
10710 ;; check the char before the outer
10711 ;; closing paren.
10712 (goto-char end)
10713 (backward-char)
10714 (c-backward-syntactic-ws)
10715 (= (char-before) (cdr type)))))
10716 (if (or (/= (char-syntax (char-before)) ?\))
10717 (= (progn
10718 (c-forward-syntactic-ws)
10719 (point))
10720 (1- end)))
10721 (cons (cons beg end) type))
10722 (cons (list beg) type)))))
10723 (error nil))))
10725 (defun c-looking-at-bos (&optional _lim)
10726 ;; Return non-nil if between two statements or declarations, assuming
10727 ;; point is not inside a literal or comment.
10729 ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
10730 ;; are recommended instead.
10732 ;; This function might do hidden buffer changes.
10733 (c-at-statement-start-p))
10734 (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
10736 (defun c-looking-at-statement-block ()
10737 ;; Point is at an opening brace. If this is a statement block (i.e. the
10738 ;; elements in the block are terminated by semicolons, or the block is
10739 ;; empty, or the block contains a keyword) return t. Otherwise, return nil.
10740 (let ((here (point)))
10741 (prog1
10742 (if (c-go-list-forward)
10743 (let ((there (point)))
10744 (backward-char)
10745 (c-syntactic-skip-backward "^;," here t)
10746 (cond
10747 ((eq (char-before) ?\;) t)
10748 ((eq (char-before) ?,) nil)
10749 (t ; We're at (1+ here).
10750 (cond
10751 ((progn (c-forward-syntactic-ws)
10752 (eq (point) (1- there))))
10753 ((c-syntactic-re-search-forward c-keywords-regexp there t))
10754 ((c-syntactic-re-search-forward "{" there t t)
10755 (backward-char)
10756 (c-looking-at-statement-block))
10757 (t nil)))))
10758 (forward-char)
10759 (cond
10760 ((c-syntactic-re-search-forward "[;,]" nil t t)
10761 (eq (char-before) ?\;))
10762 ((progn (c-forward-syntactic-ws)
10763 (eobp)))
10764 ((c-syntactic-re-search-forward c-keywords-regexp nil t t))
10765 ((c-syntactic-re-search-forward "{" nil t t)
10766 (backward-char)
10767 (c-looking-at-statement-block))
10768 (t nil)))
10769 (goto-char here))))
10771 (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
10772 ;; Return non-nil if we're looking at the beginning of a block
10773 ;; inside an expression. The value returned is actually a cons of
10774 ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
10775 ;; position of the beginning of the construct.
10777 ;; LIM limits the backward search. CONTAINING-SEXP is the start
10778 ;; position of the closest containing list. If it's nil, the
10779 ;; containing paren isn't used to decide whether we're inside an
10780 ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
10781 ;; needs to be farther back.
10783 ;; If CHECK-AT-END is non-nil then extra checks at the end of the
10784 ;; brace block might be done. It should only be used when the
10785 ;; construct can be assumed to be complete, i.e. when the original
10786 ;; starting position was further down than that.
10788 ;; This function might do hidden buffer changes.
10790 (save-excursion
10791 (let ((res 'maybe) (passed-bracket-pairs 0) bracket-pos passed-paren
10792 haskell-op-pos
10793 (closest-lim (or containing-sexp lim (point-min)))
10794 ;; Look at the character after point only as a last resort
10795 ;; when we can't disambiguate.
10796 (block-follows (and (eq (char-after) ?{) (point))))
10798 ;; Search for a C++11 "->" which suggests a lambda declaration.
10799 (when (and (c-major-mode-is 'c++-mode)
10800 (setq haskell-op-pos
10801 (save-excursion
10802 (while
10803 (progn
10804 (c-syntactic-skip-backward "^;=}>" closest-lim t)
10805 (and (eq (char-before) ?>)
10806 (c-backward-token-2)
10807 (not (looking-at c-haskell-op-re)))))
10808 (and (looking-at c-haskell-op-re)
10809 (point)))))
10810 (goto-char haskell-op-pos))
10812 (while (and (eq res 'maybe)
10813 (progn (c-backward-syntactic-ws)
10814 (> (point) closest-lim))
10815 (not (bobp))
10816 (progn (backward-char)
10817 (looking-at "[]).]\\|\\w\\|\\s_"))
10818 (c-safe (forward-char)
10819 (goto-char (scan-sexps (point) -1))))
10821 (setq res
10822 (if (looking-at c-keywords-regexp)
10823 (let ((kw-sym (c-keyword-sym (match-string 1))))
10824 (cond
10825 ((and block-follows
10826 (c-keyword-member kw-sym 'c-inexpr-class-kwds))
10827 (and (not (eq passed-paren ?\[))
10828 (or (not (looking-at c-class-key))
10829 ;; If the class definition is at the start of
10830 ;; a statement, we don't consider it an
10831 ;; in-expression class.
10832 (let ((prev (point)))
10833 (while (and
10834 (= (c-backward-token-2 1 nil closest-lim) 0)
10835 (eq (char-syntax (char-after)) ?w))
10836 (setq prev (point)))
10837 (goto-char prev)
10838 (not (c-at-statement-start-p)))
10839 ;; Also, in Pike we treat it as an
10840 ;; in-expression class if it's used in an
10841 ;; object clone expression.
10842 (save-excursion
10843 (and check-at-end
10844 (c-major-mode-is 'pike-mode)
10845 (progn (goto-char block-follows)
10846 (zerop (c-forward-token-2 1 t)))
10847 (eq (char-after) ?\())))
10848 (cons 'inexpr-class (point))))
10849 ((c-keyword-member kw-sym 'c-paren-any-kwds) ; e.g. C++11 "throw" or "noexcept"
10850 (setq passed-paren nil)
10851 (setq passed-bracket-pairs 0)
10852 (setq bracket-pos nil)
10853 'maybe)
10854 ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
10855 (when (not passed-paren)
10856 (cons 'inexpr-statement (point))))
10857 ((c-keyword-member kw-sym 'c-lambda-kwds)
10858 (when (or (not passed-paren)
10859 (eq passed-paren ?\())
10860 (cons 'inlambda (point))))
10861 ((c-keyword-member kw-sym 'c-block-stmt-kwds)
10862 nil)
10864 'maybe)))
10866 (if (looking-at "\\s(")
10867 (if passed-paren
10868 (cond
10869 ((and (eq passed-paren ?\[)
10870 (eq (char-after) ?\[)
10871 (not (eq (char-after (1+ (point))) ?\[))) ; C++ attribute.
10872 ;; Accept several square bracket sexps for
10873 ;; Java array initializations.
10874 (setq passed-bracket-pairs (1+ passed-bracket-pairs))
10875 'maybe)
10876 ((and (eq passed-paren ?\()
10877 (eq (char-after) ?\[)
10878 (not (eq (char-after (1+ (point))) ?\[))
10879 (eq passed-bracket-pairs 0))
10880 ;; C++11 lambda function declaration
10881 (setq passed-bracket-pairs 1)
10882 (setq bracket-pos (point))
10883 'maybe)
10884 (t nil))
10885 (when (not (looking-at "\\[\\["))
10886 (setq passed-paren (char-after))
10887 (when (eq passed-paren ?\[)
10888 (setq passed-bracket-pairs 1)
10889 (setq bracket-pos (point))))
10890 'maybe)
10891 'maybe))))
10893 (if (eq res 'maybe)
10894 (cond
10895 ((and (c-major-mode-is 'c++-mode)
10896 block-follows
10897 (eq passed-bracket-pairs 1)
10898 (save-excursion
10899 (goto-char bracket-pos)
10900 (or (<= (point) (or lim (point-min)))
10901 (progn
10902 (c-backward-token-2 1 nil lim)
10903 (and
10904 (not (and (c-on-identifier)
10905 (looking-at c-symbol-chars)))
10906 (not (looking-at c-opt-op-identifier-prefix)))))))
10907 (cons 'inlambda bracket-pos))
10908 ((and c-recognize-paren-inexpr-blocks
10909 block-follows
10910 containing-sexp
10911 (eq (char-after containing-sexp) ?\())
10912 (goto-char containing-sexp)
10913 (if (or (save-excursion
10914 (c-backward-syntactic-ws lim)
10915 (while (and (eq (char-before) ?>)
10916 (c-get-char-property (1- (point))
10917 'syntax-table)
10918 (c-go-list-backward nil lim))
10919 (c-backward-syntactic-ws lim))
10920 (and (> (point) (or lim (point-min)))
10921 (c-on-identifier)))
10922 (and c-special-brace-lists
10923 (c-looking-at-special-brace-list))
10924 (and (c-major-mode-is 'c++-mode)
10925 (save-excursion
10926 (goto-char block-follows)
10927 (not (c-looking-at-statement-block)))))
10929 (cons 'inexpr-statement (point)))))
10931 res))))
10933 (defun c-looking-at-inexpr-block-backward (paren-state)
10934 ;; Returns non-nil if we're looking at the end of an in-expression
10935 ;; block, otherwise the same as `c-looking-at-inexpr-block'.
10936 ;; PAREN-STATE is the paren state relevant at the current position.
10938 ;; This function might do hidden buffer changes.
10939 (save-excursion
10940 ;; We currently only recognize a block.
10941 (let ((here (point))
10942 (elem (car-safe paren-state))
10943 containing-sexp)
10944 (when (and (consp elem)
10945 (progn (goto-char (cdr elem))
10946 (c-forward-syntactic-ws here)
10947 (= (point) here)))
10948 (goto-char (car elem))
10949 (if (setq paren-state (cdr paren-state))
10950 (setq containing-sexp (car-safe paren-state)))
10951 (c-looking-at-inexpr-block (c-safe-position containing-sexp
10952 paren-state)
10953 containing-sexp)))))
10955 (defun c-looking-at-c++-lambda-capture-list ()
10956 ;; Return non-nil if we're at the opening "[" of the capture list of a C++
10957 ;; lambda function, nil otherwise.
10958 (and
10959 (eq (char-after) ?\[)
10960 (not (eq (char-before) ?\[))
10961 (not (eq (char-after (1+ (point))) ?\[))
10962 (save-excursion
10963 (or (eq (c-backward-token-2 1) 1)
10964 (looking-at c-pre-lambda-tokens-re)))
10965 (not (c-in-literal))))
10967 (defun c-at-macro-vsemi-p (&optional pos)
10968 ;; Is there a "virtual semicolon" at POS or point?
10969 ;; (See cc-defs.el for full details of "virtual semicolons".)
10971 ;; This is true when point is at the last non syntactic WS position on the
10972 ;; line, there is a macro call last on the line, and this particular macro's
10973 ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
10974 ;; semicolon.
10975 (save-excursion
10976 (save-restriction
10977 (widen)
10978 (if pos
10979 (goto-char pos)
10980 (setq pos (point)))
10981 (and
10982 c-macro-with-semi-re
10983 (eq (skip-chars-backward " \t") 0)
10985 ;; Check we've got nothing after this except comments and empty lines
10986 ;; joined by escaped EOLs.
10987 (skip-chars-forward " \t") ; always returns non-nil.
10988 (progn
10989 (while ; go over 1 block comment per iteration.
10990 (and
10991 (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
10992 (goto-char (match-end 0))
10993 (cond
10994 ((looking-at c-block-comment-start-regexp)
10995 (and (forward-comment 1)
10996 (skip-chars-forward " \t"))) ; always returns non-nil
10997 ((looking-at c-line-comment-start-regexp)
10998 (end-of-line)
10999 nil)
11000 (t nil))))
11001 (eolp))
11003 (goto-char pos)
11004 (progn (c-backward-syntactic-ws)
11005 (eq (point) pos))
11007 ;; Check for one of the listed macros being before point.
11008 (or (not (eq (char-before) ?\)))
11009 (when (c-go-list-backward)
11010 (c-backward-syntactic-ws)
11012 (c-simple-skip-symbol-backward)
11013 (looking-at c-macro-with-semi-re)
11014 (goto-char pos)
11015 (not (c-in-literal)))))) ; The most expensive check last.
11017 (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
11020 ;; `c-guess-basic-syntax' and the functions that precedes it below
11021 ;; implements the main decision tree for determining the syntactic
11022 ;; analysis of the current line of code.
11024 ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
11025 ;; auto newline analysis.
11026 (defvar c-auto-newline-analysis nil)
11028 (defun c-brace-anchor-point (bracepos)
11029 ;; BRACEPOS is the position of a brace in a construct like "namespace
11030 ;; Bar {". Return the anchor point in this construct; this is the
11031 ;; earliest symbol on the brace's line which isn't earlier than
11032 ;; "namespace".
11034 ;; Currently (2007-08-17), "like namespace" means "matches
11035 ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
11036 ;; or anything like that.
11037 (save-excursion
11038 (let ((boi (c-point 'boi bracepos)))
11039 (goto-char bracepos)
11040 (while (and (> (point) boi)
11041 (not (looking-at c-other-decl-block-key)))
11042 (c-backward-token-2))
11043 (if (> (point) boi) (point) boi))))
11045 (defsubst c-add-syntax (symbol &rest args)
11046 ;; A simple function to prepend a new syntax element to
11047 ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
11048 ;; should always be dynamically bound but since we read it first
11049 ;; we'll fail properly anyway if this function is misused.
11050 (setq c-syntactic-context (cons (cons symbol args)
11051 c-syntactic-context)))
11053 (defsubst c-append-syntax (symbol &rest args)
11054 ;; Like `c-add-syntax' but appends to the end of the syntax list.
11055 ;; (Normally not necessary.)
11056 (setq c-syntactic-context (nconc c-syntactic-context
11057 (list (cons symbol args)))))
11059 (defun c-add-stmt-syntax (syntax-symbol
11060 syntax-extra-args
11061 stop-at-boi-only
11062 containing-sexp
11063 paren-state
11064 &optional fixed-anchor)
11065 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
11066 ;; needed with further syntax elements of the types `substatement',
11067 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro',
11068 ;; `defun-block-intro', and `brace-list-intro'.
11070 ;; Do the generic processing to anchor the given syntax symbol on the
11071 ;; preceding statement: First skip over any labels and containing statements
11072 ;; on the same line. If FIXED-ANCHOR is non-nil, use this as the
11073 ;; anchor-point for the given syntactic symbol, and don't make syntactic
11074 ;; entries for constructs beginning on lines before that containing
11075 ;; ANCHOR-POINT. Otherwise search backward until we find a statement or
11076 ;; block start that begins at boi without a label or comment.
11078 ;; Point is assumed to be at the prospective anchor point for the
11079 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
11080 ;; skip past open parens and containing statements. Most of the added
11081 ;; syntax elements will get the same anchor point - the exception is
11082 ;; for an anchor in a construct like "namespace"[*] - this is as early
11083 ;; as possible in the construct but on the same line as the {.
11085 ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
11087 ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
11088 ;; syntax symbol. They are appended after the anchor point.
11090 ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
11091 ;; if the current statement starts there.
11093 ;; Note: It's not a problem if PAREN-STATE "overshoots"
11094 ;; CONTAINING-SEXP, i.e. contains info about parens further down.
11096 ;; This function might do hidden buffer changes.
11098 (if (= (point) (c-point 'boi))
11099 ;; This is by far the most common case, so let's give it special
11100 ;; treatment.
11101 (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
11103 (let ((syntax-last c-syntactic-context)
11104 (boi (c-point 'boi))
11105 (anchor-boi (c-point 'boi))
11106 ;; Set when we're on a label, so that we don't stop there.
11107 ;; FIXME: To be complete we should check if we're on a label
11108 ;; now at the start.
11109 on-label)
11111 ;; Use point as the anchor point for "namespace", "extern", etc.
11112 (apply 'c-add-syntax syntax-symbol
11113 (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
11114 (point) nil)
11115 syntax-extra-args)
11117 ;; Loop while we have to back out of containing blocks.
11118 (while
11119 (and
11120 (catch 'back-up-block
11122 ;; Loop while we have to back up statements.
11123 (while (or (/= (point) boi)
11124 on-label
11125 (looking-at c-comment-start-regexp))
11127 ;; Skip past any comments that stands between the
11128 ;; statement start and boi.
11129 (let ((savepos (point)))
11130 (while (and (/= savepos boi)
11131 (c-backward-single-comment))
11132 (setq savepos (point)
11133 boi (c-point 'boi)))
11134 (goto-char savepos))
11136 ;; Skip to the beginning of this statement or backward
11137 ;; another one.
11138 (let ((old-pos (point))
11139 (old-boi boi)
11140 (step-type (c-beginning-of-statement-1 containing-sexp)))
11141 (setq boi (c-point 'boi)
11142 on-label (eq step-type 'label))
11144 (cond ((= (point) old-pos)
11145 ;; If we didn't move we're at the start of a block and
11146 ;; have to continue outside it.
11147 (throw 'back-up-block t))
11149 ((and (eq step-type 'up)
11150 (>= (point) old-boi)
11151 (looking-at "else\\>[^_]")
11152 (save-excursion
11153 (goto-char old-pos)
11154 (looking-at "if\\>[^_]")))
11155 ;; Special case to avoid deeper and deeper indentation
11156 ;; of "else if" clauses.
11159 ((and (not stop-at-boi-only)
11160 (/= old-pos old-boi)
11161 (memq step-type '(up previous)))
11162 ;; If stop-at-boi-only is nil, we shouldn't back up
11163 ;; over previous or containing statements to try to
11164 ;; reach boi, so go back to the last position and
11165 ;; exit.
11166 (goto-char old-pos)
11167 (throw 'back-up-block nil))
11170 (if (and (not stop-at-boi-only)
11171 (memq step-type '(up previous beginning)))
11172 ;; If we've moved into another statement then we
11173 ;; should no longer try to stop in the middle of a
11174 ;; line.
11175 (setq stop-at-boi-only t))
11177 ;; Record this as a substatement if we skipped up one
11178 ;; level.
11179 (when (eq step-type 'up)
11180 (c-add-syntax 'substatement nil))))
11183 containing-sexp
11184 (or (null fixed-anchor)
11185 (> containing-sexp anchor-boi)))
11187 ;; Now we have to go out of this block.
11188 (goto-char containing-sexp)
11190 ;; Don't stop in the middle of a special brace list opener
11191 ;; like "({".
11192 (when c-special-brace-lists
11193 (let ((special-list (c-looking-at-special-brace-list)))
11194 (when (and special-list
11195 (< (car (car special-list)) (point)))
11196 (setq containing-sexp (car (car special-list)))
11197 (goto-char containing-sexp))))
11199 (setq paren-state (c-whack-state-after containing-sexp paren-state)
11200 containing-sexp (c-most-enclosing-brace paren-state)
11201 boi (c-point 'boi))
11203 ;; Analyze the construct in front of the block we've stepped out
11204 ;; from and add the right syntactic element for it.
11205 (let ((paren-pos (point))
11206 (paren-char (char-after))
11207 step-type)
11209 (if (eq paren-char ?\()
11210 ;; Stepped out of a parenthesis block, so we're in an
11211 ;; expression now.
11212 (progn
11213 (when (/= paren-pos boi)
11214 (if (and c-recognize-paren-inexpr-blocks
11215 (progn
11216 (c-backward-syntactic-ws containing-sexp)
11217 (or (not (looking-at "\\>"))
11218 (not (c-on-identifier))))
11219 (save-excursion
11220 (goto-char (1+ paren-pos))
11221 (c-forward-syntactic-ws)
11222 (eq (char-after) ?{)))
11223 ;; Stepped out of an in-expression statement. This
11224 ;; syntactic element won't get an anchor pos.
11225 (c-add-syntax 'inexpr-statement)
11227 ;; A parenthesis normally belongs to an arglist.
11228 (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
11230 (goto-char (max boi
11231 (if containing-sexp
11232 (1+ containing-sexp)
11233 (point-min))))
11234 (setq step-type 'same
11235 on-label nil))
11237 ;; Stepped out of a brace block.
11238 (setq step-type (c-beginning-of-statement-1 containing-sexp)
11239 on-label (eq step-type 'label))
11241 (if (and (eq step-type 'same)
11242 (/= paren-pos (point)))
11243 (let (inexpr)
11244 (cond
11245 ((save-excursion
11246 (goto-char paren-pos)
11247 (setq inexpr (c-looking-at-inexpr-block
11248 (c-safe-position containing-sexp paren-state)
11249 containing-sexp)))
11250 (c-add-syntax (if (eq (car inexpr) 'inlambda)
11251 'defun-block-intro
11252 'statement-block-intro)
11253 nil))
11254 ((looking-at c-other-decl-block-key)
11255 (c-add-syntax
11256 (cdr (assoc (match-string 1)
11257 c-other-decl-block-key-in-symbols-alist))
11258 (max (c-point 'boi paren-pos) (point))))
11259 ((save-excursion
11260 (goto-char paren-pos)
11261 (c-looking-at-or-maybe-in-bracelist containing-sexp))
11262 (if (save-excursion
11263 (goto-char paren-pos)
11264 (c-looking-at-statement-block))
11265 (c-add-syntax 'defun-block-intro nil)
11266 (c-add-syntax 'brace-list-intro nil)))
11267 (t (c-add-syntax 'defun-block-intro nil))))
11269 (c-add-syntax 'statement-block-intro nil)))
11271 (if (= paren-pos boi)
11272 ;; Always done if the open brace was at boi. The
11273 ;; c-beginning-of-statement-1 call above is necessary
11274 ;; anyway, to decide the type of block-intro to add.
11275 (goto-char paren-pos)
11276 (setq boi (c-point 'boi)))
11279 ;; Fill in the current point as the anchor for all the symbols
11280 ;; added above.
11281 (let ((p c-syntactic-context) q)
11282 (while (not (eq p syntax-last))
11283 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
11284 (while q
11285 (unless (car q)
11286 (setcar q (if (or (cdr p)
11287 (null fixed-anchor))
11288 (point)
11289 fixed-anchor)))
11290 (setq q (cdr q)))
11291 (setq p (cdr p))))
11294 (defun c-add-class-syntax (symbol
11295 containing-decl-open
11296 containing-decl-start
11297 containing-decl-kwd
11298 _paren-state)
11299 ;; The inclass and class-close syntactic symbols are added in
11300 ;; several places and some work is needed to fix everything.
11301 ;; Therefore it's collected here.
11303 ;; This function might do hidden buffer changes.
11304 (goto-char containing-decl-open)
11305 (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
11306 (progn
11307 (c-add-syntax symbol containing-decl-open)
11308 containing-decl-open)
11309 (goto-char containing-decl-start)
11310 ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
11311 ;; here, but we have to do like this for compatibility.
11312 (back-to-indentation)
11313 (c-add-syntax symbol (point))
11314 (if (and (c-keyword-member containing-decl-kwd
11315 'c-inexpr-class-kwds)
11316 (/= containing-decl-start (c-point 'boi containing-decl-start)))
11317 (c-add-syntax 'inexpr-class))
11318 (point)))
11320 (defun c-guess-continued-construct (indent-point
11321 char-after-ip
11322 beg-of-same-or-containing-stmt
11323 containing-sexp
11324 paren-state)
11325 ;; This function contains the decision tree reached through both
11326 ;; cases 18 and 10. It's a continued statement or top level
11327 ;; construct of some kind.
11329 ;; This function might do hidden buffer changes.
11331 (let (special-brace-list placeholder)
11332 (goto-char indent-point)
11333 (skip-chars-forward " \t")
11335 (cond
11336 ;; (CASE A removed.)
11337 ;; CASE B: open braces for class or brace-lists
11338 ((setq special-brace-list
11339 (or (and c-special-brace-lists
11340 (c-looking-at-special-brace-list))
11341 (eq char-after-ip ?{)))
11343 (cond
11344 ;; CASE B.1: class-open
11345 ((save-excursion
11346 (and (eq (char-after) ?{)
11347 (c-looking-at-decl-block containing-sexp t)
11348 (setq beg-of-same-or-containing-stmt (point))))
11349 (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
11351 ;; CASE B.2: brace-list-open
11352 ((or (consp special-brace-list)
11353 (consp
11354 (c-looking-at-or-maybe-in-bracelist
11355 containing-sexp beg-of-same-or-containing-stmt))
11357 ;; The most semantically accurate symbol here is
11358 ;; brace-list-open, but we normally report it simply as a
11359 ;; statement-cont. The reason is that one normally adjusts
11360 ;; brace-list-open for brace lists as top-level constructs,
11361 ;; and brace lists inside statements is a completely different
11362 ;; context. C.f. case 5A.3.
11363 (c-beginning-of-statement-1 containing-sexp)
11364 (c-add-stmt-syntax (if c-auto-newline-analysis
11365 ;; Turn off the dwim above when we're
11366 ;; analyzing the nature of the brace
11367 ;; for the auto newline feature.
11368 'brace-list-open
11369 'statement-cont)
11370 nil nil
11371 containing-sexp paren-state))
11373 ;; CASE B.3: The body of a function declared inside a normal
11374 ;; block. Can occur e.g. in Pike and when using gcc
11375 ;; extensions, but watch out for macros followed by blocks.
11376 ;; C.f. cases E, 16F and 17G.
11377 ((and (not (c-at-statement-start-p))
11378 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
11379 'same)
11380 (save-excursion
11381 (let ((c-recognize-typeless-decls nil))
11382 ;; Turn off recognition of constructs that lacks a
11383 ;; type in this case, since that's more likely to be
11384 ;; a macro followed by a block.
11385 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
11386 (c-add-stmt-syntax 'defun-open nil t
11387 containing-sexp paren-state))
11389 ;; CASE B.5: We have a C++11 "return \n { ..... }" Note that we're
11390 ;; not at the "{", currently.
11391 ((progn (goto-char indent-point)
11392 (backward-sexp)
11393 (looking-at c-return-key))
11394 (c-add-stmt-syntax 'statement-cont nil t
11395 containing-sexp paren-state))
11397 ;; CASE B.4: Continued statement with block open. The most
11398 ;; accurate analysis is perhaps `statement-cont' together with
11399 ;; `block-open' but we play DWIM and use `substatement-open'
11400 ;; instead. The rationale is that this typically is a macro
11401 ;; followed by a block which makes it very similar to a
11402 ;; statement with a substatement block.
11404 (c-add-stmt-syntax 'substatement-open nil nil
11405 containing-sexp paren-state))
11408 ;; CASE C: iostream insertion or extraction operator
11409 ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
11410 (save-excursion
11411 (goto-char beg-of-same-or-containing-stmt)
11412 ;; If there is no preceding streamop in the statement
11413 ;; then indent this line as a normal statement-cont.
11414 (when (c-syntactic-re-search-forward
11415 "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
11416 (c-add-syntax 'stream-op (c-point 'boi))
11417 t))))
11419 ;; CASE E: In the "K&R region" of a function declared inside a
11420 ;; normal block. C.f. case B.3.
11421 ((and (save-excursion
11422 ;; Check that the next token is a '{'. This works as
11423 ;; long as no language that allows nested function
11424 ;; definitions allows stuff like member init lists, K&R
11425 ;; declarations or throws clauses there.
11427 ;; Note that we do a forward search for something ahead
11428 ;; of the indentation line here. That's not good since
11429 ;; the user might not have typed it yet. Unfortunately
11430 ;; it's exceedingly tricky to recognize a function
11431 ;; prototype in a code block without resorting to this.
11432 (c-forward-syntactic-ws)
11433 (eq (char-after) ?{))
11434 (not (c-at-statement-start-p))
11435 (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
11436 'same)
11437 (save-excursion
11438 (let ((c-recognize-typeless-decls nil))
11439 ;; Turn off recognition of constructs that lacks a
11440 ;; type in this case, since that's more likely to be
11441 ;; a macro followed by a block.
11442 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
11443 (c-add-stmt-syntax 'func-decl-cont nil t
11444 containing-sexp paren-state))
11446 ;;CASE F: continued statement and the only preceding items are
11447 ;;annotations.
11448 ((and (c-major-mode-is 'java-mode)
11449 (setq placeholder (point))
11450 (c-beginning-of-statement-1)
11451 (progn
11452 (while (and (c-forward-annotation)
11453 (< (point) placeholder))
11454 (c-forward-syntactic-ws))
11456 (prog1
11457 (>= (point) placeholder)
11458 (goto-char placeholder)))
11459 (c-beginning-of-statement-1 containing-sexp)
11460 (c-add-syntax 'annotation-var-cont (point)))
11462 ;; CASE G: a template list continuation?
11463 ;; Mostly a duplication of case 5D.3 to fix templates-19:
11464 ((and (c-major-mode-is 'c++-mode)
11465 (save-excursion
11466 (goto-char indent-point)
11467 (c-with-syntax-table c++-template-syntax-table
11468 (setq placeholder (c-up-list-backward)))
11469 (and placeholder
11470 (eq (char-after placeholder) ?<)
11471 (/= (char-before placeholder) ?<)
11472 (progn
11473 (goto-char (1+ placeholder))
11474 (not (looking-at c-<-op-cont-regexp))))))
11475 (c-with-syntax-table c++-template-syntax-table
11476 (goto-char placeholder)
11477 (c-beginning-of-statement-1 containing-sexp t))
11478 (if (save-excursion
11479 (c-backward-syntactic-ws containing-sexp)
11480 (eq (char-before) ?<))
11481 ;; In a nested template arglist.
11482 (progn
11483 (goto-char placeholder)
11484 (c-syntactic-skip-backward "^,;" containing-sexp t)
11485 (c-forward-syntactic-ws))
11486 (back-to-indentation))
11487 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
11488 ;; template aware.
11489 (c-add-syntax 'template-args-cont (point) placeholder))
11491 ;; CASE D: continued statement.
11493 (c-beginning-of-statement-1 containing-sexp)
11494 (c-add-stmt-syntax 'statement-cont nil nil
11495 containing-sexp paren-state))
11498 ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
11499 ;; 2005/11/29).
11500 ;;;###autoload
11501 (defun c-guess-basic-syntax ()
11502 "Return the syntactic context of the current line."
11503 (save-excursion
11504 (beginning-of-line)
11505 (c-save-buffer-state
11506 ((indent-point (point))
11507 (case-fold-search nil)
11508 ;; A whole ugly bunch of various temporary variables. Have
11509 ;; to declare them here since it's not possible to declare
11510 ;; a variable with only the scope of a cond test and the
11511 ;; following result clauses, and most of this function is a
11512 ;; single gigantic cond. :P
11513 literal char-before-ip before-ws-ip char-after-ip macro-start
11514 in-macro-expr c-syntactic-context placeholder
11515 step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
11516 containing-<
11517 ;; The following record some positions for the containing
11518 ;; declaration block if we're directly within one:
11519 ;; `containing-decl-open' is the position of the open
11520 ;; brace. `containing-decl-start' is the start of the
11521 ;; declaration. `containing-decl-kwd' is the keyword
11522 ;; symbol of the keyword that tells what kind of block it
11523 ;; is.
11524 containing-decl-open
11525 containing-decl-start
11526 containing-decl-kwd
11527 ;; The open paren of the closest surrounding sexp or nil if
11528 ;; there is none.
11529 containing-sexp
11530 ;; The position after the closest preceding brace sexp
11531 ;; (nested sexps are ignored), or the position after
11532 ;; `containing-sexp' if there is none, or (point-min) if
11533 ;; `containing-sexp' is nil.
11535 ;; The paren state outside `containing-sexp', or at
11536 ;; `indent-point' if `containing-sexp' is nil.
11537 (paren-state (c-parse-state))
11538 (state-cache (copy-tree paren-state))
11539 ;; There's always at most one syntactic element which got
11540 ;; an anchor pos. It's stored in syntactic-relpos.
11541 syntactic-relpos
11542 (c-stmt-delim-chars c-stmt-delim-chars))
11544 ;; Check if we're directly inside an enclosing declaration
11545 ;; level block.
11546 (when (and (setq containing-sexp
11547 (c-most-enclosing-brace paren-state))
11548 (progn
11549 (goto-char containing-sexp)
11550 (eq (char-after) ?{))
11551 (setq placeholder
11552 (c-looking-at-decl-block
11553 (c-most-enclosing-brace paren-state
11554 containing-sexp)
11555 t)))
11556 (setq containing-decl-open containing-sexp
11557 containing-decl-start (point)
11558 containing-sexp nil)
11559 (goto-char placeholder)
11560 (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
11561 (c-keyword-sym (match-string 1)))))
11563 ;; Init some position variables.
11564 (if paren-state
11565 (progn
11566 (setq containing-sexp (car paren-state)
11567 paren-state (cdr paren-state))
11568 (if (consp containing-sexp)
11569 (save-excursion
11570 (goto-char (cdr containing-sexp))
11571 (if (and (c-major-mode-is 'c++-mode)
11572 (c-back-over-member-initializer-braces))
11573 (c-syntactic-skip-backward "^}" nil t))
11574 (setq lim (point))
11575 (if paren-state
11576 ;; Ignore balanced paren. The next entry
11577 ;; can't be another one.
11578 (setq containing-sexp (car paren-state)
11579 paren-state (cdr paren-state))
11580 ;; If there is no surrounding open paren then
11581 ;; put the last balanced pair back on paren-state.
11582 (setq paren-state (cons containing-sexp paren-state)
11583 containing-sexp nil)))
11584 (setq lim (1+ containing-sexp))))
11585 (setq lim (point-min)))
11587 ;; If we're in a parenthesis list then ',' delimits the
11588 ;; "statements" rather than being an operator (with the
11589 ;; exception of the "for" clause). This difference is
11590 ;; typically only noticeable when statements are used in macro
11591 ;; arglists.
11592 (when (and containing-sexp
11593 (eq (char-after containing-sexp) ?\())
11594 (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
11595 ;; cache char before and after indent point, and move point to
11596 ;; the most likely position to perform the majority of tests
11597 (goto-char indent-point)
11598 (c-backward-syntactic-ws lim)
11599 (setq before-ws-ip (point)
11600 char-before-ip (char-before))
11601 (goto-char indent-point)
11602 (skip-chars-forward " \t")
11603 (setq char-after-ip (char-after))
11605 ;; are we in a literal?
11606 (setq literal (c-in-literal lim))
11608 ;; now figure out syntactic qualities of the current line
11609 (cond
11611 ;; CASE 1: in a string.
11612 ((eq literal 'string)
11613 (c-add-syntax 'string (c-point 'bopl)))
11615 ;; CASE 2: in a C or C++ style comment.
11616 ((and (memq literal '(c c++))
11617 ;; This is a kludge for XEmacs where we use
11618 ;; `buffer-syntactic-context', which doesn't correctly
11619 ;; recognize "\*/" to end a block comment.
11620 ;; `parse-partial-sexp' which is used by
11621 ;; `c-literal-limits' will however do that in most
11622 ;; versions, which results in that we get nil from
11623 ;; `c-literal-limits' even when `c-in-literal' claims
11624 ;; we're inside a comment.
11625 (setq placeholder (c-literal-start lim)))
11626 (c-add-syntax literal placeholder))
11628 ;; CASE 3: in a cpp preprocessor macro continuation.
11629 ((and (save-excursion
11630 (when (c-beginning-of-macro)
11631 (setq macro-start (point))))
11632 (/= macro-start (c-point 'boi))
11633 (progn
11634 (setq tmpsymbol 'cpp-macro-cont)
11635 (or (not c-syntactic-indentation-in-macros)
11636 (save-excursion
11637 (goto-char macro-start)
11638 ;; If at the beginning of the body of a #define
11639 ;; directive then analyze as cpp-define-intro
11640 ;; only. Go on with the syntactic analysis
11641 ;; otherwise. in-macro-expr is set if we're in a
11642 ;; cpp expression, i.e. before the #define body
11643 ;; or anywhere in a non-#define directive.
11644 (if (c-forward-to-cpp-define-body)
11645 (let ((indent-boi (c-point 'boi indent-point)))
11646 (setq in-macro-expr (> (point) indent-boi)
11647 tmpsymbol 'cpp-define-intro)
11648 (= (point) indent-boi))
11649 (setq in-macro-expr t)
11650 nil)))))
11651 (c-add-syntax tmpsymbol macro-start)
11652 (setq macro-start nil))
11654 ;; CASE 11: an else clause?
11655 ((looking-at "else\\>[^_]")
11656 (c-beginning-of-statement-1 containing-sexp)
11657 (c-add-stmt-syntax 'else-clause nil t
11658 containing-sexp paren-state))
11660 ;; CASE 12: while closure of a do/while construct?
11661 ((and (looking-at "while\\>[^_]")
11662 (save-excursion
11663 (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
11664 'beginning)
11665 (setq placeholder (point)))))
11666 (goto-char placeholder)
11667 (c-add-stmt-syntax 'do-while-closure nil t
11668 containing-sexp paren-state))
11670 ;; CASE 13: A catch or finally clause? This case is simpler
11671 ;; than if-else and do-while, because a block is required
11672 ;; after every try, catch and finally.
11673 ((save-excursion
11674 (and (cond ((c-major-mode-is 'c++-mode)
11675 (looking-at "catch\\>[^_]"))
11676 ((c-major-mode-is 'java-mode)
11677 (looking-at "\\(catch\\|finally\\)\\>[^_]")))
11678 (and (c-safe (c-backward-syntactic-ws)
11679 (c-backward-sexp)
11681 (eq (char-after) ?{)
11682 (c-safe (c-backward-syntactic-ws)
11683 (c-backward-sexp)
11685 (if (eq (char-after) ?\()
11686 (c-safe (c-backward-sexp) t)
11688 (looking-at "\\(try\\|catch\\)\\>[^_]")
11689 (setq placeholder (point))))
11690 (goto-char placeholder)
11691 (c-add-stmt-syntax 'catch-clause nil t
11692 containing-sexp paren-state))
11694 ;; CASE 18: A substatement we can recognize by keyword.
11695 ((save-excursion
11696 (and c-opt-block-stmt-key
11697 (not (eq char-before-ip ?\;))
11698 (not (c-at-vsemi-p before-ws-ip))
11699 (not (memq char-after-ip '(?\) ?\] ?,)))
11700 (or (not (eq char-before-ip ?}))
11701 (c-looking-at-inexpr-block-backward state-cache))
11702 (> (point)
11703 (progn
11704 ;; Ought to cache the result from the
11705 ;; c-beginning-of-statement-1 calls here.
11706 (setq placeholder (point))
11707 (while (eq (setq step-type
11708 (c-beginning-of-statement-1 lim))
11709 'label))
11710 (if (eq step-type 'previous)
11711 (goto-char placeholder)
11712 (setq placeholder (point))
11713 (if (and (eq step-type 'same)
11714 (not (looking-at c-opt-block-stmt-key)))
11715 ;; Step up to the containing statement if we
11716 ;; stayed in the same one.
11717 (let (step)
11718 (while (eq
11719 (setq step
11720 (c-beginning-of-statement-1 lim))
11721 'label))
11722 (if (eq step 'up)
11723 (setq placeholder (point))
11724 ;; There was no containing statement after all.
11725 (goto-char placeholder)))))
11726 placeholder))
11727 (if (looking-at c-block-stmt-2-key)
11728 ;; Require a parenthesis after these keywords.
11729 ;; Necessary to catch e.g. synchronized in Java,
11730 ;; which can be used both as statement and
11731 ;; modifier.
11732 (and (zerop (c-forward-token-2 1 nil))
11733 (eq (char-after) ?\())
11734 (looking-at c-opt-block-stmt-key))))
11736 (if (eq step-type 'up)
11737 ;; CASE 18A: Simple substatement.
11738 (progn
11739 (goto-char placeholder)
11740 (cond
11741 ((eq char-after-ip ?{)
11742 (c-add-stmt-syntax 'substatement-open nil nil
11743 containing-sexp paren-state))
11744 ((save-excursion
11745 (goto-char indent-point)
11746 (back-to-indentation)
11747 (c-forward-label))
11748 (c-add-stmt-syntax 'substatement-label nil nil
11749 containing-sexp paren-state))
11751 (c-add-stmt-syntax 'substatement nil nil
11752 containing-sexp paren-state))))
11754 ;; CASE 18B: Some other substatement. This is shared
11755 ;; with case 10.
11756 (c-guess-continued-construct indent-point
11757 char-after-ip
11758 placeholder
11760 paren-state)))
11762 ;; CASE 14: A case or default label
11763 ((save-excursion
11764 (and (looking-at c-label-kwds-regexp)
11765 (or (c-major-mode-is 'idl-mode)
11766 (and
11767 containing-sexp
11768 (goto-char containing-sexp)
11769 (eq (char-after) ?{)
11770 (progn (c-backward-syntactic-ws) t)
11771 (eq (char-before) ?\))
11772 (c-go-list-backward)
11773 (progn (c-backward-syntactic-ws) t)
11774 (c-simple-skip-symbol-backward)
11775 (looking-at c-block-stmt-2-key)))))
11776 (if containing-sexp
11777 (progn
11778 (goto-char containing-sexp)
11779 (setq lim (c-most-enclosing-brace state-cache
11780 containing-sexp))
11781 (c-backward-to-block-anchor lim)
11782 (c-add-stmt-syntax 'case-label nil t lim paren-state))
11783 ;; Got a bogus label at the top level. In lack of better
11784 ;; alternatives, anchor it on (point-min).
11785 (c-add-syntax 'case-label (point-min))))
11787 ;; CASE 15: any other label
11788 ((save-excursion
11789 (back-to-indentation)
11790 (and (not (looking-at c-syntactic-ws-start))
11791 (not (looking-at c-label-kwds-regexp))
11792 (c-forward-label)))
11793 (cond (containing-decl-open
11794 (setq placeholder (c-add-class-syntax 'inclass
11795 containing-decl-open
11796 containing-decl-start
11797 containing-decl-kwd
11798 paren-state))
11799 ;; Append access-label with the same anchor point as
11800 ;; inclass gets.
11801 (c-append-syntax 'access-label placeholder))
11803 (containing-sexp
11804 (goto-char containing-sexp)
11805 (setq lim (c-most-enclosing-brace state-cache
11806 containing-sexp))
11807 (save-excursion
11808 (setq tmpsymbol
11809 (if (and (eq (c-beginning-of-statement-1 lim) 'up)
11810 (looking-at "switch\\>[^_]"))
11811 ;; If the surrounding statement is a switch then
11812 ;; let's analyze all labels as switch labels, so
11813 ;; that they get lined up consistently.
11814 'case-label
11815 'label)))
11816 (c-backward-to-block-anchor lim)
11817 (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
11820 ;; A label on the top level. Treat it as a class
11821 ;; context. (point-min) is the closest we get to the
11822 ;; class open brace.
11823 (c-add-syntax 'access-label (point-min)))))
11825 ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
11826 ;; 17E.
11827 ((setq placeholder (c-looking-at-inexpr-block
11828 (c-safe-position containing-sexp paren-state)
11829 containing-sexp
11830 ;; Have to turn on the heuristics after
11831 ;; the point even though it doesn't work
11832 ;; very well. C.f. test case class-16.pike.
11834 (setq tmpsymbol (assq (car placeholder)
11835 '((inexpr-class . class-open)
11836 (inexpr-statement . block-open))))
11837 (if tmpsymbol
11838 ;; It's a statement block or an anonymous class.
11839 (setq tmpsymbol (cdr tmpsymbol))
11840 ;; It's a Pike lambda. Check whether we are between the
11841 ;; lambda keyword and the argument list or at the defun
11842 ;; opener.
11843 (setq tmpsymbol (if (eq char-after-ip ?{)
11844 'inline-open
11845 'lambda-intro-cont)))
11846 (goto-char (cdr placeholder))
11847 (back-to-indentation)
11848 (c-add-stmt-syntax tmpsymbol nil t
11849 (c-most-enclosing-brace state-cache (point))
11850 paren-state)
11851 (unless (eq (point) (cdr placeholder))
11852 (c-add-syntax (car placeholder))))
11854 ;; CASE 5: Line is inside a declaration level block or at top level.
11855 ((or containing-decl-open (null containing-sexp))
11856 (cond
11858 ;; CASE 5A: we are looking at a defun, brace list, class,
11859 ;; or inline-inclass method opening brace
11860 ((setq special-brace-list
11861 (or (and c-special-brace-lists
11862 (c-looking-at-special-brace-list))
11863 (eq char-after-ip ?{)))
11864 (cond
11866 ;; CASE 5A.1: Non-class declaration block open.
11867 ((save-excursion
11868 (let (tmp)
11869 (and (eq char-after-ip ?{)
11870 (setq tmp (c-looking-at-decl-block containing-sexp t))
11871 (progn
11872 (setq placeholder (point))
11873 (goto-char tmp)
11874 (looking-at c-symbol-key))
11875 (c-keyword-member
11876 (c-keyword-sym (setq keyword (match-string 0)))
11877 'c-other-block-decl-kwds))))
11878 (goto-char placeholder)
11879 (c-add-stmt-syntax
11880 (if (string-equal keyword "extern")
11881 ;; Special case for extern-lang-open.
11882 'extern-lang-open
11883 (intern (concat keyword "-open")))
11884 nil t containing-sexp paren-state))
11886 ;; CASE 5A.2: we are looking at a class opening brace
11887 ((save-excursion
11888 (goto-char indent-point)
11889 (skip-chars-forward " \t")
11890 (and (eq (char-after) ?{)
11891 (c-looking-at-decl-block containing-sexp t)
11892 (setq placeholder (point))))
11893 (c-add-syntax 'class-open placeholder))
11895 ;; CASE 5A.3: brace list open
11896 ((save-excursion
11897 (goto-char indent-point)
11898 (skip-chars-forward " \t")
11899 (cond
11900 ((c-backward-over-enum-header)
11901 (setq placeholder (c-point 'boi)))
11902 ((consp (setq placeholder
11903 (c-looking-at-or-maybe-in-bracelist
11904 containing-sexp lim)))
11905 (setq tmpsymbol (and (cdr placeholder) 'topmost-intro-cont))
11906 (setq placeholder (c-point 'boi (car placeholder))))))
11907 (if (and (not c-auto-newline-analysis)
11908 ;(c-major-mode-is 'java-mode) ; Not needed anymore (2016-08-30).
11909 (eq tmpsymbol 'topmost-intro-cont))
11910 ;; We're in Java and have found that the open brace
11911 ;; belongs to a "new Foo[]" initialization list,
11912 ;; which means the brace list is part of an
11913 ;; expression and not a top level definition. We
11914 ;; therefore treat it as any topmost continuation
11915 ;; even though the semantically correct symbol still
11916 ;; is brace-list-open, on the same grounds as in
11917 ;; case B.2.
11918 (progn
11919 (c-beginning-of-statement-1 lim)
11920 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
11921 (c-add-syntax 'brace-list-open placeholder)))
11923 ;; CASE 5A.4: inline defun open
11924 ((and containing-decl-open
11925 (not (c-keyword-member containing-decl-kwd
11926 'c-other-block-decl-kwds)))
11927 (c-add-syntax 'inline-open)
11928 (c-add-class-syntax 'inclass
11929 containing-decl-open
11930 containing-decl-start
11931 containing-decl-kwd
11932 paren-state))
11934 ;; CASE 5A.5: ordinary defun open
11936 (save-excursion
11937 (c-beginning-of-decl-1 lim)
11938 (while (cond
11939 ((looking-at c-specifier-key)
11940 (c-forward-keyword-clause 1))
11941 ((and c-opt-cpp-prefix
11942 (looking-at c-noise-macro-with-parens-name-re))
11943 (c-forward-noise-clause))))
11944 (c-add-syntax 'defun-open (c-point 'boi))
11945 ;; Bogus to use bol here, but it's the legacy. (Resolved,
11946 ;; 2007-11-09)
11947 ))))
11949 ;; CASE 5R: Member init list. (Used to be part of CASE 5B.1)
11950 ;; Note there is no limit on the backward search here, since member
11951 ;; init lists can, in practice, be very large.
11952 ((save-excursion
11953 (when (and (c-major-mode-is 'c++-mode)
11954 (setq placeholder (c-back-over-member-initializers)))
11955 (setq tmp-pos (point))))
11956 (if (= (c-point 'bosws) (1+ tmp-pos))
11957 (progn
11958 ;; There is no preceding member init clause.
11959 ;; Indent relative to the beginning of indentation
11960 ;; for the topmost-intro line that contains the
11961 ;; prototype's open paren.
11962 (goto-char placeholder)
11963 (c-add-syntax 'member-init-intro (c-point 'boi)))
11964 ;; Indent relative to the first member init clause.
11965 (goto-char (1+ tmp-pos))
11966 (c-forward-syntactic-ws)
11967 (c-add-syntax 'member-init-cont (point))))
11969 ;; CASE 5B: After a function header but before the body (or
11970 ;; the ending semicolon if there's no body).
11971 ((save-excursion
11972 (when (setq placeholder (c-just-after-func-arglist-p
11973 (max lim (c-determine-limit 500))))
11974 (setq tmp-pos (point))))
11975 (cond
11977 ;; CASE 5B.1: Member init list.
11978 ((eq (char-after tmp-pos) ?:)
11979 ;; There is no preceding member init clause.
11980 ;; Indent relative to the beginning of indentation
11981 ;; for the topmost-intro line that contains the
11982 ;; prototype's open paren.
11983 (goto-char placeholder)
11984 (c-add-syntax 'member-init-intro (c-point 'boi)))
11986 ;; CASE 5B.2: K&R arg decl intro
11987 ((and c-recognize-knr-p
11988 (c-in-knr-argdecl lim))
11989 (c-beginning-of-statement-1 lim)
11990 (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
11991 (if containing-decl-open
11992 (c-add-class-syntax 'inclass
11993 containing-decl-open
11994 containing-decl-start
11995 containing-decl-kwd
11996 paren-state)))
11998 ;; CASE 5B.4: Nether region after a C++ or Java func
11999 ;; decl, which could include a `throws' declaration.
12001 (c-beginning-of-statement-1 lim)
12002 (c-add-syntax 'func-decl-cont (c-point 'boi))
12005 ;; CASE 5C: inheritance line. could be first inheritance
12006 ;; line, or continuation of a multiple inheritance
12007 ((or (and (c-major-mode-is 'c++-mode)
12008 (progn
12009 (when (eq char-after-ip ?,)
12010 (skip-chars-forward " \t")
12011 (forward-char))
12012 (looking-at c-opt-postfix-decl-spec-key)))
12013 (and (or (eq char-before-ip ?:)
12014 ;; watch out for scope operator
12015 (save-excursion
12016 (and (eq char-after-ip ?:)
12017 (c-safe (forward-char 1) t)
12018 (not (eq (char-after) ?:))
12020 (save-excursion
12021 (c-beginning-of-statement-1 lim)
12022 (when (looking-at c-opt-<>-sexp-key)
12023 (goto-char (match-end 1))
12024 (c-forward-syntactic-ws)
12025 (c-forward-<>-arglist nil)
12026 (c-forward-syntactic-ws))
12027 (looking-at c-class-key)))
12028 ;; for Java
12029 (and (c-major-mode-is 'java-mode)
12030 (let ((fence (save-excursion
12031 (c-beginning-of-statement-1 lim)
12032 (point)))
12033 cont done)
12034 (save-excursion
12035 (while (not done)
12036 (cond ((looking-at c-opt-postfix-decl-spec-key)
12037 (setq injava-inher (cons cont (point))
12038 done t))
12039 ((or (not (c-safe (c-forward-sexp -1) t))
12040 (<= (point) fence))
12041 (setq done t))
12043 (setq cont t)))
12044 injava-inher)
12045 (not (c-crosses-statement-barrier-p (cdr injava-inher)
12046 (point)))
12048 (cond
12050 ;; CASE 5C.1: non-hanging colon on an inher intro
12051 ((eq char-after-ip ?:)
12052 (c-beginning-of-statement-1 lim)
12053 (c-add-syntax 'inher-intro (c-point 'boi))
12054 ;; don't add inclass symbol since relative point already
12055 ;; contains any class offset
12058 ;; CASE 5C.2: hanging colon on an inher intro
12059 ((eq char-before-ip ?:)
12060 (c-beginning-of-statement-1 lim)
12061 (c-add-syntax 'inher-intro (c-point 'boi))
12062 (if containing-decl-open
12063 (c-add-class-syntax 'inclass
12064 containing-decl-open
12065 containing-decl-start
12066 containing-decl-kwd
12067 paren-state)))
12069 ;; CASE 5C.3: in a Java implements/extends
12070 (injava-inher
12071 (let ((where (cdr injava-inher))
12072 (cont (car injava-inher)))
12073 (goto-char where)
12074 (cond ((looking-at "throws\\>[^_]")
12075 (c-add-syntax 'func-decl-cont
12076 (progn (c-beginning-of-statement-1 lim)
12077 (c-point 'boi))))
12078 (cont (c-add-syntax 'inher-cont where))
12079 (t (c-add-syntax 'inher-intro
12080 (progn (goto-char (cdr injava-inher))
12081 (c-beginning-of-statement-1 lim)
12082 (point))))
12085 ;; CASE 5C.4: a continued inheritance line
12087 (c-beginning-of-inheritance-list lim)
12088 (c-add-syntax 'inher-cont (point))
12089 ;; don't add inclass symbol since relative point already
12090 ;; contains any class offset
12093 ;; CASE 5P: AWK pattern or function or continuation
12094 ;; thereof.
12095 ((c-major-mode-is 'awk-mode)
12096 (setq placeholder (point))
12097 (c-add-stmt-syntax
12098 (if (and (eq (c-beginning-of-statement-1) 'same)
12099 (/= (point) placeholder))
12100 'topmost-intro-cont
12101 'topmost-intro)
12102 nil nil
12103 containing-sexp paren-state))
12105 ;; CASE 5D: this could be a top-level initialization, a
12106 ;; member init list continuation, or a template argument
12107 ;; list continuation.
12108 ((save-excursion
12109 ;; Note: We use the fact that lim is always after any
12110 ;; preceding brace sexp.
12111 (if c-recognize-<>-arglists
12112 (while (and
12113 (progn
12114 (c-syntactic-skip-backward "^;,=<>" lim t)
12115 (> (point) lim))
12117 (when c-overloadable-operators-regexp
12118 (when (setq placeholder (c-after-special-operator-id lim))
12119 (goto-char placeholder)
12121 (cond
12122 ((eq (char-before) ?>)
12123 (or (c-backward-<>-arglist nil lim)
12124 (backward-char))
12126 ((eq (char-before) ?<)
12127 (backward-char)
12128 (if (save-excursion
12129 (c-forward-<>-arglist nil))
12130 (progn (forward-char)
12131 nil)
12133 (t nil)))))
12134 ;; NB: No c-after-special-operator-id stuff in this
12135 ;; clause - we assume only C++ needs it.
12136 (c-syntactic-skip-backward "^;,=" lim t))
12137 (memq (char-before) '(?, ?= ?<)))
12138 (cond
12140 ;; CASE 5D.3: perhaps a template list continuation?
12141 ((and (c-major-mode-is 'c++-mode)
12142 (save-excursion
12143 (save-restriction
12144 (c-with-syntax-table c++-template-syntax-table
12145 (goto-char indent-point)
12146 (setq placeholder (c-up-list-backward))
12147 (and placeholder
12148 (eq (char-after placeholder) ?<))))))
12149 (c-with-syntax-table c++-template-syntax-table
12150 (goto-char placeholder)
12151 (c-beginning-of-statement-1 lim t))
12152 (if (save-excursion
12153 (c-backward-syntactic-ws lim)
12154 (eq (char-before) ?<))
12155 ;; In a nested template arglist.
12156 (progn
12157 (goto-char placeholder)
12158 (c-syntactic-skip-backward "^,;" lim t)
12159 (c-forward-syntactic-ws))
12160 (back-to-indentation))
12161 ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
12162 ;; template aware.
12163 (c-add-syntax 'template-args-cont (point) placeholder))
12165 ;; CASE 5D.4: perhaps a multiple inheritance line?
12166 ((and (c-major-mode-is 'c++-mode)
12167 (save-excursion
12168 (c-beginning-of-statement-1 lim)
12169 (setq placeholder (point))
12170 (if (looking-at "static\\>[^_]")
12171 (c-forward-token-2 1 nil indent-point))
12172 (and (looking-at c-class-key)
12173 (zerop (c-forward-token-2 2 nil indent-point))
12174 (if (eq (char-after) ?<)
12175 (c-with-syntax-table c++-template-syntax-table
12176 (zerop (c-forward-token-2 1 t indent-point)))
12178 (eq (char-after) ?:))))
12179 (goto-char placeholder)
12180 (c-add-syntax 'inher-cont (c-point 'boi)))
12182 ;; CASE 5D.5: Continuation of the "expression part" of a
12183 ;; top level construct. Or, perhaps, an unrecognized construct.
12185 (while (and (setq placeholder (point))
12186 (eq (car (c-beginning-of-decl-1 containing-sexp)) ; Can't use `lim' here.
12187 'same)
12188 (save-excursion
12189 (c-backward-syntactic-ws)
12190 (eq (char-before) ?}))
12191 (< (point) placeholder)))
12192 (c-add-stmt-syntax
12193 (cond
12194 ((eq (point) placeholder) 'statement) ; unrecognized construct
12195 ;; A preceding comma at the top level means that a
12196 ;; new variable declaration starts here. Use
12197 ;; topmost-intro-cont for it, for consistency with
12198 ;; the first variable declaration. C.f. case 5N.
12199 ((eq char-before-ip ?,) 'topmost-intro-cont)
12200 (t 'statement-cont))
12201 nil nil containing-sexp paren-state))
12204 ;; CASE 5F: Close of a non-class declaration level block.
12205 ((and (eq char-after-ip ?})
12206 (c-keyword-member containing-decl-kwd
12207 'c-other-block-decl-kwds))
12208 ;; This is inconsistent: Should use `containing-decl-open'
12209 ;; here if it's at boi, like in case 5J.
12210 (goto-char containing-decl-start)
12211 (c-add-stmt-syntax
12212 (if (string-equal (symbol-name containing-decl-kwd) "extern")
12213 ;; Special case for compatibility with the
12214 ;; extern-lang syntactic symbols.
12215 'extern-lang-close
12216 (intern (concat (symbol-name containing-decl-kwd)
12217 "-close")))
12218 nil t
12219 (c-most-enclosing-brace paren-state (point))
12220 paren-state))
12222 ;; CASE 5G: we are looking at the brace which closes the
12223 ;; enclosing nested class decl
12224 ((and containing-sexp
12225 (eq char-after-ip ?})
12226 (eq containing-decl-open containing-sexp))
12227 (c-add-class-syntax 'class-close
12228 containing-decl-open
12229 containing-decl-start
12230 containing-decl-kwd
12231 paren-state))
12233 ;; CASE 5H: we could be looking at subsequent knr-argdecls
12234 ((and c-recognize-knr-p
12235 (not containing-sexp) ; can't be knr inside braces.
12236 (not (eq char-before-ip ?}))
12237 (save-excursion
12238 (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
12239 (and placeholder
12240 ;; Do an extra check to avoid tripping up on
12241 ;; statements that occur in invalid contexts
12242 ;; (e.g. in macro bodies where we don't really
12243 ;; know the context of what we're looking at).
12244 (not (and c-opt-block-stmt-key
12245 (looking-at c-opt-block-stmt-key)))))
12246 (< placeholder indent-point))
12247 (goto-char placeholder)
12248 (c-add-syntax 'knr-argdecl (point)))
12250 ;; CASE 5I: ObjC method definition.
12251 ((and c-opt-method-key
12252 (looking-at c-opt-method-key))
12253 (c-beginning-of-statement-1 nil t)
12254 (if (= (point) indent-point)
12255 ;; Handle the case when it's the first (non-comment)
12256 ;; thing in the buffer. Can't look for a 'same return
12257 ;; value from cbos1 since ObjC directives currently
12258 ;; aren't recognized fully, so that we get 'same
12259 ;; instead of 'previous if it moved over a preceding
12260 ;; directive.
12261 (goto-char (point-min)))
12262 (c-add-syntax 'objc-method-intro (c-point 'boi)))
12264 ;; CASE 5N: At a variable declaration that follows a class
12265 ;; definition or some other block declaration that doesn't
12266 ;; end at the closing '}'. C.f. case 5D.5.
12267 ((progn
12268 (c-backward-syntactic-ws lim)
12269 (and (eq (char-before) ?})
12270 (save-excursion
12271 (let ((start (point)))
12272 (if (and state-cache
12273 (consp (car state-cache))
12274 (eq (cdar state-cache) (point)))
12275 ;; Speed up the backward search a bit.
12276 (goto-char (caar state-cache)))
12277 (c-beginning-of-decl-1 containing-sexp) ; Can't use `lim' here.
12278 (setq placeholder (point))
12279 (if (= start (point))
12280 ;; The '}' is unbalanced.
12282 (c-end-of-decl-1)
12283 (>= (point) indent-point))))))
12284 (goto-char placeholder)
12285 (c-add-stmt-syntax 'topmost-intro-cont nil nil
12286 containing-sexp paren-state))
12288 ;; NOTE: The point is at the end of the previous token here.
12290 ;; CASE 5J: we are at the topmost level, make
12291 ;; sure we skip back past any access specifiers
12292 ((and
12293 ;; A macro continuation line is never at top level.
12294 (not (and macro-start
12295 (> indent-point macro-start)))
12296 (save-excursion
12297 (setq placeholder (point))
12298 (or (memq char-before-ip '(?\; ?{ ?} nil))
12299 (c-at-vsemi-p before-ws-ip)
12300 (when (and (eq char-before-ip ?:)
12301 (eq (c-beginning-of-statement-1 lim)
12302 'label))
12303 (c-backward-syntactic-ws lim)
12304 (setq placeholder (point)))
12305 (and (c-major-mode-is 'objc-mode)
12306 (catch 'not-in-directive
12307 (c-beginning-of-statement-1 lim)
12308 (setq placeholder (point))
12309 (while (and (c-forward-objc-directive)
12310 (< (point) indent-point))
12311 (c-forward-syntactic-ws)
12312 (if (>= (point) indent-point)
12313 (throw 'not-in-directive t))
12314 (setq placeholder (point)))
12315 nil)))))
12316 ;; For historic reasons we anchor at bol of the last
12317 ;; line of the previous declaration. That's clearly
12318 ;; highly bogus and useless, and it makes our lives hard
12319 ;; to remain compatible. :P
12320 (goto-char placeholder)
12321 (c-add-syntax 'topmost-intro (c-point 'bol))
12322 (if containing-decl-open
12323 (if (c-keyword-member containing-decl-kwd
12324 'c-other-block-decl-kwds)
12325 (progn
12326 (goto-char (c-brace-anchor-point containing-decl-open))
12327 (c-add-stmt-syntax
12328 (if (string-equal (symbol-name containing-decl-kwd)
12329 "extern")
12330 ;; Special case for compatibility with the
12331 ;; extern-lang syntactic symbols.
12332 'inextern-lang
12333 (intern (concat "in"
12334 (symbol-name containing-decl-kwd))))
12335 nil t
12336 (c-most-enclosing-brace paren-state (point))
12337 paren-state))
12338 (c-add-class-syntax 'inclass
12339 containing-decl-open
12340 containing-decl-start
12341 containing-decl-kwd
12342 paren-state)))
12343 (when (and c-syntactic-indentation-in-macros
12344 macro-start
12345 (/= macro-start (c-point 'boi indent-point)))
12346 (c-add-syntax 'cpp-define-intro)
12347 (setq macro-start nil)))
12349 ;; CASE 5K: we are at an ObjC method definition
12350 ;; continuation line.
12351 ((and c-opt-method-key
12352 (save-excursion
12353 (c-beginning-of-statement-1 lim)
12354 (beginning-of-line)
12355 (when (looking-at c-opt-method-key)
12356 (setq placeholder (point)))))
12357 (c-add-syntax 'objc-method-args-cont placeholder))
12359 ;; CASE 5L: we are at the first argument of a template
12360 ;; arglist that begins on the previous line.
12361 ((and c-recognize-<>-arglists
12362 (eq (char-before) ?<)
12363 (not (and c-overloadable-operators-regexp
12364 (c-after-special-operator-id lim))))
12365 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
12366 (c-add-syntax 'template-args-cont (c-point 'boi)))
12368 ;; CASE 5Q: we are at a statement within a macro.
12369 (macro-start
12370 (c-beginning-of-statement-1 containing-sexp)
12371 (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
12373 ;;CASE 5N: We are at a topmost continuation line and the only
12374 ;;preceding items are annotations.
12375 ((and (c-major-mode-is 'java-mode)
12376 (setq placeholder (point))
12377 (c-beginning-of-statement-1)
12378 (progn
12379 (while (and (c-forward-annotation))
12380 (c-forward-syntactic-ws))
12382 (prog1
12383 (>= (point) placeholder)
12384 (goto-char placeholder)))
12385 (c-add-syntax 'annotation-top-cont (c-point 'boi)))
12387 ;; CASE 5M: we are at a topmost continuation line
12389 (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
12390 (when (c-major-mode-is 'objc-mode)
12391 (setq placeholder (point))
12392 (while (and (c-forward-objc-directive)
12393 (< (point) indent-point))
12394 (c-forward-syntactic-ws)
12395 (setq placeholder (point)))
12396 (goto-char placeholder))
12397 (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
12400 ;; (CASE 6 has been removed.)
12402 ;; CASE 7: line is an expression, not a statement. Most
12403 ;; likely we are either in a function prototype or a function
12404 ;; call argument list
12405 ((not (or (and c-special-brace-lists
12406 (save-excursion
12407 (goto-char containing-sexp)
12408 (c-looking-at-special-brace-list)))
12409 (eq (char-after containing-sexp) ?{)))
12410 (cond
12412 ;; CASE 7A: we are looking at the arglist closing paren.
12413 ;; C.f. case 7F.
12414 ((memq char-after-ip '(?\) ?\]))
12415 (goto-char containing-sexp)
12416 (setq placeholder (c-point 'boi))
12417 (if (and (c-safe (backward-up-list 1) t)
12418 (>= (point) placeholder))
12419 (progn
12420 (forward-char)
12421 (skip-chars-forward " \t"))
12422 (goto-char placeholder))
12423 (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
12424 (c-most-enclosing-brace paren-state (point))
12425 paren-state))
12427 ;; CASE 7B: Looking at the opening brace of an
12428 ;; in-expression block or brace list. C.f. cases 4, 16A
12429 ;; and 17E.
12430 ((and (eq char-after-ip ?{)
12431 (progn
12432 (setq placeholder (c-inside-bracelist-p (point)
12433 paren-state
12434 nil))
12435 (if placeholder
12436 (setq tmpsymbol '(brace-list-open . inexpr-class))
12437 (setq tmpsymbol '(block-open . inexpr-statement)
12438 placeholder
12439 (cdr-safe (c-looking-at-inexpr-block
12440 (c-safe-position containing-sexp
12441 paren-state)
12442 containing-sexp)))
12443 ;; placeholder is nil if it's a block directly in
12444 ;; a function arglist. That makes us skip out of
12445 ;; this case.
12447 (goto-char placeholder)
12448 (back-to-indentation)
12449 (c-add-stmt-syntax (car tmpsymbol) nil t
12450 (c-most-enclosing-brace paren-state (point))
12451 paren-state)
12452 (if (/= (point) placeholder)
12453 (c-add-syntax (cdr tmpsymbol))))
12455 ;; CASE 7C: we are looking at the first argument in an empty
12456 ;; argument list. Use arglist-close if we're actually
12457 ;; looking at a close paren or bracket.
12458 ((memq char-before-ip '(?\( ?\[))
12459 (goto-char containing-sexp)
12460 (setq placeholder (c-point 'boi))
12461 (if (and (c-safe (backward-up-list 1) t)
12462 (>= (point) placeholder))
12463 (progn
12464 (forward-char)
12465 (skip-chars-forward " \t"))
12466 (goto-char placeholder))
12467 (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
12468 (c-most-enclosing-brace paren-state (point))
12469 paren-state))
12471 ;; CASE 7D: we are inside a conditional test clause. treat
12472 ;; these things as statements
12473 ((progn
12474 (goto-char containing-sexp)
12475 (and (c-safe (c-forward-sexp -1) t)
12476 (looking-at "\\<for\\>[^_]")))
12477 (goto-char (1+ containing-sexp))
12478 (c-forward-syntactic-ws indent-point)
12479 (if (eq char-before-ip ?\;)
12480 (c-add-syntax 'statement (point))
12481 (c-add-syntax 'statement-cont (point))
12484 ;; CASE 7E: maybe a continued ObjC method call. This is the
12485 ;; case when we are inside a [] bracketed exp, and what
12486 ;; precede the opening bracket is not an identifier.
12487 ((and c-opt-method-key
12488 (eq (char-after containing-sexp) ?\[)
12489 (progn
12490 (goto-char (1- containing-sexp))
12491 (c-backward-syntactic-ws (c-point 'bod))
12492 (if (not (looking-at c-symbol-key))
12493 (c-add-syntax 'objc-method-call-cont containing-sexp))
12496 ;; CASE 7F: we are looking at an arglist continuation line,
12497 ;; but the preceding argument is on the same line as the
12498 ;; opening paren. This case includes multi-line
12499 ;; mathematical paren groupings, but we could be on a
12500 ;; for-list continuation line. C.f. case 7A.
12501 ((progn
12502 (goto-char (1+ containing-sexp))
12503 (< (save-excursion
12504 (c-forward-syntactic-ws)
12505 (point))
12506 (c-point 'bonl)))
12507 (goto-char containing-sexp) ; paren opening the arglist
12508 (setq placeholder (c-point 'boi))
12509 (if (and (c-safe (backward-up-list 1) t)
12510 (>= (point) placeholder))
12511 (progn
12512 (forward-char)
12513 (skip-chars-forward " \t"))
12514 (goto-char placeholder))
12515 (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
12516 (c-most-enclosing-brace state-cache (point))
12517 paren-state))
12519 ;; CASE 7G: we are looking at just a normal arglist
12520 ;; continuation line
12521 (t (c-forward-syntactic-ws indent-point)
12522 (c-add-syntax 'arglist-cont (c-point 'boi)))
12525 ;; CASE 8: func-local multi-inheritance line
12526 ((and (c-major-mode-is 'c++-mode)
12527 (save-excursion
12528 (goto-char indent-point)
12529 (skip-chars-forward " \t")
12530 (looking-at c-opt-postfix-decl-spec-key)))
12531 (goto-char indent-point)
12532 (skip-chars-forward " \t")
12533 (cond
12535 ;; CASE 8A: non-hanging colon on an inher intro
12536 ((eq char-after-ip ?:)
12537 (c-backward-syntactic-ws lim)
12538 (c-add-syntax 'inher-intro (c-point 'boi)))
12540 ;; CASE 8B: hanging colon on an inher intro
12541 ((eq char-before-ip ?:)
12542 (c-add-syntax 'inher-intro (c-point 'boi)))
12544 ;; CASE 8C: a continued inheritance line
12546 (c-beginning-of-inheritance-list lim)
12547 (c-add-syntax 'inher-cont (point))
12550 ;; CASE 9: we are inside a brace-list
12551 ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
12552 (setq special-brace-list
12553 (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
12554 (save-excursion
12555 (goto-char containing-sexp)
12556 (c-looking-at-special-brace-list)))
12557 (c-inside-bracelist-p containing-sexp paren-state t))))
12558 (cond
12560 ;; CASE 9A: In the middle of a special brace list opener.
12561 ((and (consp special-brace-list)
12562 (save-excursion
12563 (goto-char containing-sexp)
12564 (eq (char-after) ?\())
12565 (eq char-after-ip (car (cdr special-brace-list))))
12566 (goto-char (car (car special-brace-list)))
12567 (skip-chars-backward " \t")
12568 (if (and (bolp)
12569 (assoc 'statement-cont
12570 (setq placeholder (c-guess-basic-syntax))))
12571 (setq c-syntactic-context placeholder)
12572 (c-beginning-of-statement-1
12573 (c-safe-position (1- containing-sexp) paren-state))
12574 (c-forward-token-2 0)
12575 (while (cond
12576 ((looking-at c-specifier-key)
12577 (c-forward-keyword-clause 1))
12578 ((and c-opt-cpp-prefix
12579 (looking-at c-noise-macro-with-parens-name-re))
12580 (c-forward-noise-clause))))
12581 (c-add-syntax 'brace-list-open (c-point 'boi))))
12583 ;; CASE 9B: brace-list-close brace
12584 ((if (consp special-brace-list)
12585 ;; Check special brace list closer.
12586 (progn
12587 (goto-char (car (car special-brace-list)))
12588 (save-excursion
12589 (goto-char indent-point)
12590 (back-to-indentation)
12592 ;; We were between the special close char and the `)'.
12593 (and (eq (char-after) ?\))
12594 (eq (1+ (point)) (cdr (car special-brace-list))))
12595 ;; We were before the special close char.
12596 (and (eq (char-after) (cdr (cdr special-brace-list)))
12597 (zerop (c-forward-token-2))
12598 (eq (1+ (point)) (cdr (car special-brace-list)))))))
12599 ;; Normal brace list check.
12600 (and (eq char-after-ip ?})
12601 (c-safe (goto-char (c-up-list-backward (point))) t)
12602 (= (point) containing-sexp)))
12603 (if (eq (point) (c-point 'boi))
12604 (c-add-syntax 'brace-list-close (point))
12605 (setq lim (c-most-enclosing-brace state-cache (point)))
12606 (c-beginning-of-statement-1 lim nil nil t)
12607 (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
12610 ;; Prepare for the rest of the cases below by going to the
12611 ;; token following the opening brace
12612 (if (consp special-brace-list)
12613 (progn
12614 (goto-char (car (car special-brace-list)))
12615 (c-forward-token-2 1 nil indent-point))
12616 (goto-char containing-sexp))
12617 (forward-char)
12618 (let ((start (point)))
12619 (c-forward-syntactic-ws indent-point)
12620 (goto-char (max start (c-point 'bol))))
12621 (c-skip-ws-forward indent-point)
12622 (cond
12624 ;; CASE 9C: we're looking at the first line in a brace-list
12625 ((= (point) indent-point)
12626 (if (consp special-brace-list)
12627 (goto-char (car (car special-brace-list)))
12628 (goto-char containing-sexp))
12629 (if (eq (point) (c-point 'boi))
12630 (c-add-syntax 'brace-list-intro (point))
12631 (setq lim (c-most-enclosing-brace state-cache (point)))
12632 (c-beginning-of-statement-1 lim)
12633 (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
12635 ;; CASE 9D: this is just a later brace-list-entry or
12636 ;; brace-entry-open
12637 (t (if (or (eq char-after-ip ?{)
12638 (and c-special-brace-lists
12639 (save-excursion
12640 (goto-char indent-point)
12641 (c-forward-syntactic-ws (c-point 'eol))
12642 (c-looking-at-special-brace-list (point)))))
12643 (c-add-syntax 'brace-entry-open (point))
12644 (c-add-stmt-syntax 'brace-list-entry nil t containing-sexp
12645 paren-state (point))
12647 ))))
12649 ;; CASE 10: A continued statement or top level construct.
12650 ((and (not (memq char-before-ip '(?\; ?:)))
12651 (not (c-at-vsemi-p before-ws-ip))
12652 (or (not (eq char-before-ip ?}))
12653 (c-looking-at-inexpr-block-backward state-cache))
12654 (> (point)
12655 (save-excursion
12656 (c-beginning-of-statement-1 containing-sexp)
12657 (setq placeholder (point))))
12658 (/= placeholder containing-sexp))
12659 ;; This is shared with case 18.
12660 (c-guess-continued-construct indent-point
12661 char-after-ip
12662 placeholder
12663 containing-sexp
12664 paren-state))
12666 ;; CASE 16: block close brace, possibly closing the defun or
12667 ;; the class
12668 ((eq char-after-ip ?})
12669 ;; From here on we have the next containing sexp in lim.
12670 (setq lim (c-most-enclosing-brace paren-state))
12671 (goto-char containing-sexp)
12672 (cond
12674 ;; CASE 16E: Closing a statement block? This catches
12675 ;; cases where it's preceded by a statement keyword,
12676 ;; which works even when used in an "invalid" context,
12677 ;; e.g. a macro argument.
12678 ((c-after-conditional)
12679 (c-backward-to-block-anchor lim)
12680 (c-add-stmt-syntax 'block-close nil t lim paren-state))
12682 ;; CASE 16A: closing a lambda defun or an in-expression
12683 ;; block? C.f. cases 4, 7B and 17E.
12684 ((setq placeholder (c-looking-at-inexpr-block
12685 (c-safe-position containing-sexp paren-state)
12686 nil))
12687 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
12688 'inline-close
12689 'block-close))
12690 (goto-char containing-sexp)
12691 (back-to-indentation)
12692 (if (= containing-sexp (point))
12693 (c-add-syntax tmpsymbol (point))
12694 (goto-char (cdr placeholder))
12695 (back-to-indentation)
12696 (c-add-stmt-syntax tmpsymbol nil t
12697 (c-most-enclosing-brace paren-state (point))
12698 paren-state)
12699 (if (/= (point) (cdr placeholder))
12700 (c-add-syntax (car placeholder)))))
12702 ;; CASE 16B: does this close an inline or a function in
12703 ;; a non-class declaration level block?
12704 ((save-excursion
12705 (and lim
12706 (progn
12707 (goto-char lim)
12708 (c-looking-at-decl-block
12709 (c-most-enclosing-brace paren-state lim)
12710 nil))
12711 (setq placeholder (point))))
12712 (c-backward-to-decl-anchor lim)
12713 (back-to-indentation)
12714 (if (save-excursion
12715 (goto-char placeholder)
12716 (looking-at c-other-decl-block-key))
12717 (c-add-syntax 'defun-close (point))
12718 (c-add-syntax 'inline-close (point))))
12720 ;; CASE 16F: Can be a defun-close of a function declared
12721 ;; in a statement block, e.g. in Pike or when using gcc
12722 ;; extensions, but watch out for macros followed by
12723 ;; blocks. Let it through to be handled below.
12724 ;; C.f. cases B.3 and 17G.
12725 ((save-excursion
12726 (and (not (c-at-statement-start-p))
12727 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
12728 (setq placeholder (point))
12729 (let ((c-recognize-typeless-decls nil))
12730 ;; Turn off recognition of constructs that
12731 ;; lacks a type in this case, since that's more
12732 ;; likely to be a macro followed by a block.
12733 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
12734 (back-to-indentation)
12735 (if (/= (point) containing-sexp)
12736 (goto-char placeholder))
12737 (c-add-stmt-syntax 'defun-close nil t lim paren-state))
12739 ;; CASE 16C: If there is an enclosing brace then this is
12740 ;; a block close since defun closes inside declaration
12741 ;; level blocks have been handled above.
12742 (lim
12743 ;; If the block is preceded by a case/switch label on
12744 ;; the same line, we anchor at the first preceding label
12745 ;; at boi. The default handling in c-add-stmt-syntax
12746 ;; really fixes it better, but we do like this to keep
12747 ;; the indentation compatible with version 5.28 and
12748 ;; earlier. C.f. case 17H.
12749 (while (and (/= (setq placeholder (point)) (c-point 'boi))
12750 (eq (c-beginning-of-statement-1 lim) 'label)))
12751 (goto-char placeholder)
12752 (if (looking-at c-label-kwds-regexp)
12753 (c-add-syntax 'block-close (point))
12754 (goto-char containing-sexp)
12755 ;; c-backward-to-block-anchor not necessary here; those
12756 ;; situations are handled in case 16E above.
12757 (c-add-stmt-syntax 'block-close nil t lim paren-state)))
12759 ;; CASE 16D: Only top level defun close left.
12761 (goto-char containing-sexp)
12762 (c-backward-to-decl-anchor lim)
12763 (c-add-stmt-syntax 'defun-close nil nil
12764 (c-most-enclosing-brace paren-state)
12765 paren-state))
12768 ;; CASE 19: line is an expression, not a statement, and is directly
12769 ;; contained by a template delimiter. Most likely, we are in a
12770 ;; template arglist within a statement. This case is based on CASE
12771 ;; 7. At some point in the future, we may wish to create more
12772 ;; syntactic symbols such as `template-intro',
12773 ;; `template-cont-nonempty', etc., and distinguish between them as we
12774 ;; do for `arglist-intro' etc. (2009-12-07).
12775 ((and c-recognize-<>-arglists
12776 (setq containing-< (c-up-list-backward indent-point containing-sexp))
12777 (eq (char-after containing-<) ?\<))
12778 (setq placeholder (c-point 'boi containing-<))
12779 (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
12780 ; '<') before indent-point.
12781 (if (>= (point) placeholder)
12782 (progn
12783 (forward-char)
12784 (skip-chars-forward " \t"))
12785 (goto-char placeholder))
12786 (c-add-stmt-syntax 'template-args-cont (list containing-<) t
12787 (c-most-enclosing-brace state-cache (point))
12788 paren-state))
12790 ;; CASE 17: Statement or defun catchall.
12792 (goto-char indent-point)
12793 ;; Back up statements until we find one that starts at boi.
12794 (while (let* ((prev-point (point))
12795 (last-step-type (c-beginning-of-statement-1
12796 containing-sexp)))
12797 (if (= (point) prev-point)
12798 (progn
12799 (setq step-type (or step-type last-step-type))
12800 nil)
12801 (setq step-type last-step-type)
12802 (/= (point) (c-point 'boi)))))
12803 (cond
12805 ;; CASE 17B: continued statement
12806 ((and (eq step-type 'same)
12807 (/= (point) indent-point))
12808 (c-add-stmt-syntax 'statement-cont nil nil
12809 containing-sexp paren-state))
12811 ;; CASE 17A: After a case/default label?
12812 ((progn
12813 (while (and (eq step-type 'label)
12814 (not (looking-at c-label-kwds-regexp)))
12815 (setq step-type
12816 (c-beginning-of-statement-1 containing-sexp)))
12817 (eq step-type 'label))
12818 (c-add-stmt-syntax (if (eq char-after-ip ?{)
12819 'statement-case-open
12820 'statement-case-intro)
12821 nil t containing-sexp paren-state))
12823 ;; CASE 17D: any old statement
12824 ((progn
12825 (while (eq step-type 'label)
12826 (setq step-type
12827 (c-beginning-of-statement-1 containing-sexp)))
12828 (eq step-type 'previous))
12829 (c-add-stmt-syntax 'statement nil t
12830 containing-sexp paren-state)
12831 (if (eq char-after-ip ?{)
12832 (c-add-syntax 'block-open)))
12834 ;; CASE 17I: Inside a substatement block.
12835 ((progn
12836 ;; The following tests are all based on containing-sexp.
12837 (goto-char containing-sexp)
12838 ;; From here on we have the next containing sexp in lim.
12839 (setq lim (c-most-enclosing-brace paren-state containing-sexp))
12840 (c-after-conditional))
12841 (c-backward-to-block-anchor lim)
12842 (c-add-stmt-syntax 'statement-block-intro nil t
12843 lim paren-state)
12844 (if (eq char-after-ip ?{)
12845 (c-add-syntax 'block-open)))
12847 ;; CASE 17E: first statement in an in-expression block.
12848 ;; C.f. cases 4, 7B and 16A.
12849 ((setq placeholder (c-looking-at-inexpr-block
12850 (c-safe-position containing-sexp paren-state)
12851 nil))
12852 (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
12853 'defun-block-intro
12854 'statement-block-intro))
12855 (back-to-indentation)
12856 (if (= containing-sexp (point))
12857 (c-add-syntax tmpsymbol (point))
12858 (goto-char (cdr placeholder))
12859 (back-to-indentation)
12860 (c-add-stmt-syntax tmpsymbol nil t
12861 (c-most-enclosing-brace state-cache (point))
12862 paren-state)
12863 (if (/= (point) (cdr placeholder))
12864 (c-add-syntax (car placeholder))))
12865 (if (eq char-after-ip ?{)
12866 (c-add-syntax 'block-open)))
12868 ;; CASE 17F: first statement in an inline, or first
12869 ;; statement in a top-level defun. we can tell this is it
12870 ;; if there are no enclosing braces that haven't been
12871 ;; narrowed out by a class (i.e. don't use bod here).
12872 ((save-excursion
12873 (or (not (setq placeholder (c-most-enclosing-brace
12874 paren-state)))
12875 (and (progn
12876 (goto-char placeholder)
12877 (eq (char-after) ?{))
12878 (c-looking-at-decl-block (c-most-enclosing-brace
12879 paren-state (point))
12880 nil))))
12881 (c-backward-to-decl-anchor lim)
12882 (back-to-indentation)
12883 (c-add-syntax 'defun-block-intro (point)))
12885 ;; CASE 17G: First statement in a function declared inside
12886 ;; a normal block. This can occur in Pike and with
12887 ;; e.g. the gcc extensions, but watch out for macros
12888 ;; followed by blocks. C.f. cases B.3 and 16F.
12889 ((save-excursion
12890 (and (not (c-at-statement-start-p))
12891 (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
12892 (setq placeholder (point))
12893 (let ((c-recognize-typeless-decls nil))
12894 ;; Turn off recognition of constructs that lacks
12895 ;; a type in this case, since that's more likely
12896 ;; to be a macro followed by a block.
12897 (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
12898 (back-to-indentation)
12899 (if (/= (point) containing-sexp)
12900 (goto-char placeholder))
12901 (c-add-stmt-syntax 'defun-block-intro nil t
12902 lim paren-state))
12904 ;; CASE 17H: First statement in a block.
12906 ;; If the block is preceded by a case/switch label on the
12907 ;; same line, we anchor at the first preceding label at
12908 ;; boi. The default handling in c-add-stmt-syntax is
12909 ;; really fixes it better, but we do like this to keep the
12910 ;; indentation compatible with version 5.28 and earlier.
12911 ;; C.f. case 16C.
12912 (while (and (/= (setq placeholder (point)) (c-point 'boi))
12913 (eq (c-beginning-of-statement-1 lim) 'label)))
12914 (goto-char placeholder)
12915 (if (looking-at c-label-kwds-regexp)
12916 (c-add-syntax 'statement-block-intro (point))
12917 (goto-char containing-sexp)
12918 ;; c-backward-to-block-anchor not necessary here; those
12919 ;; situations are handled in case 17I above.
12920 (c-add-stmt-syntax 'statement-block-intro nil t
12921 lim paren-state))
12922 (if (eq char-after-ip ?{)
12923 (c-add-syntax 'block-open)))
12927 ;; now we need to look at any modifiers
12928 (goto-char indent-point)
12929 (skip-chars-forward " \t")
12931 ;; are we looking at a comment only line?
12932 (when (and (looking-at c-comment-start-regexp)
12933 (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
12934 (c-append-syntax 'comment-intro))
12936 ;; we might want to give additional offset to friends (in C++).
12937 (when (and c-opt-friend-key
12938 (looking-at c-opt-friend-key))
12939 (c-append-syntax 'friend))
12941 ;; Set syntactic-relpos.
12942 (let ((p c-syntactic-context))
12943 (while (and p
12944 (if (integerp (c-langelem-pos (car p)))
12945 (progn
12946 (setq syntactic-relpos (c-langelem-pos (car p)))
12947 nil)
12949 (setq p (cdr p))))
12951 ;; Start of or a continuation of a preprocessor directive?
12952 (if (and macro-start
12953 (eq macro-start (c-point 'boi))
12954 (not (and (c-major-mode-is 'pike-mode)
12955 (eq (char-after (1+ macro-start)) ?\"))))
12956 (c-append-syntax 'cpp-macro)
12957 (when (and c-syntactic-indentation-in-macros macro-start)
12958 (if in-macro-expr
12959 (when (or
12960 (< syntactic-relpos macro-start)
12961 (not (or
12962 (assq 'arglist-intro c-syntactic-context)
12963 (assq 'arglist-cont c-syntactic-context)
12964 (assq 'arglist-cont-nonempty c-syntactic-context)
12965 (assq 'arglist-close c-syntactic-context))))
12966 ;; If inside a cpp expression, i.e. anywhere in a
12967 ;; cpp directive except a #define body, we only let
12968 ;; through the syntactic analysis that is internal
12969 ;; in the expression. That means the arglist
12970 ;; elements, if they are anchored inside the cpp
12971 ;; expression.
12972 (setq c-syntactic-context nil)
12973 (c-add-syntax 'cpp-macro-cont macro-start))
12974 (when (and (eq macro-start syntactic-relpos)
12975 (not (assq 'cpp-define-intro c-syntactic-context))
12976 (save-excursion
12977 (goto-char macro-start)
12978 (or (not (c-forward-to-cpp-define-body))
12979 (<= (point) (c-point 'boi indent-point)))))
12980 ;; Inside a #define body and the syntactic analysis is
12981 ;; anchored on the start of the #define. In this case
12982 ;; we add cpp-define-intro to get the extra
12983 ;; indentation of the #define body.
12984 (c-add-syntax 'cpp-define-intro)))))
12986 ;; return the syntax
12987 c-syntactic-context)))
12990 ;; Indentation calculation.
12992 (defun c-evaluate-offset (offset langelem symbol)
12993 ;; offset can be a number, a function, a variable, a list, or one of
12994 ;; the symbols + or -
12996 ;; This function might do hidden buffer changes.
12997 (let ((res
12998 (cond
12999 ((numberp offset) offset)
13000 ((vectorp offset) offset)
13001 ((null offset) nil)
13003 ((eq offset '+) c-basic-offset)
13004 ((eq offset '-) (- c-basic-offset))
13005 ((eq offset '++) (* 2 c-basic-offset))
13006 ((eq offset '--) (* 2 (- c-basic-offset)))
13007 ((eq offset '*) (/ c-basic-offset 2))
13008 ((eq offset '/) (/ (- c-basic-offset) 2))
13010 ((functionp offset)
13011 (c-evaluate-offset
13012 (funcall offset
13013 (cons (c-langelem-sym langelem)
13014 (c-langelem-pos langelem)))
13015 langelem symbol))
13017 ((listp offset)
13018 (cond
13019 ((eq (car offset) 'quote)
13020 (c-benign-error "The offset %S for %s was mistakenly quoted"
13021 offset symbol)
13022 nil)
13024 ((memq (car offset) '(min max))
13025 (let (res val (method (car offset)))
13026 (setq offset (cdr offset))
13027 (while offset
13028 (setq val (c-evaluate-offset (car offset) langelem symbol))
13029 (cond
13030 ((not val))
13031 ((not res)
13032 (setq res val))
13033 ((integerp val)
13034 (if (vectorp res)
13035 (c-benign-error "\
13036 Error evaluating offset %S for %s: \
13037 Cannot combine absolute offset %S with relative %S in `%s' method"
13038 (car offset) symbol res val method)
13039 (setq res (funcall method res val))))
13041 (if (integerp res)
13042 (c-benign-error "\
13043 Error evaluating offset %S for %s: \
13044 Cannot combine relative offset %S with absolute %S in `%s' method"
13045 (car offset) symbol res val method)
13046 (setq res (vector (funcall method (aref res 0)
13047 (aref val 0)))))))
13048 (setq offset (cdr offset)))
13049 res))
13051 ((eq (car offset) 'add)
13052 (let (res val)
13053 (setq offset (cdr offset))
13054 (while offset
13055 (setq val (c-evaluate-offset (car offset) langelem symbol))
13056 (cond
13057 ((not val))
13058 ((not res)
13059 (setq res val))
13060 ((integerp val)
13061 (if (vectorp res)
13062 (setq res (vector (+ (aref res 0) val)))
13063 (setq res (+ res val))))
13065 (if (vectorp res)
13066 (c-benign-error "\
13067 Error evaluating offset %S for %s: \
13068 Cannot combine absolute offsets %S and %S in `add' method"
13069 (car offset) symbol res val)
13070 (setq res val)))) ; Override.
13071 (setq offset (cdr offset)))
13072 res))
13075 (let (res)
13076 (when (eq (car offset) 'first)
13077 (setq offset (cdr offset)))
13078 (while (and (not res) offset)
13079 (setq res (c-evaluate-offset (car offset) langelem symbol)
13080 offset (cdr offset)))
13081 res))))
13083 ((and (symbolp offset) (boundp offset))
13084 (symbol-value offset))
13087 (c-benign-error "Unknown offset format %S for %s" offset symbol)
13088 nil))))
13090 (if (or (null res) (integerp res)
13091 (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
13093 (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
13094 offset symbol res)
13095 nil)))
13097 (defun c-calc-offset (langelem)
13098 ;; Get offset from LANGELEM which is a list beginning with the
13099 ;; syntactic symbol and followed by any analysis data it provides.
13100 ;; That data may be zero or more elements, but if at least one is
13101 ;; given then the first is the anchor position (or nil). The symbol
13102 ;; is matched against `c-offsets-alist' and the offset calculated
13103 ;; from that is returned.
13105 ;; This function might do hidden buffer changes.
13106 (let* ((symbol (c-langelem-sym langelem))
13107 (match (assq symbol c-offsets-alist))
13108 (offset (cdr-safe match)))
13109 (if match
13110 (setq offset (c-evaluate-offset offset langelem symbol))
13111 (if c-strict-syntax-p
13112 (c-benign-error "No offset found for syntactic symbol %s" symbol))
13113 (setq offset 0))
13114 (if (vectorp offset)
13115 offset
13116 (or (and (numberp offset) offset)
13117 (and (symbolp offset) (symbol-value offset))
13121 (defun c-get-offset (langelem)
13122 ;; This is a compatibility wrapper for `c-calc-offset' in case
13123 ;; someone is calling it directly. It takes an old style syntactic
13124 ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
13125 ;; new list form.
13127 ;; This function might do hidden buffer changes.
13128 (if (c-langelem-pos langelem)
13129 (c-calc-offset (list (c-langelem-sym langelem)
13130 (c-langelem-pos langelem)))
13131 (c-calc-offset langelem)))
13133 (defun c-get-syntactic-indentation (langelems)
13134 ;; Calculate the syntactic indentation from a syntactic description
13135 ;; as returned by `c-guess-syntax'.
13137 ;; Note that topmost-intro always has an anchor position at bol, for
13138 ;; historical reasons. It's often used together with other symbols
13139 ;; that have more sane positions. Since we always use the first
13140 ;; found anchor position, we rely on that these other symbols always
13141 ;; precede topmost-intro in the LANGELEMS list.
13143 ;; This function might do hidden buffer changes.
13144 (let ((indent 0) anchor)
13146 (while langelems
13147 (let* ((c-syntactic-element (car langelems))
13148 (res (c-calc-offset c-syntactic-element)))
13150 (if (vectorp res)
13151 ;; Got an absolute column that overrides any indentation
13152 ;; we've collected so far, but not the relative
13153 ;; indentation we might get for the nested structures
13154 ;; further down the langelems list.
13155 (setq indent (elt res 0)
13156 anchor (point-min)) ; A position at column 0.
13158 ;; Got a relative change of the current calculated
13159 ;; indentation.
13160 (setq indent (+ indent res))
13162 ;; Use the anchor position from the first syntactic
13163 ;; element with one.
13164 (unless anchor
13165 (setq anchor (c-langelem-pos (car langelems)))))
13167 (setq langelems (cdr langelems))))
13169 (if anchor
13170 (+ indent (save-excursion
13171 (goto-char anchor)
13172 (current-column)))
13173 indent)))
13176 (cc-provide 'cc-engine)
13178 ;; Local Variables:
13179 ;; indent-tabs-mode: t
13180 ;; tab-width: 8
13181 ;; End:
13182 ;;; cc-engine.el ends here