From 47c4d101e00953aefb071625c68f3d9ab8373e4a Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Tue, 15 Jul 2014 09:50:32 +0000 Subject: [PATCH] [clang-tidy] Add a checker that removes deducible arguments from std::make_pair Those may be incompatible with C++11 and are unnecessary. We suggest removing the template arguments when they match the types of the make_pair arguments or replace it with std::pair and explicit template arguments when not. Differential Revision: http://reviews.llvm.org/D4497 llvm-svn: 213058 --- clang-tools-extra/clang-tidy/google/CMakeLists.txt | 1 + .../clang-tidy/google/ExplicitMakePairCheck.cpp | 71 ++++++++++++++++++++++ .../clang-tidy/google/ExplicitMakePairCheck.h | 35 +++++++++++ .../clang-tidy/google/GoogleTidyModule.cpp | 4 ++ .../test/clang-tidy/google-explicit-make-pair.cpp | 44 ++++++++++++++ 5 files changed, 155 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/google/ExplicitMakePairCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/google/ExplicitMakePairCheck.h create mode 100644 clang-tools-extra/test/clang-tidy/google-explicit-make-pair.cpp diff --git a/clang-tools-extra/clang-tidy/google/CMakeLists.txt b/clang-tools-extra/clang-tidy/google/CMakeLists.txt index ad2b320..9299fad 100644 --- a/clang-tools-extra/clang-tidy/google/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/google/CMakeLists.txt @@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyGoogleModule AvoidCStyleCastsCheck.cpp ExplicitConstructorCheck.cpp + ExplicitMakePairCheck.cpp GoogleTidyModule.cpp LINK_LIBS diff --git a/clang-tools-extra/clang-tidy/google/ExplicitMakePairCheck.cpp b/clang-tools-extra/clang-tidy/google/ExplicitMakePairCheck.cpp new file mode 100644 index 0000000..ae5fbd6 --- /dev/null +++ b/clang-tools-extra/clang-tidy/google/ExplicitMakePairCheck.cpp @@ -0,0 +1,71 @@ +//===--- ExplicitMakePairCheck.cpp - clang-tidy -----------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "ExplicitMakePairCheck.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/AST/ASTContext.h" + +using namespace clang::ast_matchers; + +namespace clang { + +namespace ast_matchers { +AST_MATCHER(DeclRefExpr, hasExplicitTemplateArgs) { + return Node.hasExplicitTemplateArgs(); +} +} // namespace ast_matchers + +namespace tidy { +namespace build { + +void +ExplicitMakePairCheck::registerMatchers(ast_matchers::MatchFinder *Finder) { + // Look for std::make_pair with explicit template args. Ignore calls in + // templates. + Finder->addMatcher( + callExpr(unless(hasAncestor(decl(anyOf( + recordDecl(ast_matchers::isTemplateInstantiation()), + functionDecl(ast_matchers::isTemplateInstantiation()))))), + has(declRefExpr(hasExplicitTemplateArgs()).bind("declref")), + callee(functionDecl(hasName("::std::make_pair")))).bind("call"), + this); +} + +void ExplicitMakePairCheck::check(const MatchFinder::MatchResult &Result) { + const auto *Call = Result.Nodes.getNodeAs("call"); + const auto *DeclRef = Result.Nodes.getNodeAs("declref"); + + // Sanity check: The use might have overriden ::std::make_pair. + if (Call->getNumArgs() != 2) + return; + + const Expr *Arg0 = Call->getArg(0)->IgnoreParenImpCasts(); + const Expr *Arg1 = Call->getArg(1)->IgnoreParenImpCasts(); + + // If types don't match, we suggest replacing with std::pair and explicit + // template arguments. Otherwise just remove the template arguments from + // make_pair. + if (Arg0->getType() != Call->getArg(0)->getType() || + Arg1->getType() != Call->getArg(1)->getType()) { + diag(Call->getLocStart(), "for C++11-compatibility, use pair directly") + << FixItHint::CreateReplacement( + SourceRange(DeclRef->getLocStart(), DeclRef->getLAngleLoc()), + "std::pair<"); + } else { + diag(Call->getLocStart(), + "for C++11-compatibility, omit template arguments from make_pair") + << FixItHint::CreateRemoval( + SourceRange(DeclRef->getLAngleLoc(), DeclRef->getRAngleLoc())); + } +} + +} // namespace build +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/google/ExplicitMakePairCheck.h b/clang-tools-extra/clang-tidy/google/ExplicitMakePairCheck.h new file mode 100644 index 0000000..5ccd610 --- /dev/null +++ b/clang-tools-extra/clang-tidy/google/ExplicitMakePairCheck.h @@ -0,0 +1,35 @@ +//===--- ExplicitMakePairCheck.h - clang-tidy -------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_EXPLICIT_MAKE_PAIR_CHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_EXPLICIT_MAKE_PAIR_CHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace build { + +/// \brief Check that make_pair's template arguments are deduced. +/// +/// G++ 4.6 in C++11 mode fails badly if make_pair's template arguments are +/// specified explicitly, and such use isn't intended in any case. +/// +/// Corresponding cpplint.py check name: 'build/explicit_make_pair'. +class ExplicitMakePairCheck : public ClangTidyCheck { +public: + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace build +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_EXPLICIT_MAKE_PAIR_CHECK_H diff --git a/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp b/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp index 34abfb3..248d0f1 100644 --- a/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp @@ -12,6 +12,7 @@ #include "../ClangTidyModuleRegistry.h" #include "AvoidCStyleCastsCheck.h" #include "ExplicitConstructorCheck.h" +#include "ExplicitMakePairCheck.h" using namespace clang::ast_matchers; @@ -22,6 +23,9 @@ class GoogleModule : public ClangTidyModule { public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.addCheckFactory( + "google-build-explicit-make-pair", + new ClangTidyCheckFactory()); + CheckFactories.addCheckFactory( "google-explicit-constructor", new ClangTidyCheckFactory()); CheckFactories.addCheckFactory( diff --git a/clang-tools-extra/test/clang-tidy/google-explicit-make-pair.cpp b/clang-tools-extra/test/clang-tidy/google-explicit-make-pair.cpp new file mode 100644 index 0000000..257fd06 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/google-explicit-make-pair.cpp @@ -0,0 +1,44 @@ +// RUN: $(dirname %s)/check_clang_tidy_fix.sh %s google-build-explicit-make-pair %t +// REQUIRES: shell + +namespace std { +template +struct pair { + pair(T1 x, T2 y) {} +}; + +template +pair make_pair(T1 x, T2 y) { + return pair(x, y); +} +} + +template +void templ(T a, T b) { + std::make_pair(a, b); +} + +void test(int i) { + std::make_pair(i, i); +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: for C++11-compatibility, omit template arguments from make_pair +// CHECK-FIXES: std::make_pair(i, i) + + std::make_pair(i, i); +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: for C++11-compatibility, use pair directly +// CHECK-FIXES: std::pair(i, i) + + std::make_pair(i, i); +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: for C++11-compatibility, use pair directly +// CHECK-FIXES: std::pair(i, i) + +#define M std::make_pair(i, i); +M +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: for C++11-compatibility, use pair directly +// Can't fix in macros. +// CHECK-FIXES: #define M std::make_pair(i, i); +// CHECK-FIXES-NEXT: M + + templ(i, i); + + std::make_pair(i, 1); // no-warning +} -- 2.7.4