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

code: cleanup includes - use "include what you use" model #1089

Conversation

mhofstetter
Copy link
Member

@mhofstetter mhofstetter commented Jan 3, 2025

Currently, the C++ files are either including unused files or heavily rely on transtive includes and doesn't define the actual required includes.

Therefore, this commit fixes all includes so that all files are defining their includes according to their needs - following the model "include what you use".

See https://clangd.llvm.org/guides/include-cleaner

Advantages:

  • Get rid of warnings when using clangd as LSP server
  • Remove unused includes
  • Define all direct includes helps understanding the code and the dependencies

Note: For the time being there will not be any validation in CI. It's more or less just a cleanup and a potential start to establish this pattern because I think this helps for others to familiarize with the codebase.

Related to #1087

@mhofstetter mhofstetter force-pushed the pr/mhofstetter/clangd-cleanup-imports branch 3 times, most recently from c9f1f5a to 77609f3 Compare January 3, 2025 15:38
mhofstetter added a commit to mhofstetter/proxy that referenced this pull request Jan 3, 2025
Upstream Envoy linting prevents usages of `std::mutex` in favor of
`Thread::MutexBasicLockable`.

See https://github.com/envoyproxy/envoy/blob/main/tools/code_format/check_format.py#L605-L610

```
report_error(
  "Don't use <mutex> or <condition_variable*>, switch to "
  "Thread::MutexBasicLockable in source/common/common/thread.h")
```

This error occurred while trying to cleanup the imports - and therefore
adding the direct dependency to `<mutex>`.

See cilium#1089

Signed-off-by: Marco Hofstetter <[email protected]>
mhofstetter added a commit to mhofstetter/proxy that referenced this pull request Jan 3, 2025
Upstream Envoy linting prevents usages of `std::mutex` in favor of
`Thread::MutexBasicLockable`.

See https://github.com/envoyproxy/envoy/blob/main/tools/code_format/check_format.py#L605-L610

```
report_error(
  "Don't use <mutex> or <condition_variable*>, switch to "
  "Thread::MutexBasicLockable in source/common/common/thread.h")
```

This error occurred while trying to cleanup the imports - and therefore
adding the direct dependency to `<mutex>`.

See cilium#1089

Signed-off-by: Marco Hofstetter <[email protected]>
@mhofstetter mhofstetter changed the title code: cleanup imports - use "import what you use" model code: cleanup imports - use "include what you use" model Jan 3, 2025
@mhofstetter mhofstetter changed the title code: cleanup imports - use "include what you use" model code: cleanup includes - use "include what you use" model Jan 3, 2025
github-merge-queue bot pushed a commit that referenced this pull request Jan 7, 2025
Upstream Envoy linting prevents usages of `std::mutex` in favor of
`Thread::MutexBasicLockable`.

See https://github.com/envoyproxy/envoy/blob/main/tools/code_format/check_format.py#L605-L610

```
report_error(
  "Don't use <mutex> or <condition_variable*>, switch to "
  "Thread::MutexBasicLockable in source/common/common/thread.h")
```

This error occurred while trying to cleanup the imports - and therefore
adding the direct dependency to `<mutex>`.

See #1089

Signed-off-by: Marco Hofstetter <[email protected]>
@mhofstetter mhofstetter force-pushed the pr/mhofstetter/clangd-cleanup-imports branch 5 times, most recently from da22633 to 366a4b6 Compare January 7, 2025 09:21
Currently, the C++ files are either including unused files or
heavily rely on transtive includes and doesn't define the actual
required includes.

Therefore, this commit fixes all includes so that all files are
defining their includes according to their needs - following the
model "import what you use".

See https://clangd.llvm.org/guides/include-cleaner

Advantages:

* Get rid of warnings when using clangd as LSP server
* Remove unused imports
* Define all direct imports helps understanding the code and the
  dependencies

Signed-off-by: Marco Hofstetter <[email protected]>
To prevent upstream Envoy lint issues, this commit refactors the includes
to use `source/common/protobuf/protobuf.h` instead of the `google/protobuf/...`
includes.

Signed-off-by: Marco Hofstetter <[email protected]>
Instantiate strings for assertion with `std:string` instead of
raw string literals to prevent clangd importing `bits/basic_string.h`
which leads to potential compiler errors.

Signed-off-by: Marco Hofstetter <[email protected]>
@mhofstetter mhofstetter force-pushed the pr/mhofstetter/clangd-cleanup-imports branch from 366a4b6 to 6cd8a15 Compare January 7, 2025 09:44
Signed-off-by: Marco Hofstetter <[email protected]>
@mhofstetter mhofstetter closed this Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant