38 auto IgnoreDestructorMatcher =
39 IgnoreDestructors ? cxxMethodDecl(unless(cxxDestructorDecl()))
41 auto IgnoreTemplateInstantiationsMatcher =
42 IgnoreTemplateInstantiations
43 ? cxxMethodDecl(unless(ast_matchers::isTemplateInstantiation()))
45 Finder->addMatcher(cxxMethodDecl(isOverride(),
46 IgnoreTemplateInstantiationsMatcher,
47 IgnoreDestructorMatcher)
55parseTokens(CharSourceRange Range,
const MatchFinder::MatchResult &Result) {
56 const SourceManager &Sources = *Result.SourceManager;
57 const std::pair<FileID, unsigned> LocInfo =
58 Sources.getDecomposedLoc(Range.getBegin());
59 const StringRef File = Sources.getBufferData(LocInfo.first);
60 const char *TokenBegin = File.data() + LocInfo.second;
61 Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first),
62 Result.Context->getLangOpts(), File.begin(), TokenBegin,
64 SmallVector<Token, 16> Tokens;
67 while (!RawLexer.LexFromRawLexer(Tok)) {
68 if ((Tok.is(tok::semi) || Tok.is(tok::l_brace)) && NestedParens == 0)
70 if (Sources.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation()))
72 if (Tok.is(tok::l_paren))
74 else if (Tok.is(tok::r_paren))
76 if (Tok.is(tok::raw_identifier)) {
77 IdentifierInfo &Info = Result.Context->Idents.get(StringRef(
78 Sources.getCharacterData(Tok.getLocation()), Tok.getLength()));
79 Tok.setIdentifierInfo(&Info);
80 Tok.setKind(Info.getTokenID());
82 Tokens.push_back(Tok);
88 const auto *Method = Result.Nodes.getNodeAs<FunctionDecl>(
"method");
89 const SourceManager &Sources = *Result.SourceManager;
91 ASTContext &Context = *Result.Context;
93 assert(Method !=
nullptr);
94 if (Method->getInstantiatedFromMemberFunction() !=
nullptr)
95 Method = Method->getInstantiatedFromMemberFunction();
97 if (Method->isImplicit() || Method->getLocation().isMacroID() ||
98 Method->isOutOfLine())
101 const bool HasVirtual = Method->isVirtualAsWritten();
102 const bool HasOverride = Method->getAttr<OverrideAttr>();
103 const bool HasFinal = Method->getAttr<FinalAttr>();
105 const bool OnlyVirtualSpecified = HasVirtual && !HasOverride && !HasFinal;
106 const unsigned KeywordCount = HasVirtual + HasOverride + HasFinal;
108 if ((!OnlyVirtualSpecified && KeywordCount == 1) ||
109 (!HasVirtual && HasOverride && HasFinal && AllowOverrideAndFinal))
113 if (OnlyVirtualSpecified) {
114 Message =
"prefer using '%0' or (rarely) '%1' instead of 'virtual'";
115 }
else if (KeywordCount == 0) {
116 Message =
"annotate this function with '%0' or (rarely) '%1'";
118 const StringRef Redundant =
119 HasVirtual ? (HasOverride && HasFinal && !AllowOverrideAndFinal
120 ?
"'virtual' and '%0' are"
123 const StringRef Correct = HasFinal ?
"'%1'" :
"'%0'";
125 Message = (llvm::Twine(Redundant) +
126 " redundant since the function is already declared " + Correct)
130 auto Diag = diag(Method->getLocation(),
Message)
131 << OverrideSpelling << FinalSpelling;
133 const CharSourceRange FileRange = Lexer::makeFileCharRange(
134 CharSourceRange::getTokenRange(Method->getSourceRange()), Sources,
137 if (!FileRange.isValid())
142 SmallVector<Token, 16> Tokens =
parseTokens(FileRange, Result);
145 if (!HasFinal && !HasOverride) {
148 if (OverrideSpelling !=
"override" &&
149 !Context.Idents.get(OverrideSpelling).hasMacroDefinition())
152 Diag << FixItHint::CreateInsertion(
153 Lexer::getLocForEndOfToken(
154 Method->getTypeSourceInfo()->getTypeLoc().getEndLoc(), 0, Sources,
156 (
" " + OverrideSpelling).str());
159 if (HasFinal && HasOverride && !AllowOverrideAndFinal)
160 Diag << FixItHint::CreateRemoval(
161 Method->getAttr<OverrideAttr>()->getLocation());
164 for (
const Token Tok : Tokens) {
165 if (Tok.is(tok::kw_virtual)) {
166 std::optional<Token> NextToken =
168 Tok.getEndLoc(), Sources, getLangOpts());
169 if (NextToken.has_value()) {
170 Diag << FixItHint::CreateRemoval(CharSourceRange::getCharRange(
171 Tok.getLocation(), NextToken->getLocation()));
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.