llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Clang-format unexpected aligning behavior

Open Stehsaer opened this issue 1 year ago • 1 comments

As described in the official doc (Link), setting AlignArrayOfStructures to Left should result in:

struct test demo[] =
{
    {56, 23,    "hello"},
    {-1, 93463, "world"},
    {7,  5,     "!!"   }
};

However, when using the build-in clang-format functionality in clangd version 18.1.2

Output of clangd --version:

clangd version 18.1.2
Features: windows
Platform: x86_64-pc-windows-msvc

it results in the following:

struct Test
{
	int			a, b;
	const char* c;
};

std::vector<Test> demo = {
	{56, 23,	 "hello"},
	   {-1, 93463, "world"},
	 {7,	 5,		"!!"	}
};

Which is OBVIOUSLY unexpected.

Trying with other arrays also results in similar outputs:

struct Vertex
{
	glm::vec3 color;
	glm::vec2 pos;
};

inline const std::vector<Vertex> vertex_list = {
	{{1.0f, 0.0f, 0.0f}, {0.0f, -0.5f}},
	{{0.0f, 1.0f, 0.0f}, {0.5f, 0.5f} },
	{{0.0f, 0.0f, 1.0f}, {-0.5f, 0.5f}}
};

Notice the fifth floating point numbers are not aligned

The .clang-format config I'm currently using:

BasedOnStyle: Microsoft
IndentWidth: 4
ColumnLimit: 100
SpacesBeforeTrailingComments: 2

# Allows
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortLambdasOnASingleLine: false

# Line breaks
BreakBeforeBinaryOperators: All
BreakConstructorInitializers: AfterColon
BreakBeforeBraces: Allman

# BracedInitializerIndentWidth: 4

# Bin-packs
BinPackArguments: false
BinPackParameters: false
PackConstructorInitializers: Never
AlwaysBreakTemplateDeclarations: Yes
ExperimentalAutoDetectBinPacking: false

# Alignments
AlignConsecutiveBitFields: true
AlignConsecutiveAssignments: true
AlignArrayOfStructures: Left
AlignConsecutiveDeclarations: true
PointerAlignment: Left
AlignOperands: AlignAfterOperator
AlignAfterOpenBracket: BlockIndent

BraceWrapping:
  SplitEmptyFunction: false
  SplitEmptyRecord: false
  SplitEmptyNamespace: false
  BeforeLambdaBody: true

ContinuationIndentWidth: 4
Cpp11BracedListStyle: true

EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: Always

UseTab: Always
FixNamespaceComments: false
NamespaceIndentation: All

Note

The clangd binary is from the offical LLVM release 18.1.2, downloaded from Github. I didn't compile the binary by myself.

Stehsaer avatar Mar 24 '24 14:03 Stehsaer

Update

Under some further testing, settings UseTab to Always is causing part of the problem.

UseTab: Always

struct Test
{
	int			a, b;
	const char* c;
};

std::vector<Test> demo = {
	{56, 23,	 "hello"},
	   {-1, 93463, "world"},
	 {7,	 5,		"!!"	}
};

UseTab: AlignWithSpaces

struct Test
{
	int         a, b;
	const char* c;
};

std::vector<Test> demo = {
	{56, 23,    "hello"},
    {-1, 93463, "world"},
    {7,  5,     "!!"   }
};

Not resolve by the temporary solution above

However the alignment of another example isn't resolved by changing the UseTab attribute: With UseTab: Always:

struct Vertex
{
	glm::vec3 color;
	glm::vec2 pos;
};

inline const std::vector<Vertex> vertex_list = {
	{{1.0f, 0.0f, 0.0f}, {0.0f, -0.5f}},
	{{0.0f, 1.0f, 0.0f}, {0.5f, 0.5f} },
	{{0.0f, 0.0f, 1.0f}, {-0.5f, 0.5f}}
};

With UseTab: AlignWithSpaces:

struct Vertex
{
	glm::vec3 color;
	glm::vec2 pos;
};

inline const std::vector<Vertex> vertex_list = {
	{{1.0f, 0.0f, 0.0f}, {0.0f, -0.5f}},
	{{0.0f, 1.0f, 0.0f}, {0.5f, 0.5f} },
	{{0.0f, 0.0f, 1.0f}, {-0.5f, 0.5f}}
};

Stehsaer avatar Mar 24 '24 15:03 Stehsaer