aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clang-tidy/readability/AvoidConstParamsInDecls.cpp
blob: 24cbbd8bc60a2b52042d03d88e7c243e7a5f1b90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//===--- AvoidConstParamsInDecls.cpp - clang-tidy--------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "AvoidConstParamsInDecls.h"
#include "../utils/LexerUtils.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"

using namespace clang::ast_matchers;

namespace clang::tidy::readability {
namespace {

SourceRange getTypeRange(const ParmVarDecl &Param) {
  return {Param.getBeginLoc(), Param.getLocation().getLocWithOffset(-1)};
}

// Finds the location of the qualifying `const` token in the `ParmValDecl`'s
// return type. Returns `std::nullopt` when the parm type is not
// `const`-qualified like when the type is an alias or a macro.
static std::optional<Token>
findConstToRemove(const ParmVarDecl &Param,
                  const MatchFinder::MatchResult &Result) {

  CharSourceRange FileRange = Lexer::makeFileCharRange(
      CharSourceRange::getTokenRange(getTypeRange(Param)),
      *Result.SourceManager, Result.Context->getLangOpts());

  if (FileRange.isInvalid())
    return std::nullopt;

  return tidy::utils::lexer::getQualifyingToken(
      tok::kw_const, FileRange, *Result.Context, *Result.SourceManager);
}

} // namespace

void AvoidConstParamsInDecls::storeOptions(ClangTidyOptions::OptionMap &Opts) {
  Options.store(Opts, "IgnoreMacros", IgnoreMacros);
}

void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) {
  const auto ConstParamDecl =
      parmVarDecl(hasType(qualType(isConstQualified()))).bind("param");
  Finder->addMatcher(functionDecl(unless(isDefinition()),
                                  has(typeLoc(forEach(ConstParamDecl))))
                         .bind("func"),
                     this);
}

void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) {
  const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
  const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");

  if (!Param->getType().isLocalConstQualified())
    return;

  if (IgnoreMacros &&
      (Param->getBeginLoc().isMacroID() || Param->getEndLoc().isMacroID())) {
    // Suppress the check if macros are involved.
    return;
  }

  const auto Tok = findConstToRemove(*Param, Result);
  const auto ConstLocation = Tok ? Tok->getLocation() : Param->getBeginLoc();

  auto Diag = diag(ConstLocation,
                   "parameter %0 is const-qualified in the function "
                   "declaration; const-qualification of parameters only has an "
                   "effect in function definitions");
  if (Param->getName().empty()) {
    for (unsigned int I = 0; I < Func->getNumParams(); ++I) {
      if (Param == Func->getParamDecl(I)) {
        Diag << (I + 1);
        break;
      }
    }
  } else {
    Diag << Param;
  }

  if (Param->getBeginLoc().isMacroID() != Param->getEndLoc().isMacroID()) {
    // Do not offer a suggestion if the part of the variable declaration comes
    // from a macro.
    return;
  }
  if (!Tok)
    return;

  Diag << FixItHint::CreateRemoval(
      CharSourceRange::getTokenRange(Tok->getLocation(), Tok->getLocation()));
}

} // namespace clang::tidy::readability