gergap created this revision. gergap added reviewers: klimek, Richard. Herald added a subscriber: krytarowski. gergap requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
This allows to set a different indent width for preprocessor statements. Example: #ifdef __linux_ # define FOO #endif int main(void) { return 0; } Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D103286 Files: clang/docs/ClangFormatStyleOptions.rst clang/include/clang/Format/Format.h clang/lib/Format/Format.cpp clang/lib/Format/UnwrappedLineFormatter.cpp clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -3432,6 +3432,18 @@ } TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { + FormatStyle style = getLLVMStyleWithColumns(4); + style.PPIndentWidth = 1; + + verifyFormat("#ifdef __linux__\n" + "# define FOO\n" + "#endif" + "void foo()\n" + "{\n" + " int x = 0;\n" + "}\n", + style); + verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -57,7 +57,7 @@ while (IndentForLevel.size() <= Line.Level) IndentForLevel.push_back(-1); if (Line.InPPDirective) { - Indent = Line.Level * Style.IndentWidth + AdditionalIndent; + Indent = Line.Level * Style.PPIndentWidth + AdditionalIndent; } else { IndentForLevel.resize(Line.Level + 1); Indent = getIndent(IndentForLevel, Line.Level); Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -664,6 +664,7 @@ IO.mapOptional("PenaltyIndentedWhitespace", Style.PenaltyIndentedWhitespace); IO.mapOptional("PointerAlignment", Style.PointerAlignment); + IO.mapOptional("PPIndentWidth", Style.PPIndentWidth); IO.mapOptional("RawStringFormats", Style.RawStringFormats); IO.mapOptional("ReflowComments", Style.ReflowComments); IO.mapOptional("ShortNamespaceLines", Style.ShortNamespaceLines); @@ -1021,6 +1022,7 @@ LLVMStyle.IndentRequires = false; LLVMStyle.IndentWrappedFunctionNames = false; LLVMStyle.IndentWidth = 2; + LLVMStyle.PPIndentWidth = 2; // default to old value LLVMStyle.InsertTrailingCommas = FormatStyle::TCS_None; LLVMStyle.JavaScriptQuotes = FormatStyle::JSQS_Leave; LLVMStyle.JavaScriptWrapImports = true; @@ -1263,6 +1265,7 @@ ChromiumStyle.BreakAfterJavaFieldAnnotations = true; ChromiumStyle.ContinuationIndentWidth = 8; ChromiumStyle.IndentWidth = 4; + ChromiumStyle.PPIndentWidth = 4; // default to old value // See styleguide for import groups: // https://chromium.googlesource.com/chromium/src/+/master/styleguide/java/java.md#Import-Order ChromiumStyle.JavaImportGroups = { @@ -1309,6 +1312,7 @@ MozillaStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; MozillaStyle.ConstructorInitializerIndentWidth = 2; MozillaStyle.ContinuationIndentWidth = 2; + MozillaStyle.PPIndentWidth = 2; // default to old value MozillaStyle.Cpp11BracedListStyle = false; MozillaStyle.FixNamespaceComments = false; MozillaStyle.IndentCaseLabels = true; @@ -1334,6 +1338,7 @@ Style.ColumnLimit = 0; Style.FixNamespaceComments = false; Style.IndentWidth = 4; + Style.PPIndentWidth = 4; // default to old value Style.NamespaceIndentation = FormatStyle::NI_Inner; Style.ObjCBlockIndentWidth = 4; Style.ObjCSpaceAfterProperty = true; @@ -1363,6 +1368,7 @@ Style.ColumnLimit = 120; Style.TabWidth = 4; Style.IndentWidth = 4; + Style.PPIndentWidth = 4; // default to old value Style.UseTab = FormatStyle::UT_Never; Style.BreakBeforeBraces = FormatStyle::BS_Custom; Style.BraceWrapping.AfterClass = true; Index: clang/include/clang/Format/Format.h =================================================================== --- clang/include/clang/Format/Format.h +++ clang/include/clang/Format/Format.h @@ -2323,6 +2323,18 @@ /// \endcode unsigned IndentWidth; + /// The number of columns to use for indentation of preprocessor statements. + /// \code + /// PPIndentWidth: 1 + /// + /// #ifdef __linux__ + /// # define FOO + /// #else + /// # define BAR + /// #endif + /// \endcode + unsigned PPIndentWidth; + /// Indent if a function definition or declaration is wrapped after the /// type. /// \code Index: clang/docs/ClangFormatStyleOptions.rst =================================================================== --- clang/docs/ClangFormatStyleOptions.rst +++ clang/docs/ClangFormatStyleOptions.rst @@ -2615,6 +2615,19 @@ } } +**PPIndentWidth** (``unsigned``) + The number of columns to use for indentation of preprocessor statements. + + .. code-block:: c++ + + PPIndentWidth: 1 + + #ifdef __linux__ + # define FOO + #else + # define BAR + #endif + **IndentWrappedFunctionNames** (``bool``) Indent if a function definition or declaration is wrapped after the type.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits