1 //===--- UseUsingCheck.cpp - clang-tidy------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "UseUsingCheck.h" 10 #include "clang/AST/ASTContext.h" 11 #include "clang/Lex/Lexer.h" 12 13 using namespace clang::ast_matchers; 14 15 namespace clang { 16 namespace tidy { 17 namespace modernize { 18 19 UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context) 20 : ClangTidyCheck(Name, Context), 21 IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {} 22 23 void UseUsingCheck::registerMatchers(MatchFinder *Finder) { 24 if (!getLangOpts().CPlusPlus11) 25 return; 26 Finder->addMatcher(typedefDecl(unless(isInstantiated())).bind("typedef"), 27 this); 28 // This matcher used to find tag declarations in source code within typedefs. 29 // They appear in the AST just *prior* to the typedefs. 30 Finder->addMatcher(tagDecl(unless(isImplicit())).bind("tagdecl"), this); 31 } 32 33 void UseUsingCheck::check(const MatchFinder::MatchResult &Result) { 34 // Match CXXRecordDecl only to store the range of the last non-implicit full 35 // declaration, to later check whether it's within the typdef itself. 36 const auto *MatchedTagDecl = Result.Nodes.getNodeAs<TagDecl>("tagdecl"); 37 if (MatchedTagDecl) { 38 LastTagDeclRange = MatchedTagDecl->getSourceRange(); 39 return; 40 } 41 42 const auto *MatchedDecl = Result.Nodes.getNodeAs<TypedefDecl>("typedef"); 43 if (MatchedDecl->getLocation().isInvalid()) 44 return; 45 46 SourceLocation StartLoc = MatchedDecl->getBeginLoc(); 47 48 if (StartLoc.isMacroID() && IgnoreMacros) 49 return; 50 51 static const char *UseUsingWarning = "use 'using' instead of 'typedef'"; 52 53 // Warn at StartLoc but do not fix if there is macro or array. 54 if (MatchedDecl->getUnderlyingType()->isArrayType() || StartLoc.isMacroID()) { 55 diag(StartLoc, UseUsingWarning); 56 return; 57 } 58 59 auto printPolicy = PrintingPolicy(getLangOpts()); 60 printPolicy.SuppressScope = true; 61 printPolicy.ConstantArraySizeAsWritten = true; 62 printPolicy.UseVoidForZeroParams = false; 63 64 std::string Type = MatchedDecl->getUnderlyingType().getAsString(printPolicy); 65 std::string Name = MatchedDecl->getNameAsString(); 66 SourceRange ReplaceRange = MatchedDecl->getSourceRange(); 67 68 // typedefs with multiple comma-separated definitions produce multiple 69 // consecutive TypedefDecl nodes whose SourceRanges overlap. Each range starts 70 // at the "typedef" and then continues *across* previous definitions through 71 // the end of the current TypedefDecl definition. 72 // But also we need to check that the ranges belong to the same file because 73 // different files may contain overlapping ranges. 74 std::string Using = "using "; 75 if (ReplaceRange.getBegin().isMacroID() || 76 (Result.SourceManager->getFileID(ReplaceRange.getBegin()) != 77 Result.SourceManager->getFileID(LastReplacementEnd)) || 78 (ReplaceRange.getBegin() >= LastReplacementEnd)) { 79 // This is the first (and possibly the only) TypedefDecl in a typedef. Save 80 // Type and Name in case we find subsequent TypedefDecl's in this typedef. 81 FirstTypedefType = Type; 82 FirstTypedefName = Name; 83 } else { 84 // This is additional TypedefDecl in a comma-separated typedef declaration. 85 // Start replacement *after* prior replacement and separate with semicolon. 86 ReplaceRange.setBegin(LastReplacementEnd); 87 Using = ";\nusing "; 88 89 // If this additional TypedefDecl's Type starts with the first TypedefDecl's 90 // type, make this using statement refer back to the first type, e.g. make 91 // "typedef int Foo, *Foo_p;" -> "using Foo = int;\nusing Foo_p = Foo*;" 92 if (Type.size() > FirstTypedefType.size() && 93 Type.substr(0, FirstTypedefType.size()) == FirstTypedefType) 94 Type = FirstTypedefName + Type.substr(FirstTypedefType.size() + 1); 95 } 96 if (!ReplaceRange.getEnd().isMacroID()) 97 LastReplacementEnd = ReplaceRange.getEnd().getLocWithOffset(Name.size()); 98 99 auto Diag = diag(ReplaceRange.getBegin(), UseUsingWarning); 100 101 // If typedef contains a full tag declaration, extract its full text. 102 if (LastTagDeclRange.isValid() && 103 ReplaceRange.fullyContains(LastTagDeclRange)) { 104 bool Invalid; 105 Type = std::string( 106 Lexer::getSourceText(CharSourceRange::getTokenRange(LastTagDeclRange), 107 *Result.SourceManager, getLangOpts(), &Invalid)); 108 if (Invalid) 109 return; 110 } 111 112 std::string Replacement = Using + Name + " = " + Type; 113 Diag << FixItHint::CreateReplacement(ReplaceRange, Replacement); 114 } 115 116 } // namespace modernize 117 } // namespace tidy 118 } // namespace clang 119