| 1 | //===--- NonConstParameterCheck.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 "NonConstParameterCheck.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | |
| 13 | using namespace clang::ast_matchers; |
| 14 | |
| 15 | namespace clang::tidy::readability { |
| 16 | |
| 17 | void NonConstParameterCheck::registerMatchers(MatchFinder *Finder) { |
| 18 | // Add parameters to Parameters. |
| 19 | Finder->addMatcher(NodeMatch: parmVarDecl().bind(ID: "Parm" ), Action: this); |
| 20 | |
| 21 | // C++ constructor. |
| 22 | Finder->addMatcher(NodeMatch: cxxConstructorDecl().bind(ID: "Ctor" ), Action: this); |
| 23 | |
| 24 | // Track unused parameters, there is Wunused-parameter about unused |
| 25 | // parameters. |
| 26 | Finder->addMatcher(NodeMatch: declRefExpr().bind(ID: "Ref" ), Action: this); |
| 27 | |
| 28 | // Analyse parameter usage in function. |
| 29 | Finder->addMatcher(NodeMatch: stmt(anyOf(unaryOperator(hasAnyOperatorName("++" , "--" )), |
| 30 | binaryOperator(), callExpr(), returnStmt(), |
| 31 | cxxConstructExpr())) |
| 32 | .bind(ID: "Mark" ), |
| 33 | Action: this); |
| 34 | Finder->addMatcher(NodeMatch: varDecl(hasInitializer(InnerMatcher: anything())).bind(ID: "Mark" ), Action: this); |
| 35 | } |
| 36 | |
| 37 | void NonConstParameterCheck::check(const MatchFinder::MatchResult &Result) { |
| 38 | if (const auto *Parm = Result.Nodes.getNodeAs<ParmVarDecl>(ID: "Parm" )) { |
| 39 | if (const DeclContext *D = Parm->getParentFunctionOrMethod()) { |
| 40 | if (const auto *M = dyn_cast<CXXMethodDecl>(D)) { |
| 41 | if (M->isVirtual() || M->size_overridden_methods() != 0) |
| 42 | return; |
| 43 | } |
| 44 | } |
| 45 | addParm(Parm); |
| 46 | } else if (const auto *Ctor = |
| 47 | Result.Nodes.getNodeAs<CXXConstructorDecl>(ID: "Ctor" )) { |
| 48 | for (const auto *Parm : Ctor->parameters()) |
| 49 | addParm(Parm); |
| 50 | for (const auto *Init : Ctor->inits()) |
| 51 | markCanNotBeConst(E: Init->getInit(), CanNotBeConst: true); |
| 52 | } else if (const auto *Ref = Result.Nodes.getNodeAs<DeclRefExpr>(ID: "Ref" )) { |
| 53 | setReferenced(Ref); |
| 54 | } else if (const auto *S = Result.Nodes.getNodeAs<Stmt>(ID: "Mark" )) { |
| 55 | if (const auto *B = dyn_cast<BinaryOperator>(Val: S)) { |
| 56 | if (B->isAssignmentOp()) |
| 57 | markCanNotBeConst(B, false); |
| 58 | } else if (const auto *CE = dyn_cast<CallExpr>(Val: S)) { |
| 59 | // Typically, if a parameter is const then it is fine to make the data |
| 60 | // const. But sometimes the data is written even though the parameter |
| 61 | // is const. Mark all data passed by address to the function. |
| 62 | for (const auto *Arg : CE->arguments()) { |
| 63 | markCanNotBeConst(E: Arg->IgnoreParenCasts(), CanNotBeConst: true); |
| 64 | } |
| 65 | |
| 66 | // Data passed by nonconst reference should not be made const. |
| 67 | if (const FunctionDecl *FD = CE->getDirectCallee()) { |
| 68 | unsigned ArgNr = 0U; |
| 69 | for (const auto *Par : FD->parameters()) { |
| 70 | if (ArgNr >= CE->getNumArgs()) |
| 71 | break; |
| 72 | const Expr *Arg = CE->getArg(Arg: ArgNr++); |
| 73 | // Is this a non constant reference parameter? |
| 74 | const Type *ParType = Par->getType().getTypePtr(); |
| 75 | if (!ParType->isReferenceType() || Par->getType().isConstQualified()) |
| 76 | continue; |
| 77 | markCanNotBeConst(E: Arg->IgnoreParenCasts(), CanNotBeConst: false); |
| 78 | } |
| 79 | } |
| 80 | } else if (const auto *CE = dyn_cast<CXXConstructExpr>(Val: S)) { |
| 81 | for (const auto *Arg : CE->arguments()) { |
| 82 | markCanNotBeConst(Arg->IgnoreParenCasts(), true); |
| 83 | } |
| 84 | // Data passed by nonconst reference should not be made const. |
| 85 | unsigned ArgNr = 0U; |
| 86 | if (const auto *CD = CE->getConstructor()) { |
| 87 | for (const auto *Par : CD->parameters()) { |
| 88 | if (ArgNr >= CE->getNumArgs()) |
| 89 | break; |
| 90 | const Expr *Arg = CE->getArg(ArgNr++); |
| 91 | // Is this a non constant reference parameter? |
| 92 | const Type *ParType = Par->getType().getTypePtr(); |
| 93 | if (!ParType->isReferenceType() || Par->getType().isConstQualified()) |
| 94 | continue; |
| 95 | markCanNotBeConst(Arg->IgnoreParenCasts(), false); |
| 96 | } |
| 97 | } |
| 98 | } else if (const auto *R = dyn_cast<ReturnStmt>(Val: S)) { |
| 99 | markCanNotBeConst(E: R->getRetValue(), CanNotBeConst: true); |
| 100 | } else if (const auto *U = dyn_cast<UnaryOperator>(Val: S)) { |
| 101 | markCanNotBeConst(U, true); |
| 102 | } |
| 103 | } else if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>(ID: "Mark" )) { |
| 104 | const QualType T = VD->getType(); |
| 105 | if ((T->isPointerType() && !T->getPointeeType().isConstQualified()) || |
| 106 | T->isArrayType() || T->isRecordType()) |
| 107 | markCanNotBeConst(E: VD->getInit(), CanNotBeConst: true); |
| 108 | else if (T->isLValueReferenceType() && |
| 109 | !T->getPointeeType().isConstQualified()) |
| 110 | markCanNotBeConst(E: VD->getInit(), CanNotBeConst: false); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void NonConstParameterCheck::addParm(const ParmVarDecl *Parm) { |
| 115 | // Only add nonconst integer/float pointer parameters. |
| 116 | const QualType T = Parm->getType(); |
| 117 | if (!T->isPointerType() || T->getPointeeType().isConstQualified() || |
| 118 | !(T->getPointeeType()->isIntegerType() || |
| 119 | T->getPointeeType()->isFloatingType())) |
| 120 | return; |
| 121 | |
| 122 | auto [It, Inserted] = Parameters.try_emplace(k: Parm); |
| 123 | if (!Inserted) |
| 124 | return; |
| 125 | |
| 126 | It->second.IsReferenced = false; |
| 127 | It->second.CanBeConst = true; |
| 128 | } |
| 129 | |
| 130 | void NonConstParameterCheck::setReferenced(const DeclRefExpr *Ref) { |
| 131 | auto It = Parameters.find(x: dyn_cast<ParmVarDecl>(Val: Ref->getDecl())); |
| 132 | if (It != Parameters.end()) |
| 133 | It->second.IsReferenced = true; |
| 134 | } |
| 135 | |
| 136 | void NonConstParameterCheck::onEndOfTranslationUnit() { |
| 137 | diagnoseNonConstParameters(); |
| 138 | } |
| 139 | |
| 140 | void NonConstParameterCheck::diagnoseNonConstParameters() { |
| 141 | for (const auto &It : Parameters) { |
| 142 | const ParmVarDecl *Par = It.first; |
| 143 | const ParmInfo &ParamInfo = It.second; |
| 144 | |
| 145 | // Unused parameter => there are other warnings about this. |
| 146 | if (!ParamInfo.IsReferenced) |
| 147 | continue; |
| 148 | |
| 149 | // Parameter can't be const. |
| 150 | if (!ParamInfo.CanBeConst) |
| 151 | continue; |
| 152 | |
| 153 | SmallVector<FixItHint, 8> Fixes; |
| 154 | auto *Function = |
| 155 | dyn_cast_or_null<const FunctionDecl>(Par->getParentFunctionOrMethod()); |
| 156 | if (!Function) |
| 157 | continue; |
| 158 | unsigned Index = Par->getFunctionScopeIndex(); |
| 159 | for (FunctionDecl *FnDecl : Function->redecls()) { |
| 160 | if (FnDecl->getNumParams() <= Index) |
| 161 | continue; |
| 162 | Fixes.push_back(FixItHint::CreateInsertion( |
| 163 | FnDecl->getParamDecl(Index)->getBeginLoc(), "const " )); |
| 164 | } |
| 165 | |
| 166 | diag(Par->getLocation(), "pointer parameter '%0' can be pointer to const" ) |
| 167 | << Par->getName() << Fixes; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void NonConstParameterCheck::markCanNotBeConst(const Expr *E, |
| 172 | bool CanNotBeConst) { |
| 173 | if (!E) |
| 174 | return; |
| 175 | |
| 176 | if (const auto *Cast = dyn_cast<ImplicitCastExpr>(Val: E)) { |
| 177 | // If expression is const then ignore usage. |
| 178 | const QualType T = Cast->getType(); |
| 179 | if (T->isPointerType() && T->getPointeeType().isConstQualified()) |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | E = E->IgnoreParenCasts(); |
| 184 | |
| 185 | if (const auto *B = dyn_cast<BinaryOperator>(Val: E)) { |
| 186 | if (B->isAdditiveOp()) { |
| 187 | // p + 2 |
| 188 | markCanNotBeConst(E: B->getLHS(), CanNotBeConst); |
| 189 | markCanNotBeConst(E: B->getRHS(), CanNotBeConst); |
| 190 | } else if (B->isAssignmentOp()) { |
| 191 | markCanNotBeConst(E: B->getLHS(), CanNotBeConst: false); |
| 192 | |
| 193 | // If LHS is not const then RHS can't be const. |
| 194 | const QualType T = B->getLHS()->getType(); |
| 195 | if (T->isPointerType() && !T->getPointeeType().isConstQualified()) |
| 196 | markCanNotBeConst(E: B->getRHS(), CanNotBeConst: true); |
| 197 | } |
| 198 | } else if (const auto *C = dyn_cast<ConditionalOperator>(Val: E)) { |
| 199 | markCanNotBeConst(E: C->getTrueExpr(), CanNotBeConst); |
| 200 | markCanNotBeConst(E: C->getFalseExpr(), CanNotBeConst); |
| 201 | } else if (const auto *U = dyn_cast<UnaryOperator>(Val: E)) { |
| 202 | if (U->getOpcode() == UO_PreInc || U->getOpcode() == UO_PreDec || |
| 203 | U->getOpcode() == UO_PostInc || U->getOpcode() == UO_PostDec) { |
| 204 | if (const auto *SubU = |
| 205 | dyn_cast<UnaryOperator>(Val: U->getSubExpr()->IgnoreParenCasts())) |
| 206 | markCanNotBeConst(E: SubU->getSubExpr(), CanNotBeConst: true); |
| 207 | markCanNotBeConst(E: U->getSubExpr(), CanNotBeConst); |
| 208 | } else if (U->getOpcode() == UO_Deref) { |
| 209 | if (!CanNotBeConst) |
| 210 | markCanNotBeConst(E: U->getSubExpr(), CanNotBeConst: true); |
| 211 | } else { |
| 212 | markCanNotBeConst(E: U->getSubExpr(), CanNotBeConst); |
| 213 | } |
| 214 | } else if (const auto *A = dyn_cast<ArraySubscriptExpr>(Val: E)) { |
| 215 | markCanNotBeConst(E: A->getBase(), CanNotBeConst: true); |
| 216 | } else if (const auto *CLE = dyn_cast<CompoundLiteralExpr>(Val: E)) { |
| 217 | markCanNotBeConst(E: CLE->getInitializer(), CanNotBeConst: true); |
| 218 | } else if (const auto *Constr = dyn_cast<CXXConstructExpr>(Val: E)) { |
| 219 | for (const auto *Arg : Constr->arguments()) { |
| 220 | if (const auto *M = dyn_cast<MaterializeTemporaryExpr>(Arg)) |
| 221 | markCanNotBeConst(cast<Expr>(M->getSubExpr()), CanNotBeConst); |
| 222 | } |
| 223 | } else if (const auto *ILE = dyn_cast<InitListExpr>(Val: E)) { |
| 224 | for (unsigned I = 0U; I < ILE->getNumInits(); ++I) |
| 225 | markCanNotBeConst(E: ILE->getInit(Init: I), CanNotBeConst: true); |
| 226 | } else if (CanNotBeConst) { |
| 227 | // Referencing parameter. |
| 228 | if (const auto *D = dyn_cast<DeclRefExpr>(Val: E)) { |
| 229 | auto It = Parameters.find(x: dyn_cast<ParmVarDecl>(Val: D->getDecl())); |
| 230 | if (It != Parameters.end()) |
| 231 | It->second.CanBeConst = false; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | } // namespace clang::tidy::readability |
| 237 | |