<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 - Trailing comments after endif adds macro continuation"
   href="https://bugs.llvm.org/show_bug.cgi?id=46964">46964</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Trailing comments after endif adds macro continuation
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>10.0
          </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>normal
          </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>geoffrey.carlton@activision.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>Created <span class=""><a href="attachment.cgi?id=23809" name="attach_23809" title="Input to be formatted with llvm and ColumnLimit 0">attachment 23809</a> <a href="attachment.cgi?id=23809&action=edit" title="Input to be formatted with llvm and ColumnLimit 0">[details]</a></span>
Input to be formatted with llvm and ColumnLimit 0

When an #endif or #else has a trailing comment, and a comment follows on the
next line, clang-format can add an inappropriate macro continuation '\\' at the
end of the first comment.

Minimum repro is attached.  Input file is attached, but is in full:
  "#if A()\n#endif // #if A()\n\t\t\t// hello\n"
Output becomes:
  "#if A()\n#endif // #if A() \\\n       // hello\n"

Reproduces in clang 10.0.0 with -style="{BasedOnStyle: llvm, ColumnLimit: 0}". 
Also reproduces in latest code built from github.

The code is related to ContinuationIndenter::addTokenOnNewLine where it is
adding whitespace with ContinuePPDirective set.  That causes the comment to be
joined with the macro continuation character.

A partial workaround is before that call to replaceWhitespace:

    if (State.Line->First->is(tok::hash) && Current.is(tok::comment) &&
        State.Line->First->Next != nullptr) {
      if (isIfdefType(State.Line->First->Next)) {
        // Don't count this trailing comment part of a PP directive or it will
        // unnecessarily escape the end with '\' If we happen to have a another
        // comment on the next line.
        ContinuePPDirective = false;
        // An additional problem is later parts of clang-format can shift
around
        // the following comment, causing the output not to be stable.
        Current.Finalized = true;
      }
    }
Where isIfdefType is checking for ifdef/elif/else/endif.  That's good enough
for a quick fix but isn't the real solution to the problem.</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>