219 Commits

Author SHA1 Message Date
Sam McCall
c333b92b72 [include-cleaner] Missing changes from 38cccb906603 2022-11-08 20:18:10 +01:00
Sam McCall
38cccb9066 [include-cleaner] pass through recorded macro refs in walkUsed
Differential Revision: https://reviews.llvm.org/D137644
2022-11-08 20:10:39 +01:00
Aaron Ballman
322cf744b9 Fix call to utohexstr
This was passing a width but failed to pass the preceding boolean
argument for whether to use to upper or lowercase hex digits.

Amends d19ba74dee0b9ab553bd8a6ef5b67ff349f4bf13
2022-11-08 11:51:47 -05: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
Sam McCall
8249dc2104 [include-cleaner] Record main-file macro occurences and includes
The occurrences are roots for finding used headers, like walkAST.
Includes are the targets we're matching used headers against.

Differential Revision: https://reviews.llvm.org/D136723
2022-11-08 15:20:40 +01:00
Sam McCall
5426a1e51d [include-cleaner] Add the missing parts of Symbol and Header clases.
Differential Revision: https://reviews.llvm.org/D136710
2022-11-08 12:19:23 +01:00
Haojian Wu
9ab0827f70 [include-cleaner] Add a data-structure to capture IWYU pragmas.
PragmaIncludes captures the pragma-based header-mapping information, it is used
in the "Location => Header" step to determine the final spelling header for a
symbol (rather than the header directive).

The structure is by design to be used inside the include-cleaner library
and clangd.

Differential Revision: https://reviews.llvm.org/D136071
2022-10-31 15:21:24 +01:00
Sam McCall
b279f9bba9 [include-cleaner] Fix header guard. NFC 2022-10-26 02:24:31 +02:00
Sam McCall
3e067d4e59 [include-cleaner] Move vocabulary types into separate header for layering. NFC 2022-10-25 19:01:59 +02: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
Kazu Hirata
3f8d2c917c Ensure newlines at the end of files (NFC) 2022-10-22 09:29:40 -07:00
Kai Luo
2e73129483 [include-cleaner] Fix link errors when -DBUILD_SHARED_LIBS=ON
Fixed ppc buildbot https://lab.llvm.org/buildbot/#/builders/121/builds/24273 which is using `-DBUILD_SHARED_LIBS=ON`.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D136229
2022-10-19 07:26:08 +00:00
Sam McCall
25bac38a02 [include-cleaner] Add line numbers to HTML output 2022-10-19 00:21:33 +02:00
Sam McCall
6fa0e026c8 [include-cleaner] Add include-cleaner tool, with initial HTML report
The immediate goal is to start producing an HTML report to debug and explain
include-cleaner recommendations.
For now, this includes only the lowest-level piece: a list of the references
found in the source code.

How this fits into future ideas:
 - under refs we can also show the headers providing the symbol, which includes
   match those headers etc
 - we can also annotate the #include lines with which symbols they cover, and
   add whichever includes we're suggesting too
 - the include-cleaner tool will likely have modes where it emits diagnostics
   and/or applies edits, so the HTML report is behind a flag

Differential Revision: https://reviews.llvm.org/D135956
2022-10-18 18:09:41 +02:00
Michał Górny
52ce6776cf [clang-tools-extra] [test] Use CLANG_NO_DEFAULT_CONFIG=1
Set CLANG_NO_DEFAULT_CONFIG=1 for clang-tools-extra tests to prevent
the system configuration files for clang from affecting the test
results.

Differential Revision: https://reviews.llvm.org/D135159
2022-10-04 20:08:56 +02:00
Christian Kandeler
d352017184 [include-cleaner] Fix build error in unit test
Reviewed By: nridge

Differential Revision: https://reviews.llvm.org/D127217
2022-06-09 03:38:13 -04:00
Sam McCall
97b6c92dcd
[include-cleaner] Add missing deps from unittests 2022-04-29 13:08:28 +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