11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
15 using namespace clang::ast_matchers;
21 void UseOverrideCheck::registerMatchers(MatchFinder *
Finder) {
23 if (getLangOpts().CPlusPlus11)
24 Finder->addMatcher(cxxMethodDecl(isOverride()).bind(
"method"),
this);
29 static SmallVector<Token, 16>
31 const SourceManager &Sources = *Result.SourceManager;
32 std::pair<FileID, unsigned> LocInfo =
33 Sources.getDecomposedLoc(Range.getBegin());
34 StringRef
File = Sources.getBufferData(LocInfo.first);
35 const char *TokenBegin = File.data() + LocInfo.second;
36 Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first),
37 Result.Context->getLangOpts(), File.begin(), TokenBegin,
39 SmallVector<Token, 16>
Tokens;
42 while (!RawLexer.LexFromRawLexer(Tok)) {
43 if ((Tok.is(tok::semi) || Tok.is(tok::l_brace)) && NestedParens == 0)
45 if (Sources.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation()))
47 if (Tok.is(tok::l_paren))
49 else if (Tok.is(tok::r_paren))
51 if (Tok.is(tok::raw_identifier)) {
52 IdentifierInfo &Info = Result.Context->Idents.get(StringRef(
53 Sources.getCharacterData(Tok.getLocation()), Tok.getLength()));
54 Tok.setIdentifierInfo(&Info);
55 Tok.setKind(Info.getTokenID());
57 Tokens.push_back(Tok);
62 static StringRef
GetText(
const Token &Tok,
const SourceManager &Sources) {
63 return StringRef(Sources.getCharacterData(Tok.getLocation()),
67 void UseOverrideCheck::check(
const MatchFinder::MatchResult &Result) {
68 const auto *Method = Result.Nodes.getNodeAs<FunctionDecl>(
"method");
69 const SourceManager &Sources = *Result.SourceManager;
71 assert(Method !=
nullptr);
72 if (Method->getInstantiatedFromMemberFunction() !=
nullptr)
73 Method = Method->getInstantiatedFromMemberFunction();
75 if (Method->isImplicit() || Method->getLocation().isMacroID() ||
76 Method->isOutOfLine())
79 bool HasVirtual = Method->isVirtualAsWritten();
80 bool HasOverride = Method->getAttr<OverrideAttr>();
81 bool HasFinal = Method->getAttr<FinalAttr>();
83 bool OnlyVirtualSpecified = HasVirtual && !HasOverride && !HasFinal;
84 unsigned KeywordCount = HasVirtual + HasOverride + HasFinal;
86 if (!OnlyVirtualSpecified && KeywordCount == 1)
91 if (OnlyVirtualSpecified) {
93 "prefer using 'override' or (rarely) 'final' instead of 'virtual'";
94 }
else if (KeywordCount == 0) {
95 Message =
"annotate this function with 'override' or (rarely) 'final'";
98 HasVirtual ? (HasOverride && HasFinal ?
"'virtual' and 'override' are"
101 StringRef Correct = HasFinal ?
"'final'" :
"'override'";
103 Message = (llvm::Twine(Redundant) +
104 " redundant since the function is already declared " + Correct)
108 DiagnosticBuilder Diag = diag(Method->getLocation(),
Message);
110 CharSourceRange FileRange = Lexer::makeFileCharRange(
111 CharSourceRange::getTokenRange(Method->getSourceRange()), Sources,
114 if (!FileRange.isValid())
123 if (!HasFinal && !HasOverride) {
124 SourceLocation InsertLoc;
125 StringRef ReplacementText =
"override ";
126 SourceLocation MethodLoc = Method->getLocation();
128 for (Token T : Tokens) {
129 if (T.is(tok::kw___attribute) &&
130 !Sources.isBeforeInTranslationUnit(T.getLocation(), MethodLoc)) {
131 InsertLoc = T.getLocation();
136 if (Method->hasAttrs()) {
137 for (
const clang::Attr *A : Method->getAttrs()) {
138 if (!A->isImplicit() && !A->isInherited()) {
140 Sources.getExpansionLoc(A->getRange().getBegin());
141 if ((!InsertLoc.isValid() ||
142 Sources.isBeforeInTranslationUnit(Loc, InsertLoc)) &&
143 !Sources.isBeforeInTranslationUnit(Loc, MethodLoc))
149 if (InsertLoc.isInvalid() && Method->doesThisDeclarationHaveABody() &&
150 Method->getBody() && !Method->isDefaulted()) {
155 ReplacementText =
" override";
156 auto LastTokenIter = std::prev(Tokens.end());
159 if (LastTokenIter->is(tok::kw_try))
160 LastTokenIter = std::prev(LastTokenIter);
161 InsertLoc = LastTokenIter->getEndLoc();
164 if (!InsertLoc.isValid()) {
168 if (Tokens.size() > 2 && (
GetText(Tokens.back(), Sources) ==
"0" ||
169 Tokens.back().is(tok::kw_default) ||
170 Tokens.back().is(tok::kw_delete)) &&
171 GetText(Tokens[Tokens.size() - 2], Sources) ==
"=") {
172 InsertLoc = Tokens[Tokens.size() - 2].getLocation();
174 if ((Tokens[Tokens.size() - 2].getFlags() & Token::LeadingSpace) == 0)
175 ReplacementText =
" override ";
176 }
else if (
GetText(Tokens.back(), Sources) ==
"ABSTRACT") {
177 InsertLoc = Tokens.back().getLocation();
181 if (!InsertLoc.isValid()) {
182 InsertLoc = FileRange.getEnd();
183 ReplacementText =
" override";
185 Diag << FixItHint::CreateInsertion(InsertLoc, ReplacementText);
188 if (HasFinal && HasOverride) {
189 SourceLocation OverrideLoc = Method->getAttr<OverrideAttr>()->getLocation();
190 Diag << FixItHint::CreateRemoval(
191 CharSourceRange::getTokenRange(OverrideLoc, OverrideLoc));
195 for (Token Tok : Tokens) {
196 if (Tok.is(tok::kw_virtual)) {
197 Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
198 Tok.getLocation(), Tok.getLocation()));
SourceLocation Loc
'#' location in the include directive
std::unique_ptr< ast_matchers::MatchFinder > Finder
static const StringRef Message
static StringRef GetText(const Token &Tok, const SourceManager &Sources)
CharSourceRange Range
SourceRange for the file name.
static SmallVector< Token, 16 > ParseTokens(CharSourceRange Range, const MatchFinder::MatchResult &Result)