Skip to content

Commit b0d1e32

Browse files
authored
Revert "[clang-format][NFC] Eliminate the IsCpp parameter in all functions" (#85353)
Reverts #84599 This broke the presubmit bot.
1 parent 65b123e commit b0d1e32

13 files changed

+76
-91
lines changed

clang/include/clang/Format/Format.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5228,9 +5228,6 @@ extern const char *DefaultFormatStyle;
52285228
/// Different builds can modify the value to the preferred styles.
52295229
extern const char *DefaultFallbackStyle;
52305230

5231-
/// Whether the language is C/C++/Objective-C/Objective-C++.
5232-
extern bool IsCpp;
5233-
52345231
/// Construct a FormatStyle based on ``StyleName``.
52355232
///
52365233
/// ``StyleName`` can take several forms:

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,7 @@ ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
241241
: Style(Style), Keywords(Keywords), SourceMgr(SourceMgr),
242242
Whitespaces(Whitespaces), Encoding(Encoding),
243243
BinPackInconclusiveFunctions(BinPackInconclusiveFunctions),
244-
CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {
245-
assert(IsCpp == Style.isCpp());
246-
}
244+
CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {}
247245

248246
LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
249247
unsigned FirstStartColumn,
@@ -408,7 +406,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
408406
}
409407
if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
410408
(Previous.is(TT_TemplateCloser) && Current.is(TT_StartOfName) &&
411-
State.Line->First->isNot(TT_AttributeSquare) && IsCpp &&
409+
State.Line->First->isNot(TT_AttributeSquare) && Style.isCpp() &&
412410
// FIXME: This is a temporary workaround for the case where clang-format
413411
// sets BreakBeforeParameter to avoid bin packing and this creates a
414412
// completely unnecessary line break after a template type that isn't
@@ -679,8 +677,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
679677
auto &CurrentState = State.Stack.back();
680678

681679
bool DisallowLineBreaksOnThisLine =
682-
Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && IsCpp &&
683-
[&Current] {
680+
Style.LambdaBodyIndentation == FormatStyle::LBI_Signature &&
681+
Style.isCpp() && [&Current] {
684682
// Deal with lambda arguments in C++. The aim here is to ensure that we
685683
// don't over-indent lambda function bodies when lambdas are passed as
686684
// arguments to function calls. We do this by ensuring that either all
@@ -1093,7 +1091,7 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
10931091
// Any break on this level means that the parent level has been broken
10941092
// and we need to avoid bin packing there.
10951093
bool NestedBlockSpecialCase =
1096-
(!IsCpp && Current.is(tok::r_brace) && State.Stack.size() > 1 &&
1094+
(!Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 &&
10971095
State.Stack[State.Stack.size() - 2].NestedBlockInlined) ||
10981096
(Style.Language == FormatStyle::LK_ObjC && Current.is(tok::r_brace) &&
10991097
State.Stack.size() > 1 && !Style.ObjCBreakBeforeNestedBlockParam);

clang/lib/Format/Format.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3841,15 +3841,13 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle &Style,
38413841
}
38423842

38433843
LangOptions getFormattingLangOpts(const FormatStyle &Style) {
3844-
IsCpp = Style.isCpp();
3844+
LangOptions LangOpts;
38453845

38463846
FormatStyle::LanguageStandard LexingStd = Style.Standard;
38473847
if (LexingStd == FormatStyle::LS_Auto)
38483848
LexingStd = FormatStyle::LS_Latest;
38493849
if (LexingStd == FormatStyle::LS_Latest)
38503850
LexingStd = FormatStyle::LS_Cpp20;
3851-
3852-
LangOptions LangOpts;
38533851
LangOpts.CPlusPlus = 1;
38543852
LangOpts.CPlusPlus11 = LexingStd >= FormatStyle::LS_Cpp11;
38553853
LangOpts.CPlusPlus14 = LexingStd >= FormatStyle::LS_Cpp14;
@@ -3860,8 +3858,10 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) {
38603858
// the sequence "<::" will be unconditionally treated as "[:".
38613859
// Cf. Lexer::LexTokenInternal.
38623860
LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
3861+
38633862
LangOpts.LineComment = 1;
3864-
LangOpts.CXXOperatorNames = IsCpp;
3863+
bool AlternativeOperators = Style.isCpp();
3864+
LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
38653865
LangOpts.Bool = 1;
38663866
LangOpts.ObjC = 1;
38673867
LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
@@ -3943,8 +3943,6 @@ const char *DefaultFormatStyle = "file";
39433943

39443944
const char *DefaultFallbackStyle = "LLVM";
39453945

3946-
bool IsCpp = false;
3947-
39483946
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
39493947
loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS,
39503948
FormatStyle *Style, bool AllowUnknownOptions) {

clang/lib/Format/FormatToken.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ static SmallVector<StringRef> CppNonKeywordTypes = {
7878
"uint32_t", "uint64_t", "uint8_t", "uintptr_t",
7979
};
8080

81-
bool FormatToken::isTypeName() const {
81+
bool FormatToken::isTypeName(bool IsCpp) const {
8282
return is(TT_TypeName) || isSimpleTypeSpecifier() ||
8383
(IsCpp && is(tok::identifier) &&
8484
std::binary_search(CppNonKeywordTypes.begin(),
8585
CppNonKeywordTypes.end(), TokenText));
8686
}
8787

88-
bool FormatToken::isTypeOrIdentifier() const {
89-
return isTypeName() || isOneOf(tok::kw_auto, tok::identifier);
88+
bool FormatToken::isTypeOrIdentifier(bool IsCpp) const {
89+
return isTypeName(IsCpp) || isOneOf(tok::kw_auto, tok::identifier);
9090
}
9191

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

clang/lib/Format/FormatToken.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,9 +676,9 @@ struct FormatToken {
676676
/// Determine whether the token is a simple-type-specifier.
677677
[[nodiscard]] bool isSimpleTypeSpecifier() const;
678678

679-
[[nodiscard]] bool isTypeName() const;
679+
[[nodiscard]] bool isTypeName(bool IsCpp) const;
680680

681-
[[nodiscard]] bool isTypeOrIdentifier() const;
681+
[[nodiscard]] bool isTypeOrIdentifier(bool IsCpp) const;
682682

683683
bool isObjCAccessSpecifier() const {
684684
return is(tok::at) && Next &&
@@ -823,7 +823,7 @@ struct FormatToken {
823823

824824
/// Returns whether the token is the left square bracket of a C++
825825
/// structured binding declaration.
826-
bool isCppStructuredBinding() const {
826+
bool isCppStructuredBinding(bool IsCpp) const {
827827
if (!IsCpp || isNot(tok::l_square))
828828
return false;
829829
const FormatToken *T = this;

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ FormatTokenLexer::FormatTokenLexer(
3434
Encoding(Encoding), Allocator(Allocator), FirstInLineIndex(0),
3535
FormattingDisabled(false), MacroBlockBeginRegex(Style.MacroBlockBegin),
3636
MacroBlockEndRegex(Style.MacroBlockEnd) {
37-
assert(IsCpp == Style.isCpp());
3837
Lex.reset(new Lexer(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts));
3938
Lex->SetKeepWhitespaceMode(true);
4039

@@ -115,7 +114,7 @@ void FormatTokenLexer::tryMergePreviousTokens() {
115114
return;
116115
if (tryMergeForEach())
117116
return;
118-
if (IsCpp && tryTransformTryUsageForC())
117+
if (Style.isCpp() && tryTransformTryUsageForC())
119118
return;
120119

121120
if (Style.isJavaScript() || Style.isCSharp()) {
@@ -1342,7 +1341,7 @@ FormatToken *FormatTokenLexer::getNextToken() {
13421341
Column = FormatTok->LastLineColumnWidth;
13431342
}
13441343

1345-
if (IsCpp) {
1344+
if (Style.isCpp()) {
13461345
auto *Identifier = FormatTok->Tok.getIdentifierInfo();
13471346
auto it = Macros.find(Identifier);
13481347
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
@@ -268,20 +268,24 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
268268
if (isPossibleMacro(TypeToken))
269269
return Tok;
270270

271+
const bool IsCpp = Style.isCpp();
272+
271273
// The case `const long long int volatile` -> `long long int const volatile`
272274
// The case `long const long int volatile` -> `long long int const volatile`
273275
// The case `long long volatile int const` -> `long long int const volatile`
274276
// The case `const long long volatile int` -> `long long int const volatile`
275-
if (TypeToken->isTypeName()) {
277+
if (TypeToken->isTypeName(IsCpp)) {
276278
// The case `const decltype(foo)` -> `const decltype(foo)`
277279
// The case `const typeof(foo)` -> `const typeof(foo)`
278280
// The case `const _Atomic(foo)` -> `const _Atomic(foo)`
279281
if (TypeToken->isOneOf(tok::kw_decltype, tok::kw_typeof, tok::kw__Atomic))
280282
return Tok;
281283

282284
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
283-
while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment()))
285+
while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment(),
286+
IsCpp)) {
284287
LastSimpleTypeSpecifier = LastSimpleTypeSpecifier->getNextNonComment();
288+
}
285289

286290
rotateTokens(SourceMgr, Fixes, Tok, LastSimpleTypeSpecifier,
287291
/*Left=*/false);
@@ -291,7 +295,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
291295
// The case `unsigned short const` -> `unsigned short const`
292296
// The case:
293297
// `unsigned short volatile const` -> `unsigned short const volatile`
294-
if (PreviousCheck && PreviousCheck->isTypeName()) {
298+
if (PreviousCheck && PreviousCheck->isTypeName(IsCpp)) {
295299
if (LastQual != Tok)
296300
rotateTokens(SourceMgr, Fixes, Tok, LastQual, /*Left=*/false);
297301
return Tok;
@@ -408,11 +412,11 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
408412
// The case `volatile long long const int` -> `const volatile long long int`
409413
// The case `const long long volatile int` -> `const volatile long long int`
410414
// The case `long volatile long int const` -> `const volatile long long int`
411-
if (TypeToken->isTypeName()) {
415+
if (const bool IsCpp = Style.isCpp(); TypeToken->isTypeName(IsCpp)) {
412416
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
413417
while (isConfiguredQualifierOrType(
414418
LastSimpleTypeSpecifier->getPreviousNonComment(),
415-
ConfiguredQualifierTokens)) {
419+
ConfiguredQualifierTokens, IsCpp)) {
416420
LastSimpleTypeSpecifier =
417421
LastSimpleTypeSpecifier->getPreviousNonComment();
418422
}
@@ -527,9 +531,7 @@ LeftRightQualifierAlignmentFixer::LeftRightQualifierAlignmentFixer(
527531
const std::string &Qualifier,
528532
const std::vector<tok::TokenKind> &QualifierTokens, bool RightAlign)
529533
: TokenAnalyzer(Env, Style), Qualifier(Qualifier), RightAlign(RightAlign),
530-
ConfiguredQualifierTokens(QualifierTokens) {
531-
assert(IsCpp == Style.isCpp());
532-
}
534+
ConfiguredQualifierTokens(QualifierTokens) {}
533535

534536
std::pair<tooling::Replacements, unsigned>
535537
LeftRightQualifierAlignmentFixer::analyze(
@@ -612,15 +614,16 @@ void prepareLeftRightOrderingForQualifierAlignmentFixer(
612614
}
613615
}
614616

615-
bool LeftRightQualifierAlignmentFixer::isQualifierOrType(
616-
const FormatToken *Tok) {
617+
bool LeftRightQualifierAlignmentFixer::isQualifierOrType(const FormatToken *Tok,
618+
bool IsCpp) {
617619
return Tok &&
618-
(Tok->isTypeName() || Tok->is(tok::kw_auto) || isQualifier(Tok));
620+
(Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) || isQualifier(Tok));
619621
}
620622

621623
bool LeftRightQualifierAlignmentFixer::isConfiguredQualifierOrType(
622-
const FormatToken *Tok, const std::vector<tok::TokenKind> &Qualifiers) {
623-
return Tok && (Tok->isTypeName() || Tok->is(tok::kw_auto) ||
624+
const FormatToken *Tok, const std::vector<tok::TokenKind> &Qualifiers,
625+
bool IsCpp) {
626+
return Tok && (Tok->isTypeName(IsCpp) || Tok->is(tok::kw_auto) ||
624627
isConfiguredQualifier(Tok, Qualifiers));
625628
}
626629

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)