brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.5 KiB · c5ee240 Raw
61 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "PreferRegisterOverUnsignedCheck.h"10#include "clang/ASTMatchers/ASTMatchFinder.h"11 12using namespace clang::ast_matchers;13 14namespace clang::tidy::llvm_check {15 16void PreferRegisterOverUnsignedCheck::registerMatchers(MatchFinder *Finder) {17  auto RegisterClassMatch = hasType(18      cxxRecordDecl(hasName("::llvm::Register")).bind("registerClassDecl"));19 20  Finder->addMatcher(21      traverse(TK_AsIs,22               valueDecl(hasType(qualType(isUnsignedInteger()).bind("varType")),23                         varDecl(hasInitializer(exprWithCleanups(24                                     has(implicitCastExpr(has(cxxMemberCallExpr(25                                         on(RegisterClassMatch),26                                         has(memberExpr(hasDeclaration(27                                             cxxConversionDecl()))))))))))28                             .bind("var"))),29      this);30}31 32void PreferRegisterOverUnsignedCheck::check(33    const MatchFinder::MatchResult &Result) {34  const auto *VarType = Result.Nodes.getNodeAs<QualType>("varType");35  const auto *UserVarDecl = Result.Nodes.getNodeAs<VarDecl>("var");36 37  bool NeedsQualification = true;38  const DeclContext *Context = UserVarDecl->getDeclContext();39  while (Context) {40    if (const auto *Namespace = dyn_cast<NamespaceDecl>(Context))41      if (isa<TranslationUnitDecl>(Namespace->getDeclContext()) &&42          Namespace->getName() == "llvm")43        NeedsQualification = false;44    for (const auto *UsingDirective : Context->using_directives()) {45      const NamespaceDecl *Namespace = UsingDirective->getNominatedNamespace();46      if (isa<TranslationUnitDecl>(Namespace->getDeclContext()) &&47          Namespace->getName() == "llvm")48        NeedsQualification = false;49    }50    Context = Context->getParent();51  }52  diag(UserVarDecl->getLocation(),53       "variable %0 declared as %1; use '%select{|llvm::}2Register' instead")54      << UserVarDecl << *VarType << NeedsQualification55      << FixItHint::CreateReplacement(56             UserVarDecl->getTypeSourceInfo()->getTypeLoc().getSourceRange(),57             NeedsQualification ? "llvm::Register" : "Register");58}59 60} // namespace clang::tidy::llvm_check61