Author: d0k Date: Tue Jul 15 04:50:32 2014 New Revision: 213058 URL: http://llvm.org/viewvc/llvm-project?rev=213058&view=rev Log: [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 Added: clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.cpp clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.h clang-tools-extra/trunk/test/clang-tidy/google-explicit-make-pair.cpp Modified: clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp Modified: clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt?rev=213058&r1=213057&r2=213058&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt (original) +++ clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt Tue Jul 15 04:50:32 2014 @@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyGoogleModule AvoidCStyleCastsCheck.cpp ExplicitConstructorCheck.cpp + ExplicitMakePairCheck.cpp GoogleTidyModule.cpp LINK_LIBS Added: clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.cpp?rev=213058&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.cpp (added) +++ clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.cpp Tue Jul 15 04:50:32 2014 @@ -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<CallExpr>("call"); + const auto *DeclRef = Result.Nodes.getNodeAs<DeclRefExpr>("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 Added: clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.h?rev=213058&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.h (added) +++ clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.h Tue Jul 15 04:50:32 2014 @@ -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 Modified: clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp?rev=213058&r1=213057&r2=213058&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp Tue Jul 15 04:50:32 2014 @@ -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 ClangTidyMod public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.addCheckFactory( + "google-build-explicit-make-pair", + new ClangTidyCheckFactory<build::ExplicitMakePairCheck>()); + CheckFactories.addCheckFactory( "google-explicit-constructor", new ClangTidyCheckFactory<ExplicitConstructorCheck>()); CheckFactories.addCheckFactory( Added: clang-tools-extra/trunk/test/clang-tidy/google-explicit-make-pair.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/google-explicit-make-pair.cpp?rev=213058&view=auto ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/google-explicit-make-pair.cpp (added) +++ clang-tools-extra/trunk/test/clang-tidy/google-explicit-make-pair.cpp Tue Jul 15 04:50:32 2014 @@ -0,0 +1,44 @@ +// RUN: $(dirname %s)/check_clang_tidy_fix.sh %s google-build-explicit-make-pair %t +// REQUIRES: shell + +namespace std { +template <class T1, class T2> +struct pair { + pair(T1 x, T2 y) {} +}; + +template <class T1, class T2> +pair<T1, T2> make_pair(T1 x, T2 y) { + return pair<T1, T2>(x, y); +} +} + +template <typename T> +void templ(T a, T b) { + std::make_pair<T, unsigned>(a, b); +} + +void test(int i) { + std::make_pair<int, int>(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<unsigned, int>(i, i); +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: for C++11-compatibility, use pair directly +// CHECK-FIXES: std::pair<unsigned, int>(i, i) + + std::make_pair<int, unsigned>(i, i); +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: for C++11-compatibility, use pair directly +// CHECK-FIXES: std::pair<int, unsigned>(i, i) + +#define M std::make_pair<int, unsigned>(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<int, unsigned>(i, i); +// CHECK-FIXES-NEXT: M + + templ(i, i); + + std::make_pair(i, 1); // no-warning +} _______________________________________________ cfe-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
