Commit Graph

924 Commits

Author SHA1 Message Date
Kim Viggedal 512767eb3f Add CppCoreGuidelines I.2 "Avoid non-const global variables" check
Cpp Core Guideline I.2, a.k.a "Avoid non-const global variables"
For detailed documentation, see:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#i2-avoid-non-const-global-variables
2020-03-13 10:05:13 -04:00
Paula Toth eb41cc6198 [clang-tidy] Add module for llvm-libc and restrict-system-libc-header-check.
Summary: This adds a new module to enforce standards specific to the llvm-libc project. This change also adds the first check which restricts user from including system libc headers accidentally which can lead to subtle bugs that would be a challenge to detect.

Reviewers: alexfh, hokein, aaron.ballman

Reviewed By: aaron.ballman

Subscribers: juliehockett, arphaman, jfb, abrachet, sivachandra, Eugene.Zelenko, njames93, mgorny, xazax.hun, MaskRay, cfe-commits

Tags: #clang-tools-extra, #libc-project, #clang

Differential Revision: https://reviews.llvm.org/D75332
2020-03-12 11:46:05 -07:00
Jonathan Roelofs 2c9cf9f4dd [clang-tidy] New check: bugprone-suspicious-include
Detects and fixes suspicious code like: `#include "foo.cpp"`.

Inspired by: https://twitter.com/lefticus/status/1228458240364687360?s=20

https://reviews.llvm.org/D74669
2020-03-12 09:59:28 -06:00
Sam McCall 966cad0c65 [clangd] Add README pointing to docs, bugtracker etc. NFC 2020-03-12 14:00:08 +01:00
Sam McCall 57e81a2f64 [clangd] Redirect documentation to clangd.llvm.org.
Reviewers: hokein

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D76053
2020-03-12 11:45:40 +01:00
Paula Toth ddfcda0256 [clang-tidy] Fix warning from my previous patch in ReleaseNotes.txt 2020-03-10 14:01:23 -07:00
Paula Toth ebdb98f254 [clang-tidy] Move fuchsia-restrict-system-includes to portability module for general use.
Summary:
Created a general check for restrict-system-includes under portability as recommend in the comments under D75332. I also fleshed out the user facing documentation to show examples for common use-cases such as allow-list, block-list, and wild carding.

Removed fuchsia's check as per phosek sugguestion.

Reviewers: aaron.ballman, phosek, alexfh, hokein, njames93

Reviewed By: phosek

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

Tags: #clang-tools-extra, #clang

Differential Revision: https://reviews.llvm.org/D75786
2020-03-10 13:33:06 -07:00
Nico Weber 714466bf36 Revert "[clang-tidy] New check: bugprone-suspicious-include"
This reverts commit 1e0669bfe0
(and follow-ups 698a127129 and
52bbdad7d6).
The tests fail fail on Windows, see https://reviews.llvm.org/D74669
2020-03-10 10:28:20 -04:00
Jonathan Roelofs 52bbdad7d6 [clang-tidy][docs] Post-commit feedback on D74669 2020-03-09 16:51:41 -06:00
Jonathan Roelofs 2e9d33bccd Add missing list.rst entry 2020-03-09 16:27:35 -06:00
Jonathan Roelofs 698a127129 release notes: fix new check name 2020-03-09 16:11:35 -06:00
Jonathan Roelofs 1e0669bfe0 [clang-tidy] New check: bugprone-suspicious-include
Detects and fixes suspicious code like: `#include "foo.cpp"`.

Inspired by: https://twitter.com/lefticus/status/1228458240364687360?s=20

https://reviews.llvm.org/D74669
2020-03-09 15:54:32 -06:00
Jonathan Roelofs 47caa69120 [clang-tidy] Use ; as separator for HeaderFileExtensions
... and deprecate use of ',' for the same.

https://reviews.llvm.org/D75621
2020-03-09 11:32:44 -06:00
Jonas Devlieghere 45e2c6d956 [clang-tools-extra/clang-tidy] Mark modernize-make-shared as offering fixes
The list incorrectly states that modernize-make-shared does not offer
fixes, which is incorrect. Just like modernize-make-unique it does.
2020-03-05 21:45:20 -08:00
Eugene Zelenko db8911aad7 [clang-tidy] rename_check.py: maintain alphabetical order in Renamed checks section
Summary:
Also use //check// in add_new_check.py for terminology consistency.

PS

My GitHub ID is [[ https://github.com/EugeneZelenko | EugeneZelenko ]], if it's necessary for attribution.

Reviewers: alexfh, hokein, aaron.ballman, njames93, MyDeveloperDay

Reviewed By: njames93

Subscribers: Andi, xazax.hun, cfe-commits

Tags: #clang-tools-extra, #clang

Differential Revision: https://reviews.llvm.org/D73580
2020-02-20 17:31:08 +00:00
Nathan James d1d5180e69 [NFC] Fix issues with clang-tidy checks list.rst
Added FixItHint comments to ReservedIdentifierCheck and IdentifierNamingCheck to trick the python scripts into detecting a fix it is provided as it can't see the FixItHints in RenamerClangTidyCheck.cpp
2020-02-19 23:19:09 +00:00
Roman Lebedev 49bffa5f8b
[clang-tidy] misc-no-recursion: a new check
Summary:
Recursion is a powerful tool, but like any tool
without care it can be dangerous. For example,
if the recursion is unbounded, you will
eventually run out of stack and crash.

You can of course track the recursion depth
but if it is hardcoded, there can always be some
other environment when that depth is too large,
so said magic number would need to be env-dependent.
But then your program's behavior is suddenly more env-dependent.

Also, recursion, while it does not outright stop optimization,
recursive calls are less great than normal calls,
for example they hinder inlining.

Recursion is banned in some coding guidelines:
* SEI CERT DCL56-CPP. Avoid cycles during initialization of static objects
* JPL 2.4 Do not use direct or indirect recursion.
* I'd say it is frowned upon in LLVM, although not banned
And is plain unsupported in some cases:
* OpenCL 1.2, 6.9 Restrictions: i. Recursion is not supported.

So there's clearly a lot of reasons why one might want to
avoid recursion, and replace it with worklist handling.
It would be great to have a enforcement for it though.

This implements such a check.
Here we detect both direct and indirect recursive calls,
although since clang-tidy (unlike clang static analyzer)
is CTU-unaware, if the recursion transcends a single standalone TU,
we will naturally not find it :/

The algorithm is pretty straight-forward:
1. Build call-graph for the entire TU.
   For that, the existing `clang::CallGraph` is re-used,
   although it had to be modified to also track the location of the call.
2. Then, the hard problem: how do we detect recursion?
   Since we have a graph, let's just do the sane thing,
   and look for Strongly Connected Function Declarations - widely known as `SCC`.
   For that LLVM provides `llvm::scc_iterator`,
   which is internally an Tarjan's DFS algorithm, and is used throught LLVM,
   so this should be as performant as possible.
3. Now that we've got SCC's, we discard those that don't contain loops.
   Note that there may be more than one loop in SCC!
4. For each loopy SCC, we call out each function, and print a single example
   call graph that shows recursion -- it didn't seem worthwhile enumerating
   every possible loop in SCC, although i suppose it could be implemented.
   * To come up with that call graph cycle example, we start at first SCC node,
     see which callee of the node is within SCC (and is thus known to be in cycle),
     and recurse into it until we hit the callee that is already in call stack.

Reviewers: JonasToth, aaron.ballman, ffrankies, Eugene.Zelenko, erichkeane, NoQ

Reviewed By: aaron.ballman

Subscribers: Charusso, Naghasan, bader, riccibruno, mgorny, Anastasia, xazax.hun, cfe-commits

Tags: #llvm, #clang

Differential Revision: https://reviews.llvm.org/D72362
2020-02-13 23:37:53 +03:00
Haojian Wu a45ca670f5 [clang-tidy] No misc-definitions-in-headers warning on C++14 variable templates.
Reviewers: gribozavr2

Subscribers: xazax.hun, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D74468
2020-02-12 16:56:31 +01:00
Nathan James 784d441745 Fix Sphinx failure on ReadabilityQualifiedAuto docs 2020-02-11 02:03:37 +00:00
Michael Wyman 0151ddc2e8 Create a clang-tidy check to warn when -dealloc is implemented inside an ObjC class category.
Summary: Such implementations may override the class's own implementation, and even be a danger in case someone later comes and adds one to the class itself. Most times this has been encountered have been a mistake.

Reviewers: stephanemoore, benhamilton, dmaclach

Reviewed By: stephanemoore, benhamilton, dmaclach

Subscribers: dmaclach, mgorny, cfe-commits

Tags: #clang-tools-extra, #clang

Differential Revision: https://reviews.llvm.org/D72876
2020-02-10 08:56:28 -07:00
Nathan James d24d8af320 Fixed typo in CTE release notes failing build 2020-02-02 21:51:34 +00:00
Nathan James 8a68c40a1b [clang-tidy] Added option for disabling const qualifiers in readability-qualified-auto
Summary: Adds an option called `AddConstToQualified` to readability-qualified-auto to toggle adding const to the auto typed pointers and references. By default its enabled but in the LLVM module its disabled.

Reviewers: aaron.ballman, alexfh, JonasToth, hokein, sammccall

Reviewed By: aaron.ballman

Subscribers: Quuxplusone, merge_guards_bot, lebedev.ri, xazax.hun, cfe-commits

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D73548
2020-02-02 21:27:25 +00:00
Nathan James 66e47a5720 [clang-tidy] adjust scripts to subsubsections in Release Notes
Summary:
I added subsubsections for typical Clang-tidy entries in Release Notes, so now scripts are aware of this changes.

I don't have GitHub commit access, so please commit changes.

Reviewers: aaron.ballman, alexfh, hokein

Reviewed By: alexfh

Subscribers: njames93, xazax.hun, cfe-commits

Tags: #clang-tools-extra, #clang

Differential Revision: https://reviews.llvm.org/D72527
2020-01-28 18:10:22 +00:00
Nathan 7c90666d2c [clang-tidy] readability-redundant-string-init now flags redundant initialisation in Field Decls and Constructor Initialisers
Summary:
The original behaviour of this check only looked at VarDecls with strings that had an empty string initializer. This has been improved to check for FieldDecls with an in class initializer as well as constructor initializers.

Addresses [[ https://bugs.llvm.org/show_bug.cgi?id=44474 | clang-tidy "modernize-use-default-member-init"/"readability-redundant-string-init" and redundant initializer of std::string ]]

Reviewers: aaron.ballman, alexfh, hokein

Reviewed By: aaron.ballman

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

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D72448
2020-01-27 23:51:45 +00:00
Nathan c3d20fd472 [clang-tidy] readability-identifier-naming disregards parameters restrictions on main like functions
Summary:
Typically most main functions have the signature:
```
int main(int argc, char *argv[])
```
To stick with convention when renaming parameters we should ignore the `argc` and `argv` names even if the parameter style says they should be renamed. This patch addresses this by checking all ParmVarDecls if they form part of a function with a signature that matches main `int name(int argc, char * argv[], (optional char *env[]))`

Reviewers: aaron.ballman, JonasToth, alexfh, hokein

Reviewed By: aaron.ballman

Subscribers: Mordante, merge_guards_bot, xazax.hun, kristof.beyls, cfe-commits

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D73098
2020-01-27 23:47:51 +00:00
Shoaib Meenai c72a6ac4b6 Revert "[clang-tidy] readability-identifier-naming disregards parameters restrictions on main like functions"
This reverts commit 27e3671ff4.

This was an accidental push, and the author requested a revert on IRC as
their local branch is in a bad state.
2020-01-27 11:50:25 -08:00
Nathan 27e3671ff4 [clang-tidy] readability-identifier-naming disregards parameters restrictions on main like functions
Summary:
Typically most main functions have the signature:
```
int main(int argc, char *argv[])
```
To stick with convention when renaming parameters we should ignore the `argc` and `argv` names even if the parameter style says they should be renamed. This patch addresses this by checking all ParmVarDecls if they form part of a function with a signature that matches main `int name(int argc, char * argv[], (optional char *env[]))`

Reviewers: aaron.ballman, JonasToth, alexfh, hokein

Reviewed By: aaron.ballman

Subscribers: Mordante, merge_guards_bot, xazax.hun, kristof.beyls, cfe-commits

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D73098
2020-01-27 19:23:21 +00:00
Alexander Lanin 84c5f19637 Extend misc-misplaced-const to detect using declarations as well as typedef 2020-01-22 15:26:11 -05:00
Aaron Ballman e3b15ed376 Revert "Extend misc-misplaced-const to detect using declarations as well as typedef"
This reverts commit ecc7dae50c due to breaking bots:

http://lab.llvm.org:8011/builders/clang-x86_64-debian-fast/builds/22157
http://lab.llvm.org:8011/builders/clang-ppc64be-linux/builds/43297
2020-01-22 09:06:24 -05:00
Alexander Lanin ecc7dae50c Extend misc-misplaced-const to detect using declarations as well as typedef 2020-01-22 08:45:20 -05:00
Adam Balogh fccd0da5ee [clang-tidy] New check: bugprone-misplaced-pointer-arithmetic-in-alloc
Finds cases where an integer expression is added to the result
of a memory allocation function instead of its argument.

Differential Revision: https://reviews.llvm.org/D71001
2020-01-21 14:38:15 +01:00
Nathan a42c3eb599 [clang-tidy] Add check for CERT-OOP57-CPP
Summary:
This is a very basic warning implementation of [[ https://wiki.sei.cmu.edu/confluence/display/cplusplus/OOP57-CPP.+Prefer+special+member+functions+and+overloaded+operators+to+C+Standard+Library+functions | Prefer special member functions and overloaded operators to C Standard Library functions ]]

It absolutely needs some fine tuning though.

Reviewers: alexfh, hokein, aaron.ballman, JonasToth

Reviewed By: aaron.ballman

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

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D72488
2020-01-20 17:09:03 +00:00
Logan Smith 42a0355816 Add `bugprone-reserved-identifier`
This patch adds bugprone-reserved-identifier, which flags uses of __names _Like
::_this, which are reserved for the implementation. The check can optionally be
inverted, i.e. configured to flag any names that are _not_ reserved, which may
be useful for e.g. standard library implementors.
2020-01-17 08:44:21 -05:00
Hans Wennborg 5852475e2c Bump the trunk major version to 11
and clear the release notes.
2020-01-15 13:38:01 +01:00
Nathan James 36fcbb838c Added readability-qualified-auto check
Adds a check that detects any auto variables that are deduced to a pointer or
a const pointer then adds in the const and asterisk according. Will also
check auto L value references that could be written as const. This relates
to the coding standard
https://llvm.org/docs/CodingStandards.html#beware-unnecessary-copies-with-auto
2020-01-14 14:06:46 -05:00
Sylvestre Ledru 68cd283f3b clang-tidy doc: unbreak the CI 2020-01-10 22:25:01 +01:00
Sylvestre Ledru faeeb71a17 clang-tidy doc: Refresh the list of checkers and polish the script 2020-01-10 22:07:47 +01:00
Hans Wennborg 164da67300 Restore order in clang-tidy section of release notes
Major changes are introduction of subsubsections to prevent people
putting new entries in wrong places. I also polished line length and
highlighting.

Patch by Eugene Zelenko!
2020-01-10 09:19:58 +01:00
Sylvestre Ledru b38d0d5bdb clang-tidy doc - remove the widths 2020-01-10 08:11:05 +01:00
Mitchell Balan 73d93617d3 [clang-tidy] modernize-use-using uses AST and now supports struct defintions and multiple types in a typedef
Summary:
It now handles `typedef`s that include comma-separated multiple types, and handles embedded struct definitions, which previously could not be automatically converted.

For example, with this patch `modernize-use-using` now can convert:

typedef struct { int a; } R_t, *R_p;

to:

using R_t = struct { int a; };
using R_p = R_t*;

`-ast-dump` showed that the `CXXRecordDecl` definitions and multiple `TypedefDecl`s come consecutively in the tree, so `check()` stores information between calls to determine when it is receiving a second or additional `TypedefDecl` within a single `typedef`, or when the current `TypedefDecl` refers to an embedded `CXXRecordDecl` like a `struct`.

Reviewers: alexfh, aaron.ballman

Patch by: poelmanc

Subscribers: riccibruno, sammccall, cfe-commits, aaron.ballman

Tags: clang-tools-extra, clang

Differential Revision: https://reviews.llvm.org/D70270
2020-01-07 16:36:11 -05:00
Tamás Zolnai 350da402ef [clang-tidy] new check: bugprone-signed-char-misuse
Summary:
This check searches for signed char -> integer conversions which might
indicate programming error, because of the misinterpretation of char
values. A signed char might store the non-ASCII characters as negative
values. The human programmer probably expects that after an integer
conversion the converted value matches with the character code
(a value from [0..255]), however, the actual value is in
[-128..127] interval.

See also:
STR34-C. Cast characters to unsigned char before converting to larger integer sizes
<https://wiki.sei.cmu.edu/confluence/display/c/STR34-C.+Cast+characters+to+unsigned+char+before+converting+to+larger+integer+sizes>

By now this check is limited to assignment / variable declarations.
If we would catch all signed char -> integer conversion, then it would
produce a lot of findings and also false positives. So I added only
this use case now, but this check can be extended with additional
use cases later.
The CERT documentation mentions another use case when the char is
used for array subscript. Next to that a third use case can be
the signed char - unsigned char comparison, which also a use case
where things happen unexpectedly because of conversion to integer.

Reviewers: alexfh, hokein, aaron.ballman

Reviewed By: aaron.ballman

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

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D71174
2020-01-06 18:21:26 +01:00
Kazuaki Ishizaki b7ecf1c1c3 NFC: Fix trivial typos in comments 2020-01-04 10:28:41 -05:00
Kazuaki Ishizaki 7ab9acd8f4 Fix trivial typos in comments; NFC 2020-01-02 13:41:43 -05:00
Alexander Lanin 8188c998ff [docs] Update path to clang-tools-extra
Summary:
> tools/clang/tools/extra
has become
>clang-tools-extra
which was not updated in all docs.

Reviewers: alexfh, aaron.ballman, ilya-biryukov, juliehockett

Reviewed By: aaron.ballman

Subscribers: Jim, cfe-commits

Tags: #clang-tools-extra, #clang

Differential Revision: https://reviews.llvm.org/D71982
2020-01-02 19:30:29 +08:00
Sylvestre Ledru 773667b8c2 clang-tidy doc: Remove severities as they don't make consensus
Reviewers: jdoerfert, aaron.ballman

Reviewed By: aaron.ballman

Subscribers: whisperity, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D72049
2020-01-01 15:42:46 +01:00
Sylvestre Ledru e8c9110b56 clang-tidy doc: modernize-make-unique has an autofix 2019-12-31 11:56:17 +01:00
Florin Iucha c16b3ec597 Fix false positive in magic number checker.
cppcoreguidelines-avoid-magic-numbers should not warn about enum class.
Fixes PR40640.
2019-12-24 10:03:00 -05:00
Sylvestre Ledru 8131c04836 doc: Document that extra-arg/extra-arg-before can be used several times
Hopefully, it will help other people
2019-12-24 13:07:08 +01:00
Sylvestre Ledru c96c606b85 clang-doc remove trailing whitespaces 2019-12-24 13:07:08 +01:00
Sylvestre Ledru d2c9c9157b Move from a long list of checkers to tables
Summary:
Currently, the list isn't very useful.
This change adds two tables.

* The checkers
* The aliases

For each checkers, we provide extract info:

* the severity. Taken from codechecker - https://github.com/Ericsson/codechecker/blob/master/config/checker_severity_map.json
* if the checker has an autofix or not

I used the cvs format for the table because:
* it is easy
* the data could be reused by other tools (we could move
that into a separated / generated file at some point)

Reviewers: alexfh, jdoerfert, jfb, lebedev.ri, Eugene.Zelenko

Subscribers: dexonsmith, wuzish, nemanjai, kbarton, arphaman, lebedev.ri, whisperity, Eugene.Zelenko, JonasToth, JDevlieghere, xazax.hun, cfe-commits, #clang-tools-extra

Tags: #clang

Differential Revision: https://reviews.llvm.org/D36051
2019-12-23 18:44:31 +01:00
Sylvestre Ledru dac98cfa03 Fix the links to clang analyzers checkers 2019-12-19 22:31:24 +01:00
Sylvestre Ledru 918d393972 Fix some typos in the clang-tools-extra doc 2019-12-19 22:23:35 +01:00
Gabor Bencze bbc9f6c2ef [clang-tidy] Add cert-oop58-cpp check
The check warns when (a member of) the copied object is assigned to in a
copy constructor or copy assignment operator. Based on
https://wiki.sei.cmu.edu/confluence/display/cplusplus/OOP58-CPP.+Copy+operations+must+not+mutate+the+source+object

Differential Revision: https://reviews.llvm.org/D70052
2019-12-15 16:30:14 +01:00
Florin Iucha 6dcb1003f2 Optionally exclude bitfield definitions from magic numbers check
Adds the IgnoreBitFieldsWidths option to readability-magic-numbers.
2019-12-07 12:33:10 -05:00
Zachary Turner 64f74bf72e [clang-tidy] Rewrite modernize-avoid-bind check.
This represents largely a full re-write of modernize-avoid-bind, adding
significant new functionality in the process. In particular:

* Both boost::bind and std::bind are now supported
* Function objects are supported in addition to functions
* Member functions are supported
* Nested calls are supported using capture-init syntax
* std::ref() and boost::ref() are now recognized, and will capture by reference.
* Rather than capturing with a global =, we now build up an individual
  capture list that is both necessary and sufficient for the call.
* Fixits are supported in a much larger variety of scenarios than before.

All previous tests pass under the re-write, but a large number of new
tests have been added as well.

Differential Revision: https://reviews.llvm.org/D70368
2019-12-02 15:36:26 -08:00
Clement Courbet 95fe54931f [clang-tidy] new performance-no-automatic-move check.
Summary: The check flags constructs that prevent automatic move of local variables.

Reviewers: aaron.ballman

Subscribers: mgorny, xazax.hun, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D70390
2019-11-22 08:47:55 +01:00
Mitchell Balan 24aafcadff [clang-tidy] modernize-use-equals-default avoid adding redundant semicolons
Summary:
`modernize-use-equals-default` replaces default constructors/destructors with `= default;`. When the optional semicolon after a member function is present, this results in two consecutive semicolons.

This patch checks to see if the next non-comment token after the code to be replaced is a semicolon, and if so offers a replacement of `= default` rather than `= default;`.

This patch adds trailing comments and semicolons to about 5 existing tests.

Reviewers: malcolm.parsons, angelgarcia, aaron.ballman, alexfh

Patch by: poelmanc

Subscribers: MyDeveloperDay, JonasToth, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D70144
2019-11-20 18:08:37 -05:00
Mitchell Balan 4f7dce78c2 [NFC] Attempting to fix sphinx build failure with badly encoded characters from incoming patch. 2019-11-19 11:39:26 -05:00
Mitchell Balan 62871305c2 [NFC] Attempting to fix sphinx build failure with badly encoded characters from incoming patch. 2019-11-19 11:13:40 -05:00
Mitchell Balan 980653621e [clang-tidy] Give readability-redundant-member-init an option IgnoreBaseInCopyConstructors to avoid breaking code with gcc -Werror=extra
Summary:
readability-redundant-member-init removes redundant / unnecessary member and base class initialization. Unfortunately for the specific case of a copy constructor's initialization of a base class, gcc at strict warning levels warns if "base class is not initialized in the copy constructor of a derived class".

This patch adds an option `IgnoreBaseInCopyConstructors` defaulting to 0 (thus maintaining current behavior by default) to skip the specific case of removal of redundant base class initialization in the copy constructor. Enabling this option enables the resulting code to continue to compile successfully under `gcc -Werror=extra`. New test cases `WithCopyConstructor1` and `WithCopyConstructor2` in clang-tools-extra/test/clang-tidy/readability-redundant-member-init.cpp show that it removes redundant members even from copy constructors.

Reviewers: malcolm.parsons, alexfh, hokein, aaron.ballman, lebedev.ri

Patch by: poelmanc

Subscribers: mgehre, lebedev.ri, cfe-commits

Tags: #clang, #clang-tools-extra

Differential revision: https://reviews.llvm.org/D69145
2019-11-19 10:59:21 -05:00
Mitchell Balan df11117086 [clang-tidy] modernize-use-override new option AllowOverrideAndFinal
Summary:
In addition to adding `override` wherever possible, clang-tidy's `modernize-use-override` nicely removes `virtual` when `override` or `final` is specified, and further removes override when final is specified. While this is great default behavior, when code needs to be compiled with gcc at high warning levels that include `gcc -Wsuggest-override` or `gcc -Werror=suggest-override`, clang-tidy's removal of the redundant `override` keyword causes gcc to emit a warning or error. This discrepancy / conflict has been noted by others including a comment on Stack Overflow and by Mozilla's Firefox developers.

This patch adds an AllowOverrideAndFinal option defaulting to 0 - thus preserving current behavior - that when enabled allows both `override` and `final` to co-exist, while still fixing all other issues.

The patch includes a test file verifying all combinations of virtual/override/final, and mentions the new option in the release notes.

Reviewers: alexfh, djasper, JonasToth

Patch by: poelmanc

Subscribers: JonasToth, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D70165
2019-11-19 07:52:32 -05:00
Mitchell Balan 41ee54e5d1 Revert "[clang-tidy] Fix readability-redundant-string-init for c++17/c++2a"
This reverts commit 06f3dabe4a.
2019-11-19 07:52:31 -05:00
Balázs Kéri e8a4c74f11 [clang-tidy] Added DefaultOperatorNewCheck.
Summary:
Added new checker 'cert-default-operator-new' that checks for
CERT rule MEM57-CPP. Simple version.

Reviewers: aaron.ballman, alexfh, JonasToth, lebedev.ri

Reviewed By: aaron.ballman

Subscribers: hiraditya, martong, mehdi_amini, mgorny, inglorion, xazax.hun, dkrupp, steven_wu, dexonsmith, Szelethus, gamesh411, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D67545
2019-11-19 11:31:44 +01:00
Mitchell Balan 12d7500ba2 [clang-tidy] Give readability-redundant-string-init a customizable list of string types to fix
Summary:
This patch adds a feature requested in https://reviews.llvm.org/D69238 to enable `readability-redundant-string-init` to take a list of strings to apply the fix to rather than hard-coding `basic_string`. It adds a `StringNames` option of semicolon-delimited names of string classes to which to apply this fix. Tests ensure this works with test class out::TestString as well as std::string and std::wstring as before. It should be applicable to llvm::StringRef, QString, etc.

Note: This commit was previously reverted due to a failing unit test. That test has been fixed in this version.

Reviewers: MyDeveloperDay, aaron.ballman, hokein, alexfh, JonasToth, gribozavr2

Patch by: poelmanc

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

Tags: #clang-tools-extra, #clang

Differential Revision: https://reviews.llvm.org/D69548
2019-11-15 18:09:42 -05:00
Mitchell Balan 06f3dabe4a [clang-tidy] Fix readability-redundant-string-init for c++17/c++2a
Summary:
`readability-redundant-string-init` was one of several clang-tidy checks documented as failing for C++17. (The failure mode in C++17 is that it changes `std::string Name = ""`; to `std::string Name = Name;`, which actually compiles but crashes at run-time.)

Analyzing the AST with `clang -Xclang -ast-dump` showed that the outer `CXXConstructExprs` that previously held the correct SourceRange were being elided in C++17/2a, but the containing `VarDecl` expressions still had all the relevant information. So this patch changes the fix to get its source ranges from `VarDecl`.

It adds one test `std::string g = "u", h = "", i = "uuu", j = "", k;` to confirm proper warnings and fixit replacements in a single `DeclStmt` where some strings require replacement and others don't. The readability-redundant-string-init.cpp and readability-redundant-string-init-msvc.cpp tests now pass for C++11/14/17/2a.

Reviewers: gribozavr, etienneb, alexfh, hokein, aaron.ballman, gribozavr2

Patch by: poelmanc

Subscribers: NoQ, MyDeveloperDay, Eugene.Zelenko, dylanmckay, cfe-commits

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D69238
2019-11-15 18:09:42 -05:00
Mitchell Balan 47bd7c57e5 Revert "[clang-tidy] Give readability-redundant-string-init a customizable list of string types to fix"
This reverts commit 96fbc32cb9.
2019-11-15 16:46:58 -05:00
Mitchell Balan 96fbc32cb9 [clang-tidy] Give readability-redundant-string-init a customizable list of string types to fix
Summary:
This patch adds a feature requested in https://reviews.llvm.org/D69238 to enable `readability-redundant-string-init` to take a list of strings to apply the fix to rather than hard-coding `basic_string`. It adds a `StringNames` option of semicolon-delimited names of string classes to which to apply this fix. Tests ensure this works with test class out::TestString as well as std::string and std::wstring as before. It should be applicable to llvm::StringRef, QString, etc.

Reviewers: MyDeveloperDay, aaron.ballman, hokein, alexfh, JonasToth, gribozavr2

Patch by: poelmanc

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

Tags: #clang-tools-extra, #clang

Differential Revision: https://reviews.llvm.org/D69548
2019-11-15 16:42:54 -05:00
Simon Pilgrim b6cd799e29 Fix filename typo in rG8d288a0668a5 2019-11-11 17:23:21 +00:00
Abel Kocsis 8d288a0668 [clang-tidy] Add bugprone-bad-signal-to-kill-thread check and its alias cert-pos44-c 2019-11-11 17:47:14 +01:00
Abel Kocsis 8cec7e0208 Revert "[clang-tidy] Add bugprone-bad-signal-to-kill-thread checker and alias cert-pos44-c"
This reverts commit 4edf0cb0e0.
2019-11-11 17:34:04 +01:00
Abel Kocsis 4edf0cb0e0 [clang-tidy] Add bugprone-bad-signal-to-kill-thread checker and alias cert-pos44-c 2019-11-11 17:26:44 +01:00
Matthias Gehre 24130d661e [clang-tidy] Add readability-make-member-function-const
Summary:
Finds non-static member functions that can be made ``const``
because the functions don't use ``this`` in a non-const way.

The check conservatively tries to preserve logical costness in favor of
physical costness. See readability-make-member-function-const.rst for more
details.

Reviewers: aaron.ballman, gribozavr, hokein, alexfh

Subscribers: mgorny, xazax.hun, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D68074
2019-11-06 09:27:02 +01:00
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
Stephane Moore 9baf4958cd [clang-tidy] Regenerate clang-tidy check list 📋
Summary:
These changes were generated by invoking
clang-tools-extra/clang-tidy/add_new_check.py and then reverting the
check that was added.

Reviewers: aaron.ballman

Reviewed By: aaron.ballman

Subscribers: xazax.hun, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D69414
2019-10-30 18:15:34 -07:00
Vladimir Plyashkun 4de6b15868 Add an option to hicpp-signed-bitwise for positive integer literals.
This gives developers a way to deviate from the coding standard to reduce the
chattiness of the check.
2019-10-30 14:11:29 -04:00
Aaron Ballman 0de262d718 Move this release note to its appropriate location; NFC. 2019-10-30 13:48:26 -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
Csaba Dabis adac533d95 [clang-tidy] bugprone-not-null-terminated-result: Sphinx adjustments 2
llvm-svn: 374710
2019-10-13 08:49:43 +00:00
Csaba Dabis 2e77fcb05b [clang-tidy] bugprone-not-null-terminated-result: Sphinx adjustments
llvm-svn: 374709
2019-10-13 08:41:24 +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
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
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 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
Haojian Wu f8ecb24822 [clangd] Fix the stale documentation about background indexing.
Reviewers: kadircet

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, cfe-commits

Tags: #clang

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

llvm-svn: 372825
2019-09-25 08:26:32 +00:00
Fangrui Song 168b3fb38b [clang-tidy] Add bugprone-infinite-loop.rst from D64736 to fix buildbot
llvm-svn: 372711
2019-09-24 09:32:00 +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
Simon Pilgrim cb3d969453 Revert rL372693 : [clang-tidy] New bugprone-infinite-loop check for detecting obvious infinite loops
Finding infinite loops is well-known to be impossible (halting problem).
However, it is possible to detect some obvious infinite loops, for example,
if the loop condition is not changed. Detecting such loops is beneficial
since the tests will hang on programs containing infinite loops so
testing-time detection may be costly in large systems. Obvious cases are
where the programmer forgets to increment/decrement the counter or
increments/decrements the wrong variable.

Differential Revision: https://reviews.llvm.org/D64736
-------
Broke some buildbots "No SOURCES given to target: obj.clangTidyBugproneModule"

llvm-svn: 372704
2019-09-24 08:56:44 +00:00
Adam Balogh 54b78f3bb6 [clang-tidy] New bugprone-infinite-loop check for detecting obvious infinite loops
Finding infinite loops is well-known to be impossible (halting problem).
However, it is possible to detect some obvious infinite loops, for example,
if the loop condition is not changed. Detecting such loops is beneficial
since the tests will hang on programs containing infinite loops so
testing-time detection may be costly in large systems. Obvious cases are
where the programmer forgets to increment/decrement the counter or
increments/decrements the wrong variable.

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

llvm-svn: 372693
2019-09-24 07:43:26 +00:00
Shaurya Gupta 7e56dd0229 [Clang-doc] NFC: Fixed link to llvm bugs in documentation
llvm-svn: 372627
2019-09-23 16:55:13 +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
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 3716547c5f Fix the rst doc, unbreak buildbot.
llvm-svn: 371968
2019-09-16 09:46:53 +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
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
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
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
Alex Lorenz 559ae14c9b Remove clang-tidy-vs from clang-tools-extra (PR41791)
The clang-tidy-vs visual studio plugin in clang-tools-extra contains a
security vulnerability in the YamlDotNet package [1]. I posted to cfe-dev [2],
asking if there was anyone who was interested in updating the the plugin
to address the vulnerability. Reid mentioned that Zach (the original committer),
said that there's another plugin (Clang Power Tools) that provides clang-tidy support,
with additional extra features, so it would be ok to remove clang-tidy-vs.

This commit removes the plugin to address the security vulnerability, and adds
a section to the release notes that mentions that the plugin was removed, and
suggests to use Clang Power Tools.

Fixes PR 41791.

[1]: https://nvd.nist.gov/vuln/detail/CVE-2018-1000210
[2]: http://lists.llvm.org/pipermail/cfe-dev/2019-August/063196.html

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

llvm-svn: 370096
2019-08-27 18:36:08 +00:00