Skip to content

Commit 2e75986

Browse files
committed
bugprone-argument-comment: ignore mismatches from system headers
As of 2a3498e, we ignore parameter name mismatches for functions in the `std::` namespace, since those aren't standardized. It seems reasonable to extend this to all functions which are declared in system headers, since this lint can be a bit noisy otherwise (https://bugs.chromium.org/p/chromium/issues/detail?id=1191507). Differential Revision: https://reviews.llvm.org/D99993
1 parent 0d74bd3 commit 2e75986

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ namespace clang {
2020
namespace tidy {
2121
namespace bugprone {
2222
namespace {
23-
AST_MATCHER(Decl, isFromStdNamespace) {
23+
AST_MATCHER(Decl, isFromStdNamespaceOrSystemHeader) {
2424
if (const auto *D = Node.getDeclContext()->getEnclosingNamespaceContext())
25-
return D->isStdNamespace();
26-
return false;
25+
if (D->isStdNamespace())
26+
return true;
27+
return Node.getASTContext().getSourceManager().isInSystemHeader(
28+
Node.getLocation());
2729
}
2830
} // namespace
2931

@@ -66,13 +68,13 @@ void ArgumentCommentCheck::registerMatchers(MatchFinder *Finder) {
6668
// not specified by the standard, and standard library
6769
// implementations in practice have to use reserved names to
6870
// avoid conflicts with same-named macros.
69-
unless(hasDeclaration(isFromStdNamespace())))
70-
.bind("expr"),
71-
this);
72-
Finder->addMatcher(
73-
cxxConstructExpr(unless(hasDeclaration(isFromStdNamespace())))
71+
unless(hasDeclaration(isFromStdNamespaceOrSystemHeader())))
7472
.bind("expr"),
7573
this);
74+
Finder->addMatcher(cxxConstructExpr(unless(hasDeclaration(
75+
isFromStdNamespaceOrSystemHeader())))
76+
.bind("expr"),
77+
this);
7678
}
7779

7880
static std::vector<std::pair<SourceLocation, StringRef>>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void my_header_function(int arg);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma clang system_header
2+
3+
void my_system_header_function(int arg);

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %check_clang_tidy %s bugprone-argument-comment %t
1+
// RUN: %check_clang_tidy %s bugprone-argument-comment %t -- -- -I %S/Inputs/bugprone-argument-comment
22

33
// FIXME: clang-tidy should provide a -verify mode to make writing these checks
44
// easier and more accurate.
@@ -134,3 +134,20 @@ void test(int a, int b) {
134134
std::swap(a, /*num=*/b);
135135
}
136136
} // namespace ignore_std_functions
137+
138+
namespace regular_header {
139+
#include "header-with-decl.h"
140+
void test() {
141+
my_header_function(/*not_arg=*/1);
142+
// CHECK-NOTES: [[@LINE-1]]:22: warning: argument name 'not_arg' in comment does not match parameter name 'arg'
143+
// CHECK-NOTES: header-with-decl.h:1:29: note: 'arg' declared here
144+
// CHECK-FIXES: my_header_function(/*not_arg=*/1);
145+
}
146+
} // namespace regular_header
147+
148+
namespace system_header {
149+
#include "system-header-with-decl.h"
150+
void test() {
151+
my_system_header_function(/*not_arg=*/1);
152+
}
153+
} // namespace system_header

0 commit comments

Comments
 (0)