[llvm-bugs] [Bug 46130] New: [clang-format] When editing a file with unbalance {} the namespace comment fixer can incorrectly comment the wrong closing brace

via llvm-bugs llvm-bugs at lists.llvm.org
Fri May 29 02:39:35 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=46130

            Bug ID: 46130
           Summary: [clang-format] When editing a file with unbalance {}
                    the namespace comment fixer can incorrectly comment
                    the wrong closing brace
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Formatter
          Assignee: unassignedclangbugs at nondot.org
          Reporter: mydeveloperday at gmail.com
                CC: djasper at google.com, klimek at google.com,
                    llvm-bugs at lists.llvm.org

from Twitter https://twitter.com/ikautak/status/1265998988232159232

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};
  }
----------------------------------------------------------

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200529/a9127cf8/attachment-0001.html>


More information about the llvm-bugs mailing list