hokein created this revision. hokein added a reviewer: alexfh. hokein added a subscriber: cfe-commits.
r285239 changes the behavior of AST CXXDefaultArgExpr node. Update `modernize-use-nullptr` to handle CXXDefaultArgExpr correctly. https://reviews.llvm.org/D26301 Files: clang-tidy/modernize/UseNullptrCheck.cpp test/clang-tidy/modernize-use-nullptr.cpp Index: test/clang-tidy/modernize-use-nullptr.cpp =================================================================== --- test/clang-tidy/modernize-use-nullptr.cpp +++ test/clang-tidy/modernize-use-nullptr.cpp @@ -217,3 +217,14 @@ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use nullptr // CHECK-FIXES: C<bool, F(nullptr)> c; #undef F + +// Test default argument expression. +struct D { + explicit D(void *t, int *c = NULL) {} + // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use nullptr + // CHECK-FIXES: explicit D(void *t, int *c = nullptr) {} +}; + +void test_default_argument() { + D(nullptr); +} Index: clang-tidy/modernize/UseNullptrCheck.cpp =================================================================== --- clang-tidy/modernize/UseNullptrCheck.cpp +++ clang-tidy/modernize/UseNullptrCheck.cpp @@ -190,13 +190,21 @@ // within a cast expression. bool VisitStmt(Stmt *S) { CastExpr *C = dyn_cast<CastExpr>(S); + // Catch the castExpr inside cxxDefaultArgExpr. + if (CXXDefaultArgExpr *E = dyn_cast<CXXDefaultArgExpr>(S)) + C = dyn_cast<CastExpr>(E->getExpr()); if (!C) { FirstSubExpr = nullptr; return true; } + if (!FirstSubExpr) FirstSubExpr = C->getSubExpr()->IgnoreParens(); + // Ignore the expr if it is already a nullptr literal expr. + if (isa<CXXNullPtrLiteralExpr>(FirstSubExpr)) + return true; + if (C->getCastKind() != CK_NullToPointer && C->getCastKind() != CK_NullToMemberPointer) { return true;
Index: test/clang-tidy/modernize-use-nullptr.cpp =================================================================== --- test/clang-tidy/modernize-use-nullptr.cpp +++ test/clang-tidy/modernize-use-nullptr.cpp @@ -217,3 +217,14 @@ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use nullptr // CHECK-FIXES: C<bool, F(nullptr)> c; #undef F + +// Test default argument expression. +struct D { + explicit D(void *t, int *c = NULL) {} + // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use nullptr + // CHECK-FIXES: explicit D(void *t, int *c = nullptr) {} +}; + +void test_default_argument() { + D(nullptr); +} Index: clang-tidy/modernize/UseNullptrCheck.cpp =================================================================== --- clang-tidy/modernize/UseNullptrCheck.cpp +++ clang-tidy/modernize/UseNullptrCheck.cpp @@ -190,13 +190,21 @@ // within a cast expression. bool VisitStmt(Stmt *S) { CastExpr *C = dyn_cast<CastExpr>(S); + // Catch the castExpr inside cxxDefaultArgExpr. + if (CXXDefaultArgExpr *E = dyn_cast<CXXDefaultArgExpr>(S)) + C = dyn_cast<CastExpr>(E->getExpr()); if (!C) { FirstSubExpr = nullptr; return true; } + if (!FirstSubExpr) FirstSubExpr = C->getSubExpr()->IgnoreParens(); + // Ignore the expr if it is already a nullptr literal expr. + if (isa<CXXNullPtrLiteralExpr>(FirstSubExpr)) + return true; + if (C->getCastKind() != CK_NullToPointer && C->getCastKind() != CK_NullToMemberPointer) { return true;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits