Skip to content

Commit b780c59

Browse files
committedFeb 28, 2017
[clang-tidy] Fix a false positive on modernize-use-nullptr check.
Summary: The false positive happens on two neighbour CXXDefaultArgExpr AST nodes. like below: ``` CXXFunctionalCastExpr 0x85c9670 <col:7, col:23> 'struct ZZ' functional cast to struct ZZ <ConstructorConversion> `-CXXConstructExpr 0x85c9518 <col:7, col:23> 'struct ZZ' 'void (uint64, const uint64 *)' |-CallExpr 0x85a0a90 <col:10, col:22> 'uint64':'unsigned long long' | |-ImplicitCastExpr 0x85a0a78 <col:10> 'uint64 (*)(uint64)' <FunctionToPointerDecay> | | `-DeclRefExpr 0x85a09f0 <col:10> 'uint64 (uint64)' lvalue Function 0x85a06a0 'Hash' 'uint64 (uint64)' | `-CXXDefaultArgExpr 0x85a0ac8 <<invalid sloc>> 'uint64':'unsigned long long' `-CXXDefaultArgExpr 0x85c94f8 <<invalid sloc>> 'const uint64 *' ``` For each particular CXXDefaultArgExpr node, we need to reset FirstSubExpr, otherwise FirstSubExpr will refer to an incorrect expr. Reviewers: alexfh Reviewed By: alexfh Subscribers: JDevlieghere, cfe-commits Differential Revision: https://reviews.llvm.org/D30412 llvm-svn: 296479
1 parent 983c9b9 commit b780c59

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed
 

‎clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,10 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
190190
bool VisitStmt(Stmt *S) {
191191
auto *C = dyn_cast<CastExpr>(S);
192192
// Catch the castExpr inside cxxDefaultArgExpr.
193-
if (auto *E = dyn_cast<CXXDefaultArgExpr>(S))
193+
if (auto *E = dyn_cast<CXXDefaultArgExpr>(S)) {
194194
C = dyn_cast<CastExpr>(E->getExpr());
195+
FirstSubExpr = nullptr;
196+
}
195197
if (!C) {
196198
FirstSubExpr = nullptr;
197199
return true;

‎clang-tools-extra/test/clang-tidy/modernize-use-nullptr.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,19 @@ struct D {
228228
void test_default_argument() {
229229
D(nullptr);
230230
}
231+
232+
// Test on two neighbour CXXDefaultArgExprs nodes.
233+
typedef unsigned long long uint64;
234+
struct ZZ {
235+
explicit ZZ(uint64, const uint64* = NULL) {}
236+
// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: use nullptr
237+
// CHECK-FIXES: explicit ZZ(uint64, const uint64* = nullptr) {}
238+
operator bool() { return true; }
239+
};
240+
241+
uint64 Hash(uint64 seed = 0) { return 0; }
242+
243+
void f() {
244+
bool a;
245+
a = ZZ(Hash());
246+
}

0 commit comments

Comments
 (0)
Please sign in to comment.