[PATCH] D153243: [clang-format] Don't finalize #if, #else, #endif, etc.
Owen Pan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 18 23:32:20 PDT 2023
owenpan created this revision.
owenpan added a reviewer: Sedeniono.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, MyDeveloperDay.
owenpan requested review of this revision.
Don't finalize a preprocessor branch directive if it's the first token of an annotated line.
Fixes https://github.com/llvm/llvm-project/issues/63379.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D153243
Files:
clang/lib/Format/UnwrappedLineFormatter.cpp
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -23106,6 +23106,13 @@
"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) {
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -49,16 +49,8 @@
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, "", "",
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1411,8 +1411,16 @@
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;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153243.532539.patch
Type: text/x-patch
Size: 2660 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230619/22f2526f/attachment.bin>
More information about the cfe-commits
mailing list