blob: 9eea2f56d6d63fdd7b36002625a4f5904486640d [file] [log] [blame]
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +00001//===--- AvoidConstParamsInDecls.cpp - clang-tidy--------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "AvoidConstParamsInDecls.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13#include "clang/Lex/Lexer.h"
14
15using namespace clang::ast_matchers;
16
17namespace clang {
18namespace tidy {
19namespace readability {
20namespace {
21
22SourceRange getTypeRange(const ParmVarDecl &Param) {
23 if (Param.getIdentifier() != nullptr)
24 return SourceRange(Param.getLocStart(),
25 Param.getLocEnd().getLocWithOffset(-1));
26 return Param.getSourceRange();
27}
28
29} // namespace
30
31
32void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) {
33 const auto ConstParamDecl =
34 parmVarDecl(hasType(qualType(isConstQualified()))).bind("param");
35 Finder->addMatcher(functionDecl(unless(isDefinition()),
36 has(typeLoc(forEach(ConstParamDecl))))
37 .bind("func"),
38 this);
39}
40
41void 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 QualType Type = Param->getType();
46 if (!Type.isLocalConstQualified())
47 return;
48
49 Type.removeLocalConst();
50
51 auto Diag = diag(Param->getLocStart(),
52 "parameter %0 is const-qualified in the function "
53 "declaration; const-qualification of parameters only has an "
54 "effect in function definitions");
55 if (Param->getName().empty()) {
56 for (unsigned int i = 0; i < Func->getNumParams(); ++i) {
57 if (Param == Func->getParamDecl(i)) {
58 Diag << (i + 1);
59 break;
60 }
61 }
62 } else {
63 Diag << Param;
64 }
65 Diag << FixItHint::CreateReplacement(getTypeRange(*Param),
66 Type.getAsString());
67}
68
69} // namespace readability
70} // namespace tidy
71} // namespace clang