[PATCH] D123349: [clang-tidy] Deal with keyword tokens in preprocessor conditions
Richard via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 8 11:44:23 PDT 2022
LegalizeAdulthood updated this revision to Diff 421603.
LegalizeAdulthood marked an inline comment as done.
LegalizeAdulthood added a comment.
Update from review comments
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D123349/new/
https://reviews.llvm.org/D123349
Files:
clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/modernize-macro-to-enum.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/modernize-macro-to-enum.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/modernize-macro-to-enum.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize-macro-to-enum.cpp
@@ -208,6 +208,10 @@
#define IFNDEF3 3
#endif
+// Sometimes an argument to ifdef can be classified as a keyword token.
+#ifdef __restrict
+#endif
+
// These macros do not expand to integral constants.
#define HELLO "Hello, "
#define WORLD "World"
Index: clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
+++ clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
@@ -278,20 +278,17 @@
}
}
+StringRef getTokenName(const Token &Tok) {
+ return Tok.is(tok::raw_identifier) ? Tok.getRawIdentifier()
+ : Tok.getIdentifierInfo()->getName();
+}
+
void MacroToEnumCallbacks::checkName(const Token &MacroNameTok) {
- std::string Id;
- if (MacroNameTok.is(tok::raw_identifier))
- Id = MacroNameTok.getRawIdentifier().str();
- else if (MacroNameTok.is(tok::identifier))
- Id = MacroNameTok.getIdentifierInfo()->getName().str();
- else {
- assert(false && "Expected either an identifier or raw identifier token");
- return;
- }
+ StringRef Id = getTokenName(MacroNameTok);
llvm::erase_if(Enums, [&Id](const MacroList &MacroList) {
return llvm::any_of(MacroList, [&Id](const EnumMacro &Macro) {
- return Macro.Name.getIdentifierInfo()->getName().str() == Id;
+ return getTokenName(Macro.Name) == Id;
});
});
}
@@ -355,8 +352,7 @@
const MacroDefinition &MD,
const MacroDirective *Undef) {
auto MatchesToken = [&MacroNameTok](const EnumMacro &Macro) {
- return Macro.Name.getIdentifierInfo()->getName() ==
- MacroNameTok.getIdentifierInfo()->getName();
+ return getTokenName(Macro.Name) == getTokenName(MacroNameTok);
};
auto It = llvm::find_if(Enums, [MatchesToken](const MacroList &MacroList) {
@@ -432,8 +428,8 @@
void MacroToEnumCallbacks::warnMacroEnum(const EnumMacro &Macro) const {
Check->diag(Macro.Directive->getLocation(),
- "macro %0 defines an integral constant; prefer an enum instead")
- << Macro.Name.getIdentifierInfo();
+ "macro '%0' defines an integral constant; prefer an enum instead")
+ << getTokenName(Macro.Name);
}
void MacroToEnumCallbacks::fixEnumMacro(const MacroList &MacroList) const {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123349.421603.patch
Type: text/x-patch
Size: 2701 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220408/864d22a8/attachment.bin>
More information about the cfe-commits
mailing list