Skip to content

Commit

Permalink
[NFC][Lexer] Make access to LangOpts more consistent
Browse files Browse the repository at this point in the history
Before this change without any good reason Lexer::LangOpts is sometimes accessed by getter and another time read directly in Lexer functions.
Since getLangOpts is a bit more verbose prefer direct access to LangOpts member when possible.

Differential Revision: https://reviews.llvm.org/D120333
  • Loading branch information
yurai007 committed Feb 23, 2022
1 parent 6777ec9 commit fbe38a7
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions clang/lib/Lex/Lexer.cpp
Expand Up @@ -1881,7 +1881,7 @@ bool Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
if (!LangOpts.C99) {
if (!isHexaLiteral(BufferPtr, LangOpts))
IsHexFloat = false;
else if (!getLangOpts().CPlusPlus17 &&
else if (!LangOpts.CPlusPlus17 &&
std::find(BufferPtr, CurPtr, '_') != CurPtr)
IsHexFloat = false;
}
Expand All @@ -1890,12 +1890,12 @@ bool Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
}

// If we have a digit separator, continue.
if (C == '\'' && (getLangOpts().CPlusPlus14 || getLangOpts().C2x)) {
if (C == '\'' && (LangOpts.CPlusPlus14 || LangOpts.C2x)) {
unsigned NextSize;
char Next = getCharAndSizeNoWarn(CurPtr + Size, NextSize, getLangOpts());
char Next = getCharAndSizeNoWarn(CurPtr + Size, NextSize, LangOpts);
if (isAsciiIdentifierContinue(Next)) {
if (!isLexingRawMode())
Diag(CurPtr, getLangOpts().CPlusPlus
Diag(CurPtr, LangOpts.CPlusPlus
? diag::warn_cxx11_compat_digit_separator
: diag::warn_c2x_compat_digit_separator);
CurPtr = ConsumeChar(CurPtr, Size, Result);
Expand All @@ -1921,7 +1921,7 @@ bool Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
/// in C++11, or warn on a ud-suffix in C++98.
const char *Lexer::LexUDSuffix(Token &Result, const char *CurPtr,
bool IsStringLiteral) {
assert(getLangOpts().CPlusPlus);
assert(LangOpts.CPlusPlus);

// Maximally munch an identifier.
unsigned Size;
Expand All @@ -1937,7 +1937,7 @@ const char *Lexer::LexUDSuffix(Token &Result, const char *CurPtr,
return CurPtr;
}

if (!getLangOpts().CPlusPlus11) {
if (!LangOpts.CPlusPlus11) {
if (!isLexingRawMode())
Diag(CurPtr,
C == '_' ? diag::warn_cxx11_compat_user_defined_literal
Expand All @@ -1955,7 +1955,7 @@ const char *Lexer::LexUDSuffix(Token &Result, const char *CurPtr,
bool IsUDSuffix = false;
if (C == '_')
IsUDSuffix = true;
else if (IsStringLiteral && getLangOpts().CPlusPlus14) {
else if (IsStringLiteral && LangOpts.CPlusPlus14) {
// In C++1y, we need to look ahead a few characters to see if this is a
// valid suffix for a string literal or a numeric literal (this could be
// the 'operator""if' defining a numeric literal operator).
Expand All @@ -1965,13 +1965,12 @@ const char *Lexer::LexUDSuffix(Token &Result, const char *CurPtr,
unsigned Chars = 1;
while (true) {
unsigned NextSize;
char Next = getCharAndSizeNoWarn(CurPtr + Consumed, NextSize,
getLangOpts());
char Next = getCharAndSizeNoWarn(CurPtr + Consumed, NextSize, LangOpts);
if (!isAsciiIdentifierContinue(Next)) {
// End of suffix. Check whether this is on the allowed list.
const StringRef CompleteSuffix(Buffer, Chars);
IsUDSuffix = StringLiteralParser::isValidUDSuffix(getLangOpts(),
CompleteSuffix);
IsUDSuffix =
StringLiteralParser::isValidUDSuffix(LangOpts, CompleteSuffix);
break;
}

Expand All @@ -1986,10 +1985,10 @@ const char *Lexer::LexUDSuffix(Token &Result, const char *CurPtr,

if (!IsUDSuffix) {
if (!isLexingRawMode())
Diag(CurPtr, getLangOpts().MSVCCompat
Diag(CurPtr, LangOpts.MSVCCompat
? diag::ext_ms_reserved_user_defined_literal
: diag::ext_reserved_user_defined_literal)
<< FixItHint::CreateInsertion(getSourceLocation(CurPtr), " ");
<< FixItHint::CreateInsertion(getSourceLocation(CurPtr), " ");
return CurPtr;
}

Expand Down Expand Up @@ -2022,9 +2021,8 @@ bool Lexer::LexStringLiteral(Token &Result, const char *CurPtr,
(Kind == tok::utf8_string_literal ||
Kind == tok::utf16_string_literal ||
Kind == tok::utf32_string_literal))
Diag(BufferPtr, getLangOpts().CPlusPlus
? diag::warn_cxx98_compat_unicode_literal
: diag::warn_c99_compat_unicode_literal);
Diag(BufferPtr, LangOpts.CPlusPlus ? diag::warn_cxx98_compat_unicode_literal
: diag::warn_c99_compat_unicode_literal);

char C = getAndAdvanceChar(CurPtr, Result);
while (C != '"') {
Expand Down Expand Up @@ -2058,7 +2056,7 @@ bool Lexer::LexStringLiteral(Token &Result, const char *CurPtr,
}

// If we are in C++11, lex the optional ud-suffix.
if (getLangOpts().CPlusPlus)
if (LangOpts.CPlusPlus)
CurPtr = LexUDSuffix(Result, CurPtr, true);

// If a nul character existed in the string, warn about it.
Expand Down Expand Up @@ -2142,7 +2140,7 @@ bool Lexer::LexRawStringLiteral(Token &Result, const char *CurPtr,
}

// If we are in C++11, lex the optional ud-suffix.
if (getLangOpts().CPlusPlus)
if (LangOpts.CPlusPlus)
CurPtr = LexUDSuffix(Result, CurPtr, true);

// Update the location of token as well as BufferPtr.
Expand Down Expand Up @@ -2238,7 +2236,7 @@ bool Lexer::LexCharConstant(Token &Result, const char *CurPtr,

if (!isLexingRawMode()) {
if (Kind == tok::utf16_char_constant || Kind == tok::utf32_char_constant)
Diag(BufferPtr, getLangOpts().CPlusPlus
Diag(BufferPtr, LangOpts.CPlusPlus
? diag::warn_cxx98_compat_unicode_literal
: diag::warn_c99_compat_unicode_literal);
else if (Kind == tok::utf8_char_constant)
Expand Down Expand Up @@ -2280,7 +2278,7 @@ bool Lexer::LexCharConstant(Token &Result, const char *CurPtr,
}

// If we are in C++11, lex the optional ud-suffix.
if (getLangOpts().CPlusPlus)
if (LangOpts.CPlusPlus)
CurPtr = LexUDSuffix(Result, CurPtr, false);

// If a nul character existed in the character, warn about it.
Expand Down Expand Up @@ -3841,7 +3839,7 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
} else if (Char == '=') {
char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
if (After == '>') {
if (getLangOpts().CPlusPlus20) {
if (LangOpts.CPlusPlus20) {
if (!isLexingRawMode())
Diag(BufferPtr, diag::warn_cxx17_compat_spaceship);
CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
Expand All @@ -3851,7 +3849,7 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
}
// Suggest adding a space between the '<=' and the '>' to avoid a
// change in semantics if this turns up in C++ <=17 mode.
if (getLangOpts().CPlusPlus && !isLexingRawMode()) {
if (LangOpts.CPlusPlus && !isLexingRawMode()) {
Diag(BufferPtr, diag::warn_cxx20_compat_spaceship)
<< FixItHint::CreateInsertion(
getSourceLocation(CurPtr + SizeTmp, SizeTmp2), " ");
Expand Down

0 comments on commit fbe38a7

Please sign in to comment.