1 //===--- MisplacedConstCheck.cpp - clang-tidy------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "MisplacedConstCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 
13 using namespace clang::ast_matchers;
14 
15 namespace clang {
16 namespace tidy {
17 namespace misc {
18 
registerMatchers(MatchFinder * Finder)19 void MisplacedConstCheck::registerMatchers(MatchFinder *Finder) {
20   auto NonConstAndNonFunctionPointerType = hasType(pointerType(unless(
21       pointee(anyOf(isConstQualified(), ignoringParens(functionType()))))));
22 
23   Finder->addMatcher(
24       valueDecl(
25           hasType(isConstQualified()),
26           hasType(typedefType(hasDeclaration(anyOf(
27               typedefDecl(NonConstAndNonFunctionPointerType).bind("typedef"),
28               typeAliasDecl(NonConstAndNonFunctionPointerType)
29                   .bind("typeAlias"))))))
30           .bind("decl"),
31       this);
32 }
33 
guessAlternateQualification(ASTContext & Context,QualType QT)34 static QualType guessAlternateQualification(ASTContext &Context, QualType QT) {
35   // We're given a QualType from a typedef where the qualifiers apply to the
36   // pointer instead of the pointee. Strip the const qualifier from the pointer
37   // type and add it to the pointee instead.
38   if (!QT->isPointerType())
39     return QT;
40 
41   Qualifiers Quals = QT.getLocalQualifiers();
42   Quals.removeConst();
43 
44   QualType NewQT = Context.getPointerType(
45       QualType(QT->getPointeeType().getTypePtr(), Qualifiers::Const));
46   return NewQT.withCVRQualifiers(Quals.getCVRQualifiers());
47 }
48 
check(const MatchFinder::MatchResult & Result)49 void MisplacedConstCheck::check(const MatchFinder::MatchResult &Result) {
50   const auto *Var = Result.Nodes.getNodeAs<ValueDecl>("decl");
51   ASTContext &Ctx = *Result.Context;
52   QualType CanQT = Var->getType().getCanonicalType();
53 
54   SourceLocation AliasLoc;
55   const char *AliasType;
56   if (const auto *Typedef = Result.Nodes.getNodeAs<TypedefDecl>("typedef")) {
57     AliasLoc = Typedef->getLocation();
58     AliasType = "typedef";
59   } else if (const auto *TypeAlias =
60                  Result.Nodes.getNodeAs<TypeAliasDecl>("typeAlias")) {
61     AliasLoc = TypeAlias->getLocation();
62     AliasType = "type alias";
63   } else {
64     llvm_unreachable("registerMatchers has registered an unknown matcher,"
65                      " code out of sync");
66   }
67 
68   diag(Var->getLocation(), "%0 declared with a const-qualified %1; "
69                            "results in the type being '%2' instead of '%3'")
70       << Var << AliasType << CanQT.getAsString(Ctx.getPrintingPolicy())
71       << guessAlternateQualification(Ctx, CanQT)
72              .getAsString(Ctx.getPrintingPolicy());
73   diag(AliasLoc, "%0 declared here", DiagnosticIDs::Note) << AliasType;
74 }
75 
76 } // namespace misc
77 } // namespace tidy
78 } // namespace clang
79