clang-tools 22.0.0git
UseUsingCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 "../utils/LexerUtils.h"
11#include "clang/AST/DeclGroup.h"
12#include "clang/Basic/LangOptions.h"
13#include "clang/Basic/SourceLocation.h"
14#include "clang/Basic/SourceManager.h"
15#include "clang/Basic/TokenKinds.h"
16#include "clang/Lex/Lexer.h"
17#include <string>
18
19using namespace clang::ast_matchers;
20namespace {
21
22AST_MATCHER(clang::LinkageSpecDecl, isExternCLinkage) {
23 return Node.getLanguage() == clang::LinkageSpecLanguageIDs::C;
24}
25} // namespace
26
27namespace clang::tidy::modernize {
28
29static constexpr llvm::StringLiteral ExternCDeclName = "extern-c-decl";
30static constexpr llvm::StringLiteral ParentDeclName = "parent-decl";
31static constexpr llvm::StringLiteral TagDeclName = "tag-decl";
32static constexpr llvm::StringLiteral TypedefName = "typedef";
33static constexpr llvm::StringLiteral DeclStmtName = "decl-stmt";
34
36 : ClangTidyCheck(Name, Context),
37 IgnoreMacros(Options.get("IgnoreMacros", true)),
38 IgnoreExternC(Options.get("IgnoreExternC", false)) {}
39
41 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
42 Options.store(Opts, "IgnoreExternC", IgnoreExternC);
43}
44
45void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
46 Finder->addMatcher(
47 typedefDecl(
48 unless(isInstantiated()),
49 optionally(hasAncestor(
50 linkageSpecDecl(isExternCLinkage()).bind(ExternCDeclName))),
51 anyOf(hasParent(decl().bind(ParentDeclName)),
52 hasParent(declStmt().bind(DeclStmtName))))
53 .bind(TypedefName),
54 this);
55
56 // This matcher is used to find tag declarations in source code within
57 // typedefs. They appear in the AST just *prior* to the typedefs.
58 Finder->addMatcher(
59 tagDecl(
60 anyOf(allOf(unless(anyOf(isImplicit(),
61 classTemplateSpecializationDecl())),
62 anyOf(hasParent(decl().bind(ParentDeclName)),
63 hasParent(declStmt().bind(DeclStmtName)))),
64 // We want the parent of the ClassTemplateDecl, not the parent
65 // of the specialization.
66 classTemplateSpecializationDecl(hasAncestor(classTemplateDecl(
67 anyOf(hasParent(decl().bind(ParentDeclName)),
68 hasParent(declStmt().bind(DeclStmtName))))))))
69 .bind(TagDeclName),
70 this);
71}
72
73void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
74 const auto *ParentDecl = Result.Nodes.getNodeAs<Decl>(ParentDeclName);
75
76 if (!ParentDecl) {
77 const auto *ParentDeclStmt = Result.Nodes.getNodeAs<DeclStmt>(DeclStmtName);
78 if (ParentDeclStmt) {
79 if (ParentDeclStmt->isSingleDecl())
80 ParentDecl = ParentDeclStmt->getSingleDecl();
81 else
82 ParentDecl =
83 ParentDeclStmt->getDeclGroup().getDeclGroup()
84 [ParentDeclStmt->getDeclGroup().getDeclGroup().size() - 1];
85 }
86 }
87
88 if (!ParentDecl)
89 return;
90
91 const SourceManager &SM = *Result.SourceManager;
92 const LangOptions &LO = getLangOpts();
93
94 // Match CXXRecordDecl only to store the range of the last non-implicit full
95 // declaration, to later check whether it's within the typedef itself.
96 const auto *MatchedTagDecl = Result.Nodes.getNodeAs<TagDecl>(TagDeclName);
97 if (MatchedTagDecl) {
98 // It is not sufficient to just track the last TagDecl that we've seen,
99 // because if one struct or union is nested inside another, the last TagDecl
100 // before the typedef will be the nested one (PR#50990). Therefore, we also
101 // keep track of the parent declaration, so that we can look up the last
102 // TagDecl that is a sibling of the typedef in the AST.
103 if (MatchedTagDecl->isThisDeclarationADefinition())
104 LastTagDeclRanges[ParentDecl] = MatchedTagDecl->getSourceRange();
105 return;
106 }
107
108 const auto *MatchedDecl = Result.Nodes.getNodeAs<TypedefDecl>(TypedefName);
109 if (MatchedDecl->getLocation().isInvalid())
110 return;
111
112 const auto *ExternCDecl =
113 Result.Nodes.getNodeAs<LinkageSpecDecl>(ExternCDeclName);
114 if (ExternCDecl && IgnoreExternC)
115 return;
116
117 SourceLocation StartLoc = MatchedDecl->getBeginLoc();
118
119 if (StartLoc.isMacroID() && IgnoreMacros)
120 return;
121
122 static constexpr llvm::StringLiteral UseUsingWarning =
123 "use 'using' instead of 'typedef'";
124
125 // Warn at StartLoc but do not fix if there is macro or array.
126 if (MatchedDecl->getUnderlyingType()->isArrayType() || StartLoc.isMacroID()) {
127 diag(StartLoc, UseUsingWarning);
128 return;
129 }
130
131 const TypeLoc TL = MatchedDecl->getTypeSourceInfo()->getTypeLoc();
132
133 auto [Type, QualifierStr] = [MatchedDecl, this, &TL, &SM,
134 &LO]() -> std::pair<std::string, std::string> {
135 SourceRange TypeRange = TL.getSourceRange();
136
137 // Function pointer case, get the left and right side of the identifier
138 // without the identifier.
139 if (TypeRange.fullyContains(MatchedDecl->getLocation())) {
140 const auto RangeLeftOfIdentifier = CharSourceRange::getCharRange(
141 TypeRange.getBegin(), MatchedDecl->getLocation());
142 const auto RangeRightOfIdentifier = CharSourceRange::getCharRange(
143 Lexer::getLocForEndOfToken(MatchedDecl->getLocation(), 0, SM, LO),
144 Lexer::getLocForEndOfToken(TypeRange.getEnd(), 0, SM, LO));
145 const std::string VerbatimType =
146 (Lexer::getSourceText(RangeLeftOfIdentifier, SM, LO) +
147 Lexer::getSourceText(RangeRightOfIdentifier, SM, LO))
148 .str();
149 return {VerbatimType, ""};
150 }
151
152 StringRef ExtraReference = "";
153 if (MainTypeEndLoc.isValid() && TypeRange.fullyContains(MainTypeEndLoc)) {
154 // Each type introduced in a typedef can specify being a reference or
155 // pointer type separately, so we need to figure out if the new using-decl
156 // needs to be to a reference or pointer as well.
157 const SourceLocation Tok = utils::lexer::findPreviousAnyTokenKind(
158 MatchedDecl->getLocation(), SM, LO, tok::TokenKind::star,
159 tok::TokenKind::amp, tok::TokenKind::comma,
160 tok::TokenKind::kw_typedef);
161
162 ExtraReference = Lexer::getSourceText(
163 CharSourceRange::getCharRange(Tok, Tok.getLocWithOffset(1)), SM, LO);
164
165 if (ExtraReference != "*" && ExtraReference != "&")
166 ExtraReference = "";
167
168 TypeRange.setEnd(MainTypeEndLoc);
169 }
170 return {
171 Lexer::getSourceText(CharSourceRange::getTokenRange(TypeRange), SM, LO)
172 .str(),
173 ExtraReference.str()};
174 }();
175 StringRef Name = MatchedDecl->getName();
176 SourceRange ReplaceRange = MatchedDecl->getSourceRange();
177
178 // typedefs with multiple comma-separated definitions produce multiple
179 // consecutive TypedefDecl nodes whose SourceRanges overlap. Each range starts
180 // at the "typedef" and then continues *across* previous definitions through
181 // the end of the current TypedefDecl definition.
182 // But also we need to check that the ranges belong to the same file because
183 // different files may contain overlapping ranges.
184 std::string Using = "using ";
185 if (ReplaceRange.getBegin().isMacroID() ||
186 (Result.SourceManager->getFileID(ReplaceRange.getBegin()) !=
187 Result.SourceManager->getFileID(LastReplacementEnd)) ||
188 (ReplaceRange.getBegin() >= LastReplacementEnd)) {
189 // This is the first (and possibly the only) TypedefDecl in a typedef. Save
190 // Type and Name in case we find subsequent TypedefDecl's in this typedef.
191 FirstTypedefType = Type;
192 FirstTypedefName = Name.str();
193 MainTypeEndLoc = TL.getEndLoc();
194 } else {
195 // This is additional TypedefDecl in a comma-separated typedef declaration.
196 // Start replacement *after* prior replacement and separate with semicolon.
197 ReplaceRange.setBegin(LastReplacementEnd);
198 Using = ";\nusing ";
199
200 // If this additional TypedefDecl's Type starts with the first TypedefDecl's
201 // type, make this using statement refer back to the first type, e.g. make
202 // "typedef int Foo, *Foo_p;" -> "using Foo = int;\nusing Foo_p = Foo*;"
203 if (Type == FirstTypedefType && !QualifierStr.empty())
204 Type = FirstTypedefName;
205 }
206
207 if (!ReplaceRange.getEnd().isMacroID()) {
208 const SourceLocation::IntTy Offset =
209 MatchedDecl->getFunctionType() ? 0 : Name.size();
210 LastReplacementEnd = ReplaceRange.getEnd().getLocWithOffset(Offset);
211 }
212
213 auto Diag = diag(ReplaceRange.getBegin(), UseUsingWarning);
214
215 // If typedef contains a full tag declaration, extract its full text.
216 auto LastTagDeclRange = LastTagDeclRanges.find(ParentDecl);
217 if (LastTagDeclRange != LastTagDeclRanges.end() &&
218 LastTagDeclRange->second.isValid() &&
219 ReplaceRange.fullyContains(LastTagDeclRange->second)) {
220 Type = std::string(Lexer::getSourceText(
221 CharSourceRange::getTokenRange(LastTagDeclRange->second), SM, LO));
222 if (Type.empty())
223 return;
224 }
225
226 std::string Replacement = (Using + Name + " = " + Type + QualifierStr).str();
227 Diag << FixItHint::CreateReplacement(ReplaceRange, Replacement);
228}
229} // namespace clang::tidy::modernize
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
UseUsingCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_MATCHER(BinaryOperator, isRelationalOperator)
static constexpr llvm::StringLiteral DeclStmtName
static constexpr llvm::StringLiteral ParentDeclName
static constexpr llvm::StringLiteral ExternCDeclName
static constexpr llvm::StringLiteral TagDeclName
static constexpr llvm::StringLiteral TypedefName
SourceLocation findPreviousAnyTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, TokenKind TK, TokenKinds... TKs)
Definition LexerUtils.h:44
llvm::StringMap< ClangTidyValue > OptionMap