[clang-tools-extra] r213509 - [clang-tidy] Fix a false positive in the make_pair checker if an argument has a explicit template argument.

Benjamin Kramer benny.kra at googlemail.com
Mon Jul 21 02:40:52 PDT 2014


Author: d0k
Date: Mon Jul 21 04:40:52 2014
New Revision: 213509

URL: http://llvm.org/viewvc/llvm-project?rev=213509&view=rev
Log:
[clang-tidy] Fix a false positive in the make_pair checker if an argument has a explicit template argument.

This required a rather ugly workaround for a problem in ASTMatchers where
callee() is only overloaded for Stmt and Decl but not for Expr.

Modified:
    clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/google-explicit-make-pair.cpp

Modified: 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=213509&r1=213508&r2=213509&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/google/ExplicitMakePairCheck.cpp Mon Jul 21 04:40:52 2014
@@ -20,6 +20,15 @@ namespace ast_matchers {
 AST_MATCHER(DeclRefExpr, hasExplicitTemplateArgs) {
   return Node.hasExplicitTemplateArgs();
 }
+
+// FIXME: This should just be callee(ignoringImpCasts()) but it's not overloaded
+// for Expr.
+AST_MATCHER_P(CallExpr, calleeIgnoringParenImpCasts, internal::Matcher<Stmt>,
+              InnerMatcher) {
+  const Expr *ExprNode = Node.getCallee();
+  return (ExprNode != nullptr &&
+          InnerMatcher.matches(*ExprNode->IgnoreParenImpCasts(), Finder, Builder));
+}
 } // namespace ast_matchers
 
 namespace tidy {
@@ -33,8 +42,10 @@ ExplicitMakePairCheck::registerMatchers(
       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"),
+               calleeIgnoringParenImpCasts(
+                   declRefExpr(hasExplicitTemplateArgs(),
+                               to(functionDecl(hasName("::std::make_pair"))))
+                       .bind("declref"))).bind("call"),
       this);
 }
 

Modified: 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=213509&r1=213508&r2=213509&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/google-explicit-make-pair.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/google-explicit-make-pair.cpp Mon Jul 21 04:40:52 2014
@@ -21,6 +21,9 @@ void templ(T a, T b) {
 // CHECK-FIXES: std::make_pair(1, 2)
 }
 
+template <typename T>
+int t();
+
 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
@@ -45,4 +48,5 @@ M
   templ(1U, 2U);
 
   std::make_pair(i, 1); // no-warning
+  std::make_pair(t<int>, 1);
 }





More information about the cfe-commits mailing list