[clang-format] Indent tokens after hash only if it starts a line

Fixes #56602.

Differential Revision: https://reviews.llvm.org/D130136
This commit is contained in:
owenca 2022-07-19 18:11:31 -07:00
parent 7dda6c71b1
commit 892a9968ec
2 changed files with 5 additions and 0 deletions

View File

@ -656,6 +656,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
int PPColumnCorrection = 0;
if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash &&
Previous.is(tok::hash) && State.FirstIndent > 0 &&
&Previous == State.Line->First &&
(State.Line->Type == LT_PreprocessorDirective ||
State.Line->Type == LT_ImportStatement)) {
Spaces += State.FirstIndent;

View File

@ -5385,6 +5385,10 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) {
"#endif",
Style);
Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
verifyFormat("#if 1\n"
"# define __STR(x) #x\n"
"#endif",
Style);
verifyFormat("#ifdef _WIN32\n"
"# define A 0\n"
"# ifdef VAR2\n"