diff options
author | Aaron Ballman <aaron@aaronballman.com> | 2018-10-31 19:11:38 +0000 |
---|---|---|
committer | Aaron Ballman <aaron@aaronballman.com> | 2018-10-31 19:11:38 +0000 |
commit | 3a02722a40677c6ea39b9fe8624f439c417c282c (patch) | |
tree | b284261848eac1ae04a5d324e5619bc3b91f20bb /clang-tools-extra/clang-tidy/utils/LexerUtils.cpp | |
parent | c6c627253db6b273dd995771e7af17c53858eaba (diff) | |
download | bcm5719-llvm-3a02722a40677c6ea39b9fe8624f439c417c282c.tar.gz bcm5719-llvm-3a02722a40677c6ea39b9fe8624f439c417c282c.zip |
Implement the readability-const-return-type check.
This check flags function top-level const-qualified return types and suggests removing the mostly-superfluous const qualifier where possible.
Patch by Yitzhak Mandelbaum.
llvm-svn: 345764
Diffstat (limited to 'clang-tools-extra/clang-tidy/utils/LexerUtils.cpp')
-rw-r--r-- | clang-tools-extra/clang-tidy/utils/LexerUtils.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp b/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp index 82097978129..edd4cd62d53 100644 --- a/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp +++ b/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp @@ -92,6 +92,34 @@ bool rangeContainsExpansionsOrDirectives(SourceRange Range, return false; } + +llvm::Optional<Token> getConstQualifyingToken(CharSourceRange Range, + const ASTContext &Context, + const SourceManager &SM) { + std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Range.getBegin()); + StringRef File = SM.getBufferData(LocInfo.first); + Lexer RawLexer(SM.getLocForStartOfFile(LocInfo.first), Context.getLangOpts(), + File.begin(), File.data() + LocInfo.second, File.end()); + llvm::Optional<Token> FirstConstTok; + Token LastTokInRange; + Token Tok; + while (!RawLexer.LexFromRawLexer(Tok) && + Range.getEnd() != Tok.getLocation() && + !SM.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation())) { + if (Tok.is(tok::raw_identifier)) { + IdentifierInfo &Info = Context.Idents.get( + StringRef(SM.getCharacterData(Tok.getLocation()), Tok.getLength())); + Tok.setIdentifierInfo(&Info); + Tok.setKind(Info.getTokenID()); + } + if (Tok.is(tok::kw_const) && !FirstConstTok) + FirstConstTok = Tok; + LastTokInRange = Tok; + } + // If the last token in the range is a `const`, then it const qualifies the + // type. Otherwise, the first `const` token, if any, is the qualifier. + return LastTokInRange.is(tok::kw_const) ? LastTokInRange : FirstConstTok; +} } // namespace lexer } // namespace utils } // namespace tidy |