Skip to content

Commit 7e1a88b

Browse files
authored
[clang-format] Handle .h files for LK_C and LK_ObjC (#141714)
Fix #137792
1 parent 7b074fc commit 7e1a88b

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

clang/include/clang/Format/Format.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5536,7 +5536,7 @@ struct FormatStyle {
55365536
parseConfiguration(llvm::MemoryBufferRef Config, FormatStyle *Style,
55375537
bool AllowUnknownOptions,
55385538
llvm::SourceMgr::DiagHandlerTy DiagHandler,
5539-
void *DiagHandlerCtxt);
5539+
void *DiagHandlerCtxt, bool IsDotHFile);
55405540
};
55415541

55425542
/// Returns a format style complying with the LLVM coding standards:
@@ -5602,13 +5602,15 @@ std::error_code
56025602
parseConfiguration(llvm::MemoryBufferRef Config, FormatStyle *Style,
56035603
bool AllowUnknownOptions = false,
56045604
llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr,
5605-
void *DiagHandlerCtx = nullptr);
5605+
void *DiagHandlerCtx = nullptr, bool IsDotHFile = false);
56065606

56075607
/// Like above but accepts an unnamed buffer.
56085608
inline std::error_code parseConfiguration(StringRef Config, FormatStyle *Style,
5609-
bool AllowUnknownOptions = false) {
5609+
bool AllowUnknownOptions = false,
5610+
bool IsDotHFile = false) {
56105611
return parseConfiguration(llvm::MemoryBufferRef(Config, "YAML"), Style,
5611-
AllowUnknownOptions);
5612+
AllowUnknownOptions, /*DiagHandler=*/nullptr,
5613+
/*DiagHandlerCtx=*/nullptr, IsDotHFile);
56125614
}
56135615

56145616
/// Gets configuration in a YAML string.

clang/lib/Format/Format.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,7 +2108,7 @@ ParseError validateQualifierOrder(FormatStyle *Style) {
21082108
std::error_code parseConfiguration(llvm::MemoryBufferRef Config,
21092109
FormatStyle *Style, bool AllowUnknownOptions,
21102110
llvm::SourceMgr::DiagHandlerTy DiagHandler,
2111-
void *DiagHandlerCtxt) {
2111+
void *DiagHandlerCtxt, bool IsDotHFile) {
21122112
assert(Style);
21132113
FormatStyle::LanguageKind Language = Style->Language;
21142114
assert(Language != FormatStyle::LK_None);
@@ -2155,6 +2155,10 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config,
21552155
// For backward compatibility.
21562156
(Lang == FormatStyle::LK_Cpp && Language == FormatStyle::LK_C)) {
21572157
LanguageFound = true;
2158+
} else if (IsDotHFile && Language == FormatStyle::LK_Cpp &&
2159+
(Lang == FormatStyle::LK_C || Lang == FormatStyle::LK_ObjC)) {
2160+
Language = Lang;
2161+
LanguageFound = true;
21582162
}
21592163
}
21602164
if (!LanguageFound) {
@@ -4177,13 +4181,15 @@ const char *DefaultFallbackStyle = "LLVM";
41774181
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
41784182
loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS,
41794183
FormatStyle *Style, bool AllowUnknownOptions,
4180-
llvm::SourceMgr::DiagHandlerTy DiagHandler) {
4184+
llvm::SourceMgr::DiagHandlerTy DiagHandler,
4185+
bool IsDotHFile) {
41814186
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
41824187
FS->getBufferForFile(ConfigFile.str());
41834188
if (auto EC = Text.getError())
41844189
return EC;
41854190
if (auto EC = parseConfiguration(*Text.get(), Style, AllowUnknownOptions,
4186-
DiagHandler)) {
4191+
DiagHandler, /*DiagHandlerCtx=*/nullptr,
4192+
IsDotHFile)) {
41874193
return EC;
41884194
}
41894195
return Text;
@@ -4221,13 +4227,15 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
42214227
FS = llvm::vfs::getRealFileSystem().get();
42224228
assert(FS);
42234229

4230+
const bool IsDotHFile = FileName.ends_with(".h");
4231+
42244232
// User provided clang-format file using -style=file:path/to/format/file.
42254233
if (!Style.InheritsParentConfig &&
42264234
StyleName.starts_with_insensitive("file:")) {
42274235
auto ConfigFile = StyleName.substr(5);
42284236
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
42294237
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions,
4230-
DiagHandler);
4238+
DiagHandler, IsDotHFile);
42314239
if (auto EC = Text.getError()) {
42324240
return make_string_error("Error reading " + ConfigFile + ": " +
42334241
EC.message());
@@ -4303,7 +4311,7 @@ Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
43034311

43044312
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
43054313
loadAndParseConfigFile(ConfigFile, FS, &Style, AllowUnknownOptions,
4306-
DiagHandler);
4314+
DiagHandler, IsDotHFile);
43074315
if (auto EC = Text.getError()) {
43084316
if (EC != ParseError::Unsuitable) {
43094317
return make_string_error("Error reading " + ConfigFile + ": " +

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,24 @@ TEST(ConfigParseTest, AllowCppForC) {
12671267
ParseError::Success);
12681268
}
12691269

1270+
TEST(ConfigParseTest, HandleNonCppDotHFile) {
1271+
FormatStyle Style = {};
1272+
Style.Language = FormatStyle::LK_Cpp;
1273+
EXPECT_EQ(parseConfiguration("Language: C", &Style,
1274+
/*AllowUnknownOptions=*/false,
1275+
/*IsDotHFile=*/true),
1276+
ParseError::Success);
1277+
EXPECT_EQ(Style.Language, FormatStyle::LK_C);
1278+
1279+
Style = {};
1280+
Style.Language = FormatStyle::LK_Cpp;
1281+
EXPECT_EQ(parseConfiguration("Language: ObjC", &Style,
1282+
/*AllowUnknownOptions=*/false,
1283+
/*IsDotHFile=*/true),
1284+
ParseError::Success);
1285+
EXPECT_EQ(Style.Language, FormatStyle::LK_ObjC);
1286+
}
1287+
12701288
TEST(ConfigParseTest, UsesLanguageForBasedOnStyle) {
12711289
FormatStyle Style = {};
12721290
Style.Language = FormatStyle::LK_JavaScript;

0 commit comments

Comments
 (0)