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] clang-format Fails on Lambda with Parameter Pack, Reference Capture and Constraint in .h Header #61032

Open
kaigu1997 opened this issue Feb 27, 2023 · 4 comments

Comments

@kaigu1997
Copy link

kaigu1997 commented Feb 27, 2023

Here is the minimal example:
.clang-format:

BasedOnStyle: Microsoft
---
Language: Cpp
UseTab: Always

test.h:

void f(void)
{
	int i = 3;
	auto impl = [&i]<int... I>()
	{
		(I, ...);
	};
}

when running clang-format --style=file -i test.h, Clang-Format would replace all the tab indentations with 4 spaces, even UseTab is Always; in fact, clang-format would neglect all given style option, except for BasedOnStyle (which I am not sure).

However, if change the extension to .cpp or .hpp, Clang-Format would work fine.
Without reference capture (say, with value capture only), this would also work fine.
Type parameter (typename... T) works fine too.

@kaigu1997 kaigu1997 changed the title [clang-format] clang-format 15 ails for lambda with non-type parameter pack and reference capture in .h header [clang-format] clang-format 15 Fails on Lambda with Non-type Parameter Pack and Reference Capture in .h Header Feb 27, 2023
@llvmbot
Copy link
Member

llvmbot commented Feb 27, 2023

@llvm/issue-subscribers-clang-format

@EugeneZelenko
Copy link
Contributor

Could you please try 16 or main branch?

@kaigu1997
Copy link
Author

kaigu1997 commented Feb 28, 2023

I tried 16.0.0rc3, and it seems to be fixed.
It inlines the lambda, but keeps the indents as tab.

Similar to #59522 and #57093, and fixed by 2476135?

So here is another demo of test.h that Clang-Format will fail again:

#include <type_traits>
#include <utility>

void f(void)
{
	int i = 3;
	auto impl = [&i]<std::size_t... I>(std::index_sequence<I...>)
		requires std::is_same_v<std::index_sequence<I...>, std::make_index_sequence<3>>
	{
		(I, ...);
	};
}

with the requires constraint, Clang-Format could not work properly. Removing the constraint shows good result.
With type template parameter pack, the issue arises as well:

#include <type_traits>
#include <utility>

void f(void)
{
	int i = 3;
	auto impl = [&i]<typename... T>(std::tuple<T...> t)
		requires (std::is_same_v<T, int> && ...)
	{
		std::get<0>(t);
	};
}

Clang-Format fails for this test.h as well.

Again, removing reference capture, or change the extension to .hpp or .cpp could fix the issue.

@kaigu1997 kaigu1997 changed the title [clang-format] clang-format 15 Fails on Lambda with Non-type Parameter Pack and Reference Capture in .h Header [clang-format] clang-format Fails on Lambda with Parameter Pack, Reference Capture and Constraint in .h Header Feb 28, 2023
@rymiel
Copy link
Member

rymiel commented Feb 28, 2023

The issue here is that because of the ambiguous .h extension, clang-format makes a guess on what the language is and somehow lands on... Objective-C

One possible workaround is to just not make the config being used language-specific, such as the following:

BasedOnStyle: Microsoft
UseTab: Always

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

4 participants