30 Commits

Author SHA1 Message Date
kadir çetinkaya
ec6c3448d3
[include-cleaner] Respect langopts when analyzing macro names (#123634)
Fixes https://github.com/llvm/llvm-project/issues/113926.
Fixes https://github.com/llvm/llvm-project/issues/63976.
2025-01-21 14:48:08 +01:00
Jie Fu
2954d1f7bc [include-cleaner] Fix -Wpessimizing-move in IncludeCleaner.cpp (NFC)
/llvm-project/clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp:302:14:
error: moving a temporary object prevents copy elision [-Werror,-Wpessimizing-move]
      return std::move(llvm::errorCodeToError(Err));
             ^
/llvm-project/clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp:302:14: note: remove std::move call here
      return std::move(llvm::errorCodeToError(Err));
             ^~~~~~~~~~                           ~
1 error generated.
2024-10-17 18:51:40 +08:00
Byoungchan Lee
4cda28c1ad
[clang-include-cleaner] Fix incorrect directory issue for writing files (#111375)
If the current working directory of `clang-include-cleaner` differs from
the directory of the input files specified in the compilation database,
it doesn't adjust the input file paths properly. As a result,
`clang-include-cleaner` either writes files to the wrong directory or
fails to write files altogether.

This pull request fixes the issue by adjusting the input file paths
based on the directory specified in the compilation database. If that
directory is not writable, `clang-include-cleaner` will write the output
relative to the current working directory.

Fixes #110843.
2024-10-17 10:16:07 +02:00
kadir çetinkaya
4ef77d61b2
[include-cleaner] Attach Header to AnalysisResults for misisng headers (#110272)
Currently callers of analyze can't get detailed information about a
missing header, e.g. resolve path. Only way to get at this is to use low
level walkUsed funciton, which is way more complicated than just calling
analyze.

This enables further analysis, e.g. when includes are spelled relative
to inner directories, caller can still know their path relative to
repository root.
2024-09-30 04:57:19 +02:00
kadir çetinkaya
bb5e66e31b
[include-cleaner] Suppress all clang warnings (#109099)
This patch disables all clang warnings when running include-cleaner, as
users aren't interested in other findings and in-development code might
have them temporarily. This ensures tool can keep working even in
presence of such issues.
2024-09-19 11:16:49 +02:00
kadir çetinkaya
64d9713637
[include-cleaner] Unify symlink handling (#102615)
We were using tryGetRealPathName in certain places, which resolves
symlinks (sometimes). This was resulting in discrepancies in behavior,
depending on how a file was first reached.

This path migrates all usages of tryGetRealPathName to regular getName
instead.

This implies one backward incompatible change for header-filtering. Our
ignore-header option used to filter against suffixes of absolute paths,
whereas now filter can receive working-directory relative paths in some
cases, possibly braking existing filters.
Chances of really braking users is pretty low:
- We'll still filter against absolute paths when header is outside the
  working directory (e.g. /usr/bin/include/some/linux/header.h.)
- Most projects run builds in a working directory that's nested inside
  the repository, hence relative paths still contain all the segments
  relative to repository root and anything else is unlikely to be
  meaningful. e.g. if a header is in
  `$HOME/work/llvm-project/clang-tools-extra/header.h` with builds being
  run in `$home/work/llvm-project/build`, we'll still filter against
  `../clang-tools-extra/header.h` which has all the useful segments as a
  suffix.
- This is also a change in how we handle symlinks, but this is aligned
  with what we do in rest of our tools (clangd, tidy checks etc.). We
  tend to not resolve any symlinks for the file.
2024-08-13 12:51:52 +02:00
kadir çetinkaya
7e3fb372b0
[include-cleaner] Check emptiness instead of occurences (#79154)
Our internal integration relies on injecting some default values to
ignore/keep lists.
That means we can have filters, despite of not having occurences
for the flag.
2024-01-24 07:26:00 +01:00
Sam McCall
3de5d8e125
[include-cleaner] Add --only-headers flag, opposite of --ignore-headers (#78714) 2024-01-22 16:03:37 +01:00
Haojian Wu
67963d384b
[include-cleaner] Fix a race issue when editing multiple files. (#76960)
We have a previous fix
be861b64d9,
which snapshots all processing files.

It works most of times, the snapshot (InMemoryFileSystem) is based on
the file path. The file-path-based lookup can fail in a subtle way for
some tricky cases (we encounter it internally), which will result in
reading a corrupted file.

This is a different fix, we don't modify files on the fly, instead, we
write files when the tool finishes for all files.
2024-01-05 10:13:33 +01:00
Kadir Cetinkaya
90ecadde62
[include-cleaner] Filter references to identity macros
Despite being true positives, these results just confuse users. So
filter them out.

Differential Revision: https://reviews.llvm.org/D157905
2023-08-18 14:40:03 +02:00
Haojian Wu
be861b64d9 [include-cleaner] Avoid a caching issue when running --edit mode on multiple files.
Snapshot all analysing files before running the tool, this makes sure
that we analyse all files statelessly and avoid the FileManager caching issue
when running `-edit` on multiple files.

Differential Revision: https://reviews.llvm.org/D155195
2023-07-18 13:21:23 +02:00
Haojian Wu
7a328cf539 [include-cleaner] Bail out in the standalone tool for invalid ignore-headers
flag
2023-07-14 10:05:22 +02:00
Haojian Wu
7f3d2cd7ec [include-cleaner] Fix the fixIncludes API not respect main-file header.
The fixIncludes was using the `input` as the main file path, this will
results in inserting header at wrong places.

We need the main file path to so that we can get the real main-file
header.

Differential Revision: https://reviews.llvm.org/D154950
2023-07-11 15:02:26 +02:00
Haojian Wu
507d766d76 [include-cleaner] Add an IgnoreHeaders flag to the command-line tool.
Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D153340
2023-07-06 11:18:44 +02:00
Haojian Wu
67e94c9dc8 [include-cleaner] Ignore the layering-violation errors for the standalone tool
If the source code is compilable-but-layering-violation, we still want
the tool to be functional on it (rather than bailing out).

Differential Revision: https://reviews.llvm.org/D154477
2023-07-05 13:30:26 +02:00
Haojian Wu
e339b07944 [include-cleaner] No need to overwrite the source file if there is no
cleanups
2023-06-22 14:37:11 +02:00
Haojian Wu
ab32cc6c02 [include-cleaner] Bailout on invalid code for the command-line tool
The binary tool only works on working source code, if the source code is
not compilable, don't perform any analysis and edits.

Differential Revision: https://reviews.llvm.org/D153271
2023-06-20 09:07:27 +02:00
Haojian Wu
b6f48341c5 [include-cleaner] Fix an unintended early return when checking the
incompatible flags in the CLI tool.
2023-02-27 10:09:40 +01:00
Haojian Wu
4702d8d9cf [include-cleaner] Print the line number of removal #includes.
I found that this information is helpful when using this tool.

Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D139715
2022-12-14 10:52:13 +01:00
Haojian Wu
14af1ad4ee [include-cleaner] Add two command-line flags to the include-cleaner
category.
2022-12-05 12:20:34 +01:00
Jun Zhang
8431436e54
Link with missing libs to fix broken shared build
Signed-off-by: Jun Zhang <jun@junz.org>

Differential Revision: https://reviews.llvm.org/D139202
2022-12-03 00:01:37 +08:00
Sam McCall
1a8dd74258 [include-cleaner] clang-include-cleaner can print/apply edits
This adds command-line flags to the tool:
+ -print: prints changed source code
+ -print=changes: prints headers added/removed
+ -edit: rewrites code in place
+ -insert=0/-remove=0: disables additions/deletions for the above

These are supported by a couple of new functions dumped into Analysis:
analyze() sits on top of walkUsed and makes used/unused decisions for
Includes. fixIncludes() applies those results to source code.

Differential Revision: https://reviews.llvm.org/D139013
2022-12-02 13:16:58 +01:00
Sam McCall
6a95e67323 [include-cleaner] HTMLReport shows headers that would be inserted
Demo: 7911d8251c/PathMapping.cpp.html

Header insertion doesn't actually work that well (not this patch's fault):
- we don't have ranking of locations/headers yet, so inserted header is pretty
  random
- on my system, we get a lot of absolute "/usr/bin/../include/..." paths.
  This is a HeaderSearch bug introduced in D60873 that I'll send a fix for

Differential Revision: https://reviews.llvm.org/D138676
2022-11-25 11:57:35 +01:00
Sam McCall
19ab2a671e [include-cleaner] Show includes matched by refs in HTML report.
Demo: 8736e64c45/ASTTests.html%25202

Differential Revision: https://reviews.llvm.org/D138219
2022-11-23 13:41:24 +01:00
Fangrui Song
46202543c9 [CMake] Remove a duplicate clangLex entry 2022-11-18 00:58:04 +00:00
Fangrui Song
f962dafbbd [include-cleaner] Fix -DBUILD_SHARED_LIBS=on build
and apply an include-cleaner fix for the tool itself:)

addPPCallbacks requires the vtable of PPChainedCallbacks and needs a clangLex
dependency.
2022-11-18 00:56:04 +00:00
Alexander Shaposhnikov
2483a7fffb [include-cleaner] Fix the build of include-cleaner with LLVM_BUILD_LLVM_DYLIB=ON 2022-11-18 00:54:45 +00:00
Sam McCall
2675773258 [include-cleaner] add macro symbols and implicit refs to HTML report
Demo: 8c5c5ad927/ASTTests.html

Differential Revision: https://reviews.llvm.org/D137677
2022-11-17 17:29:41 +01: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
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