Commit Graph

7887 Commits

Author SHA1 Message Date
Sam McCall 7cf97d62f4 [clang-tidy] Make header-guard check a little looser on comment whitespace
Currently it rejects "//  FOO_BAR_H" as an endif comment due to the extra space.
A user complained that this is too picky, which seems fair enough.

Differential Revision: https://reviews.llvm.org/D124955
2022-05-05 17:42:35 +02:00
Sam McCall 04b4190489 [Driver] Make "upgrade" of -include to include-pch optional; disable in clangd
If clang is passed "-include foo.h", it will rewrite to "-include-pch foo.h.pch"
before passing it to cc1, if foo.h.pch exists.

Existence is checked, but validity is not. This is probably a reasonable
assumption for the compiler itself, but not for clang-based tools where the
actual compiler may be a different version of clang, or even GCC.
In the end, we lose our -include, we gain a -include-pch that can't be used,
and the file often fails to parse.

I would like to turn this off for all non-clang invocations (i.e.
createInvocationFromCommandLine), but we have explicit tests of this behavior
for libclang and I can't work out the implications of changing it.

Instead this patch:
 - makes it optional in the driver, default on (no change)
 - makes it optional in createInvocationFromCommandLine, default on (no change)
 - changes driver to do IO through the VFS so it can be tested
 - tests the option
 - turns the option off in clangd where the problem was reported

Subsequent patches should make libclang opt in explicitly and flip the default
for all other tools. It's probably also time to extract an options struct
for createInvocationFromCommandLine.

Fixes https://github.com/clangd/clangd/issues/856
Fixes https://github.com/clangd/vscode-clangd/issues/324

Differential Revision: https://reviews.llvm.org/D124970
2022-05-05 16:47:17 +02:00
Sam McCall 40c13720a4 [Frontend] give createInvocationFromCommandLine an options struct
It's accumulating way too many optional params (see D124970)

While here, improve the name and the documentation.

Differential Revision: https://reviews.llvm.org/D124971
2022-05-05 15:12:07 +02:00
Yitzhak Mandelbaum 9a8d33dbd8 [clang-tidy] Escape diagnostic messages before passing to `diag` in Transformer.
Messages generated by Transformer rules may have `%` in them, which
needs to be escaped before being passed to `diag`, which interprets them
specially (and crashes if they are misused).

Differential Revision: https://reviews.llvm.org/D124952
2022-05-04 20:56:56 +00:00
Aaron Ballman 2cb2cd242c Change the behavior of implicit int diagnostics
C89 allowed a type specifier to be elided with the resulting type being
int, aka implicit int behavior. This feature was subsequently removed
in C99 without a deprecation period, so implementations continued to
support the feature. Now, as with implicit function declarations, is a
good time to reevaluate the need for this support.

This patch allows -Wimplicit-int to issue warnings in C89 mode (off by
default), defaults the warning to an error in C99 through C17, and
disables support for the feature entirely in C2x. It also removes a
warning about missing declaration specifiers that really was just an
implicit int warning in disguise and other minor related cleanups.
2022-05-04 08:35:47 -04:00
Weverything 0e86cddf98 [psuedo] Fix for unused warning by moving code into debug macro. 2022-05-03 16:07:59 -07:00
Haojian Wu c4546091ed [pseudo] Use a real language option in the parser.
Differential Revision: https://reviews.llvm.org/D124831
2022-05-03 22:24:56 +02:00
Haojian Wu ed1b32791d [pseudo] Print the GSS::Node details when the unittest fails, NFC. 2022-05-03 22:06:10 +02:00
Haojian Wu 9f38da258e [pseudo] Implement the GLR parsing algorithm.
This patch implements a standard GLR parsing algorithm, the
core piece of the pseudoparser.

- it parses preprocessed C++ code, currently it supports correct code
  only and parse them as a translation-unit;
- it produces a forest which stores all possible trees in an efficient
  manner (only a single node being build for per (SymbolID, Token Range));
  no disambiguation yet;

Reland with a fix for g++'s -fpermissive error on previous declaration `GSS& GSS;`.

Differential Revision: https://reviews.llvm.org/D121150
2022-05-03 20:25:23 +02:00
Haojian Wu 860eabb395 Revert "[pseudo] Implement the GLR parsing algorithm."
This breaks some buildbots (on the declaration GSS& GSS), will fix it
later.

This reverts commit eac22d0754.
2022-05-03 15:54:10 +02:00
Sam McCall eac22d0754 [pseudo] Implement the GLR parsing algorithm.
This patch implements a standard GLR parsing algorithm, the
core piece of the pseudoparser.

- it parses preprocessed C++ code, currently it supports correct code
  only and parse them as a translation-unit;
- it produces a forest which stores all possible trees in an efficient
  manner (only a single node being build for per (SymbolID, Token Range));
  no disambiguation yet;

Differential Revision: https://reviews.llvm.org/D121150
2022-05-03 15:42:07 +02:00
Haojian Wu b18abde8ad [pseudo] Simplify the forest dump, NFC.
The code was written to handle nullable grammar, and we disallow
nullable grammar, so it is not necessary to keep it around.

Differential Revision: https://reviews.llvm.org/D124827
2022-05-03 14:14:57 +02:00
Tobias Ribizel 043e965024 [clangd] Add inlay hints for mutable reference parameters
Add a & prefix to all parameter inlay hints that refer to a non-const l-value reference. That makes it easier to identify them even if semantic highlighting is not used (where this is already available)

Reviewed By: nridge

Differential Revision: https://reviews.llvm.org/D124359
2022-05-02 15:57:23 -04:00
Nathan James 8a9e2dd48d
[clang-tidy][NFC] Re-alphabetize the clang tidy release notes 2022-05-01 07:41:04 +01:00
Aaron Ballman a9d68a5524 Generalize calls to ImplicitlyDefineFunction
In C++ and C2x, we would avoid calling ImplicitlyDefineFunction at all,
but in OpenCL mode we would still call the function and have it produce
an error diagnostic. Instead, we now have a helper function to
determine when implicit function definitions are allowed and we use
that to determine whether to call ImplicitlyDefineFunction so that the
behavior is more consistent across language modes.

This changes the diagnostic behavior from telling the users that an
implicit function declaration is not allowed in OpenCL to reporting use
of an unknown identifier and going through typo correction, as done in
C++ and C2x.
2022-04-30 10:03:51 -04:00
Sam McCall 816399cac2 Reland [clangd] More precisely enable clang warnings through ClangTidy options
This reverts commit 26c82f3d1d.

When tests enable 'Checks: *', we may get extra diagnostics.
2022-04-30 11:07:11 +02:00
Nico Weber 26c82f3d1d Revert "[clangd] More precisely enable clang warnings through ClangTidy options"
This reverts commit 5227be8b6a.
Broke check-clangd, see comment on https://reviews.llvm.org/D124679
2022-04-29 20:34:10 -04:00
Sam McCall 5227be8b6a [clangd] More precisely enable clang warnings through ClangTidy options
clang-tidy's behavior is to add the -W flags, and then map all clang diagnostics
to "clang-diagnostic-foo" pseudo-check-names, then use Checks to filter those.

Previous to this patch, we were handling -W flags but not filtering the
diagnostics, assuming both sets of information encoded the same thing.

However this intersection is nontrivial when diagnostic group hierarchy is
involved. e.g. -Wunused + clang-diagnostic-unused-function should not enable
unused label warnings.

This patch more closely emulates clang-tidy's behavior, while not going to
the extreme of generating tidy check names for all clang diagnostics and
filtering them with regexes.

Differential Revision: https://reviews.llvm.org/D124679
2022-04-29 22:24:34 +02: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
Kadir Cetinkaya 5e4a77f0c4
[clangd] Record latency for ASTSignal derivation 2022-04-28 14:34:39 +02:00
Michael Forster 4aba5fa774 Use `-text` git attribute instead of `text eol=...`
These automatic conversions lead to issues in various workflows, and all
we want here are files that retain their line endings under all
circumstances. `-text` captures that perfectly well and leads to fewer
issues.

It is preferable to `binary`, because with `-text` we still get textual
diffs.

Differential Revision: https://reviews.llvm.org/D124606
2022-04-28 14:27:29 +02:00
Bernhard Manfred Gruber b1f1688e90 [clang-tidy] support --load in clang-tidy-diff.py/run-clang-tidy.py
Support for loading shared objects as plugins into clang-tidy was added
in http://reviews.llvm.org/D111100. Unfortunately, the utility scripts
`clang-tidy-diff.py` and `run-clang-tidy.py` did not receive
corresponding arguments to forward such plugins to clang-tidy.
This diff adds a `-load=plugin` option to both scripts.

Differential Revision: http://reviews.llvm.org/D12306

Reviewed By: aaron.ballman
2022-04-28 13:49:18 +02:00
Ilya Biryukov 2b833d4086 [AST] Improve traversal of concepts and concept requirements
- Do not traverse concept decl inside `AutoType`. We only traverse
  declaration and definitions, not references to a declaration.
- Do not visit implicit AST node the relevant traversal mode.
- Add traversal extension points for concept requirements.
- Renamed `TraverseConceptReference` to mark as helper to share
  the code. Having an extension point there seems confusing given that
  there are many concept refences in the AST that do not call the
  helper. Those are `AutoType`, `AutoTypeLoc` and constraint requirements.

Only clangd code requires an update.

There are no use-cases for concept requirement traversals yet, but
I added them in the earlier version of the patch and decided to keep
them for completeness.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D124532
2022-04-28 09:33:26 +00:00
Aaron Puchert c9a16e8c3d Drop '* text=auto' from .gitattributes and normalize
Git wants to check in 'text' files with LF endings, so this changes them
in the repository but not in the checkout, where they keep CRLF endings.

Differential Revision: https://reviews.llvm.org/D124563
2022-04-28 03:05:10 +02:00
Haojian Wu 8052f4d22a [AST] Consider QualifiedTemplateName in TemplateName::getAsUsingDecl().
If the underlying template name of a qualified template name is a using
decl, TemplateName::getAsUsingDecl() will return it.

This will make the UsingTemplateName consumer life easier.

Differential Revision: https://reviews.llvm.org/D124437
2022-04-27 14:16:19 +02:00
Tobias Ribizel 56058b921b [clangd] Output inlay hints with `clangd --check`
With the addition of inlay hints to clangd, it would be useful to output them during verbose `clangd --check`.
This patch adds an output step for inlay hints and unifies the way `--check-lines` are passed around

Reviewed By: nridge

Differential Revision: https://reviews.llvm.org/D124344
2022-04-27 02:36:09 -04:00
Richard 693246e03f [clang-tidy] Modernize-macro-to-enum should skip macros used in other macros
If a macro is used in the expansion of another macro, that can cause
a compile error if the macro is replaced with an enum.  Token-pasting is
an example where converting a macro defined as an integral constant can
cause code to no longer compile.

This change causes such macros to be skipped from the conversion
process in order to prevent fixits from creating code that no longer
compiles.

A subsequent enhancement will examine macro usage in more detail to
allow more cases to be handled without breaking code.

Differential Revision: https://reviews.llvm.org/D124316

Fixes #54948
2022-04-26 21:09:13 -06:00
Ilya Biryukov 75e16fd2c6 [Index] [clangd] Support for concept declarations and requires expressions
Add support for concepts and requires expression in the clang index.
Genarate USRs for concepts.

Also change how `RecursiveASTVisitor` handles return type requirement in
requires expressions. The new code unpacks the synthetic template parameter
list used for storing the actual expression. This simplifies
implementation of the indexing. No code seems to depend on the original
traversal anyway and the synthesized template parameter list is easily
accessible from inside the requires expression if needed.

Add tests in the clangd codebase.

Fixes https://github.com/clangd/clangd/issues/1103.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D124441
2022-04-26 13:50:01 +00:00
Haojian Wu 910fb5d7e0 [pseudo] NFC, fix some code-style naming violations. 2022-04-26 10:50:50 +02:00
Vitaly Buka 2d014b72cc [test][clangd] Use StringRef instead of std::string
runWithAST stores the first parameters as StringRef, so we can't
use temporarily std::string from parameter ID.
2022-04-25 16:38:18 -07:00
Aaron Puchert ac5f7be6a8 Move test/.gitattributes to clang-tools-extra/test
It was probably accidentally added there, see the discussion on change
D97625. It is certainly without effect, to quote gitattributes(5):

    When deciding what attributes are assigned to a path, Git consults
    [...], `.gitattributes` file in the same directory as the path in
    question, and its parent directories up to the toplevel of the work
    tree [...]

Running `git check-attr -a` on the files in question shows that now the
settings are indeed effective whereas before they were not.

Lastly, lit ignores the file like any dotfile, see getTestsInDirectory
of FileBasedTest in llvm/utils/lit/lit/formats/base.py. This can be
verified with `llvm-lit --show-tests clang-tools-extra/test`.
2022-04-24 22:30:25 +02:00
Jonas Toth 3f0f203666 run-clang-tidy: Fix infinite loop on windows
`find_compilation_database` checked only for "/" as exit point, but on Windows, this root is impossible.
Fixes #53642

Authored By: Febbe
Reviewed By: JonasToth
Differential Revision: https://reviews.llvm.org/D119481
2022-04-24 17:17:02 +02:00
Richard b985b6e3c1 [clang-tidy] Ignore macros defined within declarations
Modernize-macro-to-enum shouldn't try to convert macros to enums
when they are defined inside a declaration or definition, only
when the macros are defined at the top level.  Since preprocessing
is disconnected from AST traversal, match nodes in the AST and then
invalidate source ranges spanning AST nodes before issuing diagnostics.

ClangTidyCheck::onEndOfTranslationUnit is called before
PPCallbacks::EndOfMainFile, so defer final diagnostics to the
PPCallbacks implementation.

Differential Revision: https://reviews.llvm.org/D124066

Fixes #54883
2022-04-22 17:46:54 -06:00
Christopher Di Bella e9a902c7f7 Revert "Revert "Revert "[clang][pp] adds '#pragma include_instead'"""
> Includes regression test for problem noted by @hans.
> is reverts commit 973de71.
>
> Differential Revision: https://reviews.llvm.org/D106898

Feature implemented as-is is fairly expensive and hasn't been used by
libc++. A potential reimplementation is possible if libc++ become
interested in this feature again.

Differential Revision: https://reviews.llvm.org/D123885
2022-04-22 16:37:20 +00:00
Kadir Cetinkaya 2efccf5166
[clangd][NFC] Reduce memory usage while building dex
Reduce peak memory usage by tearing down the intermediate representation
as we build the final one. Rather than deleting it in the end.

Differential Revision: https://reviews.llvm.org/D124240
2022-04-22 17:44:47 +02:00
Sam McCall 5287237a74 [clangd] Fix CLANGD_TIDY_CHECKS=Off tests after b859c39c40 2022-04-21 23:02:41 +02:00
Haojian Wu 84051d8226 [clangd] Fix a declare-constructor tweak crash on incomplete fields.
Differential Revision: https://reviews.llvm.org/D124154
2022-04-21 19:44:43 +02:00
Adam Czachorowski ad46aaede6 [clangd] Add beforeExecute() callback to FeatureModules.
It runs immediatelly before FrontendAction::Execute() with a mutable
CompilerInstance, allowing FeatureModules to register callbacks, remap
files, etc.

Differential Revision: https://reviews.llvm.org/D124176
2022-04-21 18:03:39 +02:00
Kirill Bobyrev 9f05b111ee
[clangd] Include Cleaner: suppress unused warnings for IWYU pragma: export
Add limited support for "IWYU pragma: export" - for now it just supresses the
warning similar to "IWYU pragma: keep".

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D124170
2022-04-21 17:00:06 +02:00
Kirill Bobyrev e1c0d2fb82 [clangd] Correctly identify self-contained headers included rercursively
Right now when exiting the file Headers.cpp will identify the recursive
inclusion (with a new FileID) as non self-contained and will add it to the set
from which it will never be removed. As a result, we get incorrect results in
the IncludeStructure and Include Cleaner. This patch is a fix.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D124166
2022-04-21 16:54:59 +02:00
Fabian Wolff 95d77383f2 [clang-tidy] Fix behavior of `modernize-use-using` with nested structs/unions
Fixes https://github.com/llvm/llvm-project/issues/50334.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D113804
2022-04-21 15:18:31 +02:00
Haojian Wu 82cddb173f [clangd] tweak tile should start with a capital letter.
to consistent with other tweaks.
2022-04-21 11:24:02 +02:00
Aaron Ballman 7d644e1215 [C11/C2x] Change the behavior of the implicit function declaration warning
C89 had a questionable feature where the compiler would implicitly
declare a function that the user called but was never previously
declared. The resulting function would be globally declared as
extern int func(); -- a function without a prototype which accepts zero
or more arguments.

C99 removed support for this questionable feature due to severe
security concerns. However, there was no deprecation period; C89 had
the feature, C99 didn't. So Clang (and GCC) both supported the
functionality as an extension in C99 and later modes.

C2x no longer supports that function signature as it now requires all
functions to have a prototype, and given the known security issues with
the feature, continuing to support it as an extension is not tenable.

This patch changes the diagnostic behavior for the
-Wimplicit-function-declaration warning group depending on the language
mode in effect. We continue to warn by default in C89 mode (due to the
feature being dangerous to use). However, because this feature will not
be supported in C2x mode, we've diagnosed it as being invalid for so
long, the security concerns with the feature, and the trivial
workaround for users (declare the function), we now default the
extension warning to an error in C99-C17 mode. This still gives users
an easy workaround if they are extensively using the extension in those
modes (they can disable the warning or use -Wno-error to downgrade the
error), but the new diagnostic makes it more clear that this feature is
not supported and should be avoided. In C2x mode, we no longer allow an
implicit function to be defined and treat the situation the same as any
other lookup failure.

Differential Revision: https://reviews.llvm.org/D122983
2022-04-20 11:30:12 -04:00
Fabian Wolff cce79514ff [clang-tidy] Reduce false positives for `bugprone-infinite-loop` with dependent expressions
Fixes https://github.com/llvm/llvm-project/issues/51423.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D113499
2022-04-20 17:17:54 +02:00
Fabian Wolff fb3b3f76bf [clang-tidy] Fix `readability-container-size-empty` check for smart pointers
Fixes https://github.com/llvm/llvm-project/issues/51118.

Reviewed By: Sockke

Differential Revision: https://reviews.llvm.org/D115124
2022-04-20 17:03:30 +02:00
Fabian Wolff f25935a000 [clang-tidy] Fix `altera-struct-pack-align` check for empty structs
Fixes https://github.com/llvm/llvm-project/issues/50962.

Reviewed By: whisperity, aaron.ballman

Differential Revision: https://reviews.llvm.org/D114292
2022-04-20 16:55:29 +02:00
Haojian Wu 95f0f69441 [clangd] Handle the new Using TemplateName.
Add supports in FindTarget and IncludeCleaner. This would
improve AST-based features on a tempalte which is found via a using
declaration. For example, go-to-def on `vect^or<int> v;` gives us the
location of `using std::vector`, which was not previously.

Base on https://reviews.llvm.org/D123127

Differential Revision: https://reviews.llvm.org/D123212
2022-04-20 15:42:24 +02:00
Whisperity f4834815f4 [clang-tidy] Fix crash on calls to overloaded operators in `llvmlibc-callee-namespace`
The routine that facilitated symbols to be explicitly allowed asked
the name of the called function, which resulted in a crash when the
check was accidentally run on non-trivial C++ code.

Differential Revision: http://reviews.llvm.org/D123992

Reviewed By: aaron.ballman
2022-04-20 10:15:03 +02:00
Richard 08881c2de6 [clang-tidy] Improve macro handling in modernize-macro-to-enum
When a macro is undef'ed or used in a preprocessor conditional
expression, we need to remember that macro should it later be
defined in the file to an integral value.  We need to exclude
such macro names from being turned into an enum.

Maintain a blacklist of identifiers that we've seen in an
undef or conditional preprocessor directive.  When the file is
done processing, remove all the blacklisted identifiers from
conversion to an enum.

Differential Revision: https://reviews.llvm.org/D123889

Fixes #54842
2022-04-19 21:28:17 -06:00