11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Lex/Lexer.h" 21 void PostfixOperatorCheck::registerMatchers(MatchFinder *Finder) {
22 if (!getLangOpts().CPlusPlus)
25 Finder->addMatcher(functionDecl(anyOf(hasOverloadedOperatorName(
"++"),
26 hasOverloadedOperatorName(
"--")))
31 void PostfixOperatorCheck::check(
const MatchFinder::MatchResult &Result) {
32 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"decl");
35 if (
const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
36 HasThis = MethodDecl->isInstance();
39 if (FuncDecl->getNumParams() != (HasThis ? 1 : 2))
42 SourceRange ReturnRange = FuncDecl->getReturnTypeSourceRange();
43 SourceLocation
Location = ReturnRange.getBegin();
44 if (!Location.isValid())
47 QualType
ReturnType = FuncDecl->getReturnType();
50 if (
const auto *RefType = ReturnType->getAs<ReferenceType>()) {
51 auto Diag = diag(Location,
"overloaded %0 returns a reference instead of a " 52 "constant object type")
55 if (Location.isMacroID() || ReturnType->getAs<TypedefType>() ||
56 RefType->getPointeeTypeAsWritten()->getAs<TypedefType>())
59 QualType ReplaceType =
60 ReturnType.getNonReferenceType().getLocalUnqualifiedType();
63 if (!ReturnType->getPointeeType().isConstQualified())
64 ReplaceType.addConst();
66 Diag << FixItHint::CreateReplacement(
68 ReplaceType.getAsString(Result.Context->getPrintingPolicy()) +
" ");
73 if (ReturnType.isConstQualified() || ReturnType->isBuiltinType() ||
74 ReturnType->isPointerType())
78 diag(Location,
"overloaded %0 returns a non-constant object instead of a " 79 "constant object type")
82 if (!Location.isMacroID() && !ReturnType->getAs<TypedefType>())
83 Diag << FixItHint::CreateInsertion(Location,
"const ");
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//