Skip to content

Commit fe9aef0

Browse files
pointhexowenca
andauthored
[clang-format] Add DiagHandler parameter to format::getStyle() (#91317)
It allows to control of error output for the function. Closes #94205. --------- Co-authored-by: Owen Pan <[email protected]>
1 parent a106131 commit fe9aef0

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3946,20 +3946,24 @@ 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

39593962
Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
39603963
StringRef FallbackStyleName, StringRef Code,
39613964
llvm::vfs::FileSystem *FS,
3962-
bool AllowUnknownOptions) {
3965+
bool AllowUnknownOptions,
3966+
llvm::SourceMgr::DiagHandlerTy DiagHandler) {
39633967
FormatStyle Style = getLLVMStyle(guessLanguage(FileName, Code));
39643968
FormatStyle FallbackStyle = getNoStyle();
39653969
if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle))
@@ -3972,7 +3976,7 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
39723976
StringRef Source = "<command-line>";
39733977
if (std::error_code ec =
39743978
parseConfiguration(llvm::MemoryBufferRef(StyleName, Source), &Style,
3975-
AllowUnknownOptions)) {
3979+
AllowUnknownOptions, DiagHandler)) {
39763980
return make_string_error("Error parsing -style: " + ec.message());
39773981
}
39783982

@@ -3992,7 +3996,8 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
39923996
StyleName.starts_with_insensitive("file:")) {
39933997
auto ConfigFile = StyleName.substr(5);
39943998
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
3995-
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
3999+
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions,
4000+
DiagHandler);
39964001
if (auto EC = Text.getError()) {
39974002
return make_string_error("Error reading " + ConfigFile + ": " +
39984003
EC.message());
@@ -4031,8 +4036,9 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
40314036

40324037
auto applyChildFormatTexts = [&](FormatStyle *Style) {
40334038
for (const auto &MemBuf : llvm::reverse(ChildFormatTextToApply)) {
4034-
auto EC = parseConfiguration(*MemBuf, Style, AllowUnknownOptions,
4035-
dropDiagnosticHandler);
4039+
auto EC =
4040+
parseConfiguration(*MemBuf, Style, AllowUnknownOptions,
4041+
DiagHandler ? DiagHandler : dropDiagnosticHandler);
40364042
// It was already correctly parsed.
40374043
assert(!EC);
40384044
static_cast<void>(EC);
@@ -4066,7 +4072,8 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
40664072
}
40674073

40684074
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
4069-
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
4075+
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions,
4076+
DiagHandler);
40704077
if (auto EC = Text.getError()) {
40714078
if (EC != ParseError::Unsuitable) {
40724079
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+
auto Output = testing::internal::GetCapturedStderr();
1463+
ASSERT_TRUE((bool)Style);
1464+
ASSERT_FALSE(Output.empty());
1465+
1466+
// Suppress stderr.
1467+
testing::internal::CaptureStderr();
1468+
Style = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS,
1469+
/*AllowUnknownOptions=*/true,
1470+
[](const llvm::SMDiagnostic &, void *) {});
1471+
Output = testing::internal::GetCapturedStderr();
1472+
ASSERT_TRUE((bool)Style);
1473+
ASSERT_TRUE(Output.empty());
1474+
}
1475+
14551476
} // namespace
14561477
} // namespace format
14571478
} // namespace clang

0 commit comments

Comments
 (0)