Commit Graph

1871 Commits

Author SHA1 Message Date
Anton Bikineev d36a033310 [clang-tidy] New checker performance-trivially-destructible-check
Checks for types which can be made trivially-destructible by removing
out-of-line defaulted destructor declarations.

The check is motivated by the work on C++ garbage collector in Blink
(rendering engine for Chrome), which strives to minimize destructors and
improve runtime of sweeping phase.

In the entire chromium codebase the check hits over 2000 times.

Differential Revision: https://reviews.llvm.org/D69435
2019-11-01 16:16:49 +01:00
Vladimir Plyashkun 3a0c86a179 Add a test file that was missed in 4de6b15868 2019-10-30 16:55:43 -04:00
Daniel 7b6174bb14 Add a test file that was missed in e477988309 2019-10-30 16:52:19 -04:00
Aaron Ballman 661d2ce619 Fix modernize-use-nodiscard for classes marked [[nodiscard]]
Current implementation suggests to add [[nodiscard]] to methods even if the
return type is marked already as [[nodiscard]].

Patch by Eugene Sedykh.
2019-10-30 13:45:37 -04:00
Aaron Ballman 1caa66d075 Fix a false positive in misc-redundant-expression check
Do not warn for redundant conditional expressions when the true and false
branches are expanded from different macros even when they are defined by
one another.

Patch by Daniel Krupp.
2019-10-30 13:38:25 -04:00
Aaron Ballman 29dc0b17de Add the readability-redundant-access-specifiers check.
This finds redundant access specifier declarations inside classes, structs, and unions.

Patch by Mateusz Mackowski.
2019-10-30 13:30:57 -04:00
Joel E. Denny d3dd489b9a [lit] Fix another test case that r374652 missed
llvm-svn: 375058
2019-10-16 23:58:58 +00:00
Csaba Dabis d2b790eb68 [clang-tidy] bugprone-not-null-terminated-result: checker adjustments 4
llvm-svn: 374715
2019-10-13 10:59:30 +00:00
Csaba Dabis b56989bcc7 [clang-tidy] bugprone-not-null-terminated-result: checker adjustments 3
On Windows the signed/unsigned int conversions of APInt seems broken, so that
two of the test files marked as unsupported on Windows, as a hotfix.

llvm-svn: 374713
2019-10-13 10:41:13 +00:00
Csaba Dabis 82f8f8b44c [clang-tidy] New checker for not null-terminated result caused by strlen(), size() or equal length
Summary:
New checker called bugprone-not-null-terminated-result. This checker finds
function calls where it is possible to cause a not null-terminated result.
Usually the proper length of a string is `strlen(src) + 1` or equal length
of this expression, because the null terminator needs an extra space.
Without the null terminator it can result in undefined behaviour when the
string is read.

The following and their respective `wchar_t` based functions are checked:

`memcpy`, `memcpy_s`, `memchr`, `memmove`, `memmove_s`, `strerror_s`,
`strncmp`, `strxfrm`

The following is a real-world example where the programmer forgot to
increase the passed third argument, which is `size_t length`.
That is why the length of the allocated memory is not enough to hold the
null terminator.

```
    static char *stringCpy(const std::string &str) {
      char *result = reinterpret_cast<char *>(malloc(str.size()));
      memcpy(result, str.data(), str.size());
      return result;
    }
```

In addition to issuing warnings, fix-it rewrites all the necessary code.
It also tries to adjust the capacity of the destination array:

```
    static char *stringCpy(const std::string &str) {
      char *result = reinterpret_cast<char *>(malloc(str.size() + 1));
      strcpy(result, str.data());
      return result;
    }
```

Note: It cannot guarantee to rewrite every of the path-sensitive memory
allocations.

Reviewed By: JonasToth, aaron.ballman, whisperity, alexfh

Tags: #clang-tools-extra, #clang

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

llvm-svn: 374707
2019-10-13 08:28:27 +00:00
Dmitri Gribenko d103e2d267 [ClangTidy] Separate tests for infrastructure and checkers, fixup
Renamed a file that I missed in r374540.

llvm-svn: 374549
2019-10-11 13:16:49 +00:00
Dmitri Gribenko 885c559369 [ClangTidy] Separate tests for infrastructure and checkers
Summary:
This change moves tests for checkers and infrastructure into separate
directories, making it easier to find infrastructure tests. Tests for
checkers are already easy to find because they are named after the
checker. Tests for infrastructure were difficult to find because they
were outnumbered by tests for checkers. Now they are in a separate
directory.

Reviewers: jfb, jdoerfert, lebedev.ri

Subscribers: srhines, nemanjai, aheejin, kbarton, christof, mgrang, arphaman, jfb, lebedev.ri, cfe-commits

Tags: #clang

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

llvm-svn: 374540
2019-10-11 12:05:42 +00:00
Nico Weber 8cb804a3c9 Try to get readability-deleted-default.cpp to pass on Windows.
In MS compatibility mode, "extern inline void g()" is not a redundant
declaration for "inline void g()", because of redeclForcesDefMSVC()
(see PR19264, r205485).

To fix, run the test with -fms-compatiblity forced on and off
and explicit check for the differing behavior for extern inline.

Final bit of PR43593.

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

llvm-svn: 374103
2019-10-08 19:14:34 +00:00
Nico Weber 0db7b6a44c Attempt to fix a few clang-tidy tests on Windows, see PR43593.
llvm-svn: 373951
2019-10-07 19:54:19 +00:00
Aaron Ballman b879fd05bd Add the misc-init-local-variables check.
This checks finds all primitive type local variables (integers, doubles, pointers) that are declared without an initial value. Includes fixit functionality to initialize said variables with a default value. This is zero for most types and NaN for floating point types. The use of NaNs is copied from the D programming language.

Patch by Jussi Pakkanen.

llvm-svn: 373489
2019-10-02 17:18:57 +00:00
Sam McCall 442be72777 [clang-tidy] Fix typo in r373428
llvm-svn: 373436
2019-10-02 09:16:48 +00:00
Adam Balogh 1c57143742 [clang-tidy] Fix for commits rL372706 and rL372711
The patch committed was not the accepted version but the
previous one. This commit fixes this issue.

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

llvm-svn: 373428
2019-10-02 07:14:11 +00:00
Stephane Moore e4acb971f1 [clang-tidy] Rename objc-avoid-spinlock check to darwin-avoid-spinlock
Summary:
OSSpinLock* are Apple/Darwin functions, but were previously located with ObjC checks as those were most closely tied to Apple platforms before.

Now that there's a specific Darwin module, relocating the check there.

This change was prepared by running rename_check.py.

Contributed By: mwyman

Reviewers: stephanemoore, dmaclach

Reviewed By: stephanemoore

Subscribers: Eugene.Zelenko, mgorny, xazax.hun, cfe-commits

Tags: #clang-tools-extra, #clang, #llvm

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

llvm-svn: 373392
2019-10-01 21:18:40 +00:00
Dmitri Gribenko 6328f948ad Fixed indentation in a ClangTidy test
llvm-svn: 373068
2019-09-27 10:58:10 +00:00
Dmitri Gribenko 69f9f20fc5 Moved -fblocks from an individual test to check_clang_tidy.py
This way, all tests will benefit from it and will not have to worry
about setting up language options properly.

llvm-svn: 373066
2019-09-27 10:54:28 +00:00
Dmitri Gribenko 405c3a6be1 [clang-tidy] New check to warn when storing dispatch_once_t in non-static, non-global storage.
Summary:
Creates a new darwin ClangTidy module and adds the darwin-dispatch-once-nonstatic check that warns about dispatch_once_t variables not in static or global storage. This catches a missing static for local variables in e.g. singleton initialization behavior, and also warns on storing dispatch_once_t values in Objective-C instance variables. C/C++ struct/class instances may potentially live in static/global storage, and are ignored for this check.

The osx.API static analysis checker can find the non-static storage use of dispatch_once_t; I thought it useful to also catch this issue in clang-tidy when possible.

This is a re-land of https://reviews.llvm.org/D67567

Reviewers: thakis, gribozavr, stephanemoore

Subscribers: Eugene.Zelenko, mgorny, xazax.hun, jkorous, arphaman, kadircet, usaxena95

Tags: #clang-tools-extra, #clang, #llvm

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

llvm-svn: 373065
2019-09-27 10:49:12 +00:00
Dmitri Gribenko 847f4d3f6d Revert "[clang-tidy] New check to warn when storing dispatch_once_t in non-static, non-global storage"
This reverts commit r373028, because the new test fails on Linux.

llvm-svn: 373032
2019-09-26 23:28:31 +00:00
Stephane Moore aa7d6544c1 [clang-tidy] New check to warn when storing dispatch_once_t in non-static, non-global storage
Summary:
Creates a new darwin ClangTidy module and adds the darwin-dispatch-once-nonstatic check that warns about dispatch_once_t variables not in static or global storage. This catches a missing static for local variables in e.g. singleton initialization behavior, and also warns on storing dispatch_once_t values in Objective-C instance variables. C/C++ struct/class instances may potentially live in static/global storage, and are ignored for this check.

The osx.API static analysis checker can find the non-static storage use of dispatch_once_t; I thought it useful to also catch this issue in clang-tidy when possible.

Contributed By: mwyman

Reviewers: benhamilton, hokein, stephanemoore, aaron.ballman, gribozavr

Reviewed By: stephanemoore, gribozavr

Subscribers: jkorous, arphaman, kadircet, usaxena95, NoQ, xazax.hun, lebedev.ri, mgorny, cfe-commits

Tags: #clang, #clang-tools-extra

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

llvm-svn: 373028
2019-09-26 23:04:59 +00:00
Fangrui Song a0d79d846f [clang-tidy][test] Add -fexceptions to bugprone-infinite-loop.test
This fixes llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast.

-fexceptions is disabled by default on XCore and PS4.

llvm-svn: 372715
2019-09-24 09:55:35 +00:00
Fangrui Song 3352bdfaab [clang-tidy] Add missing InfiniteLoopCheck.h, InfiniteLoopCheck.cpp and test from D64736
llvm-svn: 372706
2019-09-24 09:06:31 +00:00
Dmitri Gribenko 7964e89409 Added a test for agreement between paths used in ClangTidy's diagnostics and header filter
This test would have been broken by r372388.

llvm-svn: 372607
2019-09-23 13:44:42 +00:00
Dmitri Gribenko ddc9a06e95 Revert "[clang-tidy] Fix relative path in header-filter."
This reverts commit r372388. It made '-header-filter' inconsistent with
paths printed in diagnostics.

llvm-svn: 372601
2019-09-23 13:06:25 +00:00
Stephane Moore 2f6a52816f [clang-tidy] Add check for classes missing -hash ⚠️
Summary:
Apple documentation states that:
"If two objects are equal, they must have the same hash value. This last
point is particularly important if you define isEqual: in a subclass and
intend to put instances of that subclass into a collection. Make sure
you also define hash in your subclass."
https://developer.apple.com/documentation/objectivec/1418956-nsobject/1418795-isequal?language=objc

In many or all versions of libobjc, -[NSObject isEqual:] is a pointer
equality check and -[NSObject hash] returns the messaged object's
pointer. A relatively common form of developer error is for a developer to
override -isEqual: in a subclass without overriding -hash to ensure that
hashes are equal for objects that are equal.

It is assumed that an override of -isEqual: is a strong signal for
changing the object's equality operator to something other than pointer
equality which implies that a missing override of -hash could result in
distinct objects being equal but having distinct hashes because they are
independent instances. This added check flags classes that override
-isEqual: but inherit NSObject's implementation of -hash to warn of the
potential for unexpected behavior.

The proper implementation of -hash is the responsibility of the
developer and the check will only verify that the developer made an
effort to properly implement -hash. Developers can set up unit tests
to verify that their implementation of -hash is appropriate.

Test Notes:
Ran check-clang-tools.

Reviewers: aaron.ballman, benhamilton

Reviewed By: aaron.ballman

Subscribers: Eugene.Zelenko, mgorny, xazax.hun, cfe-commits

Tags: #clang

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

llvm-svn: 372445
2019-09-21 01:22:22 +00:00
Dmitri Gribenko 4a13c828f6 [clang-tidy] Fix relative path in header-filter.
Summary:
Clang-tidy supports output diagnostics from header files if user
specifies --header-filter. But it can't handle relative path well.
For example, the folder structure of a project is:

```
// a.h is in /src/a/a.h

// b.h is in /src/b/b.h
...

// c.cpp is in /src/c.cpp

```

Now, we set --header-filter as --header-filter=/a/. That means we only
want to check header files under /src/a/ path, and ignore header files
uder /src/b/ path, but in current implementation, clang-tidy will check
/src/b/b.h also, because the name of b.h used in clang-tidy is
/src/a/../b/b.h.

This change tries to fix this issue.

Reviewers: alexfh, hokein, aaron.ballman, gribozavr

Reviewed By: gribozavr

Subscribers: MyDeveloperDay, xazax.hun, cfe-commits

Tags: #clang, #clang-tools-extra

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

Patch by Yubo Xie.

llvm-svn: 372388
2019-09-20 13:19:32 +00:00
Yitzhak Mandelbaum 57990b4be0 [clang-tidy] Fix bugprone-argument-comment-check to correctly ignore implicit constructors.
Summary:
After revision 370919, this check incorrectly flags certain cases of implicit
constructors. Specifically, if an argument is annotated with an
argument-comment and the argument expression triggers an implicit constructor,
then the argument comment is associated with argument of the implicit
constructor.

However, this only happens when the constructor has more than one argument.
This revision fixes the check for implicit constructors and adds a regression
test for this case.

Note: r370919 didn't cause this bug, it simply uncovered it by fixing another
bug that was masking the behavior.

Reviewers: gribozavr

Subscribers: xazax.hun, cfe-commits

Tags: #clang

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

llvm-svn: 372317
2019-09-19 13:12:05 +00:00
Haojian Wu d94c7bf06e [clang-tidy] Fix a potential infinite loop in readability-isolate-declaration check.
Reviewers: ilya-biryukov

Subscribers: xazax.hun, cfe-commits

Tags: #clang

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

llvm-svn: 372206
2019-09-18 09:21:35 +00:00
Jian Cai 9d2066af8d [clang-tidy] add checks to bugprone-posix-return
This check now also checks if any calls to pthread_* functions expect negative return values. These functions return either 0 on success or an errno on failure, which is positive only.

llvm-svn: 372037
2019-09-16 21:43:56 +00:00
Haojian Wu ad7a7cea89 [clang-tidy] performance-inefficient-vector-operation: Support proto repeated field
Summary:
Finds calls that add element to protobuf repeated field in a loop
without calling Reserve() before the loop. Calling Reserve() first can avoid
unnecessary memory reallocations.

A new option EnableProto is added to guard this feature.

Patch by Cong Liu!

Reviewers: gribozavr, alexfh, hokein, aaron.ballman

Reviewed By: hokein

Subscribers: lebedev.ri, xazax.hun, Eugene.Zelenko, cfe-commits

Tags: #clang, #clang-tools-extra

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

llvm-svn: 371963
2019-09-16 08:54:10 +00:00
Yitzhak Mandelbaum f9ce864558 [clang-tidy] Fix bug in bugprone-use-after-move check
Summary:
The bugprone-use-after-move check exhibits false positives for certain uses of
the C++17 if/switch init statements. These false positives are caused by a bug
in the ExprSequence calculations.

This revision adds tests for the false positives and fixes the corresponding
sequence calculation.

Reviewers: gribozavr

Subscribers: xazax.hun, cfe-commits

Tags: #clang

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

llvm-svn: 371396
2019-09-09 12:59:14 +00:00
Alexander Kornienko 30424e4268 [clang-tidy] Make most ArgumentCommentCheck options local, as they should be
llvm-svn: 371076
2019-09-05 14:48:31 +00:00
Alexander Kornienko 42443e50ce Add a bugprone-argument-comment option: IgnoreSingleArgument.
Summary:
Add bugprone-argument-comment option: IgnoreSingleArgument.
When true, the check will ignore the single argument.

Sometimes, it's not necessary to add comment to single argument.
For example:

> std::string name("Yubo Xie");
> pScreen->SetWidth(1920);
> pScreen->SetHeight(1080);

This option can ignore such single argument in bugprone-argument-comment check.

Reviewers: alexfh

Reviewed By: alexfh

Subscribers: cfe-commits

Tags: #clang

Patch by Yubo Xie.

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

llvm-svn: 371075
2019-09-05 14:48:23 +00:00
Alexander Kornienko b6d9703050 [clang-tidy] Fix bugprone-argument-comment bug: negative literal number is not checked.
Summary:
For example:
```
void foo(int a);
foo(-2);
```
should be fixed as:
```
foo(/*a=*/-2);
```
This change tries to fix this issue.

Reviewers: alexfh, hokein, aaron.ballman

Reviewed By: alexfh, aaron.ballman

Subscribers: xazax.hun, cfe-commits

Tags: #clang, #clang-tools-extra

Patch by Yubo Xie.

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

llvm-svn: 371072
2019-09-05 14:13:57 +00:00
Kadir Cetinkaya 4a16c29551 [clang-tidy] Fix definitions in headers check to respect qualifiers
Summary:
The check was generating a fix without taking qualifiers in return type
into account. This patch changes the insertion location to be before qualifers.

Reviewers: gribozavr

Subscribers: xazax.hun, cfe-commits

Tags: #clang

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

llvm-svn: 371022
2019-09-05 08:11:21 +00:00
Alexander Kornienko 240a2e25c6 [clang-tidy] Fix bugprone-argument-comment bug if there are marcos.
Summary:
Fix bugprone-argument-comment bug if there are marcos.

For example:
```
void j(int a, int b, int c);
j(X(1), /*b=*/1, X(1));
```

clang-tidy can't recognize comment "/*b=*/". It suggests fix like this:
```
j(X(1), /*b=*//*b=*/1, X(1));
```

This change tries to fix this issue.

Reviewers: alexfh, hokein, aaron.ballman

Reviewed By: alexfh

Subscribers: xazax.hun, cfe-commits

Tags: #clang, #clang-tools-extra

Patch by Yubo Xie.

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

llvm-svn: 370919
2019-09-04 16:19:32 +00:00
Haojian Wu 67853ac4e0 [clang-tidy] Fix a false positive in unused-using-decl check
The previous matcher "hasAnyTemplateArgument(templateArgument())" only
matches the first template argument, but the check wants to iterate all
template arguments. This patch fixes this.

Also some refactorings in this patch (to make the code reusable).

llvm-svn: 370760
2019-09-03 14:13:00 +00:00
Daniel Sanders 5b4f640499 [clang-tidy] Add llvm-prefer-register-over-unsigned to clang-tidy
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).

Reviewers: arsenm, bogner

Subscribers: jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, mgorny, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, cfe-commits, llvm-commits

Tags: #clang, #llvm

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

llvm-svn: 370512
2019-08-30 20:01:59 +00:00
Haojian Wu d1a24bab3a [clang-tidy] Fix the potential infinite loop in recordIsTriviallyDefaultConstructible.
Summary:
The recordIsTriviallyDefaultConstructible may cause an infinite loop when
running on an ill-formed decl.

Reviewers: gribozavr

Subscribers: nemanjai, xazax.hun, kbarton, cfe-commits

Tags: #clang

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

llvm-svn: 370200
2019-08-28 13:45:53 +00:00
Sam McCall 9004c077c0 [clang-tidy] readability-identifier-naming shouldn't complain about CRTP pseudo-overrides
Reviewers: ilya-biryukov

Subscribers: xazax.hun, cfe-commits

Tags: #clang

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

llvm-svn: 370193
2019-08-28 12:08:57 +00:00
Benjamin Kramer 3c614f7b48 [clang-tidy] Manually enable exceptions in tesst that uses them
llvm-svn: 369853
2019-08-24 17:19:06 +00:00
Kristof Umann dabfea85fc [clang-tidy] Possibility of displaying duplicate warnings
Summary: In case a checker is registered multiple times as an alias, the emitted warnings are uniqued by the report message. However, it is random which checker name is included in the warning. When processing the output of clang-tidy this behavior caused some problems. In this commit the uniquing key contains the checker name too.

Reviewers: alexfh, xazax.hun, Szelethus, aaron.ballman, lebedev.ri, JonasToth, gribozavr

Reviewed By: alexfh

Subscribers: dkrupp, whisperity, rnkovacs, mgrang, cfe-commits

Patch by Tibor Brunner!

Tags: #clang

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

llvm-svn: 369763
2019-08-23 14:57:27 +00:00
Haojian Wu c4905a232c [clang-tidy] Don't emit google-runtime-references warning for functions defined in macros.
Summary:
The macro are usually defined in the common/base headers which are hard
for normal users to modify it.

Reviewers: gribozavr, alexfh

Subscribers: xazax.hun, cfe-commits

Tags: #clang

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

llvm-svn: 369739
2019-08-23 08:47:27 +00:00
Yuanfang Chen f24c1e6b51 [clang-tidy] Check for dynamically initialized statics in headers.
Finds instances where variables with static storage are initialized dynamically in header files.

Reviewed By: aaron.ballman, alexfh

Patch by Charles Zhang!

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

llvm-svn: 369568
2019-08-21 20:00:01 +00:00
Richard Trieu 8f9e489a66 Fix typo. "piont" => "point"
Found by Chris Morris (cwmorris).

llvm-svn: 369316
2019-08-20 00:28:21 +00:00
Diego Astiazaran b46131e5c3 [clang-doc] Fix records in global namespace
When a Record is declared in the global namespace, clang-doc serializes
it as a child of the global namespace, so the global namespace is now
one if its parent namespaces. This namespace was not being included in
the list of namespaces of the Info causing paths to be incorrect and the
index rendered incorrectly.
Affected tests have been fixed.

Differential revision: https://reviews.llvm.org/D66298

llvm-svn: 369123
2019-08-16 16:10:32 +00:00
Diego Astiazaran 6a29ae4bde [clang-doc] Fix bitcode writer for access specifiers
Bitcode writer was not emitting the corresponding record for the Access attribute of a FunctionInfo. This has been added.
AS_none was being used as the default value for any AcesssSpecifier attribute
(in FunctionInfo and MemberTypeInfo), this has been changed to AS_public
because this is the enum value that evaluates to 0.
The bitcode writer doesn't write values that are 0 so if an attribute
was set to AS_public, this value is not written and after reading the
bitcode it would have the default value which is AS_none. This is why
the default value is now AS_public.

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

llvm-svn: 369063
2019-08-15 23:04:27 +00:00