10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
19 Finder->addMatcher(functionDecl(hasAnyOverloadedOperatorName(
"++",
"--"),
20 unless(isInstantiated()))
26 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"decl");
29 if (
const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
30 HasThis = MethodDecl->isInstance();
33 if (FuncDecl->getNumParams() != (HasThis ? 1 : 2))
36 SourceRange ReturnRange = FuncDecl->getReturnTypeSourceRange();
37 SourceLocation
Location = ReturnRange.getBegin();
41 QualType
ReturnType = FuncDecl->getReturnType();
44 if (
const auto *RefType =
ReturnType->getAs<ReferenceType>()) {
45 auto Diag =
diag(
Location,
"overloaded %0 returns a reference instead of a "
46 "constant object type")
50 RefType->getPointeeTypeAsWritten()->getAs<TypedefType>())
53 QualType ReplaceType =
54 ReturnType.getNonReferenceType().getLocalUnqualifiedType();
57 if (!
ReturnType->getPointeeType().isConstQualified())
58 ReplaceType.addConst();
60 Diag << FixItHint::CreateReplacement(
62 ReplaceType.getAsString(Result.Context->getPrintingPolicy()) +
" ");
72 diag(
Location,
"overloaded %0 returns a non-constant object instead of a "
73 "constant object type")
77 Diag << FixItHint::CreateInsertion(
Location,
"const ");
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
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.