[clang] [clang-format] Add IndentPPDirectives Leave option (PR #139750)

via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 12 00:57:16 PDT 2025


================
@@ -778,6 +778,12 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
 
   unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
 
+  if (Style.IndentPPDirectives == FormatStyle::PPDIS_Leave &&
+      State.Line->InPPDirective && Previous.is(tok::hash) &&
----------------
owenca wrote:

```suggestion
      (State.Line->Type == LT_PreprocessorDirective ||
       State.Line->Type == LT_ImportStatement) && Previous.is(tok::hash) &&
```
because `InPPDirective` doesn't include `LT_ImportStatement`?

Actually, you can combine this and `PPDIS_AfterHash` below into the following (after some simplifications):
```c++
  if (&Previous == State.Line->First && Previous.is(tok::hash) &&
      (State.Line->Type == LT_PreprocessorDirective ||
       State.Line->Type == LT_ImportStatement)) {
    if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash) {
      Spaces += State.FirstIndent;
      // For preprocessor indent with tabs, State.Column will be 1 because of
      // the hash. This causes second-level indents onward to have an extra
      // space after the tabs. We avoid this misalignment by subtracting 1 from
      // the column value passed to replaceWhitespace().
      if (Style.UseTab != FormatStyle::UT_Never)
        PPColumnCorrection = -1;
    } else if (Style.IndentPPDirectives == FormatStyle::PPDIS_Leave) {
      Spaces += Current.OriginalColumn - Previous.OriginalColumn - 1;
    }
  }
```

https://github.com/llvm/llvm-project/pull/139750


More information about the cfe-commits mailing list