10#include "../utils/LexerUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Basic/SourceLocation.h"
14#include "clang/Lex/Lexer.h"
25static std::optional<Token>
27 const MatchFinder::MatchResult &Result) {
28 if (!Def->getReturnType().isLocalConstQualified())
35 SourceLocation NameBeginLoc = Def->getQualifier()
36 ? Def->getQualifierLoc().getBeginLoc()
41 CharSourceRange FileRange = Lexer::makeFileCharRange(
42 CharSourceRange::getCharRange(Def->getBeginLoc(), NameBeginLoc),
43 *Result.SourceManager, Result.Context->getLangOpts());
45 if (FileRange.isInvalid())
49 tok::kw_const, FileRange, *Result.Context, *Result.SourceManager);
55 return Node.isLocalConstQualified();
63 llvm::SmallVector<clang::FixItHint, 4>
Hints;
66 llvm::SmallVector<clang::SourceLocation, 4>
DeclLocs;
72static CheckResult
checkDef(
const clang::FunctionDecl *Def,
73 const MatchFinder::MatchResult &MatchResult) {
80 CharSourceRange::getCharRange(Tok->getLocation(), Tok->getEndLoc());
81 Result.Hints.push_back(FixItHint::CreateRemoval(Result.ConstRange));
86 for (
const FunctionDecl *
Decl = Def->getPreviousDecl();
Decl !=
nullptr;
89 Result.Hints.push_back(FixItHint::CreateRemoval(
90 CharSourceRange::getCharRange(T->getLocation(), T->getEndLoc())));
93 Result.DeclLocs.push_back(
Decl->getInnerLocStart());
106 functionDecl(returns(isLocalConstQualified()),
107 anyOf(isDefinition(), cxxMethodDecl(isPure())),
109 unless(cxxMethodDecl(isOverride())))
115 const auto *Def = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
118 (Def->getBeginLoc().isMacroID() || Def->getEndLoc().isMacroID()))
121 CheckResult CR =
checkDef(Def, Result);
127 diag(Def->getInnerLocStart(),
128 "return type %0 is 'const'-qualified at the top level, which may "
129 "reduce code readability without improving const correctness")
130 << Def->getReturnType();
131 if (CR.ConstRange.isValid())
135 const auto *Method = dyn_cast<CXXMethodDecl>(Def);
136 if (Method && Method->isVirtual())
139 for (
auto &Hint : CR.Hints)
142 for (
auto Loc : CR.DeclLocs)
143 diag(
Loc,
"could not transform this declaration", DiagnosticIDs::Note);
const FunctionDecl * Decl
DiagnosticCallback Diagnostic
llvm::SmallVector< clang::SourceLocation, 4 > DeclLocs
CharSourceRange ConstRange
std::vector< FixItHint > Hints
::clang::DynTypedNode Node
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.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
AST_MATCHER(CXXMethodDecl, isStatic)
static CheckResult checkDef(const clang::FunctionDecl *Def, const MatchFinder::MatchResult &MatchResult)
static std::optional< Token > findConstToRemove(const FunctionDecl *Def, const MatchFinder::MatchResult &Result)
std::optional< Token > getQualifyingToken(tok::TokenKind TK, CharSourceRange Range, const ASTContext &Context, const SourceManager &SM)
Assuming that Range spans a CVR-qualified type, returns the token in Range that is responsible for th...
llvm::StringMap< ClangTidyValue > OptionMap