Skip to content

Commit

Permalink
clang-format with UseTab: Always sometimes doesn't insert the right a…
Browse files Browse the repository at this point in the history
…mount of tabs.

Trailing comments are not always aligned properly when UseTab is set to Always.

Consider:

int a;        // x
int bbbbbbb; // x
With .clang-format:

---
Language:        Cpp
BasedOnStyle: LLVM
UseTab: Always
...
The trailing comments of this code block should be aligned, but aren't

To align the first trailing comment it needs to insert 8 spaces. This should be
one tab plus six spaces. It skips the logic of the first partial tab in
FirstTabWidth (=2) + Style.TabWidth (=8) <= Spaces (=8) and only inserts one
tab. Proposed fix and test is attached.

Patch by Hylke Kleve.

Differential revision: https://reviews.llvm.org/D57655

llvm-svn: 354183
  • Loading branch information
alexfh committed Feb 15, 2019
1 parent 383ccfb commit 027f5f5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 8 additions & 4 deletions clang/lib/Format/WhitespaceManager.cpp
Expand Up @@ -679,11 +679,15 @@ void WhitespaceManager::appendIndentText(std::string &Text,
case FormatStyle::UT_Always: {
unsigned FirstTabWidth =
Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
// Indent with tabs only when there's at least one full tab.
if (FirstTabWidth + Style.TabWidth <= Spaces) {
Spaces -= FirstTabWidth;
Text.append("\t");
// Insert only spaces when we want to end up before the next tab.
if (Spaces < FirstTabWidth || Spaces == 1) {
Text.append(Spaces, ' ');
break;
}
// Align to the next tab.
Spaces -= FirstTabWidth;
Text.append("\t");

Text.append(Spaces / Style.TabWidth, '\t');
Text.append(Spaces % Style.TabWidth, ' ');
break;
Expand Down
3 changes: 3 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Expand Up @@ -8751,6 +8751,9 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
"\t\t parameter2); \\\n"
"\t}",
Tab);
verifyFormat("int a;\t // x\n"
"int bbbbbbbb; // x\n",
Tab);

Tab.TabWidth = 4;
Tab.IndentWidth = 8;
Expand Down

0 comments on commit 027f5f5

Please sign in to comment.