Skip to content

Commit 8d206f5

Browse files
authored
[clang-tidy] Allow renaming macro arguments (#87792)
Although the identifier-naming.cpp lit test expected macro arguments not to be renamed, the code seemed to already allow it. The code was simply not being exercised because a SourceManager argument wasn't being provided. With this change, renaming of macro arguments that expand to renamable decls is permitted.
1 parent a0651db commit 8d206f5

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ void RenamerClangTidyCheck::checkNamedDecl(const NamedDecl *Decl,
489489
}
490490

491491
Failure.Info = std::move(Info);
492-
addUsage(Decl, Range);
492+
addUsage(Decl, Range, &SourceMgr);
493493
}
494494

495495
void RenamerClangTidyCheck::check(const MatchFinder::MatchResult &Result) {

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ Changes in existing checks
268268
<clang-tidy/checks/readability/identifier-naming>` check in `GetConfigPerFile`
269269
mode by resolving symbolic links to header files. Fixed handling of Hungarian
270270
Prefix when configured to `LowerCase`. Added support for renaming designated
271-
initializers.
271+
initializers. Added support for renaming macro arguments.
272272

273273
- Improved :doc:`readability-implicit-bool-conversion
274274
<clang-tidy/checks/readability/implicit-bool-conversion>` check to provide

clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ USER_NS::object g_s2;
108108
// NO warnings or fixes expected as USER_NS and object are declared in a header file
109109

110110
SYSTEM_MACRO(var1);
111-
// NO warnings or fixes expected as var1 is from macro expansion
111+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: invalid case style for global variable 'var1' [readability-identifier-naming]
112+
// CHECK-FIXES: {{^}}SYSTEM_MACRO(g_var1);
112113

113114
USER_MACRO(var2);
114-
// NO warnings or fixes expected as var2 is declared in a macro expansion
115+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: invalid case style for global variable 'var2' [readability-identifier-naming]
116+
// CHECK-FIXES: {{^}}USER_MACRO(g_var2);
115117

116118
#define BLA int FOO_bar
117119
BLA;
@@ -602,9 +604,20 @@ static void static_Function() {
602604
// CHECK-FIXES: {{^}}#define MY_TEST_MACRO(X) X()
603605

604606
void MY_TEST_Macro(function) {}
605-
// CHECK-FIXES: {{^}}void MY_TEST_MACRO(function) {}
606-
}
607-
}
607+
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: invalid case style for global function 'function' [readability-identifier-naming]
608+
// CHECK-FIXES: {{^}}void MY_TEST_MACRO(Function) {}
609+
610+
#define MY_CAT_IMPL(l, r) l ## r
611+
#define MY_CAT(l, r) MY_CAT_IMPL(l, r)
612+
#define MY_MACRO2(foo) int MY_CAT(awesome_, MY_CAT(foo, __COUNTER__)) = 0
613+
#define MY_MACRO3(foo) int MY_CAT(awesome_, foo) = 0
614+
MY_MACRO2(myglob);
615+
MY_MACRO3(myglob);
616+
// No suggestions should occur even though the resulting decl of awesome_myglob#
617+
// or awesome_myglob are not entirely within a macro argument.
618+
619+
} // namespace InlineNamespace
620+
} // namespace FOO_NS
608621

609622
template <typename t_t> struct a {
610623
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: invalid case style for struct 'a'

0 commit comments

Comments
 (0)