llvm / llvm/llvm-project

clang-format assumes C++ language by default for .h files which makes proper C/C++ hybrid code bases very difficult

Open
#167,673 15 comments 0 reactions 0 assignees View on GitHub
clang-format
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

clang-format offers barely any support to manage C/C++ hybrid code bases via file extension. Assume use cases like

- Working with hybrid code bases including C/C++, or multiple projects with shared code, or multiple projects at all that are managed using a common .clang-format file
- The defined coding style guidelines for C and C++ have any differences that are nicely managed using the language sections in the .clang-format file

Now it is still impossible to manage these code bases using clang-format without extra steps or external scripting due to a very important detail: clang-format assumes the C++ language for .h files by default and seems to have no option to change this behavior. Why is this bad?

- .h file ending is the only accepted file ending for pure C header files. There is no other option to indicate a file shall be a C include.
- C++ include files offer a huge variety in accepted extensions: .hh, .hpp, .hxx

But due to clang-formats decision to handle .h files also as C++ include file I cannot manage a C/C++ hybrid code base even if I properly distinguish these file types with their extension.

Helpful alternatives would be to either assume .h as C language or to offer the option to set the language associated with .h files. I am aware of the `// clang-format Language:` language specifier but that is not really a nice option, if the files could already be distinguished.

Remark: Objective C also unluckily chose .h for their header file extension but I guess this can be mostly neglected since Objective C is by far less relevant than C or C++.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.