Skip to content

Commit b341566

Browse files
committed
[clang-tidy] Allow renaming macro arguments
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 2950283 commit b341566

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ struct DenseMapInfo<clang::tidy::RenamerClangTidyCheck::NamingCheckId> {
3131
using NamingCheckId = clang::tidy::RenamerClangTidyCheck::NamingCheckId;
3232

3333
static inline NamingCheckId getEmptyKey() {
34-
return {DenseMapInfo<clang::SourceLocation>::getEmptyKey(),
35-
"EMPTY"};
34+
return {DenseMapInfo<clang::SourceLocation>::getEmptyKey(), "EMPTY"};
3635
}
3736

3837
static inline NamingCheckId getTombstoneKey() {
3938
return {DenseMapInfo<clang::SourceLocation>::getTombstoneKey(),
40-
"TOMBSTONE"};
39+
"TOMBSTONE"};
4140
}
4241

4342
static unsigned getHashValue(NamingCheckId Val) {
@@ -473,7 +472,7 @@ void RenamerClangTidyCheck::checkNamedDecl(const NamedDecl *Decl,
473472
}
474473

475474
Failure.Info = std::move(Info);
476-
addUsage(Decl, Range);
475+
addUsage(Decl, Range, &SourceMgr);
477476
}
478477

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

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)