9059 Commits

Author SHA1 Message Date
AMS21
dc4359fecf [clang-tidy] Fix wrong code generation for modernize-loop-convert with structured bindings.
Fixes llvm#62951

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D152852
2023-06-14 18:57:29 +00:00
Nathan Ridge
19c9af81b1 [clangd] Unwrap type sugar in HeuristicResolver::resolveTypeToRecordDecl()
Fixes https://github.com/clangd/clangd/issues/1663

Differential Revision: https://reviews.llvm.org/D152500
2023-06-14 13:51:00 -04:00
Viktoriia Bakalova
6a6c7ed5cd [clangd] Use include_cleaner spelling strategies in clangd.
Differential Revision: https://reviews.llvm.org/D152913
2023-06-14 14:06:35 +00:00
Nemanja Ivanovic
ffd7a200fd [clang-tidy] Fix build bot break after 474a2b9367ad
The commmit added clang-tidy checks without adding
the required library to the link step.
Caused failures with shared library builds.
2023-06-14 06:47:34 -05:00
AMS21
474a2b9367 [clang-tidy] Add more checks for functions which should be noexcept
Added new checks
- `performance-noexcept-destructor`
- `performance-noexcept-swap`

Also added cppcoreguidlines aliases for the 2 new checks as well as `performance-noexcept-move-constructor`

This fixes llvm#62154

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D148697
2023-06-13 18:56:57 +00:00
Mike Crowe
6eb6ee3cfe [clang-tidy] Move formatDereference to FixitHintUtils
I'd like to use RedundantStringCStrCheck's formatDereference function
from the up-coming modernize-use-std-print check. Let's move it to
FixItHintUtils so that the implementation can be shared.

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D150602
2023-06-13 18:54:49 +00:00
AMS21
311091e2b0 [clang-tidy] Fix crash in modernize-use-default-member-init
This was causes by `getValueOfValueInit` unconditionally calling
`getScalarTypeKind` on the member type, which would then trigger an
assertions since arrays are not scalar type.

This fixes llvm#63285

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D152802
2023-06-13 18:13:52 +00:00
AMS21
92420f4aef [clang-tidy] Fix false positive in readability-named-parameter for defaulted out-of-line special member functions
This fixes llvm#63056

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D152825
2023-06-13 18:13:52 +00:00
Haojian Wu
48967c6e55 [include-cleaner] Don't apply the PreferredHeader hint for standard headers.
Fixes https://github.com/llvm/llvm-project/issues/62635

Differential Revision: https://reviews.llvm.org/D152801
2023-06-13 18:57:45 +02:00
Vitaly Buka
0e8384a0fe [clangd] Initialize local variable
desugarForDiagnostic only sets value to true.
2023-06-13 08:58:46 -07:00
Younan Zhang
5cdb906f1e [clangd] Unify printing policy for type hints
(This patch addresses the comment from https://reviews.llvm.org/D151785#4402460.)

Previously, we used a special printing policy that enabled `PrintCanonicalTypes`
to print type hints for structure bindings. This was intended to
eliminate type aliases like `tuple_element::type`. However, this also
caused TypePrinter to print default template arguments, which could
result in losing the ability to see types like `std::basic_string<char>`
if the fully expanded template-id exceeded the default inlay hint threshold.

Simply getting the canonical type at the call site could help us get rid of
the side effect.

This also merges overloaded `addTypeHint` into one function without
`PrintingPolicy`.

Reviewed By: nridge

Differential Revision: https://reviews.llvm.org/D152520
2023-06-13 21:31:10 +08:00
Younan Zhang
7d68f2ef41 [clangd] Desugar template parameter aliases in type hints
This patch alleviates https://github.com/clangd/clangd/issues/1298.

Containers in C++ such as `std::vector` or `llvm::SmallVector`,
introduce a series of type aliases to adapt to generic algorithms.

Currently, If we write an declarator involving expressions with
these containers and `auto` placeholder, we probably obtain opaque
type alias like following:

```
std::vector<int> v = {1, 2, 3};
auto value = v[1]; // hint for `value`: value_type
auto *ptr = &v[0]; // hint for `ptr`: value_type *
```

These hints are useless for most of the time. It would be nice if we
desugar the type of `value_type` and print `int`, `int *` respectively
in this situation. But note we can't always prefer desugared type
since user might introduce type-aliases for brevity, where printing
sugared types makes more sense.

This patch introduces a heuristic method that displays the desugared
type that is an alias of template parameter. It merges
analogous method `shouldPrintCanonicalType` into `maybeDesugar` as well.

Previous commit for shouldPrintCanonicalType: dde8a0fe91cc

Reviewed By: nridge

Differential Revision: https://reviews.llvm.org/D151785
2023-06-13 19:15:24 +08:00
indev
2499beeb5a [clang-tidy] run-clang-tidy: forward warnings-as-errors argument
Forward `warnings-as-errors` argument to clang-tidy.

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D152625
2023-06-12 12:24:08 +00:00
Kugan Vivekanandarajah
a8ad413f0d [RFC][clangd] Move preamble index out of document open critical path
We would like to move the preamble index out of the critical path.
This patch is an RFC to get feedback on the correct implementation and potential pitfalls to keep into consideration.

I am not entirely sure if the lazy AST initialisation would create using Preamble AST in parallel. I tried with tsan enabled clangd but it seems to work OK (at least for the cases I tried)

Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D148088
2023-06-12 12:56:41 +01:00
Kadir Cetinkaya
ea20f339d9
[clangd] Enforce strict unused includes by default
Depends on D152685

Differential Revision: https://reviews.llvm.org/D152686
2023-06-12 11:47:03 +02:00
Kadir Cetinkaya
031ffc3e06
[clangd] Decouple IncludeCleaner implementation from Config
This should help managing tests as we change defaults in configs.

Differential Revision: https://reviews.llvm.org/D152685
2023-06-12 11:38:58 +02:00
Piotr Zegar
529633ccfd Revert "[clang-tidy] Move formatDereference to FixitHintUtils"
This reverts commit 636c672751425f1c73c28bea57c1913043cd21b2.
2023-06-12 06:27:20 +00:00
Mike Crowe
636c672751 [clang-tidy] Move formatDereference to FixitHintUtils
I'd like to use RedundantStringCStrCheck's formatDereference function
from the up-coming modernize-use-std-print check. Let's move it to
FixItHintUtils so that the implementation can be shared.

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D150602
2023-06-12 06:09:17 +00:00
Piotr Zegar
4099053646 [clang-tidy][NFC] Sort changes by check name in ReleaseNotes
Sort changes listed in ClangTidy release notes by
a check name.
2023-06-12 06:07:27 +00:00
Felix
90bbe97036 [clang-tidy] Readability-container-data-pointer adds new option to ignore Containers
Adds a new option to the clang-tidy's check : readability-container-data-pointer to ignore some containers.

This option is useful in the case of std::array where the size is known at compile time and there is no real risk to access the first index of the container. In that case some users might prefer to ignore this type of container.

Relates to : https://github.com/llvm/llvm-project/issues/57445

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D133244
2023-06-12 06:07:27 +00:00
Deniz Evrenci
daac014fec [clang-tidy] Check functions called from catch blocks
These functions can rethrow a current exception that is caught by the
catch block. We can pass the currently caught excections to the function
declaration analyzer just like the statement analyzer to handle this
case.

Differential Revision: https://reviews.llvm.org/D152330
2023-06-11 16:40:29 +01:00
Piotr Zegar
8fdedcd1a2 [clang-tidy] Optimize misc-confusable-identifiers
This is final optimization for this check. Main
improvements comes from changing a logic order
in mayShadow function, to first validate result
of mayShadowImpl, then search primary context in
a vectors. Secondary improvement comes from excluding
all implicit code by using TK_IgnoreUnlessSpelledInSource.
All other changes are just cosmetic improvements.

Tested on Cataclysm-DDA open source project, result in
check execution time reduction from 3682 seconds to
100 seconds (~0.25s per TU). That's 97.2% reduction for
this change alone. Resulting in cumulative improvement for
this check around -99.6%, finally bringing this check
into a cheap category.

Reviewed By: serge-sans-paille

Differential Revision: https://reviews.llvm.org/D151594
2023-06-10 11:06:49 +00:00
Yitzhak Mandelbaum
2adf9c9f50 [clang-tidy] Fix error in documentation of bugprone-unchecked-optional-access.
The documentation claims that the check recognizes `ASSERT_THAT`, but it doesn't
recognize any googletest macros at the moment. This patch removes the reference.
2023-06-09 20:42:09 +00:00
Christian Kandeler
e72baa76b9 [clangd] Add semantic token for labels
Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D143260
2023-06-07 12:28:06 +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
Sam McCall
3f9f146371 [clangd] Move completion signatures and labelDetails
(When clients support it, otherwise keep the existing rendering).

In VSCode this makes the signature darker.

Differential Revision: https://reviews.llvm.org/D151253
2023-06-05 18:05:18 +02:00
SR_team
4cb5e436ae [clangd] Show size, offset and padding for bit fields on hover
Examle:
```
struct test {
	char a;
	char b : 3;
	char c : 5;
	int d;
	int e : 27;
};
```

{F27617774}
{F27617776}
{F27617777}
{F27617780}

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D151128
2023-06-05 17:52:34 +02:00
Viktoriia Bakalova
cd5fcea6d4 [clangd] Revert to older include spelling approach. 2023-06-05 15:18:28 +00:00
Philipp Stephani
f69110dcc9 Check for a ‘buffer’ type instead of ‘buffer-live’.
Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D148918
2023-06-05 15:10:31 +02:00
Haojian Wu
fc4a8bf7da [clang-tidy] Move the misc system header dir to Inputs subdir, NFC.
Follows the existing patterns, and makes life easier for bazel build
system.
2023-06-05 13:57:01 +02:00
Viktoriia Bakalova
90c5fe9822 [include-cleaner] Allow multiple strategies for spelling includes.
Summary:

Reviewers:

Subscribers:

Differential Revision: https://reviews.llvm.org/D150185
2023-06-05 09:47:12 +00:00
Nathan Ridge
8ec44987e5 [clangd] Do not end inactiveRegions range at position 0 of line
This carries over the fix previously made for semantic highlighting
https://reviews.llvm.org/D92148, to the new inactiveRegions
protocol as well.

In addition, the directives at the beginning and end of an
inactive region are now excluded from the region.

Fixes https://github.com/clangd/clangd/issues/1631
Fixes https://github.com/clangd/clangd/issues/773

Differential Revision: https://reviews.llvm.org/D151190
2023-06-05 03:51:15 -04:00
Martin Storsjö
ac0ea7555e [clang-tools-extra] Fix linking when built with CLANG_LINK_CLANG_DYLIB=ON
clangIncludeCleaner isn't part of libclang-cpp, so link it with
target_link_libraries instead of clang_target_link_libraries.

This fixes a regression from
c28506ba4b6961950849f8fdecd0cf7e503a14f9.

Fix suggested by Michal Gorny.
2023-06-03 23:25:33 +03:00
Joseph Huber
860e439fb2 [Clang] Fix missing libraries for the include cleaner check
Summary:
Recently, the changes in https://reviews.llvm.org/D148793 introduced
some extra dependencies that caused link failured on my machine. This
patch adds the necessary libraries to resolve the link failures and
allow me to build again.
2023-06-02 12:43:55 -05:00
Viktoriia Bakalova
5c2072e74b [clang-tidy] Fix docs. 2023-06-02 15:31:55 +00:00
Viktoriia Bakalova
c28506ba4b [clang-tidy] Implement an include-cleaner check.
Differential Revision: https://reviews.llvm.org/D148793
2023-06-02 15:21:20 +00:00
Haojian Wu
be8da1f6e6 [clangd] Use FileManager for getCanonicalPath, NFC
get rid of the SourceManager dependency -- getCanonicalPath doesn't use
other SourceManager fields.
2023-06-02 12:55:21 +02:00
Haojian Wu
ad0543f4ea [clangd] NFC, use const HeaderSearch when possible. 2023-06-01 15:35:57 +02:00
Haojian Wu
7de541235f [include-cleaner] NFC, use const HeaderSearch when possible. 2023-06-01 15:26:19 +02:00
Haojian Wu
b3e38a1743 [clangd] NFC, remove an unused member in
IncludeStructure::RecordHeaders.
2023-06-01 14:53:33 +02:00
Jan Svoboda
2d817d0368 [clang] NFCI: Use the *Ref() variant on search paths
This removes some uses of the deprecated `DirectoryEntry::getName()`.
2023-05-31 16:04:45 -07:00
Richard Sandiford
301eb6b68f [clang] Add support for “regular” keyword attributes
Platform-specific language extensions often want to provide a way of
indicating that certain functions should be called in a different way,
compiled in a different way, or otherwise treated differently from a
“normal” function.  Honoring these indications is often required for
correctness, rather being than an optimization/QoI thing.

If a function declaration has a property P that matters for correctness,
it will be ODR-incompatible with a function that does not have property P.
If a function type has a property P that affects the calling convention,
it will not be two-way compatible with a function type that does not
have property P.  These properties therefore affect language semantics.
That in turn means that they cannot be treated as standard [[]]
attributes.

Until now, many of these properties have been specified using GNU-style
attributes instead.  GNU attributes have traditionally been more lax
than standard attributes, with many of them having semantic meaning.
Examples include calling conventions and the vector_size attribute.

However, there is a big drawback to using GNU attributes for semantic
information: compilers that don't understand the attributes will
(by default) emit a warning rather than an error.  They will go on to
compile the code as though the attributes weren't present, which will
inevitably lead to wrong code in most cases.  For users who live
dangerously and disable the warning, this wrong code could even be
generated silently.

A more robust approach would be to specify the properties using
keywords, which older compilers would then reject.  Some vendor-specific
extensions have already taken this approach.  But traditionally, each
such keyword has been treated as a language extension in its own right.
This has three major drawbacks:

(1) The parsing rules need to be kept up-to-date as the language evolves.

(2) There are often corner cases that similar extensions handle differently.

(3) Each extension requires more custom code than a standard attribute.

The underlying problem for all three is that, unlike for true attributes,
there is no established template that extensions can reuse.  The purpose
of this patch series is to try to provide such a template.

One option would have been to pick an existing keyword and do whatever
that keyword does.  The problem with that is that most keywords only
apply to specific kinds of types, kinds of decls, etc., and so the
parsing rules are (for good reason) not generally applicable to all
types and decls.

Really, the “only” thing wrong with using standard attributes is that
standard attributes cannot affect semantics.  In all other respects
they provide exactly what we need: a well-defined grammar that evolves
with the language, clear rules about what an attribute appertains to,
and so on.

This series therefore adds keyword “attributes” that can appear
exactly where a standard attribute can appear and that appertain
to exactly what a standard attribute would appertain to.  The link is
mechanical and no opt-outs or variations are allowed.  This should
make the keywords predictable for programmers who are already
familiar with standard attributes.

This does mean that these keywords will be accepted for parsing purposes
in many more places than necessary.  Inappropriate uses will then be
diagnosed during semantic analysis.  However, the compiler would need
to reject the keywords in those positions whatever happens, and treating
them as ostensible attributes shouldn't be any worse than the alternative.
In some cases it might even be better.  For example, SME's
__arm_streaming attribute would make conceptual sense as a statement
attribute, so someone who takes a “try-it-and-see” approach might write:

  __arm_streaming { …block-of-code…; }

In fact, we did consider supporting this originally.  The reason for
rejecting it was that it was too difficult to implement, rather than
because it didn't make conceptual sense.

One slight disadvantage of the keyword-based approach is that it isn't
possible to use #pragma clang attribute with the keywords.  Perhaps we
could add support for that in future, if it turns out to be useful.

For want of a better term, I've called the new attributes "regular"
keyword attributes (in the sense that their parsing is regular wrt
standard attributes), as opposed to "custom" keyword attributes that
have their own parsing rules.

This patch adds the Attr.td support for regular keyword attributes.
Adding an attribute with a RegularKeyword spelling causes tablegen
to define the associated tokens and to record that attributes created
with that syntax are regular keyword attributes rather than custom
keyword attributes.

A follow-on patch contains the main Parse and Sema support,
which is enabled automatically by the Attr.td definition.

Other notes:

* The series does not allow regular keyword attributes to take
arguments, but this could be added in future.

* I wondered about trying to use tablegen for
TypePrinter::printAttributedAfter too, but decided against it.
RegularKeyword is really a spelling-level classification rather
than an attribute-level classification, and in general, an attribute
could have both GNU and RegularKeyword spellings.  In contrast,
printAttributedAfter is only given the attribute kind and the type
that results from applying the attribute.  AFAIK, it doesn't have
access to the original attribute spelling.  This means that some
attribute-specific or type-specific knowledge might be needed
to print the attribute in the best way.

* Generating the tokens automatically from Attr.td means that
pseudo's libgrammar does now depend on tablegen.

* The patch uses the SME __arm_streaming attribute as an example
for testing purposes.  The attribute does not do anything at this
stage.  Later SME-specific patches will add proper semantics for it,
and add other SME-related keyword attributes.

Differential Revision: https://reviews.llvm.org/D148700
2023-05-31 10:43:10 +01:00
Balázs Kéri
b0bab14b8b [clang-tidy] Add check bugprone-unique-ptr-array-mismatch.
Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D151431
2023-05-31 09:55:01 +02:00
Timm Bäder
f63155aaa6 [clang] Show line numbers in diagnostic code snippets
Show line numbers to the left of diagnostic code snippets and increase
the numbers of lines shown from 1 to 16.

Differential Revision: https://reviews.llvm.org/D147875
2023-05-31 07:26:03 +02:00
Jan Svoboda
e6830b6028 [clang][modules] NFCI: Extract optionality out of Module::{Header,DirectoryName}
Most users of `Module::Header` already assume its `Entry` is populated. Enforce this assumption in the type system and handle the only case where this is not the case by wrapping the whole struct in `std::optional`. Do the same for `Module::DirectoryName`.

Depends on D151584.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D151586
2023-05-30 21:06:51 -07:00
Jan Svoboda
d574e918db [clang][lex] NFCI: Use DirectoryEntryRef in ModuleMap::parseModuleMapFile()
This patch changes the argument type of `ModuleMap::parseModuleMapFile()` from `const DirectoryEntry *` to `DirectoryEntryRef` in order to remove the deprecated uses of `DirectoryEntry::getName()`.

Depends on D127648.

Reviewed By: bnbarham

Differential Revision: https://reviews.llvm.org/D127651
2023-05-30 13:54:06 -07:00
Alex Brachet
54d45ddc89 [clang-tidy][docs] Fix link to libc style guide
Differential Revision: https://reviews.llvm.org/D151502
2023-05-30 18:22:26 +00:00
Deniz Evrenci
6219b7c61a [clang-tidy] Do not emit bugprone-exception-escape warnings from coroutines
All exceptions thrown in coroutine bodies are caught and
unhandled_exception member of the coroutine promise type is called.
In accordance with the existing rules of diagnostics related to
exceptions thrown in functions marked noexcept, even if the promise
type's constructor, get_return_object, or unhandled_exception
throws, diagnostics should not be emitted.

Fixes #61905.

Reviewed By: PiotrZSL, ChuanqiXu

Differential Revision: https://reviews.llvm.org/D147417
2023-05-30 17:48:11 +00:00
Kazu Hirata
5bbb66cae7 [clangd] Remove unused variable IsBroadcasting
The variable was introduced without a use by:

  commit 2bebc3d0602b407b3f351e782940959da5808f97
  Author: Sam McCall <sam.mccall@gmail.com>
  Date:   Tue Nov 20 10:56:03 2018 +0000
2023-05-29 14:17:03 -07:00
Kazu Hirata
32fc78c26f [cland] Remove unused declaration getCurrentPreamble
The corresponding function definition was removed by:

  commit 2214b9076f1d3a4784820c4479e2417685e5c980
  Author: Kadir Cetinkaya <kadircet@google.com>
  Date:   Thu Apr 2 10:53:23 2020 +0200
2023-05-29 14:17:01 -07:00