================ @@ -0,0 +1,65 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "RedundantZeroInitializerCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Type.h" +#include "clang/AST/TypeLoc.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +namespace { +AST_MATCHER(InitListExpr, isSingleElementBracedList) { + return Node.isExplicit() && Node.getNumInits() == 1; +} + +AST_MATCHER(InitListExpr, isInMacro) { + return Node.getBeginLoc().isMacroID() || Node.getEndLoc().isMacroID(); +} + +AST_MATCHER(InitListExpr, hasScalarArrayType) { + const ConstantArrayType *CAT = + Finder->getASTContext().getAsConstantArrayType(Node.getType()); + return CAT && CAT->getElementType()->isScalarType(); +} + +// Matches an array declarator whose bound is deduced from the initializer +// (``T a[]``); replacing ``{0}`` with ``{}`` there would change the size. +AST_MATCHER(TypeLoc, hasDeducedArrayBound) { + const auto ATL = Node.getAs<ArrayTypeLoc>(); + return ATL && ATL.getSizeExpr() == nullptr; +} +} // namespace + +void RedundantZeroInitializerCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + initListExpr( + hasScalarArrayType(), isSingleElementBracedList(), + hasInit(0, ignoringParenImpCasts(integerLiteral(equals(0)))), + unless(isInMacro()), + unless(hasParent(varDecl(hasTypeLoc(hasDeducedArrayBound()))))) ---------------- zwuis wrote:
```cpp hasTypeLoc(arrayTypeLoc(/* ... */)) ``` Sorry I forgot that there is a `arrayTypeLoc` matcher. https://github.com/llvm/llvm-project/pull/209367 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
