================
@@ -0,0 +1,186 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/DeclCXX.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
+#include "clang/AST/NestedNameSpecifierBase.h"
+#include "clang/AST/ParentMapContext.h"
+#include "clang/AST/TemplateBase.h"
+#include "clang/AST/TypeBase.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Lex/Lexer.h"
+#include "llvm/ADT/STLExtras.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::llvm_check {
+
+namespace {
+AST_MATCHER(Expr, isMacroID) { return Node.getExprLoc().isMacroID(); }
+AST_MATCHER_P(OverloadExpr, hasAnyUnresolvedName, ArrayRef<StringRef>, Names) {
+  auto DeclName = Node.getName();
+  if (!DeclName.isIdentifier())
+    return false;
+  const IdentifierInfo *II = DeclName.getAsIdentifierInfo();
+  return llvm::any_of(Names, [II](StringRef Name) { return II->isStr(Name); });
+}
+} // namespace
+
+static constexpr StringRef FunctionNames[] = {
+    "cast",     "cast_or_null",     "cast_if_present",
+    "dyn_cast", "dyn_cast_or_null", "dyn_cast_if_present"};
+
+void RedundantCastingCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsInLLVMNamespace = hasDeclContext(
+      namespaceDecl(hasName("llvm"), hasDeclContext(translationUnitDecl())));
+  auto AnyCalleeName =
+      allOf(unless(isMacroID()), unless(cxxMemberCallExpr()),
+            callee(expr(ignoringImpCasts(
+                declRefExpr(
+                    to(namedDecl(hasAnyName(FunctionNames), 
IsInLLVMNamespace)),
+                    hasAnyTemplateArgumentLoc(anything()))
+                    .bind("callee")))));
+  auto AnyCalleeNameInUninstantiatedTemplate =
+      allOf(unless(isMacroID()), unless(cxxMemberCallExpr()),
+            callee(expr(ignoringImpCasts(
+                unresolvedLookupExpr(hasAnyUnresolvedName(FunctionNames))
+                    .bind("callee")))));
+  Finder->addMatcher(
+      callExpr(AnyCalleeName, optionally(hasParent(
+                                  
callExpr(AnyCalleeName).bind("parent_cast"))))
+          .bind("call"),
+      this);
+  Finder->addMatcher(
+      callExpr(
+          AnyCalleeNameInUninstantiatedTemplate,
+          optionally(hasAncestor(
+              namespaceDecl(hasName("llvm"), hasParent(translationUnitDecl()))
+                  .bind("llvm_ns"))))
+          .bind("call"),
+      this);
+}
+
+static QualType stripPointerOrReference(QualType Ty) {
+  QualType Pointee = Ty->getPointeeType();
+  if (Pointee.isNull())
+    return Ty;
+  return Pointee;
+}
+
+static bool isLLVMNamespace(NestedNameSpecifier NNS) {
+  if (NNS.getKind() != NestedNameSpecifier::Kind::Namespace)
+    return false;
+  auto Pair = NNS.getAsNamespaceAndPrefix();
+  if (Pair.Namespace->getNamespace()->getName() != "llvm")
+    return false;
+  const NestedNameSpecifier::Kind Kind = Pair.Prefix.getKind();
+  return Kind == NestedNameSpecifier::Kind::Null ||
+         Kind == NestedNameSpecifier::Kind::Global;
+}
+
+void RedundantCastingCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto &Nodes = Result.Nodes;
+  const auto *Call = Nodes.getNodeAs<CallExpr>("call");
+  if (Call->getNumArgs() != 1)
+    return;
----------------
zwuis wrote:

Use `argumentCountIs` matcher in `registerMatchers`.

https://github.com/llvm/llvm-project/pull/189274
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to