Skip to content

Commit 6f21a7b

Browse files
[clang-tidy] insert static keyword in correct position for misc-use-internal-linkage (#108792)
Fixes: #108760 --------- Co-authored-by: Danny Mösch <[email protected]>
1 parent 256bbdb commit 6f21a7b

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88

99
#include "UseInternalLinkageCheck.h"
1010
#include "../utils/FileExtensionsUtils.h"
11+
#include "../utils/LexerUtils.h"
1112
#include "clang/AST/Decl.h"
1213
#include "clang/ASTMatchers/ASTMatchFinder.h"
1314
#include "clang/ASTMatchers/ASTMatchers.h"
1415
#include "clang/ASTMatchers/ASTMatchersMacros.h"
1516
#include "clang/Basic/SourceLocation.h"
1617
#include "clang/Basic/Specifiers.h"
18+
#include "clang/Basic/TokenKinds.h"
19+
#include "clang/Lex/Token.h"
1720
#include "llvm/ADT/STLExtras.h"
1821

1922
using namespace clang::ast_matchers;
@@ -113,7 +116,7 @@ static constexpr StringRef Message =
113116
void UseInternalLinkageCheck::check(const MatchFinder::MatchResult &Result) {
114117
if (const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("fn")) {
115118
DiagnosticBuilder DB = diag(FD->getLocation(), Message) << "function" << FD;
116-
SourceLocation FixLoc = FD->getTypeSpecStartLoc();
119+
const SourceLocation FixLoc = FD->getInnerLocStart();
117120
if (FixLoc.isInvalid() || FixLoc.isMacroID())
118121
return;
119122
if (FixMode == FixModeKind::UseStatic)
@@ -128,7 +131,7 @@ void UseInternalLinkageCheck::check(const MatchFinder::MatchResult &Result) {
128131
return;
129132

130133
DiagnosticBuilder DB = diag(VD->getLocation(), Message) << "variable" << VD;
131-
SourceLocation FixLoc = VD->getTypeSpecStartLoc();
134+
const SourceLocation FixLoc = VD->getInnerLocStart();
132135
if (FixLoc.isInvalid() || FixLoc.isMacroID())
133136
return;
134137
if (FixMode == FixModeKind::UseStatic)

clang-tools-extra/clang-tidy/utils/LexerUtils.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ getPreviousTokenAndStart(SourceLocation Location, const SourceManager &SM,
2424
if (Location.isInvalid())
2525
return {Token, Location};
2626

27-
auto StartOfFile = SM.getLocForStartOfFile(SM.getFileID(Location));
27+
const auto StartOfFile = SM.getLocForStartOfFile(SM.getFileID(Location));
2828
while (Location != StartOfFile) {
2929
Location = Lexer::GetBeginningOfToken(Location, SM, LangOpts);
3030
if (!Lexer::getRawToken(Location, Token, SM, LangOpts) &&
3131
(!SkipComments || !Token.is(tok::comment))) {
3232
break;
3333
}
34+
if (Location == StartOfFile)
35+
return {Token, Location};
3436
Location = Location.getLocWithOffset(-1);
3537
}
3638
return {Token, Location};

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ Changes in existing checks
195195
<clang-tidy/checks/modernize/loop-convert>` check to fix false positive when
196196
using loop variable in initializer of lambda capture.
197197

198+
- Improved :doc:`misc-use-internal-linkage
199+
<clang-tidy/checks/misc/use-internal-linkage>` check to insert ``static`` keyword
200+
before type qualifiers such as ``const`` and ``volatile``.
201+
198202
- Improved :doc:`modernize-min-max-use-initializer-list
199203
<clang-tidy/checks/modernize/min-max-use-initializer-list>` check by fixing
200204
a false positive when only an implicit conversion happened inside an

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,41 @@ void func_cpp_inc();
1717
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func_cpp_inc'
1818
// CHECK-FIXES: static void func_cpp_inc();
1919

20+
int* func_cpp_inc_return_ptr();
21+
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func_cpp_inc_return_ptr'
22+
// CHECK-FIXES: static int* func_cpp_inc_return_ptr();
23+
24+
const int* func_cpp_inc_return_const_ptr();
25+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: function 'func_cpp_inc_return_const_ptr'
26+
// CHECK-FIXES: static const int* func_cpp_inc_return_const_ptr();
27+
28+
int const* func_cpp_inc_return_ptr_const();
29+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: function 'func_cpp_inc_return_ptr_const'
30+
// CHECK-FIXES: static int const* func_cpp_inc_return_ptr_const();
31+
32+
int * const func_cpp_inc_return_const();
33+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: function 'func_cpp_inc_return_const'
34+
// CHECK-FIXES: static int * const func_cpp_inc_return_const();
35+
36+
volatile const int* func_cpp_inc_return_volatile_const_ptr();
37+
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: function 'func_cpp_inc_return_volatile_const_ptr'
38+
// CHECK-FIXES: static volatile const int* func_cpp_inc_return_volatile_const_ptr();
39+
40+
[[nodiscard]] void func_nodiscard();
41+
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: function 'func_nodiscard'
42+
// CHECK-FIXES: {{\[\[nodiscard\]\]}} static void func_nodiscard();
43+
44+
#define NDS [[nodiscard]]
45+
#define NNDS
46+
47+
NDS void func_nds();
48+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function 'func_nds'
49+
// CHECK-FIXES: NDS static void func_nds();
50+
51+
NNDS void func_nnds();
52+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: function 'func_nnds'
53+
// CHECK-FIXES: NNDS static void func_nnds();
54+
2055
#include "func_cpp.inc"
2156

2257
void func_h_inc();

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ T global_template;
1313
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: variable 'global_template'
1414
// CHECK-FIXES: static T global_template;
1515

16+
int const* ptr_const_star;
17+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'ptr_const_star'
18+
// CHECK-FIXES: static int const* ptr_const_star;
19+
20+
const int* const_ptr_star;
21+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: variable 'const_ptr_star'
22+
// CHECK-FIXES: static const int* const_ptr_star;
23+
24+
const volatile int* const_volatile_ptr_star;
25+
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: variable 'const_volatile_ptr_star'
26+
// CHECK-FIXES: static const volatile int* const_volatile_ptr_star;
27+
1628
int gloabl_header;
1729

1830
extern int global_extern;

0 commit comments

Comments
 (0)