Skip to content

Commit 2a3498e

Browse files
committed
[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
1 parent a400aa5 commit 2a3498e

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ using namespace clang::ast_matchers;
1919
namespace clang {
2020
namespace tidy {
2121
namespace bugprone {
22+
namespace {
23+
AST_MATCHER(Decl, isFromStdNamespace) {
24+
if (const auto *D = Node.getDeclContext()->getEnclosingNamespaceContext())
25+
return D->isStdNamespace();
26+
return false;
27+
}
28+
} // namespace
2229

2330
ArgumentCommentCheck::ArgumentCommentCheck(StringRef Name,
2431
ClangTidyContext *Context)
@@ -54,10 +61,18 @@ void ArgumentCommentCheck::registerMatchers(MatchFinder *Finder) {
5461
// don't check them against NewCallback's parameter names.
5562
// FIXME: Make this configurable.
5663
unless(hasDeclaration(functionDecl(
57-
hasAnyName("NewCallback", "NewPermanentCallback")))))
64+
hasAnyName("NewCallback", "NewPermanentCallback")))),
65+
// Ignore APIs from the standard library, since their names are
66+
// not specified by the standard, and standard library
67+
// implementations in practice have to use reserved names to
68+
// avoid conflicts with same-named macros.
69+
unless(hasDeclaration(isFromStdNamespace())))
70+
.bind("expr"),
71+
this);
72+
Finder->addMatcher(
73+
cxxConstructExpr(unless(hasDeclaration(isFromStdNamespace())))
5874
.bind("expr"),
5975
this);
60-
Finder->addMatcher(cxxConstructExpr().bind("expr"), this);
6176
}
6277

6378
static std::vector<std::pair<SourceLocation, StringRef>>

clang-tools-extra/test/clang-tidy/checkers/bugprone-argument-comment.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,22 @@ void g() { f6(/*xxy=*/0, 0); }
115115
// CHECK-NOTES: [[@LINE-3]]:13: note: 'xxx' declared here
116116
// CHECK-FIXES: void g() { f6(/*xxy=*/0, 0); }
117117
}
118+
119+
120+
namespace std {
121+
template <typename T>
122+
class vector {
123+
public:
124+
void assign(int __n, const T &__val);
125+
};
126+
template<typename T>
127+
void swap(T& __a, T& __b);
128+
} // namespace std
129+
namespace ignore_std_functions {
130+
void test(int a, int b) {
131+
std::vector<int> s;
132+
// verify the check is not fired on std functions.
133+
s.assign(1, /*value=*/2);
134+
std::swap(a, /*num=*/b);
135+
}
136+
} // namespace ignore_std_functions

0 commit comments

Comments
 (0)