diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.h b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.h --- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.h +++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.h @@ -23,7 +23,7 @@ const bool IgnoreMacros; SourceLocation LastReplacementEnd; - SourceRange LastCxxDeclRange; + SourceRange LastTagDeclRange; std::string FirstTypedefType; std::string FirstTypedefName; diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp --- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp @@ -25,18 +25,17 @@ 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("struct"); - if (MatchedCxxRecordDecl) { - LastCxxDeclRange = MatchedCxxRecordDecl->getSourceRange(); + const auto *MatchedTagDecl = Result.Nodes.getNodeAs("tagdecl"); + if (MatchedTagDecl) { + LastTagDeclRange = MatchedTagDecl->getSourceRange(); return; } @@ -72,7 +71,8 @@ // the end of the current TypedefDecl definition. 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 +95,11 @@ 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/test/clang-tidy/checkers/modernize-use-using.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp @@ -267,3 +267,7 @@ // 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 { ea, eb } EnumT; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef' +// CHECK-FIXES: using EnumT = enum { ea, eb };