-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-format] Add ConfigFile option #113864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesClose #107808. Full diff: https://github.com/llvm/llvm-project/pull/113864.diff 2 Files Affected:
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.
|
clang/lib/Format/Format.cpp
Outdated
} | ||
*Style = *StyleSet.Get(Language); | ||
std::string ConfigFile; | ||
if (!Style->ConfigFile.empty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!Style->ConfigFile.empty()) { | |
if (!Style.InheritsParentConfig && !Style->ConfigFile.empty()) { |
Hi @owenca. i think this would solve my problem. Would you mind moving forward with getting this merged? |
Did you get an answer to my question yet? |
Closes #107808.