Alexander Kornienko | e3ae0c6 | 2016-03-30 11:31:33 +0000 | [diff] [blame] | 1 | //===--- 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 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace tidy { |
| 19 | namespace readability { |
| 20 | namespace { |
| 21 | |
| 22 | SourceRange 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 Kornienko | e3ae0c6 | 2016-03-30 11:31:33 +0000 | [diff] [blame] | 31 | void 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 Gehre | 018c1d4 | 2016-04-12 05:45:13 +0000 | [diff] [blame^] | 40 | // Re-lex the tokens to get precise location of last 'const' |
| 41 | static 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 Kornienko | e3ae0c6 | 2016-03-30 11:31:33 +0000 | [diff] [blame] | 68 | void 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 Gehre | 018c1d4 | 2016-04-12 05:45:13 +0000 | [diff] [blame^] | 72 | if (!Param->getType().isLocalConstQualified()) |
Alexander Kornienko | e3ae0c6 | 2016-03-30 11:31:33 +0000 | [diff] [blame] | 73 | return; |
| 74 | |
Alexander Kornienko | e3ae0c6 | 2016-03-30 11:31:33 +0000 | [diff] [blame] | 75 | 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 Gehre | 018c1d4 | 2016-04-12 05:45:13 +0000 | [diff] [blame^] | 89 | |
| 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 Kornienko | e3ae0c6 | 2016-03-30 11:31:33 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | } // namespace readability |
| 103 | } // namespace tidy |
| 104 | } // namespace clang |