================ @@ -0,0 +1,242 @@ +//===--- RedundantCastingCheck.cpp - clang-tidy ---------------------------===// +// +// 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 "RedundantCastingCheck.h" +#include "../utils/FixItHintUtils.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static bool areTypesEquals(QualType S, QualType D) { + if (S == D) + return true; + + const auto *TS = S->getAs<TypedefType>(); + const auto *TD = D->getAs<TypedefType>(); + if (TS != TD) + return false; + + QualType PtrS = S->getPointeeType(); + QualType PtrD = D->getPointeeType(); + + if (!PtrS.isNull() && !PtrD.isNull()) + return areTypesEquals(PtrS, PtrD); + + const DeducedType *DT = S->getContainedDeducedType(); + if (DT && DT->isDeduced()) + return D == DT->getDeducedType(); + + return false; +} + +static bool areTypesEquals(QualType TypeS, QualType TypeD, + bool IgnoreTypeAliases) { + const QualType CTypeS = TypeS.getCanonicalType(); + const QualType CTypeD = TypeD.getCanonicalType(); + if (CTypeS != CTypeD) + return false; + + return IgnoreTypeAliases || areTypesEquals(TypeS.getLocalUnqualifiedType(), + TypeD.getLocalUnqualifiedType()); +} + +static bool areBinaryOperatorOperandsTypesEqual(const Expr *E, + bool IgnoreTypeAliases) { + if (!E) + return false; + const Expr *WithoutImplicitAndParen = E->IgnoreParenImpCasts(); + if (!WithoutImplicitAndParen) + return false; + if (const auto *B = dyn_cast<BinaryOperator>(WithoutImplicitAndParen)) { + const QualType NonReferenceType = + WithoutImplicitAndParen->getType().getNonReferenceType(); + if (!areTypesEquals( + B->getLHS()->IgnoreImplicit()->getType().getNonReferenceType(), + NonReferenceType, IgnoreTypeAliases)) + return true; + if (!areTypesEquals( + B->getRHS()->IgnoreImplicit()->getType().getNonReferenceType(), + NonReferenceType, IgnoreTypeAliases)) ---------------- 5chmidti wrote:
Should these three results of `getType` be checked with `isNull`? https://github.com/llvm/llvm-project/pull/70595 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits