35 Commits

Author SHA1 Message Date
kadir çetinkaya
bd595d5421
[include-cleaner] Generate references from explicit functiontemplate specializations (#83392) 2024-02-29 10:37:00 +01:00
kadir çetinkaya
9dfb843050
[include-cleaner] Use FoundDecl only for using-shadow-decls (#82615) 2024-02-23 11:48:04 +01:00
kadir çetinkaya
379e890bd8
[include-cleaner] Add handling for FriendDecls (#72125)
Fixes https://github.com/llvm/llvm-project/issues/64382.
2023-11-13 16:35:52 +01:00
kadir çetinkaya
a4d14011e5
[include-cleaner] Fix handling of enums in presence of qualifiers (#65952) 2023-09-11 19:21:02 +02:00
Kazu Hirata
840a968226 [clang-tools-extra] Use range-based for loops (NFC) 2023-09-02 12:12:16 -07:00
Utkarsh Saxena
ac2d2652db [include-cleaner] Handle decls/refs to concepts 2023-08-31 13:57:22 +02:00
Kadir Cetinkaya
ab090e9e49
[include-cleaner] Make handling of enum constants similar to members
We were treating enum constants more like regular decls, which results
in ignoring type aliases/exports.
This patch brings the handling to be closer to member-like decls, with
one caveat. When we encounter reference to an enum constant we still
report an explicit reference to the particular enum constant, as
otherwise we might not see any references to the enum itself.

Also drops implicit references from qualified names to containers, as
we already have explicit references from the qualifier to relevant
container.

Differential Revision: https://reviews.llvm.org/D158515
2023-08-28 09:31:58 +02:00
Kadir Cetinkaya
3736eaa6a0
[include-cleaner] Handle StdInitializerListExprs
Per C++ standard, programs omitting the definition for initializer_list
is ill-formed, https://eel.is/c++draft/dcl.init.list#2.

Fixes https://github.com/llvm/llvm-project/issues/64198

Differential Revision: https://reviews.llvm.org/D156712
2023-08-01 10:58:56 +02:00
Haojian Wu
149f309f50 [include-cleaner] Ignore the ParmVarDecl itself in WalkAST.cpp
This fixes a false positive where a ParamVarDecl happend to be the
same name of some C standard symbol and has a global namespace.

```
using A = int(int time); // we suggest <ctime> for the `int time`.
```

Differential Revision: https://reviews.llvm.org/D153330
2023-06-20 14:26:55 +02:00
Haojian Wu
100ffbf991 [include-cleaner] Report all specializations if the primary template is introduced by a using-decl.
This will fix unused-include false positive.

```
// primary.h
namespace ns {
template<class T1, class T2> class Z {}; // primary template
}

// partial.h
namespace ns {
template<class T> class Z<T, T*> {};     // partial specialization
}

// main.cpp

using ns::Z; // refs to the primary
void k() {
  Z<int, int*> z; // use the partial specialization
}
```

Differential Revision: https://reviews.llvm.org/D152345
2023-06-07 10:52:36 +02:00
Kadir Cetinkaya
cce7b816a1
[include-cleaner] Treat references to nested types implicit
Differential Revision: https://reviews.llvm.org/D149948
2023-05-23 16:40:51 +02:00
Kadir Cetinkaya
cc5fb7a79b
[include-cleaner] Unify behaviour for static & instance members
Fixes https://github.com/llvm/llvm-project/issues/62185

Differential Revision: https://reviews.llvm.org/D148552
2023-04-18 10:01:31 +02:00
Kadir Cetinkaya
3d6d2ae6f4
[include-cleaner] Handle incomplete template specializations
Instantiation pattern is null for incomplete template types and using
specializaiton decl results in not seeing re-declarations.

Differential Revision: https://reviews.llvm.org/D148158
2023-04-13 13:34:12 +02:00
Kadir Cetinkaya
34f5774920
[include-cleaner] Improve handling for templates
Principal here is:
- Making sure each template instantiation implies use of the most specialized
  template. As explicit instantiations/specializations are not redeclarations of
  the primary template.
- Introducing a use from explicit instantions/specializaitons to the primary
  template, as they're required but not traversed as part of the RAV.

Differential Revision: https://reviews.llvm.org/D148112
2023-04-12 17:36:05 +02:00
Kadir Cetinkaya
3402b77db3
[include-cleaner] Only ignore builtins without a header
Certain standard library functions (e.g. std::move) are also implemented
as builtins. This patch moves filtering logic to the symbol->header
mapping phase to rather generate these references without any providers
only when we don't have a mapping.
That way we can also map them to header names mentioned in the builtin
mappings.

Differential Revision: https://reviews.llvm.org/D147449
2023-04-04 13:28:56 +02:00
Kadir Cetinkaya
f323e7f3ca
[include-cleaner] Treat member operator calls as implicit
26ff268b80c589fd9f71c1c214af77cd972642ca treated member operator calls
as explicit, while trying to treat them the same way as regular member
expressions, which should've been implicit.
2023-04-03 16:27:23 +02:00
Kadir Cetinkaya
26ff268b80
[include-cleaner] Report references to operator calls as implicit
Missing these references can result in false negatives in the used-ness
analysis and break builds.

Differential Revision: https://reviews.llvm.org/D147144
2023-04-03 15:49:28 +02:00
Haojian Wu
83546221af [include-cleaner] Ignore builtin symbols in the WalkAST.
There is no need to add headers for builtin symbols.

Additionally, there is a bonus benefit which help eliminate some bugs -- builtin
functions are modeled as implicit FunctionDecls in the clang AST, which results in
them being treated as normal FunctionDecls in the implementation of the include-cleaner
(going through the path: ast-node -> decl -> source location -> header).
And, the source location of these built-in symbols' AST nodes is not precise (e.g. points to the first call site),
which leads to subtle behavior that inserts a header of the call site.

Differential Revision: https://reviews.llvm.org/D147213
2023-03-30 13:46:41 +02:00
Haojian Wu
7cd6ce9e91 [include-cleaner] Visit the VarDecl in ASTWalker.
Fixes https://github.com/clangd/clangd/issues/1554

Differential Revision: https://reviews.llvm.org/D147135
2023-03-29 22:20:30 +02:00
Kadir Cetinkaya
03101e141b
[include-cleaner] Attribute references to explicit specializations
Fixes https://github.com/llvm/llvm-project/issues/61652

Differential Revision: https://reviews.llvm.org/D146732
2023-03-24 11:39:21 +01:00
Kadir Cetinkaya
279b4985ed
[include-cleaner] Always treat constructor calls as implicit
Treating constructor calls when the type name isn't explicitly spelled
can cause spurious results, so turn them into implicit references.
This doesn't change the behaviour for constructor calls that explicitly spell
the type name, as we should see a reference through the typeloc.

Fixes https://github.com/llvm/llvm-project/issues/60812

Differential Revision: https://reviews.llvm.org/D144582
2023-02-23 10:00:50 +01:00
Haojian Wu
e435f56815 [include-cleaner] Remove a stale FIXME.
This FIXME was addressed in 0e545816a9e582af29ea4b9441fea8ed376cf52a.
2023-01-13 16:26:27 +01:00
Haojian Wu
22db1fb10b [include-cleaner] Treat a constructor call as a use of the class type.
Per the discussion in https://github.com/llvm/llvm-project/issues/59916.

Differential Revision: https://reviews.llvm.org/D141592
2023-01-12 13:12:14 +01:00
Haojian Wu
499bf67208 [include-cleaner] Don't count references to operators as uses
Fixes https://github.com/llvm/llvm-project/issues/59655

Differential Revision: https://reviews.llvm.org/D140551
2023-01-09 13:59:15 +01:00
Haojian Wu
8a7ea764b2 [include-cleaner] Base-type usage from member exprs is implicit.
Per the discussion on https://reviews.llvm.org/D140095#inline-1352956

Differential Revision: https://reviews.llvm.org/D140284
2022-12-19 19:46:18 +01:00
Viktoriia Bakalova
0e545816a9 [include-cleaner] Handle dependent type members in AST.
Handles dependent type members in AST.

Fix: https://github.com/llvm/llvm-project/issues/59354
Differential Revision: https://reviews.llvm.org/D139409
2022-12-19 12:45:41 +00:00
Haojian Wu
8551563c0d [include-cleaner] Fix a missing review comment.
I forgot to add the change to the commit when committing.
2022-12-19 09:14:33 +01:00
Haojian Wu
0ab57bdc97 [include-cleaner] Fix the member-expr-access usage for sugar type.
Fixes https://github.com/llvm/llvm-project/issues/59533

Differential Revision: https://reviews.llvm.org/D140095
2022-12-19 08:48:32 +01:00
Viktoriia Bakalova
d03e9f8fb0 [include-cleaner] Handle base class member access from derived class.
Fix: https://github.com/llvm/llvm-project/issues/59251
Differential Revision: https://reviews.llvm.org/D139087
2022-12-08 10:39:18 +00:00
Viktoriia Bakalova
45659b3bd9 [include-cleaner] Remove filtering from UsingDecl visit.
Removes filtering from the VisitUsingDecl method for implementation files.

Differential Revision: https://reviews.llvm.org/D138821
2022-12-08 10:23:55 +00:00
Sam McCall
8dfaf9940a [include-cleaner] Provide public to_string of RefType (for HTMLReport), clean up includes. NFC 2022-11-11 13:25:22 +01:00
Kadir Cetinkaya
d19ba74dee
[Includecleaner] Introduce RefType to ast walking
RefTypes are distinct categories for each reference to a symbol. They
are signals indicating strength of a reference, that'll enable different
decision making based on the finding being provided.

There are 3 kinds of ref types:
- Explicit, the reference is spelled in the code.
- Implicit, the reference is not directly visible in the code.
- Ambigious, the reference exists but can't be proven as used (e.g.
  overloads brought in by a using decl but not used by the code).

Differential Revision: https://reviews.llvm.org/D135859
2022-11-08 16:29:31 +01:00
Kadir Cetinkaya
772fc63f5b
[IncludeCleaner] Handle more C++ constructs
Summary:
This brings IncludeCleaner's reference discovery from AST to the parity
with current implementation in clangd. Some highlights:
- Handling of MemberExprs, only the member declaration is marked as
  referenced and not the container, unlike clangd.
- Constructor calls, only the constructor and not the container, unlike
  clangd.
- All the possible candidates for unresolved overloads, same as clangd.
- All the shadow decls for using-decls, same as clangd.
- Declarations for definitions of enums with an underlying type and
  functions, same as clangd.
- Using typelocs, using templatenames and typedefs only reference the
  found decl, same as clangd.
- Template specializations only reference the primary template, not the
  explicit specializations, to be fixed.
- Expr types aren't marked as used, unlike clangd.

Going forward, we can consider having signals to indicate type of a
reference (e.g. `implicit` signal for type of an expr) so that the
applications can perform a filtering based on their needs.
At the moment the biggest discrepancy is around type of exprs, i.e. not
marking containers for member/constructor accesses. I believe this is
the right model since the declaration of the member and the container
should be available in a single file (modulo macros).

Reviewers: sammccall

Subscribers:

Differential Revision: https://reviews.llvm.org/D132110
2022-10-25 09:58:49 +02:00
Kadir Cetinkaya
ce286eccac
[IncludeCleaner] Add public API
Introduces walkUsed, a very simple version of the public API to enable
incremental development on rest of the pieces.

Differential Revision: https://reviews.llvm.org/D136293
2022-10-24 17:06:15 +02:00
Sam McCall
41ac245c10 [include-cleaner] Include-cleaner library structure, and simplistic AST walking.
Include-cleaner is a library that uses the clang AST and preprocessor to
determine which headers are used. It will be used in clang-tidy, in
clangd, in a standalone tool at least for testing, and in out-of-tree tools.

Roughly, it walks the AST, finds referenced decls, maps these to
used sourcelocations, then to FileEntrys, then matching these against #includes.
However there are many wrinkles: dealing with macros, standard library
symbols, umbrella headers, IWYU directives etc.

It is not built on the C++20 modules concept of usage, to allow:
 - use with existing non-modules codebases
 - a flexible API embeddable in clang-tidy, clangd, and other tools
 - avoiding a chicken-and-egg problem where include cleanups are needed
   before modules can be adopted

This library is based on existing functionality in clangd that provides
an unused-include warning. However it has design changes:
 - it accommodates diagnosing missing includes too (this means tracking
   where references come from, not just the set of targets)
 - it more clearly separates the different mappings
   (symbol => location => header => include) for better testing
 - it handles special cases like standard library symbols and IWYU directives
   more elegantly by adding unified Location and Header types instead of
   side-tables
 - it will support some customization of policy where necessary (e.g.
   for style questions of what constitutes a use, or to allow
   both missing-include and unused-include modes to be conservative)

This patch adds the basic directory structure under clang-tools-extra
and a skeleton version of the AST traversal, which will be the central
piece.
A more end-to-end prototype is in https://reviews.llvm.org/D122677

RFC: https://discourse.llvm.org/t/rfc-lifting-include-cleaner-missing-unused-include-detection-out-of-clangd/61228

Differential Revision: https://reviews.llvm.org/D124164
2022-04-29 11:04:11 +02:00