<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - [clang-format] When editing a file with unbalance {} the namespace comment fixer can incorrectly comment the wrong closing brace"
   href="https://bugs.llvm.org/show_bug.cgi?id=46130">46130</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[clang-format] When editing a file with unbalance {} the namespace comment fixer can incorrectly comment the wrong closing brace
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Formatter
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>mydeveloperday@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>djasper@google.com, klimek@google.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>from Twitter <a href="https://twitter.com/ikautak/status/1265998988232159232">https://twitter.com/ikautak/status/1265998988232159232</a>

I have seen this myself many times.. if you have format on save and you work in
an editor where you are constantly saving (:w muscle memory)

If you are in the middle of editing and somehow you've missed a { or } in your
code, way way below at the bottom of you file the namespace comment fixer will
have put the namespace on the previous closing brace.

This leads to you having to fix up the bottom of the file.

This could be easily fixed by an initial pass of the tokens where we simply
count the number of {} ensuring they balance.

If they don't balance we don't do any namespace fixing as it will likely be
unstable and incorrect.

----------------------------------------------------------

  // Spin through the lines and ensure we have balanced braces
  int Braces = 0;
  for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
    FormatToken *Tok = AnnotatedLines[I]->First;
    while (Tok) {
      Braces += Tok->is(tok::l_brace) ? 1 : Tok->is(tok::r_brace) ? -1 : 0;
      Tok = Tok->Next;
    }
  }
  // Bon't attempt to comment unbalanced braces or this can
  // lead to comments being places on the closing brace which isn't
  // the brace of the namespace. (occurs during incomplete editing)
  if (Braces != 0) {
    return {Fixes, 0};
  }
----------------------------------------------------------</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>