Skip to content

Commit 35abd12

Browse files
committed
Updates from review
1 parent 2ee8fab commit 35abd12

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -778,27 +778,23 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
778778

779779
unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
780780

781-
if (Style.IndentPPDirectives == FormatStyle::PPDIS_Leave &&
782-
State.Line->InPPDirective && Previous.is(tok::hash) &&
783-
&Previous == State.Line->First) {
784-
Spaces += Current.OriginalColumn - Previous.OriginalColumn - 1;
785-
}
786-
787781
// Indent preprocessor directives after the hash if required.
788782
int PPColumnCorrection = 0;
789-
if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash &&
790-
Previous.is(tok::hash) && State.FirstIndent > 0 &&
791-
&Previous == State.Line->First &&
783+
if (&Previous == State.Line->First && Previous.is(tok::hash) &&
792784
(State.Line->Type == LT_PreprocessorDirective ||
793785
State.Line->Type == LT_ImportStatement)) {
794-
Spaces += State.FirstIndent;
795-
796-
// For preprocessor indent with tabs, State.Column will be 1 because of the
797-
// hash. This causes second-level indents onward to have an extra space
798-
// after the tabs. We avoid this misalignment by subtracting 1 from the
799-
// column value passed to replaceWhitespace().
800-
if (Style.UseTab != FormatStyle::UT_Never)
801-
PPColumnCorrection = -1;
786+
if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash) {
787+
Spaces += State.FirstIndent;
788+
789+
// For preprocessor indent with tabs, State.Column will be 1 because of
790+
// the hash. This causes second-level indents onward to have an extra
791+
// space after the tabs. We avoid this misalignment by subtracting 1 from
792+
// the column value passed to replaceWhitespace().
793+
if (Style.UseTab != FormatStyle::UT_Never)
794+
PPColumnCorrection = -1;
795+
} else if (Style.IndentPPDirectives == FormatStyle::PPDIS_Leave) {
796+
Spaces += Current.OriginalColumn - Previous.OriginalColumn - 1;
797+
}
802798
}
803799

804800
if (!DryRun) {

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3627,8 +3627,7 @@ void TokenAnnotator::setCommentLineLevels(
36273627
// Align comments for preprocessor lines with the # in column 0 if
36283628
// preprocessor lines are not indented. Otherwise, align with the next
36293629
// line.
3630-
Line->Level = (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash ||
3631-
Style.IndentPPDirectives == FormatStyle::PPDIS_None) &&
3630+
Line->Level = Style.IndentPPDirectives < FormatStyle::PPDIS_BeforeHash &&
36323631
PPDirectiveOrImportStmt
36333632
? 0
36343633
: NextNonCommentLine->Level;

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,8 +1662,7 @@ void UnwrappedLineFormatter::formatFirstToken(
16621662
// Preprocessor directives get indented before the hash only if specified. In
16631663
// Javascript import statements are indented like normal statements.
16641664
if (!Style.isJavaScript() &&
1665-
(Style.IndentPPDirectives == FormatStyle::PPDIS_None ||
1666-
Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash) &&
1665+
Style.IndentPPDirectives < FormatStyle::PPDIS_BeforeHash &&
16671666
(Line.Type == LT_PreprocessorDirective ||
16681667
Line.Type == LT_ImportStatement)) {
16691668
Indent = 0;

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ UnwrappedLineParser::UnwrappedLineParser(
162162
LangOpts(getFormattingLangOpts(Style)), Keywords(Keywords),
163163
CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
164164
Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
165-
IncludeGuard((Style.IndentPPDirectives == FormatStyle::PPDIS_None ||
166-
Style.IndentPPDirectives == FormatStyle::PPDIS_Leave)
165+
IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None ||
166+
Style.IndentPPDirectives == FormatStyle::PPDIS_Leave
167167
? IG_Rejected
168168
: IG_Inited),
169169
IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn),

clang/unittests/Format/FormatTest.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5547,6 +5547,23 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
55475547
"#endif",
55485548
style);
55495549

5550+
style.IndentWidth = 1;
5551+
style.PPIndentWidth = 4;
5552+
verifyFormat("#if 1\n"
5553+
"#define X \\\n"
5554+
" { \\\n"
5555+
" x; \\\n"
5556+
" x; \\\n"
5557+
" }\n"
5558+
"#endif",
5559+
style);
5560+
verifyFormat("#define X \\\n"
5561+
" { \\\n"
5562+
" x; \\\n"
5563+
" x; \\\n"
5564+
" }",
5565+
style);
5566+
55505567
style.IndentPPDirectives = FormatStyle::PPDIS_Leave;
55515568
style.IndentWidth = 4;
55525569
verifyNoChange("#ifndef foo\n"

0 commit comments

Comments
 (0)