diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 3ac9e3795cae7..19aaff389fe17 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -4426,6 +4426,21 @@ the configuration (without a prefix: ``Auto``). #endif #endif + * ``PPDIS_Leave`` (in configuration: ``Leave``) + Leaves indentation of directives as-is. + + .. note:: + + Ignores ``PPIndentWidth``. + + .. code-block:: c++ + + #if FOO + #if BAR + #include + #endif + #endif + .. _IndentRequiresClause: diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 0d85b6f426995..8fde7c5e5d4c8 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -381,6 +381,7 @@ AST Matchers clang-format ------------ - Add ``SpaceInEmptyBraces`` option and set it to ``Always`` for WebKit style. +- Add ``Leave`` suboption to ``IndentPPDirectives``. libclang -------- diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 5dfdb23594610..0bd08ea2248c5 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -2976,7 +2976,19 @@ struct FormatStyle { /// #endif /// #endif /// \endcode - PPDIS_BeforeHash + PPDIS_BeforeHash, + /// Leaves indentation of directives as-is. + /// \note + /// Ignores ``PPIndentWidth``. + /// \endnote + /// \code + /// #if FOO + /// #if BAR + /// #include + /// #endif + /// #endif + /// \endcode + PPDIS_Leave }; /// The preprocessor directive indenting style to use. diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 888d0faf80931..a02c1fef19cea 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -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) && + &Previous == State.Line->First) { + Spaces += Current.OriginalColumn - Previous.OriginalColumn - 1; + } + // Indent preprocessor directives after the hash if required. int PPColumnCorrection = 0; if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index e3b22cdabaccd..998727cb70d2d 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -515,6 +515,7 @@ struct ScalarEnumerationTraits { IO.enumCase(Value, "None", FormatStyle::PPDIS_None); IO.enumCase(Value, "AfterHash", FormatStyle::PPDIS_AfterHash); IO.enumCase(Value, "BeforeHash", FormatStyle::PPDIS_BeforeHash); + IO.enumCase(Value, "Leave", FormatStyle::PPDIS_Leave); } }; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index a220de54f46bf..d23861a889a41 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3616,7 +3616,8 @@ void TokenAnnotator::setCommentLineLevels( // Align comments for preprocessor lines with the # in column 0 if // preprocessor lines are not indented. Otherwise, align with the next // line. - Line->Level = Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash && + Line->Level = (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash || + Style.IndentPPDirectives == FormatStyle::PPDIS_None) && PPDirectiveOrImportStmt ? 0 : NextNonCommentLine->Level; diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index c938ff3965f9e..49b31661accf5 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -62,10 +62,16 @@ class LevelIndentTracker { // having the right size in adjustToUnmodifiedline. if (Line.Level >= IndentForLevel.size()) IndentForLevel.resize(Line.Level + 1, -1); - if (Style.IndentPPDirectives != FormatStyle::PPDIS_None && - (Line.InPPDirective || - (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash && - Line.Type == LT_CommentAbovePPDirective))) { + if (Style.IndentPPDirectives == FormatStyle::PPDIS_Leave && + Line.InPPDirective) { + Indent = Line.InMacroBody + ? (Line.Level - Line.PPLevel) * Style.IndentWidth + + AdditionalIndent + : Line.First->OriginalColumn; + } else if (Style.IndentPPDirectives != FormatStyle::PPDIS_None && + (Line.InPPDirective || + (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash && + Line.Type == LT_CommentAbovePPDirective))) { unsigned PPIndentWidth = (Style.PPIndentWidth >= 0) ? Style.PPIndentWidth : Style.IndentWidth; Indent = Line.InMacroBody @@ -1653,7 +1659,8 @@ void UnwrappedLineFormatter::formatFirstToken( // Preprocessor directives get indented before the hash only if specified. In // Javascript import statements are indented like normal statements. if (!Style.isJavaScript() && - Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash && + (Style.IndentPPDirectives == FormatStyle::PPDIS_None || + Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash) && (Line.Type == LT_PreprocessorDirective || Line.Type == LT_ImportStatement)) { Indent = 0; diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 91b8fdc8a3c38..306123882a834 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -162,7 +162,8 @@ UnwrappedLineParser::UnwrappedLineParser( LangOpts(getFormattingLangOpts(Style)), Keywords(Keywords), CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr), Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1), - IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None + IncludeGuard((Style.IndentPPDirectives == FormatStyle::PPDIS_None || + Style.IndentPPDirectives == FormatStyle::PPDIS_Leave) ? IG_Rejected : IG_Inited), IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn), @@ -170,7 +171,8 @@ UnwrappedLineParser::UnwrappedLineParser( void UnwrappedLineParser::reset() { PPBranchLevel = -1; - IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None + IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None || + Style.IndentPPDirectives == FormatStyle::PPDIS_Leave ? IG_Rejected : IG_Inited; IncludeGuardToken = nullptr; @@ -1140,7 +1142,8 @@ void UnwrappedLineParser::parsePPEndIf() { // If the #endif of a potential include guard is the last thing in the file, // then we found an include guard. if (IncludeGuard == IG_Defined && PPBranchLevel == -1 && Tokens->isEOF() && - Style.IndentPPDirectives != FormatStyle::PPDIS_None) { + Style.IndentPPDirectives != FormatStyle::PPDIS_None && + Style.IndentPPDirectives != FormatStyle::PPDIS_Leave) { IncludeGuard = IG_Found; } } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 83c664c3b81f3..0f8dc539a185e 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -5547,22 +5547,55 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { "#endif", style); + style.IndentPPDirectives = FormatStyle::PPDIS_Leave; + style.IndentWidth = 4; + verifyNoChange("#ifndef foo\n" + "#define foo\n" + "if (emacs) {\n" + "#ifdef is\n" + "#define lit \\\n" + " if (af) { \\\n" + " return duh(); \\\n" + " }\n" + "#endif\n" + "}\n" + "#endif", + style); + verifyNoChange("#ifndef foo\n" + " #define foo\n" + "if (emacs) {\n" + " #ifdef is\n" + "#define lit \\\n" + " if (af) { \\\n" + " return duh(); \\\n" + " }\n" + " #endif\n" + "}\n" + "#endif", + style); + verifyNoChange(" #ifndef foo\n" + "# define foo\n" + "if (emacs) {\n" + "#ifdef is\n" + " # define lit \\\n" + " if (af) { \\\n" + " return duh(); \\\n" + " }\n" + "#endif\n" + "}\n" + " #endif", + style); + style.IndentWidth = 1; style.PPIndentWidth = 4; - verifyFormat("#if 1\n" - "#define X \\\n" - " { \\\n" - " x; \\\n" - " x; \\\n" - " }\n" - "#endif", - style); - verifyFormat("#define X \\\n" - " { \\\n" - " x; \\\n" - " x; \\\n" - " }", - style); + verifyNoChange("# if 1\n" + " #define X \\\n" + " { \\\n" + " x; \\\n" + " x; \\\n" + " }\n" + "# endif", + style); style.IndentWidth = 4; style.PPIndentWidth = 1; @@ -25947,6 +25980,20 @@ TEST_F(FormatTest, SkipMacroDefinitionBody) { "a", Style); + Style.IndentPPDirectives = FormatStyle::PPDIS_Leave; + verifyNoChange("#if A\n" + "#define A a\n" + "#endif", + Style); + verifyNoChange("#if A\n" + " #define A a\n" + "#endif", + Style); + verifyNoChange("#if A\n" + "# define A a\n" + "#endif", + Style); + // Adjust indendations but don't change the definition. Style.IndentPPDirectives = FormatStyle::PPDIS_None; verifyNoChange("#if A\n"