[clang] [clang-format] Add ConfigFile option (PR #113864)
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/113864 >From 85f78a4879a37fb367dccd5299b35d1c75c2b46f Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sun, 27 Oct 2024 22:22:11 -0700 Subject: [PATCH 1/2] [clang-format] Add ConfigFile option Closes #107808. --- clang/docs/ClangFormatStyleOptions.rst | 5 clang/include/clang/Format/Format.h| 5 clang/lib/Format/Format.cpp| 38 -- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 9ef1fd5f36d1d3..9033bdc9018ae7 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -3727,6 +3727,11 @@ the configuration (without a prefix: ``Auto``). namespace Extra { }}} +.. _ConfigFile: + +**ConfigFile** (``String``) :versionbadge:`clang-format 20` :ref:`¶ ` + Specify the absolute or relative path of another config file to process. + .. _ConstructorInitializerAllOnOneLineOrOnePerLine: **ConstructorInitializerAllOnOneLineOrOnePerLine** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index c9b72e65cb236d..44ea7d25ee0e77 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; + /// Specify the absolute or relative path of another config file to process. + /// \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..e835cf7c8b3b45 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1163,6 +1163,7 @@ template <> struct MappingTraits { 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> +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, @@ -2107,8 +2113,36 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config, // See comment on FormatStyle::TSC_Wrapped. return make_error_code(ParseError::BinPackTrailingCommaConflict); } - if (Style->QualifierAlignment != FormatStyle::QAS_Leave) -return make_error_code(validateQualifierOrder(Style)); + if (Style->QualifierAlignment != FormatStyle::QAS_Leave) { +const auto EC = validateQualifierOrder(Style); +if (EC != ParseError::Success) + return make_error_code(EC); + } + if (!Style->InheritsParentConfig && !Style->ConfigFile.empty()) { +auto *FS = llvm::vfs::getRealFileSystem().get(); +assert(FS); +SmallString<128> ConfigFile{Style->ConfigFile}; +Style->ConfigFile.clear(); +switch (ConfigFile[0]) { +case '~': + llvm::sys::fs::expand_tilde(ConfigFile, ConfigFile); + break; +case '/': + break; +default: + llvm::sys::fs::make_absolute( + llvm::sys::path::parent_path(Config.getBufferIdentifier()), + ConfigFile); +} +if (!llvm::sys::fs::exists(ConfigFile)) { + llvm::errs() << ConfigFile << ": " << "file not found\n"; + return make_error_code(ParseError::Error); +} +const auto Text = loadAndParseConfigFile(ConfigFile, FS, Style, + AllowUnknownOptions, DiagHandler); +if (Text.getError()) + return make_error_code(ParseError::Error); + } return make_error_code(ParseError::Success); } >From 73587ff7f377e021b030a2efad4d8a810ec05ad1 Mon Sep 17 00:00:00 2001 From: Owen Pan
[clang] [clang-format] Add ConfigFile option (PR #113864)
https://github.com/mydeveloperday approved this pull request. https://github.com/llvm/llvm-project/pull/113864 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add ConfigFile option (PR #113864)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/113864 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add ConfigFile option (PR #113864)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/113864 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add -ConfigFile option (PR #113864)
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/113864 >From 85f78a4879a37fb367dccd5299b35d1c75c2b46f Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sun, 27 Oct 2024 22:22:11 -0700 Subject: [PATCH] [clang-format] Add ConfigFile option Closes #107808. --- clang/docs/ClangFormatStyleOptions.rst | 5 clang/include/clang/Format/Format.h| 5 clang/lib/Format/Format.cpp| 38 -- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 9ef1fd5f36d1d3..9033bdc9018ae7 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -3727,6 +3727,11 @@ the configuration (without a prefix: ``Auto``). namespace Extra { }}} +.. _ConfigFile: + +**ConfigFile** (``String``) :versionbadge:`clang-format 20` :ref:`¶ ` + Specify the absolute or relative path of another config file to process. + .. _ConstructorInitializerAllOnOneLineOrOnePerLine: **ConstructorInitializerAllOnOneLineOrOnePerLine** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index c9b72e65cb236d..44ea7d25ee0e77 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; + /// Specify the absolute or relative path of another config file to process. + /// \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..e835cf7c8b3b45 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1163,6 +1163,7 @@ template <> struct MappingTraits { 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> +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, @@ -2107,8 +2113,36 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config, // See comment on FormatStyle::TSC_Wrapped. return make_error_code(ParseError::BinPackTrailingCommaConflict); } - if (Style->QualifierAlignment != FormatStyle::QAS_Leave) -return make_error_code(validateQualifierOrder(Style)); + if (Style->QualifierAlignment != FormatStyle::QAS_Leave) { +const auto EC = validateQualifierOrder(Style); +if (EC != ParseError::Success) + return make_error_code(EC); + } + if (!Style->InheritsParentConfig && !Style->ConfigFile.empty()) { +auto *FS = llvm::vfs::getRealFileSystem().get(); +assert(FS); +SmallString<128> ConfigFile{Style->ConfigFile}; +Style->ConfigFile.clear(); +switch (ConfigFile[0]) { +case '~': + llvm::sys::fs::expand_tilde(ConfigFile, ConfigFile); + break; +case '/': + break; +default: + llvm::sys::fs::make_absolute( + llvm::sys::path::parent_path(Config.getBufferIdentifier()), + ConfigFile); +} +if (!llvm::sys::fs::exists(ConfigFile)) { + llvm::errs() << ConfigFile << ": " << "file not found\n"; + return make_error_code(ParseError::Error); +} +const auto Text = loadAndParseConfigFile(ConfigFile, FS, Style, + AllowUnknownOptions, DiagHandler); +if (Text.getError()) + return make_error_code(ParseError::Error); + } return make_error_code(ParseError::Success); } ___ cfe-commits mailing list cfe-commits@lists.l
[clang] [clang-format] Add -ConfigFile option (PR #113864)
@@ -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()) { owenca wrote: ```suggestion if (!Style.InheritsParentConfig && !Style->ConfigFile.empty()) { ``` https://github.com/llvm/llvm-project/pull/113864 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add -ConfigFile option (PR #113864)
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 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 { 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> +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. ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add -ConfigFile option (PR #113864)
https://github.com/owenca converted_to_draft https://github.com/llvm/llvm-project/pull/113864 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add -ConfigFile option (PR #113864)
llvmbot wrote: @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 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 { 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> +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. `` https://github.com/llvm/llvm-project/pull/113864 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add -ConfigFile option (PR #113864)
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 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 { 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> +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