[clang-tools-extra] 6423ae4 - Allow modernize-use-using to apply to enumerations as well.

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 3 04:54:47 PST 2020


Author: Karasev Nikita
Date: 2020-02-03T07:54:38-05:00
New Revision: 6423ae417e17611c4ee529f5848e839e6d9cb795

URL: https://github.com/llvm/llvm-project/commit/6423ae417e17611c4ee529f5848e839e6d9cb795
DIFF: https://github.com/llvm/llvm-project/commit/6423ae417e17611c4ee529f5848e839e6d9cb795.diff

LOG: Allow modernize-use-using to apply to enumerations as well.

This addresses PR44528.

Added: 
    clang-tools-extra/test/clang-tidy/checkers/Inputs/modernize-use-using/modernize-use-using.h

Modified: 
    clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
    clang-tools-extra/clang-tidy/modernize/UseUsingCheck.h
    clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index 164c9fe9bae4..918b4846bf3a 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -25,18 +25,17 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
     return;
   Finder->addMatcher(typedefDecl(unless(isInstantiated())).bind("typedef"),
                      this);
-  // This matcher used to find structs defined in source code within typedefs.
+  // This matcher used to find tag declarations in source code within typedefs.
   // They appear in the AST just *prior* to the typedefs.
-  Finder->addMatcher(cxxRecordDecl(unless(isImplicit())).bind("struct"), this);
+  Finder->addMatcher(tagDecl(unless(isImplicit())).bind("tagdecl"), this);
 }
 
 void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
   // Match CXXRecordDecl only to store the range of the last non-implicit full
   // declaration, to later check whether it's within the typdef itself.
-  const auto *MatchedCxxRecordDecl =
-      Result.Nodes.getNodeAs<CXXRecordDecl>("struct");
-  if (MatchedCxxRecordDecl) {
-    LastCxxDeclRange = MatchedCxxRecordDecl->getSourceRange();
+  const auto *MatchedTagDecl = Result.Nodes.getNodeAs<TagDecl>("tagdecl");
+  if (MatchedTagDecl) {
+    LastTagDeclRange = MatchedTagDecl->getSourceRange();
     return;
   }
 
@@ -70,9 +69,13 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
   // consecutive TypedefDecl nodes whose SourceRanges overlap. Each range starts
   // at the "typedef" and then continues *across* previous definitions through
   // the end of the current TypedefDecl definition.
+  // But also we need to check that the ranges belong to the same file because
+  // 
diff erent files may contain overlapping ranges.
   std::string Using = "using ";
   if (ReplaceRange.getBegin().isMacroID() ||
-      ReplaceRange.getBegin() >= LastReplacementEnd) {
+      (Result.SourceManager->getFileID(ReplaceRange.getBegin()) !=
+       Result.SourceManager->getFileID(LastReplacementEnd)) ||
+      (ReplaceRange.getBegin() >= LastReplacementEnd)) {
     // This is the first (and possibly the only) TypedefDecl in a typedef. Save
     // Type and Name in case we find subsequent TypedefDecl's in this typedef.
     FirstTypedefType = Type;
@@ -95,11 +98,12 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
 
   auto Diag = diag(ReplaceRange.getBegin(), UseUsingWarning);
 
-  // If typedef contains a full struct/class declaration, extract its full text.
-  if (LastCxxDeclRange.isValid() && ReplaceRange.fullyContains(LastCxxDeclRange)) {
+  // If typedef contains a full tag declaration, extract its full text.
+  if (LastTagDeclRange.isValid() &&
+      ReplaceRange.fullyContains(LastTagDeclRange)) {
     bool Invalid;
     Type = std::string(
-        Lexer::getSourceText(CharSourceRange::getTokenRange(LastCxxDeclRange),
+        Lexer::getSourceText(CharSourceRange::getTokenRange(LastTagDeclRange),
                              *Result.SourceManager, getLangOpts(), &Invalid));
     if (Invalid)
       return;

diff  --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.h b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.h
index f1899da7124b..616bc27947cf 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.h
@@ -23,7 +23,7 @@ class UseUsingCheck : public ClangTidyCheck {
 
   const bool IgnoreMacros;
   SourceLocation LastReplacementEnd;
-  SourceRange LastCxxDeclRange;
+  SourceRange LastTagDeclRange;
   std::string FirstTypedefType;
   std::string FirstTypedefName;
 

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/Inputs/modernize-use-using/modernize-use-using.h b/clang-tools-extra/test/clang-tidy/checkers/Inputs/modernize-use-using/modernize-use-using.h
new file mode 100644
index 000000000000..c82be21fcf1e
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/Inputs/modernize-use-using/modernize-use-using.h
@@ -0,0 +1,6 @@
+#ifndef MODERNIZE_USE_USING_H
+#define MODERNIZE_USE_USING_H
+
+typedef int mytype;
+
+#endif

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp
index 843f8943ba30..eb3316a4a04e 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s modernize-use-using %t
+// RUN: %check_clang_tidy %s modernize-use-using %t -- -- -I %S/Inputs/modernize-use-using/
 
 typedef int Type;
 // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef' [modernize-use-using]
@@ -267,3 +267,14 @@ typedef struct { int a; } R_t, *R_p;
 // CHECK-MESSAGES: :[[@LINE-2]]:30: warning: use 'using' instead of 'typedef'
 // CHECK-FIXES: using R_t = struct { int a; };
 // CHECK-FIXES-NEXT: using R_p = R_t*;
+
+typedef enum { ea1, eb1 } EnumT1;
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
+// CHECK-FIXES: using EnumT1 = enum { ea1, eb1 };
+
+#include "modernize-use-using.h"
+
+typedef enum { ea2, eb2 } EnumT2_CheckTypedefImpactFromAnotherFile;
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
+// CHECK-FIXES: using EnumT2_CheckTypedefImpactFromAnotherFile = enum { ea2, eb2 };
+


        


More information about the cfe-commits mailing list