10 #include "clang/AST/ASTContext.h"
11 #include "clang/Lex/Lexer.h"
21 IgnoreMacros(Options.getLocalOrGlobal(
"IgnoreMacros", true)) {}
28 Finder->addMatcher(typedefDecl(unless(isInstantiated())).bind(
"typedef"),
32 Finder->addMatcher(tagDecl(unless(isImplicit())).bind(
"tagdecl"),
this);
38 const auto *MatchedTagDecl = Result.Nodes.getNodeAs<TagDecl>(
"tagdecl");
40 LastTagDeclRange = MatchedTagDecl->getSourceRange();
44 const auto *MatchedDecl = Result.Nodes.getNodeAs<TypedefDecl>(
"typedef");
45 if (MatchedDecl->getLocation().isInvalid())
48 SourceLocation StartLoc = MatchedDecl->getBeginLoc();
50 if (StartLoc.isMacroID() && IgnoreMacros)
53 static const char *UseUsingWarning =
"use 'using' instead of 'typedef'";
56 if (MatchedDecl->getUnderlyingType()->isArrayType() || StartLoc.isMacroID()) {
57 diag(StartLoc, UseUsingWarning);
62 printPolicy.SuppressScope =
true;
63 printPolicy.ConstantArraySizeAsWritten =
true;
64 printPolicy.UseVoidForZeroParams =
false;
65 printPolicy.PrintInjectedClassNameWithArguments =
false;
67 std::string
Type = MatchedDecl->getUnderlyingType().getAsString(printPolicy);
68 std::string
Name = MatchedDecl->getNameAsString();
69 SourceRange ReplaceRange = MatchedDecl->getSourceRange();
77 std::string Using =
"using ";
78 if (ReplaceRange.getBegin().isMacroID() ||
79 (Result.SourceManager->getFileID(ReplaceRange.getBegin()) !=
80 Result.SourceManager->getFileID(LastReplacementEnd)) ||
81 (ReplaceRange.getBegin() >= LastReplacementEnd)) {
84 FirstTypedefType =
Type;
85 FirstTypedefName =
Name;
89 ReplaceRange.setBegin(LastReplacementEnd);
95 if (
Type.size() > FirstTypedefType.size() &&
96 Type.substr(0, FirstTypedefType.size()) == FirstTypedefType)
97 Type = FirstTypedefName +
Type.substr(FirstTypedefType.size() + 1);
99 if (!ReplaceRange.getEnd().isMacroID())
100 LastReplacementEnd = ReplaceRange.getEnd().getLocWithOffset(
Name.size());
102 auto Diag =
diag(ReplaceRange.getBegin(), UseUsingWarning);
105 if (LastTagDeclRange.isValid() &&
106 ReplaceRange.fullyContains(LastTagDeclRange)) {
109 Lexer::getSourceText(CharSourceRange::getTokenRange(LastTagDeclRange),
115 std::string Replacement = Using +
Name +
" = " +
Type;
116 Diag << FixItHint::CreateReplacement(ReplaceRange, Replacement);