10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
18 auto NonConstAndNonFunctionPointerType = hasType(pointerType(unless(
19 pointee(anyOf(isConstQualified(), ignoringParens(functionType()))))));
22 valueDecl(hasType(qualType(
24 elaboratedType(namesType(typedefType(hasDeclaration(
25 anyOf(typedefDecl(NonConstAndNonFunctionPointerType)
27 typeAliasDecl(NonConstAndNonFunctionPointerType)
28 .bind(
"typeAlias")))))))))
37 if (!QT->isPointerType())
40 Qualifiers Quals = QT.getLocalQualifiers();
43 QualType NewQT = Context.getPointerType(
44 QualType(QT->getPointeeType().getTypePtr(), Qualifiers::Const));
45 return NewQT.withCVRQualifiers(Quals.getCVRQualifiers());
49 const auto *Var = Result.Nodes.getNodeAs<ValueDecl>(
"decl");
50 ASTContext &Ctx = *Result.Context;
51 QualType CanQT = Var->getType().getCanonicalType();
53 SourceLocation AliasLoc;
54 const char *AliasType =
nullptr;
55 if (
const auto *Typedef = Result.Nodes.getNodeAs<TypedefDecl>(
"typedef")) {
56 AliasLoc = Typedef->getLocation();
57 AliasType =
"typedef";
58 }
else if (
const auto *TypeAlias =
59 Result.Nodes.getNodeAs<TypeAliasDecl>(
"typeAlias")) {
60 AliasLoc = TypeAlias->getLocation();
61 AliasType =
"type alias";
63 llvm_unreachable(
"registerMatchers has registered an unknown matcher,"
67 diag(Var->getLocation(),
"%0 declared with a const-qualified %1; "
68 "results in the type being '%2' instead of '%3'")
69 << Var << AliasType << CanQT.getAsString(Ctx.getPrintingPolicy())
71 .getAsString(Ctx.getPrintingPolicy());
72 diag(AliasLoc,
"%0 declared here", DiagnosticIDs::Note) << AliasType;
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
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.
static QualType guessAlternateQualification(ASTContext &Context, QualType QT)