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