Commit Graph

20 Commits

Author SHA1 Message Date
John Stiles
3215385aeb Disable ClangTidy namespace comments for short blocks.
Namespace comments don't assist readability for blocks that can easily
fit on a single screen.

Change-Id: I93cbebe8e51400dead794c9eb41cb1eaa86bf756
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/312639
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
2020-08-24 14:01:22 +00:00
John Stiles
a008b0fa8b Enable ClangTidy check readability-redundant-smartptr-get.
To my surprise, this even works with homegrown smart pointers (such as
SkTLazy).

https://clang.llvm.org/extra/clang-tidy/checks/readability-redundant-smartptr-get.html

Find and remove redundant calls to smart pointer’s .get() method.

Examples:

  ptr.get()->Foo()  ==>  ptr->Foo()
  *ptr.get()  ==>  *ptr
  *ptr->get()  ==>  **ptr
  if (ptr.get() == nullptr) ... => if (ptr == nullptr) ...

Change-Id: I8ff541e0229656b4d8e875c8053a7e6138302547
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/310976
Auto-Submit: John Stiles <johnstiles@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2020-08-16 15:56:48 +00:00
John Stiles
c1c3c6d70d Enable ClangTidy flag bugprone-suspicious-string-compare.
https://clang.llvm.org/extra/clang-tidy/checks/bugprone-suspicious-string-compare.html

Find suspicious usage of runtime string comparison functions.
This check is valid in C and C++.

Checks for calls with implicit comparator and proposed to
explicitly add it:

  if (strcmp(...))       // Implicitly compare to zero
  if (!strcmp(...))      // Won't warn
  if (strcmp(...) != 0)  // Won't warn

Checks that compare function results (i,e, strcmp) are compared to valid
constant. The resulting value is

  <  0    when lower than,
  >  0    when greater than,
  == 0    when equals.

A common mistake is to compare the result to 1 or -1:

  if (strcmp(...) == -1)  // Incorrect usage of the returned value.

Additionally, the check warns if the results value is implicitly cast
to a suspicious non-integer type. It’s happening when the returned
value is used in a wrong context:

  if (strcmp(...) < 0.)  // Incorrect usage of the returned value.

Change-Id: I001b88d06cc4f3eb5846103885be675f9b78e126
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/310761
Commit-Queue: John Stiles <johnstiles@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2020-08-16 03:54:08 +00:00
John Stiles
fe0de30a87 Enable ClangTidy check modernize-use-nullptr.
https://clang.llvm.org/extra/clang-tidy/checks/modernize-use-nullptr.html

The check converts the usage of null pointer constants (eg. NULL, 0) to
use the new C++11 nullptr keyword.

Change-Id: Iaea2d843154c70e49d62affdc5dceb3bca8c1089
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/310297
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2020-08-14 16:14:30 +00:00
John Stiles
1cf2c8d6ec Enable ClangTidy flag modernize-use-override.
https://clang.llvm.org/extra/clang-tidy/checks/modernize-use-override.html

Adds override (introduced in C++11) to overridden virtual functions and
removes virtual from those functions as it is not required.

virtual on non base class implementations was used to help indicate to
the user that a function was virtual. C++ compilers did not use the
presence of this to signify an overridden function.

Change-Id: If66d8919358f72a4035190caf8d7569268037a9a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/310160
Commit-Queue: Mike Klein <mtklein@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2020-08-14 10:54:45 +00:00
John Stiles
31954bf23f Enable ClangTidy check performance-unnecessary-copy-initialization.
https://clang.llvm.org/extra/clang-tidy/checks/performance-unnecessary-copy-initialization.html

Finds local variable declarations that are initialized using the copy
constructor of a non-trivially-copyable type but it would suffice to
obtain a const reference.

The check is only applied if it is safe to replace the copy by a const
reference. This is the case when the variable is const qualified or when
it is only used as a const, i.e. only const methods or operators are
invoked on it, or it is used as const reference or value argument in
constructors or function calls.

Change-Id: I1261410deccd8ea64e85edec53fbd5360940e587
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/308759
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2020-08-07 22:20:36 +00:00
John Stiles
ec9b4aab87 Enable ClangTidy check readability-const-return-type.
https://clang.llvm.org/extra/clang-tidy/checks/readability-const-return-type.html

`const` on a non-pointer/reference return type typically doesn't add
value and can have negative side effects. (i.e., returning a
`const std::string` isn't meaningfully different from returning a
`std::string`, but can sometimes inhibit move-related optimizations.)

In Skia's case, the priv() functions are a notable exception where const
return types are intentional and valuable. These calls have been marked
with NOLINT to exclude them from the check.

This check does not affect pointer and reference returns, where
constness is important.

Change-Id: I86cab92332f164e5ab710b4127182eec99831d7d
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/308564
Commit-Queue: John Stiles <johnstiles@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2020-08-07 17:42:38 +00:00
John Stiles
3977088e23 Enable ClangTidy check readability-static-accessed-through-instance.
Our codebase generally tends to avoid this pattern naturally, and this
can catch real mistakes, e.g. http://review.skia.org/308659

https://clang.llvm.org/extra/clang-tidy/checks/readability-static-accessed-through-instance.html

Change-Id: I3b37ab6242bcca310bbbec718facdd8687f9c4ff
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/308658
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2020-08-07 16:57:17 +00:00
John Stiles
a6841be235 Enable ClangTidy check llvm-namespace-comment.
This fixes a large number of SkSL namespaces which were labeled as if
they were anonymous, and also a handful of other mislabeled namespaces.
Missing namespace-end comments have been added throughout.
A number of diffs are just indentation-related (adjusting 1- or 3-
space indents to 2-space).

Change-Id: I6c62052a0d3aea4ae12ca07e0c2a8587b2fce4ec
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/308503
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
2020-08-06 19:07:52 +00:00
John Stiles
fbd050bd0b Enable ClangTidy check modernize-make-unique.
The majority of existing call sites were automatically updated using
clang-tidy -fix. A small handful required a manual update,
e.g. CppCodeGen.

This check is a bit lenient, and in particular will not flag cases like
`std::unique_ptr<Base>(new Derived())` which is still pretty common
throughout our codebase. This CL does not attempt to replace all the
cases that ClangTidy does not flag.

Change-Id: I5eba48ef880e25d22de80f321a68c389ba769e36
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/307459
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2020-08-03 17:53:52 +00:00
John Stiles
0633f76324 Enable ClangTidy check misc-definitions-in-headers.
https://clang.llvm.org/extra/clang-tidy/checks/misc-definitions-in-headers.html

Finds non-extern non-inline function and variable definitions in header
files, which can lead to potential ODR violations in case these headers
are included from multiple translation units.

Change-Id: I5a80d8bddbc7fae97a3b714ac4e376bcefc3b060
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/307436
Commit-Queue: John Stiles <johnstiles@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2020-08-02 18:53:15 +00:00
John Stiles
8423d060bd Enable ClangTidy check readability-redundant-preprocessor.
https://clang.llvm.org/extra/clang-tidy/checks/readability-redundant-preprocessor.html
-----
Finds potentially redundant preprocessor directives.

- #ifdef .. #endif pairs which are nested inside an outer pair with the
  same condition. For example:

```
#ifdef FOO
#ifdef FOO // inner ifdef is considered redundant
void f();
#endif
#endif
```

- #ifndef .. #endif pairs
- #ifndef inside an #ifdef with the same condition
- #ifdef inside an #ifndef with the same condition:
- #if .. #endif pairs which are nested inside an outer pair with the
  same condition.

Change-Id: Ic1c745c0755a985e34f77a398686bf36f310acf2
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/307343
Reviewed-by: Mike Klein <mtklein@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
2020-08-02 18:49:05 +00:00
John Stiles
ad1e2005f1 Add ClangTidy check bugprone-bool-pointer-implicit-conversion.
------
https://clang.llvm.org/extra/clang-tidy/checks/bugprone-bool-pointer-implicit-conversion.html
Checks for conditions based on implicit conversion from a bool pointer to bool.

Example:

bool *p;
if (p) {
  // Never used in a pointer-specific way.
}

Change-Id: I9399469df2d2ea1980ed6d996430f0fd6631ff0c
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/307342
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
2020-07-31 22:17:24 +00:00
John Stiles
bfbe71252a Enable ClangTidy check performance-for-range-copy.
This will prevent us from writing range-based for loops that copy the
loop variable when a const& would suffice.

https://clang.llvm.org/extra/clang-tidy/checks/performance-for-range-copy.html
-----
Finds C++11 for ranges where the loop variable is copied in each
iteration but it would suffice to obtain it by const reference.

The check is only applied to loop variables of types that are expensive
to copy which means they are not trivially copyable or have a
non-trivial copy constructor or destructor.

Change-Id: Ic26bff7e9c48b4d1a9ad9c0606199920ea7a0af8
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/306945
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2020-07-31 01:42:51 +00:00
John Stiles
e554301263 Enable ClangTidy guard bugprone-argument-comment.
https://clang.llvm.org/extra/clang-tidy/checks/bugprone-argument-comment.html
-----
Checks that argument comments match parameter names.

The check understands argument comments in the form /*parameter_name=*/
that are placed right before the argument.

  void f(bool foo);

  ...

  f(/*bar=*/true);
  // warning: argument name 'bar' in comment does not match parameter
  // name 'foo'

The check tries to detect typos and suggest automated fixes for them.
-----

Change-Id: I76fa82f5338f465b8f9e5a13654195f25a618b35
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/306848
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2020-07-30 18:24:00 +00:00
John Stiles
5f2b2d6dc6 Enable ClangTidy guard bugprone-unused-raii.
https://clang.llvm.org/extra/clang-tidy/checks/bugprone-unused-raii.html
-------
Finds temporaries that look like RAII objects.

The canonical example for this is a scoped lock.

{
  scoped_lock(&global_mutex);
  critical_section();
}

The destructor of the scoped_lock is called before the critical_section
is entered, leaving it unprotected.
-------

Change-Id: I40394eb84e7e4ad0564c409ecb52df2ef5af35b6
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/306838
Reviewed-by: Mike Klein <mtklein@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
2020-07-30 16:17:20 +00:00
John Stiles
398c654ce7 Enable ClangTidy 'undelegated constructor' warning.
Change-Id: I79dee7422f8a24fa0fffef33b131e96b5bf0d4ba
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/306728
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
2020-07-29 23:26:00 +00:00
Mike Klein
c33d614357 add google-build-namespaces to clang-tidy checks
Android's using this check in their clang-tidy builds.

The check itself is well intentioned but doesn't seem to take into
account the particular reason we do this... being able to use these
types and functions from files compiled with different optimization
settings without causing ODR violations or runtime crashes.

Each of the places that's marked is using an anonymous namespace
from a header for good reason, but I don't mind making clang-tidy
ask us to explicitly exempt any others that may come up in the
future.  It's definitely unusual, and rarely the best idea.

Adding -header-filters='.*' actually checks headers...
until now they've been ignored.

Change-Id: Ie421d2b47076bd384b10c7339cfb7a1c3ea90906
Reviewed-on: https://skia-review.googlesource.com/c/176963
Commit-Queue: Mike Klein <mtklein@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
Auto-Submit: Mike Klein <mtklein@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
2018-12-12 16:33:59 +00:00
Mike Klein
21840ed430 restrict -Werror to runs of clang-tidy.sh
Android runs clang-tidy with a different set of checks
that override our choices in .clang-tidy.

Change-Id: I95d92bb9b61bc5f94fe2bb8bff382edd876d2594
Reviewed-on: https://skia-review.googlesource.com/c/176962
Reviewed-by: Mike Klein <mtklein@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
2018-12-12 14:29:50 +00:00
Mike Klein
1688507f83 Try out clang-tidy, starting with bugprone-use-after-move
- add drop-in clang-tidy cxx wrapper
 - get build clean for bugprone-use-after-move

The wrapper can be used by setting

   cxx = "/path/to/skia/tools/clang-tidy.sh"

in GN.

Change-Id: Idbba911e23bd6ef7530b08fd31906b92c1c1b28c
Reviewed-on: https://skia-review.googlesource.com/c/176523
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
2018-12-11 17:28:19 +00:00