Author: Owen Pan Date: 2024-05-12T23:15:35-07:00 New Revision: 1fadb2b0c881ced247931f442fdee6c4ed96dccb
URL: https://github.com/llvm/llvm-project/commit/1fadb2b0c881ced247931f442fdee6c4ed96dccb DIFF: https://github.com/llvm/llvm-project/commit/1fadb2b0c881ced247931f442fdee6c4ed96dccb.diff LOG: Revert "[clang-format] Fix FormatToken::isSimpleTypeSpecifier() (#91712)" This reverts commits e62ce1f8842c, 5cd280433e8e, and de641e289269 due to buildbot failures. Added: Modified: clang/lib/Format/Format.cpp clang/lib/Format/FormatToken.cpp clang/lib/Format/FormatToken.h clang/lib/Format/FormatTokenLexer.cpp clang/lib/Format/QualifierAlignmentFixer.cpp clang/lib/Format/QualifierAlignmentFixer.h clang/lib/Format/TokenAnalyzer.cpp clang/lib/Format/TokenAnalyzer.h clang/lib/Format/TokenAnnotator.cpp clang/lib/Format/TokenAnnotator.h clang/lib/Format/UnwrappedLineParser.cpp clang/lib/Format/UnwrappedLineParser.h clang/unittests/Format/QualifierFixerTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 52005a6c881f3..8f027ffa20cca 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -3858,7 +3858,8 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) { LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11; LangOpts.LineComment = 1; - LangOpts.CXXOperatorNames = Style.isCpp(); + bool AlternativeOperators = Style.isCpp(); + LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0; LangOpts.Bool = 1; LangOpts.ObjC = 1; LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally. diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index 85bec71ffbbc8..4fb70ffac706d 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -34,6 +34,43 @@ const char *getTokenTypeName(TokenType Type) { return nullptr; } +// FIXME: This is copy&pasted from Sema. Put it in a common place and remove +// duplication. +bool FormatToken::isSimpleTypeSpecifier() const { + switch (Tok.getKind()) { + case tok::kw_short: + case tok::kw_long: + case tok::kw___int64: + case tok::kw___int128: + case tok::kw_signed: + case tok::kw_unsigned: + case tok::kw_void: + case tok::kw_char: + case tok::kw_int: + case tok::kw_half: + case tok::kw_float: + case tok::kw_double: + case tok::kw___bf16: + case tok::kw__Float16: + case tok::kw___float128: + case tok::kw___ibm128: + case tok::kw_wchar_t: + case tok::kw_bool: +#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait: +#include "clang/Basic/TransformTypeTraits.def" + case tok::annot_typename: + case tok::kw_char8_t: + case tok::kw_char16_t: + case tok::kw_char32_t: + case tok::kw_typeof: + case tok::kw_decltype: + case tok::kw__Atomic: + return true; + default: + return false; + } +} + // Sorted common C++ non-keyword types. static SmallVector<StringRef> CppNonKeywordTypes = { "clock_t", "int16_t", "int32_t", "int64_t", "int8_t", @@ -41,16 +78,15 @@ static SmallVector<StringRef> CppNonKeywordTypes = { "uint32_t", "uint64_t", "uint8_t", "uintptr_t", }; -bool FormatToken::isTypeName(const LangOptions &LangOpts) const { - const bool IsCpp = LangOpts.CXXOperatorNames; - return is(TT_TypeName) || Tok.isSimpleTypeSpecifier(LangOpts) || +bool FormatToken::isTypeName(bool IsCpp) const { + return is(TT_TypeName) || isSimpleTypeSpecifier() || (IsCpp && is(tok::identifier) && std::binary_search(CppNonKeywordTypes.begin(), CppNonKeywordTypes.end(), TokenText)); } -bool FormatToken::isTypeOrIdentifier(const LangOptions &LangOpts) const { - return isTypeName(LangOpts) || isOneOf(tok::kw_auto, tok::identifier); +bool FormatToken::isTypeOrIdentifier(bool IsCpp) const { + return isTypeName(IsCpp) || isOneOf(tok::kw_auto, tok::identifier); } bool FormatToken::isBlockIndentedInitRBrace(const FormatStyle &Style) const { diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index 8792f4c750748..95f16fde5005f 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -684,8 +684,12 @@ struct FormatToken { isAttribute(); } - [[nodiscard]] bool isTypeName(const LangOptions &LangOpts) const; - [[nodiscard]] bool isTypeOrIdentifier(const LangOptions &LangOpts) const; + /// Determine whether the token is a simple-type-specifier. + [[nodiscard]] bool isSimpleTypeSpecifier() const; + + [[nodiscard]] bool isTypeName(bool IsCpp) const; + + [[nodiscard]] bool isTypeOrIdentifier(bool IsCpp) const; bool isObjCAccessSpecifier() const { return is(tok::at) && Next && diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index e21b5a882b777..f430d3764babe 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -1442,6 +1442,7 @@ void FormatTokenLexer::readRawToken(FormatToken &Tok) { void FormatTokenLexer::resetLexer(unsigned Offset) { StringRef Buffer = SourceMgr.getBufferData(ID); + LangOpts = getFormattingLangOpts(Style); Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), LangOpts, Buffer.begin(), Buffer.begin() + Offset, Buffer.end())); Lex->SetKeepWhitespaceMode(true); diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp index a904f0b773c6d..c263530456727 100644 --- a/clang/lib/Format/QualifierAlignmentFixer.cpp +++ b/clang/lib/Format/QualifierAlignmentFixer.cpp @@ -268,11 +268,13 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight( if (isPossibleMacro(TypeToken)) return Tok; + const bool IsCpp = Style.isCpp(); + // The case `const long long int volatile` -> `long long int const volatile` // The case `long const long int volatile` -> `long long int const volatile` // The case `long long volatile int const` -> `long long int const volatile` // The case `const long long volatile int` -> `long long int const volatile` - if (TypeToken->isTypeName(LangOpts)) { + if (TypeToken->isTypeName(IsCpp)) { // The case `const decltype(foo)` -> `const decltype(foo)` // The case `const typeof(foo)` -> `const typeof(foo)` // The case `const _Atomic(foo)` -> `const _Atomic(foo)` @@ -281,7 +283,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight( const FormatToken *LastSimpleTypeSpecifier = TypeToken; while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment(), - LangOpts)) { + IsCpp)) { LastSimpleTypeSpecifier = LastSimpleTypeSpecifier->getNextNonComment(); } @@ -293,7 +295,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight( // The case `unsigned short const` -> `unsigned short const` // The case: // `unsigned short volatile const` -> `unsigned short const volatile` - if (PreviousCheck && PreviousCheck->isTypeName(LangOpts)) { + if (PreviousCheck && PreviousCheck->isTypeName(IsCpp)) { if (LastQual != Tok) rotateTokens(SourceMgr, Fixes, Tok, LastQual, /*Left=*/false); return Tok; @@ -410,11 +412,11 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft( // The case `volatile long long const int` -> `const volatile long long int` // The case `const long long volatile int` -> `const volatile long long int` // The case `long volatile long int const` -> `const volatile long long int` - if (TypeToken->isTypeName(LangOpts)) { + if (const bool IsCpp = Style.isCpp(); TypeToken->isTypeName(IsCpp)) { const FormatToken *LastSimpleTypeSpecifier = TypeToken; while (isConfiguredQualifierOrType( LastSimpleTypeSpecifier->getPreviousNonComment(), - ConfiguredQualifierTokens, LangOpts)) { + ConfiguredQualifierTokens, IsCpp)) { LastSimpleTypeSpecifier = LastSimpleTypeSpecifier->getPreviousNonComment(); } @@ -612,16 +614,16 @@ void prepareLeftRightOrderingForQualifierAlignmentFixer( } } -bool LeftRightQualifierAlignmentFixer::isQualifierOrType( - const FormatToken *Tok, const LangOptions &LangOpts) { - return Tok && (Tok->isTypeName(LangOpts) || Tok->is(tok::kw_auto) || - isQualifier(Tok)); +bool LeftRightQualifierAlignmentFixer::isQualifierOrType(const FormatToken *Tok, + bool IsCpp) { + return Tok && + (Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) || isQualifier(Tok)); } bool LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( const FormatToken *Tok, const std::vector<tok::TokenKind> &Qualifiers, - const LangOptions &LangOpts) { - return Tok && (Tok->isTypeName(LangOpts) || Tok->is(tok::kw_auto) || + bool IsCpp) { + return Tok && (Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) || isConfiguredQualifier(Tok, Qualifiers)); } diff --git a/clang/lib/Format/QualifierAlignmentFixer.h b/clang/lib/Format/QualifierAlignmentFixer.h index 710fa2dc00309..e1cc27e62b13a 100644 --- a/clang/lib/Format/QualifierAlignmentFixer.h +++ b/clang/lib/Format/QualifierAlignmentFixer.h @@ -71,12 +71,11 @@ class LeftRightQualifierAlignmentFixer : public TokenAnalyzer { tok::TokenKind QualifierType); // Is the Token a simple or qualifier type - static bool isQualifierOrType(const FormatToken *Tok, - const LangOptions &LangOpts); + static bool isQualifierOrType(const FormatToken *Tok, bool IsCpp = true); static bool isConfiguredQualifierOrType(const FormatToken *Tok, const std::vector<tok::TokenKind> &Qualifiers, - const LangOptions &LangOpts); + bool IsCpp = true); // Is the Token likely a Macro static bool isPossibleMacro(const FormatToken *Tok); diff --git a/clang/lib/Format/TokenAnalyzer.cpp b/clang/lib/Format/TokenAnalyzer.cpp index 804a2b0f5e8c1..bd648c430f9b0 100644 --- a/clang/lib/Format/TokenAnalyzer.cpp +++ b/clang/lib/Format/TokenAnalyzer.cpp @@ -84,7 +84,7 @@ Environment::Environment(StringRef Code, StringRef FileName, NextStartColumn(NextStartColumn), LastStartColumn(LastStartColumn) {} TokenAnalyzer::TokenAnalyzer(const Environment &Env, const FormatStyle &Style) - : Style(Style), LangOpts(getFormattingLangOpts(Style)), Env(Env), + : Style(Style), Env(Env), AffectedRangeMgr(Env.getSourceManager(), Env.getCharRanges()), UnwrappedLines(1), Encoding(encoding::detectEncoding( @@ -101,7 +101,7 @@ std::pair<tooling::Replacements, unsigned> TokenAnalyzer::process(bool SkipAnnotation) { tooling::Replacements Result; llvm::SpecificBumpPtrAllocator<FormatToken> Allocator; - IdentifierTable IdentTable(LangOpts); + IdentifierTable IdentTable(getFormattingLangOpts(Style)); FormatTokenLexer Lex(Env.getSourceManager(), Env.getFileID(), Env.getFirstStartColumn(), Style, Encoding, Allocator, IdentTable); diff --git a/clang/lib/Format/TokenAnalyzer.h b/clang/lib/Format/TokenAnalyzer.h index ef559099d325d..b7494c395c8a2 100644 --- a/clang/lib/Format/TokenAnalyzer.h +++ b/clang/lib/Format/TokenAnalyzer.h @@ -92,7 +92,6 @@ class TokenAnalyzer : public UnwrappedLineConsumer { void finishRun() override; FormatStyle Style; - LangOptions LangOpts; // Stores Style, FileID and SourceManager etc. const Environment &Env; // AffectedRangeMgr stores ranges to be fixed. diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 478cae23d3c8c..e935d3e2709cc 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -126,9 +126,7 @@ class AnnotatingParser { const AdditionalKeywords &Keywords, SmallVector<ScopeType> &Scopes) : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false), - IsCpp(Style.isCpp()), LangOpts(getFormattingLangOpts(Style)), - Keywords(Keywords), Scopes(Scopes) { - assert(IsCpp == LangOpts.CXXOperatorNames); + IsCpp(Style.isCpp()), Keywords(Keywords), Scopes(Scopes) { Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false)); resetTokenMetadata(); } @@ -564,7 +562,7 @@ class AnnotatingParser { (CurrentToken->is(tok::l_paren) && CurrentToken->Next && CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret)); if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) || - CurrentToken->Previous->isTypeName(LangOpts)) && + CurrentToken->Previous->isTypeName(IsCpp)) && !(CurrentToken->is(tok::l_brace) || (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) { Contexts.back().IsExpression = false; @@ -2626,7 +2624,7 @@ class AnnotatingParser { return true; // MyClass a; - if (PreviousNotConst->isTypeName(LangOpts)) + if (PreviousNotConst->isTypeName(IsCpp)) return true; // type[] a in Java @@ -2730,7 +2728,7 @@ class AnnotatingParser { } if (Tok.Next->is(tok::question) || - (Tok.Next->is(tok::ampamp) && !Tok.Previous->isTypeName(LangOpts))) { + (Tok.Next->is(tok::ampamp) && !Tok.Previous->isTypeName(IsCpp))) { return false; } @@ -2759,10 +2757,9 @@ class AnnotatingParser { } // Heuristically try to determine whether the parentheses contain a type. - auto IsQualifiedPointerOrReference = [](FormatToken *T, - const LangOptions &LangOpts) { + auto IsQualifiedPointerOrReference = [](FormatToken *T, bool IsCpp) { // This is used to handle cases such as x = (foo *const)&y; - assert(!T->isTypeName(LangOpts) && "Should have already been checked"); + assert(!T->isTypeName(IsCpp) && "Should have already been checked"); // Strip trailing qualifiers such as const or volatile when checking // whether the parens could be a cast to a pointer/reference type. while (T) { @@ -2794,8 +2791,8 @@ class AnnotatingParser { bool ParensAreType = !Tok.Previous || Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) || - Tok.Previous->isTypeName(LangOpts) || - IsQualifiedPointerOrReference(Tok.Previous, LangOpts); + Tok.Previous->isTypeName(IsCpp) || + IsQualifiedPointerOrReference(Tok.Previous, IsCpp); bool ParensCouldEndDecl = Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater); if (ParensAreType && !ParensCouldEndDecl) @@ -3068,7 +3065,6 @@ class AnnotatingParser { FormatToken *CurrentToken; bool AutoFound; bool IsCpp; - LangOptions LangOpts; const AdditionalKeywords &Keywords; SmallVector<ScopeType> &Scopes; @@ -3643,8 +3639,7 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) { // This function heuristically determines whether 'Current' starts the name of a // function declaration. -static bool isFunctionDeclarationName(const LangOptions &LangOpts, - const FormatToken &Current, +static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current, const AnnotatedLine &Line, FormatToken *&ClosingParen) { assert(Current.Previous); @@ -3663,7 +3658,7 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts, } auto skipOperatorName = - [&LangOpts](const FormatToken *Next) -> const FormatToken * { + [IsCpp](const FormatToken *Next) -> const FormatToken * { for (; Next; Next = Next->Next) { if (Next->is(TT_OverloadedOperatorLParen)) return Next; @@ -3682,7 +3677,7 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts, Next = Next->Next; continue; } - if ((Next->isTypeName(LangOpts) || Next->is(tok::identifier)) && + if ((Next->isTypeName(IsCpp) || Next->is(tok::identifier)) && Next->Next && Next->Next->isPointerOrReference()) { // For operator void*(), operator char*(), operator Foo*(). Next = Next->Next; @@ -3698,10 +3693,8 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts, return nullptr; }; - const auto *Next = Current.Next; - const bool IsCpp = LangOpts.CXXOperatorNames; - // Find parentheses of parameter list. + const FormatToken *Next = Current.Next; if (Current.is(tok::kw_operator)) { if (Previous.Tok.getIdentifierInfo() && !Previous.isOneOf(tok::kw_return, tok::kw_co_return)) { @@ -3781,7 +3774,7 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts, Tok = Tok->MatchingParen; continue; } - if (Tok->is(tok::kw_const) || Tok->isTypeName(LangOpts) || + if (Tok->is(tok::kw_const) || Tok->isTypeName(IsCpp) || Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) { return true; } @@ -3844,7 +3837,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const { AfterLastAttribute = Tok; if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName); IsCtorOrDtor || - isFunctionDeclarationName(LangOpts, *Tok, Line, ClosingParen)) { + isFunctionDeclarationName(IsCpp, *Tok, Line, ClosingParen)) { if (!IsCtorOrDtor) Tok->setFinalizedType(TT_FunctionDeclarationName); LineIsFunctionDeclaration = true; @@ -4454,7 +4447,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, if (Left.Tok.isLiteral()) return true; // for (auto a = 0, b = 0; const auto & c : {1, 2, 3}) - if (Left.isTypeOrIdentifier(LangOpts) && Right.Next && Right.Next->Next && + if (Left.isTypeOrIdentifier(IsCpp) && Right.Next && Right.Next->Next && Right.Next->Next->is(TT_RangeBasedForLoopColon)) { return getTokenPointerOrReferenceAlignment(Right) != FormatStyle::PAS_Left; @@ -4497,7 +4490,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, if (Right.is(tok::l_brace) && Right.is(BK_Block)) return true; // for (auto a = 0, b = 0; const auto& c : {1, 2, 3}) - if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) && Right.Next && + if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(IsCpp) && Right.Next && Right.Next->is(TT_RangeBasedForLoopColon)) { return getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right; @@ -4541,7 +4534,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, if (Right.isPointerOrReference()) { const FormatToken *Previous = &Left; while (Previous && Previous->isNot(tok::kw_operator)) { - if (Previous->is(tok::identifier) || Previous->isTypeName(LangOpts)) { + if (Previous->is(tok::identifier) || Previous->isTypeName(IsCpp)) { Previous = Previous->getPreviousNonComment(); continue; } @@ -4730,7 +4723,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, if (!Style.isVerilog() && (Left.isOneOf(tok::identifier, tok::greater, tok::r_square, tok::r_paren) || - Left.isTypeName(LangOpts)) && + Left.isTypeName(IsCpp)) && Right.is(tok::l_brace) && Right.getNextNonComment() && Right.isNot(BK_Block)) { return false; diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h index d19d3d061e40c..25a24dccb1b83 100644 --- a/clang/lib/Format/TokenAnnotator.h +++ b/clang/lib/Format/TokenAnnotator.h @@ -211,10 +211,7 @@ class AnnotatedLine { class TokenAnnotator { public: TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords) - : Style(Style), IsCpp(Style.isCpp()), - LangOpts(getFormattingLangOpts(Style)), Keywords(Keywords) { - assert(IsCpp == LangOpts.CXXOperatorNames); - } + : Style(Style), IsCpp(Style.isCpp()), Keywords(Keywords) {} /// Adapts the indent levels of comment lines to the indent of the /// subsequent line. @@ -263,7 +260,6 @@ class TokenAnnotator { const FormatStyle &Style; bool IsCpp; - LangOptions LangOpts; const AdditionalKeywords &Keywords; diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 4f1c2c5114e93..310b75485e089 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -160,16 +160,13 @@ UnwrappedLineParser::UnwrappedLineParser( IdentifierTable &IdentTable) : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), CurrentLines(&Lines), Style(Style), IsCpp(Style.isCpp()), - LangOpts(getFormattingLangOpts(Style)), Keywords(Keywords), - CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr), - Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1), + Keywords(Keywords), CommentPragmasRegex(Style.CommentPragmas), + Tokens(nullptr), Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1), IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None ? IG_Rejected : IG_Inited), IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn), - Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) { - assert(IsCpp == LangOpts.CXXOperatorNames); -} + Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) {} void UnwrappedLineParser::reset() { PPBranchLevel = -1; @@ -1873,7 +1870,7 @@ void UnwrappedLineParser::parseStructuralElement( case tok::caret: nextToken(); // Block return type. - if (FormatTok->Tok.isAnyIdentifier() || FormatTok->isTypeName(LangOpts)) { + if (FormatTok->Tok.isAnyIdentifier() || FormatTok->isTypeName(IsCpp)) { nextToken(); // Return types: pointers are ok too. while (FormatTok->is(tok::star)) @@ -2234,7 +2231,7 @@ bool UnwrappedLineParser::tryToParseLambda() { bool InTemplateParameterList = false; while (FormatTok->isNot(tok::l_brace)) { - if (FormatTok->isTypeName(LangOpts)) { + if (FormatTok->isTypeName(IsCpp)) { nextToken(); continue; } @@ -3451,7 +3448,7 @@ bool UnwrappedLineParser::parseRequires() { break; } default: - if (PreviousNonComment->isTypeOrIdentifier(LangOpts)) { + if (PreviousNonComment->isTypeOrIdentifier(IsCpp)) { // This is a requires clause. parseRequiresClause(RequiresToken); return true; @@ -3514,7 +3511,7 @@ bool UnwrappedLineParser::parseRequires() { --OpenAngles; break; default: - if (NextToken->isTypeName(LangOpts)) { + if (NextToken->isTypeName(IsCpp)) { FormatTok = Tokens->setPosition(StoredPosition); parseRequiresExpression(RequiresToken); return false; @@ -4030,7 +4027,7 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) { if (FormatTok->is(tok::l_square)) { FormatToken *Previous = FormatTok->Previous; if (!Previous || (Previous->isNot(tok::r_paren) && - !Previous->isTypeOrIdentifier(LangOpts))) { + !Previous->isTypeOrIdentifier(IsCpp))) { // Don't try parsing a lambda if we had a closing parenthesis before, // it was probably a pointer to an array: int (*)[]. if (!tryToParseLambda()) diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h index d7963a4211bb9..2a0fe19d09572 100644 --- a/clang/lib/Format/UnwrappedLineParser.h +++ b/clang/lib/Format/UnwrappedLineParser.h @@ -316,7 +316,6 @@ class UnwrappedLineParser { const FormatStyle &Style; bool IsCpp; - LangOptions LangOpts; const AdditionalKeywords &Keywords; llvm::Regex CommentPragmasRegex; diff --git a/clang/unittests/Format/QualifierFixerTest.cpp b/clang/unittests/Format/QualifierFixerTest.cpp index fdc392e5d9481..792d8f3c3a982 100644 --- a/clang/unittests/Format/QualifierFixerTest.cpp +++ b/clang/unittests/Format/QualifierFixerTest.cpp @@ -1055,82 +1055,70 @@ TEST_F(QualifierFixerTest, IsQualifierType) { ConfiguredTokens.push_back(tok::kw_constexpr); ConfiguredTokens.push_back(tok::kw_friend); - static const LangOptions LangOpts{getFormattingLangOpts()}; - auto Tokens = annotate( "const static inline auto restrict int double long constexpr friend"); ASSERT_EQ(Tokens.size(), 11u) << Tokens; EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - Tokens[0], ConfiguredTokens, LangOpts)); + Tokens[0], ConfiguredTokens)); EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - Tokens[1], ConfiguredTokens, LangOpts)); + Tokens[1], ConfiguredTokens)); EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - Tokens[2], ConfiguredTokens, LangOpts)); + Tokens[2], ConfiguredTokens)); EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - Tokens[3], ConfiguredTokens, LangOpts)); + Tokens[3], ConfiguredTokens)); EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - Tokens[4], ConfiguredTokens, LangOpts)); + Tokens[4], ConfiguredTokens)); EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - Tokens[5], ConfiguredTokens, LangOpts)); + Tokens[5], ConfiguredTokens)); EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - Tokens[6], ConfiguredTokens, LangOpts)); + Tokens[6], ConfiguredTokens)); EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - Tokens[7], ConfiguredTokens, LangOpts)); + Tokens[7], ConfiguredTokens)); EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - Tokens[8], ConfiguredTokens, LangOpts)); + Tokens[8], ConfiguredTokens)); EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - Tokens[9], ConfiguredTokens, LangOpts)); - - EXPECT_TRUE( - LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[0], LangOpts)); - EXPECT_TRUE( - LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[1], LangOpts)); - EXPECT_TRUE( - LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[2], LangOpts)); - EXPECT_TRUE( - LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[3], LangOpts)); - EXPECT_TRUE( - LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[4], LangOpts)); - EXPECT_TRUE( - LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[5], LangOpts)); - EXPECT_TRUE( - LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[6], LangOpts)); - EXPECT_TRUE( - LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[7], LangOpts)); - EXPECT_TRUE( - LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[8], LangOpts)); - EXPECT_TRUE( - LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[9], LangOpts)); + Tokens[9], ConfiguredTokens)); + + EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[0])); + EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[1])); + EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[2])); + EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[3])); + EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[4])); + EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[5])); + EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[6])); + EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[7])); + EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[8])); + EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(Tokens[9])); auto NotTokens = annotate("for while do Foo Bar "); ASSERT_EQ(NotTokens.size(), 6u) << Tokens; EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - NotTokens[0], ConfiguredTokens, LangOpts)); + NotTokens[0], ConfiguredTokens)); EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - NotTokens[1], ConfiguredTokens, LangOpts)); + NotTokens[1], ConfiguredTokens)); EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - NotTokens[2], ConfiguredTokens, LangOpts)); + NotTokens[2], ConfiguredTokens)); EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - NotTokens[3], ConfiguredTokens, LangOpts)); + NotTokens[3], ConfiguredTokens)); EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - NotTokens[4], ConfiguredTokens, LangOpts)); + NotTokens[4], ConfiguredTokens)); EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - NotTokens[5], ConfiguredTokens, LangOpts)); - - EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[0], - LangOpts)); - EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[1], - LangOpts)); - EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[2], - LangOpts)); - EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[3], - LangOpts)); - EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[4], - LangOpts)); - EXPECT_FALSE(LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[5], - LangOpts)); + NotTokens[5], ConfiguredTokens)); + + EXPECT_FALSE( + LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[0])); + EXPECT_FALSE( + LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[1])); + EXPECT_FALSE( + LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[2])); + EXPECT_FALSE( + LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[3])); + EXPECT_FALSE( + LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[4])); + EXPECT_FALSE( + LeftRightQualifierAlignmentFixer::isQualifierOrType(NotTokens[5])); } TEST_F(QualifierFixerTest, IsMacro) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits