aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Format/FormatTestJava.cpp
diff options
context:
space:
mode:
authorsstwcw <su3e8a96kzlver@posteo.net>2023-06-29 15:23:36 +0000
committersstwcw <su3e8a96kzlver@posteo.net>2023-09-05 03:19:49 +0000
commitddc80637ccbc5be26ae40f01841c6019a38d1955 (patch)
treede973a69e1871bdc0e0193f138606652f6b4b507 /clang/unittests/Format/FormatTestJava.cpp
parentef5217b3c0dcbb58927fe43400b6d1faa677bf98 (diff)
downloadllvm-ddc80637ccbc5be26ae40f01841c6019a38d1955.zip
llvm-ddc80637ccbc5be26ae40f01841c6019a38d1955.tar.gz
llvm-ddc80637ccbc5be26ae40f01841c6019a38d1955.tar.bz2
[clang-format] Break long string literals in C#, etc.
Now strings that are too long for one line in C#, Java, JavaScript, and Verilog get broken into several lines. C# and JavaScript interpolated strings are not broken. A new subclass BreakableStringLiteralUsingOperators is used to handle the logic for adding plus signs and commas. The updateAfterBroken method was added because now parentheses or braces may be required after the parentheses or commas are added. In order to decide whether the added plus sign should be unindented in the BreakableToken object, the logic for it is taken out into a separate function shouldUnindentNextOperator. The logic for finding the continuation indentation when the option AlignAfterOpenBracket is set to DontAlign is not implemented yet. So in that case the new line may have the wrong indentation, and the parts may have the wrong length if the string needs to be broken more than once because finding where to break the string depends on where the string starts. The preambles for the C# and Java unit tests are changed to the newer style in order to allow the 3-argument verifyFormat macro. Some cases are changed from verifyFormat to verifyImcompleteFormat because those use incomplete code and the new verifyFormat function checks that the code is complete. The line in the doc was changed to being indented by 4 spaces, that is, the default continuation indentation. It has always been the case. It was probably a mistake that the doc showed 2 spaces previously. This commit was fist committed as 16ccba51072b. The tests caused assertion failures. Then it was reverted in 547bce36132a. Reviewed By: MyDeveloperDay Differential Revision: https://reviews.llvm.org/D154093
Diffstat (limited to 'clang/unittests/Format/FormatTestJava.cpp')
-rw-r--r--clang/unittests/Format/FormatTestJava.cpp44
1 files changed, 12 insertions, 32 deletions
diff --git a/clang/unittests/Format/FormatTestJava.cpp b/clang/unittests/Format/FormatTestJava.cpp
index 51afe79..f3bf70a 100644
--- a/clang/unittests/Format/FormatTestJava.cpp
+++ b/clang/unittests/Format/FormatTestJava.cpp
@@ -6,34 +6,19 @@
//
//===----------------------------------------------------------------------===//
-#include "FormatTestUtils.h"
-#include "clang/Format/Format.h"
-#include "llvm/Support/Debug.h"
-#include "gtest/gtest.h"
+#include "FormatTestBase.h"
#define DEBUG_TYPE "format-test"
namespace clang {
namespace format {
+namespace test {
+namespace {
-class FormatTestJava : public ::testing::Test {
+class FormatTestJava : public test::FormatTestBase {
protected:
- static std::string format(llvm::StringRef Code, unsigned Offset,
- unsigned Length, const FormatStyle &Style) {
- LLVM_DEBUG(llvm::errs() << "---\n");
- LLVM_DEBUG(llvm::errs() << Code << "\n\n");
- std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
- tooling::Replacements Replaces = reformat(Style, Code, Ranges);
- auto Result = applyAllReplacements(Code, Replaces);
- EXPECT_TRUE(static_cast<bool>(Result));
- LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
- return *Result;
- }
-
- static std::string
- format(llvm::StringRef Code,
- const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
- return format(Code, 0, Code.size(), Style);
+ FormatStyle getDefaultStyle() const override {
+ return getGoogleStyle(FormatStyle::LK_Java);
}
static FormatStyle getStyleWithColumns(unsigned ColumnLimit) {
@@ -41,13 +26,6 @@ protected:
Style.ColumnLimit = ColumnLimit;
return Style;
}
-
- static void verifyFormat(
- llvm::StringRef Code,
- const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
- EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable";
- EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
- }
};
TEST_F(FormatTestJava, NoAlternativeOperatorNames) {
@@ -565,9 +543,9 @@ TEST_F(FormatTestJava, FormatsLambdas) {
}
TEST_F(FormatTestJava, BreaksStringLiterals) {
- // FIXME: String literal breaking is currently disabled for Java and JS, as it
- // requires strings to be merged using "+" which we don't support.
- verifyFormat("\"some text other\";", getStyleWithColumns(14));
+ verifyFormat("x = \"some text \"\n"
+ " + \"other\";",
+ "x = \"some text other\";", getStyleWithColumns(18));
}
TEST_F(FormatTestJava, AlignsBlockComments) {
@@ -625,5 +603,7 @@ TEST_F(FormatTestJava, ShortFunctions) {
Style);
}
+} // namespace
+} // namespace test
} // namespace format
-} // end namespace clang
+} // namespace clang