Skip to content

[clang-format] git-clang-format can indent modified lines incorrectly #141861

Closed as not planned
@andrurogerz

Description

@andrurogerz

Overview

I observed this behavior while authoring #141650. When running git clang-format on the modifications made to llvm/IR/BasicBlock.h, it incorrectly indents some function declarations that were modified. However, when running clang-format first on the entire file and committing its seemingly unrelated changes, git clang-format no longer adds the incorrect indentation.

Details

Looking into this a bit further, I think it is just a matter of git clang-format matching indentation of a modified line to the indentation of the preceding declaration within the same scope. In this case, the author of llvm/IR/BasicBlock.h formatted these two getParent method declarations as follows:

  const Function *getParent() const { return Parent; }
        Function *getParent()       { return Parent; }

While there is something appealing about this format, it incorrectly indents the second getParent declaration. #141650 does not modify either of these getParent declaration lines, but it does modify the next method declaration in the class. When git clang-format reformats the modified line, it matches its indentation to the improperly indented getParent declaration.

Repro

I made a very simple repo here.

1. Clone the repro source:

git clone [email protected]:andrurogerz/clang-format-issue.git
cd clang-format-issue

2. Format the HEAD change:

git clang-format HEAD~1

Expected results

The example.hpp file should be unmodified because the line that changed in HEAD is still formatted correctly.

Actual results

Indentation of the doSomething method in example.hpp incorrectly matches the preceding getSomething method declaration.
If you instead clang-format -i example.hpp, it will fix the indentation of the second getSomething method instead (which is correct).

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang-formatquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions