Skip to content

Commit a249084

Browse files
committed
Fix FP
1 parent 34dc72a commit a249084

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

clang-tools-extra/clang-tidy/misc/ShadowedNamespaceFunctionCheck.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "clang/ASTMatchers/ASTMatchers.h"
1313
#include "clang/AST/Decl.h"
1414
#include "clang/AST/DeclCXX.h"
15+
#include "llvm/ADT/STLExtras.h"
1516

1617
using namespace clang;
1718
using namespace clang::ast_matchers;
@@ -21,6 +22,18 @@ namespace clang {
2122
namespace tidy {
2223
namespace misc {
2324

25+
static bool hasSameParameters(const FunctionDecl *Func1, const FunctionDecl *Func2) {
26+
if (Func1->param_size() != Func2->param_size())
27+
return false;
28+
29+
return llvm::all_of_zip(
30+
Func1->parameters(), Func2->parameters(),
31+
[](const ParmVarDecl *Param1, const ParmVarDecl *Param2) {
32+
return Param1->getType().getCanonicalType() ==
33+
Param2->getType().getCanonicalType();
34+
});
35+
}
36+
2437
void ShadowedNamespaceFunctionCheck::registerMatchers(MatchFinder *Finder) {
2538
// Simple matcher for all function definitions
2639
Finder->addMatcher(
@@ -45,7 +58,7 @@ void ShadowedNamespaceFunctionCheck::check(
4558

4659
// Skip templates, static functions, main, etc.
4760
if (Func->isTemplated() || Func->isStatic() ||
48-
Func->isMain() || Func->isImplicit())
61+
Func->isMain() || Func->isImplicit() || Func->isVariadic())
4962
return;
5063

5164
const std::string FuncName = Func->getNameAsString();
@@ -117,7 +130,7 @@ std::pair<const FunctionDecl *, const NamespaceDecl *> ShadowedNamespaceFunction
117130
Func->isThisDeclarationADefinition())
118131
continue;
119132

120-
if (Func->getNameAsString() == GlobalFuncName) {
133+
if (Func->getNameAsString() == GlobalFuncName && !Func->isVariadic() && hasSameParameters(Func, GlobalFunc) && Func->getReturnType().getCanonicalType() == GlobalFunc->getReturnType().getCanonicalType()) {
121134
return {Func, NS};
122135
}
123136
}

clang-tools-extra/docs/clang-tidy/checks/misc/shadowed-namespace-function.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ Limitations
4242
- Does not warn about functions in anonymous namespaces
4343
- Does not warn about template functions
4444
- Does not warn about static functions or member functions
45+
- Does not warn about variadic functions
4546
- Does not warn about the ``main`` function
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %check_clang_tidy %s misc-shadowed-namespace-function %t
2+
3+
void f1(int);
4+
namespace foo {
5+
void f0(short);
6+
void f1(unsigned);
7+
}
8+
void f0(char) {}
9+
void f1(int) {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %check_clang_tidy %s misc-shadowed-namespace-function %t
2+
3+
int f1();
4+
namespace foo {
5+
char f0();
6+
unsigned f1();
7+
}
8+
short f0() { return {}; }
9+
int f1() { return {}; }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %check_clang_tidy %s misc-shadowed-namespace-function %t
2+
3+
using my_int = int;
4+
using my_short = short;
5+
using my_short2 = short;
6+
7+
my_int f1(my_short2);
8+
namespace foo {
9+
int f0(short);
10+
int f1(short);
11+
}
12+
my_int f0(my_short) {}
13+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: free function 'f0' shadows 'foo::f0' [misc-shadowed-namespace-function]
14+
// CHECK-FIXES: my_int foo::f0(my_short) {}
15+
my_int f1(my_short) {}
16+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: free function 'f1' shadows 'foo::f1' [misc-shadowed-namespace-function]
17+
// CHECK-MESSAGES-NOT: :[[@LINE-2]]:{{.*}}: note: FIX-IT applied suggested code changes
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %check_clang_tidy %s misc-shadowed-namespace-function %t
2+
3+
void f1(...);
4+
namespace foo {
5+
void f0(...);
6+
void f1(...);
7+
}
8+
void f0(...) {}
9+
void f1(...) {}

0 commit comments

Comments
 (0)