[clang-tools-extra] 9f3edc3 - [clang-tidy] Fix RenamerClangTidy checks trying to emit a fix that isnt a valid identifier

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 12:04:58 PST 2020


Author: Nathan James
Date: 2020-11-23T20:04:51Z
New Revision: 9f3edc323a88c1a179a0a5a9dc9a87a2964c0d48

URL: https://github.com/llvm/llvm-project/commit/9f3edc323a88c1a179a0a5a9dc9a87a2964c0d48
DIFF: https://github.com/llvm/llvm-project/commit/9f3edc323a88c1a179a0a5a9dc9a87a2964c0d48.diff

LOG: [clang-tidy] Fix RenamerClangTidy checks trying to emit a fix that isnt a valid identifier

Addresses https://bugs.llvm.org/show_bug.cgi?id=48230.
Handle the case when the Fixup suggested isn't a valid c/c++ identifer.

Reviewed By: aaron.ballman, gribozavr2

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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
    clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h
    clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-bugfix-name-conflicts.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
index 6a25813eb748..2bb97eca14ab 100644
--- a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
@@ -10,6 +10,7 @@
 #include "ASTUtils.h"
 #include "clang/AST/CXXInheritance.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Basic/CharInfo.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/Preprocessor.h"
@@ -463,6 +464,8 @@ void RenamerClangTidyCheck::check(const MatchFinder::MatchResult &Result) {
         Failure.FixStatus = ShouldFixStatus::ConflictsWithKeyword;
       else if (Ident->hasMacroDefinition())
         Failure.FixStatus = ShouldFixStatus::ConflictsWithMacroDefinition;
+    } else if (!isValidIdentifier(Info.Fixup)) {
+      Failure.FixStatus = ShouldFixStatus::FixInvalidIdentifier;
     }
 
     Failure.Info = std::move(Info);
@@ -503,7 +506,8 @@ void RenamerClangTidyCheck::expandMacro(const Token &MacroNameTok,
 static std::string
 getDiagnosticSuffix(const RenamerClangTidyCheck::ShouldFixStatus FixStatus,
                     const std::string &Fixup) {
-  if (Fixup.empty())
+  if (Fixup.empty() ||
+      FixStatus == RenamerClangTidyCheck::ShouldFixStatus::FixInvalidIdentifier)
     return "; cannot be fixed automatically";
   if (FixStatus == RenamerClangTidyCheck::ShouldFixStatus::ShouldFix)
     return {};
@@ -517,7 +521,6 @@ getDiagnosticSuffix(const RenamerClangTidyCheck::ShouldFixStatus FixStatus,
       RenamerClangTidyCheck::ShouldFixStatus::ConflictsWithMacroDefinition)
     return "; cannot be fixed because '" + Fixup +
            "' would conflict with a macro definition";
-
   llvm_unreachable("invalid ShouldFixStatus");
 }
 

diff  --git a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h
index aabb80133939..fd5b32075cbe 100644
--- a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h
+++ b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h
@@ -59,6 +59,9 @@ class RenamerClangTidyCheck : public ClangTidyCheck {
     /// automatically.
     ConflictsWithMacroDefinition,
 
+    /// The fixup results in an identifier that is not a valid c/c++ identifier.
+    FixInvalidIdentifier,
+
     /// Values pass this threshold will be ignored completely
     /// i.e no message, no fixup.
     IgnoreFailureThreshold,

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-bugfix-name-conflicts.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-bugfix-name-conflicts.cpp
index 12ddcf55c119..bee93c99976b 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-bugfix-name-conflicts.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-bugfix-name-conflicts.cpp
@@ -25,3 +25,13 @@ int func2(int Foo) {
 
   return 0;
 }
+
+int func3(int _0Bad) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: invalid case style for parameter '_0Bad'; cannot be fixed automatically [readability-identifier-naming]
+  // CHECK-FIXES: {{^}}int func3(int _0Bad) {{{$}}
+  if (_0Bad == 1) {
+    // CHECK-FIXES: {{^}}  if (_0Bad == 1) {{{$}}
+    return 2;
+  }
+  return 0;
+}


        


More information about the cfe-commits mailing list