Skip to content

Commit c951d76

Browse files
vbvictorzeyi2
andauthored
[clang-tidy][NFC] Convert Lexer utils to use std::optional<Token> (llvm#174809)
This bring a more unified api and avoid caveats like "return ``tok::unknown`` if not found.", which makes easier to forget error checking. --------- Co-authored-by: mitchell <zeyi2@nekoarch.cc>
1 parent 1658456 commit c951d76

12 files changed

Lines changed: 70 additions & 60 deletions

clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,16 @@ static std::vector<std::pair<SourceLocation, StringRef>>
127127
getCommentsBeforeLoc(ASTContext *Ctx, SourceLocation Loc) {
128128
std::vector<std::pair<SourceLocation, StringRef>> Comments;
129129
while (Loc.isValid()) {
130-
const clang::Token Tok = utils::lexer::getPreviousToken(
130+
const std::optional<Token> Tok = utils::lexer::getPreviousToken(
131131
Loc, Ctx->getSourceManager(), Ctx->getLangOpts(),
132132
/*SkipComments=*/false);
133-
if (Tok.isNot(tok::comment))
133+
if (!Tok || Tok->isNot(tok::comment))
134134
break;
135-
Loc = Tok.getLocation();
135+
Loc = Tok->getLocation();
136136
Comments.emplace_back(
137137
Loc,
138138
Lexer::getSourceText(CharSourceRange::getCharRange(
139-
Loc, Loc.getLocWithOffset(Tok.getLength())),
139+
Loc, Loc.getLocWithOffset(Tok->getLength())),
140140
Ctx->getSourceManager(), Ctx->getLangOpts()));
141141
}
142142
return Comments;

clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,11 @@ void OptionalValueConversionCheck::check(
141141
}
142142
if (const auto *CallExpr =
143143
Result.Nodes.getNodeAs<CXXMemberCallExpr>("member-call")) {
144-
const SourceLocation Begin =
145-
utils::lexer::getPreviousToken(CallExpr->getExprLoc(),
146-
*Result.SourceManager, getLangOpts())
147-
.getLocation();
144+
const std::optional<Token> Tok = utils::lexer::getPreviousToken(
145+
CallExpr->getExprLoc(), *Result.SourceManager, getLangOpts());
146+
if (!Tok)
147+
return;
148+
const SourceLocation Begin = Tok->getLocation();
148149
auto Diag =
149150
diag(CallExpr->getExprLoc(),
150151
"remove call to %0 to silence this warning", DiagnosticIDs::Note);

clang-tools-extra/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ void SuspiciousSemicolonCheck::check(const MatchFinder::MatchResult &Result) {
3737
return;
3838

3939
ASTContext &Ctxt = *Result.Context;
40-
auto Token = utils::lexer::getPreviousToken(LocStart, Ctxt.getSourceManager(),
41-
Ctxt.getLangOpts());
42-
auto &SM = *Result.SourceManager;
40+
const auto &SM = *Result.SourceManager;
4341
const unsigned SemicolonLine = SM.getSpellingLineNumber(LocStart);
4442

4543
const auto *Statement = Result.Nodes.getNodeAs<Stmt>("stmt");
4644
const bool IsIfStmt = isa<IfStmt>(Statement);
4745

48-
if (!IsIfStmt &&
49-
SM.getSpellingLineNumber(Token.getLocation()) != SemicolonLine)
46+
const std::optional<Token> PrevTok = utils::lexer::getPreviousToken(
47+
LocStart, Ctxt.getSourceManager(), Ctxt.getLangOpts());
48+
if (!PrevTok || (!IsIfStmt && SM.getSpellingLineNumber(
49+
PrevTok->getLocation()) != SemicolonLine))
5050
return;
5151

5252
const SourceLocation LocEnd = Semicolon->getEndLoc();
@@ -55,6 +55,7 @@ void SuspiciousSemicolonCheck::check(const MatchFinder::MatchResult &Result) {
5555
Lexer Lexer(SM.getLocForStartOfFile(FID), Ctxt.getLangOpts(),
5656
Buffer.getBufferStart(), SM.getCharacterData(LocEnd) + 1,
5757
Buffer.getBufferEnd());
58+
Token Token;
5859
if (Lexer.LexFromRawLexer(Token))
5960
return;
6061

clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,20 @@ struct InitializerInsertion {
149149
"insertion represents a new initializer list.");
150150
SourceLocation Location;
151151
switch (Placement) {
152-
case InitializerPlacement::New:
153-
Location = utils::lexer::getPreviousToken(
154-
Constructor.getBody()->getBeginLoc(),
155-
Context.getSourceManager(), Context.getLangOpts())
156-
.getLocation();
152+
case InitializerPlacement::New: {
153+
const std::optional<Token> Tok = utils::lexer::getPreviousToken(
154+
Constructor.getBody()->getBeginLoc(), Context.getSourceManager(),
155+
Context.getLangOpts());
156+
Location = Tok ? Tok->getLocation() : SourceLocation{};
157157
break;
158-
case InitializerPlacement::Before:
159-
Location = utils::lexer::getPreviousToken(
160-
Where->getSourceRange().getBegin(),
161-
Context.getSourceManager(), Context.getLangOpts())
162-
.getLocation();
158+
}
159+
case InitializerPlacement::Before: {
160+
const std::optional<Token> Tok = utils::lexer::getPreviousToken(
161+
Where->getSourceRange().getBegin(), Context.getSourceManager(),
162+
Context.getLangOpts());
163+
Location = Tok ? Tok->getLocation() : SourceLocation{};
163164
break;
165+
}
164166
case InitializerPlacement::After:
165167
Location = Where->getRParenLoc();
166168
break;

clang-tools-extra/clang-tidy/modernize/UseConstraintsCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,11 @@ static std::optional<std::string> getConditionText(const Expr *ConditionExpr,
318318
return std::nullopt;
319319

320320
const bool SkipComments = false;
321-
Token PrevToken;
321+
std::optional<Token> PrevToken;
322322
std::tie(PrevToken, PrevTokenLoc) = utils::lexer::getPreviousTokenAndStart(
323323
PrevTokenLoc, SM, LangOpts, SkipComments);
324324
const bool EndsWithDoubleSlash =
325-
PrevToken.is(tok::comment) &&
325+
PrevToken && PrevToken->is(tok::comment) &&
326326
Lexer::getSourceText(CharSourceRange::getCharRange(
327327
PrevTokenLoc, PrevTokenLoc.getLocWithOffset(2)),
328328
SM, LangOpts) == "//";

clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,15 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
144144
SourceLocation EndLoc = MatchedDecl->getLocation();
145145

146146
while (true) {
147-
const Token Prev = utils::lexer::getPreviousToken(StartLoc, SM, LO);
147+
const std::optional<Token> Prev =
148+
utils::lexer::getPreviousToken(StartLoc, SM, LO);
148149
const std::optional<Token> Next =
149150
utils::lexer::findNextTokenSkippingComments(EndLoc, SM, LO);
150-
if (Prev.isNot(tok::l_paren) || !Next || Next->isNot(tok::r_paren))
151+
if (!Prev || Prev->isNot(tok::l_paren) || !Next ||
152+
Next->isNot(tok::r_paren))
151153
break;
152154

153-
StartLoc = Prev.getLocation();
155+
StartLoc = Prev->getLocation();
154156
EndLoc = Next->getLocation();
155157
}
156158

clang-tools-extra/clang-tidy/readability/DeleteNullPointerCheck.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,18 @@ void DeleteNullPointerCheck::check(const MatchFinder::MatchResult &Result) {
5252
auto Diag = diag(
5353
IfWithDelete->getBeginLoc(),
5454
"'if' statement is unnecessary; deleting null pointer has no effect");
55-
if (IfWithDelete->getElse())
55+
if (IfWithDelete->hasElseStorage())
5656
return;
5757
// FIXME: generate fixit for this case.
5858

59+
const std::optional<Token> PrevTok = utils::lexer::getPreviousToken(
60+
IfWithDelete->getThen()->getBeginLoc(), *Result.SourceManager,
61+
Result.Context->getLangOpts());
62+
if (!PrevTok)
63+
return;
64+
5965
Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
60-
IfWithDelete->getBeginLoc(),
61-
utils::lexer::getPreviousToken(IfWithDelete->getThen()->getBeginLoc(),
62-
*Result.SourceManager,
63-
Result.Context->getLangOpts())
64-
.getLocation()));
66+
IfWithDelete->getBeginLoc(), PrevTok->getLocation()));
6567

6668
if (Compound) {
6769
Diag << FixItHint::CreateRemoval(

clang-tools-extra/clang-tidy/readability/IsolateDeclarationCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ declRanges(const DeclStmt *DS, const SourceManager &SM,
157157
if (Start.isInvalid() || Start.isMacroID())
158158
break;
159159

160-
const Token T = getPreviousToken(Start, SM, LangOpts);
161-
if (T.is(tok::l_paren)) {
160+
const std::optional<Token> T = getPreviousToken(Start, SM, LangOpts);
161+
if (T && T->is(tok::l_paren)) {
162162
Start = findPreviousTokenStart(Start, SM, LangOpts);
163163
continue;
164164
}

clang-tools-extra/clang-tidy/readability/RedundantMemberInitCheck.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ namespace clang::tidy::readability {
2121
static SourceRange
2222
getFullInitRangeInclWhitespaces(SourceRange Range, const SourceManager &SM,
2323
const LangOptions &LangOpts) {
24-
const Token PrevToken =
24+
const std::optional<Token> PrevToken =
2525
utils::lexer::getPreviousToken(Range.getBegin(), SM, LangOpts, false);
26-
if (PrevToken.is(tok::unknown))
26+
if (!PrevToken)
2727
return Range;
2828

29-
if (PrevToken.isNot(tok::equal))
30-
return {PrevToken.getEndLoc(), Range.getEnd()};
29+
if (PrevToken->isNot(tok::equal))
30+
return {PrevToken->getEndLoc(), Range.getEnd()};
3131

3232
return getFullInitRangeInclWhitespaces(
33-
{PrevToken.getLocation(), Range.getEnd()}, SM, LangOpts);
33+
{PrevToken->getLocation(), Range.getEnd()}, SM, LangOpts);
3434
}
3535

3636
void RedundantMemberInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {

clang-tools-extra/clang-tidy/utils/FixItHintUtils.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ namespace clang::tidy::utils::fixit {
1919

2020
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context) {
2121
SourceLocation AmpLocation = Var.getLocation();
22-
auto Token = utils::lexer::getPreviousToken(
22+
const std::optional<Token> Token = utils::lexer::getPreviousToken(
2323
AmpLocation, Context.getSourceManager(), Context.getLangOpts());
2424

2525
// For parameter packs the '&' must go before the '...' token
26-
if (Token.is(tok::ellipsis))
27-
return FixItHint::CreateInsertion(Token.getLocation(), "&");
26+
if (Token && Token->is(tok::ellipsis))
27+
return FixItHint::CreateInsertion(Token->getLocation(), "&");
2828

29-
if (!Token.is(tok::unknown))
30-
AmpLocation = Lexer::getLocForEndOfToken(Token.getLocation(), 0,
29+
if (Token)
30+
AmpLocation = Lexer::getLocForEndOfToken(Token->getLocation(), 0,
3131
Context.getSourceManager(),
3232
Context.getLangOpts());
3333
return FixItHint::CreateInsertion(AmpLocation, "&");
@@ -53,10 +53,9 @@ skipLParensBackwards(SourceLocation Start, const ASTContext &Context) {
5353
return std::nullopt;
5454

5555
auto PreviousTokenLParen = [&Start, &Context]() {
56-
Token T;
57-
T = lexer::getPreviousToken(Start, Context.getSourceManager(),
58-
Context.getLangOpts());
59-
return T.is(tok::l_paren);
56+
const std::optional<Token> T = lexer::getPreviousToken(
57+
Start, Context.getSourceManager(), Context.getLangOpts());
58+
return T && T->is(tok::l_paren);
6059
};
6160

6261
while (Start.isValid() && PreviousTokenLParen())

0 commit comments

Comments
 (0)