From 71e0694b6af00196cc91ad44e602ad34d27554c3 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 6 Aug 2014 13:40:26 +0000 Subject: [PATCH] clang-format: Add special comments to disable formatting. With this patch: int ThisWillBeFormatted; // clang-format off int ThisWontBeFormatted; // clang-format on int Formatted; This is for regions where a significantly nicer code layout can be found knowing the content of the code. This fixes llvm.org/PR20463. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@214966 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 7 +++++++ unittests/Format/FormatTest.cpp | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 8f119d3b65..bd9f323323 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1648,6 +1648,8 @@ private: SmallVector Tokens; SmallVector ForEachMacros; + bool FormattingDisabled = false; + void readRawToken(FormatToken &Tok) { Lex.LexFromRawLexer(Tok.Tok); Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()), @@ -1663,6 +1665,11 @@ private: Tok.Tok.setKind(tok::char_constant); } } + if (Tok.is(tok::comment) && Tok.TokenText == "// clang-format on") + FormattingDisabled = false; + Tok.Finalized = FormattingDisabled; + if (Tok.is(tok::comment) && Tok.TokenText == "// clang-format off") + FormattingDisabled = true; } }; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 5c526c12e1..318f5b4a7c 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -9133,5 +9133,18 @@ TEST_F(FormatTest, HandleConflictMarkers) { "int i;\n")); } +TEST_F(FormatTest, DisableRegions) { + EXPECT_EQ("int i;\n" + "// clang-format off\n" + " int j;\n" + "// clang-format on\n" + "int k;", + format(" int i;\n" + " // clang-format off\n" + " int j;\n" + " // clang-format on\n" + " int k;")); +} + } // end namespace tooling } // end namespace clang -- 2.40.0