diff options
| author | Ilya Mirsky <ilya.mirsky@ericsson.com> | 2019-12-24 10:10:01 -0500 |
|---|---|---|
| committer | Aaron Ballman <aaron@aaronballman.com> | 2019-12-24 10:10:01 -0500 |
| commit | f58f39137c6e5a324ef684b1d72bddae244aa94d (patch) | |
| tree | 4ee63f5b7e676a6f2248a9ce53f924964e6d9d03 /clang-tools-extra/clang-tidy | |
| parent | c16b3ec597d277b5a7397db308f8ec730f3330a3 (diff) | |
| download | bcm5719-llvm-f58f39137c6e5a324ef684b1d72bddae244aa94d.tar.gz bcm5719-llvm-f58f39137c6e5a324ef684b1d72bddae244aa94d.zip | |
Fix readability-const-return-type identifying the wrong `const` token
Replace tidy::utils::lexer::getConstQualifyingToken with a corrected and also
generalized to other qualifiers variant - getQualifyingToken.
Fixes PR44326
Diffstat (limited to 'clang-tools-extra/clang-tidy')
| -rw-r--r-- | clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp | 4 | ||||
| -rw-r--r-- | clang-tools-extra/clang-tidy/utils/LexerUtils.cpp | 33 | ||||
| -rw-r--r-- | clang-tools-extra/clang-tidy/utils/LexerUtils.h | 14 |
3 files changed, 32 insertions, 19 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp index 0f237ec8ee1..d987886b62b 100644 --- a/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ConstReturnTypeCheck.cpp @@ -47,8 +47,8 @@ findConstToRemove(const FunctionDecl *Def, if (FileRange.isInvalid()) return None; - return utils::lexer::getConstQualifyingToken(FileRange, *Result.Context, - *Result.SourceManager); + return utils::lexer::getQualifyingToken( + tok::kw_const, FileRange, *Result.Context, *Result.SourceManager); } namespace { diff --git a/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp b/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp index e80fda6e318..17838fe577f 100644 --- a/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp +++ b/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp @@ -102,15 +102,20 @@ bool rangeContainsExpansionsOrDirectives(SourceRange Range, return false; } -llvm::Optional<Token> getConstQualifyingToken(CharSourceRange Range, - const ASTContext &Context, - const SourceManager &SM) { +llvm::Optional<Token> getQualifyingToken(tok::TokenKind TK, + CharSourceRange Range, + const ASTContext &Context, + const SourceManager &SM) { + assert((TK == tok::kw_const || TK == tok::kw_volatile || + TK == tok::kw_restrict) && + "TK is not a qualifier keyword"); 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; + llvm::Optional<Token> LastMatchBeforeTemplate; + llvm::Optional<Token> LastMatchAfterTemplate; + bool SawTemplate = false; Token Tok; while (!RawLexer.LexFromRawLexer(Tok) && Range.getEnd() != Tok.getLocation() && @@ -121,13 +126,19 @@ llvm::Optional<Token> getConstQualifyingToken(CharSourceRange Range, Tok.setIdentifierInfo(&Info); Tok.setKind(Info.getTokenID()); } - if (Tok.is(tok::kw_const) && !FirstConstTok) - FirstConstTok = Tok; - LastTokInRange = Tok; + if (Tok.is(tok::less)) + SawTemplate = true; + else if (Tok.isOneOf(tok::greater, tok::greatergreater)) + LastMatchAfterTemplate = None; + else if (Tok.is(TK)) { + if (SawTemplate) + LastMatchAfterTemplate = Tok; + else + LastMatchBeforeTemplate = 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; + return LastMatchAfterTemplate != None ? LastMatchAfterTemplate + : LastMatchBeforeTemplate; } } // namespace lexer } // namespace utils diff --git a/clang-tools-extra/clang-tidy/utils/LexerUtils.h b/clang-tools-extra/clang-tidy/utils/LexerUtils.h index 2c4a2518259..fcf9ada85ff 100644 --- a/clang-tools-extra/clang-tidy/utils/LexerUtils.h +++ b/clang-tools-extra/clang-tidy/utils/LexerUtils.h @@ -92,13 +92,15 @@ bool rangeContainsExpansionsOrDirectives(SourceRange Range, const SourceManager &SM, const LangOptions &LangOpts); -/// Assuming that ``Range`` spans a const-qualified type, returns the ``const`` -/// token in ``Range`` that is responsible for const qualification. ``Range`` -/// must be valid with respect to ``SM``. Returns ``None`` if no ``const`` +/// Assuming that ``Range`` spans a CVR-qualified type, returns the +/// token in ``Range`` that is responsible for the qualification. ``Range`` +/// must be valid with respect to ``SM``. Returns ``None`` if no qualifying /// tokens are found. -llvm::Optional<Token> getConstQualifyingToken(CharSourceRange Range, - const ASTContext &Context, - const SourceManager &SM); +/// \note: doesn't support member function qualifiers. +llvm::Optional<Token> getQualifyingToken(tok::TokenKind TK, + CharSourceRange Range, + const ASTContext &Context, + const SourceManager &SM); } // namespace lexer } // namespace utils |

