Commit Graph

547 Commits

Author SHA1 Message Date
Aaron Ballman f71c9661df Test to ensure the function does not have an unresolved or unevaluated exception specification before testing whether the function throws or not. Fixes PR25574.
llvm-svn: 253598
2015-11-19 20:45:35 +00:00
NAKAMURA Takumi 67361cc2e1 Revert r253401, "[clang-tidy] add check cppcoreguidelines-pro-bounds-constant-array-index"
cppcoreguidelines-pro-bounds-constant-array-index.cpp is failing in several hosts.

llvm-svn: 253428
2015-11-18 02:14:35 +00:00
Matthias Gehre 55020566ed [clang-tidy] add check cppcoreguidelines-pro-bounds-constant-array-index
Summary:
This check flags all array subscriptions on static arrays and
std::arrays that either have a non-compile-time-constant index or are
out of bounds.

Dynamic accesses into arrays are difficult for both tools and humans to
validate as safe. array_view is a bounds-checked, safe type for
accessing arrays of data. at() is another alternative that ensures
single accesses are bounds-checked. If iterators are needed to access an
array, use the iterators from an array_view constructed over the array.

This rule is part of the "Bounds safety" profile of the C++ Core
Guidelines, see
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-bounds2-only-index-into-arrays-using-constant-expressions

Reviewers: alexfh, sbenza, bkramer, aaron.ballman

Subscribers: cfe-commits

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

llvm-svn: 253401
2015-11-17 23:43:20 +00:00
Matthias Gehre 4722f1921a Fix bug 25362 "cppcoreguidelines-pro-bounds-array-to-pointer-decay does not consider const"
Summary:
The current matcher is
  implicitCastExpr(unless(hasParent(explicitCastExpr())))
but the AST in the bug is
  `-CXXStaticCastExpr 0x2bb64f8 <col:21, col:55> 'void *const *'
static_cast<void *const *> <NoOp>
    `-ImplicitCastExpr 0x2bb64e0 <col:47> 'void *const *' <NoOp>
      `-ImplicitCastExpr 0x2bb64c8 <col:47> 'void **'
<ArrayToPointerDecay>
        `-DeclRefExpr 0x2bb6458 <col:47> 'void *[2]' lvalue Var
0x2bb59d0 'addrlist' 'void *[2]'
i.e. an ImplicitCastExpr (const cast) between decay and explicit cast.

Reviewers: alexfh, sbenza, bkramer, aaron.ballman

Subscribers: cfe-commits

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

llvm-svn: 253399
2015-11-17 23:35:39 +00:00
Aaron Ballman 5a786ddf4c Add a new clang-tidy checker that flags throw expressions whose thrown type is not nothrow copy constructible. While the compiler is free to elide copy constructor calls in some cases, it is under no obligation to do so, which makes the code a portability concern as well as a security concern.
This checker corresponds to the CERT secure coding rule: https://www.securecoding.cert.org/confluence/display/cplusplus/ERR60-CPP.+Exception+objects+must+be+nothrow+copy+constructible

llvm-svn: 253246
2015-11-16 19:17:43 +00:00
Samuel Benzaquen b43962768a Fix bug in suggested fix that truncated variable names to 1 character.
Summary:
Fix bug in suggested fix that truncated variable names to 1 character.
Also, rework the suggested fix to try to remove unnecessary whitespace.

Reviewers: alexfh, aaron.ballman

Subscribers: cfe-commits

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

llvm-svn: 252773
2015-11-11 18:40:36 +00:00
Daniel Marjamaki 1e9ef81187 [clang-tidy] misc-macro-parentheses: fix fp when using object member pointers
Fixes http://llvm.org/PR25208.

llvm-svn: 252608
2015-11-10 14:32:25 +00:00
Alexander Kornienko 64956b5e9a Add ExtraArgs and ExtraArgsBefore options to enable clang warnings via configuration files.
Summary: This patch depends on http://reviews.llvm.org/D14191

Reviewers: djasper, klimek

Subscribers: cfe-commits

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

llvm-svn: 252485
2015-11-09 16:28:11 +00:00
Alexander Kornienko 301130ef7c [clang-tidy] Fix message style (capitalization, trailing period).
llvm-svn: 252471
2015-11-09 15:53:28 +00:00
Matthias Gehre eeb71758cc [clang-tidy] add new check cppcoreguidelines-pro-type-cstyle-cast
Summary:
This check flags all use of c-style casts that perform a static_cast
downcast, const_cast, or reinterpret_cast.

Use of these casts can violate type safety and cause the program to
access a
variable that is actually of type X to be accessed as if it were of an
unrelated type Z. Note that a C-style (T)expression cast means to
perform
the first of the following that is possible: a const_cast, a
static_cast, a
static_cast followed by a const_cast, a reinterpret_cast, or a
reinterpret_cast followed by a const_cast. This rule bans (T)expression
only when used to perform an unsafe cast.

This rule is part of the "Type safety" profile of the C++ Core
Guidelines, see
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-type4-dont-use-c-style-texpression-casts-that-would-perform-a-static_cast-downcast-const_cast-or-reinterpret_cast.

Reviewers: alexfh, sbenza, bkramer, aaron.ballman

Subscribers: cfe-commits

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

llvm-svn: 252425
2015-11-08 21:10:39 +00:00
Angel Garcia Gomez 2c19d4cee3 Allow the alias to be of a different type.
Summary: Consider a declaration an alias even if it doesn't have the same unqualified type than the container element, as long as one can be converted to the other using only implicit casts.

Reviewers: klimek

Subscribers: alexfh, cfe-commits

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

llvm-svn: 252315
2015-11-06 15:47:04 +00:00
Angel Garcia Gomez 7056f7488f Use the old index identifier by default, instead of 'elem'.
Summary: Use the old index name in the cases where the check would come up with an invented name.

Reviewers: klimek

Subscribers: cfe-commits

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

llvm-svn: 252308
2015-11-06 15:03:14 +00:00
Angel Garcia Gomez 7e1d4ae937 Avoid naming conflicts with the old index in modernize-loop-convert.
Summary: The old index declaration is going to be removed anyway, so we can reuse its name if it is the best candidate for the new index.

Reviewers: klimek

Subscribers: cfe-commits, alexfh

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

llvm-svn: 252303
2015-11-06 14:04:12 +00:00
Angel Garcia Gomez 7856ad0bcc Fix another case where loop-convert wasn't handling correctly data members.
Summary:
If the container expression was obtained from the point where "size" (which usually is a const method) is invoked, then the topmost node in this expression may be an implicit cast to const.

When the container is a data member, the check was trying to obtain the member expression directly and was failing in the case mentioned above. This is solved by ignoring implicit casts.

Reviewers: klimek

Subscribers: cfe-commits, alexfh

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

llvm-svn: 252278
2015-11-06 09:59:14 +00:00
Alexander Kornienko 272397b42e [clang-tidy] readability-named-parameter: don't complain about implicit parameters
Fixes http://llvm.org/PR24464.

llvm-svn: 252248
2015-11-06 00:19:21 +00:00
Angel Garcia Gomez 3d7d922832 Improve modernize-make-unique matcher.
Summary: "std::unique_ptr<int>" is not the same type as "std::unique_ptr<int, std::default_delete<int>>", unless we insert a "hasCanonicalType" in the middle. Probably it also happens in other cases related to default template argument.

Reviewers: klimek

Subscribers: alexfh, cfe-commits

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

llvm-svn: 252041
2015-11-04 10:27:51 +00:00
Angel Garcia Gomez 432ff5e205 Handle correctly containers that are data members in modernize-loop-convert.
Summary:
I recently found that the variable naming wasn't working as expected with containers that are data members. The new index always received the name "Elem" (or equivalent) regardless of the container's name.
The check was assuming that the container's declaration was a VarDecl, which cannot be converted to a FieldDecl (a data member), and then it could never retrieve its name.

This also fixes some cases where the check failed to find the container at all (so it didn't do any fix) because of the same reason.

Reviewers: klimek

Subscribers: cfe-commits, alexfh

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

llvm-svn: 251943
2015-11-03 16:38:31 +00:00
Angel Garcia Gomez 5b5c94ba2c Improve more the const-detection in modernize-loop-convert.
Summary: The previous change was focused in detecting when a non-const object was used in a constant way. Looks like I forgot the most important and trivial case: when the object is already constant. Failing to detect this cases results in compile errors, due to trying to bind a constant object to a non-const reference in the range-for statement. This change should fix that.

Reviewers: klimek

Subscribers: alexfh, cfe-commits

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

llvm-svn: 251940
2015-11-03 16:31:36 +00:00
Angel Garcia Gomez 5daa9b231f Make the modernize-loop-convert's const-detection smarter.
Summary:
Now, it detects that several kinds of usages are can't modify the elements. Examples:
-When an usage is a call to a const member function or operator of the element.
-If the element is used as an argument to a function or constructor that takes a const-reference or a value.
-LValue to RValue conversion, if the element is a fundamental type (which allows the use of most of the builtin operators).

Reviewers: klimek

Subscribers: cfe-commits, alexfh

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

llvm-svn: 251808
2015-11-02 17:02:52 +00:00
Angel Garcia Gomez ff11a44fe0 Fix another crash in the redundant-void-arg check.
Summary: The check was assuming that a definition of a function always has a body, but a declaration that explicitly defaults or deletes a function is a definition too.

Reviewers: alexfh

Subscribers: klimek, cfe-commits

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

llvm-svn: 251807
2015-11-02 16:18:23 +00:00
Angel Garcia Gomez dc39f313e2 Remove unreachable that was reached in modernize-use-nullptr.
Summary: When traversing the parent map, the check assumed that all the nodes would be either Stmt or Decl. After r251101, this is no longer true: there can be TypeLoc and NestedNameSpecifierLoc nodes.

Reviewers: alexfh

Subscribers: klimek, cfe-commits

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

llvm-svn: 251803
2015-11-02 15:28:06 +00:00
Angel Garcia Gomez dd4ed3af98 Fix crash in redundant-void-arg check.
Summary:
When applying this check to the unit tests, it would hit an assertion:
llvm/tools/clang/lib/Lex/Lexer.cpp:1056: clang::SourceLocation clang::Lexer::getSourceLocation(const char*, unsigned int) const: Assertion `PP && "This doesn't work on raw lexers"' failed.

Reviewers: klimek, LegalizeAdulthood, alexfh

Subscribers: cfe-commits, alexfh

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

llvm-svn: 251792
2015-11-02 11:39:17 +00:00
Angel Garcia Gomez 184864adc6 Try to fix buildbots failure.
Summary: Add -fexceptions flag to enable exceptions.

Subscribers: cfe-commits

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

llvm-svn: 251790
2015-11-02 10:54:50 +00:00
Angel Garcia Gomez f2bc2f001a modernize-use-default supports copy constructor and copy-assignment operator.
Summary: the check will now warn when the user provided definitions of this functions is equivalent to the explicitly defaulted ones.

Reviewers: klimek

Subscribers: klimek, cfe-commits, alexfh

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

llvm-svn: 251788
2015-11-02 10:34:19 +00:00
Angel Garcia Gomez 68175a02fb Only copy small types in modernize-loop-convert.
Summary: If the size of the type is above a certain bound, we'll take a const reference. This bound can be set as an option. For now, the default value is 16 bytes.

Reviewers: klimek

Subscribers: alexfh, cfe-commits

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

llvm-svn: 251694
2015-10-30 09:37:57 +00:00
Daniel Jasper 15cffc00b3 Change test to just define NULL instead of #including stddef.h. In some
test environments, even that builtin header isn't available. Also, this
makes it more consistent with the C++ version of this test.

llvm-svn: 251520
2015-10-28 14:51:09 +00:00
NAKAMURA Takumi 257fc92826 clang-tools-extra/test/clang-tidy/modernize-redundant-void-arg.c: Use <stddef.h> provided by clang, instead of <stdio.h>.
llvm-svn: 251503
2015-10-28 09:22:21 +00:00
Benjamin Kramer e18da83bcc [tidy] Remove stray iostream include from test.
It is unused and we cannot rely on standard headers being present while
executing tests.

llvm-svn: 251499
2015-10-28 05:16:37 +00:00
Alexander Kornienko c3ceb27690 Add modernize-redundant-void-arg check to clang-tidy
This check for clang-tidy looks for function with zero arguments declared as (void) and removes the unnecessary void token.

  int foo(void);

becomes

  int foo();

The check performs no formatting of the surrounding context but uses the lexer to look for the token sequence "(", "void", ")" in the prototype text. If this sequence of tokens is found, a removal is issued for the void token only.


Patch by Richard Thomson!

(+fixed tests, moved the check to the modernize module)

Differential revision: http://reviews.llvm.org/D7639

llvm-svn: 251475
2015-10-28 01:36:20 +00:00
Matthias Gehre f33319699d [clang-tidy] Add new check cppcoreguidelines-pro-bounds-array-to-pointer-decay
Summary:
This check flags all array to pointer decays.

Pointers should not be used as arrays. array_view is a bounds-checked,
safe alternative to using pointers to access arrays.

This rule is part of the "Bounds safety" profile of the C++ Core
Guidelines, see
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-bounds3-no-array-to-pointer-decay

Reviewers: alexfh, sbenza, bkramer, aaron.ballman

Subscribers: cfe-commits

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

llvm-svn: 251358
2015-10-26 21:56:02 +00:00
Piotr Dziwinski 9c5c7a6ab4 [clang-tidy] Another fix for failing buildbots regarding signedness of char
I totally forgot that char can be defined as unsigned on some platforms.
Now I made explicit mention of signed type where necessary in tests.

Also fixed '//RUN: ' header of cxx98 test to correct format.

llvm-svn: 251244
2015-10-25 17:11:13 +00:00
Piotr Dziwinski db9d130fb1 [clang-tidy] Fix for build bots not liking #include <cstddef>
llvm-svn: 251239
2015-10-25 15:47:21 +00:00
Piotr Dziwinski 7f1b5099d7 [clang-tidy] Add check readability-implicit-bool-cast
Summary:
This is another check that I ported to clang-tidy from colobot-lint tool.

As previously discussed on cfe-dev mailing list, this is one of those
checks that I think is general and useful enough for contribution to
clang-tidy.

This patch contains implementation of check taken from colobot-lint, but
it is extended a great deal, including FixIt hints for automated
refactoring, exhaustive testcases, and user documentation.

Reviewers: sbenza, aaron.ballman, alexfh

Subscribers: Eugene.Zelenko

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

llvm-svn: 251235
2015-10-25 15:31:25 +00:00
Manuel Klimek 5fdd843d14 Make isExpensiveToCopy() tri-state.
This allows returning "don't know" for dependent types.

llvm-svn: 251103
2015-10-23 10:00:50 +00:00
Manuel Klimek 8f9e444061 Switch check_clang_tidy to argparse and add a -resource-dir argument.
-resource-dir can be used to inject non-standard resource dirs via the
lit site config.

llvm-svn: 251021
2015-10-22 14:54:50 +00:00
Angel Garcia Gomez d8336f3af5 Don't use "auto" on loops over fundamental types in modernize-loop-convert.
Summary: using "auto" on a loop that iterates over ints is kind of an overkill. Use the real type name instead.

Reviewers: klimek

Subscribers: alexfh, cfe-commits

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

llvm-svn: 251015
2015-10-22 13:23:46 +00:00
Angel Garcia Gomez 37ab7fda4c Correctly print the type in modernize-make-unique.
Summary: Take into account the current LangOptions the check has to add back the template argument.

Reviewers: klimek

Subscribers: alexfh, cfe-commits

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

llvm-svn: 251013
2015-10-22 13:20:49 +00:00
Manuel Klimek b91bee06de Add %check_clang_tidy and %clang_tidy_diff.
With this, site specific lit configs can inject parameters into the
test scripts if they need site specific parameters.

Next up: enable check_clang_tidy to take a resource dir to enable
non-standard locations for builtin includes.

llvm-svn: 251010
2015-10-22 11:31:44 +00:00
NAKAMURA Takumi 6025991fa9 clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-type-vararg.cpp: Tweak not to depend on out-of-tree header <cstdarg>.
llvm-svn: 250986
2015-10-22 04:51:47 +00:00
NAKAMURA Takumi 05063bd4cb clang-tools-extra/test/clang-tidy/modernize-use-default.cpp: Appease MS mode.
llvm-svn: 250983
2015-10-22 04:32:21 +00:00
Matthias Gehre 27da23464f [clang-tidy] add check cppcoreguidelines-pro-type-vararg
Summary:
This check flags all calls to c-style vararg functions and all use
of va_list, va_start and va_arg.

Passing to varargs assumes the correct type will be read. This is
fragile because it cannot generally be enforced to be safe in the
language and so relies on programmer discipline to get it right.

This rule is part of the "Type safety" profile of the C++ Core
Guidelines, see
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-type8-avoid-reading-from-varargs-or-passing-vararg-arguments-prefer-variadic-template-parameters-instead

This commits also reverts
  "[clang-tidy] add cert's VariadicFunctionDefCheck as cppcoreguidelines-pro-type-vararg-def"
because that check makes the SFINAE use of vararg functions impossible.

Reviewers: alexfh, sbenza, bkramer, aaron.ballman

Subscribers: cfe-commits

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

llvm-svn: 250939
2015-10-21 20:09:02 +00:00
Angel Garcia Gomez 8dedeb0230 Add modernize-use-default check to clang-tidy.
Summary:
Add a check that replaces empty bodies of special member functions with '= default;'.
For now, it is only implemented for the default constructor and the destructor, which are the easier cases.
The copy-constructor and the copy-assignment operator cases will be implemented later.

I applied this check to the llvm code base and found 627 warnings (385 in llvm, 9 in compiler-rt, 220 in clang and 13 in clang-tools-extra).
Applying the fixes didn't break any build or test, it only caused a -Wpedantic warning in lib/Target/Mips/MipsOptionRecord.h:33 becaused it replaced
virtual ~MipsOptionRecord(){}; to virtual ~MipsOptionRecord()= default;;

Reviewers: klimek

Subscribers: george.burgess.iv, Eugene.Zelenko, alexfh, cfe-commits

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

llvm-svn: 250897
2015-10-21 12:58:15 +00:00
Samuel Benzaquen daef163199 Added check uniqueptr-delete-release to replace "delete x.release()" with "x = nullptr"
Reviewers: alexfh

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

llvm-svn: 250742
2015-10-19 21:49:51 +00:00
Matthias Gehre b785407c28 [clang-tidy] add check cppcoreguidelines-pro-type-union-access
Summary:
This check flags all access to members of unions. Passing unions as a
whole is not flagged.

Reading from a union member assumes that member was the last one
written, and writing to a union member assumes another member with a
nontrivial destructor had its destructor called. This is fragile because
it cannot generally be enforced to be safe in the language and so relies
on programmer discipline to get it right.

This rule is part of the "Type safety" profile of the C++ Core
Guidelines, see
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-type7-avoid-accessing-members-of-raw-unions-prefer-variant-instead

Reviewers: alexfh, sbenza, bkramer, aaron.ballman

Subscribers: cfe-commits

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

llvm-svn: 250537
2015-10-16 18:46:30 +00:00
Angel Garcia Gomez 2918904673 Replacements in different files do not overlap.
Summary: Prevent clang-tidy from discarding fixes that are in different files but happen to have the same file offset.

Reviewers: klimek, bkramer

Subscribers: bkramer, alexfh, cfe-commits

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

llvm-svn: 250523
2015-10-16 16:15:27 +00:00
Angel Garcia Gomez 9eb6e2e3cb Use __SIZE_TYPE__ to fix buildbot failures.
Summary: Use __SIZE_TYPE__ to fix buildbot failures.

Reviewers: klimek

Subscribers: cfe-commits

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

llvm-svn: 250288
2015-10-14 10:30:32 +00:00
Angel Garcia Gomez 2df36481b6 Prevent modernize-use-auto from emitting a warning when 'auto' was already being used.
Summary: This fixes https://llvm.org/bugs/show_bug.cgi?id=25082 .

Reviewers: bkramer, klimek

Subscribers: cfe-commits, alexfh

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

llvm-svn: 250284
2015-10-14 09:29:55 +00:00
Angel Garcia Gomez baf573eb4c Support every kind of initialization.
Summary: modernize-make-unique now correctly supports the different kinds of list initialization.

Reviewers: klimek

Subscribers: cfe-commits, alexfh

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

llvm-svn: 250283
2015-10-14 09:22:32 +00:00
Aaron Ballman 17b6feef69 Expose the clang-tidy misc-assign-operator-signature checker as cppcoreguidelines-c-copy-assignment-signature.
llvm-svn: 250165
2015-10-13 15:24:33 +00:00
Matthias Gehre dc48412c93 [clang-tidy] new check cppcoreguidelines-pro-bounds-pointer-arithmetic
Summary:
This check flags all usage of pointer arithmetic, because it could lead
to an
invalid pointer.
Subtraction of two pointers is not flagged by this check.

Pointers should only refer to single objects, and pointer arithmetic is
fragile and easy to get wrong. array_view is a bounds-checked, safe type
for accessing arrays of data.

This rule is part of the "Bounds safety" profile of the C++ Core
Guidelines, see
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-bounds1-dont-use-pointer-arithmetic-use-array_view-instead

Depends on D13313

Subscribers: cfe-commits

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

llvm-svn: 250116
2015-10-12 21:53:19 +00:00