summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2016-06-03 21:22:58 +0000
committerAlexander Kornienko <alexfh@google.com>2016-06-03 21:22:58 +0000
commitc0308c451bc1f2b19ea4c60b3606edf6e1651731 (patch)
treee8dc44a114298d15f1e4a5a26638b2f88403e76e
parent94edaaaefba9727db12206de96c9046fe1629646 (diff)
downloadbcm5719-llvm-c0308c451bc1f2b19ea4c60b3606edf6e1651731.tar.gz
bcm5719-llvm-c0308c451bc1f2b19ea4c60b3606edf6e1651731.zip
[clang-tidy] modernize-use-auto: don't remove stars by default
Summary: By default, modernize-use-auto check will retain stars when replacing an explicit type with `auto`: `MyType *t = new MyType;` will be changed to `auto *t = new MyType;`, thus resulting in more consistency with the recommendations to use `auto *` for iterating over pointers in range-based for loops: http://llvm.org/docs/CodingStandards.html#beware-unnecessary-copies-with-auto The new `RemoveStars` option allows to revert to the old behavior: with the new option turned on the check will change `MyType *t = new MyType;` to `auto t = new MyType;`. Reviewers: aaron.ballman, sbenza Subscribers: Eugene.Zelenko, cfe-commits Differential Revision: http://reviews.llvm.org/D20917 llvm-svn: 271739
-rw-r--r--clang-tools-extra/clang-tidy/modernize/UseAutoCheck.cpp51
-rw-r--r--clang-tools-extra/clang-tidy/modernize/UseAutoCheck.h7
-rw-r--r--clang-tools-extra/docs/clang-tidy/checks/modernize-use-auto.rst21
-rw-r--r--clang-tools-extra/test/clang-tidy/modernize-use-auto-new-remove-stars.cpp106
-rw-r--r--clang-tools-extra/test/clang-tidy/modernize-use-auto-new.cpp27
5 files changed, 175 insertions, 37 deletions
diff --git a/clang-tools-extra/clang-tidy/modernize/UseAutoCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseAutoCheck.cpp
index 415ecdd83ad..1f1c140f027 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseAutoCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseAutoCheck.cpp
@@ -243,6 +243,14 @@ StatementMatcher makeDeclWithNewMatcher() {
} // namespace
+UseAutoCheck::UseAutoCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ RemoveStars(Options.get("RemoveStars", 0)) {}
+
+void UseAutoCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "RemoveStars", RemoveStars ? 1 : 0);
+}
+
void UseAutoCheck::registerMatchers(MatchFinder *Finder) {
// Only register the matchers for C++; the functionality currently does not
// provide any benefit to other languages, despite being benign.
@@ -311,7 +319,7 @@ void UseAutoCheck::replaceNew(const DeclStmt *D, ASTContext *Context) {
const QualType FirstDeclType = FirstDecl->getType().getCanonicalType();
- std::vector<SourceLocation> StarLocations;
+ std::vector<FixItHint> StarRemovals;
for (const auto *Dec : D->decls()) {
const auto *V = cast<VarDecl>(Dec);
// Ensure that every DeclStmt child is a VarDecl.
@@ -327,19 +335,23 @@ void UseAutoCheck::replaceNew(const DeclStmt *D, ASTContext *Context) {
if (!Context->hasSameUnqualifiedType(V->getType(), NewExpr->getType()))
return;
- // Remove explicitly written '*' from declarations where there's more than
- // one declaration in the declaration list.
- if (Dec == *D->decl_begin())
- continue;
-
- // All subsequent declarations should match the same non-decorated type.
+ // All subsequent variables in this declaration should have the same
+ // canonical type. For example, we don't want to use `auto` in
+ // `T *p = new T, **pp = new T*;`.
if (FirstDeclType != V->getType().getCanonicalType())
return;
- auto Q = V->getTypeSourceInfo()->getTypeLoc().getAs<PointerTypeLoc>();
- while (!Q.isNull()) {
- StarLocations.push_back(Q.getStarLoc());
- Q = Q.getNextTypeLoc().getAs<PointerTypeLoc>();
+ if (RemoveStars) {
+ // Remove explicitly written '*' from declarations where there's more than
+ // one declaration in the declaration list.
+ if (Dec == *D->decl_begin())
+ continue;
+
+ auto Q = V->getTypeSourceInfo()->getTypeLoc().getAs<PointerTypeLoc>();
+ while (!Q.isNull()) {
+ StarRemovals.push_back(FixItHint::CreateRemoval(Q.getStarLoc()));
+ Q = Q.getNextTypeLoc().getAs<PointerTypeLoc>();
+ }
}
}
@@ -347,19 +359,20 @@ void UseAutoCheck::replaceNew(const DeclStmt *D, ASTContext *Context) {
// is the same as the initializer, just more CV-qualified. However, TypeLoc
// information is not reliable where CV qualifiers are concerned so we can't
// do anything about this case for now.
- SourceRange Range(
- FirstDecl->getTypeSourceInfo()->getTypeLoc().getSourceRange());
+ TypeLoc Loc = FirstDecl->getTypeSourceInfo()->getTypeLoc();
+ if (!RemoveStars) {
+ while (Loc.getTypeLocClass() == TypeLoc::Pointer ||
+ Loc.getTypeLocClass() == TypeLoc::Qualified)
+ Loc = Loc.getNextTypeLoc();
+ }
+ SourceRange Range(Loc.getSourceRange());
auto Diag = diag(Range.getBegin(), "use auto when initializing with new"
" to avoid duplicating the type name");
// Space after 'auto' to handle cases where the '*' in the pointer type is
// next to the identifier. This avoids changing 'int *p' into 'autop'.
- Diag << FixItHint::CreateReplacement(Range, "auto ");
-
- // Remove '*' from declarations using the saved star locations.
- for (const auto &Loc : StarLocations) {
- Diag << FixItHint::CreateReplacement(Loc, "");
- }
+ Diag << FixItHint::CreateReplacement(Range, RemoveStars ? "auto " : "auto")
+ << StarRemovals;
}
void UseAutoCheck::check(const MatchFinder::MatchResult &Result) {
diff --git a/clang-tools-extra/clang-tidy/modernize/UseAutoCheck.h b/clang-tools-extra/clang-tidy/modernize/UseAutoCheck.h
index d2218876df1..c63db06b503 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseAutoCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/UseAutoCheck.h
@@ -18,15 +18,16 @@ namespace modernize {
class UseAutoCheck : public ClangTidyCheck {
public:
- UseAutoCheck(StringRef Name, ClangTidyContext *Context)
- : ClangTidyCheck(Name, Context) {}
-
+ UseAutoCheck(StringRef Name, ClangTidyContext *Context);
+ void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
void replaceIterators(const DeclStmt *D, ASTContext *Context);
void replaceNew(const DeclStmt *D, ASTContext *Context);
+
+ const bool RemoveStars;
};
} // namespace modernize
diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize-use-auto.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize-use-auto.rst
index f6207840dc7..8a31c90966a 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/modernize-use-auto.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/modernize-use-auto.rst
@@ -124,7 +124,7 @@ pointee type has to be written twice: in the declaration type and in the
// becomes
- auto my_pointer = new TypeName(my_param);
+ auto *my_pointer = new TypeName(my_param);
The check will also replace the declaration type in multiple declarations, if
the following conditions are satisfied:
@@ -141,7 +141,7 @@ the following conditions are satisfied:
// becomes
- auto my_first_pointer = new TypeName, my_second_pointer = new TypeName;
+ auto *my_first_pointer = new TypeName, *my_second_pointer = new TypeName;
Known Limitations
-----------------
@@ -150,3 +150,20 @@ Known Limitations
* User-defined iterators are not handled at this time.
+RemoveStars option
+------------------
+If the option is set to non-zero (default is `0`), the check will remove stars
+from the non-typedef pointer types when replacing type names with ``auto``.
+Otherwise, the check will leave stars. For example:
+
+.. code-block:: c++
+
+ TypeName *my_first_pointer = new TypeName, *my_second_pointer = new TypeName;
+
+ // RemoveStars = 0
+
+ auto *my_first_pointer = new TypeName, *my_second_pointer = new TypeName;
+
+ // RemoveStars = 1
+
+ auto my_first_pointer = new TypeName, my_second_pointer = new TypeName;
diff --git a/clang-tools-extra/test/clang-tidy/modernize-use-auto-new-remove-stars.cpp b/clang-tools-extra/test/clang-tidy/modernize-use-auto-new-remove-stars.cpp
new file mode 100644
index 00000000000..42859b8f1b5
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/modernize-use-auto-new-remove-stars.cpp
@@ -0,0 +1,106 @@
+// RUN: %check_clang_tidy %s modernize-use-auto %t -- \
+// RUN: -config="{CheckOptions: [{key: modernize-use-auto.RemoveStars, value: '1'}]}" \
+// RUN: -- -std=c++11
+
+class MyType {};
+
+class MyDerivedType : public MyType {};
+
+// FIXME: the replacement sometimes results in two consecutive spaces after
+// the word 'auto' (due to the presence of spaces at both sides of '*').
+void auto_new() {
+ MyType *a_new = new MyType();
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
+ // CHECK-FIXES: auto a_new = new MyType();
+
+ static MyType *a_static = new MyType();
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use auto when initializing with new
+ // CHECK-FIXES: static auto a_static = new MyType();
+
+ MyType *derived = new MyDerivedType();
+
+ void *vd = new MyType();
+
+ // CV-qualifier tests.
+ //
+ // NOTE : the form "type const" is expected here because of a deficiency in
+ // TypeLoc where CV qualifiers are not considered part of the type location
+ // info. That is, all that is being replaced in each case is "MyType *" and
+ // not "MyType * const".
+ static MyType * const d_static = new MyType();
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use auto when initializing with new
+ // CHECK-FIXES: static auto const d_static = new MyType();
+
+ MyType * const a_const = new MyType();
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
+ // CHECK-FIXES: auto const a_const = new MyType();
+
+ MyType * volatile vol = new MyType();
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
+ // CHECK-FIXES: auto volatile vol = new MyType();
+
+ struct SType {} *stype = new SType;
+
+ int (**func)(int, int) = new (int(*[5])(int,int));
+
+ int *array = new int[5];
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
+ // CHECK-FIXES: auto array = new int[5];
+
+ MyType *ptr(new MyType);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
+ // CHECK-FIXES: auto ptr(new MyType);
+
+ MyType *ptr2{new MyType};
+
+ {
+ // Test for declaration lists.
+ MyType *a = new MyType(), *b = new MyType(), *c = new MyType();
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
+ // CHECK-FIXES: auto a = new MyType(), b = new MyType(), c = new MyType();
+
+ // Non-initialized declaration should not be transformed.
+ MyType *d = new MyType(), *e;
+
+ MyType **f = new MyType*(), **g = new MyType*();
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
+ // CHECK-FIXES: auto f = new MyType*(), g = new MyType*();
+
+ // Mismatching types in declaration lists should not be transformed.
+ MyType *h = new MyType(), **i = new MyType*();
+
+ // '*' shouldn't be removed in case of mismatching types with multiple
+ // declarations.
+ MyType *j = new MyType(), *k = new MyType(), **l = new MyType*();
+ }
+
+ {
+ // Test for typedefs.
+ typedef int * int_p;
+ // CHECK-FIXES: typedef int * int_p;
+
+ int_p a = new int;
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
+ // CHECK-FIXES: auto a = new int;
+ int_p *b = new int*;
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
+ // CHECK-FIXES: auto b = new int*;
+
+ // Test for typedefs in declarations lists.
+ int_p c = new int, d = new int;
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
+ // CHECK-FIXES: auto c = new int, d = new int;
+
+ // Different types should not be transformed.
+ int_p e = new int, *f = new int_p;
+
+ int_p *g = new int*, *h = new int_p;
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
+ // CHECK-FIXES: auto g = new int*, h = new int_p;
+ }
+
+ // Don't warn when 'auto' is already being used.
+ auto aut = new MyType();
+ auto *paut = new MyType();
+ const auto *pcaut = new MyType();
+}
diff --git a/clang-tools-extra/test/clang-tidy/modernize-use-auto-new.cpp b/clang-tools-extra/test/clang-tidy/modernize-use-auto-new.cpp
index ccf48d509d0..7ced0a4735e 100644
--- a/clang-tools-extra/test/clang-tidy/modernize-use-auto-new.cpp
+++ b/clang-tools-extra/test/clang-tidy/modernize-use-auto-new.cpp
@@ -9,11 +9,11 @@ class MyDerivedType : public MyType {};
void auto_new() {
MyType *a_new = new MyType();
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
- // CHECK-FIXES: auto a_new = new MyType();
+ // CHECK-FIXES: auto *a_new = new MyType();
static MyType *a_static = new MyType();
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use auto when initializing with new
- // CHECK-FIXES: static auto a_static = new MyType();
+ // CHECK-FIXES: static auto *a_static = new MyType();
MyType *derived = new MyDerivedType();
@@ -27,15 +27,15 @@ void auto_new() {
// not "MyType * const".
static MyType * const d_static = new MyType();
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use auto when initializing with new
- // CHECK-FIXES: static auto const d_static = new MyType();
+ // CHECK-FIXES: static auto * const d_static = new MyType();
MyType * const a_const = new MyType();
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
- // CHECK-FIXES: auto const a_const = new MyType();
+ // CHECK-FIXES: auto * const a_const = new MyType();
MyType * volatile vol = new MyType();
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
- // CHECK-FIXES: auto volatile vol = new MyType();
+ // CHECK-FIXES: auto * volatile vol = new MyType();
struct SType {} *stype = new SType;
@@ -43,11 +43,11 @@ void auto_new() {
int *array = new int[5];
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
- // CHECK-FIXES: auto array = new int[5];
+ // CHECK-FIXES: auto *array = new int[5];
MyType *ptr(new MyType);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
- // CHECK-FIXES: auto ptr(new MyType);
+ // CHECK-FIXES: auto *ptr(new MyType);
MyType *ptr2{new MyType};
@@ -55,14 +55,14 @@ void auto_new() {
// Test for declaration lists.
MyType *a = new MyType(), *b = new MyType(), *c = new MyType();
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
- // CHECK-FIXES: auto a = new MyType(), b = new MyType(), c = new MyType();
+ // CHECK-FIXES: auto *a = new MyType(), *b = new MyType(), *c = new MyType();
// Non-initialized declaration should not be transformed.
MyType *d = new MyType(), *e;
MyType **f = new MyType*(), **g = new MyType*();
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
- // CHECK-FIXES: auto f = new MyType*(), g = new MyType*();
+ // CHECK-FIXES: auto **f = new MyType*(), **g = new MyType*();
// Mismatching types in declaration lists should not be transformed.
MyType *h = new MyType(), **i = new MyType*();
@@ -75,25 +75,26 @@ void auto_new() {
{
// Test for typedefs.
typedef int * int_p;
+ // CHECK-FIXES: typedef int * int_p;
int_p a = new int;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
- // CHECK-FIXES: auto a = new int;
+ // CHECK-FIXES: auto a = new int;
int_p *b = new int*;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
- // CHECK-FIXES: auto b = new int*;
+ // CHECK-FIXES: auto *b = new int*;
// Test for typedefs in declarations lists.
int_p c = new int, d = new int;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
- // CHECK-FIXES: auto c = new int, d = new int;
+ // CHECK-FIXES: auto c = new int, d = new int;
// Different types should not be transformed.
int_p e = new int, *f = new int_p;
int_p *g = new int*, *h = new int_p;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
- // CHECK-FIXES: auto g = new int*, h = new int_p;
+ // CHECK-FIXES: auto *g = new int*, *h = new int_p;
}
// Don't warn when 'auto' is already being used.
OpenPOWER on IntegriCloud