[clang] 6f31cf5 - Revert "[clang-format][NFC] Eliminate the IsCpp parameter in all functions (#84599)"
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 19 18:07:11 PDT 2024
Author: Owen Pan
Date: 2024-03-19T18:06:59-07:00
New Revision: 6f31cf51dfdc2c317ba8149d57d2ffb583403833
URL: https://github.com/llvm/llvm-project/commit/6f31cf51dfdc2c317ba8149d57d2ffb583403833
DIFF: https://github.com/llvm/llvm-project/commit/6f31cf51dfdc2c317ba8149d57d2ffb583403833.diff
LOG: Revert "[clang-format][NFC] Eliminate the IsCpp parameter in all functions (#84599)"
This reverts c3a1eb6207d8 (and the related commit f3c5278efa3b) which makes
cleanupAroundReplacements() no longer thread-safe.
Added:
Modified:
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/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index b3de317f16336c..6ccc81909368ef 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -232,9 +232,7 @@ ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
: Style(Style), Keywords(Keywords), SourceMgr(SourceMgr),
Whitespaces(Whitespaces), Encoding(Encoding),
BinPackInconclusiveFunctions(BinPackInconclusiveFunctions),
- CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {
- assert(IsCpp == Style.isCpp());
-}
+ CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {}
LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
unsigned FirstStartColumn,
@@ -399,7 +397,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) && IsCpp &&
+ State.Line->First->isNot(TT_AttributeSquare) && Style.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
@@ -670,8 +668,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
auto &CurrentState = State.Stack.back();
bool DisallowLineBreaksOnThisLine =
- Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && IsCpp &&
- [&Current] {
+ Style.LambdaBodyIndentation == FormatStyle::LBI_Signature &&
+ Style.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
@@ -1085,7 +1083,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 =
- (!IsCpp && Current.is(tok::r_brace) && State.Stack.size() > 1 &&
+ (!Style.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 89813badc8ec20..63ec3a88978dd9 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3827,15 +3827,13 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle &Style,
}
LangOptions getFormattingLangOpts(const FormatStyle &Style) {
- IsCpp = Style.isCpp();
+ LangOptions LangOpts;
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;
@@ -3846,8 +3844,10 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) {
// the sequence "<::" will be unconditionally treated as "[:".
// Cf. Lexer::LexTokenInternal.
LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
+
LangOpts.LineComment = 1;
- LangOpts.CXXOperatorNames = 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 cd94a9df6cff79..99405fb9d08db5 100644
--- a/clang/lib/Format/FormatToken.cpp
+++ b/clang/lib/Format/FormatToken.cpp
@@ -18,8 +18,6 @@
namespace clang {
namespace format {
-bool IsCpp = false;
-
const char *getTokenTypeName(TokenType Type) {
static const char *const TokNames[] = {
#define TYPE(X) #X,
@@ -77,15 +75,15 @@ static SmallVector<StringRef> CppNonKeywordTypes = {
"uint32_t", "uint64_t", "uint8_t", "uintptr_t",
};
-bool FormatToken::isTypeName() const {
+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 {
- return isTypeName() || 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 a141db3d41d7b9..06f567059c3576 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -24,9 +24,6 @@
namespace clang {
namespace format {
-/// Whether the language is C/C++/Objective-C/Objective-C++.
-extern bool IsCpp;
-
#define LIST_TOKEN_TYPES \
TYPE(ArrayInitializerLSquare) \
TYPE(ArraySubscriptLSquare) \
@@ -681,9 +678,9 @@ struct FormatToken {
/// Determine whether the token is a simple-type-specifier.
[[nodiscard]] bool isSimpleTypeSpecifier() const;
- [[nodiscard]] bool isTypeName() const;
+ [[nodiscard]] bool isTypeName(bool IsCpp) const;
- [[nodiscard]] bool isTypeOrIdentifier() const;
+ [[nodiscard]] bool isTypeOrIdentifier(bool IsCpp) const;
bool isObjCAccessSpecifier() const {
return is(tok::at) && Next &&
@@ -828,7 +825,7 @@ struct FormatToken {
/// Returns whether the token is the left square bracket of a C++
/// structured binding declaration.
- bool isCppStructuredBinding() const {
+ bool isCppStructuredBinding(bool IsCpp) 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 a1c8df80e6a9f5..12db4def10fbd8 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -30,7 +30,6 @@ 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);
@@ -111,7 +110,7 @@ void FormatTokenLexer::tryMergePreviousTokens() {
return;
if (tryMergeForEach())
return;
- if (IsCpp && tryTransformTryUsageForC())
+ if (Style.isCpp() && tryTransformTryUsageForC())
return;
if (Style.isJavaScript() || Style.isCSharp()) {
@@ -1338,7 +1337,7 @@ FormatToken *FormatTokenLexer::getNextToken() {
Column = FormatTok->LastLineColumnWidth;
}
- if (IsCpp) {
+ if (Style.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 32d5153fc8151d..4bc2698a1146f6 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -262,11 +262,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()) {
+ 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)`
@@ -274,8 +276,10 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
return Tok;
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
- while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment()))
+ while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment(),
+ IsCpp)) {
LastSimpleTypeSpecifier = LastSimpleTypeSpecifier->getNextNonComment();
+ }
rotateTokens(SourceMgr, Fixes, Tok, LastSimpleTypeSpecifier,
/*Left=*/false);
@@ -285,7 +289,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()) {
+ if (PreviousCheck && PreviousCheck->isTypeName(IsCpp)) {
if (LastQual != Tok)
rotateTokens(SourceMgr, Fixes, Tok, LastQual, /*Left=*/false);
return Tok;
@@ -402,11 +406,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()) {
+ if (const bool IsCpp = Style.isCpp(); TypeToken->isTypeName(IsCpp)) {
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
while (isConfiguredQualifierOrType(
LastSimpleTypeSpecifier->getPreviousNonComment(),
- ConfiguredQualifierTokens)) {
+ ConfiguredQualifierTokens, IsCpp)) {
LastSimpleTypeSpecifier =
LastSimpleTypeSpecifier->getPreviousNonComment();
}
@@ -521,9 +525,7 @@ LeftRightQualifierAlignmentFixer::LeftRightQualifierAlignmentFixer(
const std::string &Qualifier,
const std::vector<tok::TokenKind> &QualifierTokens, bool RightAlign)
: TokenAnalyzer(Env, Style), Qualifier(Qualifier), RightAlign(RightAlign),
- ConfiguredQualifierTokens(QualifierTokens) {
- IsCpp = Style.isCpp();
-}
+ ConfiguredQualifierTokens(QualifierTokens) {}
std::pair<tooling::Replacements, unsigned>
LeftRightQualifierAlignmentFixer::analyze(
@@ -606,15 +608,16 @@ void prepareLeftRightOrderingForQualifierAlignmentFixer(
}
}
-bool LeftRightQualifierAlignmentFixer::isQualifierOrType(
- const FormatToken *Tok) {
+bool LeftRightQualifierAlignmentFixer::isQualifierOrType(const FormatToken *Tok,
+ bool IsCpp) {
return Tok &&
- (Tok->isTypeName() || Tok->is(tok::kw_auto) || isQualifier(Tok));
+ (Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) || isQualifier(Tok));
}
bool LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
- const FormatToken *Tok, const std::vector<tok::TokenKind> &Qualifiers) {
- return Tok && (Tok->isTypeName() || Tok->is(tok::kw_auto) ||
+ const FormatToken *Tok, const std::vector<tok::TokenKind> &Qualifiers,
+ 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 e922d800559510..e1cc27e62b13a0 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.h
+++ b/clang/lib/Format/QualifierAlignmentFixer.h
@@ -71,10 +71,11 @@ class LeftRightQualifierAlignmentFixer : public TokenAnalyzer {
tok::TokenKind QualifierType);
// Is the Token a simple or qualifier type
- static bool isQualifierOrType(const FormatToken *Tok);
+ static bool isQualifierOrType(const FormatToken *Tok, bool IsCpp = true);
static bool
isConfiguredQualifierOrType(const FormatToken *Tok,
- const std::vector<tok::TokenKind> &Qualifiers);
+ const std::vector<tok::TokenKind> &Qualifiers,
+ bool IsCpp = true);
// 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 911f7cca470a32..8dc8e13688ed9a 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -80,7 +80,7 @@ static bool isKeywordWithCondition(const FormatToken &Tok) {
}
/// Returns \c true if the token starts a C++ attribute, \c false otherwise.
-static bool isCppAttribute(const FormatToken &Tok) {
+static bool isCppAttribute(bool IsCpp, 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
@@ -122,8 +122,7 @@ class AnnotatingParser {
const AdditionalKeywords &Keywords,
SmallVector<ScopeType> &Scopes)
: Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
- Keywords(Keywords), Scopes(Scopes) {
- assert(IsCpp == Style.isCpp());
+ IsCpp(Style.isCpp()), Keywords(Keywords), Scopes(Scopes) {
Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
resetTokenMetadata();
}
@@ -559,7 +558,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()) &&
+ CurrentToken->Previous->isTypeName(IsCpp)) &&
!(CurrentToken->is(tok::l_brace) ||
(CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
Contexts.back().IsExpression = false;
@@ -679,7 +678,7 @@ class AnnotatingParser {
const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
const bool IsCpp11AttributeSpecifier =
- isCppAttribute(*Left) || IsInnerSquare;
+ isCppAttribute(IsCpp, *Left) || IsInnerSquare;
// Treat C# Attributes [STAThread] much like C++ attributes [[...]].
bool IsCSharpAttributeSpecifier =
@@ -687,7 +686,7 @@ class AnnotatingParser {
Contexts.back().InCSharpAttributeSpecifier;
bool InsideInlineASM = Line.startsWith(tok::kw_asm);
- bool IsCppStructuredBinding = Left->isCppStructuredBinding();
+ bool IsCppStructuredBinding = Left->isCppStructuredBinding(IsCpp);
bool StartsObjCMethodExpr =
!IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
@@ -2613,7 +2612,7 @@ class AnnotatingParser {
return true;
// MyClass a;
- if (PreviousNotConst->isTypeName())
+ if (PreviousNotConst->isTypeName(IsCpp))
return true;
// type[] a in Java
@@ -2728,7 +2727,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(*Tok.Next)) {
+ isCppAttribute(IsCpp, *Tok.Next)) {
return false;
}
@@ -2744,9 +2743,10 @@ class AnnotatingParser {
}
// Heuristically try to determine whether the parentheses contain a type.
- auto IsQualifiedPointerOrReference = [](FormatToken *T) {
+ auto IsQualifiedPointerOrReference = [this](FormatToken *T) {
// This is used to handle cases such as x = (foo *const)&y;
- assert(!T->isTypeName() && "Should have already been checked");
+ assert(!T->isTypeName(IsCpp) && "Should have already been checked");
+ (void)IsCpp; // Avoid -Wunused-lambda-capture when assertion is disabled.
// Strip trailing qualifiers such as const or volatile when checking
// whether the parens could be a cast to a pointer/reference type.
while (T) {
@@ -2778,7 +2778,7 @@ class AnnotatingParser {
bool ParensAreType =
!Tok.Previous ||
Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
- Tok.Previous->isTypeName() ||
+ Tok.Previous->isTypeName(IsCpp) ||
IsQualifiedPointerOrReference(Tok.Previous);
bool ParensCouldEndDecl =
Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
@@ -3049,6 +3049,7 @@ class AnnotatingParser {
AnnotatedLine &Line;
FormatToken *CurrentToken;
bool AutoFound;
+ bool IsCpp;
const AdditionalKeywords &Keywords;
SmallVector<ScopeType> &Scopes;
@@ -3623,7 +3624,7 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
// This function heuristically determines whether 'Current' starts the name of a
// function declaration.
-static bool isFunctionDeclarationName(const FormatToken &Current,
+static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
const AnnotatedLine &Line,
FormatToken *&ClosingParen) {
assert(Current.Previous);
@@ -3641,7 +3642,8 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
return false;
}
- auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
+ auto skipOperatorName =
+ [IsCpp](const FormatToken *Next) -> const FormatToken * {
for (; Next; Next = Next->Next) {
if (Next->is(TT_OverloadedOperatorLParen))
return Next;
@@ -3660,8 +3662,8 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
Next = Next->Next;
continue;
}
- if ((Next->isTypeName() || Next->is(tok::identifier)) && Next->Next &&
- Next->Next->isPointerOrReference()) {
+ if ((Next->isTypeName(IsCpp) || Next->is(tok::identifier)) &&
+ Next->Next && Next->Next->isPointerOrReference()) {
// For operator void*(), operator char*(), operator Foo*().
Next = Next->Next;
continue;
@@ -3708,7 +3710,7 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
}
if (Next->isNot(tok::identifier))
return false;
- } else if (isCppAttribute(*Next)) {
+ } else if (isCppAttribute(IsCpp, *Next)) {
Next = Next->MatchingParen;
if (!Next)
return false;
@@ -3757,7 +3759,7 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
Tok = Tok->MatchingParen;
continue;
}
- if (Tok->is(tok::kw_const) || Tok->isTypeName() ||
+ if (Tok->is(tok::kw_const) || Tok->isTypeName(IsCpp) ||
Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
return true;
}
@@ -3819,7 +3821,8 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
if (Tok->Previous->EndsCppAttributeGroup)
AfterLastAttribute = Tok;
if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
- IsCtorOrDtor || isFunctionDeclarationName(*Tok, Line, ClosingParen)) {
+ IsCtorOrDtor ||
+ isFunctionDeclarationName(IsCpp, *Tok, Line, ClosingParen)) {
if (!IsCtorOrDtor)
Tok->setFinalizedType(TT_FunctionDeclarationName);
LineIsFunctionDeclaration = true;
@@ -4423,7 +4426,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() && 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;
@@ -4466,8 +4469,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() && Right.Next &&
- Right.Next->is(TT_RangeBasedForLoopColon)) {
+ if (Left.Previous && Left.Previous->isTypeOrIdentifier(IsCpp) &&
+ Right.Next && Right.Next->is(TT_RangeBasedForLoopColon)) {
return getTokenPointerOrReferenceAlignment(Left) !=
FormatStyle::PAS_Right;
}
@@ -4505,7 +4508,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()) {
+ if (Previous->is(tok::identifier) || Previous->isTypeName(IsCpp)) {
Previous = Previous->getPreviousNonComment();
continue;
}
@@ -4694,7 +4697,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
if (!Style.isVerilog() &&
(Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
tok::r_paren) ||
- Left.isTypeName()) &&
+ 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 aa44b31e0fce9e..25a24dccb1b834 100644
--- a/clang/lib/Format/TokenAnnotator.h
+++ b/clang/lib/Format/TokenAnnotator.h
@@ -211,9 +211,7 @@ class AnnotatedLine {
class TokenAnnotator {
public:
TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
- : Style(Style), Keywords(Keywords) {
- assert(IsCpp == Style.isCpp());
- }
+ : Style(Style), IsCpp(Style.isCpp()), Keywords(Keywords) {}
/// Adapts the indent levels of comment lines to the indent of the
/// subsequent line.
@@ -261,6 +259,8 @@ 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 6ac4c7dc1a187b..e89dfbdb2e0cba 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -148,16 +148,14 @@ UnwrappedLineParser::UnwrappedLineParser(
llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
IdentifierTable &IdentTable)
: Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
- CurrentLines(&Lines), Style(Style), Keywords(Keywords),
- CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
- Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
+ CurrentLines(&Lines), Style(Style), IsCpp(Style.isCpp()),
+ 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 == Style.isCpp());
-}
+ Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) {}
void UnwrappedLineParser::reset() {
PPBranchLevel = -1;
@@ -1855,7 +1853,7 @@ void UnwrappedLineParser::parseStructuralElement(
case tok::caret:
nextToken();
// Block return type.
- if (FormatTok->Tok.isAnyIdentifier() || FormatTok->isTypeName()) {
+ if (FormatTok->Tok.isAnyIdentifier() || FormatTok->isTypeName(IsCpp)) {
nextToken();
// Return types: pointers are ok too.
while (FormatTok->is(tok::star))
@@ -2211,7 +2209,7 @@ bool UnwrappedLineParser::tryToParseLambda() {
bool InTemplateParameterList = false;
while (FormatTok->isNot(tok::l_brace)) {
- if (FormatTok->isTypeName()) {
+ if (FormatTok->isTypeName(IsCpp)) {
nextToken();
continue;
}
@@ -2328,7 +2326,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()) {
+ LeftSquare->isCppStructuredBinding(IsCpp)) {
return false;
}
if (FormatTok->is(tok::l_square) || tok::isLiteral(FormatTok->Tok.getKind()))
@@ -3404,7 +3402,7 @@ bool clang::format::UnwrappedLineParser::parseRequires() {
break;
}
default:
- if (PreviousNonComment->isTypeOrIdentifier()) {
+ if (PreviousNonComment->isTypeOrIdentifier(IsCpp)) {
// This is a requires clause.
parseRequiresClause(RequiresToken);
return true;
@@ -3467,7 +3465,7 @@ bool clang::format::UnwrappedLineParser::parseRequires() {
--OpenAngles;
break;
default:
- if (NextToken->isTypeName()) {
+ if (NextToken->isTypeName(IsCpp)) {
FormatTok = Tokens->setPosition(StoredPosition);
parseRequiresExpression(RequiresToken);
return false;
@@ -3956,7 +3954,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())) {
+ !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 da3188ed240c93..e2cf28c0c065dc 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -315,6 +315,7 @@ 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 bfb71d1f7e97ea..1aa855b3419877 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1776,22 +1776,9 @@ 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) {
More information about the cfe-commits
mailing list