Hi alexfh, djasper,
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.
http://reviews.llvm.org/D4497
Files:
clang-tidy/google/CMakeLists.txt
clang-tidy/google/ExplicitMakePairCheck.cpp
clang-tidy/google/ExplicitMakePairCheck.h
clang-tidy/google/GoogleTidyModule.cpp
test/clang-tidy/google-explicit-make-pair.cpp
Index: clang-tidy/google/CMakeLists.txt
===================================================================
--- clang-tidy/google/CMakeLists.txt
+++ 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-tidy/google/ExplicitMakePairCheck.cpp
===================================================================
--- /dev/null
+++ clang-tidy/google/ExplicitMakePairCheck.cpp
@@ -0,0 +1,62 @@
+//===--- 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) {
+ Finder->addMatcher(
+ callExpr(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");
+
+ 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-tidy/google/ExplicitMakePairCheck.h
===================================================================
--- /dev/null
+++ clang-tidy/google/ExplicitMakePairCheck.h
@@ -0,0 +1,35 @@
+//===--- AvoidCStyleCastsCheck.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-tidy/google/GoogleTidyModule.cpp
===================================================================
--- clang-tidy/google/GoogleTidyModule.cpp
+++ 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<build::ExplicitMakePairCheck>());
+ CheckFactories.addCheckFactory(
"google-explicit-constructor",
new ClangTidyCheckFactory<ExplicitConstructorCheck>());
CheckFactories.addCheckFactory(
Index: test/clang-tidy/google-explicit-make-pair.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/google-explicit-make-pair.cpp
@@ -0,0 +1,30 @@
+// 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);
+}
+}
+
+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)
+
+ std::make_pair(i, 1); // no-warning
+}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits