11 #include "clang/AST/ASTContext.h" 12 #include "clang/Lex/Lexer.h" 22 IgnoreMacros(Options.getLocalOrGlobal(
"IgnoreMacros", true)) {}
27 Finder->addMatcher(typedefDecl(unless(isInstantiated())).bind(
"typedef"),
33 static bool CheckRemoval(SourceManager &SM, SourceLocation StartLoc,
34 ASTContext &Context) {
35 assert(StartLoc.isFileID() &&
"StartLoc must not be in a macro");
36 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(StartLoc);
37 StringRef File = SM.getBufferData(LocInfo.first);
38 const char *TokenBegin = File.data() + LocInfo.second;
39 Lexer DeclLexer(SM.getLocForStartOfFile(LocInfo.first), Context.getLangOpts(),
40 File.begin(), TokenBegin, File.end());
44 bool FoundTypedef =
false;
46 while (!DeclLexer.LexFromRawLexer(Tok) && !Tok.is(tok::semi)) {
47 switch (Tok.getKind()) {
59 if (ParenLevel == 0) {
65 case tok::raw_identifier:
66 if (Tok.getRawIdentifier() ==
"typedef") {
80 const auto *MatchedDecl = Result.Nodes.getNodeAs<TypedefDecl>(
"typedef");
81 if (MatchedDecl->getLocation().isInvalid())
84 auto &Context = *Result.Context;
85 auto &SM = *Result.SourceManager;
87 SourceLocation StartLoc = MatchedDecl->getBeginLoc();
89 if (StartLoc.isMacroID() && IgnoreMacros)
93 diag(StartLoc,
"use 'using' instead of 'typedef'");
96 if (MatchedDecl->getUnderlyingType()->isArrayType() || StartLoc.isMacroID())
101 printPolicy.SuppressScope =
true;
102 printPolicy.ConstantArraySizeAsWritten =
true;
103 printPolicy.UseVoidForZeroParams =
false;
105 Diag << FixItHint::CreateReplacement(
106 MatchedDecl->getSourceRange(),
107 "using " + MatchedDecl->getNameAsString() +
" = " +
108 MatchedDecl->getUnderlyingType().getAsString(printPolicy));
LangOptions getLangOpts() const
Returns the language options from the context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Base class for all clang-tidy checks.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static constexpr llvm::StringLiteral Name
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
static bool CheckRemoval(SourceManager &SM, SourceLocation StartLoc, ASTContext &Context)