Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-format] unexpected aligning behavior #86439

Open
Stehsaer opened this issue Mar 24, 2024 · 1 comment
Open

[clang-format] unexpected aligning behavior #86439

Stehsaer opened this issue Mar 24, 2024 · 1 comment

Comments

@Stehsaer
Copy link

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
Copy link
Author

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}}
};

@mydeveloperday mydeveloperday changed the title Clang-format unexpected aligning behavior [clang-format] unexpected aligning behavior May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant