<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{mso-style-priority:34;
margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.5in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
@list l0
{mso-list-id:1755855993;
mso-list-type:hybrid;
mso-list-template-ids:794047764 1749074866 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l0:level1
{mso-level-text:%1-;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level2
{mso-level-number-format:alpha-lower;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level3
{mso-level-number-format:roman-lower;
mso-level-tab-stop:none;
mso-level-number-position:right;
text-indent:-9.0pt;}
@list l0:level4
{mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level5
{mso-level-number-format:alpha-lower;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level6
{mso-level-number-format:roman-lower;
mso-level-tab-stop:none;
mso-level-number-position:right;
text-indent:-9.0pt;}
@list l0:level7
{mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level8
{mso-level-number-format:alpha-lower;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level9
{mso-level-number-format:roman-lower;
mso-level-tab-stop:none;
mso-level-number-position:right;
text-indent:-9.0pt;}
ol
{margin-bottom:0in;}
ul
{margin-bottom:0in;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">My thoughts (as one of the people who pushed for this RFC) are that we have a few options:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<ol style="margin-top:0in" start="1" type="1">
<li class="MsoListParagraph" style="margin-left:0in;mso-list:l0 level1 lfo1">Clang Format is NEVER allowed to break code. This is already violated with the reordering of includes.<o:p></o:p></li><li class="MsoListParagraph" style="margin-left:0in;mso-list:l0 level1 lfo1">Clang Format should be able to break code at will, and we should publish this fact.<o:p></o:p></li><li class="MsoListParagraph" style="margin-left:0in;mso-list:l0 level1 lfo1">Clang Format should strive to not break code, but can make exceptions for good features when the examples are quite contrived, and the patch makes an as-good-as-reasonable attempt
to avoid those breakages.<o:p></o:p></li></ol>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I personally LIKE #1 in theory, but the ship has sailed, and it is at the expense of GOOD formatting options.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I personally HATE #2, I think it makes the tool unusable as a formatting tool.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I personally quite like #3, and believe it should be the policy going forward (which I believe is the RIGHT outcome of this RFC). I also believe the patch for left/right const already falls into this category, and thus would be generally
acceptable to me (though, I think the include-reordering distinctly does NOT).<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">-Erich <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> cfe-dev <cfe-dev-bounces@lists.llvm.org> <b>On Behalf Of
</b>MyDeveloper Day via cfe-dev<br>
<b>Sent:</b> Monday, August 9, 2021 12:36 PM<br>
<b>To:</b> clang developer list <cfe-dev@lists.llvm.org>; llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> [cfe-dev] [RFC] Adding support for clang-format making further code modifying changes<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">Hi all,<br>
<br>
As a frequent user and maintainer of clang-format I would like to formalize a change to the "charter" of what clang-format can/could/should do<br>
<br>
Motivation<br>
==========<br>
<br>
As you all know clang-format parses source code and can manipulate the tokens to produce consistently formatted code (most of the time), its become ubiquitous in the industry and its integration into
<br>
popular editors & IDEs such as vim/visual studio/code mean it very simple for users to get setup and running producing good looking code.<br>
<br>
clang-format does not use semantic information, and as such it doesn't need includes, defines or compiler flags to interpret code. Clang-format is generally guessing that certain sequences of tokens from the lexer represent certain patterns. It's a good guess
but it gets it wrong from time to time, hence trading correctness for performance.<br>
<br>
Because of this clang-format is fast (not maybe as fast as we'd like) but fast enough to be part of in a "save" operation such that the code is formatted as the ide saves your work.<br>
<br>
Mostly clang-format manipulates only whitespace, but over the years there have been a number of extremely useful features that have broken this rule, namely include sorting, namespace commenting to name a few.<br>
<br>
The usage scenario of clang-format has also changed slightly to include a non modifying advisory role identifying clang-format violations (as in our own llvm-premerge checks), which can greatly aid the code review process by removing the need to constantly
ask for the code to be formatted correctly or follow the LLVM convention.<br>
<br>
Recently a number of new features have been proposed that would also alter code, insertion of braces, east/west const conversion that can be performed at "save" speeds.<br>
<br>
As no semantic information is present, this raises the question as to whether clang-format could actually break your code.
<br>
This has actually always been the case especially since the introduction of include sorting, but also we all know that clang-format can break your code from the visual perspective too and hence the need for // clang-format off/on<br>
<br>
In the most part include sorting not only might break your code noisily such that it won't compile, but it can also break it silently,
<br>
and because IncludeSorting is on by default this breakage could potentially go unnoticed.<br>
<br>
<a href="https://stackoverflow.com/questions/37927553/can-clang-format-break-my-code">https://stackoverflow.com/questions/37927553/can-clang-format-break-my-code</a><br>
<a href="https://travisdowns.github.io/blog/2019/11/19/toupper.html">https://travisdowns.github.io/blog/2019/11/19/toupper.html</a><br>
<br>
I don't think it can be in any doubt that IncludeSorting is a nice feature used by 100,000's of developers without too many issues, but there is some suggestion that its inclusion as "on by default" was potentially a mistake.<br>
<br>
Proposals for other new features that modify the code in a similar way are getting some push back for changing the "charter" of clang-format when it could be considered to have already changed.<br>
This is causing friction on the review of some features and so it was suggested to present an RFC to gain wider consensus on the concept of clang-format changing code.<br>
<br>
Mostly when a code modifying change is submitted the view is that this isn't clang-formats job but more clang-tidy, however clang-tidy requires full access to all include files and compiler definitions and only works on the preprocessor paths of the code you
are analyzing for and its speed and hence its frequency of use is drastically reduced.
<br>
<br>
Some clang-format based modifications can in theory be made with a relatively high level of confidence without paying the price and the configuration complexity of getting all the
<br>
semantic information. <a href="https://reviews.llvm.org/D105701">https://reviews.llvm.org/D105701</a>.<o:p></o:p></p>
<div>
<p class="MsoNormal">There is potentially for clang-format to introduce breaking changes and whilst this could in theory cause noisy breakages they could also in theory produce silent ones.<br>
<br>
These new features want to be run at "reformat" speeds & frequency and benefit from the rich Ecosystem of inclusion and integration in IDEs and editors that clang-format enjoys.<br>
<br>
This RFC is to try to gain some consensus as to what clang-format can do and what the conditions/constraints should be if allowed to do so.<br>
<br>
Benefits<br>
========<br>
<br>
The benefits are that clang-format can be used to further make code modifications to adhere to a coding convention (insertion/removal of braces),
<br>
clang-format could be used to validate and correct coding convention (left/right const), and could be used to remove unnecessary semicolons or
<br>
potentially convert code to trailing return types all of which could be performed at "reformat" speeds.<br>
<br>
Whilst some of these capabilities are available in clang-tidy, it requires significant infrastructure to often perform these often relatively simple operations and it's unlikely<br>
that all users of clang-format are set up to perform these actions in clang-tidy.<br>
<br>
There are likely a number of clang-tidy modifications that could in theory be made at "reformat" speeds with such an approach. But there really needs some agreement that it's OK for clang-format to modify the code.<br>
<br>
Allowing these kinds of modification capabilities could lead to a new set of "Resharper" style capabilities being added to clang-format,
<br>
capable of bringing source code quickly into line with coding conventions.<br>
<br>
Concerns<br>
========<br>
<br>
Correctness is King, the concern is your formatting tool should not perform operations that could break your code. (this is already the case)<br>
<br>
It's perhaps not clang-format's job to do this.<br>
<br>
I should personally declare myself as being in favor of allowing clang-format to modify code, I think it only fair that I let others reply to the RFC with their own concerns.<br>
<br>
Constraints<br>
===========<br>
<br>
To minimize the impact to existing users, We suggest that a number of constraints be generally considered good practice when submitting reviews for clang-format with modifying changes<br>
<br>
1) Code Modifying Features should always be off by default<br>
The user should have to make a positive affirmative action to use such a feature<br>
<br>
2) Code Modifying Features configuration options should be highlighted as such in the ClangFormatStyleOptions.rst such that its clear these are potentially code breaking options<br>
<br>
3) Existing "Code Modifying Features" do not have to adhere to 1) but the documentation should be updated to adhere to 2)<br>
<br>
4) Code Modifying Features should be conservative to be "correct first" before being "complete".<br>
i.e. If it's possible a change could be ambiguous it should tend towards not making the incorrect change at all rather than forcing an incorrect change. (This could cause some
<br>
cases to be missed)<br>
<br>
Request<br>
=======<br>
<br>
I would like to get some general agreement that it's OK for future reviews of code modification changes to become part of clang-format (as they are in IncludeSorting) assuming the best practices are
<br>
followed to protect users from unwanted changes.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">Feedback would be appreciated<br>
<br>
MyDeveloperDay<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<o:p></o:p></p>
</div>
</div>
</div>
</body>
</html>