[clang-format] Handle .h files for LK_C and LK_ObjC (#141714)

Fix #137792
This commit is contained in:
Owen Pan
2025-05-28 19:10:20 -07:00
committed by GitHub
parent 7b074fc936
commit 7e1a88b9d1
3 changed files with 37 additions and 9 deletions

View File

@@ -5536,7 +5536,7 @@ private:
parseConfiguration(llvm::MemoryBufferRef Config, FormatStyle *Style,
bool AllowUnknownOptions,
llvm::SourceMgr::DiagHandlerTy DiagHandler,
void *DiagHandlerCtxt);
void *DiagHandlerCtxt, bool IsDotHFile);
};
/// Returns a format style complying with the LLVM coding standards:
@@ -5602,13 +5602,15 @@ std::error_code
parseConfiguration(llvm::MemoryBufferRef Config, FormatStyle *Style,
bool AllowUnknownOptions = false,
llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr,
void *DiagHandlerCtx = nullptr);
void *DiagHandlerCtx = nullptr, bool IsDotHFile = false);
/// Like above but accepts an unnamed buffer.
inline std::error_code parseConfiguration(StringRef Config, FormatStyle *Style,
bool AllowUnknownOptions = false) {
bool AllowUnknownOptions = false,
bool IsDotHFile = false) {
return parseConfiguration(llvm::MemoryBufferRef(Config, "YAML"), Style,
AllowUnknownOptions);
AllowUnknownOptions, /*DiagHandler=*/nullptr,
/*DiagHandlerCtx=*/nullptr, IsDotHFile);
}
/// Gets configuration in a YAML string.

View File

@@ -2108,7 +2108,7 @@ ParseError validateQualifierOrder(FormatStyle *Style) {
std::error_code parseConfiguration(llvm::MemoryBufferRef Config,
FormatStyle *Style, bool AllowUnknownOptions,
llvm::SourceMgr::DiagHandlerTy DiagHandler,
void *DiagHandlerCtxt) {
void *DiagHandlerCtxt, bool IsDotHFile) {
assert(Style);
FormatStyle::LanguageKind Language = Style->Language;
assert(Language != FormatStyle::LK_None);
@@ -2155,6 +2155,10 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config,
// For backward compatibility.
(Lang == FormatStyle::LK_Cpp && Language == FormatStyle::LK_C)) {
LanguageFound = true;
} else if (IsDotHFile && Language == FormatStyle::LK_Cpp &&
(Lang == FormatStyle::LK_C || Lang == FormatStyle::LK_ObjC)) {
Language = Lang;
LanguageFound = true;
}
}
if (!LanguageFound) {
@@ -4177,13 +4181,15 @@ const char *DefaultFallbackStyle = "LLVM";
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS,
FormatStyle *Style, bool AllowUnknownOptions,
llvm::SourceMgr::DiagHandlerTy DiagHandler) {
llvm::SourceMgr::DiagHandlerTy DiagHandler,
bool IsDotHFile) {
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,
DiagHandler)) {
DiagHandler, /*DiagHandlerCtx=*/nullptr,
IsDotHFile)) {
return EC;
}
return Text;
@@ -4221,13 +4227,15 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
FS = llvm::vfs::getRealFileSystem().get();
assert(FS);
const bool IsDotHFile = FileName.ends_with(".h");
// User provided clang-format file using -style=file:path/to/format/file.
if (!Style.InheritsParentConfig &&
StyleName.starts_with_insensitive("file:")) {
auto ConfigFile = StyleName.substr(5);
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions,
DiagHandler);
DiagHandler, IsDotHFile);
if (auto EC = Text.getError()) {
return make_string_error("Error reading " + ConfigFile + ": " +
EC.message());
@@ -4303,7 +4311,7 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions,
DiagHandler);
DiagHandler, IsDotHFile);
if (auto EC = Text.getError()) {
if (EC != ParseError::Unsuitable) {
return make_string_error("Error reading " + ConfigFile + ": " +

View File

@@ -1267,6 +1267,24 @@ TEST(ConfigParseTest, AllowCppForC) {
ParseError::Success);
}
TEST(ConfigParseTest, HandleNonCppDotHFile) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_Cpp;
EXPECT_EQ(parseConfiguration("Language: C", &Style,
/*AllowUnknownOptions=*/false,
/*IsDotHFile=*/true),
ParseError::Success);
EXPECT_EQ(Style.Language, FormatStyle::LK_C);
Style = {};
Style.Language = FormatStyle::LK_Cpp;
EXPECT_EQ(parseConfiguration("Language: ObjC", &Style,
/*AllowUnknownOptions=*/false,
/*IsDotHFile=*/true),
ParseError::Success);
EXPECT_EQ(Style.Language, FormatStyle::LK_ObjC);
}
TEST(ConfigParseTest, UsesLanguageForBasedOnStyle) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_JavaScript;