diff options
author | Owen Pan <owenpiano@gmail.com> | 2025-05-28 19:10:20 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-05-28 19:10:20 -0700 |
commit | 7e1a88b9d1431e263258e3ff0f729c1fdce342d3 (patch) | |
tree | 0b3a128656c01979dee35ef9b854336fc88667be /clang/lib/Format | |
parent | 7b074fc9362a4a6a5f172dd8936a22fb25eff96b (diff) | |
download | llvm-7e1a88b9d1431e263258e3ff0f729c1fdce342d3.zip llvm-7e1a88b9d1431e263258e3ff0f729c1fdce342d3.tar.gz llvm-7e1a88b9d1431e263258e3ff0f729c1fdce342d3.tar.bz2 |
[clang-format] Handle .h files for LK_C and LK_ObjC (#141714)
Fix #137792
Diffstat (limited to 'clang/lib/Format')
-rw-r--r-- | clang/lib/Format/Format.cpp | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 0cfa061..bdaf264 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -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 + ": " + |