Index: clang-tools-extra/trunk/clang-tidy/llvm/CMakeLists.txt =================================================================== --- clang-tools-extra/trunk/clang-tidy/llvm/CMakeLists.txt +++ clang-tools-extra/trunk/clang-tidy/llvm/CMakeLists.txt @@ -5,6 +5,7 @@ IncludeOrderCheck.cpp LLVMTidyModule.cpp PreferIsaOrDynCastInConditionalsCheck.cpp + PreferRegisterOverUnsignedCheck.cpp TwineLocalCheck.cpp LINK_LIBS Index: clang-tools-extra/trunk/clang-tidy/llvm/LLVMTidyModule.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/llvm/LLVMTidyModule.cpp +++ clang-tools-extra/trunk/clang-tidy/llvm/LLVMTidyModule.cpp @@ -13,6 +13,7 @@ #include "HeaderGuardCheck.h" #include "IncludeOrderCheck.h" #include "PreferIsaOrDynCastInConditionalsCheck.h" +#include "PreferRegisterOverUnsignedCheck.h" #include "TwineLocalCheck.h" namespace clang { @@ -28,6 +29,8 @@ "llvm-namespace-comment"); CheckFactories.registerCheck( "llvm-prefer-isa-or-dyn-cast-in-conditionals"); + CheckFactories.registerCheck( + "llvm-prefer-register-over-unsigned"); CheckFactories.registerCheck("llvm-twine-local"); } }; Index: clang-tools-extra/trunk/clang-tidy/llvm/PreferRegisterOverUnsignedCheck.h =================================================================== --- clang-tools-extra/trunk/clang-tidy/llvm/PreferRegisterOverUnsignedCheck.h +++ clang-tools-extra/trunk/clang-tidy/llvm/PreferRegisterOverUnsignedCheck.h @@ -0,0 +1,36 @@ +//===--- PreferRegisterOverUnsignedCheck.h - clang-tidy ---------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERREGISTEROVERUNSIGNEDCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERREGISTEROVERUNSIGNEDCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace llvm_check { + +/// Historically, LLVM has used `unsigned` to represent registers. Since then +/// a `Register` object has been introduced for improved type-safety and make +/// the code more explicit. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/llvm-prefer-register-over-unsigned.html +class PreferRegisterOverUnsignedCheck : public ClangTidyCheck { +public: + PreferRegisterOverUnsignedCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace llvm_check +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVM_PREFERREGISTEROVERUNSIGNEDCHECK_H Index: clang-tools-extra/trunk/clang-tidy/llvm/PreferRegisterOverUnsignedCheck.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/llvm/PreferRegisterOverUnsignedCheck.cpp +++ clang-tools-extra/trunk/clang-tidy/llvm/PreferRegisterOverUnsignedCheck.cpp @@ -0,0 +1,64 @@ +//===--- PreferRegisterOverUnsignedCheck.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 "PreferRegisterOverUnsignedCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace llvm_check { + +void PreferRegisterOverUnsignedCheck::registerMatchers(MatchFinder *Finder) { + auto RegisterClassMatch = hasType( + cxxRecordDecl(hasName("::llvm::Register")).bind("registerClassDecl")); + + Finder->addMatcher( + valueDecl(allOf( + hasType(qualType(isUnsignedInteger()).bind("varType")), + varDecl(hasInitializer(exprWithCleanups(has(implicitCastExpr(has( + cxxMemberCallExpr(allOf(on(RegisterClassMatch), + has(memberExpr(hasDeclaration( + cxxConversionDecl()))))))))))) + .bind("var"))), + this); +} + +void PreferRegisterOverUnsignedCheck::check( + const MatchFinder::MatchResult &Result) { + const auto *VarType = Result.Nodes.getNodeAs("varType"); + const auto *UserVarDecl = Result.Nodes.getNodeAs("var"); + + StringRef Replacement = "llvm::Register"; + const DeclContext *Context = UserVarDecl->getDeclContext(); + while (Context) { + if (const auto *Namespace = dyn_cast(Context)) + if (isa(Namespace->getDeclContext()) && + Namespace->getName() == "llvm") + Replacement = "Register"; + for (const auto *UsingDirective : Context->using_directives()) { + const NamespaceDecl *Namespace = UsingDirective->getNominatedNamespace(); + if (isa(Namespace->getDeclContext()) && + Namespace->getName() == "llvm") + Replacement = "Register"; + } + Context = Context->getParent(); + } + diag(UserVarDecl->getLocation(), + "variable %0 declared as %1; use '%2' instead") + << UserVarDecl << *VarType << Replacement + << FixItHint::CreateReplacement( + UserVarDecl->getTypeSourceInfo()->getTypeLoc().getSourceRange(), + Replacement); +} + +} // namespace llvm_check +} // namespace tidy +} // namespace clang Index: clang-tools-extra/trunk/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/trunk/docs/ReleaseNotes.rst +++ clang-tools-extra/trunk/docs/ReleaseNotes.rst @@ -85,6 +85,13 @@ Finds uses of deprecated Googletest APIs with names containing ``case`` and replaces them with equivalent APIs with ``suite``. +- New :doc:`llvm-prefer-register-over-unsigned + ` check. + + Finds historical use of ``unsigned`` to hold vregs and physregs and rewrites + them to use ``Register`` + + Improvements to include-fixer ----------------------------- Index: clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst =================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst +++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst @@ -275,6 +275,7 @@ llvm-include-order llvm-namespace-comment llvm-prefer-isa-or-dyn-cast-in-conditionals + llvm-prefer-register-over-unsigned llvm-twine-local misc-definitions-in-headers misc-misplaced-const Index: clang-tools-extra/trunk/docs/clang-tidy/checks/llvm-prefer-register-over-unsigned.rst =================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/llvm-prefer-register-over-unsigned.rst +++ clang-tools-extra/trunk/docs/clang-tidy/checks/llvm-prefer-register-over-unsigned.rst @@ -0,0 +1,27 @@ +.. title:: clang-tidy - llvm-prefer-register-over-unsigned + +llvm-prefer-register-over-unsigned +================================== + +Finds historical use of ``unsigned`` to hold vregs and physregs and rewrites +them to use ``Register``. + +Currently this works by finding all variables of unsigned integer type whose +initializer begins with an implicit cast from ``Register`` to ``unsigned``. + +.. code-block:: c++ + + void example(MachineOperand &MO) { + unsigned Reg = MO.getReg(); + ... + } + +becomes: + +.. code-block:: c++ + + void example(MachineOperand &MO) { + Register Reg = MO.getReg(); + ... + } + Index: clang-tools-extra/trunk/test/clang-tidy/llvm-prefer-register-over-unsigned.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-tidy/llvm-prefer-register-over-unsigned.cpp +++ clang-tools-extra/trunk/test/clang-tidy/llvm-prefer-register-over-unsigned.cpp @@ -0,0 +1,143 @@ +// RUN: %check_clang_tidy %s llvm-prefer-register-over-unsigned %t + +namespace llvm { +class Register { +public: + operator unsigned(); + + unsigned Reg; +}; + +// This class shouldn't trigger it despite the similarity. +class RegisterLike { +public: + operator unsigned(); + + unsigned Reg; +}; +} // end namespace llvm + +llvm::Register getReg(); +llvm::RegisterLike getRegLike(); + +void apply_1() { + unsigned Reg1 = getReg(); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'Reg1' declared as 'unsigned int'; use 'llvm::Register' instead [llvm-prefer-register-over-unsigned] + // CHECK-FIXES: apply_1() + // CHECK-FIXES-NEXT: llvm::Register Reg1 = getReg(); +} + +void apply_2() { + using namespace llvm; + unsigned Reg2 = getReg(); + // FIXME: Function-scoped UsingDirectiveDecl's don't appear to be in the + // DeclContext for the function so we can't elide the llvm:: in this + // case. Fortunately, it doesn't actually occur in the LLVM code base. + // CHECK-MESSAGES: :[[@LINE-4]]:12: warning: variable 'Reg2' declared as 'unsigned int'; use 'llvm::Register' instead [llvm-prefer-register-over-unsigned] + // CHECK-FIXES: apply_2() + // CHECK-FIXES-NEXT: using namespace llvm; + // CHECK-FIXES-NEXT: llvm::Register Reg2 = getReg(); +} + +namespace llvm { +void apply_3() { + unsigned Reg3 = getReg(); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'Reg3' declared as 'unsigned int'; use 'Register' instead [llvm-prefer-register-over-unsigned] + // CHECK-FIXES: apply_3() + // CHECK-FIXES-NEXT: Register Reg3 = getReg(); +} +} // end namespace llvm + +void done_1() { + llvm::Register Reg1 = getReg(); + // CHECK-FIXES: done_1() + // CHECK-FIXES-NEXT: llvm::Register Reg1 = getReg(); +} + +void done_2() { + using namespace llvm; + Register Reg2 = getReg(); + // CHECK-FIXES: done_2() + // CHECK-FIXES-NEXT: using namespace llvm; + // CHECK-FIXES-NEXT: Register Reg2 = getReg(); +} + +namespace llvm { +void done_3() { + Register Reg3 = getReg(); + // CHECK-FIXES: done_3() + // CHECK-FIXES-NEXT: Register Reg3 = getReg(); +} +} // end namespace llvm + +void do_nothing_1() { + unsigned Reg1 = getRegLike(); + // CHECK-FIXES: do_nothing_1() + // CHECK-FIXES-NEXT: unsigned Reg1 = getRegLike(); +} + +void do_nothing_2() { + using namespace llvm; + unsigned Reg2 = getRegLike(); + // CHECK-FIXES: do_nothing_2() + // CHECK-FIXES-NEXT: using namespace llvm; + // CHECK-FIXES-NEXT: unsigned Reg2 = getRegLike(); +} + +namespace llvm { +void do_nothing_3() { + unsigned Reg3 = getRegLike(); + // CHECK-FIXES: do_nothing_3() + // CHECK-FIXES-NEXT: unsigned Reg3 = getRegLike(); +} +} // end namespace llvm + +void fn1(llvm::Register R); +void do_nothing_4() { + fn1(getReg()); + // CHECK-FIXES: do_nothing_4() + // CHECK-FIXES-NEXT: fn1(getReg()); +} + +void fn2(unsigned R); +void do_nothing_5() { + fn2(getReg()); + // CHECK-FIXES: do_nothing_5() + // CHECK-FIXES-NEXT: fn2(getReg()); +} + +void do_nothing_6() { + using namespace llvm; + Register Reg6{getReg()}; + // CHECK-FIXES: do_nothing_6() + // CHECK-FIXES-NEXT: using namespace llvm; + // CHECK-FIXES-NEXT: Register Reg6{getReg()}; +} + +void do_nothing_7() { + using namespace llvm; + Register Reg7; + Reg7.Reg = getReg(); + // CHECK-FIXES: do_nothing_7() + // CHECK-FIXES-NEXT: using namespace llvm; + // CHECK-FIXES-NEXT: Register Reg7; + // CHECK-FIXES-NEXT: Reg7.Reg = getReg(); +} + +void do_nothing_8() { + using namespace llvm; + RegisterLike Reg8{getReg()}; + // CHECK-FIXES: do_nothing_8() + // CHECK-FIXES-NEXT: using namespace llvm; + // CHECK-FIXES-NEXT: RegisterLike Reg8{getReg()}; +} + +void do_nothing_9() { + using namespace llvm; + RegisterLike Reg9; + Reg9.Reg = getReg(); + // CHECK-FIXES: do_nothing_9() + // CHECK-FIXES-NEXT: using namespace llvm; + // CHECK-FIXES-NEXT: RegisterLike Reg9; + // CHECK-FIXES-NEXT: Reg9.Reg = getReg(); +} Index: clang-tools-extra/trunk/test/clang-tidy/llvm-prefer-register-over-unsigned2.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-tidy/llvm-prefer-register-over-unsigned2.cpp +++ clang-tools-extra/trunk/test/clang-tidy/llvm-prefer-register-over-unsigned2.cpp @@ -0,0 +1,25 @@ +// RUN: %check_clang_tidy %s llvm-prefer-register-over-unsigned %t + +namespace llvm { +class Register { +public: + operator unsigned(); +}; +} // end namespace llvm + +llvm::Register getReg(); + +using namespace llvm; + +void apply_1() { + unsigned Reg = getReg(); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'Reg' declared as 'unsigned int'; use 'Register' instead [llvm-prefer-register-over-unsigned] + // CHECK-FIXES: apply_1() + // CHECK-FIXES-NEXT: Register Reg = getReg(); +} + +void done_1() { + llvm::Register Reg = getReg(); + // CHECK-FIXES: done_1() + // CHECK-FIXES-NEXT: llvm::Register Reg = getReg(); +} Index: clang-tools-extra/trunk/test/clang-tidy/llvm-prefer-register-over-unsigned3.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-tidy/llvm-prefer-register-over-unsigned3.cpp +++ clang-tools-extra/trunk/test/clang-tidy/llvm-prefer-register-over-unsigned3.cpp @@ -0,0 +1,33 @@ +// RUN: %check_clang_tidy %s llvm-prefer-register-over-unsigned %t + +namespace llvm { }; + +// This class shouldn't trigger it despite the similarity as it's not inside the llvm namespace +class Register { +public: + operator unsigned(); +}; + +Register getReg(); + +void do_nothing_1() { + unsigned Reg1 = getReg(); + // CHECK-FIXES: do_nothing_1() + // CHECK-FIXES-NEXT: unsigned Reg1 = getReg(); +} + +void do_nothing_2() { + using namespace llvm; + unsigned Reg2 = getReg(); + // CHECK-FIXES: do_nothing_2() + // CHECK-FIXES-NEXT: using namespace llvm; + // CHECK-FIXES-NEXT: unsigned Reg2 = getReg(); +} + +namespace llvm { +void do_nothing_3() { + unsigned Reg3 = getReg(); + // CHECK-FIXES: do_nothing_3() + // CHECK-FIXES-NEXT: unsigned Reg3 = getReg(); +} +} // end namespace llvm