10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
20 IgnoreDestructors(Options.get(
"IgnoreDestructors", false)),
21 AllowOverrideAndFinal(Options.get(
"AllowOverrideAndFinal", false)),
22 OverrideSpelling(Options.get(
"OverrideSpelling",
"override")),
23 FinalSpelling(Options.get(
"FinalSpelling",
"final")) {}
26 Options.
store(Opts,
"IgnoreDestructors", IgnoreDestructors);
27 Options.
store(Opts,
"AllowOverrideAndFinal", AllowOverrideAndFinal);
28 Options.
store(Opts,
"OverrideSpelling", OverrideSpelling);
33 if (IgnoreDestructors)
35 cxxMethodDecl(isOverride(), unless(cxxDestructorDecl())).bind(
"method"),
38 Finder->addMatcher(cxxMethodDecl(isOverride()).bind(
"method"),
this);
43static SmallVector<Token, 16>
44parseTokens(CharSourceRange Range,
const MatchFinder::MatchResult &Result) {
45 const SourceManager &Sources = *Result.SourceManager;
46 std::pair<FileID, unsigned> LocInfo =
47 Sources.getDecomposedLoc(
Range.getBegin());
48 StringRef File = Sources.getBufferData(LocInfo.first);
49 const char *TokenBegin = File.data() + LocInfo.second;
50 Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first),
51 Result.Context->getLangOpts(), File.begin(), TokenBegin,
53 SmallVector<Token, 16> Tokens;
56 while (!RawLexer.LexFromRawLexer(Tok)) {
57 if ((Tok.is(tok::semi) || Tok.is(tok::l_brace)) && NestedParens == 0)
59 if (Sources.isBeforeInTranslationUnit(
Range.getEnd(), Tok.getLocation()))
61 if (Tok.is(tok::l_paren))
63 else if (Tok.is(tok::r_paren))
65 if (Tok.is(tok::raw_identifier)) {
66 IdentifierInfo &
Info = Result.Context->Idents.get(StringRef(
67 Sources.getCharacterData(Tok.getLocation()), Tok.getLength()));
68 Tok.setIdentifierInfo(&
Info);
69 Tok.setKind(
Info.getTokenID());
71 Tokens.push_back(Tok);
76static StringRef
getText(
const Token &Tok,
const SourceManager &Sources) {
77 return StringRef(Sources.getCharacterData(Tok.getLocation()),
82 const auto *Method = Result.Nodes.getNodeAs<FunctionDecl>(
"method");
83 const SourceManager &Sources = *Result.SourceManager;
85 ASTContext &Context = *Result.Context;
87 assert(Method !=
nullptr);
88 if (Method->getInstantiatedFromMemberFunction() !=
nullptr)
89 Method = Method->getInstantiatedFromMemberFunction();
91 if (Method->isImplicit() || Method->getLocation().isMacroID() ||
92 Method->isOutOfLine())
95 bool HasVirtual = Method->isVirtualAsWritten();
96 bool HasOverride = Method->getAttr<OverrideAttr>();
97 bool HasFinal = Method->getAttr<FinalAttr>();
99 bool OnlyVirtualSpecified = HasVirtual && !HasOverride && !HasFinal;
100 unsigned KeywordCount = HasVirtual + HasOverride + HasFinal;
102 if ((!OnlyVirtualSpecified && KeywordCount == 1) ||
103 (!HasVirtual && HasOverride && HasFinal && AllowOverrideAndFinal))
107 if (OnlyVirtualSpecified) {
108 Message =
"prefer using '%0' or (rarely) '%1' instead of 'virtual'";
109 }
else if (KeywordCount == 0) {
110 Message =
"annotate this function with '%0' or (rarely) '%1'";
112 StringRef Redundant =
113 HasVirtual ? (HasOverride && HasFinal && !AllowOverrideAndFinal
114 ?
"'virtual' and '%0' are"
117 StringRef Correct = HasFinal ?
"'%1'" :
"'%0'";
119 Message = (llvm::Twine(Redundant) +
120 " redundant since the function is already declared " + Correct)
125 << OverrideSpelling << FinalSpelling;
127 CharSourceRange FileRange = Lexer::makeFileCharRange(
128 CharSourceRange::getTokenRange(Method->getSourceRange()), Sources,
131 if (!FileRange.isValid())
137 SmallVector<Token, 16> Tokens =
parseTokens(FileRange, Result);
140 if (!HasFinal && !HasOverride) {
141 SourceLocation InsertLoc;
142 std::string ReplacementText = (OverrideSpelling +
" ").str();
143 SourceLocation MethodLoc = Method->getLocation();
145 for (Token T : Tokens) {
146 if (T.is(tok::kw___attribute) &&
147 !Sources.isBeforeInTranslationUnit(T.getLocation(), MethodLoc)) {
148 InsertLoc = T.getLocation();
153 if (Method->hasAttrs()) {
154 for (
const clang::Attr *A : Method->getAttrs()) {
155 if (!A->isImplicit() && !A->isInherited()) {
157 Sources.getExpansionLoc(A->getRange().getBegin());
158 if ((!InsertLoc.isValid() ||
159 Sources.isBeforeInTranslationUnit(
Loc, InsertLoc)) &&
160 !Sources.isBeforeInTranslationUnit(
Loc, MethodLoc))
166 if (InsertLoc.isInvalid() && Method->doesThisDeclarationHaveABody() &&
167 Method->getBody() && !Method->isDefaulted()) {
172 ReplacementText = (
" " + OverrideSpelling).str();
173 auto *LastTokenIter = std::prev(Tokens.end());
176 if (LastTokenIter->is(tok::kw_try))
177 LastTokenIter = std::prev(LastTokenIter);
178 InsertLoc = LastTokenIter->getEndLoc();
181 if (!InsertLoc.isValid()) {
185 if (Tokens.size() > 2 &&
186 (
getText(Tokens.back(), Sources) ==
"0" ||
187 Tokens.back().is(tok::kw_default) ||
188 Tokens.back().is(tok::kw_delete)) &&
189 getText(Tokens[Tokens.size() - 2], Sources) ==
"=") {
190 InsertLoc = Tokens[Tokens.size() - 2].getLocation();
192 if ((Tokens[Tokens.size() - 2].getFlags() & Token::LeadingSpace) == 0)
193 ReplacementText = (
" " + OverrideSpelling +
" ").str();
194 }
else if (
getText(Tokens.back(), Sources) ==
"ABSTRACT")
195 InsertLoc = Tokens.back().getLocation();
198 if (!InsertLoc.isValid()) {
199 InsertLoc = FileRange.getEnd();
200 ReplacementText = (
" " + OverrideSpelling).str();
205 if (OverrideSpelling !=
"override" &&
206 !Context.Idents.get(OverrideSpelling).hasMacroDefinition())
209 Diag << FixItHint::CreateInsertion(InsertLoc, ReplacementText);
212 if (HasFinal && HasOverride && !AllowOverrideAndFinal) {
213 SourceLocation OverrideLoc = Method->getAttr<OverrideAttr>()->getLocation();
214 Diag << FixItHint::CreateRemoval(
215 CharSourceRange::getTokenRange(OverrideLoc, OverrideLoc));
219 for (Token Tok : Tokens) {
220 if (Tok.is(tok::kw_virtual)) {
221 Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
222 Tok.getLocation(), Tok.getLocation()));
CharSourceRange Range
SourceRange for the file name.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
UseOverrideCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
static StringRef getText(const Token &Tok, const SourceManager &Sources)
static SmallVector< Token, 16 > parseTokens(CharSourceRange Range, const MatchFinder::MatchResult &Result)
constexpr llvm::StringLiteral Message
llvm::StringMap< ClangTidyValue > OptionMap