Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,10 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
FormatStyle::SIS_WithoutElse;
GoogleStyle.AllowShortLoopsOnASingleLine = true;
GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
// Abseil aliases to clang's `_Nonnull`, `_Nullable` and `_Null_unspecified`.
GoogleStyle.AttributeMacros.push_back("absl_nonnull");
GoogleStyle.AttributeMacros.push_back("absl_nullable");
GoogleStyle.AttributeMacros.push_back("absl_nullability_unknown");
GoogleStyle.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
GoogleStyle.DerivePointerAlignment = true;
GoogleStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
Expand Down
6 changes: 6 additions & 0 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,12 @@ TEST(ConfigParseTest, ParsesConfiguration) {
Style.AttributeMacros.clear();
CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
std::vector<std::string>{"__capability"});
Style.AttributeMacros.clear();
CHECK_PARSE(
"BasedOnStyle: Google", 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"}));

Expand Down
34 changes: 32 additions & 2 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12375,6 +12375,9 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyFormat("vector<a *_Nonnull> v;");
verifyFormat("vector<a *_Nullable> v;");
verifyFormat("vector<a *_Null_unspecified> v;");
verifyGoogleFormat("vector<a *absl_nonnull> v;");
verifyGoogleFormat("vector<a *absl_nullable> v;");
verifyGoogleFormat("vector<a *absl_nullability_unknown> v;");
verifyFormat("vector<a *__ptr32> v;");
verifyFormat("vector<a *__ptr64> v;");
verifyFormat("vector<a *__capability> v;");
Expand Down Expand Up @@ -12518,6 +12521,10 @@ 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);", getGoogleStyle());
verifyIndependentOfContext("MACRO(A *absl_nullable a);", getGoogleStyle());
verifyIndependentOfContext("MACRO(A *absl_nullability_unknown a);",
getGoogleStyle());
verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
Expand Down Expand Up @@ -12676,6 +12683,12 @@ TEST_F(FormatTest, UnderstandsAttributes) {
verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
verifyGoogleFormat("SomeType *absl_nonnull s(InitValue);");
verifyGoogleFormat("SomeType *absl_nonnull s{InitValue};");
verifyGoogleFormat("SomeType *absl_nullable s(InitValue);");
verifyGoogleFormat("SomeType *absl_nullable s{InitValue};");
verifyGoogleFormat("SomeType *absl_nullability_unknown s(InitValue);");
verifyGoogleFormat("SomeType *absl_nullability_unknown s{InitValue};");
}

TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
Expand All @@ -12687,7 +12700,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;");
verifyGoogleFormat("x = (foo *absl_nonnull)*v;");
verifyGoogleFormat("x = (foo *absl_nullable)*v;");
verifyGoogleFormat("x = (foo *absl_nullability_unknown)*v;");
verifyFormat("x = (foo *[[clang::attr]])*v;");
verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
verifyFormat("x = (foo *__ptr32)*v;");
Expand All @@ -12701,7 +12716,7 @@ 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 [[clang::attr]] __ptr32 __ptr64 __capability";
verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);

Expand All @@ -12720,6 +12735,21 @@ TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
CustomQualifier);

// Check additional attribute macros in Google style:
FormatStyle LongPointerRightGoogle = getGoogleStyleWithColumns(999);
FormatStyle LongPointerLeftGoogle = getGoogleStyleWithColumns(999);
LongPointerLeftGoogle.PointerAlignment = FormatStyle::PAS_Left;
Twine AllQualifiersPlusGoogle =
AllQualifiers + " absl_nonnull absl_nullable absl_nullability_unknown";
verifyFormat(("x = (foo *" + AllQualifiersPlusGoogle + ")*v;").str(),
LongPointerRightGoogle);
verifyFormat(("x = (foo* " + AllQualifiersPlusGoogle + ")*v;").str(),
LongPointerLeftGoogle);
verifyFormat(("x = (foo *" + AllQualifiersPlusGoogle + ")&v;").str(),
LongPointerRightGoogle);
verifyFormat(("x = (foo* " + AllQualifiersPlusGoogle + ")&v;").str(),
LongPointerLeftGoogle);

// Check that unknown identifiers result in binary operator parsing:
verifyFormat("x = (foo * __unknown_qualifier) * v;");
verifyFormat("x = (foo * __unknown_qualifier) & v;");
Expand Down