[clang] 8e85739 - [clang-format] Don't finalize #if, #else, #endif, etc.
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 21 15:16:04 PDT 2023
Author: Owen Pan
Date: 2023-06-21T15:15:57-07:00
New Revision: 8e85739a5f24e34cb22768fe6ef33b593ed054df
URL: https://github.com/llvm/llvm-project/commit/8e85739a5f24e34cb22768fe6ef33b593ed054df
DIFF: https://github.com/llvm/llvm-project/commit/8e85739a5f24e34cb22768fe6ef33b593ed054df.diff
LOG: [clang-format] Don't finalize #if, #else, #endif, etc.
Don't finalize a preprocessor branch directive if it's the first
token of an annotated line. See the rationale at
https://reviews.llvm.org/D150057#inline-1449546.
Fixes #63379
Differential Revision: https://reviews.llvm.org/D153243
Added:
Modified:
clang/lib/Format/UnwrappedLineFormatter.cpp
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 6d9a43dea0568..3eacd0201df32 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1411,8 +1411,16 @@ unsigned UnwrappedLineFormatter::format(
NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
RangeMinLevel = UINT_MAX;
}
- if (!DryRun)
- markFinalized(TheLine.First);
+ if (!DryRun) {
+ auto *Tok = TheLine.First;
+ if (Tok->is(tok::hash) && !Tok->Previous && Tok->Next &&
+ Tok->Next->isOneOf(tok::pp_if, tok::pp_ifdef, tok::pp_ifndef,
+ tok::pp_elif, tok::pp_elifdef, tok::pp_elifndef,
+ tok::pp_else, tok::pp_endif)) {
+ Tok = Tok->Next;
+ }
+ markFinalized(Tok);
+ }
}
PenaltyCache[CacheKey] = Penalty;
return Penalty;
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 040b9536b6305..24255522263ad 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -49,16 +49,8 @@ void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
unsigned Spaces,
unsigned StartOfTokenColumn,
bool IsAligned, bool InPPDirective) {
- auto PPBranchDirectiveStartsLine = [&Tok] {
- return Tok.is(tok::hash) && !Tok.Previous && Tok.Next &&
- Tok.Next->isOneOf(tok::pp_if, tok::pp_ifdef, tok::pp_ifndef,
- tok::pp_elif, tok::pp_elifdef, tok::pp_elifndef,
- tok::pp_else, tok::pp_endif);
- };
- if ((Tok.Finalized && !PPBranchDirectiveStartsLine()) ||
- (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg)) {
+ if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
return;
- }
Tok.setDecision((Newlines > 0) ? FD_Break : FD_Continue);
Changes.push_back(Change(Tok, /*CreateReplacement=*/true, Tok.WhitespaceRange,
Spaces, StartOfTokenColumn, Newlines, "", "",
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 4c58df893aa8f..6b0540400d7b7 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -23106,6 +23106,13 @@ TEST_F(FormatTest, DisableRegions) {
"int* j;\n"
"// clang-format only\n"
"int* k;");
+
+ verifyNoChange("// clang-format off\n"
+ "#if 0\n"
+ " #if SHOULD_STAY_INDENTED\n"
+ " #endif\n"
+ "#endif\n"
+ "// clang-format on");
}
TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
More information about the cfe-commits
mailing list