Skip to content

Commit 829b1c1

Browse files
committed
[Clang] Add IsCurrentLexingTokAtPhysicalStartOfLine in Lexer to avoid pass this flag as argument
Signed-off-by: yronglin <[email protected]>
1 parent e25019f commit 829b1c1

File tree

2 files changed

+28
-32
lines changed

2 files changed

+28
-32
lines changed

clang/include/clang/Lex/Lexer.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ class Lexer : public PreprocessorLexer {
136136

137137
bool IsAtPhysicalStartOfLine;
138138

139+
bool IsCurrentLexingTokAtPhysicalStartOfLine;
140+
139141
bool HasLeadingSpace;
140142

141143
bool HasLeadingEmptyMacro;
@@ -609,7 +611,7 @@ class Lexer : public PreprocessorLexer {
609611
/// LexTokenInternal - Internal interface to lex a preprocessing token. Called
610612
/// by Lex.
611613
///
612-
bool LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine);
614+
bool LexTokenInternal(Token &Result);
613615

614616
bool CheckUnicodeWhitespace(Token &Result, uint32_t C, const char *CurPtr);
615617

@@ -749,12 +751,9 @@ class Lexer : public PreprocessorLexer {
749751
bool LexCharConstant (Token &Result, const char *CurPtr,
750752
tok::TokenKind Kind);
751753
bool LexEndOfFile (Token &Result, const char *CurPtr);
752-
bool SkipWhitespace (Token &Result, const char *CurPtr,
753-
bool &TokAtPhysicalStartOfLine);
754-
bool SkipLineComment (Token &Result, const char *CurPtr,
755-
bool &TokAtPhysicalStartOfLine);
756-
bool SkipBlockComment (Token &Result, const char *CurPtr,
757-
bool &TokAtPhysicalStartOfLine);
754+
bool SkipWhitespace (Token &Result, const char *CurPtr);
755+
bool SkipLineComment (Token &Result, const char *CurPtr);
756+
bool SkipBlockComment (Token &Result, const char *CurPtr);
758757
bool SaveLineComment (Token &Result, const char *CurPtr);
759758

760759
bool IsStartOfConflictMarker(const char *CurPtr);

clang/lib/Lex/Lexer.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,8 +2484,7 @@ bool Lexer::LexCharConstant(Token &Result, const char *CurPtr,
24842484
/// Update BufferPtr to point to the next non-whitespace character and return.
24852485
///
24862486
/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
2487-
bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr,
2488-
bool &TokAtPhysicalStartOfLine) {
2487+
bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
24892488
// Whitespace - Skip it, then return the token after the whitespace.
24902489
bool SawNewline = isVerticalWhitespace(CurPtr[-1]);
24912490

@@ -2541,7 +2540,7 @@ bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr,
25412540
Result.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
25422541
if (SawNewline) {
25432542
Result.setFlag(Token::StartOfLine);
2544-
TokAtPhysicalStartOfLine = true;
2543+
IsCurrentLexingTokAtPhysicalStartOfLine = true;
25452544

25462545
if (NewLinePtr && lastNewLine && NewLinePtr != lastNewLine && PP) {
25472546
if (auto *Handler = PP->getEmptylineHandler())
@@ -2560,8 +2559,7 @@ bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr,
25602559
///
25612560
/// If we're in KeepCommentMode or any CommentHandler has inserted
25622561
/// some tokens, this will store the first token and return true.
2563-
bool Lexer::SkipLineComment(Token &Result, const char *CurPtr,
2564-
bool &TokAtPhysicalStartOfLine) {
2562+
bool Lexer::SkipLineComment(Token &Result, const char *CurPtr) {
25652563
// If Line comments aren't explicitly enabled for this language, emit an
25662564
// extension warning.
25672565
if (!LineComment) {
@@ -2717,7 +2715,7 @@ bool Lexer::SkipLineComment(Token &Result, const char *CurPtr,
27172715

27182716
// The next returned token is at the start of the line.
27192717
Result.setFlag(Token::StartOfLine);
2720-
TokAtPhysicalStartOfLine = true;
2718+
IsCurrentLexingTokAtPhysicalStartOfLine = true;
27212719
// No leading whitespace seen so far.
27222720
Result.clearFlag(Token::LeadingSpace);
27232721
BufferPtr = CurPtr;
@@ -2842,8 +2840,7 @@ static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr, Lexer *L,
28422840
///
28432841
/// If we're in KeepCommentMode or any CommentHandler has inserted
28442842
/// some tokens, this will store the first token and return true.
2845-
bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr,
2846-
bool &TokAtPhysicalStartOfLine) {
2843+
bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
28472844
// Scan one character past where we should, looking for a '/' character. Once
28482845
// we find it, check to see if it was preceded by a *. This common
28492846
// optimization helps people who like to put a lot of * characters in their
@@ -3046,7 +3043,7 @@ bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr,
30463043
// efficiently now. This is safe even in KeepWhitespaceMode because we would
30473044
// have already returned above with the comment as a token.
30483045
if (isHorizontalWhitespace(*CurPtr)) {
3049-
SkipWhitespace(Result, CurPtr+1, TokAtPhysicalStartOfLine);
3046+
SkipWhitespace(Result, CurPtr + 1);
30503047
return false;
30513048
}
30523049

@@ -3698,11 +3695,11 @@ bool Lexer::Lex(Token &Result) {
36983695
HasLeadingEmptyMacro = false;
36993696
}
37003697

3701-
bool atPhysicalStartOfLine = IsAtPhysicalStartOfLine;
3698+
IsCurrentLexingTokAtPhysicalStartOfLine = IsAtPhysicalStartOfLine;
37023699
IsAtPhysicalStartOfLine = false;
37033700
bool isRawLex = isLexingRawMode();
37043701
(void) isRawLex;
3705-
bool returnedToken = LexTokenInternal(Result, atPhysicalStartOfLine);
3702+
bool returnedToken = LexTokenInternal(Result);
37063703
// (After the LexTokenInternal call, the lexer might be destroyed.)
37073704
assert((returnedToken || !isRawLex) && "Raw lex must succeed");
37083705
return returnedToken;
@@ -3713,7 +3710,7 @@ bool Lexer::Lex(Token &Result) {
37133710
/// has a null character at the end of the file. This returns a preprocessing
37143711
/// token, not a normal token, as such, it is an internal interface. It assumes
37153712
/// that the Flags of result have been cleared before calling this.
3716-
bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
3713+
bool Lexer::LexTokenInternal(Token &Result) {
37173714
LexStart:
37183715
assert(!Result.needsCleaning() && "Result needs cleaning");
37193716
assert(!Result.hasPtrData() && "Result has not been reset");
@@ -3766,7 +3763,7 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
37663763
if (!isLexingRawMode())
37673764
Diag(CurPtr-1, diag::null_in_file);
37683765
Result.setFlag(Token::LeadingSpace);
3769-
if (SkipWhitespace(Result, CurPtr, TokAtPhysicalStartOfLine))
3766+
if (SkipWhitespace(Result, CurPtr))
37703767
return true; // KeepWhitespaceMode
37713768

37723769
// We know the lexer hasn't changed, so just try again with this lexer.
@@ -3812,7 +3809,7 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
38123809
// No leading whitespace seen so far.
38133810
Result.clearFlag(Token::LeadingSpace);
38143811

3815-
if (SkipWhitespace(Result, CurPtr, TokAtPhysicalStartOfLine))
3812+
if (SkipWhitespace(Result, CurPtr))
38163813
return true; // KeepWhitespaceMode
38173814

38183815
// We only saw whitespace, so just try again with this lexer.
@@ -3824,7 +3821,7 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
38243821
case '\v':
38253822
SkipHorizontalWhitespace:
38263823
Result.setFlag(Token::LeadingSpace);
3827-
if (SkipWhitespace(Result, CurPtr, TokAtPhysicalStartOfLine))
3824+
if (SkipWhitespace(Result, CurPtr))
38283825
return true; // KeepWhitespaceMode
38293826

38303827
SkipIgnoredUnits:
@@ -3834,11 +3831,11 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
38343831
// too (without going through the big switch stmt).
38353832
if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
38363833
LineComment && (LangOpts.CPlusPlus || !LangOpts.TraditionalCPP)) {
3837-
if (SkipLineComment(Result, CurPtr+2, TokAtPhysicalStartOfLine))
3834+
if (SkipLineComment(Result, CurPtr + 2))
38383835
return true; // There is a token to return.
38393836
goto SkipIgnoredUnits;
38403837
} else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
3841-
if (SkipBlockComment(Result, CurPtr+2, TokAtPhysicalStartOfLine))
3838+
if (SkipBlockComment(Result, CurPtr + 2))
38423839
return true; // There is a token to return.
38433840
goto SkipIgnoredUnits;
38443841
} else if (isHorizontalWhitespace(*CurPtr)) {
@@ -4150,8 +4147,7 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
41504147
TreatAsComment = getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*';
41514148

41524149
if (TreatAsComment) {
4153-
if (SkipLineComment(Result, ConsumeChar(CurPtr, SizeTmp, Result),
4154-
TokAtPhysicalStartOfLine))
4150+
if (SkipLineComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
41554151
return true; // There is a token to return.
41564152

41574153
// It is common for the tokens immediately after a // comment to be
@@ -4162,8 +4158,7 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
41624158
}
41634159

41644160
if (Char == '*') { // /**/ comment.
4165-
if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result),
4166-
TokAtPhysicalStartOfLine))
4161+
if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
41674162
return true; // There is a token to return.
41684163

41694164
// We only saw whitespace, so just try again with this lexer.
@@ -4203,7 +4198,8 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
42034198
// it's actually the start of a preprocessing directive. Callback to
42044199
// the preprocessor to handle it.
42054200
// TODO: -fpreprocessed mode??
4206-
if (TokAtPhysicalStartOfLine && !LexingRawMode && !Is_PragmaLexer)
4201+
if (IsCurrentLexingTokAtPhysicalStartOfLine && !LexingRawMode &&
4202+
!Is_PragmaLexer)
42074203
goto HandleDirective;
42084204

42094205
Kind = tok::hash;
@@ -4392,7 +4388,8 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
43924388
// it's actually the start of a preprocessing directive. Callback to
43934389
// the preprocessor to handle it.
43944390
// TODO: -fpreprocessed mode??
4395-
if (TokAtPhysicalStartOfLine && !LexingRawMode && !Is_PragmaLexer)
4391+
if (IsCurrentLexingTokAtPhysicalStartOfLine && !LexingRawMode &&
4392+
!Is_PragmaLexer)
43964393
goto HandleDirective;
43974394

43984395
Kind = tok::hash;
@@ -4412,7 +4409,7 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
44124409
if (!LangOpts.AsmPreprocessor) {
44134410
if (uint32_t CodePoint = tryReadUCN(CurPtr, BufferPtr, &Result)) {
44144411
if (CheckUnicodeWhitespace(Result, CodePoint, CurPtr)) {
4415-
if (SkipWhitespace(Result, CurPtr, TokAtPhysicalStartOfLine))
4412+
if (SkipWhitespace(Result, CurPtr))
44164413
return true; // KeepWhitespaceMode
44174414

44184415
// We only saw whitespace, so just try again with this lexer.
@@ -4445,7 +4442,7 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
44454442
llvm::strictConversion);
44464443
if (Status == llvm::conversionOK) {
44474444
if (CheckUnicodeWhitespace(Result, CodePoint, CurPtr)) {
4448-
if (SkipWhitespace(Result, CurPtr, TokAtPhysicalStartOfLine))
4445+
if (SkipWhitespace(Result, CurPtr))
44494446
return true; // KeepWhitespaceMode
44504447

44514448
// We only saw whitespace, so just try again with this lexer.

0 commit comments

Comments
 (0)