aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Format/FormatTest.cpp
diff options
context:
space:
mode:
authorZhao Wei Liew <zhaoweiliew@gmail.com>2022-01-03 11:37:20 +0100
committerMarek Kurdej <marek.kurdej+llvm.org@gmail.com>2022-01-03 11:43:25 +0100
commitb9e173fcd46b336b5589f577a74de9472d4deae3 (patch)
tree530323c4c70fada23e5dde1d7ba3408c2339018c /clang/unittests/Format/FormatTest.cpp
parent0090cd4e7a24bedeb24dfe5b3b55167ad74e231e (diff)
downloadllvm-b9e173fcd46b336b5589f577a74de9472d4deae3.zip
llvm-b9e173fcd46b336b5589f577a74de9472d4deae3.tar.gz
llvm-b9e173fcd46b336b5589f577a74de9472d4deae3.tar.bz2
[clang-format] Add option to explicitly specify a config file
This diff extends the -style=file option to allow a config file to be specified explicitly. This is useful (for instance) when adding IDE commands to reformat code to a personal style. Usage: `clang-format -style=file:<path/to/config/file> ...` Reviewed By: HazardyKnusperkeks, curdeius, MyDeveloperDay, zwliew Differential Revision: https://reviews.llvm.org/D72326
Diffstat (limited to 'clang/unittests/Format/FormatTest.cpp')
-rw-r--r--clang/unittests/Format/FormatTest.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index bb344d4..7160c7a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -21579,6 +21579,70 @@ TEST(FormatStyle, GetStyleOfFile) {
Style.IndentWidth = 7;
return Style;
}());
+
+ // Test 9.9: use inheritance from a specific config file.
+ Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp",
+ "none", "", &FS);
+ ASSERT_TRUE(static_cast<bool>(Style9));
+ ASSERT_EQ(*Style9, SubSubStyle);
+}
+
+TEST(FormatStyle, GetStyleOfSpecificFile) {
+ llvm::vfs::InMemoryFileSystem FS;
+ // Specify absolute path to a format file in a parent directory.
+ ASSERT_TRUE(
+ FS.addFile("/e/.clang-format", 0,
+ llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
+ ASSERT_TRUE(
+ FS.addFile("/e/explicit.clang-format", 0,
+ llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
+ ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0,
+ llvm::MemoryBuffer::getMemBuffer("int i;")));
+ auto Style = getStyle("file:/e/explicit.clang-format",
+ "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
+ ASSERT_TRUE(static_cast<bool>(Style));
+ ASSERT_EQ(*Style, getGoogleStyle());
+
+ // Specify relative path to a format file.
+ ASSERT_TRUE(
+ FS.addFile("../../e/explicit.clang-format", 0,
+ llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
+ Style = getStyle("file:../../e/explicit.clang-format",
+ "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS);
+ ASSERT_TRUE(static_cast<bool>(Style));
+ ASSERT_EQ(*Style, getGoogleStyle());
+
+ // Specify path to a format file that does not exist.
+ Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp",
+ "LLVM", "", &FS);
+ ASSERT_FALSE(static_cast<bool>(Style));
+ llvm::consumeError(Style.takeError());
+
+ // Specify path to a file on the filesystem.
+ SmallString<128> FormatFilePath;
+ std::error_code ECF = llvm::sys::fs::createTemporaryFile(
+ "FormatFileTest", "tpl", FormatFilePath);
+ EXPECT_FALSE((bool)ECF);
+ llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF);
+ EXPECT_FALSE((bool)ECF);
+ FormatFileTest << "BasedOnStyle: Google\n";
+ FormatFileTest.close();
+
+ SmallString<128> TestFilePath;
+ std::error_code ECT =
+ llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath);
+ EXPECT_FALSE((bool)ECT);
+ llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT);
+ CodeFileTest << "int i;\n";
+ CodeFileTest.close();
+
+ std::string format_file_arg = std::string("file:") + FormatFilePath.c_str();
+ Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr);
+
+ llvm::sys::fs::remove(FormatFilePath.c_str());
+ llvm::sys::fs::remove(TestFilePath.c_str());
+ ASSERT_TRUE(static_cast<bool>(Style));
+ ASSERT_EQ(*Style, getGoogleStyle());
}
TEST_F(ReplacementTest, FormatCodeAfterReplacements) {