unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* cc-mode uniform initialization support
@ 2015-08-19 18:36 Daniel Colascione
  2015-08-20 16:49 ` Alan Mackenzie
  2016-07-04 18:32 ` Alan Mackenzie
  0 siblings, 2 replies; 5+ messages in thread
From: Daniel Colascione @ 2015-08-19 18:36 UTC (permalink / raw)
  To: Emacs developers; +Cc: Alan Mackenzie

[-- Attachment #1: Type: text/plain, Size: 2699 bytes --]

Recent versions of C++ support using {} to indicate an initialization
value. For example,

  1 int foo{5};
  2 int bar = {5};
  3 mumble_t mumble{
  4  foo,
  5    bar
  6 };
  7 return {mumble, foo}; // Type inferred from function declaration

Working with code written in this style is a nightmare right now because
cc-mode recognizes line 4 as defun-block-intro, leading to line 5 being
indented spuriously, since cc-mode thinks it's a statement continuation.
In reality, the construct starting at line 3 is a brace list, but
there's no syntactic clue to tell us that. (We can, of course, look for
an "=", but an "=" is no longer required for a brace list.)

I don't see a way around needing to use awful heuristics to distinguish
the cases. Does anyone have a better idea? Keep in mind that code like
this is common too:

  foo({
    abc,
    def
  });

And plenty of people define macros that accept blocks:

  MY_FOREACH {
    statement1;
    statement2;
  }

diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
index 06b03a2..84f3ad7 100644
--- a/lisp/progmodes/cc-engine.el
+++ b/lisp/progmodes/cc-engine.el
@@ -8725,6 +8725,34 @@ comment at the start of cc-engine.el for more info."
    (save-excursion
      (goto-char containing-sexp)
      (c-backward-over-enum-header))
+   ;; Detect C++11 uniform initialization.  This bit is necessarily
+   ;; heuristic, since there are few syntactic clues to go
+   ;; on. Initializer lists can now stand in for any value at all when
+   ;; the compiler can infer the type to build from the
+   ;; initializer list.
+   (save-excursion
+     (goto-char containing-sexp)
+     (and
+      (eq (char-after) ?\{)
+      (progn (c-backward-sws)
+            (or
+             ;; After a comma means universal initialization
+             (eq (char-before) ?\,)
+             ;; Er, we use GCC statement expressions most often in
+             ;; macros, so if we see ({ outside of one, think of it
+             ;; as uniform initialization.
+             (and (eq (char-before) ?\()
+                  (save-excursion
+                    (not (c-beginning-of-macro))))
+             ;; foo{} versus foo {. Yuck.
+             (and (not (bobp))
+                  (prog1
+                      (backward-char)
+                    (looking-at "\\(\\w\\|\\s_\\){")
+                    (forward-char)))
+             ;; Special case for return {...}
+             (looking-back "\\_<return\\=" (point-at-bol))))
+      containing-sexp))
    ;; this will pick up array/aggregate init lists, even if they are
nested.
    (save-excursion
      (let ((class-key


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-07-22 21:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-19 18:36 cc-mode uniform initialization support Daniel Colascione
2015-08-20 16:49 ` Alan Mackenzie
2015-08-30  0:54   ` Daniel Colascione
2016-07-04 18:32 ` Alan Mackenzie
2016-07-22 21:09   ` Alan Mackenzie

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).