[PATCH] D65043: [Format] Add C++20 standard to style options

Brian Gesiak via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 20 17:35:31 PDT 2019


modocache created this revision.
modocache added reviewers: rsmith, sammccall, Typz, klimek.
Herald added a project: clang.

When C++ coroutines were adopted as part of the C++20 standard, a change
was committed in https://github.com/llvm/llvm-project/commit/10ab78e854f:
coroutine keywords such as `co_yield` went from being gated on the
presence of the `-fcoroutines-ts` flag, to instead being gated on
`-std=c++2a`. This resulted, perhaps unexpectedly to some users, in a
change in how coroutine keywords were formatted. Because libclangFormat
has only 3 options for formatting according to a language standard --
C++03, C++11, or "auto" -- and because it enabled C++20 keywords for all
settings aside from C++03, users who specified a standard of C++11 in
their style options would have their C++ formatted as if `co_yield` were
a keyword:

- Before, C++03: `co_yield ++i` would be formatted as `co_yield++ i`
- Before, C++11: `co_yield ++i` would be formatted as `co_yield++ i`
- After, C++03: `co_yield ++i` would be formatted as `co_yield++ i`
- After, C++11: `co_yield ++i` would be formatted as `co_yield ++i`

Although the "after" examples above appear like correct formatting
choices to those who are used to seeing coroutine keywords, I would
argue that they aren't technically correct, because a user may define a
variable in C++11 named `co_yield`, and they could increment that
variable by typing `co_yield++`. In this case, clang-format would change
the formatting, despite the user never opting-in to treating `co_yield`
as a keyword.

(There are other examples of clang-format suddenly formatting C++11 code
according to C++20 standards differently as a result of changes like
https://github.com/llvm/llvm-project/commit/10ab78e854f, and I've included
them as tests in this commit, but I won't go into detail explaining them
here.)

To give users the option of formatting according to the C++11 standard,
without the use of coroutines, I've added a style option for the C++20
standard (and, similarly to how the C++11 standard enables C++14, 17,
and 1z, I've written the documentation to indicate using the C++20
option enables any future C++2a standards).

In a future commit, I add a boolean style option to enable coroutines,
so that users may specify they wish to format according to the C++11
standard, but with coroutine keywords enabled.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D65043

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65043.210978.patch
Type: text/x-patch
Size: 5435 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190721/1447f353/attachment.bin>


More information about the cfe-commits mailing list