Commit Graph

7408 Commits

Author SHA1 Message Date
Serikzhan Kazi 0dc856ed20 [clang-tidy] run-clang-tidy.py: analyze unique files only
The files in compile-commands.json can potentially include duplicates.
Change run-clang-tidy.py so that it does not run on the duplicate entries.

Differential Revision: https://reviews.llvm.org/D112926
2021-11-06 19:53:18 +13:00
Quinn Pham c71fbdd87b [NFC] Inclusive language: Remove instances of master in URLs
[NFC] This patch fixes URLs containing "master". Old URLs were either broken or
redirecting to the new URL.

Reviewed By: #libc, ldionne, mehdi_amini

Differential Revision: https://reviews.llvm.org/D113186
2021-11-05 08:48:41 -05:00
Vitaly Buka 6c6ccc72a6 [NFC] Don't set rlimit in test with MSAN 2021-11-04 18:18:03 -07:00
Adam Czachorowski 97fbc975fa [clangd] Find definition of ClassTemplate without going through index.
I noticed that, while go-to-def works on cases like:

namespace ns {
  template<typename T> struct Foo {};
}
using ::ns::Fo^o;

it only works because of the FileIndex. We can get definition location
directly from AST too.

Differential Revision: https://reviews.llvm.org/D113029
2021-11-04 15:25:21 +01:00
Chuanqi Xu ec117158a3 [Coroutines] [Frontend] Lookup in std namespace first
Now in libcxx and clang, all the coroutine components are defined in
std::experimental namespace.
And now the coroutine TS is merged into C++20. So in the working draft
like N4892, we could find the coroutine components is defined in std
namespace instead of std::experimental namespace.
And the coroutine support in clang seems to be relatively stable. So I
think it may be suitable to move the coroutine component into the
experiment namespace now.

This patch would make clang lookup coroutine_traits in std namespace
first. For the compatibility consideration, clang would lookup in
std::experimental namespace if it can't find definitions in std
namespace. So the existing codes wouldn't be break after update
compiler.

And in case the compiler found std::coroutine_traits and
std::experimental::coroutine_traits at the same time, it would emit an
error for it.

The support for looking up std::experimental::coroutine_traits would be
removed in Clang16.

Reviewed By: lxfind, Quuxplusone

Differential Revision: https://reviews.llvm.org/D108696
2021-11-04 11:53:47 +08:00
Sam McCall 6a5e08cc4a [AST] injected-class-name is not a redecl, even in template specializations
Back in the mists of time, the CXXRecordDecl for the injected-class-name was
a redecl of the outer class itself.
This got changed in 470c454a61, but only for plain
classes: class template instantation was still detecting the injected-class-name
in the template body and marking its instantiation as a redecl.

This causes some subtle inconsistent behavior between the two, e.g.
hasDefinition() returns true for Foo<int>::Foo but false for Bar::Bar.
This is the root cause of PR51912.

Differential Revision: https://reviews.llvm.org/D112765
2021-11-02 14:37:45 +01:00
Balázs Kéri 4bcbb3d4d7 [clang-tidy] Add check 'cert-err33-c'.
The CERT rule ERR33-C can be modeled partially by the existing check
'bugprone-unused-return-value'. The existing check is reused with
a fixed set of checked functions.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D112409
2021-11-02 11:38:47 +01:00
Salman Javed ade0662c51 [clang-tidy] Fix lint warnings in clang-tidy source code (NFC)
Run clang-tidy on all source files under `clang-tools-extra/clang-tidy`
with `-header-filter=clang-tidy.*` and make suggested corrections.

Differential Revision: https://reviews.llvm.org/D112864
2021-11-02 20:14:25 +13:00
Kazu Hirata 4db2e4cebe Use {DenseSet,SetVector,SmallPtrSet}::contains (NFC) 2021-10-30 19:00:19 -07:00
Kazu Hirata 972d4133e9 Use {DenseSet,SmallPtrSet}::contains (NFC) 2021-10-29 20:26:07 -07:00
Kirill Bobyrev f47564ea87
[clangd] IncludeCleaner: Skip non self-contained headers
Headers without include guards might have side effects or can be the files we
don't want to consider (e.g. tablegen ".inc" files). Skip them when translating
headers to the HeaderIDs that we will consider as unused.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D112695
2021-10-29 17:57:31 +02:00
Kirill Bobyrev 32f1029124
[clangd] Track performance of IncludeCleaner
Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D112783
2021-10-29 12:59:22 +02:00
Clement Courbet fc1b24d736 [clang-tidy]performance-unnecessary-copy-initialization: fix false negative
We're missing all cases where the return value is a type alias.

Unfortunately, this includes things we care about, such as
`std::vector<T>::operator[]` (return value is `const_reference`,
not `const T&`).

Match the canonical type instead.

Differential Revision: https://reviews.llvm.org/D112722
2021-10-29 08:41:03 +02:00
Kadir Cetinkaya 3d735480bd
[clangd] SelectionTree should prefer lexical declcontext
This is important especially for code that tries to traverse scopes as
written in code, which is the contract SelectionTree tries to satisfy.

Differential Revision: https://reviews.llvm.org/D112712
2021-10-28 17:39:35 +02:00
Kadir Cetinkaya fbf1745722
[clangd] Escape error message in AddUsing
Fixes https://github.com/clangd/clangd/issues/900
2021-10-28 14:52:12 +02:00
Kirill Bobyrev f9201c70ad
[clangd] NFC: Use more idiomatic way of checking for definition 2021-10-28 12:25:12 +02:00
Kirill Bobyrev 56a8aee100 [clangd] NFC: Match function signature in the header and source file 2021-10-28 12:13:18 +02:00
Sam McCall 73453e7ade [clangd] Avoid expensive checks of buffer names in IncludeCleaner
This changes the handling of special buffers (<command-line> etc) that
SourceManager treats as files but FileManager does not.

We now include them in findReferencedFiles() and drop them as part of
translateToHeaderIDs(). This pairs more naturally with the data representations
we're using, and so avoids a bunch of converting between representations for
filtering.

Differential Revision: https://reviews.llvm.org/D112652
2021-10-28 08:00:57 +02:00
Kirill Bobyrev 22079c61a8
[clangd] IncludeCleaner: Do not process locations in built-in files
Doing otherwise leads to crashing. Way to reproduce: open "gmock/gmock.h" in
the LLVM source tree.

Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D112608
2021-10-27 19:31:43 +02:00
Kirill Bobyrev 6edc509719 [clangd] NFC: Remove single-line braces in IncludeCleaner 2021-10-27 13:53:30 +02:00
Sam McCall 9cc08cb02f [clangd] Add integration test for crash handling
This replaces the test removed in 51be7061d0
It is more principled and tests more critical cases: a crash while parsing.

We need two pieces of plumbing:
 - a way to re-enable the crashing #pragmas via a flag, to test parse crashes
 - a bit of reshuffling around ASTWorker execution so that we set up the
   crash handler in both sync/async modes.
   Sync mode is useful for debugging, so I tested both.

Differential Revision: https://reviews.llvm.org/D112565
2021-10-27 11:52:31 +02:00
Kirill Bobyrev c4723785c1 [clangd] IncludeCleaner: Don't warn on system headers
This is a temporary hack to disable diagnostics for system headers. As of right
now, IncludeCleaner does not handle the Standard Library correctly and will
report most system headers as unused because very few symbols are defined in
top-level system headers. This will eventually be fixed, but for now we are
aiming for the most conservative approach with as little false-positive
warnings as possible. After the initial prototype and core functionality is
polished, I will turn back to handling the Standard Library as it requires
custom logic.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D112571
2021-10-27 11:51:08 +02:00
Salman Javed 897402e959 [clang-tidy] Correct typo in bugprone-easily-swappable-parameters
The string table `DefaultIgnoredParameterTypeSuffixes` has a typo:
`ForwardIt` is mistyped as `FowardIt`.

Correct typo and add test coverage.

Differential Revision: https://reviews.llvm.org/D112596
2021-10-27 22:38:52 +13:00
Kirill Bobyrev e3c6090e59 [clangd] IncludeCleaner: Support macros
Collect the macro definition locations for all the macros used in the main
file.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D112447
2021-10-27 10:30:04 +02:00
Kadir Cetinkaya e42f5d4b48
[clangd] Fix filename ranges while replaying preamble
Clangd used first token of filename as filename range rather than the
synthezied filename token. Unfortunately the former only contains `"` or `<` in
the raw lexing mode, resulting in wrong range information and breaking tidy
checks that relied on it.

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

Differential Revision: https://reviews.llvm.org/D112559
2021-10-27 09:34:17 +02:00
Adam Czachorowski 2174524116 [clangd] AddUsing: Fix support for template specializations.
Before this change, we would add "using std::vector<int>" instead of
just "using std::vector;", which would not even compile.

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

Differential Revision: https://reviews.llvm.org/D112530
2021-10-26 17:32:33 +02:00
Adam Czachorowski e8f4a01189 [clangd] Fix a hover crash on templated spaceship operator.
We make assumption that:
getDeclForComment(getDeclForComment(X)) == getDeclForComment(X)
but this is not true if you have a template
instantionation of a template instantiation, which is the case when, for
example, you have a <=> operator in a templated class.

This fix makes getDeclForComment() call itself recursively to ensure
this property is always true.

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

Differential Revision: https://reviews.llvm.org/D112527
2021-10-26 17:28:40 +02:00
Kirill Bobyrev 1c2e249f93
[clangd] IncludeCleaner: don't stop the traversal
I was under the impression that `return false;` in the
RecursiveASTVisitor stops the traversal for the subtree but it appears
that it stops the whole tree traversal, so this change introduces a bug
where `ReferencedLocationCrawler` will not collect any symbols past an
enum.

This is a follow-up on D112209.
2021-10-26 16:08:54 +02:00
Sam McCall 51be7061d0 [clangd] Remove tricky integration test that flakes/fails on some platforms.
This functionality is covered by unittests, will add a better
integration test soon.
2021-10-26 14:19:05 +02:00
Kirill Bobyrev 6d314ee570
[clangd] Add a way to enable IncludeCleaner through config
This is useful for dogfooding the feature and spotting bugs.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D111870
2021-10-26 12:53:05 +02:00
Kirill Bobyrev c521288ed3
[clangd] IncludeCleaner: Don't consider the definition as usage for function forward declarations
Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D111711
2021-10-26 10:37:10 +02:00
Carlos Galvez b43a2aee4e [clang-tidy] Suppress readability-static-access-through-instance for CUDA built-in variables
clang-tidy can be used to statically analyze CUDA code,
thanks to clang being able to compile CUDA code natively.
This makes clang-tidy the one and only open-source
static analyzer for CUDA.

However it currently warns for native CUDA built-in
variables, like threadIdx, due to the way they
are implemented in clang.

Users don't need to know the details of the clang
implementation, and they should continue to write
idiomatic code. Therefore, suppress the warning
if a CUDA built-in variable is encountered.

Fixes https://bugs.llvm.org/show_bug.cgi?id=48758
2021-10-26 05:45:25 +00:00
Shoaib Meenai ba94b8bdff [clangd] Attempt to fix buildbots
http://45.33.8.238/win/47615/step_4.txt is a sample error; I believe it
just needs the right header to be included.
2021-10-25 20:16:59 -07:00
Sam McCall aa1ac2ae45 [clangd] Flush stderr after signal handlers run, so we always get the full stack/crash info 2021-10-26 01:08:36 +02:00
Emma Blink 045695f85c [clangd] Print current request context along with the stack trace
Motivation:

At the moment it is hard to attribute a clangd crash to a specific request out of all in-flight requests that might be processed concurrently. So before we can act on production clangd crashes, we have to do quite some digging through the log tables populated by our in-house VSCode extension or sometimes even directly reach out to the affected developer. Having all the details needed to reproduce a crash printed alongside its stack trace has a potential to save us quite some time, that could better be spent on fixing the actual problems.

Implementation approach:

* introduce `ThreadCrashReporter` class that allows to set a temporary signal handler for the current thread
* follow RAII pattern to simplify printing context for crashes occurring within a particular scope
* hold `std::function` as a handler to allow capturing context to print
* set local `ThreadCrashReporter` within `JSONTransport::loop()` to print request JSON for main thread crashes, and in `ASTWorker::run()` to print the file paths, arguments and contents for worker thread crashes

`ThreadCrashReporter` currently allows only one active handler per thread, but the approach can be extended to support stacked handlers printing context incrementally.

Example output for main thread crashes:

  ```
  ...
  #15 0x00007f7ddc819493 __libc_start_main (/lib64/libc.so.6+0x23493)
  #16 0x000000000249775e _start (/home/emmablink/local/llvm-project/build/bin/clangd+0x249775e)
  Signalled while processing message:
  {"jsonrpc": "2.0", "method": "textDocument/didOpen", "params": {"textDocument": {"uri": "file:///home/emmablink/test.cpp", "languageId": "cpp", "version": 1, "text": "template <typename>\nclass Bar {\n  Bar<int> *variables_to_modify;\n  foo() {\n    for (auto *c : *variables_to_modify)\n      delete c;\n  }\n};\n"}}}
  ```

Example output for AST worker crashes:

  ```
  ...
  #41 0x00007fb18304c14a start_thread pthread_create.c:0:0
  #42 0x00007fb181bfcdc3 clone (/lib64/libc.so.6+0xfcdc3)
  Signalled during AST action:
  Filename: test.cpp
  Directory: /home/emmablink
  Command Line: /usr/bin/clang -resource-dir=/data/users/emmablink/llvm-project/build/lib/clang/14.0.0 -- /home/emmablink/test.cpp
  Version: 1
  Contents:
  template <typename>
  class Bar {
    Bar<int> *variables_to_modify;
    foo() {
      for (auto *c : *variables_to_modify)
        delete c;
    }
  };
  ```

Testing:

The unit test covers the thread-localitity and nesting aspects of `ThreadCrashReporter`. There might be way to set up a lit-based integration test that would spawn clangd, send a message to it, signal it immediately and check the standard output, but this might be prone to raceconditions.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D109506
2021-10-26 00:58:09 +02:00
Nathan Ridge e6a971b1ba [clangd] Respect diagnostic suppression during preamble build
Fixes https://github.com/clangd/clangd/issues/906

Differential Revision: https://reviews.llvm.org/D112403
2021-10-25 17:39:32 -04:00
Kirill Bobyrev 593814a10c [clangd] IncludeCleaner: Complicated rules for enum usage
Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D112209
2021-10-25 22:04:22 +02:00
Kadir Cetinkaya 9ab9caf214
[clang] Visit enum base specifiers in libIndex
Fixes https://github.com/clangd/clangd/issues/878.

Differential Revision: https://reviews.llvm.org/D111260
2021-10-25 13:16:14 +02:00
Kadir Cetinkaya 4e4511df8d
[clang] Traverse enum base specifier in RAV 2021-10-25 13:16:14 +02:00
Kazu Hirata 4ba9d9c84f Use StringRef::contains (NFC) 2021-10-23 20:41:46 -07:00
Kazu Hirata d8e4170b0a Ensure newlines at the end of files (NFC) 2021-10-23 08:45:29 -07:00
Salman Javed 5de69e16ea [clang-tidy] Tidy up spelling, grammar, and inconsistencies in documentation (NFC)
Differential Revision: https://reviews.llvm.org/D112356
2021-10-23 00:07:36 -07:00
David Blaikie aee4925507 Recommit: Compress formatting of array type names (int [4] -> int[4])
Based on post-commit review discussion on
2bd8493847 with Richard Smith.

Other uses of forcing HasEmptyPlaceHolder to false seem OK to me -
they're all around pointer/reference types where the pointer/reference
token will appear at the rightmost side of the left side of the type
name, so they make nested types (eg: the "int" in "int *") behave as
though there is a non-empty placeholder (because the "*" is essentially
the placeholder as far as the "int" is concerned).

This was originally committed in 277623f4d5

Reverted in f9ad1d1c77 due to breakages
outside of clang - lldb seems to have some strange/strong dependence on
"char [N]" versus "char[N]" when printing strings (not due to that name
appearing in DWARF, but probably due to using clang to stringify type
names) that'll need to be addressed, plus a few other odds and ends in
other subprojects (clang-tools-extra, compiler-rt, etc).
2021-10-21 11:34:43 -07:00
Kadir Cetinkaya 60e19f6752
[clangd] Fix use-after-free in HeaderIncluderCache
Includer cache could get into a bad state when a main file went bad and
added back afterwards. This patch adds a check to invalidate to prevent
that.

Differential Revision: https://reviews.llvm.org/D112130
2021-10-20 16:36:07 +02:00
Kadir Cetinkaya c959da9ef3
[clangd] Only publish preamble after rebuilds
Don't invoke parsing callback for preamble if clangd is using a
previously built one.

Differential Revision: https://reviews.llvm.org/D112137
2021-10-20 16:32:32 +02:00
Carlos Galvez 7812cb72a3 Use reference type in for loop
To fix failing build job.
2021-10-19 16:37:56 +00:00
Carlos Galvez bf6b0d1674 [clang-tidy] Support globbing in NOLINT* expressions
To simplify suppressing warnings (for example, for
when multiple check aliases are enabled).

The globbing format reuses the same code as for
globbing when enabling checks, so the semantics
and behavior is identical.

Differential Revision: https://reviews.llvm.org/D111208
2021-10-19 16:30:51 +00:00
Adam Czachorowski 8fbac4e88a [clangd] Add code completion of param name on /* inside function calls.
For example, if you have:
  void foo(int bar);
  foo(/*^
it should auto-complete to "bar=".

Because Sema callbacks for code completion in comments happen before we
have an AST we need to cheat in clangd by detecting completion on /*
before, moving cursor back by two characters, then running a simplified
verion of SignatureHelp to extract argument name(s) from possible
overloads.

Differential Revision: https://reviews.llvm.org/D110823
2021-10-19 12:49:46 +02:00
Carlos Galvez f0711106dc [clang-tidy] Fix false positive in cppcoreguidelines-virtual-class-destructor
Incorrectly triggers for template classes that inherit
from a base class that has virtual destructor.

Any class inheriting from a base that has a virtual destructor
will have their destructor also virtual, as per the Standard:

https://timsong-cpp.github.io/cppwp/n4140/class.dtor#9

> If a class has a base class with a virtual destructor,
> its destructor (whether user- or implicitly-declared) is virtual.

Added unit tests to prevent regression.

Fixes bug https://bugs.llvm.org/show_bug.cgi?id=51912

Differential Revision: https://reviews.llvm.org/D110614
2021-10-16 08:27:08 +00:00
Kirill Bobyrev 0ce3c7111e
[clangd] IncludeCleaner: Handle macros coming from ScratchBuffer
Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D111698
2021-10-14 13:36:37 +02:00