diff options
| author | Joakim Verona | 2012-01-23 15:10:06 +0100 |
|---|---|---|
| committer | Joakim Verona | 2012-01-23 15:10:06 +0100 |
| commit | 0322b140eead7c94de7f0f6d19a90bd15690b4eb (patch) | |
| tree | 950c011783cc896d0450084cb5155e54548bfe5b /lisp/progmodes/cc-engine.el | |
| parent | d5114bfea3ea4c37c57e2af0f3b095be9fcd8bac (diff) | |
| parent | cb5850f27c1b4d26957d58e2da2314dd12498671 (diff) | |
| download | emacs-0322b140eead7c94de7f0f6d19a90bd15690b4eb.tar.gz emacs-0322b140eead7c94de7f0f6d19a90bd15690b4eb.zip | |
upstream
Diffstat (limited to 'lisp/progmodes/cc-engine.el')
| -rw-r--r-- | lisp/progmodes/cc-engine.el | 143 |
1 files changed, 125 insertions, 18 deletions
diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el index 2e0294341da..25344fe96a7 100644 --- a/lisp/progmodes/cc-engine.el +++ b/lisp/progmodes/cc-engine.el | |||
| @@ -2074,7 +2074,7 @@ comment at the start of cc-engine.el for more info." | |||
| 2074 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 2074 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2075 | ;; We maintain a simple cache of positions which aren't in a literal, so as to | 2075 | ;; We maintain a simple cache of positions which aren't in a literal, so as to |
| 2076 | ;; speed up testing for non-literality. | 2076 | ;; speed up testing for non-literality. |
| 2077 | (defconst c-state-nonlit-pos-interval 10000) | 2077 | (defconst c-state-nonlit-pos-interval 3000) |
| 2078 | ;; The approximate interval between entries in `c-state-nonlit-pos-cache'. | 2078 | ;; The approximate interval between entries in `c-state-nonlit-pos-cache'. |
| 2079 | 2079 | ||
| 2080 | (defvar c-state-nonlit-pos-cache nil) | 2080 | (defvar c-state-nonlit-pos-cache nil) |
| @@ -2129,7 +2129,7 @@ comment at the start of cc-engine.el for more info." | |||
| 2129 | (widen) | 2129 | (widen) |
| 2130 | (save-excursion | 2130 | (save-excursion |
| 2131 | (let ((c c-state-nonlit-pos-cache) | 2131 | (let ((c c-state-nonlit-pos-cache) |
| 2132 | pos npos lit) | 2132 | pos npos lit macro-beg) |
| 2133 | ;; Trim the cache to take account of buffer changes. | 2133 | ;; Trim the cache to take account of buffer changes. |
| 2134 | (while (and c (> (car c) c-state-nonlit-pos-cache-limit)) | 2134 | (while (and c (> (car c) c-state-nonlit-pos-cache-limit)) |
| 2135 | (setq c (cdr c))) | 2135 | (setq c (cdr c))) |
| @@ -2139,16 +2139,32 @@ comment at the start of cc-engine.el for more info." | |||
| 2139 | (setq c (cdr c))) | 2139 | (setq c (cdr c))) |
| 2140 | (setq pos (or (car c) (point-min))) | 2140 | (setq pos (or (car c) (point-min))) |
| 2141 | 2141 | ||
| 2142 | (while (<= (setq npos (+ pos c-state-nonlit-pos-interval)) | 2142 | (while |
| 2143 | here) | 2143 | ;; Add an element to `c-state-nonlit-pos-cache' each iteration. |
| 2144 | (setq lit (car (cddr (c-state-pp-to-literal pos npos)))) | 2144 | (and |
| 2145 | (setq pos (or (cdr lit) npos)) ; end of literal containing npos. | 2145 | (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here) |
| 2146 | (progn | ||
| 2147 | (setq lit (car (cddr (c-state-pp-to-literal pos npos)))) | ||
| 2148 | (cond | ||
| 2149 | ((null lit) | ||
| 2150 | (setq pos npos) | ||
| 2151 | t) | ||
| 2152 | ((<= (cdr lit) here) | ||
| 2153 | (setq pos (cdr lit)) | ||
| 2154 | t) | ||
| 2155 | (t | ||
| 2156 | (setq pos (car lit)) | ||
| 2157 | nil)))) | ||
| 2158 | |||
| 2146 | (goto-char pos) | 2159 | (goto-char pos) |
| 2147 | (when (and (c-beginning-of-macro) (/= (point) pos)) | 2160 | (when (and (c-beginning-of-macro) (/= (point) pos)) |
| 2148 | (c-syntactic-end-of-macro) | 2161 | (setq macro-beg (point)) |
| 2149 | (or (eobp) (forward-char)) | 2162 | (c-syntactic-end-of-macro) |
| 2150 | (setq pos (point))) | 2163 | (or (eobp) (forward-char)) |
| 2151 | (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache))) | 2164 | (setq pos (if (<= (point) here) |
| 2165 | (point) | ||
| 2166 | macro-beg))) | ||
| 2167 | (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache))) | ||
| 2152 | 2168 | ||
| 2153 | (if (> pos c-state-nonlit-pos-cache-limit) | 2169 | (if (> pos c-state-nonlit-pos-cache-limit) |
| 2154 | (setq c-state-nonlit-pos-cache-limit pos)) | 2170 | (setq c-state-nonlit-pos-cache-limit pos)) |
| @@ -4351,6 +4367,78 @@ comment at the start of cc-engine.el for more info." | |||
| 4351 | (t 'c))) ; Assuming the range is valid. | 4367 | (t 'c))) ; Assuming the range is valid. |
| 4352 | range)) | 4368 | range)) |
| 4353 | 4369 | ||
| 4370 | (defsubst c-determine-limit-get-base (start try-size) | ||
| 4371 | ;; Get a "safe place" approximately TRY-SIZE characters before START. | ||
| 4372 | ;; This doesn't preserve point. | ||
| 4373 | (let* ((pos (max (- start try-size) (point-min))) | ||
| 4374 | (base (c-state-safe-place pos)) | ||
| 4375 | (s (parse-partial-sexp base pos))) | ||
| 4376 | (if (or (nth 4 s) (nth 3 s)) ; comment or string | ||
| 4377 | (nth 8 s) | ||
| 4378 | (point)))) | ||
| 4379 | |||
| 4380 | (defun c-determine-limit (how-far-back &optional start try-size) | ||
| 4381 | ;; Return a buffer position HOW-FAR-BACK non-literal characters from START | ||
| 4382 | ;; (default point). This is done by going back further in the buffer then | ||
| 4383 | ;; searching forward for literals. The position found won't be in a | ||
| 4384 | ;; literal. We start searching for the sought position TRY-SIZE (default | ||
| 4385 | ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast. | ||
| 4386 | ;; :-) | ||
| 4387 | (save-excursion | ||
| 4388 | (let* ((start (or start (point))) | ||
| 4389 | (try-size (or try-size (* 2 how-far-back))) | ||
| 4390 | (base (c-determine-limit-get-base start try-size)) | ||
| 4391 | (pos base) | ||
| 4392 | |||
| 4393 | (s (parse-partial-sexp pos pos)) ; null state. | ||
| 4394 | stack elt size | ||
| 4395 | (count 0)) | ||
| 4396 | (while (< pos start) | ||
| 4397 | ;; Move forward one literal each time round this loop. | ||
| 4398 | ;; Move forward to the start of a comment or string. | ||
| 4399 | (setq s (parse-partial-sexp | ||
| 4400 | pos | ||
| 4401 | start | ||
| 4402 | nil ; target-depth | ||
| 4403 | nil ; stop-before | ||
| 4404 | s ; state | ||
| 4405 | 'syntax-table)) ; stop-comment | ||
| 4406 | |||
| 4407 | ;; Gather details of the non-literal-bit - starting pos and size. | ||
| 4408 | (setq size (- (if (or (nth 4 s) (nth 3 s)) | ||
| 4409 | (nth 8 s) | ||
| 4410 | (point)) | ||
| 4411 | pos)) | ||
| 4412 | (if (> size 0) | ||
| 4413 | (setq stack (cons (cons pos size) stack))) | ||
| 4414 | |||
| 4415 | ;; Move forward to the end of the comment/string. | ||
| 4416 | (if (or (nth 4 s) (nth 3 s)) | ||
| 4417 | (setq s (parse-partial-sexp | ||
| 4418 | (point) | ||
| 4419 | start | ||
| 4420 | nil ; target-depth | ||
| 4421 | nil ; stop-before | ||
| 4422 | s ; state | ||
| 4423 | 'syntax-table))) ; stop-comment | ||
| 4424 | (setq pos (point))) | ||
| 4425 | |||
| 4426 | ;; Now try and find enough non-literal characters recorded on the stack. | ||
| 4427 | ;; Go back one recorded literal each time round this loop. | ||
| 4428 | (while (and (< count how-far-back) | ||
| 4429 | stack) | ||
| 4430 | (setq elt (car stack) | ||
| 4431 | stack (cdr stack)) | ||
| 4432 | (setq count (+ count (cdr elt)))) | ||
| 4433 | |||
| 4434 | ;; Have we found enough yet? | ||
| 4435 | (cond | ||
| 4436 | ((>= count how-far-back) | ||
| 4437 | (+ (car elt) (- count how-far-back))) | ||
| 4438 | ((eq base (point-min)) | ||
| 4439 | (point-min)) | ||
| 4440 | (t | ||
| 4441 | (c-determine-limit (- how-far-back count) base try-size)))))) | ||
| 4354 | 4442 | ||
| 4355 | ;; `c-find-decl-spots' and accompanying stuff. | 4443 | ;; `c-find-decl-spots' and accompanying stuff. |
| 4356 | 4444 | ||
| @@ -4487,13 +4575,14 @@ comment at the start of cc-engine.el for more info." | |||
| 4487 | ;; Call CFD-FUN for each possible spot for a declaration, cast or | 4575 | ;; Call CFD-FUN for each possible spot for a declaration, cast or |
| 4488 | ;; label from the point to CFD-LIMIT. | 4576 | ;; label from the point to CFD-LIMIT. |
| 4489 | ;; | 4577 | ;; |
| 4490 | ;; CFD-FUN is called with point at the start of the spot. It's | 4578 | ;; CFD-FUN is called with point at the start of the spot. It's passed two |
| 4491 | ;; passed two arguments: The first is the end position of the token | 4579 | ;; arguments: The first is the end position of the token preceding the spot, |
| 4492 | ;; preceding the spot, or 0 for the implicit match at bob. The | 4580 | ;; or 0 for the implicit match at bob. The second is a flag that is t when |
| 4493 | ;; second is a flag that is t when the match is inside a macro. If | 4581 | ;; the match is inside a macro. Point should be moved forward by at least |
| 4494 | ;; CFD-FUN adds `c-decl-end' properties somewhere below the current | 4582 | ;; one token. |
| 4495 | ;; spot, it should return non-nil to ensure that the next search | 4583 | ;; |
| 4496 | ;; will find them. | 4584 | ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot, |
| 4585 | ;; it should return non-nil to ensure that the next search will find them. | ||
| 4497 | ;; | 4586 | ;; |
| 4498 | ;; Such a spot is: | 4587 | ;; Such a spot is: |
| 4499 | ;; o The first token after bob. | 4588 | ;; o The first token after bob. |
| @@ -4867,7 +4956,8 @@ comment at the start of cc-engine.el for more info." | |||
| 4867 | (goto-char cfd-continue-pos) | 4956 | (goto-char cfd-continue-pos) |
| 4868 | (if (= cfd-continue-pos cfd-limit) | 4957 | (if (= cfd-continue-pos cfd-limit) |
| 4869 | (setq cfd-match-pos cfd-limit) | 4958 | (setq cfd-match-pos cfd-limit) |
| 4870 | (c-find-decl-prefix-search))))) | 4959 | (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos, |
| 4960 | ; cfd-match-pos, etc. | ||
| 4871 | 4961 | ||
| 4872 | 4962 | ||
| 4873 | ;; A cache for found types. | 4963 | ;; A cache for found types. |
| @@ -8047,6 +8137,23 @@ comment at the start of cc-engine.el for more info." | |||
| 8047 | next-open-brace (c-pull-open-brace paren-state))) | 8137 | next-open-brace (c-pull-open-brace paren-state))) |
| 8048 | open-brace)) | 8138 | open-brace)) |
| 8049 | 8139 | ||
| 8140 | (defun c-cheap-inside-bracelist-p (paren-state) | ||
| 8141 | ;; Return the position of the L-brace if point is inside a brace list | ||
| 8142 | ;; initialization of an array, etc. This is an approximate function, | ||
| 8143 | ;; designed for speed over accuracy. It will not find every bracelist, but | ||
| 8144 | ;; a non-nil result is reliable. We simply search for "= {" (naturally with | ||
| 8145 | ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it | ||
| 8146 | ;; is everywhere else. | ||
| 8147 | (let (b-pos) | ||
| 8148 | (save-excursion | ||
| 8149 | (while | ||
| 8150 | (and (setq b-pos (c-pull-open-brace paren-state)) | ||
| 8151 | (progn (goto-char b-pos) | ||
| 8152 | (c-backward-sws) | ||
| 8153 | (c-backward-token-2) | ||
| 8154 | (not (looking-at "="))))) | ||
| 8155 | b-pos))) | ||
| 8156 | |||
| 8050 | (defun c-inside-bracelist-p (containing-sexp paren-state) | 8157 | (defun c-inside-bracelist-p (containing-sexp paren-state) |
| 8051 | ;; return the buffer position of the beginning of the brace list | 8158 | ;; return the buffer position of the beginning of the brace list |
| 8052 | ;; statement if we're inside a brace list, otherwise return nil. | 8159 | ;; statement if we're inside a brace list, otherwise return nil. |