Skip to content

Commit c3a1eb6

Browse files
committed
Reland [clang-format][NFC] Eliminate the IsCpp parameter in all functions (#84599)
Initialize IsCpp in LeftRightQualifierAlignmentFixer ctor.
1 parent b0d1e32 commit c3a1eb6

13 files changed

+91
-76
lines changed

clang/include/clang/Format/Format.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5228,6 +5228,9 @@ 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+
52315234
/// Construct a FormatStyle based on ``StyleName``.
52325235
///
52335236
/// ``StyleName`` can take several forms:

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ 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) {}
244+
CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {
245+
assert(IsCpp == Style.isCpp());
246+
}
245247

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

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

clang/lib/Format/Format.cpp

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

38433843
LangOptions getFormattingLangOpts(const FormatStyle &Style) {
3844-
LangOptions LangOpts;
3844+
IsCpp = Style.isCpp();
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;
38513853
LangOpts.CPlusPlus = 1;
38523854
LangOpts.CPlusPlus11 = LexingStd >= FormatStyle::LS_Cpp11;
38533855
LangOpts.CPlusPlus14 = LexingStd >= FormatStyle::LS_Cpp14;
@@ -3858,10 +3860,8 @@ LangOptions getFormattingLangOpts(const FormatStyle &Style) {
38583860
// the sequence "<::" will be unconditionally treated as "[:".
38593861
// Cf. Lexer::LexTokenInternal.
38603862
LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
3861-
38623863
LangOpts.LineComment = 1;
3863-
bool AlternativeOperators = Style.isCpp();
3864-
LangOpts.CXXOperatorNames = AlternativeOperators ? 1 : 0;
3864+
LangOpts.CXXOperatorNames = IsCpp;
38653865
LangOpts.Bool = 1;
38663866
LangOpts.ObjC = 1;
38673867
LangOpts.MicrosoftExt = 1; // To get kw___try, kw___finally.
@@ -3943,6 +3943,8 @@ const char *DefaultFormatStyle = "file";
39433943

39443944
const char *DefaultFallbackStyle = "LLVM";
39453945

3946+
bool IsCpp = false;
3947+
39463948
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
39473949
loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS,
39483950
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(bool IsCpp) const {
81+
bool FormatToken::isTypeName() 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(bool IsCpp) const {
89-
return isTypeName(IsCpp) || isOneOf(tok::kw_auto, tok::identifier);
88+
bool FormatToken::isTypeOrIdentifier() const {
89+
return isTypeName() || 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(bool IsCpp) const;
679+
[[nodiscard]] bool isTypeName() const;
680680

681-
[[nodiscard]] bool isTypeOrIdentifier(bool IsCpp) const;
681+
[[nodiscard]] bool isTypeOrIdentifier() 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(bool IsCpp) const {
826+
bool isCppStructuredBinding() const {
827827
if (!IsCpp || isNot(tok::l_square))
828828
return false;
829829
const FormatToken *T = this;

clang/lib/Format/FormatTokenLexer.cpp

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

@@ -114,7 +115,7 @@ void FormatTokenLexer::tryMergePreviousTokens() {
114115
return;
115116
if (tryMergeForEach())
116117
return;
117-
if (Style.isCpp() && tryTransformTryUsageForC())
118+
if (IsCpp && tryTransformTryUsageForC())
118119
return;
119120

120121
if (Style.isJavaScript() || Style.isCSharp()) {
@@ -1341,7 +1342,7 @@ FormatToken *FormatTokenLexer::getNextToken() {
13411342
Column = FormatTok->LastLineColumnWidth;
13421343
}
13431344

1344-
if (Style.isCpp()) {
1345+
if (IsCpp) {
13451346
auto *Identifier = FormatTok->Tok.getIdentifierInfo();
13461347
auto it = Macros.find(Identifier);
13471348
if (!(Tokens.size() > 0 && Tokens.back()->Tok.getIdentifierInfo() &&

clang/lib/Format/QualifierAlignmentFixer.cpp

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

271-
const bool IsCpp = Style.isCpp();
272-
273271
// The case `const long long int volatile` -> `long long int const volatile`
274272
// The case `long const long int volatile` -> `long long int const volatile`
275273
// The case `long long volatile int const` -> `long long int const volatile`
276274
// The case `const long long volatile int` -> `long long int const volatile`
277-
if (TypeToken->isTypeName(IsCpp)) {
275+
if (TypeToken->isTypeName()) {
278276
// The case `const decltype(foo)` -> `const decltype(foo)`
279277
// The case `const typeof(foo)` -> `const typeof(foo)`
280278
// The case `const _Atomic(foo)` -> `const _Atomic(foo)`
281279
if (TypeToken->isOneOf(tok::kw_decltype, tok::kw_typeof, tok::kw__Atomic))
282280
return Tok;
283281

284282
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
285-
while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment(),
286-
IsCpp)) {
283+
while (isQualifierOrType(LastSimpleTypeSpecifier->getNextNonComment()))
287284
LastSimpleTypeSpecifier = LastSimpleTypeSpecifier->getNextNonComment();
288-
}
289285

290286
rotateTokens(SourceMgr, Fixes, Tok, LastSimpleTypeSpecifier,
291287
/*Left=*/false);
@@ -295,7 +291,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
295291
// The case `unsigned short const` -> `unsigned short const`
296292
// The case:
297293
// `unsigned short volatile const` -> `unsigned short const volatile`
298-
if (PreviousCheck && PreviousCheck->isTypeName(IsCpp)) {
294+
if (PreviousCheck && PreviousCheck->isTypeName()) {
299295
if (LastQual != Tok)
300296
rotateTokens(SourceMgr, Fixes, Tok, LastQual, /*Left=*/false);
301297
return Tok;
@@ -412,11 +408,11 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
412408
// The case `volatile long long const int` -> `const volatile long long int`
413409
// The case `const long long volatile int` -> `const volatile long long int`
414410
// The case `long volatile long int const` -> `const volatile long long int`
415-
if (const bool IsCpp = Style.isCpp(); TypeToken->isTypeName(IsCpp)) {
411+
if (TypeToken->isTypeName()) {
416412
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
417413
while (isConfiguredQualifierOrType(
418414
LastSimpleTypeSpecifier->getPreviousNonComment(),
419-
ConfiguredQualifierTokens, IsCpp)) {
415+
ConfiguredQualifierTokens)) {
420416
LastSimpleTypeSpecifier =
421417
LastSimpleTypeSpecifier->getPreviousNonComment();
422418
}
@@ -531,7 +527,9 @@ LeftRightQualifierAlignmentFixer::LeftRightQualifierAlignmentFixer(
531527
const std::string &Qualifier,
532528
const std::vector<tok::TokenKind> &QualifierTokens, bool RightAlign)
533529
: TokenAnalyzer(Env, Style), Qualifier(Qualifier), RightAlign(RightAlign),
534-
ConfiguredQualifierTokens(QualifierTokens) {}
530+
ConfiguredQualifierTokens(QualifierTokens) {
531+
IsCpp = Style.isCpp();
532+
}
535533

536534
std::pair<tooling::Replacements, unsigned>
537535
LeftRightQualifierAlignmentFixer::analyze(
@@ -614,16 +612,15 @@ void prepareLeftRightOrderingForQualifierAlignmentFixer(
614612
}
615613
}
616614

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

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

clang/lib/Format/QualifierAlignmentFixer.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,10 @@ 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, bool IsCpp = true);
74+
static bool isQualifierOrType(const FormatToken *Tok);
7575
static bool
7676
isConfiguredQualifierOrType(const FormatToken *Tok,
77-
const std::vector<tok::TokenKind> &Qualifiers,
78-
bool IsCpp = true);
77+
const std::vector<tok::TokenKind> &Qualifiers);
7978

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

0 commit comments

Comments
 (0)