https://github.com/owenca created https://github.com/llvm/llvm-project/pull/113864
Close #107808. >From 4cbc31029c5d773dd68c80030e896b3fed88ab77 Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Sun, 27 Oct 2024 22:22:11 -0700 Subject: [PATCH] [clang-format] Add -ConfigFile option Close #107808. --- clang/include/clang/Format/Format.h | 5 +++++ clang/lib/Format/Format.cpp | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index c9b72e65cb236d..1380223493187a 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -2481,6 +2481,10 @@ struct FormatStyle { /// \version 5 bool CompactNamespaces; + /// + /// \version 20 + std::string ConfigFile; + /// This option is **deprecated**. See ``CurrentLine`` of /// ``PackConstructorInitializers``. /// \version 3.7 @@ -5195,6 +5199,7 @@ struct FormatStyle { BreakTemplateDeclarations == R.BreakTemplateDeclarations && ColumnLimit == R.ColumnLimit && CommentPragmas == R.CommentPragmas && CompactNamespaces == R.CompactNamespaces && + ConfigFile == R.ConfigFile && ConstructorInitializerIndentWidth == R.ConstructorInitializerIndentWidth && ContinuationIndentWidth == R.ContinuationIndentWidth && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 0cf4cdbeab31f3..172578092066e1 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1163,6 +1163,7 @@ template <> struct MappingTraits<FormatStyle> { IO.mapOptional("TemplateNames", Style.TemplateNames); IO.mapOptional("TypeNames", Style.TypeNames); IO.mapOptional("TypenameMacros", Style.TypenameMacros); + IO.mapOptional("ConfigFile", Style.ConfigFile); IO.mapOptional("UseTab", Style.UseTab); IO.mapOptional("VerilogBreakBetweenInstancePorts", Style.VerilogBreakBetweenInstancePorts); @@ -2046,6 +2047,11 @@ ParseError validateQualifierOrder(FormatStyle *Style) { return ParseError::Success; } +llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> +loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS, + FormatStyle *Style, bool AllowUnknownOptions, + llvm::SourceMgr::DiagHandlerTy DiagHandler); + std::error_code parseConfiguration(llvm::MemoryBufferRef Config, FormatStyle *Style, bool AllowUnknownOptions, llvm::SourceMgr::DiagHandlerTy DiagHandler, @@ -2102,6 +2108,14 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config, StyleSet.Add(std::move(DefaultStyle)); } *Style = *StyleSet.Get(Language); + if (const StringRef ConfigFile{Style->ConfigFile}; !ConfigFile.empty()) { + auto *FS = llvm::vfs::getRealFileSystem().get(); + assert(FS); + const auto Text = loadAndParseConfigFile(ConfigFile, FS, Style, + AllowUnknownOptions, DiagHandler); + if (Text.getError()) + return make_error_code(ParseError::Error); + } if (Style->InsertTrailingCommas != FormatStyle::TCS_None && Style->BinPackArguments) { // See comment on FormatStyle::TSC_Wrapped. _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits