Index: docs/ClangFormat.rst =================================================================== --- docs/ClangFormat.rst +++ docs/ClangFormat.rst @@ -42,6 +42,7 @@ -style=file, but can not find the .clang-format file to use. Use -fallback-style=none to skip formatting. + Use -fallback-style=fail to exit with an error. -i - Inplace edit s, if specified. -length= - Format a range of this length (in bytes). Multiple ranges can be formatted by specifying Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -737,7 +737,7 @@ *Style = getWebKitStyle(); } else if (Name.equals_lower("gnu")) { *Style = getGNUStyle(); - } else if (Name.equals_lower("none")) { + } else if (Name.equals_lower("none") || Name.equals_lower("fail")) { *Style = getNoStyle(); } else { return false; @@ -1977,6 +1977,9 @@ return make_string_error("Configuration file(s) do(es) not support " + getLanguageName(Style.Language) + ": " + UnsuitableConfigFiles); + + if (FallbackStyleName.equals_lower("fail")) + return make_string_error("Configuration file not found."); return FallbackStyle; } Index: tools/clang-format/ClangFormat.cpp =================================================================== --- tools/clang-format/ClangFormat.cpp +++ tools/clang-format/ClangFormat.cpp @@ -68,7 +68,8 @@ "fallback in case clang-format is invoked with\n" "-style=file, but can not find the .clang-format\n" "file to use.\n" - "Use -fallback-style=none to skip formatting."), + "Use -fallback-style=none to skip formatting.\n" + "Use -fallback-style=fail to exit with an error."), cl::init("LLVM"), cl::cat(ClangFormatCategory)); static cl::opt Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -11465,7 +11465,12 @@ ASSERT_TRUE((bool)Style2); ASSERT_EQ(*Style2, getNoStyle()); - // Test 2.3: format if config is found with no based style while fallback is + // Test 2.3: no format on 'fail' fallback style. + Style2 = getStyle("file", "/b/test.cpp", "fail", "", &FS); + ASSERT_FALSE((bool)Style2); + llvm::consumeError(Style2.takeError()); + + // Test 2.4: format if config is found with no based style while fallback is // 'none'. ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); @@ -11473,7 +11478,7 @@ ASSERT_TRUE((bool)Style2); ASSERT_EQ(*Style2, getLLVMStyle()); - // Test 2.4: format if yaml with no based style, while fallback is 'none'. + // Test 2.5: format if yaml with no based style, while fallback is 'none'. Style2 = getStyle("{}", "a.h", "none", "", &FS); ASSERT_TRUE((bool)Style2); ASSERT_EQ(*Style2, getLLVMStyle());