blob: 11a588310f8ad1e1fe09c1434ede5396d8b3aae2 [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"
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +000011#include "llvm/ADT/Optional.h"
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Lex/Lexer.h"
15
16using namespace clang::ast_matchers;
17
18namespace clang {
19namespace tidy {
20namespace readability {
21namespace {
22
23SourceRange getTypeRange(const ParmVarDecl &Param) {
24 if (Param.getIdentifier() != nullptr)
25 return SourceRange(Param.getLocStart(),
26 Param.getLocEnd().getLocWithOffset(-1));
27 return Param.getSourceRange();
28}
29
30} // namespace
31
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000032void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) {
33 const auto ConstParamDecl =
34 parmVarDecl(hasType(qualType(isConstQualified()))).bind("param");
Samuel Benzaquen25cd6132016-06-28 14:19:41 +000035 Finder->addMatcher(
36 functionDecl(unless(isDefinition()),
37 // Lambdas are always their own definition, but they
38 // generate a non-definition FunctionDecl too. Ignore those.
39 unless(cxxMethodDecl(ofClass(cxxRecordDecl(isLambda())))),
40 has(typeLoc(forEach(ConstParamDecl))))
41 .bind("func"),
42 this);
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000043}
44
Matthias Gehre018c1d42016-04-12 05:45:13 +000045// Re-lex the tokens to get precise location of last 'const'
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +000046static llvm::Optional<Token> ConstTok(CharSourceRange Range,
47 const MatchFinder::MatchResult &Result) {
Matthias Gehre018c1d42016-04-12 05:45:13 +000048 const SourceManager &Sources = *Result.SourceManager;
49 std::pair<FileID, unsigned> LocInfo =
50 Sources.getDecomposedLoc(Range.getBegin());
51 StringRef File = Sources.getBufferData(LocInfo.first);
52 const char *TokenBegin = File.data() + LocInfo.second;
53 Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first),
54 Result.Context->getLangOpts(), File.begin(), TokenBegin,
55 File.end());
56 Token Tok;
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +000057 llvm::Optional<Token> ConstTok;
Matthias Gehre018c1d42016-04-12 05:45:13 +000058 while (!RawLexer.LexFromRawLexer(Tok)) {
59 if (Sources.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation()))
60 break;
61 if (Tok.is(tok::raw_identifier)) {
62 IdentifierInfo &Info = Result.Context->Idents.get(StringRef(
63 Sources.getCharacterData(Tok.getLocation()), Tok.getLength()));
64 Tok.setIdentifierInfo(&Info);
65 Tok.setKind(Info.getTokenID());
66 }
67 if (Tok.is(tok::kw_const))
68 ConstTok = Tok;
69 }
70 return ConstTok;
71}
72
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000073void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) {
74 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
75 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
76
Matthias Gehre018c1d42016-04-12 05:45:13 +000077 if (!Param->getType().isLocalConstQualified())
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000078 return;
79
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000080 auto Diag = diag(Param->getLocStart(),
81 "parameter %0 is const-qualified in the function "
82 "declaration; const-qualification of parameters only has an "
83 "effect in function definitions");
84 if (Param->getName().empty()) {
85 for (unsigned int i = 0; i < Func->getNumParams(); ++i) {
86 if (Param == Func->getParamDecl(i)) {
87 Diag << (i + 1);
88 break;
89 }
90 }
91 } else {
92 Diag << Param;
93 }
Matthias Gehre018c1d42016-04-12 05:45:13 +000094
Samuel Benzaquen79c76102016-06-06 14:21:11 +000095 if (Param->getLocStart().isMacroID() != Param->getLocEnd().isMacroID()) {
96 // Do not offer a suggestion if the part of the variable declaration comes
97 // from a macro.
98 return;
99 }
100
Matthias Gehre018c1d42016-04-12 05:45:13 +0000101 CharSourceRange FileRange = Lexer::makeFileCharRange(
102 CharSourceRange::getTokenRange(getTypeRange(*Param)),
103 *Result.SourceManager, Result.Context->getLangOpts());
104
105 if (!FileRange.isValid())
106 return;
107
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +0000108 auto Tok = ConstTok(FileRange, Result);
109 if (!Tok)
110 return;
Matthias Gehre018c1d42016-04-12 05:45:13 +0000111 Diag << FixItHint::CreateRemoval(
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +0000112 CharSourceRange::getTokenRange(Tok->getLocation(), Tok->getLocation()));
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +0000113}
114
115} // namespace readability
116} // namespace tidy
117} // namespace clang