xref: /llvm-project/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp (revision 145dcef8bdf9da9684c9c4e34a8e6c45baafab74)
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   printPolicy.PrintInjectedClassNameWithArguments = false;
62 
63   std::string Type = MatchedDecl->getUnderlyingType().getAsString(printPolicy);
64   std::string Name = MatchedDecl->getNameAsString();
65   SourceRange ReplaceRange = MatchedDecl->getSourceRange();
66 
67   // typedefs with multiple comma-separated definitions produce multiple
68   // consecutive TypedefDecl nodes whose SourceRanges overlap. Each range starts
69   // at the "typedef" and then continues *across* previous definitions through
70   // the end of the current TypedefDecl definition.
71   // But also we need to check that the ranges belong to the same file because
72   // different files may contain overlapping ranges.
73   std::string Using = "using ";
74   if (ReplaceRange.getBegin().isMacroID() ||
75       (Result.SourceManager->getFileID(ReplaceRange.getBegin()) !=
76        Result.SourceManager->getFileID(LastReplacementEnd)) ||
77       (ReplaceRange.getBegin() >= LastReplacementEnd)) {
78     // This is the first (and possibly the only) TypedefDecl in a typedef. Save
79     // Type and Name in case we find subsequent TypedefDecl's in this typedef.
80     FirstTypedefType = Type;
81     FirstTypedefName = Name;
82   } else {
83     // This is additional TypedefDecl in a comma-separated typedef declaration.
84     // Start replacement *after* prior replacement and separate with semicolon.
85     ReplaceRange.setBegin(LastReplacementEnd);
86     Using = ";\nusing ";
87 
88     // If this additional TypedefDecl's Type starts with the first TypedefDecl's
89     // type, make this using statement refer back to the first type, e.g. make
90     // "typedef int Foo, *Foo_p;" -> "using Foo = int;\nusing Foo_p = Foo*;"
91     if (Type.size() > FirstTypedefType.size() &&
92         Type.substr(0, FirstTypedefType.size()) == FirstTypedefType)
93       Type = FirstTypedefName + Type.substr(FirstTypedefType.size() + 1);
94   }
95   if (!ReplaceRange.getEnd().isMacroID())
96     LastReplacementEnd = ReplaceRange.getEnd().getLocWithOffset(Name.size());
97 
98   auto Diag = diag(ReplaceRange.getBegin(), UseUsingWarning);
99 
100   // If typedef contains a full tag declaration, extract its full text.
101   if (LastTagDeclRange.isValid() &&
102       ReplaceRange.fullyContains(LastTagDeclRange)) {
103     bool Invalid;
104     Type = std::string(
105         Lexer::getSourceText(CharSourceRange::getTokenRange(LastTagDeclRange),
106                              *Result.SourceManager, getLangOpts(), &Invalid));
107     if (Invalid)
108       return;
109   }
110 
111   std::string Replacement = Using + Name + " = " + Type;
112   Diag << FixItHint::CreateReplacement(ReplaceRange, Replacement);
113 }
114 
115 } // namespace modernize
116 } // namespace tidy
117 } // namespace clang
118