74 const auto *ParentDecl = Result.Nodes.getNodeAs<Decl>(
ParentDeclName);
77 const auto *ParentDeclStmt = Result.Nodes.getNodeAs<DeclStmt>(
DeclStmtName);
79 if (ParentDeclStmt->isSingleDecl())
80 ParentDecl = ParentDeclStmt->getSingleDecl();
83 ParentDeclStmt->getDeclGroup().getDeclGroup()
84 [ParentDeclStmt->getDeclGroup().getDeclGroup().size() - 1];
91 const SourceManager &SM = *Result.SourceManager;
92 const LangOptions &LO = getLangOpts();
96 const auto *MatchedTagDecl = Result.Nodes.getNodeAs<TagDecl>(
TagDeclName);
103 if (MatchedTagDecl->isThisDeclarationADefinition())
104 LastTagDeclRanges[ParentDecl] = MatchedTagDecl->getSourceRange();
108 const auto *MatchedDecl = Result.Nodes.getNodeAs<TypedefDecl>(
TypedefName);
109 if (MatchedDecl->getLocation().isInvalid())
112 const auto *ExternCDecl =
114 if (ExternCDecl && IgnoreExternC)
117 SourceLocation StartLoc = MatchedDecl->getBeginLoc();
119 if (StartLoc.isMacroID() && IgnoreMacros)
122 static constexpr llvm::StringLiteral UseUsingWarning =
123 "use 'using' instead of 'typedef'";
126 if (MatchedDecl->getUnderlyingType()->isArrayType() || StartLoc.isMacroID()) {
127 diag(StartLoc, UseUsingWarning);
131 const TypeLoc TL = MatchedDecl->getTypeSourceInfo()->getTypeLoc();
133 auto [Type, QualifierStr] = [MatchedDecl,
this, &TL, &SM,
134 &LO]() -> std::pair<std::string, std::string> {
135 SourceRange TypeRange = TL.getSourceRange();
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))
149 return {VerbatimType,
""};
152 StringRef ExtraReference =
"";
153 if (MainTypeEndLoc.isValid() && TypeRange.fullyContains(MainTypeEndLoc)) {
158 MatchedDecl->getLocation(), SM, LO, tok::TokenKind::star,
159 tok::TokenKind::amp, tok::TokenKind::comma,
160 tok::TokenKind::kw_typedef);
162 ExtraReference = Lexer::getSourceText(
163 CharSourceRange::getCharRange(Tok, Tok.getLocWithOffset(1)), SM, LO);
165 if (ExtraReference !=
"*" && ExtraReference !=
"&")
168 TypeRange.setEnd(MainTypeEndLoc);
171 Lexer::getSourceText(CharSourceRange::getTokenRange(TypeRange), SM, LO)
173 ExtraReference.str()};
175 StringRef Name = MatchedDecl->getName();
176 SourceRange ReplaceRange = MatchedDecl->getSourceRange();
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)) {
191 FirstTypedefType = Type;
192 FirstTypedefName = Name.str();
193 MainTypeEndLoc = TL.getEndLoc();
197 ReplaceRange.setBegin(LastReplacementEnd);
203 if (Type == FirstTypedefType && !QualifierStr.empty())
204 Type = FirstTypedefName;
207 if (!ReplaceRange.getEnd().isMacroID()) {
208 const SourceLocation::IntTy Offset =
209 MatchedDecl->getFunctionType() ? 0 : Name.size();
210 LastReplacementEnd = ReplaceRange.getEnd().getLocWithOffset(Offset);
213 auto Diag = diag(ReplaceRange.getBegin(), UseUsingWarning);
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));
226 std::string Replacement = (Using + Name +
" = " + Type + QualifierStr).str();
227 Diag << FixItHint::CreateReplacement(ReplaceRange, Replacement);
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.