r214966 - clang-format: Add special comments to disable formatting.
Daniel Jasper
djasper at google.com
Wed Aug 6 06:40:26 PDT 2014
Author: djasper
Date: Wed Aug 6 08:40:26 2014
New Revision: 214966
URL: http://llvm.org/viewvc/llvm-project?rev=214966&view=rev
Log:
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.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=214966&r1=214965&r2=214966&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed Aug 6 08:40:26 2014
@@ -1648,6 +1648,8 @@ private:
SmallVector<FormatToken *, 16> Tokens;
SmallVector<IdentifierInfo *, 8> 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;
}
};
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=214966&r1=214965&r2=214966&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Aug 6 08:40:26 2014
@@ -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
More information about the cfe-commits
mailing list