Skip to content

Commit 2992d08

Browse files
committed
[clang-tidy] Do not warn on macros starting with underscore and lowercase letter in bugprone-reserved-identifier
Fixes llvm#64130 Differential Revision: https://reviews.llvm.org/D156608
1 parent 5864256 commit 2992d08

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,15 @@ static std::optional<std::string> getUnderscoreCapitalFixup(StringRef Name) {
102102
}
103103

104104
static bool startsWithUnderscoreInGlobalNamespace(StringRef Name,
105-
bool IsInGlobalNamespace) {
106-
return IsInGlobalNamespace && Name.size() >= 1 && Name[0] == '_';
105+
bool IsInGlobalNamespace,
106+
bool IsMacro) {
107+
return !IsMacro && IsInGlobalNamespace && Name.size() >= 1 && Name[0] == '_';
107108
}
108109

109110
static std::optional<std::string>
110-
getUnderscoreGlobalNamespaceFixup(StringRef Name, bool IsInGlobalNamespace) {
111-
if (startsWithUnderscoreInGlobalNamespace(Name, IsInGlobalNamespace))
111+
getUnderscoreGlobalNamespaceFixup(StringRef Name, bool IsInGlobalNamespace,
112+
bool IsMacro) {
113+
if (startsWithUnderscoreInGlobalNamespace(Name, IsInGlobalNamespace, IsMacro))
112114
return std::string(Name.drop_front(1));
113115
return std::nullopt;
114116
}
@@ -123,7 +125,7 @@ static std::string getNonReservedFixup(std::string Name) {
123125
}
124126

125127
static std::optional<RenamerClangTidyCheck::FailureInfo>
126-
getFailureInfoImpl(StringRef Name, bool IsInGlobalNamespace,
128+
getFailureInfoImpl(StringRef Name, bool IsInGlobalNamespace, bool IsMacro,
127129
const LangOptions &LangOpts, bool Invert,
128130
ArrayRef<llvm::Regex> AllowedIdentifiers) {
129131
assert(!Name.empty());
@@ -158,15 +160,16 @@ getFailureInfoImpl(StringRef Name, bool IsInGlobalNamespace,
158160
AppendFailure(DoubleUnderscoreTag, std::move(*Fixup));
159161
if (auto Fixup = getUnderscoreCapitalFixup(InProgressFixup()))
160162
AppendFailure(UnderscoreCapitalTag, std::move(*Fixup));
161-
if (auto Fixup = getUnderscoreGlobalNamespaceFixup(InProgressFixup(),
162-
IsInGlobalNamespace))
163+
if (auto Fixup = getUnderscoreGlobalNamespaceFixup(
164+
InProgressFixup(), IsInGlobalNamespace, IsMacro))
163165
AppendFailure(GlobalUnderscoreTag, std::move(*Fixup));
164166

165167
return Info;
166168
}
167169
if (!(hasReservedDoubleUnderscore(Name, LangOpts) ||
168170
startsWithUnderscoreCapital(Name) ||
169-
startsWithUnderscoreInGlobalNamespace(Name, IsInGlobalNamespace)))
171+
startsWithUnderscoreInGlobalNamespace(Name, IsInGlobalNamespace,
172+
IsMacro)))
170173
return FailureInfo{NonReservedTag, getNonReservedFixup(std::string(Name))};
171174
return std::nullopt;
172175
}
@@ -177,16 +180,17 @@ ReservedIdentifierCheck::getDeclFailureInfo(const NamedDecl *Decl,
177180
assert(Decl && Decl->getIdentifier() && !Decl->getName().empty() &&
178181
!Decl->isImplicit() &&
179182
"Decl must be an explicit identifier with a name.");
180-
return getFailureInfoImpl(Decl->getName(),
181-
isa<TranslationUnitDecl>(Decl->getDeclContext()),
182-
getLangOpts(), Invert, AllowedIdentifiers);
183+
return getFailureInfoImpl(
184+
Decl->getName(), isa<TranslationUnitDecl>(Decl->getDeclContext()),
185+
/*IsMacro = */ false, getLangOpts(), Invert, AllowedIdentifiers);
183186
}
184187

185188
std::optional<RenamerClangTidyCheck::FailureInfo>
186189
ReservedIdentifierCheck::getMacroFailureInfo(const Token &MacroNameTok,
187190
const SourceManager &) const {
188191
return getFailureInfoImpl(MacroNameTok.getIdentifierInfo()->getName(), true,
189-
getLangOpts(), Invert, AllowedIdentifiers);
192+
/*IsMacro = */ true, getLangOpts(), Invert,
193+
AllowedIdentifiers);
190194
}
191195

192196
RenamerClangTidyCheck::DiagInfo

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ New check aliases
140140
Changes in existing checks
141141
^^^^^^^^^^^^^^^^^^^^^^^^^^
142142

143+
- Fixed bug in :doc:`bugprone-reserved-identifier
144+
<clang-tidy/checks/bugprone/reserved-identifier>`, so that it does not warn
145+
on macros starting with underscore and lowercase letter.
146+
143147
Removed checks
144148
^^^^^^^^^^^^^^
145149

clang-tools-extra/test/clang-tidy/checkers/bugprone/reserved-identifier.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct S {};
4545

4646
#define __macro(m) int m = 0
4747
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: declaration uses identifier '__macro', which is a reserved identifier [bugprone-reserved-identifier]
48-
// CHECK-FIXES: {{^}}#define macro(m) int m = 0{{$}}
48+
// CHECK-FIXES: {{^}}#define _macro(m) int m = 0{{$}}
4949

5050
namespace __ns {
5151
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: declaration uses identifier '__ns', which is a reserved identifier [bugprone-reserved-identifier]
@@ -115,9 +115,9 @@ struct S {};
115115

116116
//
117117

118+
// OK, this rule does not apply to macros:
119+
// https://github.com/llvm/llvm-project/issues/64130#issuecomment-1655751676
118120
#define _macro(m) int m = 0
119-
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: declaration uses identifier '_macro', which is reserved in the global namespace [bugprone-reserved-identifier]
120-
// CHECK-FIXES: {{^}}#define macro(m) int m = 0{{$}}
121121

122122
namespace _ns {
123123
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: declaration uses identifier '_ns', which is reserved in the global namespace [bugprone-reserved-identifier]
@@ -171,10 +171,9 @@ int _;
171171
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: declaration uses identifier '_', which is reserved in the global namespace; cannot be fixed automatically [bugprone-reserved-identifier]
172172
// CHECK-FIXES: {{^}}int _;{{$}}
173173

174+
// This should not trigger a warning
174175
// https://github.com/llvm/llvm-project/issues/52895
175176
#define _5_kmph_rpm 459
176-
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: declaration uses identifier '_5_kmph_rpm', which is reserved in the global namespace; cannot be fixed automatically [bugprone-reserved-identifier]
177-
// CHECK-FIXES: {{^}}#define _5_kmph_rpm 459{{$}}
178177

179178
// these should pass
180179
#define MACRO(m) int m = 0

0 commit comments

Comments
 (0)