Skip to content

Commit

Permalink
Revert "[clang-format][NFC] Eliminate the IsCpp parameter in all func…
Browse files Browse the repository at this point in the history
…tions" (#85353)

Reverts #84599

This broke the presubmit bot.
  • Loading branch information
joker-eph committed Mar 15, 2024
1 parent 65b123e commit b0d1e32
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 91 deletions.
3 changes: 0 additions & 3 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -5228,9 +5228,6 @@ 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:
Expand Down
12 changes: 5 additions & 7 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,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,
Expand Down Expand Up @@ -408,7 +406,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
Expand Down Expand Up @@ -679,8 +677,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
Expand Down Expand Up @@ -1093,7 +1091,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);
Expand Down
10 changes: 4 additions & 6 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3841,15 +3841,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;
Expand All @@ -3860,8 +3858,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.
Expand Down Expand Up @@ -3943,8 +3943,6 @@ 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) {
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Format/FormatToken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,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 {
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Format/FormatToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,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 &&
Expand Down Expand Up @@ -823,7 +823,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;
Expand Down
5 changes: 2 additions & 3 deletions clang/lib/Format/FormatTokenLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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);

Expand Down Expand Up @@ -115,7 +114,7 @@ void FormatTokenLexer::tryMergePreviousTokens() {
return;
if (tryMergeForEach())
return;
if (IsCpp && tryTransformTryUsageForC())
if (Style.isCpp() && tryTransformTryUsageForC())
return;

if (Style.isJavaScript() || Style.isCSharp()) {
Expand Down Expand Up @@ -1342,7 +1341,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() &&
Expand Down
29 changes: 16 additions & 13 deletions clang/lib/Format/QualifierAlignmentFixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,20 +268,24 @@ 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)`
if (TypeToken->isOneOf(tok::kw_decltype, tok::kw_typeof, tok::kw__Atomic))
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);
Expand All @@ -291,7 +295,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;
Expand Down Expand Up @@ -408,11 +412,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();
}
Expand Down Expand Up @@ -527,9 +531,7 @@ LeftRightQualifierAlignmentFixer::LeftRightQualifierAlignmentFixer(
const std::string &Qualifier,
const std::vector<tok::TokenKind> &QualifierTokens, bool RightAlign)
: TokenAnalyzer(Env, Style), Qualifier(Qualifier), RightAlign(RightAlign),
ConfiguredQualifierTokens(QualifierTokens) {
assert(IsCpp == Style.isCpp());
}
ConfiguredQualifierTokens(QualifierTokens) {}

std::pair<tooling::Replacements, unsigned>
LeftRightQualifierAlignmentFixer::analyze(
Expand Down Expand Up @@ -612,15 +614,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));
}

Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Format/QualifierAlignmentFixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit b0d1e32

Please sign in to comment.