Skip to content

Commit e9a1b62

Browse files
committed
merge tests file into one and fix fp with templated method
1 parent d9ecaed commit e9a1b62

File tree

5 files changed

+78
-145
lines changed

5 files changed

+78
-145
lines changed

clang-tools-extra/clang-tidy/llvm/PreferStaticOverAnonymousNamespaceCheck.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ void PreferStaticOverAnonymousNamespaceCheck::registerMatchers(
4545
unless(isInMacro()), isDefinition());
4646

4747
if (AllowMemberFunctionsInClass) {
48-
Finder->addMatcher(functionDecl(IsDefinitionInAnonymousNamespace,
49-
unless(hasParent(cxxRecordDecl())))
50-
.bind("function"),
51-
this);
48+
Finder->addMatcher(
49+
functionDecl(IsDefinitionInAnonymousNamespace,
50+
unless(anyOf(hasParent(cxxRecordDecl()),
51+
hasParent(functionTemplateDecl(
52+
hasParent(cxxRecordDecl()))))))
53+
.bind("function"),
54+
this);
5255
} else
5356
Finder->addMatcher(
5457
functionDecl(IsDefinitionInAnonymousNamespace).bind("function"), this);

clang-tools-extra/docs/clang-tidy/checks/llvm/prefer-static-over-anonymous-namespace.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ suggests replacing them with ``static`` declarations.
88

99
The `LLVM Coding Standards <https://llvm.org/docs/CodingStandards.html#restrict-visibility>`_
1010
recommend keeping anonymous namespaces as small as possible and only use them
11-
for class declarations. For functions and variables, ``static`` specifier
11+
for class declarations. For functions and variables the ``static`` specifier
1212
should be preferred for restricting visibility.
1313

1414
For example non-compliant code:

clang-tools-extra/test/clang-tidy/checkers/llvm/prefer-static-over-anonymous-namespace-allow-member-functions.cpp

Lines changed: 0 additions & 65 deletions
This file was deleted.

clang-tools-extra/test/clang-tidy/checkers/llvm/prefer-static-over-anonymous-namespace-allow-variables.cpp

Lines changed: 0 additions & 60 deletions
This file was deleted.

clang-tools-extra/test/clang-tidy/checkers/llvm/prefer-static-over-anonymous-namespace.cpp

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,82 @@
11
// RUN: %check_clang_tidy %s llvm-prefer-static-over-anonymous-namespace %t -- -- -fno-delayed-template-parsing
2+
// RUN: %check_clang_tidy -check-suffixes=,VAR %s llvm-prefer-static-over-anonymous-namespace %t -- \
3+
// RUN: -config="{CheckOptions: { \
4+
// RUN: llvm-prefer-static-over-anonymous-namespace.AllowVariableDeclarations: false }, \
5+
// RUN: }" -- -fno-delayed-template-parsing
6+
// RUN: %check_clang_tidy -check-suffixes=,MEM %s llvm-prefer-static-over-anonymous-namespace %t -- \
7+
// RUN: -config="{CheckOptions: { \
8+
// RUN: llvm-prefer-static-over-anonymous-namespace.AllowMemberFunctionsInClass: false }, \
9+
// RUN: }" -- -fno-delayed-template-parsing
210

311
namespace {
412

5-
void regularFunction() {}
13+
void regularFunction() {
614
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'regularFunction' is declared in an anonymous namespace; prefer using 'static' for restricting visibility [llvm-prefer-static-over-anonymous-namespace]
715

16+
int Variable = 42;
17+
auto Lambda = []() { return 42; };
18+
static int StaticVariable = 42;
19+
}
20+
821
void declaredFunction();
922

1023
static void staticFunction() {}
1124
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: place static function 'staticFunction' outside of an anonymous namespace
1225

1326
int globalVariable = 42;
27+
// CHECK-MESSAGES-VAR: :[[@LINE-1]]:5: warning: variable 'globalVariable' is declared in an anonymous namespace;
1428

1529
static int staticVariable = 42;
30+
// CHECK-MESSAGES-VAR: :[[@LINE-1]]:12: warning: place static variable 'staticVariable' outside of an anonymous namespace
31+
32+
typedef int MyInt;
33+
const MyInt myGlobalVariable = 42;
34+
// CHECK-MESSAGES-VAR: :[[@LINE-1]]:13: warning: variable 'myGlobalVariable' is declared in an anonymous namespace;
35+
36+
template<typename T>
37+
constexpr T Pi = T(3.1415926);
38+
// CHECK-MESSAGES-VAR: :[[@LINE-1]]:13: warning: variable 'Pi' is declared in an anonymous namespace;
39+
40+
void (*funcPtr)() = nullptr;
41+
// CHECK-MESSAGES-VAR: :[[@LINE-1]]:8: warning: variable 'funcPtr' is declared in an anonymous namespace;
42+
43+
auto lambda = []() { return 42; };
44+
// CHECK-MESSAGES-VAR: :[[@LINE-1]]:6: warning: variable 'lambda' is declared in an anonymous namespace;
45+
46+
class InstanceClass {
47+
int member;
48+
};
49+
50+
InstanceClass instance;
51+
// CHECK-MESSAGES-VAR: :[[@LINE-1]]:15: warning: variable 'instance' is declared in an anonymous namespace;
52+
53+
InstanceClass* instancePtr = nullptr;
54+
// CHECK-MESSAGES-VAR: :[[@LINE-1]]:16: warning: variable 'instancePtr' is declared in an anonymous namespace;
55+
56+
InstanceClass& instanceRef = instance;
57+
// CHECK-MESSAGES-VAR: :[[@LINE-1]]:16: warning: variable 'instanceRef' is declared in an anonymous namespace;
1658

1759
class MyClass {
1860
public:
1961
MyClass();
2062
MyClass(const MyClass&) {}
63+
// CHECK-MESSAGES-MEM: :[[@LINE-1]]:3: warning: place definition of method 'MyClass' outside of an anonymous namespace
2164
MyClass(MyClass&&) = default;
65+
// CHECK-MESSAGES-MEM: :[[@LINE-1]]:3: warning: place definition of method 'MyClass' outside of an anonymous namespace
2266
MyClass& operator=(const MyClass&);
2367
MyClass& operator=(MyClass&&);
2468
bool operator<(const MyClass&) const;
2569
void memberFunction();
2670
static void staticMemberFunction();
2771
void memberDefinedInClass() {}
72+
// CHECK-MESSAGES-MEM: :[[@LINE-1]]:8: warning: place definition of method 'memberDefinedInClass' outside of an anonymous namespace
2873
static void staticMemberDefinedInClass() {}
74+
// CHECK-MESSAGES-MEM: :[[@LINE-1]]:15: warning: place definition of method 'staticMemberDefinedInClass' outside of an anonymous namespace
75+
template <typename T>
76+
void templateFunction();
77+
template <typename T>
78+
void templateFunctionInClass() {}
79+
// CHECK-MESSAGES-MEM: :[[@LINE-1]]:8: warning: place definition of method 'templateFunctionInClass' outside of an anonymous namespace
2980
};
3081

3182
MyClass::MyClass() {}
@@ -46,27 +97,38 @@ void MyClass::memberFunction() {}
4697
void MyClass::staticMemberFunction() {}
4798
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: place definition of method 'staticMemberFunction' outside of an anonymous namespace
4899

100+
template <typename T>
101+
void MyClass::templateFunction() {}
102+
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: place definition of method 'templateFunction' outside of an anonymous namespace
103+
49104
template<typename T>
50105
void templateFunction(T Value) {}
51-
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'templateFunction' is declared in an anonymous namespace;
106+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'templateFunction' is declared in an anonymous namespace; prefer using 'static' for restricting visibility
52107

53108
template<>
54109
void templateFunction<int>(int Value) {}
55-
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'templateFunction<int>' is declared in an anonymous namespace;
110+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'templateFunction<int>' is declared in an anonymous namespace; prefer using 'static' for restricting visibility
56111

57112
template<typename T>
58113
class TemplateClass {
59114
public:
60115
TemplateClass();
61116
TemplateClass(const TemplateClass&) {}
117+
// CHECK-MESSAGES-MEM: :[[@LINE-1]]:3: warning: place definition of method 'TemplateClass<T>' outside of an anonymous namespace
62118
TemplateClass(TemplateClass&&) = default;
119+
// CHECK-MESSAGES-MEM: :[[@LINE-1]]:3: warning: place definition of method 'TemplateClass<T>' outside of an anonymous namespace
63120
TemplateClass& operator=(const TemplateClass&);
64121
TemplateClass& operator=(TemplateClass&&);
65122
bool operator<(const TemplateClass&) const;
66123
void memberFunc();
67124
T getValue() const;
68125
void memberDefinedInClass() {}
126+
// CHECK-MESSAGES-MEM: :[[@LINE-1]]:8: warning: place definition of method 'memberDefinedInClass' outside of an anonymous namespace
69127
static void staticMemberDefinedInClass() {}
128+
// CHECK-MESSAGES-MEM: :[[@LINE-1]]:15: warning: place definition of method 'staticMemberDefinedInClass' outside of an anonymous namespace
129+
template <typename U>
130+
void templateMethodInTemplateClass() {}
131+
// CHECK-MESSAGES-MEM: :[[@LINE-1]]:8: warning: place definition of method 'templateMethodInTemplateClass' outside of an anonymous namespace
70132
private:
71133
T Value;
72134
};
@@ -96,31 +158,24 @@ T TemplateClass<T>::getValue() const { return Value; }
96158
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: place definition of method 'getValue' outside of an anonymous namespace
97159

98160
inline void inlineFunction() {}
99-
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: function 'inlineFunction' is declared in an anonymous namespace;
161+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: function 'inlineFunction' is declared in an anonymous namespace; prefer using 'static' for restricting visibility
100162

101163
auto autoReturnFunction() -> int { return 42; }
102-
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'autoReturnFunction' is declared in an anonymous namespace;
164+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'autoReturnFunction' is declared in an anonymous namespace; prefer using 'static' for restricting visibility
103165

104166
class OuterClass {
105167
public:
106168
class NestedClass {
107169
public:
108170
void nestedMemberFunc();
171+
void nestedMemberDefinedInClass() {}
172+
// CHECK-MESSAGES-MEM: :[[@LINE-1]]:10: warning: place definition of method 'nestedMemberDefinedInClass' outside of an anonymous namespace
109173
};
110174
};
111175

112176
void OuterClass::NestedClass::nestedMemberFunc() {}
113177
// CHECK-MESSAGES: :[[@LINE-1]]:31: warning: place definition of method 'nestedMemberFunc' outside of an anonymous namespace
114178

115-
116-
// Variables are not warned by default
117-
template<typename T>
118-
constexpr T Pi = T(3.1415926);
119-
120-
void (*funcPtr)() = nullptr;
121-
122-
auto lambda = []() { return 42; };
123-
124179
} // namespace
125180

126181
#define DEFINE_FUNCTION(name) \
@@ -143,4 +198,4 @@ namespace {
143198

144199
INTERNAL_FUNC
145200

146-
} // namespace
201+
} // namespace

0 commit comments

Comments
 (0)