Skip to content

Commit c1e0ad6

Browse files
committed
[clang-format] Add DiagHandler for getStyle function
It allows to control of error output for the function.
1 parent ae71609 commit c1e0ad6

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

clang/include/clang/Format/Format.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5387,10 +5387,11 @@ extern const char *DefaultFallbackStyle;
53875387
/// \returns FormatStyle as specified by ``StyleName``. If ``StyleName`` is
53885388
/// "file" and no file is found, returns ``FallbackStyle``. If no style could be
53895389
/// determined, returns an Error.
5390-
Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
5391-
StringRef FallbackStyle, StringRef Code = "",
5392-
llvm::vfs::FileSystem *FS = nullptr,
5393-
bool AllowUnknownOptions = false);
5390+
Expected<FormatStyle>
5391+
getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyle,
5392+
StringRef Code = "", llvm::vfs::FileSystem *FS = nullptr,
5393+
bool AllowUnknownOptions = false,
5394+
llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr);
53945395

53955396
// Guesses the language from the ``FileName`` and ``Code`` to be formatted.
53965397
// Defaults to FormatStyle::LK_Cpp.

clang/lib/Format/Format.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3946,20 +3946,23 @@ const char *DefaultFallbackStyle = "LLVM";
39463946

39473947
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
39483948
loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS,
3949-
FormatStyle *Style, bool AllowUnknownOptions) {
3949+
FormatStyle *Style, bool AllowUnknownOptions,
3950+
llvm::SourceMgr::DiagHandlerTy DiagHandler) {
39503951
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
39513952
FS->getBufferForFile(ConfigFile.str());
39523953
if (auto EC = Text.getError())
39533954
return EC;
3954-
if (auto EC = parseConfiguration(*Text.get(), Style, AllowUnknownOptions))
3955+
if (auto EC = parseConfiguration(*Text.get(), Style, AllowUnknownOptions,
3956+
DiagHandler)) {
39553957
return EC;
3958+
}
39563959
return Text;
39573960
}
39583961

3959-
Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
3960-
StringRef FallbackStyleName, StringRef Code,
3961-
llvm::vfs::FileSystem *FS,
3962-
bool AllowUnknownOptions) {
3962+
Expected<FormatStyle>
3963+
getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyleName,
3964+
StringRef Code, llvm::vfs::FileSystem *FS, bool AllowUnknownOptions,
3965+
llvm::SourceMgr::DiagHandlerTy DiagHandler) {
39633966
FormatStyle Style = getLLVMStyle(guessLanguage(FileName, Code));
39643967
FormatStyle FallbackStyle = getNoStyle();
39653968
if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle))
@@ -3972,7 +3975,7 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
39723975
StringRef Source = "<command-line>";
39733976
if (std::error_code ec =
39743977
parseConfiguration(llvm::MemoryBufferRef(StyleName, Source), &Style,
3975-
AllowUnknownOptions)) {
3978+
AllowUnknownOptions, DiagHandler)) {
39763979
return make_string_error("Error parsing -style: " + ec.message());
39773980
}
39783981

@@ -3992,7 +3995,8 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
39923995
StyleName.starts_with_insensitive("file:")) {
39933996
auto ConfigFile = StyleName.substr(5);
39943997
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
3995-
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
3998+
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions,
3999+
DiagHandler);
39964000
if (auto EC = Text.getError()) {
39974001
return make_string_error("Error reading " + ConfigFile + ": " +
39984002
EC.message());
@@ -4032,7 +4036,7 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
40324036
auto applyChildFormatTexts = [&](FormatStyle *Style) {
40334037
for (const auto &MemBuf : llvm::reverse(ChildFormatTextToApply)) {
40344038
auto EC = parseConfiguration(*MemBuf, Style, AllowUnknownOptions,
4035-
dropDiagnosticHandler);
4039+
DiagHandler ? DiagHandler : dropDiagnosticHandler);
40364040
// It was already correctly parsed.
40374041
assert(!EC);
40384042
static_cast<void>(EC);
@@ -4066,7 +4070,8 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
40664070
}
40674071

40684072
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
4069-
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
4073+
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions,
4074+
DiagHandler);
40704075
if (auto EC = Text.getError()) {
40714076
if (EC != ParseError::Unsuitable) {
40724077
return make_string_error("Error reading " + ConfigFile + ": " +

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,27 @@ TEST(ConfigParseTest, GetStyleOfSpecificFile) {
14521452
ASSERT_EQ(*Style, getGoogleStyle());
14531453
}
14541454

1455+
TEST(ConfigParseTest, GetStyleOutput) {
1456+
llvm::vfs::InMemoryFileSystem FS;
1457+
1458+
// Don't suppress output.
1459+
testing::internal::CaptureStderr();
1460+
auto Style = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS,
1461+
/*AllowUnknownOptions=*/true);
1462+
const auto output = testing::internal::GetCapturedStderr();
1463+
ASSERT_TRUE((bool)Style);
1464+
ASSERT_FALSE(output.empty());
1465+
1466+
// Suppress stderr.
1467+
testing::internal::CaptureStderr();
1468+
auto Style1 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS,
1469+
/*AllowUnknownOptions=*/true,
1470+
[](const llvm::SMDiagnostic &, void *) {});
1471+
const auto output1 = testing::internal::GetCapturedStderr();
1472+
ASSERT_TRUE((bool)Style1);
1473+
ASSERT_TRUE(output1.empty());
1474+
}
1475+
14551476
} // namespace
14561477
} // namespace format
14571478
} // namespace clang

0 commit comments

Comments
 (0)