[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
d0k wrote: It calls `clang::format::cleanupAroundReplacements` from multiple threads. Now there's a race condition on IsCpp. If this was supposed to be a cleanup please revert it. https://github.com/llvm/llvm-project/pull/84599 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
owenca wrote: Can you provide more info about the failed use cases? https://github.com/llvm/llvm-project/pull/84599 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
d0k wrote: This is now breaking multithreaded IDE use cases, can you explain why this change is necessary? https://github.com/llvm/llvm-project/pull/84599 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/84599 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
HazardyKnusperkeks wrote: > > I mean multiple threads in the same process with different languages. maybe > > unlikely, but not impossible. > > But libFormat/clang-format are _not_ multithreaded, right? clang-format is not, but libFormat I don't know. I know my IDE uses directly libFormat. I will not block this, but I will also not approve it. https://github.com/llvm/llvm-project/pull/84599 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/84599 >From 43238d58ff490073c13ff621faddceb89b05b22e Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Fri, 8 Mar 2024 19:47:54 -0800 Subject: [PATCH 1/2] [clang-format][NFC] Eliminate the IsCpp parameter in all functions --- clang/include/clang/Format/Format.h | 3 ++ clang/lib/Format/ContinuationIndenter.cpp| 12 +++-- clang/lib/Format/Format.cpp | 2 + clang/lib/Format/FormatToken.cpp | 6 +-- clang/lib/Format/FormatToken.h | 6 +-- clang/lib/Format/FormatTokenLexer.cpp| 5 +- clang/lib/Format/QualifierAlignmentFixer.cpp | 29 ++-- clang/lib/Format/QualifierAlignmentFixer.h | 5 +- clang/lib/Format/TokenAnnotator.cpp | 48 ++-- clang/lib/Format/TokenAnnotator.h| 6 +-- clang/lib/Format/UnwrappedLineParser.cpp | 22 + clang/lib/Format/UnwrappedLineParser.h | 1 - 12 files changed, 74 insertions(+), 71 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 590297fd89a398..a72c1b171c3e18 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -5228,6 +5228,9 @@ 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: diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index df44e6994c4784..506e21725ba9f7 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -241,7 +241,9 @@ ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style, : Style(Style), Keywords(Keywords), SourceMgr(SourceMgr), Whitespaces(Whitespaces), Encoding(Encoding), BinPackInconclusiveFunctions(BinPackInconclusiveFunctions), - CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {} + CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) { + IsCpp = Style.isCpp(); +} LineState ContinuationIndenter::getInitialState(unsigned FirstIndent, unsigned FirstStartColumn, @@ -406,7 +408,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) && Style.isCpp() && +State.Line->First->isNot(TT_AttributeSquare) && 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 @@ -677,8 +679,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, auto &CurrentState = State.Stack.back(); bool DisallowLineBreaksOnThisLine = - Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && - Style.isCpp() && [&Current] { + Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && 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 @@ -1091,7 +1093,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 = - (!Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 && + (!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 e64ba7eebc1ce8..00182f75560a2c 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -3943,6 +3943,8 @@ const char *DefaultFormatStyle = "file"; const char *DefaultFallbackStyle = "LLVM"; +bool IsCpp = false; + llvm::ErrorOr> loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS, FormatStyle *Style, bool AllowUnknownOptions) { diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index 4fb70ffac706d0..665b2e43259b21 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
owenca wrote: > I mean multiple threads in the same process with different languages. maybe > unlikely, but not impossible. But libFormat/clang-format are _not_ multithreaded, right? https://github.com/llvm/llvm-project/pull/84599 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
HazardyKnusperkeks wrote: > > What happens if you use libFormat concurrently? > > Can you elaborate? Doesn't each process get its own copy of the globals of a > shared library? I mean multiple threads in the same process with different languages. maybe unlikely, but not impossible. https://github.com/llvm/llvm-project/pull/84599 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
https://github.com/mydeveloperday approved this pull request. This looks good to me https://github.com/llvm/llvm-project/pull/84599 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
owenca wrote: > Then I'd go back to `Style.isCpp()`. That would still require passing `Style.isCpp()` as the parameter to the `FormatToken` (and other) functions. > What happens if you use libFormat concurrently? Can you elaborate? Doesn't each process get its own copy of the globals of a shared library? https://github.com/llvm/llvm-project/pull/84599 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
HazardyKnusperkeks wrote: Then I'd go back to `Style.isCpp()`. What happens if you use libFormat concurrently? https://github.com/llvm/llvm-project/pull/84599 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/84599 >From 43238d58ff490073c13ff621faddceb89b05b22e Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Fri, 8 Mar 2024 19:47:54 -0800 Subject: [PATCH 1/2] [clang-format][NFC] Eliminate the IsCpp parameter in all functions --- clang/include/clang/Format/Format.h | 3 ++ clang/lib/Format/ContinuationIndenter.cpp| 12 +++-- clang/lib/Format/Format.cpp | 2 + clang/lib/Format/FormatToken.cpp | 6 +-- clang/lib/Format/FormatToken.h | 6 +-- clang/lib/Format/FormatTokenLexer.cpp| 5 +- clang/lib/Format/QualifierAlignmentFixer.cpp | 29 ++-- clang/lib/Format/QualifierAlignmentFixer.h | 5 +- clang/lib/Format/TokenAnnotator.cpp | 48 ++-- clang/lib/Format/TokenAnnotator.h| 6 +-- clang/lib/Format/UnwrappedLineParser.cpp | 22 + clang/lib/Format/UnwrappedLineParser.h | 1 - 12 files changed, 74 insertions(+), 71 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 590297fd89a398..a72c1b171c3e18 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -5228,6 +5228,9 @@ 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: diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index df44e6994c4784..506e21725ba9f7 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -241,7 +241,9 @@ ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style, : Style(Style), Keywords(Keywords), SourceMgr(SourceMgr), Whitespaces(Whitespaces), Encoding(Encoding), BinPackInconclusiveFunctions(BinPackInconclusiveFunctions), - CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {} + CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) { + IsCpp = Style.isCpp(); +} LineState ContinuationIndenter::getInitialState(unsigned FirstIndent, unsigned FirstStartColumn, @@ -406,7 +408,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) && Style.isCpp() && +State.Line->First->isNot(TT_AttributeSquare) && 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 @@ -677,8 +679,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, auto &CurrentState = State.Stack.back(); bool DisallowLineBreaksOnThisLine = - Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && - Style.isCpp() && [&Current] { + Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && 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 @@ -1091,7 +1093,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 = - (!Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 && + (!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 e64ba7eebc1ce8..00182f75560a2c 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -3943,6 +3943,8 @@ const char *DefaultFormatStyle = "file"; const char *DefaultFallbackStyle = "LLVM"; +bool IsCpp = false; + llvm::ErrorOr> loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS, FormatStyle *Style, bool AllowUnknownOptions) { diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index 4fb70ffac706d0..665b2e43259b21 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
owenca wrote: > That's okay for clang-format, but you couldn't use libFormat with different > languages at once anymore. I believe we still could because `IsCpp` is set to `Style.isCpp()` every time new instances of the classes are constructed. I'll add a couple of test cases to demonstrate this. > I dislike global state in libraries. So do I, but I prefer this to keeping the `IsCpp` parameter. See also https://github.com/llvm/llvm-project/pull/83709#discussion_r1512657115. https://github.com/llvm/llvm-project/pull/84599 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
HazardyKnusperkeks wrote: That's okay for clang-format, but you couldn't use libFormat with different languages at once anymore. I dislike global state in libraries. https://github.com/llvm/llvm-project/pull/84599 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/84599 >From 43238d58ff490073c13ff621faddceb89b05b22e Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Fri, 8 Mar 2024 19:47:54 -0800 Subject: [PATCH] [clang-format][NFC] Eliminate the IsCpp parameter in all functions --- clang/include/clang/Format/Format.h | 3 ++ clang/lib/Format/ContinuationIndenter.cpp| 12 +++-- clang/lib/Format/Format.cpp | 2 + clang/lib/Format/FormatToken.cpp | 6 +-- clang/lib/Format/FormatToken.h | 6 +-- clang/lib/Format/FormatTokenLexer.cpp| 5 +- clang/lib/Format/QualifierAlignmentFixer.cpp | 29 ++-- clang/lib/Format/QualifierAlignmentFixer.h | 5 +- clang/lib/Format/TokenAnnotator.cpp | 48 ++-- clang/lib/Format/TokenAnnotator.h| 6 +-- clang/lib/Format/UnwrappedLineParser.cpp | 22 + clang/lib/Format/UnwrappedLineParser.h | 1 - 12 files changed, 74 insertions(+), 71 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 590297fd89a398..a72c1b171c3e18 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -5228,6 +5228,9 @@ 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: diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index df44e6994c4784..506e21725ba9f7 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -241,7 +241,9 @@ ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style, : Style(Style), Keywords(Keywords), SourceMgr(SourceMgr), Whitespaces(Whitespaces), Encoding(Encoding), BinPackInconclusiveFunctions(BinPackInconclusiveFunctions), - CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {} + CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) { + IsCpp = Style.isCpp(); +} LineState ContinuationIndenter::getInitialState(unsigned FirstIndent, unsigned FirstStartColumn, @@ -406,7 +408,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) && Style.isCpp() && +State.Line->First->isNot(TT_AttributeSquare) && 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 @@ -677,8 +679,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, auto &CurrentState = State.Stack.back(); bool DisallowLineBreaksOnThisLine = - Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && - Style.isCpp() && [&Current] { + Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && 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 @@ -1091,7 +1093,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 = - (!Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 && + (!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 e64ba7eebc1ce8..00182f75560a2c 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -3943,6 +3943,8 @@ const char *DefaultFormatStyle = "file"; const char *DefaultFallbackStyle = "LLVM"; +bool IsCpp = false; + llvm::ErrorOr> loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS, FormatStyle *Style, bool AllowUnknownOptions) { diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index 4fb70ffac706d0..665b2e43259b21 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
llvmbot wrote: @llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) Changes --- Patch is 25.61 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/84599.diff 12 Files Affected: - (modified) clang/include/clang/Format/Format.h (+2) - (modified) clang/lib/Format/ContinuationIndenter.cpp (+7-5) - (modified) clang/lib/Format/Format.cpp (+2) - (modified) clang/lib/Format/FormatToken.cpp (+3-3) - (modified) clang/lib/Format/FormatToken.h (+3-3) - (modified) clang/lib/Format/FormatTokenLexer.cpp (+3-2) - (modified) clang/lib/Format/QualifierAlignmentFixer.cpp (+13-16) - (modified) clang/lib/Format/QualifierAlignmentFixer.h (+2-3) - (modified) clang/lib/Format/TokenAnnotator.cpp (+23-25) - (modified) clang/lib/Format/TokenAnnotator.h (+3-3) - (modified) clang/lib/Format/UnwrappedLineParser.cpp (+12-10) - (modified) clang/lib/Format/UnwrappedLineParser.h (-1) ``diff diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 590297fd89a398..ff96e2bdbbc1d8 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -5228,6 +5228,8 @@ extern const char *DefaultFormatStyle; /// Different builds can modify the value to the preferred styles. extern const char *DefaultFallbackStyle; +extern bool IsCpp; + /// Construct a FormatStyle based on ``StyleName``. /// /// ``StyleName`` can take several forms: diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index df44e6994c4784..506e21725ba9f7 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -241,7 +241,9 @@ ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style, : Style(Style), Keywords(Keywords), SourceMgr(SourceMgr), Whitespaces(Whitespaces), Encoding(Encoding), BinPackInconclusiveFunctions(BinPackInconclusiveFunctions), - CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {} + CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) { + IsCpp = Style.isCpp(); +} LineState ContinuationIndenter::getInitialState(unsigned FirstIndent, unsigned FirstStartColumn, @@ -406,7 +408,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) && Style.isCpp() && +State.Line->First->isNot(TT_AttributeSquare) && 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 @@ -677,8 +679,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, auto &CurrentState = State.Stack.back(); bool DisallowLineBreaksOnThisLine = - Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && - Style.isCpp() && [&Current] { + Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && 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 @@ -1091,7 +1093,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 = - (!Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 && + (!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 e64ba7eebc1ce8..00182f75560a2c 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -3943,6 +3943,8 @@ const char *DefaultFormatStyle = "file"; const char *DefaultFallbackStyle = "LLVM"; +bool IsCpp = false; + llvm::ErrorOr> loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS, FormatStyle *Style, bool AllowUnknownOptions) { diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index 4fb70ffac706d0..665b2e43259b21 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -78,15 +78,15 @@ static SmallVector CppNonKeywordTypes = { "uint32_t", "uint64_t", "uint8_t", "uintptr_t", }; -bool FormatToken::isTypeNa
[clang] [clang-format][NFC] Eliminate the IsCpp parameter in all functions (PR #84599)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/84599 None >From bd3e866189ad4c238ea0afbbc30fbe88e0406eb9 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Fri, 8 Mar 2024 19:47:54 -0800 Subject: [PATCH] [clang-format][NFC] Eliminate the IsCpp parameter in all functions --- clang/include/clang/Format/Format.h | 2 + clang/lib/Format/ContinuationIndenter.cpp| 12 +++-- clang/lib/Format/Format.cpp | 2 + clang/lib/Format/FormatToken.cpp | 6 +-- clang/lib/Format/FormatToken.h | 6 +-- clang/lib/Format/FormatTokenLexer.cpp| 5 +- clang/lib/Format/QualifierAlignmentFixer.cpp | 29 ++-- clang/lib/Format/QualifierAlignmentFixer.h | 5 +- clang/lib/Format/TokenAnnotator.cpp | 48 ++-- clang/lib/Format/TokenAnnotator.h| 6 +-- clang/lib/Format/UnwrappedLineParser.cpp | 22 + clang/lib/Format/UnwrappedLineParser.h | 1 - 12 files changed, 73 insertions(+), 71 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 590297fd89a398..ff96e2bdbbc1d8 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -5228,6 +5228,8 @@ extern const char *DefaultFormatStyle; /// Different builds can modify the value to the preferred styles. extern const char *DefaultFallbackStyle; +extern bool IsCpp; + /// Construct a FormatStyle based on ``StyleName``. /// /// ``StyleName`` can take several forms: diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index df44e6994c4784..506e21725ba9f7 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -241,7 +241,9 @@ ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style, : Style(Style), Keywords(Keywords), SourceMgr(SourceMgr), Whitespaces(Whitespaces), Encoding(Encoding), BinPackInconclusiveFunctions(BinPackInconclusiveFunctions), - CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {} + CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) { + IsCpp = Style.isCpp(); +} LineState ContinuationIndenter::getInitialState(unsigned FirstIndent, unsigned FirstStartColumn, @@ -406,7 +408,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) && Style.isCpp() && +State.Line->First->isNot(TT_AttributeSquare) && 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 @@ -677,8 +679,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, auto &CurrentState = State.Stack.back(); bool DisallowLineBreaksOnThisLine = - Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && - Style.isCpp() && [&Current] { + Style.LambdaBodyIndentation == FormatStyle::LBI_Signature && 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 @@ -1091,7 +1093,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 = - (!Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 && + (!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 e64ba7eebc1ce8..00182f75560a2c 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -3943,6 +3943,8 @@ const char *DefaultFormatStyle = "file"; const char *DefaultFallbackStyle = "LLVM"; +bool IsCpp = false; + llvm::ErrorOr> loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS, FormatStyle *Style, bool AllowUnknownOptions) { diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index 4fb70ffac706d0..665b2e43259b21 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -78,15 +78,15 @@ static SmallVector CppNonKeywordTypes