llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.15k stars 12.03k forks source link

clang-format: changes when reformatting one line of formatted file #47840

Open dd39fb77-d703-45fe-9da2-cbf576d010cd opened 3 years ago

dd39fb77-d703-45fe-9da2-cbf576d010cd commented 3 years ago
Bugzilla Link 48496
Version trunk
OS Linux
CC @dwblaikie,@mydeveloperday

Extended Description

Test case is as follows (add to clang/unittests/Format/FormatTestSelective.cpp):

TEST_F(FormatTestSelective, Bug) {
  Style.AlignConsecutiveDeclarations = true;
  Style.ColumnLimit = 16;

  std::string Code = "int b(void);\n"
                     "int u(int, int);\n"
                     "bool g(void);";

  std::string Full = format(Code, 0, Code.length());
  std::string First = format(Full, 0, 0);
  EXPECT_EQ(Full, First);
}

Test result:

      Expected: Full
      Which is: "int b(void);\nint u(int, int);\nbool g(void);"
To be equal to: First
      Which is: "int  b(void);\nint u(int, int);\nbool g(void);"
With diff:
@@ -1,3 +1,3 @@
-int b(void);
+int  b(void);
 int u(int, int);
 bool g(void);

Expected result: There are no changes when reformatting the first line.

Another thing to note is that u is not aligned to g. It could be, if the parameters were moved to the next line. This is what happens when u is changed to uu, making that line longer than the ColumnLimit.

The bug is present on the main branch at commit 8e6fc1f97eb9a63780158470596ddbec3d0ecd59

dd39fb77-d703-45fe-9da2-cbf576d010cd commented 3 years ago

I think both examples you present are correct. However the bug reported here is something else.

The bug happens when you format the file, then you format the first line only on the already formatted file, e.g. "clang-format -lines 1:1", or when you use git clang-format after modifying the first line.

I have attached an archive which has a .clang-format file and a test.c file to reproduce the issue. Run "clang-format test.c", and test.c will be output with no changes:

int b(void);
int u(int, int);
bool g(void);

Now run "clang-format --lines 1:1 test.c" and you will see:

int  b(void);
int u(int, int);
bool g(void);

I hope we can agree that this is not desired behaviour.

dd39fb77-d703-45fe-9da2-cbf576d010cd commented 3 years ago

Config and c file to reproduce the bug

mydeveloperday commented 3 years ago

Is this really a bug? or is your column limit just turning off the alignment for the longer line: i.e.

int u(int, int);

will be formatted as

int u(int, int);

which is 17 characters not 16

For me ColumnLimit = 16 formats as

int b(void); int u(int, int); bool g(void);

ColummLimit = 17 as

int b(void); int u(int, int); bool g(void);

Both of which seem correct to me, can you confirm