Skip to content

Commit 34c52f3

Browse files
committed
[clang-tidy] Add first-class C support to misc-use-internal-linkage
1 parent 92e343e commit 34c52f3

File tree

7 files changed

+66
-30
lines changed

7 files changed

+66
-30
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ void UseInternalLinkageCheck::registerMatchers(MatchFinder *Finder) {
137137
}
138138

139139
static constexpr StringRef Message =
140-
"%0 %1 can be made static or moved into an anonymous namespace "
140+
"%0 %1 can be made static %select{|or moved into an anonymous namespace }2"
141141
"to enforce internal linkage";
142142

143143
void UseInternalLinkageCheck::check(const MatchFinder::MatchResult &Result) {
144144
if (const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("fn")) {
145145
const DiagnosticBuilder DB = diag(FD->getLocation(), Message)
146-
<< "function" << FD;
146+
<< "function" << FD << getLangOpts().CPlusPlus;
147147
const SourceLocation FixLoc = FD->getInnerLocStart();
148148
if (FixLoc.isInvalid() || FixLoc.isMacroID())
149149
return;
@@ -159,7 +159,7 @@ void UseInternalLinkageCheck::check(const MatchFinder::MatchResult &Result) {
159159
return;
160160

161161
const DiagnosticBuilder DB = diag(VD->getLocation(), Message)
162-
<< "variable" << VD;
162+
<< "variable" << VD << getLangOpts().CPlusPlus;
163163
const SourceLocation FixLoc = VD->getInnerLocStart();
164164
if (FixLoc.isInvalid() || FixLoc.isMacroID())
165165
return;

clang-tools-extra/docs/clang-tidy/checks/misc/use-internal-linkage.rst

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
misc-use-internal-linkage
44
=========================
55

6-
Detects variables and functions that can be marked as static or moved into
7-
an anonymous namespace to enforce internal linkage.
6+
Detects variables and functions that can be marked as static or (in C++)
7+
moved into an anonymous namespace to enforce internal linkage.
88

99
Static functions and variables are scoped to a single file. Marking functions
1010
and variables as static helps to better remove dead code. In addition, it gives
@@ -18,17 +18,20 @@ Example:
1818

1919
void fn1() {} // can be marked as static
2020

21-
namespace {
22-
// already in anonymous namespace
23-
int v2;
24-
void fn2();
25-
}
2621
// already declared as extern
2722
extern int v2;
2823

2924
void fn3(); // without function body in all declaration, maybe external linkage
3025
void fn3();
3126

27+
// === C++-specific ===
28+
29+
namespace {
30+
// already in anonymous namespace
31+
int v2;
32+
void fn2();
33+
}
34+
3235
// export declarations
3336
export void fn4() {}
3437
export namespace t { void fn5() {} }

clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-consteval.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
consteval void gh122096() {}
44

55
constexpr void cxf() {}
6-
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: function 'cxf'
6+
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: function 'cxf' can be made static or moved into an anonymous namespace to enforce internal linkage
77
// CHECK-FIXES: static constexpr void cxf() {}

clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-fix-mode-none.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// RUN: -config="{CheckOptions: {misc-use-internal-linkage.FixMode: 'None'}}" -- -I%S/Inputs/use-internal-linkage
33

44
void func() {}
5-
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func'
5+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func' can be made static or moved into an anonymous namespace to enforce internal linkage
66
// CHECK-FIXES-NOT: static void func() {}
77

88
int global;
9-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'global'
9+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'global' can be made static or moved into an anonymous namespace to enforce internal linkage
1010
// CHECK-FIXES-NOT: static int global;

clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-func.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,51 @@
55
#include "func.h"
66

77
void func() {}
8-
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func'
8+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func' can be made static or moved into an anonymous namespace to enforce internal linkage
99
// CHECK-FIXES: static void func() {}
1010

1111
template<class T>
1212
void func_template() {}
13-
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func_template'
13+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func_template' can be made static or moved into an anonymous namespace to enforce internal linkage
1414
// CHECK-FIXES: static void func_template() {}
1515

1616
void func_cpp_inc() {}
17-
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func_cpp_inc'
17+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func_cpp_inc' can be made static or moved into an anonymous namespace to enforce internal linkage
1818
// CHECK-FIXES: static void func_cpp_inc() {}
1919

2020
int* func_cpp_inc_return_ptr() { return nullptr; }
21-
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func_cpp_inc_return_ptr'
21+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func_cpp_inc_return_ptr' can be made static or moved into an anonymous namespace to enforce internal linkage
2222
// CHECK-FIXES: static int* func_cpp_inc_return_ptr() { return nullptr; }
2323

2424
const int* func_cpp_inc_return_const_ptr() { return nullptr; }
25-
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: function 'func_cpp_inc_return_const_ptr'
25+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: function 'func_cpp_inc_return_const_ptr' can be made static or moved into an anonymous namespace to enforce internal linkage
2626
// CHECK-FIXES: static const int* func_cpp_inc_return_const_ptr() { return nullptr; }
2727

2828
int const* func_cpp_inc_return_ptr_const() { return nullptr; }
29-
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: function 'func_cpp_inc_return_ptr_const'
29+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: function 'func_cpp_inc_return_ptr_const' can be made static or moved into an anonymous namespace to enforce internal linkage
3030
// CHECK-FIXES: static int const* func_cpp_inc_return_ptr_const() { return nullptr; }
3131

3232
int * const func_cpp_inc_return_const() { return nullptr; }
33-
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: function 'func_cpp_inc_return_const'
33+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: function 'func_cpp_inc_return_const' can be made static or moved into an anonymous namespace to enforce internal linkage
3434
// CHECK-FIXES: static int * const func_cpp_inc_return_const() { return nullptr; }
3535

3636
volatile const int* func_cpp_inc_return_volatile_const_ptr() { return nullptr; }
37-
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: function 'func_cpp_inc_return_volatile_const_ptr'
37+
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: function 'func_cpp_inc_return_volatile_const_ptr' can be made static or moved into an anonymous namespace to enforce internal linkage
3838
// CHECK-FIXES: static volatile const int* func_cpp_inc_return_volatile_const_ptr() { return nullptr; }
3939

4040
[[nodiscard]] void func_nodiscard() {}
41-
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: function 'func_nodiscard'
41+
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: function 'func_nodiscard' can be made static or moved into an anonymous namespace to enforce internal linkage
4242
// CHECK-FIXES: {{\[\[nodiscard\]\]}} static void func_nodiscard() {}
4343

4444
#define NDS [[nodiscard]]
4545
#define NNDS
4646

4747
NDS void func_nds() {}
48-
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function 'func_nds'
48+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function 'func_nds' can be made static or moved into an anonymous namespace to enforce internal linkage
4949
// CHECK-FIXES: NDS static void func_nds() {}
5050

5151
NNDS void func_nnds() {}
52-
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: function 'func_nnds'
52+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: function 'func_nnds' can be made static or moved into an anonymous namespace to enforce internal linkage
5353
// CHECK-FIXES: NNDS static void func_nnds() {}
5454

5555
#include "func_cpp.inc"
@@ -78,7 +78,7 @@ extern "C" void func_extern_c_2() {}
7878

7979
namespace gh117488 {
8080
void func_with_body();
81-
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func_with_body'
81+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func_with_body' can be made static or moved into an anonymous namespace to enforce internal linkage
8282
// CHECK-FIXES: static void func_with_body();
8383
void func_with_body() {}
8484

clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
#include "var.h"
66

77
int global;
8-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'global'
8+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'global' can be made static or moved into an anonymous namespace to enforce internal linkage
99
// CHECK-FIXES: static int global;
1010

1111
template<class T>
1212
T global_template;
13-
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: variable 'global_template'
13+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: variable 'global_template' can be made static or moved into an anonymous namespace to enforce internal linkage
1414
// CHECK-FIXES: static T global_template;
1515

1616
int const* ptr_const_star;
17-
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'ptr_const_star'
17+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'ptr_const_star' can be made static or moved into an anonymous namespace to enforce internal linkage
1818
// CHECK-FIXES: static int const* ptr_const_star;
1919

2020
const int* const_ptr_star;
21-
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'const_ptr_star'
21+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'const_ptr_star' can be made static or moved into an anonymous namespace to enforce internal linkage
2222
// CHECK-FIXES: static const int* const_ptr_star;
2323

2424
const volatile int* const_volatile_ptr_star;
25-
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: variable 'const_volatile_ptr_star'
25+
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: variable 'const_volatile_ptr_star' can be made static or moved into an anonymous namespace to enforce internal linkage
2626
// CHECK-FIXES: static const volatile int* const_volatile_ptr_star;
2727

2828
int gloabl_header;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %check_clang_tidy %s misc-use-internal-linkage %t -- -- -I%S/Inputs/use-internal-linkage
2+
// RUN: %check_clang_tidy %s misc-use-internal-linkage %t -- \
3+
// RUN: -config="{CheckOptions: {misc-use-internal-linkage.FixMode: 'UseStatic'}}" -- -I%S/Inputs/use-internal-linkage
4+
5+
#include "func.h"
6+
7+
void func() {}
8+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func' can be made static to enforce internal linkage
9+
// CHECK-FIXES: static void func() {}
10+
11+
void func_header() {}
12+
extern void func_extern() {}
13+
static void func_static() {}
14+
15+
int main() {}
16+
17+
18+
#include "var.h"
19+
20+
int global;
21+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'global' can be made static to enforce internal linkage
22+
// CHECK-FIXES: static int global;
23+
24+
const int const_global = 123;
25+
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: variable 'const_global' can be made static to enforce internal linkage
26+
// CHECK-FIXES: static const int const_global = 123;
27+
28+
int gloabl_header;
29+
extern int global_extern;
30+
static int global_static;
31+
#if __STDC_VERSION__ >= 201112L
32+
_Thread_local int global_thread_local;
33+
#endif

0 commit comments

Comments
 (0)