[clang-tools-extra] 88a7508 - [clang-tidy] Deal with keyword tokens in preprocessor conditions

via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 8 15:07:03 PDT 2022


Author: Richard
Date: 2022-04-08T16:06:06-06:00
New Revision: 88a7508b1fd87c7dd7f155cb1541c61f4c1f1bc8

URL: https://github.com/llvm/llvm-project/commit/88a7508b1fd87c7dd7f155cb1541c61f4c1f1bc8
DIFF: https://github.com/llvm/llvm-project/commit/88a7508b1fd87c7dd7f155cb1541c61f4c1f1bc8.diff

LOG: [clang-tidy] Deal with keyword tokens in preprocessor conditions

When a "keyword" token like __restrict was present in a macro condition,
modernize-macro-to-enum would assert in non-release builds.  However,
even for a "keyword" token, calling getIdentifierInfo()->getName() would
retrieve the text of the token, which is what we want.  Our intention is
to scan names that appear in conditional expressions in potential enum
clusters and invalidate those clusters if they contain the name.

Also, guard against "raw identifiers" appearing as potential enums.
This shouldn't happen, but it doesn't hurt to generalize the code.

Differential Revision: https://reviews.llvm.org/D123349

Fixes #54775

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/modernize-macro-to-enum.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
index e6115af77bd5e..b2d59adc9a556 100644
--- a/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
@@ -92,6 +92,11 @@ static bool isIntegralConstant(const Token &Token) {
       Begin, End, [](char C) { return C == '.' || std::toupper(C) == 'E'; });
 }
 
+static StringRef getTokenName(const Token &Tok) {
+  return Tok.is(tok::raw_identifier) ? Tok.getRawIdentifier()
+                                     : Tok.getIdentifierInfo()->getName();
+}
+
 namespace {
 
 struct EnumMacro {
@@ -279,19 +284,11 @@ void MacroToEnumCallbacks::checkCondition(SourceRange Range) {
 }
 
 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 @@ void MacroToEnumCallbacks::MacroUndefined(const Token &MacroNameTok,
                                           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::EndOfMainFile() {
 
 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 {

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/modernize-macro-to-enum.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize-macro-to-enum.cpp
index 5a5514fc074cb..37122cfc61b9e 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize-macro-to-enum.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize-macro-to-enum.cpp
@@ -208,6 +208,10 @@ inline void used_ifndef() {}
 #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"


        


More information about the cfe-commits mailing list