Author: Owen Pan Date: 2024-03-14T18:56:24-07:00 New Revision: 0c07102927869f9cd23889e0666774f6298e824f
URL: https://github.com/llvm/llvm-project/commit/0c07102927869f9cd23889e0666774f6298e824f DIFF: https://github.com/llvm/llvm-project/commit/0c07102927869f9cd23889e0666774f6298e824f.diff LOG: [clang-format][NFC] Eliminate the IsCpp parameter in all functions (#84599) Added: Modified: clang/include/clang/Format/Format.h clang/lib/Format/ContinuationIndenter.cpp 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/TokenAnnotator.cpp clang/lib/Format/TokenAnnotator.h clang/lib/Format/UnwrappedLineParser.cpp clang/lib/Format/UnwrappedLineParser.h clang/unittests/Format/TokenAnnotatorTest.cpp Removed: ################################################################################ diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 590297fd89a398..a72c1b171c3e18 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -5228,6 +5228,9 @@ extern const char *DefaultFormatStyle; /// Different builds can modify the value to the preferred styles. extern const char *DefaultFallbackStyle; +/// Whether the language is C/C++/Objective-C/Objective-C++. +extern bool IsCpp; + /// Construct a FormatStyle based on ``StyleName``. /// /// ``StyleName`` can take several forms: diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index df44e6994c4784..964f1ead806bfb 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -241,7 +241,9 @@ ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style, : Style(Style), Keywords(Keywords), SourceMgr(SourceMgr), Whitespaces(Whitespaces), Encoding(Encoding), BinPackInconclusiveFunctions(BinPackInconclusiveFunctions), - CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {} + CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) { + assert(IsCpp == Style.isCpp()); +} LineState ContinuationIndenter::getInitialState(unsigned FirstIndent, unsigned FirstStartColumn, @@ -406,7 +408,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { } if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) || (Previous.is(TT_TemplateCloser) && Current.is(TT_StartOfName) && - State.Line->First->isNot(TT_AttributeSquare) && Style.isCpp() && + State.Line->First->isNot(TT_AttributeSquare) && IsCpp && // FIXME: This is a temporary workaround for the case where clang-format // sets BreakBeforeParameter to avoid bin packing and this creates a // completely unnecessary line break after a template type that isn't @@ -677,8 +679,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, auto &CurrentState = State.Stack.back(); bool DisallowLineBreaksOnThisLine = - Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && - Style.isCpp() && [&Current] { + Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && IsCpp && + [&Current] { // Deal with lambda arguments in C++. The aim here is to ensure that we // don't over-indent lambda function bodies when lambdas are passed as // arguments to function calls. We do this by ensuring that either all @@ -1091,7 +1093,7 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, // Any break on this level means that the parent level has been broken // and we need to avoid bin packing there. bool NestedBlockSpecialCase = - (!Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 && + (!IsCpp && Current.is(tok::r_brace) && State.Stack.size() > 1 && State.Stack[State.Stack.size() - 2].NestedBlockInlined) || (Style.Language == FormatStyle::LK_ObjC && Current.is(tok::r_brace) && State.Stack.size() > 1 && !Style.ObjCBreakBeforeNestedBlockParam); diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index e64ba7eebc1ce8..faf65c619b237b 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -3841,13 +3841,15 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle &Style, } LangOptions getFormattingLangOpts(const FormatStyle &Style) { - LangOptions LangOpts; + IsCpp = Style.isCpp(); FormatStyle::LanguageStandard LexingStd = Style.Standard; if (LexingStd == FormatStyle::LS_Auto) LexingStd = FormatStyle::LS_Latest; if (LexingStd == FormatStyle::LS_Latest) LexingStd = FormatStyle::LS_Cpp20; + + LangOptions LangOpts; LangOpts.CPlusPlus = 1; LangOpts.CPlusPlus11 = LexingStd >= FormatStyle::LS_Cpp11; LangOpts.CPlusPlus14 = LexingStd >= FormatStyle::LS_Cpp14; @@ -3858,10 +3860,8 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) { // the sequence "<::" will be unconditionally treated as "[:". // Cf. Lexer::LexTokenInternal. LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11; - LangOpts.LineComment = 1; - bool AlternativeOperators = Style.isCpp(); - LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0; + LangOpts.CXXOperatorNames = IsCpp; LangOpts.Bool = 1; LangOpts.ObjC = 1; LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally. @@ -3943,6 +3943,8 @@ const char *DefaultFormatStyle = "file"; const char *DefaultFallbackStyle = "LLVM"; +bool IsCpp = false; + llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS, FormatStyle *Style, bool AllowUnknownOptions) { diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index 4fb70ffac706d0..665b2e43259b21 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -78,15 +78,15 @@ static SmallVector<StringRef> CppNonKeywordTypes = { "uint32_t", "uint64_t", "uint8_t", "uintptr_t", }; -bool FormatToken::isTypeName(bool IsCpp) const { +bool FormatToken::isTypeName() const { return is(TT_TypeName) || isSimpleTypeSpecifier() || (IsCpp && is(tok::identifier) && std::binary_search(CppNonKeywordTypes.begin(), CppNonKeywordTypes.end(), TokenText)); } -bool FormatToken::isTypeOrIdentifier(bool IsCpp) const { - return isTypeName(IsCpp) || isOneOf(tok::kw_auto, tok::identifier); +bool FormatToken::isTypeOrIdentifier() const { + return isTypeName() || 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 f4566e4a335138..ee96d072b12059 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -676,9 +676,9 @@ struct FormatToken { /// Determine whether the token is a simple-type-specifier. [[nodiscard]] bool isSimpleTypeSpecifier() const; - [[nodiscard]] bool isTypeName(bool IsCpp) const; + [[nodiscard]] bool isTypeName() const; - [[nodiscard]] bool isTypeOrIdentifier(bool IsCpp) const; + [[nodiscard]] bool isTypeOrIdentifier() const; bool isObjCAccessSpecifier() const { return is(tok::at) && Next && @@ -823,7 +823,7 @@ struct FormatToken { /// Returns whether the token is the left square bracket of a C++ /// structured binding declaration. - bool isCppStructuredBinding(bool IsCpp) const { + bool isCppStructuredBinding() const { if (!IsCpp || isNot(tok::l_square)) return false; const FormatToken *T = this; diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index 036f7e6a4efc1e..717eb6bb49b1f4 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -34,6 +34,7 @@ FormatTokenLexer::FormatTokenLexer( Encoding(Encoding), Allocator(Allocator), FirstInLineIndex(0), FormattingDisabled(false), MacroBlockBeginRegex(Style.MacroBlockBegin), MacroBlockEndRegex(Style.MacroBlockEnd) { + assert(IsCpp == Style.isCpp()); Lex.reset(new Lexer(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts)); Lex->SetKeepWhitespaceMode(true); @@ -114,7 +115,7 @@ void FormatTokenLexer::tryMergePreviousTokens() { return; if (tryMergeForEach()) return; - if (Style.isCpp() && tryTransformTryUsageForC()) + if (IsCpp && tryTransformTryUsageForC()) return; if (Style.isJavaScript() || Style.isCSharp()) { @@ -1341,7 +1342,7 @@ FormatToken *FormatTokenLexer::getNextToken() { Column = FormatTok->LastLineColumnWidth; } - if (Style.isCpp()) { + if (IsCpp) { auto *Identifier = FormatTok->Tok.getIdentifierInfo(); auto it = Macros.find(Identifier); if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() && diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp index c263530456727c..8d47137351b58a 100644 --- a/clang/lib/Format/QualifierAlignmentFixer.cpp +++ b/clang/lib/Format/QualifierAlignmentFixer.cpp @@ -268,13 +268,11 @@ 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(IsCpp)) { + if (TypeToken->isTypeName()) { // The case `const decltype(foo)` -> `const decltype(foo)` // The case `const typeof(foo)` -> `const typeof(foo)` // The case `const _Atomic(foo)` -> `const _Atomic(foo)` @@ -282,10 +280,8 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight( return Tok; const FormatToken *LastSimpleTypeSpecifier = TypeToken; - while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment(), - IsCpp)) { + while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment())) LastSimpleTypeSpecifier = LastSimpleTypeSpecifier->getNextNonComment(); - } rotateTokens(SourceMgr, Fixes, Tok, LastSimpleTypeSpecifier, /*Left=*/false); @@ -295,7 +291,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(IsCpp)) { + if (PreviousCheck && PreviousCheck->isTypeName()) { if (LastQual != Tok) rotateTokens(SourceMgr, Fixes, Tok, LastQual, /*Left=*/false); return Tok; @@ -412,11 +408,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 (const bool IsCpp = Style.isCpp(); TypeToken->isTypeName(IsCpp)) { + if (TypeToken->isTypeName()) { const FormatToken *LastSimpleTypeSpecifier = TypeToken; while (isConfiguredQualifierOrType( LastSimpleTypeSpecifier->getPreviousNonComment(), - ConfiguredQualifierTokens, IsCpp)) { + ConfiguredQualifierTokens)) { LastSimpleTypeSpecifier = LastSimpleTypeSpecifier->getPreviousNonComment(); } @@ -531,7 +527,9 @@ LeftRightQualifierAlignmentFixer::LeftRightQualifierAlignmentFixer( const std::string &Qualifier, const std::vector<tok::TokenKind> &QualifierTokens, bool RightAlign) : TokenAnalyzer(Env, Style), Qualifier(Qualifier), RightAlign(RightAlign), - ConfiguredQualifierTokens(QualifierTokens) {} + ConfiguredQualifierTokens(QualifierTokens) { + assert(IsCpp == Style.isCpp()); +} std::pair<tooling::Replacements, unsigned> LeftRightQualifierAlignmentFixer::analyze( @@ -614,16 +612,15 @@ void prepareLeftRightOrderingForQualifierAlignmentFixer( } } -bool LeftRightQualifierAlignmentFixer::isQualifierOrType(const FormatToken *Tok, - bool IsCpp) { +bool LeftRightQualifierAlignmentFixer::isQualifierOrType( + const FormatToken *Tok) { return Tok && - (Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) || isQualifier(Tok)); + (Tok->isTypeName() || Tok->is(tok::kw_auto) || isQualifier(Tok)); } bool LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType( - const FormatToken *Tok, const std::vector<tok::TokenKind> &Qualifiers, - bool IsCpp) { - return Tok && (Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) || + const FormatToken *Tok, const std::vector<tok::TokenKind> &Qualifiers) { + return Tok && (Tok->isTypeName() || Tok->is(tok::kw_auto) || isConfiguredQualifier(Tok, Qualifiers)); } diff --git a/clang/lib/Format/QualifierAlignmentFixer.h b/clang/lib/Format/QualifierAlignmentFixer.h index e1cc27e62b13a0..e922d800559510 100644 --- a/clang/lib/Format/QualifierAlignmentFixer.h +++ b/clang/lib/Format/QualifierAlignmentFixer.h @@ -71,11 +71,10 @@ class LeftRightQualifierAlignmentFixer : public TokenAnalyzer { tok::TokenKind QualifierType); // Is the Token a simple or qualifier type - static bool isQualifierOrType(const FormatToken *Tok, bool IsCpp = true); + static bool isQualifierOrType(const FormatToken *Tok); static bool isConfiguredQualifierOrType(const FormatToken *Tok, - const std::vector<tok::TokenKind> &Qualifiers, - bool IsCpp = true); + const std::vector<tok::TokenKind> &Qualifiers); // Is the Token likely a Macro static bool isPossibleMacro(const FormatToken *Tok); diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index d7b84e309e0964..e464c2b5731a35 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -84,7 +84,7 @@ static bool isKeywordWithCondition(const FormatToken &Tok) { } /// Returns \c true if the token starts a C++ attribute, \c false otherwise. -static bool isCppAttribute(bool IsCpp, const FormatToken &Tok) { +static bool isCppAttribute(const FormatToken &Tok) { if (!IsCpp || !Tok.startsSequence(tok::l_square, tok::l_square)) return false; // The first square bracket is part of an ObjC array literal @@ -126,7 +126,8 @@ class AnnotatingParser { const AdditionalKeywords &Keywords, SmallVector<ScopeType> &Scopes) : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false), - IsCpp(Style.isCpp()), Keywords(Keywords), Scopes(Scopes) { + Keywords(Keywords), Scopes(Scopes) { + assert(IsCpp == Style.isCpp()); Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false)); resetTokenMetadata(); } @@ -562,7 +563,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(IsCpp)) && + CurrentToken->Previous->isTypeName()) && !(CurrentToken->is(tok::l_brace) || (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) { Contexts.back().IsExpression = false; @@ -682,7 +683,7 @@ class AnnotatingParser { const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier; const bool IsCpp11AttributeSpecifier = - isCppAttribute(IsCpp, *Left) || IsInnerSquare; + isCppAttribute(*Left) || IsInnerSquare; // Treat C# Attributes [STAThread] much like C++ attributes [[...]]. bool IsCSharpAttributeSpecifier = @@ -690,7 +691,7 @@ class AnnotatingParser { Contexts.back().InCSharpAttributeSpecifier; bool InsideInlineASM = Line.startsWith(tok::kw_asm); - bool IsCppStructuredBinding = Left->isCppStructuredBinding(IsCpp); + bool IsCppStructuredBinding = Left->isCppStructuredBinding(); bool StartsObjCMethodExpr = !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates && IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier && @@ -2573,7 +2574,7 @@ class AnnotatingParser { return true; // MyClass a; - if (PreviousNotConst->isTypeName(IsCpp)) + if (PreviousNotConst->isTypeName()) return true; // type[] a in Java @@ -2688,7 +2689,7 @@ class AnnotatingParser { if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const, tok::kw_requires, tok::kw_throw, tok::arrow, Keywords.kw_override, Keywords.kw_final) || - isCppAttribute(IsCpp, *Tok.Next)) { + isCppAttribute(*Tok.Next)) { return false; } @@ -2704,10 +2705,9 @@ class AnnotatingParser { } // Heuristically try to determine whether the parentheses contain a type. - auto IsQualifiedPointerOrReference = [this](FormatToken *T) { + auto IsQualifiedPointerOrReference = [](FormatToken *T) { // This is used to handle cases such as x = (foo *const)&y; - assert(!T->isTypeName(IsCpp) && "Should have already been checked"); - (void)IsCpp; // Avoid -Wunused-lambda-capture when assertion is disabled. + assert(!T->isTypeName() && "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) { @@ -2739,7 +2739,7 @@ class AnnotatingParser { bool ParensAreType = !Tok.Previous || Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) || - Tok.Previous->isTypeName(IsCpp) || + Tok.Previous->isTypeName() || IsQualifiedPointerOrReference(Tok.Previous); bool ParensCouldEndDecl = Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater); @@ -3010,7 +3010,6 @@ class AnnotatingParser { AnnotatedLine &Line; FormatToken *CurrentToken; bool AutoFound; - bool IsCpp; const AdditionalKeywords &Keywords; SmallVector<ScopeType> &Scopes; @@ -3585,7 +3584,7 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) { // This function heuristically determines whether 'Current' starts the name of a // function declaration. -static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current, +static bool isFunctionDeclarationName(const FormatToken &Current, const AnnotatedLine &Line, FormatToken *&ClosingParen) { assert(Current.Previous); @@ -3596,8 +3595,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current, if (!Current.Tok.getIdentifierInfo()) return false; - auto skipOperatorName = - [IsCpp](const FormatToken *Next) -> const FormatToken * { + auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * { for (; Next; Next = Next->Next) { if (Next->is(TT_OverloadedOperatorLParen)) return Next; @@ -3616,8 +3614,8 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current, Next = Next->Next; continue; } - if ((Next->isTypeName(IsCpp) || Next->is(tok::identifier)) && - Next->Next && Next->Next->isPointerOrReference()) { + if ((Next->isTypeName() || Next->is(tok::identifier)) && Next->Next && + Next->Next->isPointerOrReference()) { // For operator void*(), operator char*(), operator Foo*(). Next = Next->Next; continue; @@ -3665,7 +3663,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current, } if (Next->isNot(tok::identifier)) return false; - } else if (isCppAttribute(IsCpp, *Next)) { + } else if (isCppAttribute(*Next)) { Next = Next->MatchingParen; if (!Next) return false; @@ -3714,7 +3712,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current, Tok = Tok->MatchingParen; continue; } - if (Tok->is(tok::kw_const) || Tok->isTypeName(IsCpp) || + if (Tok->is(tok::kw_const) || Tok->isTypeName() || Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) { return true; } @@ -3776,8 +3774,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const { if (Tok->Previous->EndsCppAttributeGroup) AfterLastAttribute = Tok; if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName); - IsCtorOrDtor || - isFunctionDeclarationName(IsCpp, *Tok, Line, ClosingParen)) { + IsCtorOrDtor || isFunctionDeclarationName(*Tok, Line, ClosingParen)) { if (!IsCtorOrDtor) Tok->setFinalizedType(TT_FunctionDeclarationName); LineIsFunctionDeclaration = true; @@ -4377,7 +4374,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(IsCpp) && Right.Next && Right.Next->Next && + if (Left.isTypeOrIdentifier() && Right.Next && Right.Next->Next && Right.Next->Next->is(TT_RangeBasedForLoopColon)) { return getTokenPointerOrReferenceAlignment(Right) != FormatStyle::PAS_Left; @@ -4420,8 +4417,8 @@ 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 (Left.Previous && Left.Previous->isTypeOrIdentifier(IsCpp) && - Right.Next && Right.Next->is(TT_RangeBasedForLoopColon)) { + if (Left.Previous && Left.Previous->isTypeOrIdentifier() && Right.Next && + Right.Next->is(TT_RangeBasedForLoopColon)) { return getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right; } @@ -4459,7 +4456,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(IsCpp)) { + if (Previous->is(tok::identifier) || Previous->isTypeName()) { Previous = Previous->getPreviousNonComment(); continue; } @@ -4648,7 +4645,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, if (!Style.isVerilog() && (Left.isOneOf(tok::identifier, tok::greater, tok::r_square, tok::r_paren) || - Left.isTypeName(IsCpp)) && + Left.isTypeName()) && 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 a631e5f52bc60a..b0931e85c16c4d 100644 --- a/clang/lib/Format/TokenAnnotator.h +++ b/clang/lib/Format/TokenAnnotator.h @@ -212,7 +212,9 @@ class AnnotatedLine { class TokenAnnotator { public: TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords) - : Style(Style), IsCpp(Style.isCpp()), Keywords(Keywords) {} + : Style(Style), Keywords(Keywords) { + assert(IsCpp == Style.isCpp()); + } /// Adapts the indent levels of comment lines to the indent of the /// subsequent line. @@ -260,8 +262,6 @@ class TokenAnnotator { const FormatStyle &Style; - bool IsCpp; - const AdditionalKeywords &Keywords; SmallVector<ScopeType> Scopes; diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index e3f0af176b567d..a1f6ce05e45ebc 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -159,14 +159,16 @@ UnwrappedLineParser::UnwrappedLineParser( llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator, IdentifierTable &IdentTable) : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), - CurrentLines(&Lines), Style(Style), IsCpp(Style.isCpp()), - Keywords(Keywords), CommentPragmasRegex(Style.CommentPragmas), - Tokens(nullptr), Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1), + CurrentLines(&Lines), Style(Style), 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) {} + Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) { + assert(IsCpp == Style.isCpp()); +} void UnwrappedLineParser::reset() { PPBranchLevel = -1; @@ -1865,7 +1867,7 @@ void UnwrappedLineParser::parseStructuralElement( case tok::caret: nextToken(); // Block return type. - if (FormatTok->Tok.isAnyIdentifier() || FormatTok->isTypeName(IsCpp)) { + if (FormatTok->Tok.isAnyIdentifier() || FormatTok->isTypeName()) { nextToken(); // Return types: pointers are ok too. while (FormatTok->is(tok::star)) @@ -2221,7 +2223,7 @@ bool UnwrappedLineParser::tryToParseLambda() { bool InTemplateParameterList = false; while (FormatTok->isNot(tok::l_brace)) { - if (FormatTok->isTypeName(IsCpp)) { + if (FormatTok->isTypeName()) { nextToken(); continue; } @@ -2338,7 +2340,7 @@ bool UnwrappedLineParser::tryToParseLambdaIntroducer() { !Previous->isOneOf(tok::kw_return, tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return)) || Previous->closesScope())) || - LeftSquare->isCppStructuredBinding(IsCpp)) { + LeftSquare->isCppStructuredBinding()) { return false; } if (FormatTok->is(tok::l_square) || tok::isLiteral(FormatTok->Tok.getKind())) @@ -3414,7 +3416,7 @@ bool clang::format::UnwrappedLineParser::parseRequires() { break; } default: - if (PreviousNonComment->isTypeOrIdentifier(IsCpp)) { + if (PreviousNonComment->isTypeOrIdentifier()) { // This is a requires clause. parseRequiresClause(RequiresToken); return true; @@ -3477,7 +3479,7 @@ bool clang::format::UnwrappedLineParser::parseRequires() { --OpenAngles; break; default: - if (NextToken->isTypeName(IsCpp)) { + if (NextToken->isTypeName()) { FormatTok = Tokens->setPosition(StoredPosition); parseRequiresExpression(RequiresToken); return false; @@ -3962,7 +3964,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(IsCpp))) { + !Previous->isTypeOrIdentifier())) { // 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 619fbb217882b3..1403533a2d0ef6 100644 --- a/clang/lib/Format/UnwrappedLineParser.h +++ b/clang/lib/Format/UnwrappedLineParser.h @@ -324,7 +324,6 @@ class UnwrappedLineParser { llvm::BitVector DeclarationScopeStack; const FormatStyle &Style; - bool IsCpp; const AdditionalKeywords &Keywords; llvm::Regex CommentPragmasRegex; diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index fcba0e63c78254..21c18a03a4fc7f 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -1776,9 +1776,22 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) { auto Style = getLLVMStyle(); Style.TypeNames.push_back("MyType"); Tokens = annotate("int iso_time(MyType);", Style); + ASSERT_TRUE(IsCpp); ASSERT_EQ(Tokens.size(), 7u) << Tokens; EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName); EXPECT_TOKEN(Tokens[3], tok::identifier, TT_TypeName); + + Style.Language = FormatStyle::LK_CSharp; + Tokens = annotate("int iso_time(time_t);", Style); + ASSERT_FALSE(IsCpp); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName); + + Style.Language = FormatStyle::LK_ObjC; + Tokens = annotate("int iso_time(time_t);", Style); + ASSERT_TRUE(IsCpp); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName); } TEST_F(TokenAnnotatorTest, UnderstandsCtorAndDtorDeclNames) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits