blob: 6e68dda945b38ee5cfd7a760c311a761668a7956 [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");
35 Finder->addMatcher(functionDecl(unless(isDefinition()),
36 has(typeLoc(forEach(ConstParamDecl))))
37 .bind("func"),
38 this);
39}
40
Matthias Gehre018c1d42016-04-12 05:45:13 +000041// Re-lex the tokens to get precise location of last 'const'
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +000042static llvm::Optional<Token> ConstTok(CharSourceRange Range,
43 const MatchFinder::MatchResult &Result) {
Matthias Gehre018c1d42016-04-12 05:45:13 +000044 const SourceManager &Sources = *Result.SourceManager;
45 std::pair<FileID, unsigned> LocInfo =
46 Sources.getDecomposedLoc(Range.getBegin());
47 StringRef File = Sources.getBufferData(LocInfo.first);
48 const char *TokenBegin = File.data() + LocInfo.second;
49 Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first),
50 Result.Context->getLangOpts(), File.begin(), TokenBegin,
51 File.end());
52 Token Tok;
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +000053 llvm::Optional<Token> ConstTok;
Matthias Gehre018c1d42016-04-12 05:45:13 +000054 while (!RawLexer.LexFromRawLexer(Tok)) {
55 if (Sources.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation()))
56 break;
57 if (Tok.is(tok::raw_identifier)) {
58 IdentifierInfo &Info = Result.Context->Idents.get(StringRef(
59 Sources.getCharacterData(Tok.getLocation()), Tok.getLength()));
60 Tok.setIdentifierInfo(&Info);
61 Tok.setKind(Info.getTokenID());
62 }
63 if (Tok.is(tok::kw_const))
64 ConstTok = Tok;
65 }
66 return ConstTok;
67}
68
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000069void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) {
70 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
71 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
72
Matthias Gehre018c1d42016-04-12 05:45:13 +000073 if (!Param->getType().isLocalConstQualified())
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000074 return;
75
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +000076 auto Diag = diag(Param->getLocStart(),
77 "parameter %0 is const-qualified in the function "
78 "declaration; const-qualification of parameters only has an "
79 "effect in function definitions");
80 if (Param->getName().empty()) {
81 for (unsigned int i = 0; i < Func->getNumParams(); ++i) {
82 if (Param == Func->getParamDecl(i)) {
83 Diag << (i + 1);
84 break;
85 }
86 }
87 } else {
88 Diag << Param;
89 }
Matthias Gehre018c1d42016-04-12 05:45:13 +000090
Samuel Benzaquen79c76102016-06-06 14:21:11 +000091 if (Param->getLocStart().isMacroID() != Param->getLocEnd().isMacroID()) {
92 // Do not offer a suggestion if the part of the variable declaration comes
93 // from a macro.
94 return;
95 }
96
Matthias Gehre018c1d42016-04-12 05:45:13 +000097 CharSourceRange FileRange = Lexer::makeFileCharRange(
98 CharSourceRange::getTokenRange(getTypeRange(*Param)),
99 *Result.SourceManager, Result.Context->getLangOpts());
100
101 if (!FileRange.isValid())
102 return;
103
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +0000104 auto Tok = ConstTok(FileRange, Result);
105 if (!Tok)
106 return;
Matthias Gehre018c1d42016-04-12 05:45:13 +0000107 Diag << FixItHint::CreateRemoval(
Samuel Benzaquenaa05ae92016-06-01 20:37:23 +0000108 CharSourceRange::getTokenRange(Tok->getLocation(), Tok->getLocation()));
Alexander Kornienkoe3ae0c62016-03-30 11:31:33 +0000109}
110
111} // namespace readability
112} // namespace tidy
113} // namespace clang