From 6b4c0b5b663841f3c6702e5c1999b599bf1687ca Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Tue, 16 Feb 2016 10:36:51 +0000 Subject: [PATCH] [clang-tidy] Fix an assert failure in `modernize-loop-convert`. Summary: The test code will trigger following an assert failure: assert.h assertion failed at LoopConvertUtils.cpp:560 in bool clang::tidy::modernize::ForLoopIndexUseVisitor::TraverseMemberExpr(clang::MemberExpr*): ExprType->isPointerType() && "Operator-> returned non-pointer type" Reviewers: alexfh Differential Revision: http://reviews.llvm.org/D17287 llvm-svn: 260953 --- .../clang-tidy/modernize/LoopConvertUtils.cpp | 4 +++- .../modernize-loop-convert-assert-failure.cpp | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/test/clang-tidy/modernize-loop-convert-assert-failure.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp index b3dfa93d67c8..03f85836812d 100644 --- a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp +++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp @@ -557,7 +557,9 @@ bool ForLoopIndexUseVisitor::TraverseMemberExpr(MemberExpr *Member) { if (ExprType.isNull()) ExprType = Obj->getType(); - assert(ExprType->isPointerType() && "Operator-> returned non-pointer type"); + if (!ExprType->isPointerType()) + return false; + // FIXME: This works around not having the location of the arrow operator. // Consider adding OperatorLoc to MemberExpr? SourceLocation ArrowLoc = Lexer::getLocForEndOfToken( diff --git a/clang-tools-extra/test/clang-tidy/modernize-loop-convert-assert-failure.cpp b/clang-tools-extra/test/clang-tidy/modernize-loop-convert-assert-failure.cpp new file mode 100644 index 000000000000..1e289017251b --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/modernize-loop-convert-assert-failure.cpp @@ -0,0 +1,18 @@ +// RUN: clang-tidy %s -checks=-*,modernize-loop-convert -- + +// Note: this test expects no assert failure happened in clang-tidy. + +class LinguisticItem { + LinguisticItem *x0; + class x1 { + bool operator!= ( const x1 &; + operator* ( ; + LinguisticItem * &operator-> ( ; + operator++ ( + } begin() const; + x1 end() const { + LinguisticStream x2; + for (x1 x3 = x2.begin x3 != x2.end; ++x3) + x3->x0 + } +};