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

clang-format: incorrect indentation after #elif

Open KernelAddress opened this issue 5 years ago • 1 comments

Bugzilla Link 48664
Version unspecified
OS Linux

Extended Description

The code is:

void loop()
{
	int iter = 0;
#if FOO
	for (; iter < 10; iter++) {
#else
	for (;; iter++) {
#endif
		for (;;) {
#if BAR && BAZ
			if (foo1())
				continue;
#elif BAR
			if (foo2())
				continue;
#else
		if (foo3())
			continue;
#endif
			break;
		}
	}
}

The bug is foo3 mis-indentation.

This breaks builds because clang later errors on the code produced by clang-format:

:353:4: error: misleading indentation; statement is not part of the previous 'if' [-Werror,-Wmisleading-indentation]

This may have something to do with #elif, because this works:

void loop()
{
	int iter = 0;
#if FOO
	for (; iter < 10; iter++) {
#else
	for (;; iter++) {
#endif
		for (;;) {
#if BAR
#if BAZ
			if (foo1())
				continue;
#else
			if (foo2())
				continue;
#endif
#else
			if (foo3())
				continue;
#endif
			break;
		}
	}
}

The style is:

BasedOnStyle: LLVM IndentWidth: 8 UseTab: Always BreakBeforeBraces: Linux IndentCaseLabels: false DerivePointerAlignment: false PointerAlignment: Left AlignTrailingComments: true AllowShortBlocksOnASingleLine: false AllowShortFunctionsOnASingleLine: false AllowShortIfStatementsOnASingleLine: false AllowShortLoopsOnASingleLine: false ColumnLimit: 0 AlignTrailingComments: false

Factored out of: llvm/llvm-project#44188 #c4

For context see: https://github.com/google/syzkaller/pull/2364

Tested with 11.0.0 ca2dcbd030eadbf0aa9b660efe864ff08af6e18b and 38d32e4fd70cc2b1f6ec10b578a56e631e8ed658.

KernelAddress avatar Jan 05 '21 12:01 KernelAddress

Edited top post to preserve meaningful whitespace.

JakeMerdichAMD avatar Jul 26 '22 04:07 JakeMerdichAMD