diff options
author | Alexander Kornienko <alexfh@google.com> | 2019-02-15 23:07:43 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2019-02-15 23:07:43 +0000 |
commit | 62964850c878e71ca8497b17336232d870afae2f (patch) | |
tree | b163d63c777245f28661b32d92a7f01bb743028c | |
parent | 7246b0179cbba51d4b88077c4540e3be21e83a83 (diff) |
clang-format with UseTab: Always sometimes doesn't insert the right amount of tabs.
Trailing comments are not always aligned properly when UseTab is set to Always.
Consider:
int a; // x
int bbbbbbbb; // 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
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@354183 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Format/WhitespaceManager.cpp | 12 | ||||
-rw-r--r-- | unittests/Format/FormatTest.cpp | 3 |
2 files changed, 11 insertions, 4 deletions
diff --git a/lib/Format/WhitespaceManager.cpp b/lib/Format/WhitespaceManager.cpp index 7b649c95fd..44aa46f1f6 100644 --- a/lib/Format/WhitespaceManager.cpp +++ b/lib/Format/WhitespaceManager.cpp @@ -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; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 2b0ef9a9dd..eea18440d0 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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; |