Index: clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt =================================================================== --- clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt +++ clang-tools-extra/trunk/clang-tidy/google/CMakeLists.txt @@ -3,6 +3,7 @@ add_clang_library(clangTidyGoogleModule AvoidCStyleCastsCheck.cpp ExplicitConstructorCheck.cpp + ExplicitMakePairCheck.cpp GoogleTidyModule.cpp LINK_LIBS Index: clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.h =================================================================== --- clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.h +++ clang-tools-extra/trunk/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 Index: clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.cpp +++ clang-tools-extra/trunk/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 Index: clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp +++ clang-tools-extra/trunk/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 @@ public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.addCheckFactory( + "google-build-explicit-make-pair", + new ClangTidyCheckFactory()); + CheckFactories.addCheckFactory( "google-explicit-constructor", new ClangTidyCheckFactory()); CheckFactories.addCheckFactory( Index: clang-tools-extra/trunk/test/clang-tidy/google-explicit-make-pair.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-tidy/google-explicit-make-pair.cpp +++ clang-tools-extra/trunk/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 +}