From 2a3498e24f97d85ddd2bb25467ff6fc1f6baa5f1 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Wed, 6 May 2020 17:06:14 +0200 Subject: [PATCH] [clang-tidy] Exclude function calls in std namespace for bugprone-argument-comment. Reviewers: gribozavr2 Subscribers: xazax.hun, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D79494 --- .../bugprone/ArgumentCommentCheck.cpp | 19 +++++++++++++++++-- .../checkers/bugprone-argument-comment.cpp | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp index 5ec4c779d59a..3e401dac8a59 100644 --- a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp @@ -19,6 +19,13 @@ using namespace clang::ast_matchers; namespace clang { namespace tidy { namespace bugprone { +namespace { +AST_MATCHER(Decl, isFromStdNamespace) { + if (const auto *D = Node.getDeclContext()->getEnclosingNamespaceContext()) + return D->isStdNamespace(); + return false; +} +} // namespace ArgumentCommentCheck::ArgumentCommentCheck(StringRef Name, ClangTidyContext *Context) @@ -54,10 +61,18 @@ void ArgumentCommentCheck::registerMatchers(MatchFinder *Finder) { // don't check them against NewCallback's parameter names. // FIXME: Make this configurable. unless(hasDeclaration(functionDecl( - hasAnyName("NewCallback", "NewPermanentCallback"))))) + hasAnyName("NewCallback", "NewPermanentCallback")))), + // Ignore APIs from the standard library, since their names are + // not specified by the standard, and standard library + // implementations in practice have to use reserved names to + // avoid conflicts with same-named macros. + unless(hasDeclaration(isFromStdNamespace()))) + .bind("expr"), + this); + Finder->addMatcher( + cxxConstructExpr(unless(hasDeclaration(isFromStdNamespace()))) .bind("expr"), this); - Finder->addMatcher(cxxConstructExpr().bind("expr"), this); } static std::vector> diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-argument-comment.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone-argument-comment.cpp index 8a4d9e5b644c..8a6fe097a55d 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-argument-comment.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-argument-comment.cpp @@ -115,3 +115,22 @@ void g() { f6(/*xxy=*/0, 0); } // CHECK-NOTES: [[@LINE-3]]:13: note: 'xxx' declared here // CHECK-FIXES: void g() { f6(/*xxy=*/0, 0); } } + + +namespace std { +template +class vector { +public: + void assign(int __n, const T &__val); +}; +template +void swap(T& __a, T& __b); +} // namespace std +namespace ignore_std_functions { +void test(int a, int b) { + std::vector s; + // verify the check is not fired on std functions. + s.assign(1, /*value=*/2); + std::swap(a, /*num=*/b); +} +} // namespace ignore_std_functions