| 1 |
//===--- AvoidConstParamsInDecls.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 "AvoidConstParamsInDecls.h" |
| 10 |
#include "../utils/LexerUtils.h" |
| 11 |
#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 |
#include "clang/ASTMatchers/ASTMatchers.h" |
| 13 |
#include "clang/Lex/Lexer.h" |
| 14 |
|
| 15 |
using namespace clang::ast_matchers; |
| 16 |
|
| 17 |
namespace clang::tidy::readability { |
| 18 |
namespace { |
| 19 |
|
| 20 |
SourceRange getTypeRange(const ParmVarDecl &Param) { |
| 21 |
return SourceRange(Param.getBeginLoc(), |
| 22 |
Param.getLocation().getLocWithOffset(-1)); |
| 23 |
} |
| 24 |
|
| 25 |
} // namespace |
| 26 |
|
| 27 |
void AvoidConstParamsInDecls::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 28 |
Options.store(Opts, "IgnoreMacros", IgnoreMacros); |
| 29 |
} |
| 30 |
|
| 31 |
void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) { |
| 32 |
const auto ConstParamDecl = |
| 33 |
parmVarDecl(hasType(qualType(isConstQualified()))).bind("param"); |
| 34 |
Finder->addMatcher( |
| 35 |
functionDecl(unless(isDefinition()), |
| 36 |
has(typeLoc(forEach(ConstParamDecl)))) |
| 37 |
.bind("func"), |
| 38 |
this); |
| 39 |
} |
| 40 |
|
| 41 |
void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) { |
| 42 |
const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func"); |
| 43 |
const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param"); |
| 44 |
|
| 45 |
if (!Param->getType().isLocalConstQualified()) |
| 46 |
return; |
| 47 |
|
| 48 |
if (IgnoreMacros && |
| 49 |
(Param->getBeginLoc().isMacroID() || Param->getEndLoc().isMacroID())) { |
| 50 |
// Suppress the check if macros are involved. |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
auto Diag = diag(Param->getBeginLoc(), |
| 55 |
"parameter %0 is const-qualified in the function " |
| 56 |
"declaration; const-qualification of parameters only has an " |
| 57 |
"effect in function definitions"); |
| 58 |
if (Param->getName().empty()) { |
| 59 |
for (unsigned int I = 0; I < Func->getNumParams(); ++I) { |
| 60 |
if (Param == Func->getParamDecl(I)) { |
| 61 |
Diag << (I + 1); |
| 62 |
break; |
| 63 |
} |
| 64 |
} |
| 65 |
} else { |
| 66 |
Diag << Param; |
| 67 |
} |
| 68 |
|
| 69 |
if (Param->getBeginLoc().isMacroID() != Param->getEndLoc().isMacroID()) { |
| 70 |
// Do not offer a suggestion if the part of the variable declaration comes |
| 71 |
// from a macro. |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
CharSourceRange FileRange = Lexer::makeFileCharRange( |
| 76 |
CharSourceRange::getTokenRange(getTypeRange(*Param)), |
| 77 |
*Result.SourceManager, getLangOpts()); |
| 78 |
|
| 79 |
if (!FileRange.isValid()) |
| 80 |
return; |
| 81 |
|
| 82 |
auto Tok = tidy::utils::lexer::getQualifyingToken( |
| 83 |
tok::kw_const, FileRange, *Result.Context, *Result.SourceManager); |
| 84 |
if (!Tok) |
| 85 |
return; |
| 86 |
Diag << FixItHint::CreateRemoval( |
| 87 |
CharSourceRange::getTokenRange(Tok->getLocation(), Tok->getLocation())); |
| 88 |
} |
| 89 |
|
| 90 |
} // namespace clang::tidy::readability |