NoIntToPtrCheck.cpp 1.1 KB

123456789101112131415161718192021222324252627282930
  1. //===--- NoIntToPtrCheck.cpp - clang-tidy ---------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "NoIntToPtrCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. using namespace clang::ast_matchers;
  12. namespace clang::tidy::performance {
  13. void NoIntToPtrCheck::registerMatchers(MatchFinder *Finder) {
  14. Finder->addMatcher(castExpr(hasCastKind(CK_IntegralToPointer),
  15. unless(hasSourceExpression(integerLiteral())))
  16. .bind("x"),
  17. this);
  18. }
  19. void NoIntToPtrCheck::check(const MatchFinder::MatchResult &Result) {
  20. const auto *MatchedCast = Result.Nodes.getNodeAs<CastExpr>("x");
  21. diag(MatchedCast->getBeginLoc(),
  22. "integer to pointer cast pessimizes optimization opportunities");
  23. }
  24. } // namespace clang::tidy::performance