llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang-format Author: Jan Voung (jvoung) <details> <summary>Changes</summary> Add support for formatting w/ absl nullability macros (https://github.com/abseil/abseil-cpp/blob/c52afac4f87ef76e6293b84874e5126a62be1f15/absl/base/nullability.h#L237). Example at https://godbolt.org/z/PYv19M1Gj input: ``` std::vector<int* _Nonnull> x; std::vector<int* absl_nonnull> y; ``` orig output: ``` std::vector<int* _Nonnull> x; std::vector<int * absl_nonnull> y; ``` new output: ``` std::vector<int* _Nonnull> x; std::vector<int* absl_nonnull> y; ``` credit to @<!-- -->ymand for the original patch --- Full diff: https://github.com/llvm/llvm-project/pull/130346.diff 3 Files Affected: - (modified) clang/lib/Format/Format.cpp (+4) - (modified) clang/unittests/Format/ConfigParseTest.cpp (+5-2) - (modified) clang/unittests/Format/FormatTest.cpp (+17-2) ``````````diff diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index b5f1241321891..1401b51586d03 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1508,6 +1508,10 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None; LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; LLVMStyle.AttributeMacros.push_back("__capability"); + // Abseil aliases to clang's `_Nonnull`, `_Nullable` and `_Null_unspecified`. + LLVMStyle.AttributeMacros.push_back("absl_nonnull"); + LLVMStyle.AttributeMacros.push_back("absl_nullable"); + LLVMStyle.AttributeMacros.push_back("absl_nullability_unknown"); LLVMStyle.BinPackArguments = true; LLVMStyle.BinPackLongBracedList = true; LLVMStyle.BinPackParameters = FormatStyle::BPPS_BinPack; diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index 273bab87b1ee1..5c789b21c306f 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -907,8 +907,11 @@ TEST(ConfigParseTest, ParsesConfiguration) { CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); Style.AttributeMacros.clear(); - CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, - std::vector<std::string>{"__capability"}); + CHECK_PARSE( + "BasedOnStyle: LLVM", AttributeMacros, + std::vector<std::string>({"__capability", "absl_nonnull", "absl_nullable", + "absl_nullability_unknown"})); + Style.AttributeMacros.clear(); CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, std::vector<std::string>({"attr1", "attr2"})); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index bd335f4b6a21b..a17cad0b67b08 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -12375,6 +12375,9 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyFormat("vector<a *_Nonnull> v;"); verifyFormat("vector<a *_Nullable> v;"); verifyFormat("vector<a *_Null_unspecified> v;"); + verifyFormat("vector<a *absl_nonnull> v;"); + verifyFormat("vector<a *absl_nullable> v;"); + verifyFormat("vector<a *absl_nullability_unknown> v;"); verifyFormat("vector<a *__ptr32> v;"); verifyFormat("vector<a *__ptr64> v;"); verifyFormat("vector<a *__capability> v;"); @@ -12518,6 +12521,9 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyIndependentOfContext("MACRO(A *_Nonnull a);"); verifyIndependentOfContext("MACRO(A *_Nullable a);"); verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); + verifyIndependentOfContext("MACRO(A *absl_nonnull a);"); + verifyIndependentOfContext("MACRO(A *absl_nullable a);"); + verifyIndependentOfContext("MACRO(A *absl_nullability_unknown a);"); verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); @@ -12674,6 +12680,12 @@ TEST_F(FormatTest, UnderstandsAttributes) { verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); + verifyFormat("SomeType *absl_nonnull s(InitValue);", CustomAttrs); + verifyFormat("SomeType *absl_nonnull s{InitValue};", CustomAttrs); + verifyFormat("SomeType *absl_nullable s(InitValue);", CustomAttrs); + verifyFormat("SomeType *absl_nullable s{InitValue};", CustomAttrs); + verifyFormat("SomeType *absl_nullability_unknown s(InitValue);", CustomAttrs); + verifyFormat("SomeType *absl_nullability_unknown s{InitValue};", CustomAttrs); verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); } @@ -12687,7 +12699,9 @@ TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { verifyFormat("x = (foo *_Nonnull)*v;"); verifyFormat("x = (foo *_Nullable)*v;"); verifyFormat("x = (foo *_Null_unspecified)*v;"); - verifyFormat("x = (foo *_Nonnull)*v;"); + verifyFormat("x = (foo *absl_nonnull)*v;"); + verifyFormat("x = (foo *absl_nullable)*v;"); + verifyFormat("x = (foo *absl_nullability_unknown)*v;"); verifyFormat("x = (foo *[[clang::attr]])*v;"); verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); verifyFormat("x = (foo *__ptr32)*v;"); @@ -12701,7 +12715,8 @@ TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; StringRef AllQualifiers = "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " - "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; + "_Nullable absl_nonnull absl_nullable absl_nullability_unknown " + "[[clang::attr]] __ptr32 __ptr64 __capability"; verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); `````````` </details> https://github.com/llvm/llvm-project/pull/130346 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits