Skip to content

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Oct 28, 2024

Closes #107808.

@llvmbot
Copy link
Member

llvmbot commented Oct 28, 2024

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Close #107808.


Full diff: https://github.com/llvm/llvm-project/pull/113864.diff

2 Files Affected:

  • (modified) clang/include/clang/Format/Format.h (+5)
  • (modified) clang/lib/Format/Format.cpp (+14)
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.

}
*Style = *StyleSet.Get(Language);
std::string ConfigFile;
if (!Style->ConfigFile.empty()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!Style->ConfigFile.empty()) {
if (!Style.InheritsParentConfig && !Style->ConfigFile.empty()) {

@owenca owenca changed the title [clang-format] Add -ConfigFile option [clang-format] Add ConfigFile option Oct 31, 2024
@jediry
Copy link

jediry commented Feb 10, 2025

Hi @owenca. i think this would solve my problem. Would you mind moving forward with getting this merged?

@owenca
Copy link
Contributor Author

owenca commented Feb 12, 2025

Did you get an answer to my question yet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to support non-hierarchical .clang-format inheritance
4 participants