11 #include "clang/AST/ASTContext.h" 12 #include "clang/Lex/Lexer.h" 22 IgnoreMacros(Options.getLocalOrGlobal(
"IgnoreMacros", true)) {}
27 Finder->addMatcher(typedefDecl().bind(
"typedef"),
this);
32 static bool CheckRemoval(SourceManager &SM, SourceLocation StartLoc,
33 ASTContext &Context) {
34 assert(StartLoc.isFileID() &&
"StartLoc must not be in a macro");
35 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(StartLoc);
36 StringRef
File = SM.getBufferData(LocInfo.first);
37 const char *TokenBegin = File.data() + LocInfo.second;
38 Lexer DeclLexer(SM.getLocForStartOfFile(LocInfo.first), Context.getLangOpts(),
39 File.begin(), TokenBegin, File.end());
43 bool FoundTypedef =
false;
45 while (!DeclLexer.LexFromRawLexer(Tok) && !Tok.is(tok::semi)) {
46 switch (Tok.getKind()) {
58 if (ParenLevel == 0) {
64 case tok::raw_identifier:
65 if (Tok.getRawIdentifier() ==
"typedef") {
79 const auto *MatchedDecl = Result.Nodes.getNodeAs<TypedefDecl>(
"typedef");
80 if (MatchedDecl->getLocation().isInvalid())
83 auto &Context = *Result.Context;
84 auto &SM = *Result.SourceManager;
86 SourceLocation StartLoc = MatchedDecl->getLocStart();
88 if (StartLoc.isMacroID() && IgnoreMacros)
92 diag(StartLoc,
"use 'using' instead of 'typedef'");
95 if (MatchedDecl->getUnderlyingType()->isArrayType() || StartLoc.isMacroID())
100 printPolicy.SuppressScope =
true;
101 printPolicy.ConstantArraySizeAsWritten =
true;
102 printPolicy.UseVoidForZeroParams =
false;
104 Diag << FixItHint::CreateReplacement(
105 MatchedDecl->getSourceRange(),
106 "using " + MatchedDecl->getNameAsString() +
" = " +
107 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.
===– 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)