[clang] 6f605b8 - [clang-format] Add PPIndentWidth option
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 3 08:55:04 PDT 2021
Author: Gerhard Gappmeier
Date: 2021-06-03T17:55:11+02:00
New Revision: 6f605b8d0bc1c19dccc7a6c248b4fa60e6f7fde3
URL: https://github.com/llvm/llvm-project/commit/6f605b8d0bc1c19dccc7a6c248b4fa60e6f7fde3
DIFF: https://github.com/llvm/llvm-project/commit/6f605b8d0bc1c19dccc7a6c248b4fa60e6f7fde3.diff
LOG: [clang-format] Add PPIndentWidth option
This allows to set a different indent width for preprocessor statements.
Example:
#ifdef __linux_
# define FOO
#endif
int main(void)
{
return 0;
}
Differential Revision: https://reviews.llvm.org/D103286
Added:
Modified:
clang/docs/ClangFormatStyleOptions.rst
clang/docs/ReleaseNotes.rst
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/lib/Format/UnwrappedLineFormatter.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index bbe2807c2e23a..9686830c3bd3e 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -3033,6 +3033,21 @@ the configuration (without a prefix: ``Auto``).
Add a space in front of an Objective-C protocol list, i.e. use
``Foo <Protocol>`` instead of ``Foo<Protocol>``.
+**PPIndentWidth** (``int``)
+ The number of columns to use for indentation of preprocessor statements.
+ When set to -1 (default) ``IndentWidth`` is used also for preprocessor
+ statements.
+
+ .. code-block:: c++
+
+ PPIndentWidth: 1
+
+ #ifdef __linux__
+ # define FOO
+ #else
+ # define BAR
+ #endif
+
**PenaltyBreakAssignment** (``unsigned``)
The penalty for breaking around an assignment operator.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6d3582a6297d6..f5835dd2f7c63 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -233,6 +233,9 @@ clang-format
- Option ``IndentAccessModifiers`` has been added to be able to give access
modifiers their own indentation level inside records.
+- Option ``PPIndentWidth`` has been added to be able to configure pre-processor
+ indentation independent from regular code.
+
- Option ``ShortNamespaceLines`` has been added to give better control
over ``FixNamespaceComments`` when determining a namespace length.
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index c30e357b5d0dd..503f0e446bff3 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2666,6 +2666,20 @@ struct FormatStyle {
/// Pointer and reference alignment style.
PointerAlignmentStyle PointerAlignment;
+ /// The number of columns to use for indentation of preprocessor statements.
+ /// When set to -1 (default) ``IndentWidth`` is used also for preprocessor
+ /// statements.
+ /// \code
+ /// PPIndentWidth: 1
+ ///
+ /// #ifdef __linux__
+ /// # define FOO
+ /// #else
+ /// # define BAR
+ /// #endif
+ /// \endcode
+ int PPIndentWidth;
+
/// See documentation of ``RawStringFormats``.
struct RawStringFormat {
/// The language of this raw string.
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index c85ecd970ff15..8d16f299d3aef 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -665,6 +665,7 @@ template <> struct MappingTraits<FormatStyle> {
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);
@@ -1022,6 +1023,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.IndentRequires = false;
LLVMStyle.IndentWrappedFunctionNames = false;
LLVMStyle.IndentWidth = 2;
+ LLVMStyle.PPIndentWidth = -1;
LLVMStyle.InsertTrailingCommas = FormatStyle::TCS_None;
LLVMStyle.JavaScriptQuotes = FormatStyle::JSQS_Leave;
LLVMStyle.JavaScriptWrapImports = true;
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 2b1b15de8bbf1..41fbb16f0db12 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -57,7 +57,9 @@ class LevelIndentTracker {
while (IndentForLevel.size() <= Line.Level)
IndentForLevel.push_back(-1);
if (Line.InPPDirective) {
- Indent = Line.Level * Style.IndentWidth + AdditionalIndent;
+ unsigned IndentWidth =
+ (Style.PPIndentWidth >= 0) ? Style.PPIndentWidth : Style.IndentWidth;
+ Indent = Line.Level * IndentWidth + AdditionalIndent;
} else {
IndentForLevel.resize(Line.Level + 1);
Indent = getIndent(IndentForLevel, Line.Level);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 79d838cb28b7a..a2583aecf5a9f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -3453,6 +3453,48 @@ TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
verifyFormat("a\f\\");
}
+TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
+ FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
+ style.IndentWidth = 4;
+ style.PPIndentWidth = 1;
+
+ style.IndentPPDirectives = FormatStyle::PPDIS_None;
+ verifyFormat("#ifdef __linux__\n"
+ "void foo() {\n"
+ " int x = 0;\n"
+ "}\n"
+ "#define FOO\n"
+ "#endif\n"
+ "void bar() {\n"
+ " int y = 0;\n"
+ "}\n",
+ style);
+
+ style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
+ verifyFormat("#ifdef __linux__\n"
+ "void foo() {\n"
+ " int x = 0;\n"
+ "}\n"
+ "# define FOO foo\n"
+ "#endif\n"
+ "void bar() {\n"
+ " int y = 0;\n"
+ "}\n",
+ style);
+
+ style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
+ verifyFormat("#ifdef __linux__\n"
+ "void foo() {\n"
+ " int x = 0;\n"
+ "}\n"
+ " #define FOO foo\n"
+ "#endif\n"
+ "void bar() {\n"
+ " int y = 0;\n"
+ "}\n",
+ style);
+}
+
TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
More information about the cfe-commits
mailing list