Skip to content

Commit 6f31cf5

Browse files
committed
Revert "[clang-format][NFC] Eliminate the IsCpp parameter in all functions (llvm#84599)"
This reverts c3a1eb6 (and the related commit f3c5278) which makes cleanupAroundReplacements() no longer thread-safe.
1 parent a289f66 commit 6f31cf5

12 files changed

+76
-91
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,7 @@ ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
232232
: Style(Style), Keywords(Keywords), SourceMgr(SourceMgr),
233233
Whitespaces(Whitespaces), Encoding(Encoding),
234234
BinPackInconclusiveFunctions(BinPackInconclusiveFunctions),
235-
CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {
236-
assert(IsCpp == Style.isCpp());
237-
}
235+
CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {}
238236

239237
LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
240238
unsigned FirstStartColumn,
@@ -399,7 +397,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
399397
}
400398
if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
401399
(Previous.is(TT_TemplateCloser) && Current.is(TT_StartOfName) &&
402-
State.Line->First->isNot(TT_AttributeSquare) && IsCpp &&
400+
State.Line->First->isNot(TT_AttributeSquare) && Style.isCpp() &&
403401
// FIXME: This is a temporary workaround for the case where clang-format
404402
// sets BreakBeforeParameter to avoid bin packing and this creates a
405403
// completely unnecessary line break after a template type that isn't
@@ -670,8 +668,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
670668
auto &CurrentState = State.Stack.back();
671669

672670
bool DisallowLineBreaksOnThisLine =
673-
Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && IsCpp &&
674-
[&Current] {
671+
Style.LambdaBodyIndentation == FormatStyle::LBI_Signature &&
672+
Style.isCpp() && [&Current] {
675673
// Deal with lambda arguments in C++. The aim here is to ensure that we
676674
// don't over-indent lambda function bodies when lambdas are passed as
677675
// arguments to function calls. We do this by ensuring that either all
@@ -1085,7 +1083,7 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
10851083
// Any break on this level means that the parent level has been broken
10861084
// and we need to avoid bin packing there.
10871085
bool NestedBlockSpecialCase =
1088-
(!IsCpp && Current.is(tok::r_brace) && State.Stack.size() > 1 &&
1086+
(!Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 &&
10891087
State.Stack[State.Stack.size() - 2].NestedBlockInlined) ||
10901088
(Style.Language == FormatStyle::LK_ObjC && Current.is(tok::r_brace) &&
10911089
State.Stack.size() > 1 && !Style.ObjCBreakBeforeNestedBlockParam);

clang/lib/Format/Format.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3827,15 +3827,13 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle &Style,
38273827
}
38283828

38293829
LangOptions getFormattingLangOpts(const FormatStyle &Style) {
3830-
IsCpp = Style.isCpp();
3830+
LangOptions LangOpts;
38313831

38323832
FormatStyle::LanguageStandard LexingStd = Style.Standard;
38333833
if (LexingStd == FormatStyle::LS_Auto)
38343834
LexingStd = FormatStyle::LS_Latest;
38353835
if (LexingStd == FormatStyle::LS_Latest)
38363836
LexingStd = FormatStyle::LS_Cpp20;
3837-
3838-
LangOptions LangOpts;
38393837
LangOpts.CPlusPlus = 1;
38403838
LangOpts.CPlusPlus11 = LexingStd >= FormatStyle::LS_Cpp11;
38413839
LangOpts.CPlusPlus14 = LexingStd >= FormatStyle::LS_Cpp14;
@@ -3846,8 +3844,10 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) {
38463844
// the sequence "<::" will be unconditionally treated as "[:".
38473845
// Cf. Lexer::LexTokenInternal.
38483846
LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
3847+
38493848
LangOpts.LineComment = 1;
3850-
LangOpts.CXXOperatorNames = IsCpp;
3849+
bool AlternativeOperators = Style.isCpp();
3850+
LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
38513851
LangOpts.Bool = 1;
38523852
LangOpts.ObjC = 1;
38533853
LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.

clang/lib/Format/FormatToken.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
namespace clang {
1919
namespace format {
2020

21-
bool IsCpp = false;
22-
2321
const char *getTokenTypeName(TokenType Type) {
2422
static const char *const TokNames[] = {
2523
#define TYPE(X) #X,
@@ -77,15 +75,15 @@ static SmallVector<StringRef> CppNonKeywordTypes = {
7775
"uint32_t", "uint64_t", "uint8_t", "uintptr_t",
7876
};
7977

80-
bool FormatToken::isTypeName() const {
78+
bool FormatToken::isTypeName(bool IsCpp) const {
8179
return is(TT_TypeName) || isSimpleTypeSpecifier() ||
8280
(IsCpp && is(tok::identifier) &&
8381
std::binary_search(CppNonKeywordTypes.begin(),
8482
CppNonKeywordTypes.end(), TokenText));
8583
}
8684

87-
bool FormatToken::isTypeOrIdentifier() const {
88-
return isTypeName() || isOneOf(tok::kw_auto, tok::identifier);
85+
bool FormatToken::isTypeOrIdentifier(bool IsCpp) const {
86+
return isTypeName(IsCpp) || isOneOf(tok::kw_auto, tok::identifier);
8987
}
9088

9189
bool FormatToken::isBlockIndentedInitRBrace(const FormatStyle &Style) const {

clang/lib/Format/FormatToken.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
namespace clang {
2525
namespace format {
2626

27-
/// Whether the language is C/C++/Objective-C/Objective-C++.
28-
extern bool IsCpp;
29-
3027
#define LIST_TOKEN_TYPES \
3128
TYPE(ArrayInitializerLSquare) \
3229
TYPE(ArraySubscriptLSquare) \
@@ -681,9 +678,9 @@ struct FormatToken {
681678
/// Determine whether the token is a simple-type-specifier.
682679
[[nodiscard]] bool isSimpleTypeSpecifier() const;
683680

684-
[[nodiscard]] bool isTypeName() const;
681+
[[nodiscard]] bool isTypeName(bool IsCpp) const;
685682

686-
[[nodiscard]] bool isTypeOrIdentifier() const;
683+
[[nodiscard]] bool isTypeOrIdentifier(bool IsCpp) const;
687684

688685
bool isObjCAccessSpecifier() const {
689686
return is(tok::at) && Next &&
@@ -828,7 +825,7 @@ struct FormatToken {
828825

829826
/// Returns whether the token is the left square bracket of a C++
830827
/// structured binding declaration.
831-
bool isCppStructuredBinding() const {
828+
bool isCppStructuredBinding(bool IsCpp) const {
832829
if (!IsCpp || isNot(tok::l_square))
833830
return false;
834831
const FormatToken *T = this;

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ FormatTokenLexer::FormatTokenLexer(
3030
Encoding(Encoding), Allocator(Allocator), FirstInLineIndex(0),
3131
FormattingDisabled(false), MacroBlockBeginRegex(Style.MacroBlockBegin),
3232
MacroBlockEndRegex(Style.MacroBlockEnd) {
33-
assert(IsCpp == Style.isCpp());
3433
Lex.reset(new Lexer(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts));
3534
Lex->SetKeepWhitespaceMode(true);
3635

@@ -111,7 +110,7 @@ void FormatTokenLexer::tryMergePreviousTokens() {
111110
return;
112111
if (tryMergeForEach())
113112
return;
114-
if (IsCpp && tryTransformTryUsageForC())
113+
if (Style.isCpp() && tryTransformTryUsageForC())
115114
return;
116115

117116
if (Style.isJavaScript() || Style.isCSharp()) {
@@ -1338,7 +1337,7 @@ FormatToken *FormatTokenLexer::getNextToken() {
13381337
Column = FormatTok->LastLineColumnWidth;
13391338
}
13401339

1341-
if (IsCpp) {
1340+
if (Style.isCpp()) {
13421341
auto *Identifier = FormatTok->Tok.getIdentifierInfo();
13431342
auto it = Macros.find(Identifier);
13441343
if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&

clang/lib/Format/QualifierAlignmentFixer.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,20 +262,24 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
262262
if (isPossibleMacro(TypeToken))
263263
return Tok;
264264

265+
const bool IsCpp = Style.isCpp();
266+
265267
// The case `const long long int volatile` -> `long long int const volatile`
266268
// The case `long const long int volatile` -> `long long int const volatile`
267269
// The case `long long volatile int const` -> `long long int const volatile`
268270
// The case `const long long volatile int` -> `long long int const volatile`
269-
if (TypeToken->isTypeName()) {
271+
if (TypeToken->isTypeName(IsCpp)) {
270272
// The case `const decltype(foo)` -> `const decltype(foo)`
271273
// The case `const typeof(foo)` -> `const typeof(foo)`
272274
// The case `const _Atomic(foo)` -> `const _Atomic(foo)`
273275
if (TypeToken->isOneOf(tok::kw_decltype, tok::kw_typeof, tok::kw__Atomic))
274276
return Tok;
275277

276278
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
277-
while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment()))
279+
while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment(),
280+
IsCpp)) {
278281
LastSimpleTypeSpecifier = LastSimpleTypeSpecifier->getNextNonComment();
282+
}
279283

280284
rotateTokens(SourceMgr, Fixes, Tok, LastSimpleTypeSpecifier,
281285
/*Left=*/false);
@@ -285,7 +289,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
285289
// The case `unsigned short const` -> `unsigned short const`
286290
// The case:
287291
// `unsigned short volatile const` -> `unsigned short const volatile`
288-
if (PreviousCheck && PreviousCheck->isTypeName()) {
292+
if (PreviousCheck && PreviousCheck->isTypeName(IsCpp)) {
289293
if (LastQual != Tok)
290294
rotateTokens(SourceMgr, Fixes, Tok, LastQual, /*Left=*/false);
291295
return Tok;
@@ -402,11 +406,11 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
402406
// The case `volatile long long const int` -> `const volatile long long int`
403407
// The case `const long long volatile int` -> `const volatile long long int`
404408
// The case `long volatile long int const` -> `const volatile long long int`
405-
if (TypeToken->isTypeName()) {
409+
if (const bool IsCpp = Style.isCpp(); TypeToken->isTypeName(IsCpp)) {
406410
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
407411
while (isConfiguredQualifierOrType(
408412
LastSimpleTypeSpecifier->getPreviousNonComment(),
409-
ConfiguredQualifierTokens)) {
413+
ConfiguredQualifierTokens, IsCpp)) {
410414
LastSimpleTypeSpecifier =
411415
LastSimpleTypeSpecifier->getPreviousNonComment();
412416
}
@@ -521,9 +525,7 @@ LeftRightQualifierAlignmentFixer::LeftRightQualifierAlignmentFixer(
521525
const std::string &Qualifier,
522526
const std::vector<tok::TokenKind> &QualifierTokens, bool RightAlign)
523527
: TokenAnalyzer(Env, Style), Qualifier(Qualifier), RightAlign(RightAlign),
524-
ConfiguredQualifierTokens(QualifierTokens) {
525-
IsCpp = Style.isCpp();
526-
}
528+
ConfiguredQualifierTokens(QualifierTokens) {}
527529

528530
std::pair<tooling::Replacements, unsigned>
529531
LeftRightQualifierAlignmentFixer::analyze(
@@ -606,15 +608,16 @@ void prepareLeftRightOrderingForQualifierAlignmentFixer(
606608
}
607609
}
608610

609-
bool LeftRightQualifierAlignmentFixer::isQualifierOrType(
610-
const FormatToken *Tok) {
611+
bool LeftRightQualifierAlignmentFixer::isQualifierOrType(const FormatToken *Tok,
612+
bool IsCpp) {
611613
return Tok &&
612-
(Tok->isTypeName() || Tok->is(tok::kw_auto) || isQualifier(Tok));
614+
(Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) || isQualifier(Tok));
613615
}
614616

615617
bool LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
616-
const FormatToken *Tok, const std::vector<tok::TokenKind> &Qualifiers) {
617-
return Tok && (Tok->isTypeName() || Tok->is(tok::kw_auto) ||
618+
const FormatToken *Tok, const std::vector<tok::TokenKind> &Qualifiers,
619+
bool IsCpp) {
620+
return Tok && (Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) ||
618621
isConfiguredQualifier(Tok, Qualifiers));
619622
}
620623

clang/lib/Format/QualifierAlignmentFixer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ class LeftRightQualifierAlignmentFixer : public TokenAnalyzer {
7171
tok::TokenKind QualifierType);
7272

7373
// Is the Token a simple or qualifier type
74-
static bool isQualifierOrType(const FormatToken *Tok);
74+
static bool isQualifierOrType(const FormatToken *Tok, bool IsCpp = true);
7575
static bool
7676
isConfiguredQualifierOrType(const FormatToken *Tok,
77-
const std::vector<tok::TokenKind> &Qualifiers);
77+
const std::vector<tok::TokenKind> &Qualifiers,
78+
bool IsCpp = true);
7879

7980
// Is the Token likely a Macro
8081
static bool isPossibleMacro(const FormatToken *Tok);

0 commit comments

Comments
 (0)