<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 24, 2020 at 5:35 PM Mehdi AMINI <<a href="mailto:joker.eph@gmail.com">joker.eph@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 24, 2020 at 11:51 AM Adrian McCarthy via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 24, 2020 at 2:37 AM Chris Lattner via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Jun 23, 2020, at 11:02 AM, Philip Reames <<a href="mailto:listmail@philipreames.com" target="_blank">listmail@philipreames.com</a>> wrote:<br>
> I'll note that reading along I haven't found any of the proposed changes particularly worthwhile.  I'm also not strongly opposed to any of them - I just don't care - but I certainly haven't been convinced there's any clear benefit to be had by changing our current policy.<br>
<br>
I agree.  The discussion is also hard to follow, because there are many different competing suggestions and opinions.  There are a couple of people talking about clarifying the rules to be less prescriptive, which seem like it is worth discussing.</blockquote><div><br></div><div><div>"Clarifying the rules to be less prescriptive" sounds self-contradictory.  Are you in favor of talking about clarifying the existing guidelines or changing them to be less prescriptive?  Or maybe you want to change them a little so that they are easier to express clearly?</div><div><br></div><div>There are already several well-defined de facto standard brace styles.  One way to make the guidelines clear (and concise) is simply to declare LLVM uses $(FOO) Brace Style with a link to the Wikipedia description.  That suggests to me that it's not super feasible to divorce clarification from style choice, at least, not without putting a bound on how clear we can be.</div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">  I think we should take the suggestion of “always require braces” off the table, because it doesn’t make sense given the impact to the code base.<br></blockquote><div><br></div><div>Given that the codebase is already riddled with inconsistencies (and instances that I cannot determine the correctness against the current guidelines), I don't understand why you think it doesn't make sense to consider a simpler scheme.  The current inconsistencies exist because the rules are unclear and, because of the edge cases, hard to internalize.  A simpler rule (or set of rules) would presumably result in fewer inconsistencies going forward, so the code would evolve toward a more consistent state.  <br></div></div></div></blockquote><div><br></div><div>Can you clarify what is unclear with the <a href="https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements" target="_blank">current rule</a>? </div></div></div></div></blockquote><div><br></div><div>I'm not the one who asserted that the goal of this thread is to clarify the rule.  But since you asked, I don't understand the sentences about hoisted comments.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="color:rgb(0,0,0);font-family:"Lucida Grande","Lucida Sans Unicode",Geneva,Verdana,sans-serif;font-size:14px">Readability is harmed when a single statement is accompanied by a comment that loses its meaning if hoisted above the </span><code class="gmail-docutils gmail-literal gmail-notranslate" style="font-family:Consolas,"Deja Vu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:14px;color:rgb(0,0,0)"><span class="gmail-pre" style="hyphens: none;">if</span></code><span style="color:rgb(0,0,0);font-family:"Lucida Grande","Lucida Sans Unicode",Geneva,Verdana,sans-serif;font-size:14px"> or loop statement.</span></blockquote><div><br></div><div>Does this mean "a comment that <i>would lose</i> its meaning if <i>it were to be</i> hoisted"?</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="color:rgb(0,0,0);font-family:"Lucida Grande","Lucida Sans Unicode",Geneva,Verdana,sans-serif;font-size:14px">Note that comments should only be hoisted for loops and </span><code class="gmail-docutils gmail-literal gmail-notranslate" style="font-family:Consolas,"Deja Vu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:14px;color:rgb(0,0,0)"><span class="gmail-pre" style="hyphens: none;">if</span></code><span style="color:rgb(0,0,0);font-family:"Lucida Grande","Lucida Sans Unicode",Geneva,Verdana,sans-serif;font-size:14px">, and not in </span><code class="gmail-docutils gmail-literal gmail-notranslate" style="font-family:Consolas,"Deja Vu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:14px;color:rgb(0,0,0)"><span class="gmail-pre" style="hyphens: none;">else</span> <span class="gmail-pre" style="hyphens: none;">if</span></code><span style="color:rgb(0,0,0);font-family:"Lucida Grande","Lucida Sans Unicode",Geneva,Verdana,sans-serif;font-size:14px"> or </span><code class="gmail-docutils gmail-literal gmail-notranslate" style="font-family:Consolas,"Deja Vu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:14px;color:rgb(0,0,0)"><span class="gmail-pre" style="hyphens: none;">else</span></code><span style="color:rgb(0,0,0);font-family:"Lucida Grande","Lucida Sans Unicode",Geneva,Verdana,sans-serif;font-size:14px">, where it would be unclear whether the comment belonged to the preceeding condition, or the </span><code class="gmail-docutils gmail-literal gmail-notranslate" style="font-family:Consolas,"Deja Vu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:14px;color:rgb(0,0,0)"><span class="gmail-pre" style="hyphens: none;">else</span></code><span style="color:rgb(0,0,0);font-family:"Lucida Grande","Lucida Sans Unicode",Geneva,Verdana,sans-serif;font-size:14px">.</span><br></blockquote><div><br></div><div>This seems to imply that we should be hoisting comments up whenever we can.  Is that actually the case?  Or is this simply guidance for when someone is considering hoisting a comment?</div><div><br></div><div>In any event, I'm not the one who said we should clarify the rule.  In fact, I was trying to get clarification about Chris's comment about both clarifying and/or changing the rule.</div><div><br></div><div>It's my opinion that we should simplify the rule.  My issues are:</div><div><ul><li>The rule is overly complex for what it accomplishes.  There are a lot of details and edge cases adding to the cognitive load when trying to read and/or write the code.  I believe that's the root cause of many of the misunderstandings and of the existing inconsistencies in the code.</li><li>As currently worded, the rule contradicts claims I've seen in code reviews and up-thread.</li><ul><li>For example, I've seen requests to add braces to both the if and the else blocks when only one of those requires it.  If I read the current text of the rule, that _might_ be covered by "<span style="color:rgb(0,0,0);font-family:"Lucida Grande","Lucida Sans Unicode",Geneva,Verdana,sans-serif;font-size:14px">This list is not exhaustive, for example, readability is also harmed if an </span><code class="gmail-docutils gmail-literal gmail-notranslate" style="font-family:Consolas,"Deja Vu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:14px;color:rgb(0,0,0)"><span class="gmail-pre" style="hyphens: none;">if</span></code><span style="color:rgb(0,0,0);font-family:"Lucida Grande","Lucida Sans Unicode",Geneva,Verdana,sans-serif;font-size:14px">/</span><code class="gmail-docutils gmail-literal gmail-notranslate" style="font-family:Consolas,"Deja Vu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:14px;color:rgb(0,0,0)"><span class="gmail-pre" style="hyphens: none;">else</span></code><span style="color:rgb(0,0,0);font-family:"Lucida Grande","Lucida Sans Unicode",Geneva,Verdana,sans-serif;font-size:14px"> chain starts using braced bodies partway through and does not continue on with braced bodies."  </span>On the other hand that wording seems to say you shouldn't go up and add braces to the if body just because the else body requires it, a position reinforced in the example in the guidelines, but you should consistently add them below the first use.</li><li>Another example is whether you should add braces when the body is a single statement that takes multiple lines.  In the present wording, as I understand it, that's the case <i>only</i> if one or more of the lines is a comment (and even then, only in certain circumstances?!), but not if the statement itself wraps across multiple lines.</li></ul><li>Even if the entire codebase followed the rules consistently, when combined with other style decisions, the fact that there are sometimes braces and sometimes not inhibits visual pattern matching when reading the code, at least for some, including myself.  It wouldn't be as bad if we had more generous indentation levels and if we didn't grant indentation exceptions for case labels and public/protected/private markers in class definitions.</li></ul><div><div>It's easy to find examples to illustrate that the code is not currently even close to consistent; the code often uses braces where the guidelines tell us not to.  And there's no real harm done by that, not for readability, not for maintenance, and not even for consistency since we're already in an inconsistent state.  A simpler rule with fewer exceptions and less need for examples would be a benefit.  I spent a few minutes scrolling through Support/Path.cpp and found these illustrative examples:</div><div><br></div><div><a href="https://github.com/llvm/llvm-project/blob/master/llvm/lib/Support/Path.cpp#L353">https://github.com/llvm/llvm-project/blob/master/llvm/lib/Support/Path.cpp#L353</a> <br></div><div><font face="monospace">if (has_net || has_drive) {<br>  if ((++pos != e) && is_separator((*pos)[0], style)) {<br>    // {C:/,//net/}, so get the first two components.<br>    return path.substr(0, b->size() + pos->size());<br>  } else {<br>    // just {C:,//net}, return the first component.<br>    return *b;<br>  }<br>}</font> <br></div><div><br></div><div>I believe, according to the rule, the outer `if` should not have braces because its body is a single statement and there's no risk of a dangling `else`.  On the other hand, there's the "this list is not exhaustive" escape clause which opens the door to subjectivity.  (Personally, I think the braces here do help readability, but that doesn't seem to be the predominant opinion in the community.)  I believe the blocks on the inner `if` are correctly braced because the bodies also have comments.  I'll admit I don't understand all the wording about hoisting comments, but I'm pretty sure that doesn't apply here.</div><div><br></div><div><a href="https://github.com/llvm/llvm-project/blob/master/llvm/lib/Support/Path.cpp#L363">https://github.com/llvm/llvm-project/blob/master/llvm/lib/Support/Path.cpp#L363</a> </div><div><font face="monospace">// POSIX style root directory</font></div><div><font face="monospace">if (is_separator((*b)[0], style)) {</font></div><div><font face="monospace">  return *b;</font></div><div><font face="monospace">}</font></div><div><br></div><div>There are lots of examples of this kind of inconsistency.  But maybe this started out with braces because the comment was inside the body, and then it was later hoisted above the `if` (where it makes less sense) and the hoister forgot to remove the braces.</div><div><br></div><div><a href="https://github.com/llvm/llvm-project/blob/master/llvm/lib/Support/Path.cpp#L396">https://github.com/llvm/llvm-project/blob/master/llvm/lib/Support/Path.cpp#L396</a> </div><div><font face="monospace" size="1">if ((has_net || has_drive) &&<br>    // {C:,//net}, skip to the next component.<br>    (++pos != e) && is_separator((*pos)[0], style)) {<br>  return *pos;<br>}<br></font></div><div><font face="monospace" size="1"><br></font></div><div>According to the rule, the braces don't belong here, so we could just treat this as another example of general inconsistency.  But, oh my.  That's a line in the middle of the conditional expression.  Was that a merge error?  Was that a cut+paste error while trying to hoist (or sink?) the comment?  Or is the comment actually intended to describe just what the first part of the expression helps us determine.  I'm thankful for the braces here--they help this unusual pattern stand out.</div><div><br></div><div><a href="https://github.com/llvm/llvm-project/blob/master/llvm/lib/Support/Path.cpp#L733">https://github.com/llvm/llvm-project/blob/master/llvm/lib/Support/Path.cpp#L733</a> </div><font face="monospace">    // Check for path traversal components or double separators.<br>    if (component.empty() || component == ".") {<br>      needs_change = true;<br>    } else if (remove_dot_dot && component == "..") {<br>      needs_change = true;<br>      // Do not allow ".." to remove the root component. If this is the<br>      // beginning of a relative path, keep the ".." component.<br>      if (!components.empty() && components.back() != "..") {<br>        components.pop_back();<br>      } else if (!absolute) {<br>        components.push_back(component);<br>      }<br>    } else {<br>      components.push_back(component);<br>    } </font><div><br></div><div>I think, if we spend a minute or two studying this with the guidelines in an adjacent window, we can agree that there are exactly three sets of braces that should not be there.  I argue that code authors and reviewers should not have to spend a minute or two.  A simpler rule would save that cognitive load for more important details.  As a reader, I appreciate the consistent use of braces here rather than what it would have looked like without them.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div><br></div><div></div></div></div></div></blockquote></div></div><div><br></div></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div>The title of the section is "Don't Use Braces on Simple Single-Statement Bodies of if/else/loop Statements", which seems already fairly clear.</div><div>It then also mentions exceptions to the rule: readability and maintainability ; and clarifies what is considered not readable and not maintainable. It even gives two examples. </div></div></div></div></blockquote><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div>Maybe adding more examples there could help?</div></div></div></div></blockquote><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div>-- </div><div>Mehdi</div><div><br></div><div><br></div><div> </div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
-Chris<br>
<br>
> <br>
> Philip<br>
> <br>
> On 6/22/20 1:44 PM, Chris Lattner via llvm-dev wrote:<br>
>> For those who don’t like it, is the currently documented policy broken enough to be important to changing?<br>
>> <br>
>> I assume you wouldn’t recommend a massive rewrite of the codebase, so we’re going to be with this for quite some time.<br>
>> <br>
>> -Chris<br>
>> <br>
>>> On Jun 22, 2020, at 1:36 PM, Steve Scalpone via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>>> <br>
>>> Did this conversation reach a conclusion?<br>
>>> <br>
>>> My ad hoc tally says that a slight majority of the responders preferred to fully brace statements and no one wanted to totally eliminate braces.<br>
>>> <br>
>>> The technical arguments for fully braced statements were 1) it's considered a slightly safer coding style and 2) commit diffs with fully braced statements may be slightly more to the point.<br>
>>> <br>
>>> I didn't register any technical arguments for less-than-fully-braced statement -- the preference seemed to be aesthetic.  I may have missed a technical argument.<br>
>>> <br>
>>> Certainly an "always use braces" rule would be simpler than what's documented now in the LLVM Coding Standards [1].<br>
>>> <br>
>>> Another option would be to make braces a developer's choice, and ask that those omitting braces please follow the rules documented in [1].<br>
>>> <br>
>>> [1] <a href="https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements" rel="noreferrer" target="_blank">https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements</a><br>
>>> <br>
>>> On 6/18/20, 3:56 AM, "llvm-dev on behalf of Nicolai Hähnle via llvm-dev" <<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists.llvm.org</a> on behalf of <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>>> <br>
>>>    External email: Use caution opening links or attachments<br>
>>> <br>
>>> <br>
>>>    On Tue, Jun 16, 2020 at 10:35 AM Momchil Velikov via llvm-dev<br>
>>>    <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>>>> My 2 pennies is braces add unnecessary clutter and impair readability when<br>
>>>> used on a *single-line* statement. I count comments, that are on their<br>
>>>> own line as statement(s).<br>
>>>    +1 for this. I think braces around single-line statements can be<br>
>>>    allowed, but they really shouldn't be mandated, and that's been my<br>
>>>    personal policy for reviews. In particular,<br>
>>> <br>
>>>      if (!is_transform_applicable) {<br>
>>>        return {};<br>
>>>      }<br>
>>> <br>
>>>    is very aggravating clutter.<br>
>>> <br>
>>>    Braces should be required around multi-line statements. Note:<br>
>>> <br>
>>>    BAD:<br>
>>>      for (...)<br>
>>>        for (...)<br>
>>>          single_line_statement;<br>
>>> <br>
>>>    GOOD:<br>
>>>      for (...) {<br>
>>>        for (...)<br>
>>>          single_line_statement;<br>
>>>      }<br>
>>> <br>
>>>    Cheers,<br>
>>>    Nicolai<br>
>>>    --<br>
>>>    Lerne, wie die Welt wirklich ist,<br>
>>>    aber vergiss niemals, wie sie sein sollte.<br>
>>>    _______________________________________________<br>
>>>    LLVM Developers mailing list<br>
>>>    <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
>>>    <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
>>> <br>
>>> _______________________________________________<br>
>>> LLVM Developers mailing list<br>
>>> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
>>> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
>> _______________________________________________<br>
>> LLVM Developers mailing list<br>
>> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
>> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div></div>
</blockquote></div></div>