From 8bd97e752f325e2bbeb92f66b5bbb709ae361622 Mon Sep 17 00:00:00 2001 From: Jordan Rupprecht Date: Thu, 28 Feb 2019 19:16:45 +0000 Subject: [PATCH] [clang-format][NFC] Allow getLLVMStyle() to take a language Summary: getLLVMStyle() sets the default style, but doesn't take the language as a parameter, so can't set default parameters when they differ from C++. This change adds LanguageKind as an input to getLLVMStyle so that we can start doing that. See D55964 as a motivation for this, where we want Tablegen to be formatted differently than C++. Reviewers: djasper, krasimir, MyDeveloperDay Reviewed By: MyDeveloperDay Subscribers: jdoerfert, MyDeveloperDay, kristina, cfe-commits, arphaman Tags: #clang Differential Revision: https://reviews.llvm.org/D56943 llvm-svn: 355123 --- clang/include/clang/Format/Format.h | 3 ++- clang/lib/Format/Format.cpp | 10 ++++------ clang/unittests/Format/FormatTest.cpp | 9 +++++++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 4bc0a5c..94cceb3 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1849,7 +1849,8 @@ private: /// Returns a format style complying with the LLVM coding standards: /// http://llvm.org/docs/CodingStandards.html. -FormatStyle getLLVMStyle(); +FormatStyle getLLVMStyle( + FormatStyle::LanguageKind Language = FormatStyle::LanguageKind::LK_Cpp); /// Returns a format style complying with one of Google's style guides: /// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml. diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 9feffeb..d8751a3 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -618,9 +618,9 @@ static FormatStyle expandPresets(const FormatStyle &Style) { return Expanded; } -FormatStyle getLLVMStyle() { +FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { FormatStyle LLVMStyle; - LLVMStyle.Language = FormatStyle::LK_Cpp; + LLVMStyle.Language = Language; LLVMStyle.AccessModifierOffset = -2; LLVMStyle.AlignEscapedNewlines = FormatStyle::ENAS_Right; LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align; @@ -729,8 +729,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { return GoogleStyle; } - FormatStyle GoogleStyle = getLLVMStyle(); - GoogleStyle.Language = Language; + FormatStyle GoogleStyle = getLLVMStyle(Language); GoogleStyle.AccessModifierOffset = -1; GoogleStyle.AlignEscapedNewlines = FormatStyle::ENAS_Left; @@ -2344,8 +2343,7 @@ llvm::Expected getStyle(StringRef StyleName, StringRef FileName, if (!FS) { FS = llvm::vfs::getRealFileSystem().get(); } - FormatStyle Style = getLLVMStyle(); - Style.Language = guessLanguage(FileName, Code); + FormatStyle Style = getLLVMStyle(guessLanguage(FileName, Code)); FormatStyle FallbackStyle = getNoStyle(); if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle)) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 064ece1..0385665 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -120,6 +120,15 @@ TEST_F(FormatTest, MessUp) { EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); } +TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { + EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); +} + +TEST_F(FormatTest, LLVMStyleOverride) { + EXPECT_EQ(FormatStyle::LK_Proto, + getLLVMStyle(FormatStyle::LK_Proto).Language); +} + //===----------------------------------------------------------------------===// // Basic function tests. //===----------------------------------------------------------------------===// -- 2.7.4