all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Daniel Colascione <dancol@dancol.org>
To: Emacs developers <emacs-devel@gnu.org>
Cc: Alan Mackenzie <acm@muc.de>
Subject: cc-mode uniform initialization support
Date: Wed, 19 Aug 2015 11:36:11 -0700	[thread overview]
Message-ID: <55D4CC9B.7020302@dancol.org> (raw)

[-- 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 --]

             reply	other threads:[~2015-08-19 18:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-19 18:36 Daniel Colascione [this message]
2015-08-20 16:49 ` cc-mode uniform initialization support Alan Mackenzie
2015-08-30  0:54   ` Daniel Colascione
2016-07-04 18:32 ` Alan Mackenzie
2016-07-22 21:09   ` Alan Mackenzie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=55D4CC9B.7020302@dancol.org \
    --to=dancol@dancol.org \
    --cc=acm@muc.de \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.