| Issue |
171601
|
| Summary |
[clang-tidy] checker to use forward declarations for optimizing headers inclusions
|
| Labels |
clang-tidy
|
| Assignees |
|
| Reporter |
denzor200
|
**Motivation**
C++ developers often make suboptimal use of #include directives in header files, which leads to:
- Slower compilation times due to redundant inclusions;
- Potential circular dependencies between header files;
- Increased size of precompiled headers.
In C++, forward declarations of classes can be used instead of full header inclusions when:
1. The type is used only through a pointer or reference;
2. Class methods are not called or member access is not required;
3. Knowledge of the class size is not required (e.g., for `sizeof` or some containers).
**The goal of new checker**
Detect cases where a full header inclusion can be replaced with a forward declaration without violating the semantics of the program.
**Before**
```
// bar.hpp
#pragma once
#include "foo.hpp" // Extra inclusion slows down compilation
void process(const foo&); // Only forward declaration would be enough
```
**After**
```
// bar.hpp
#pragma once
class foo; // forward declaration instead of full inclusion
void process(const foo&);
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs