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 option AlignTrailingComments: Leave misaligns comment-only lines #92530

Closed
MatejVitek opened this issue May 17, 2024 · 0 comments · Fixed by #98427
Closed

clang-format option AlignTrailingComments: Leave misaligns comment-only lines #92530

MatejVitek opened this issue May 17, 2024 · 0 comments · Fixed by #98427
Assignees

Comments

@MatejVitek
Copy link

MatejVitek commented May 17, 2024

I'm using clang-format in VS Code (via Microsoft's C/C++ extension) with the following setup:

"C_Cpp.clang_format_style": "{BasedOnStyle: LLVM, UseTab: ForIndentation, IndentWidth: 4, TabWidth: 4, ColumnLimit: 0, PointerAlignment: Left, BreakBeforeBraces: Attach, SortIncludes: CaseInsensitive, AllowShortIfStatementsOnASingleLine: Never, AlignTrailingComments: Leave, SpacesBeforeTrailingComments: 2, SpaceBeforeCaseColon: false, InsertNewlineAtEOF: true}",

However, this setup is indenting my comment lines incorrectly. My expected result would look like:

int main(int argc, char* argv[]) {
	// create 10 threads
	pthread_t threads[10];
	for (int i = 0; i < 10; i++)
		pthread_create(&threads[i], NULL, thread, NULL);

	// wait for a bit then stop the threads
	sleep(3);
	for (int i = 0; i < 10; i++) {
		// pthread_yield();  // deprecated
		sched_yield();
		pthread_cancel(threads[i]);  // cancel thread
	}
}

However, the result of formatting with the above setup is the following:

int main(int argc, char* argv[]) {
 // create 10 threads
	pthread_t threads[10];
	for (int i = 0; i < 10; i++)
		pthread_create(&threads[i], NULL, thread, NULL);

 // wait for a bit then stop the threads
	sleep(3);
	for (int i = 0; i < 10; i++) {
  // pthread_yield();  // deprecated
		sched_yield();
		pthread_cancel(threads[i]);  // cancel thread
	}
}

This seems to be caused by the setting AlignTrailingComments: Leave. Setting this option to either Never or Always formats comment-only lines correctly (but obviously correspondingly changes the behaviour for actual trailing comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants