Skip to content

[clang-format] Add DiagHandler parameter to format::getStyle() #91317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -5387,10 +5387,11 @@ extern const char *DefaultFallbackStyle;
/// \returns FormatStyle as specified by ``StyleName``. If ``StyleName`` is
/// "file" and no file is found, returns ``FallbackStyle``. If no style could be
/// determined, returns an Error.
Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
StringRef FallbackStyle, StringRef Code = "",
llvm::vfs::FileSystem *FS = nullptr,
bool AllowUnknownOptions = false);
Expected<FormatStyle>
getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyle,
StringRef Code = "", llvm::vfs::FileSystem *FS = nullptr,
bool AllowUnknownOptions = false,
llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr);

// Guesses the language from the ``FileName`` and ``Code`` to be formatted.
// Defaults to FormatStyle::LK_Cpp.
Expand Down
23 changes: 15 additions & 8 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3946,20 +3946,24 @@ const char *DefaultFallbackStyle = "LLVM";

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS,
FormatStyle *Style, bool AllowUnknownOptions) {
FormatStyle *Style, bool AllowUnknownOptions,
llvm::SourceMgr::DiagHandlerTy DiagHandler) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
FS->getBufferForFile(ConfigFile.str());
if (auto EC = Text.getError())
return EC;
if (auto EC = parseConfiguration(*Text.get(), Style, AllowUnknownOptions))
if (auto EC = parseConfiguration(*Text.get(), Style, AllowUnknownOptions,
DiagHandler)) {
return EC;
}
return Text;
}

Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
StringRef FallbackStyleName, StringRef Code,
llvm::vfs::FileSystem *FS,
bool AllowUnknownOptions) {
bool AllowUnknownOptions,
llvm::SourceMgr::DiagHandlerTy DiagHandler) {
FormatStyle Style = getLLVMStyle(guessLanguage(FileName, Code));
FormatStyle FallbackStyle = getNoStyle();
if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle))
Expand All @@ -3972,7 +3976,7 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
StringRef Source = "<command-line>";
if (std::error_code ec =
parseConfiguration(llvm::MemoryBufferRef(StyleName, Source), &Style,
AllowUnknownOptions)) {
AllowUnknownOptions, DiagHandler)) {
return make_string_error("Error parsing -style: " + ec.message());
}

Expand All @@ -3992,7 +3996,8 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
StyleName.starts_with_insensitive("file:")) {
auto ConfigFile = StyleName.substr(5);
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions,
DiagHandler);
if (auto EC = Text.getError()) {
return make_string_error("Error reading " + ConfigFile + ": " +
EC.message());
Expand Down Expand Up @@ -4031,8 +4036,9 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,

auto applyChildFormatTexts = [&](FormatStyle *Style) {
for (const auto &MemBuf : llvm::reverse(ChildFormatTextToApply)) {
auto EC = parseConfiguration(*MemBuf, Style, AllowUnknownOptions,
dropDiagnosticHandler);
auto EC =
parseConfiguration(*MemBuf, Style, AllowUnknownOptions,
DiagHandler ? DiagHandler : dropDiagnosticHandler);
// It was already correctly parsed.
assert(!EC);
static_cast<void>(EC);
Expand Down Expand Up @@ -4066,7 +4072,8 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
}

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions);
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions,
DiagHandler);
if (auto EC = Text.getError()) {
if (EC != ParseError::Unsuitable) {
return make_string_error("Error reading " + ConfigFile + ": " +
Expand Down
21 changes: 21 additions & 0 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,27 @@ TEST(ConfigParseTest, GetStyleOfSpecificFile) {
ASSERT_EQ(*Style, getGoogleStyle());
}

TEST(ConfigParseTest, GetStyleOutput) {
llvm::vfs::InMemoryFileSystem FS;

// Don't suppress output.
testing::internal::CaptureStderr();
auto Style = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS,
/*AllowUnknownOptions=*/true);
auto Output = testing::internal::GetCapturedStderr();
ASSERT_TRUE((bool)Style);
ASSERT_FALSE(Output.empty());

// Suppress stderr.
testing::internal::CaptureStderr();
Style = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS,
/*AllowUnknownOptions=*/true,
[](const llvm::SMDiagnostic &, void *) {});
Output = testing::internal::GetCapturedStderr();
ASSERT_TRUE((bool)Style);
ASSERT_TRUE(Output.empty());
}

} // namespace
} // namespace format
} // namespace clang
Loading