blob: 8a2a053c4ae559d017b02f6494fc9c1c2134bf19 [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
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000031void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) {
32 const auto ConstParamDecl =
33 parmVarDecl(hasType(qualType(isConstQualified()))).bind("param");
34 Finder->addMatcher(functionDecl(unless(isDefinition()),
35 has(typeLoc(forEach(ConstParamDecl))))
36 .bind("func"),
37 this);
38}
39
Matthias Gehre018c1d42016-04-12 05:45:13 +000040// Re-lex the tokens to get precise location of last 'const'
41static Token ConstTok(CharSourceRange Range,
42 const MatchFinder::MatchResult &Result) {
43 const SourceManager &Sources = *Result.SourceManager;
44 std::pair<FileID, unsigned> LocInfo =
45 Sources.getDecomposedLoc(Range.getBegin());
46 StringRef File = Sources.getBufferData(LocInfo.first);
47 const char *TokenBegin = File.data() + LocInfo.second;
48 Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first),
49 Result.Context->getLangOpts(), File.begin(), TokenBegin,
50 File.end());
51 Token Tok;
52 Token ConstTok;
53 while (!RawLexer.LexFromRawLexer(Tok)) {
54 if (Sources.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation()))
55 break;
56 if (Tok.is(tok::raw_identifier)) {
57 IdentifierInfo &Info = Result.Context->Idents.get(StringRef(
58 Sources.getCharacterData(Tok.getLocation()), Tok.getLength()));
59 Tok.setIdentifierInfo(&Info);
60 Tok.setKind(Info.getTokenID());
61 }
62 if (Tok.is(tok::kw_const))
63 ConstTok = Tok;
64 }
65 return ConstTok;
66}
67
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000068void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) {
69 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
70 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
71
Matthias Gehre018c1d42016-04-12 05:45:13 +000072 if (!Param->getType().isLocalConstQualified())
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000073 return;
74
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000075 auto Diag = diag(Param->getLocStart(),
76 "parameter %0 is const-qualified in the function "
77 "declaration; const-qualification of parameters only has an "
78 "effect in function definitions");
79 if (Param->getName().empty()) {
80 for (unsigned int i = 0; i < Func->getNumParams(); ++i) {
81 if (Param == Func->getParamDecl(i)) {
82 Diag << (i + 1);
83 break;
84 }
85 }
86 } else {
87 Diag << Param;
88 }
Matthias Gehre018c1d42016-04-12 05:45:13 +000089
90 CharSourceRange FileRange = Lexer::makeFileCharRange(
91 CharSourceRange::getTokenRange(getTypeRange(*Param)),
92 *Result.SourceManager, Result.Context->getLangOpts());
93
94 if (!FileRange.isValid())
95 return;
96
97 Token Tok = ConstTok(FileRange, Result);
98 Diag << FixItHint::CreateRemoval(
99 CharSourceRange::getTokenRange(Tok.getLocation(), Tok.getLocation()));
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +0000100}
101
102} // namespace readability
103} // namespace tidy
104} // namespace clang