[clang] [clang-format] Add -ConfigFile option (PR #113864)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sun Oct 27 23:55:10 PDT 2024
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/113864
>From b5f89c18b22bbc07d2f4fcbc2896996efea52478 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at 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 | 17 +++++++++++++++++
2 files changed, 22 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..379aec9e19a5fb 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,17 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config,
StyleSet.Add(std::move(DefaultStyle));
}
*Style = *StyleSet.Get(Language);
+ std::string ConfigFile;
+ if (!Style->ConfigFile.empty()) {
+ auto *FS = llvm::vfs::getRealFileSystem().get();
+ assert(FS);
+ ConfigFile = Style->ConfigFile;
+ Style->ConfigFile.clear();
+ 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.
More information about the cfe-commits
mailing list