r322634 - [clang-format] Reorganize RawStringFormat based on language
Krasimir Georgiev via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 17 04:24:59 PST 2018
Author: krasimir
Date: Wed Jan 17 04:24:59 2018
New Revision: 322634
URL: http://llvm.org/viewvc/llvm-project?rev=322634&view=rev
Log:
[clang-format] Reorganize RawStringFormat based on language
Summary:
This patch changes the structure for raw string formatting options by making it
language based (enumerate delimiters per language) as opposed to delimiter-based
(specify the language for a delimiter). The raw string formatting now uses an
appropriate style from the .clang-format file, if exists.
Reviewers: bkramer
Reviewed By: bkramer
Subscribers: cfe-commits, klimek
Differential Revision: https://reviews.llvm.org/D42098
Modified:
cfe/trunk/include/clang/Format/Format.h
cfe/trunk/lib/Format/ContinuationIndenter.cpp
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
cfe/trunk/unittests/Format/FormatTestRawStrings.cpp
Modified: cfe/trunk/include/clang/Format/Format.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=322634&r1=322633&r2=322634&view=diff
==============================================================================
--- cfe/trunk/include/clang/Format/Format.h (original)
+++ cfe/trunk/include/clang/Format/Format.h Wed Jan 17 04:24:59 2018
@@ -1363,36 +1363,43 @@ struct FormatStyle {
/// See documentation of ``RawStringFormats``.
struct RawStringFormat {
- /// \brief The delimiter that this raw string format matches.
- std::string Delimiter;
/// \brief The language of this raw string.
LanguageKind Language;
+ /// \brief A list of raw string delimiters that match this language.
+ std::vector<std::string> Delimiters;
/// \brief The style name on which this raw string format is based on.
/// If not specified, the raw string format is based on the style that this
/// format is based on.
std::string BasedOnStyle;
bool operator==(const RawStringFormat &Other) const {
- return Delimiter == Other.Delimiter && Language == Other.Language &&
+ return Language == Other.Language && Delimiters == Other.Delimiters &&
BasedOnStyle == Other.BasedOnStyle;
}
};
- /// \brief Raw string delimiters denoting that the raw string contents are
- /// code in a particular language and can be reformatted.
+ /// \brief Defines hints for detecting supported languages code blocks in raw
+ /// strings.
///
/// A raw string with a matching delimiter will be reformatted assuming the
- /// specified language based on a predefined style given by 'BasedOnStyle'.
- /// If 'BasedOnStyle' is not found, the formatting is based on llvm style.
+ /// specified language based on the style for that language defined in the
+ /// .clang-format file. If no style has been defined in the .clang-format file
+ /// for the specific language, a predefined style given by 'BasedOnStyle' is
+ /// used. If 'BasedOnStyle' is not found, the formatting is based on llvm
+ /// style.
///
/// To configure this in the .clang-format file, use:
/// \code{.yaml}
/// RawStringFormats:
- /// - Delimiter: 'pb'
- /// Language: TextProto
- /// BasedOnStyle: llvm
- /// - Delimiter: 'proto'
- /// Language: TextProto
- /// BasedOnStyle: google
+ /// - Language: TextProto
+ /// Delimiters:
+ /// - 'pb'
+ /// - 'proto'
+ /// BasedOnStyle: google
+ /// - Language: Cpp
+ /// Delimiters:
+ /// - 'cc'
+ /// - 'cpp'
+ /// BasedOnStyle: llvm
/// \endcode
std::vector<RawStringFormat> RawStringFormats;
Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=322634&r1=322633&r2=322634&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Wed Jan 17 04:24:59 2018
@@ -105,14 +105,21 @@ static llvm::Optional<StringRef> getRawS
RawStringFormatStyleManager::RawStringFormatStyleManager(
const FormatStyle &CodeStyle) {
for (const auto &RawStringFormat : CodeStyle.RawStringFormats) {
- FormatStyle Style;
- if (!getPredefinedStyle(RawStringFormat.BasedOnStyle,
- RawStringFormat.Language, &Style)) {
- Style = getLLVMStyle();
- Style.Language = RawStringFormat.Language;
+ for (StringRef Delimiter : RawStringFormat.Delimiters) {
+ llvm::Optional<FormatStyle> LanguageStyle =
+ CodeStyle.GetLanguageStyle(RawStringFormat.Language);
+ if (!LanguageStyle) {
+ FormatStyle PredefinedStyle;
+ if (!getPredefinedStyle(RawStringFormat.BasedOnStyle,
+ RawStringFormat.Language, &PredefinedStyle)) {
+ PredefinedStyle = getLLVMStyle();
+ PredefinedStyle.Language = RawStringFormat.Language;
+ }
+ LanguageStyle = PredefinedStyle;
+ }
+ LanguageStyle->ColumnLimit = CodeStyle.ColumnLimit;
+ DelimiterStyle.insert({Delimiter, *LanguageStyle});
}
- Style.ColumnLimit = CodeStyle.ColumnLimit;
- DelimiterStyle.insert({RawStringFormat.Delimiter, Style});
}
}
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=322634&r1=322633&r2=322634&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed Jan 17 04:24:59 2018
@@ -455,8 +455,8 @@ template <> struct ScalarEnumerationTrai
template <> struct MappingTraits<FormatStyle::RawStringFormat> {
static void mapping(IO &IO, FormatStyle::RawStringFormat &Format) {
- IO.mapOptional("Delimiter", Format.Delimiter);
IO.mapOptional("Language", Format.Language);
+ IO.mapOptional("Delimiters", Format.Delimiters);
IO.mapOptional("BasedOnStyle", Format.BasedOnStyle);
}
};
@@ -641,7 +641,6 @@ FormatStyle getLLVMStyle() {
LLVMStyle.SpacesBeforeTrailingComments = 1;
LLVMStyle.Standard = FormatStyle::LS_Cpp11;
LLVMStyle.UseTab = FormatStyle::UT_Never;
- LLVMStyle.RawStringFormats = {{"pb", FormatStyle::LK_TextProto, "google"}};
LLVMStyle.ReflowComments = true;
LLVMStyle.SpacesInParentheses = false;
LLVMStyle.SpacesInSquareBrackets = false;
@@ -695,6 +694,19 @@ FormatStyle getGoogleStyle(FormatStyle::
GoogleStyle.ObjCSpaceAfterProperty = false;
GoogleStyle.ObjCSpaceBeforeProtocolList = false;
GoogleStyle.PointerAlignment = FormatStyle::PAS_Left;
+ GoogleStyle.RawStringFormats = {{
+ FormatStyle::LK_TextProto,
+ /*Delimiters=*/
+ {
+ "pb",
+ "PB",
+ "proto",
+ "PROTO",
+ "textproto",
+ "TEXTPROTO",
+ },
+ /*BasedOnStyle=*/"google",
+ }};
GoogleStyle.SpacesBeforeTrailingComments = 2;
GoogleStyle.Standard = FormatStyle::LS_Auto;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=322634&r1=322633&r2=322634&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Jan 17 04:24:59 2018
@@ -10408,16 +10408,21 @@ TEST_F(FormatTest, ParsesConfiguration)
Style.RawStringFormats.clear();
std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
- {"pb", FormatStyle::LK_TextProto, "llvm"},
- {"cpp", FormatStyle::LK_Cpp, "google"}};
+ {FormatStyle::LK_TextProto, {"pb", "proto"}, "llvm"},
+ {FormatStyle::LK_Cpp, {"cc", "cpp"}, "google"},
+ };
CHECK_PARSE("RawStringFormats:\n"
- " - Delimiter: 'pb'\n"
- " Language: TextProto\n"
+ " - Language: TextProto\n"
+ " Delimiters:\n"
+ " - 'pb'\n"
+ " - 'proto'\n"
" BasedOnStyle: llvm\n"
- " - Delimiter: 'cpp'\n"
- " Language: Cpp\n"
- " BasedOnStyle: google",
+ " - Language: Cpp\n"
+ " Delimiters:\n"
+ " - 'cc'\n"
+ " - 'cpp'\n"
+ " BasedOnStyle: google\n",
RawStringFormats, ExpectedRawStringFormats);
}
Modified: cfe/trunk/unittests/Format/FormatTestRawStrings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestRawStrings.cpp?rev=322634&r1=322633&r2=322634&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestRawStrings.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestRawStrings.cpp Wed Jan 17 04:24:59 2018
@@ -65,23 +65,23 @@ protected:
FormatStyle getRawStringPbStyleWithColumns(unsigned ColumnLimit) {
FormatStyle Style = getLLVMStyle();
Style.ColumnLimit = ColumnLimit;
- Style.RawStringFormats = {{/*Delimiter=*/"pb",
- /*Kind=*/FormatStyle::LK_TextProto,
+ Style.RawStringFormats = {{/*Language=*/FormatStyle::LK_TextProto,
+ /*Delimiters=*/{"pb"},
/*BasedOnStyle=*/"google"}};
return Style;
}
FormatStyle getRawStringLLVMCppStyleBasedOn(std::string BasedOnStyle) {
FormatStyle Style = getLLVMStyle();
- Style.RawStringFormats = {{/*Delimiter=*/"cpp",
- /*Kind=*/FormatStyle::LK_Cpp, BasedOnStyle}};
+ Style.RawStringFormats = {{/*Language=*/FormatStyle::LK_Cpp,
+ /*Delimiters=*/{"cpp"}, BasedOnStyle}};
return Style;
}
FormatStyle getRawStringGoogleCppStyleBasedOn(std::string BasedOnStyle) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
- Style.RawStringFormats = {{/*Delimiter=*/"cpp",
- /*Kind=*/FormatStyle::LK_Cpp, BasedOnStyle}};
+ Style.RawStringFormats = {{/*Language=*/FormatStyle::LK_Cpp,
+ /*Delimiters=*/{"cpp"}, BasedOnStyle}};
return Style;
}
@@ -112,6 +112,21 @@ TEST_F(FormatTestRawStrings, ReformatsAc
getRawStringGoogleCppStyleBasedOn("llvm")));
}
+TEST_F(FormatTestRawStrings, UsesConfigurationOverBaseStyle) {
+ // llvm style puts '*' on the right.
+ // google style puts '*' on the left.
+
+ // Uses the configured google style inside raw strings even if BasedOnStyle in
+ // the raw string format is llvm.
+ FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
+ EXPECT_EQ(0, parseConfiguration("---\n"
+ "Language: Cpp\n"
+ "BasedOnStyle: Google", &Style).value());
+ Style.RawStringFormats = {{FormatStyle::LK_Cpp, {"cpp"}, "llvm"}};
+ expect_eq(R"test(int* i = R"cpp(int* j = 0;)cpp";)test",
+ format(R"test(int * i = R"cpp(int * j = 0;)cpp";)test", Style));
+}
+
TEST_F(FormatTestRawStrings, MatchesDelimitersCaseSensitively) {
// Don't touch the 'PB' raw string, format the 'pb' raw string.
expect_eq(R"test(
@@ -121,29 +136,6 @@ t = R"pb(item: 1)pb";)test",
s = R"PB(item:1)PB";
t = R"pb(item:1)pb";)test",
getRawStringPbStyleWithColumns(40)));
-
- FormatStyle MixedStyle = getLLVMStyle();
- MixedStyle.RawStringFormats = {
- {/*Delimiter=*/"cpp", /*Kind=*/FormatStyle::LK_Cpp,
- /*BasedOnStyle=*/"llvm"},
- {/*Delimiter=*/"CPP", /*Kind=*/FormatStyle::LK_Cpp,
- /*BasedOnStyle=*/"google"}};
-
- // Format the 'cpp' raw string with '*' on the right.
- // Format the 'CPP' raw string with '*' on the left.
- // Do not format the 'Cpp' raw string.
- // Do not format non-raw strings.
- expect_eq(R"test(
-a = R"cpp(int *i = 0;)cpp";
-b = R"CPP(int* j = 0;)CPP";
-c = R"Cpp(int * k = 0;)Cpp";
-d = R"cpp(int * k = 0;)Cpp";)test",
- format(R"test(
-a = R"cpp(int * i = 0;)cpp";
-b = R"CPP(int * j = 0;)CPP";
-c = R"Cpp(int * k = 0;)Cpp";
-d = R"cpp(int * k = 0;)Cpp";)test",
- MixedStyle));
}
TEST_F(FormatTestRawStrings, ReformatsShortRawStringsOnSingleLine) {
More information about the cfe-commits
mailing list