219 Commits

Author SHA1 Message Date
Matheus Izvekov
dc17429ae6
[clang] improved preservation of template keyword (#133610) 2025-04-01 17:15:18 -03:00
kadir çetinkaya
fcb12346dd
[include-cleaner] Dont report explicit refs for global operator new/delete (#125199)
These are available for all translations implicitly. We shouldn't report
explicit refs and enforce includes.
2025-01-31 14:01:45 +01:00
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
Benjamin Kramer
35c5e56b61 Clean up -Wdangling-assignment-gsl in clang and mlir
These are triggering after b037bceef6a40c5c00c1f67cc5a334e2c4e5e041.
2025-01-08 14:46:15 +01:00
Kadir Cetinkaya
df9a14d7bb
Reapply "[NFC] Explicitly pass a VFS when creating DiagnosticsEngine (#115852)"
This reverts commit a1153cd6fedd4c906a9840987934ca4712e34cb2 with fixes
to lldb breakages.

Fixes https://github.com/llvm/llvm-project/issues/117145.
2024-11-21 14:55:30 +01:00
Sylvestre Ledru
a1153cd6fe Revert "[NFC] Explicitly pass a VFS when creating DiagnosticsEngine (#115852)"
Reverted for causing:
https://github.com/llvm/llvm-project/issues/117145

This reverts commit bdd10d9d249bd1c2a45e3de56a5accd97e953458.
2024-11-21 13:04:30 +01:00
kadir çetinkaya
bdd10d9d24
[NFC] Explicitly pass a VFS when creating DiagnosticsEngine (#115852)
Starting with 41e3919ded78d8870f7c95e9181c7f7e29aa3cc4 DiagnosticsEngine
creation might perform IO. It was implicitly defaulting to
getRealFileSystem. This patch makes it explicit by pushing the decision
making to callers.

It uses ambient VFS if one is available, and keeps using
`getRealFileSystem` if there aren't any VFS.
2024-11-21 12:11:41 +01:00
Thomas Fransham
b735c66da9
Reland 'Update llvm::Registry to work for LLVM shared library builds on windows' (#109024) (#112640)
Fix missing extern templates for llvm::Registry use in other projects of
llvm

Windows doesn't implicitly import and merge exported symbols across
shared libraries
like Linux does so we need to explicitly export/import each
instantiation of llvm::Registry.
Updated LLVM_INSTANTIATE_REGISTRY to just be a full explicit template
instantiation.

This is part of the work to enable LLVM_BUILD_LLVM_DYLIB and LLVM
plugins on window.
2024-10-22 10:29:27 +03: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
Thomas Fransham
1f2c08b33b
[clang-tools-extra] Fix add_clang_library usage (#109321)
If a add_clang_library call doesn't specify building as static or shared
library they are implicitly added to the list static libraries that is
linked in to clang-cpp shared library here.

315ba77406/clang/cmake/modules/AddClang.cmake (L107)
Because the clang-tools-extra libraries targets were declared after
clang-cpp they by luck never got linked to clang-cpp.
This change is required for clang symbol visibility macros on windows to
work correctly for clang tools since we need to distinguish if a target
being built will be importing or exporting clang symbols from the
clang-cpp DLL.
2024-10-08 09:22:19 +03: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
Jan Svoboda
b1aea98cfa
[clang] Make deprecations of some FileManager APIs formal (#110014)
Some `FileManager` APIs still return `{File,Directory}Entry` instead of
the preferred `{File,Directory}EntryRef`. These are documented to be
deprecated, but don't have the attribute that warns on their usage. This
PR marks them as such with `LLVM_DEPRECATED()` and replaces their usage
with the recommended counterparts. NFCI.
2024-09-25 10:36:44 -07:00
Youngsuk Kim
f5838cc17f [clang-tools-extra] Don't flush llvm::raw_string_ostream (NFC)
Don't call raw_string_ostream::flush(), which is essentially a no-op.
As specified in the docs, raw_string_ostream is always unbuffered.
( 65b13610a5226b84889b923bae884ba395ad084d for further reference )
2024-09-25 06:12:45 -05: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
60ed1043d7
[include-cleaner] Report refs for enum constants used through namespace aliases (#106706) 2024-09-02 14:16:49 +02:00
kadir çetinkaya
acff429191
[include-cleaner] Mark RecordDecls referenced in UsingDecls as explicit (#106430)
We were reporting ambigious references from using declarations as user
can be depending on different overloads of a function just because they
are visible in the TU.
This doesn't apply to records, or primary templates as declaration being
referenced in such cases is unambigious, the ambiguity applies to
specializations though.

Hence this patch returns an explicit reference to record decls and
primary templates of those.
2024-08-29 14:23:57 +02:00
kadir çetinkaya
74b538d7e6
[include-cleaner] Turn new/delete usages to ambiguous references (#105844)
In practice most of these expressions just resolve to implicitly
provided `operator new` and standard says it's not necessary to include
`<new>` for that.
Hence this is resulting in a lot of churn in cases where inclusion of
`<new>` doesn't matter, and might even be undesired by the developer.

By switching to an ambiguous reference we try to find a middle ground
here, ensuring that we don't drop providers of `operator new` when the
developer explicitly listed them in the includes, and chose to believe
it's the implicitly provided `operator new` and don't insert an include
in other cases.
2024-08-24 02:53:59 +02:00
Kadir Cetinkaya
14d57e21e1
[include-cleaner] fix 32-bit buildbots after a426ffdee1ca7814f2684b6 2024-08-16 16:17:07 +02:00
kadir çetinkaya
a426ffdee1
[include-cleaner] Add handling for new/delete expressions (#104033) 2024-08-16 13:17:58 +02:00
Haojian Wu
3eaf483c29 [include-cleaner] Remove two commented-out lines of code. 2024-08-15 08:44:46 +02:00
Kadir Cetinkaya
778a1f29fc
[include-cleaner] Fix windows buildbots after 64d9713637ab9 2024-08-13 13:28:07 +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
2f5dc596b5
[IncludeCleaner] Also check for spellings of physical headers (#99843)
Some physical headers can have "conflicting" spellings, when same
filename exists under two different include search paths. e.g.
<stdlib.h>
can be provided by both standard library and underlying libc headers. In
such scenarios if the usage is from the latter include-search path,
users can't spell it directly.

This patch ensures we also consider spellings of such includes, in
addition to their physical files to prevent conflicting suggestions.
2024-07-22 14:58:07 +02:00
Mital Ashok
482c41e992
[Clang] [Sema] Diagnose unknown std::initializer_list layout in SemaInit (#95580)
This checks if the layout of `std::initializer_list` is something Clang
can handle much earlier and deduplicates the checks in
CodeGen/CGExprAgg.cpp and AST/ExprConstant.cpp

Also now diagnose `union initializer_list` (Fixes #95495), bit-field for
the size (Fixes a crash that would happen during codegen if it were
unnamed), base classes (that wouldn't be initialized) and polymorphic
classes (whose vtable pointer wouldn't be initialized).
2024-06-20 19:44:06 +02:00
Sam McCall
5574a5894f
[include-cleaner] don't consider the associated header unused (#67228)
Loosely, the "associated header" of `foo.cpp` is `foo.h`.
It should be included, many styles include it first.

So far we haven't special cased it in any way, and require this include
to be justified. e.g. if foo.cpp defines a function declared in foo.h,
then the #include is allowed to check these declarations match.

However this doesn't really align with what users want:
- people reasonably want to include the associated header for the
  side-effect of validating that it compiles.
  In the degenerate case, `lib.cpp`is just `#include "lib.h"` (see bug)
- That `void foo(){}` IWYU-uses `void foo();` is a bit artificial, and
  most users won't internalize this. Instead they'll stick with the
  simpler model "include the header that defines your API".
  In the rare cases where these give different answers[1], our current
  behavior is a puzzling special case from the user POV.
  It is more user-friendly to accept both models.
- even where this diagnostic is a true positive (defs don't match header
  decls) the diagnostic does not communicate this usefully.

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

[1] Example of an associated header that's not IWYU-used:
```
// http.h
inline URL buildHttpURL(string host, int port, string path) {
  return "http://" + host + ":" + port + "/" + path;
}
// http.cpp
class HTTPURLHandler : URLHandler { ... };
REGISTER_URL_HANDLER("http", HTTPURLHandler);
```
2024-06-19 15:29:05 +02:00
Kadir Cetinkaya
e7b4b437fb
[include-cleaner] Use filename as requested, not resolved path
This was an unintended change in d5297b72aa32ad3a69563a1fcc61294282f0b379.
We don't want to resolve symlinks in filenames, as these might lead to
unexpected spellings, compared to requested filenames.
2024-06-19 12:04:42 +02:00
Kadir Cetinkaya
ae0813f2d7
Revert "[clang-tidy] fix false positives for the functions with the same name as standard library functions in misc-include-cleaner (#94923)"
This reverts commit 1bae10879d9183c5edfb709c36b55086ebc772f0. Also add
some test cases to prevent further regressions.
2024-06-18 09:15:30 +02:00
kadir çetinkaya
d5297b72aa
[include-cleaner] Pass WorkingDir to suggestPathToFileForDiagnostics (#95114)
Addresses https://github.com/llvm/llvm-project/issues/81215.
2024-06-14 15:45:04 +02:00
Congcong Cai
1bae10879d
[clang-tidy] fix false positives for the functions with the same name as standard library functions in misc-include-cleaner (#94923)
Fixes: #93335
For decl with body, we should provide physical locations also. Because
it may be the function which have the same name as std library.
2024-06-11 21:01:52 +08:00
Michael Kruse
c87a7b3bdb
[clang-tools-extra] Revise IDE folder structure (#89744)
Update the folder titles for targets in the monorepository that have not
seen taken care of for some time. These are the folders that targets are
organized in Visual Studio and XCode
(`set_property(TARGET <target> PROPERTY FOLDER "<title>")`)
when using the respective CMake's IDE generator.

 * Ensure that every target is in a folder
 * Use a folder hierarchy with each LLVM subproject as a top-level folder
 * Use consistent folder names between subprojects
 * When using target-creating functions from AddLLVM.cmake, automatically
deduce the folder. This reduces the number of
`set_property`/`set_target_property`, but are still necessary when
`add_custom_target`, `add_executable`, `add_library`, etc. are used. A
LLVM_SUBPROJECT_TITLE definition is used for that in each subproject's
root CMakeLists.txt.
2024-05-25 23:27:33 +02:00
Krystian Stasiowski
1202837302
Reapply "[Clang] Unify interface for accessing template arguments as written for class/variable template specializations (#81642)" (#91393)
Reapplies #81642, fixing the crash which occurs when running the lldb test suite.
2024-05-14 16:09:57 -04:00
Adrian Prantl
c6855ab24e Revert "[Clang] Unify interface for accessing template arguments as written for class/variable template specializations (#81642)"
This reverts commit 7115ed0fff027b65fa76fdfae215ed1382ed1473.

This commit broke several LLDB tests.

https://green.lab.llvm.org/job/llvm.org/view/LLDB/job/as-lldb-cmake/3480/
2024-05-07 13:03:14 -07:00
Krystian Stasiowski
7115ed0fff
[Clang] Unify interface for accessing template arguments as written for class/variable template specializations (#81642)
Our current method of storing the template arguments as written for
`(Class/Var)Template(Partial)SpecializationDecl` suffers from a number
of flaws:
- We use `TypeSourceInfo` to store `TemplateArgumentLocs` for class
template/variable template partial/explicit specializations. For
variable template specializations, this is a rather unintuitive hack (as
we store a non-type specialization as a type). Moreover, we don't ever
*need* the type as written -- in almost all cases, we only want the
template arguments (e.g. in tooling use-cases).
- The template arguments as written are stored in a number of redundant
data members. For example, `(Class/Var)TemplatePartialSpecialization`
have their own `ArgsAsWritten` member that stores an
`ASTTemplateArgumentListInfo` (the template arguments).
`VarTemplateSpecializationDecl` has yet _another_ redundant member
"`TemplateArgsInfo`" that also stores an `ASTTemplateArgumentListInfo`.

This patch eliminates all
`(Class/Var)Template(Partial)SpecializationDecl` members which store the
template arguments as written, and turns the `ExplicitInfo` member into
a `llvm::PointerUnion<const ASTTemplateArgumentListInfo*,
ExplicitInstantiationInfo*>` (to avoid unnecessary allocations when the
declaration isn't an explicit instantiation). The template arguments as
written are now accessed via `getTemplateArgsWritten` in all cases.

The "most breaking" change is to AST Matchers, insofar that `hasTypeLoc`
will no longer match class template specializations (since they no
longer store the type as written).
2024-05-07 14:45:52 -04:00
kadir çetinkaya
599adf30af
[include-cleaner] Dont apply name-match for non-owning headers (#82625) 2024-04-11 15:33:35 +02:00
Younan Zhang
c1c8a0cb17
[concepts] Preserve the FoundDecl of ConceptReference properly (#85032)
The `ConceptReference`'s `FoundDecl` claims it "can differ from
`NamedConcept` when, for example, the concept was found through a
`UsingShadowDecl`", but such the contract was not previously respected.

Fixes https://github.com/llvm/llvm-project/issues/82628
2024-03-14 10:09:47 +08:00
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
5cb2ebc08f
Reland "[clang] Preserve found-decl when constructing VarTemplateIds" (#82612)
Update include-cleaner tests. Now that we have proper found-decls set up
for VarTemplates, in case of instationtations we point to primary
templates and not specializations. To be changed in a follow-up patch.
2024-02-23 11:37:30 +01:00
Jan Svoboda
da95d926f6
[clang][lex] Always pass suggested module to InclusionDirective() callback (#81061)
This patch provides more information to the
`PPCallbacks::InclusionDirective()` hook. We now always pass the
suggested module, regardless of whether it was actually imported or not.
The extra `bool ModuleImported` parameter then denotes whether the
header `#include` will be automatically translated into import the the
module.

The main change is in `clang/lib/Lex/PPDirectives.cpp`, where we take
care to not modify `SuggestedModule` after it's been populated by
`LookupHeaderIncludeOrImport()`. We now exclusively use the `SM`
(`ModuleToImport`) variable instead, which has been equivalent to
`SuggestedModule` until now. This allows us to use the original
non-modified `SuggestedModule` for the callback itself.

(This patch turns out to be necessary for
https://github.com/apple/llvm-project/pull/8011).
2024-02-08 10:19:18 -08: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 çetinkaya
7837110ed8
[clangd] Track IWYU pragmas for non-preamble includes (#75612)
This makes PragmaIncldues copyable, and copies it from preamble when
building a
new AST.

Fixes https://github.com/clangd/clangd/issues/1843
Fixes https://github.com/clangd/clangd/issues/1571
2024-01-03 15:57:30 +01:00
Michael Spencer
a171d248ca
[clang][modules] Deprecate module.map in favor of module.modulemap (#75142)
This patch deprecates `module.map` in favor of `module.modulemap`, which
has been the preferred form since 2014. The eventual goal is to remove
support for `module.map` to reduce the number of stats Clang needs to do
while searching for module map files.

This patch touches a lot of files, but the majority of them are just
renaming tests or references to the file in comments or documentation.

The relevant files are:
* lib/Lex/HeaderSearch.cpp
* include/clang/Basic/DiagnosticGroups.td
* include/clang/Basic/DiagnosticLexKinds.td
2023-12-14 14:03:57 -08:00
Kazu Hirata
732bccb8c1 Use StringRef::{starts,ends}_with (NFC)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-14 07:53:20 -08:00
Jan Svoboda
cb92511c4d
[clang] NFC: Remove OptionalDirectoryEntryRefDegradesToDirectoryEntryPtr (#74900) 2023-12-08 18:22:53 -08:00
Kadir Cetinkaya
7aaa86b28d
[include-cleaner] Add regression tests for outliving File&Source Manager 2023-11-17 11:32:51 +01:00
kadir çetinkaya
fd2d5add43
[include-cleaner] Make sure exports of stdlib also works for physical files (#72246)
This was creating discrepancy in cases where we might have a physical
file entry (e.g. because we followed a source location from a stdlib
file) and tried to find its exporters.
2023-11-17 10:11:21 +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