Author: Alex Richardson Date: 2020-09-02T18:35:21+01:00 New Revision: d70e05c9e36ada3ea6341764a3bc34de7de7d8dd
URL: https://github.com/llvm/llvm-project/commit/d70e05c9e36ada3ea6341764a3bc34de7de7d8dd DIFF: https://github.com/llvm/llvm-project/commit/d70e05c9e36ada3ea6341764a3bc34de7de7d8dd.diff LOG: [clang-format] Parse double-square attributes as pointer qualifiers Before: x = (foo *[[clang::attr]]) * v; After: x = (foo *[[clang::attr]])*v; Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D86721 Added: Modified: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index a9077500e041..f04f101f0459 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1840,6 +1840,12 @@ class AnnotatingParser { T = T->MatchingParen->Previous->Previous; continue; } + } else if (T->is(TT_AttributeSquare)) { + // Handle `x = (foo *[[clang::foo]])&v;`: + if (T->MatchingParen && T->MatchingParen->Previous) { + T = T->MatchingParen->Previous; + continue; + } } else if (T->canBePointerOrReferenceQualifier()) { T = T->Previous; continue; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index f2978cdbed8d..14c97784b738 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -8068,6 +8068,8 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); + verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); + verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); verifyFormat("void f() { f(float{1}, a * a); }"); // FIXME: Is there a way to make this work? @@ -8137,14 +8139,17 @@ TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { verifyFormat("x = (foo *_Nullable)*v;"); verifyFormat("x = (foo *_Null_unspecified)*v;"); verifyFormat("x = (foo *_Nonnull)*v;"); + verifyFormat("x = (foo *[[clang::attr]])*v;"); + verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); // Check that we handle multiple trailing qualifiers and skip them all to // determine that the expression is a cast to a pointer type. FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; - StringRef AllQualifiers = "const volatile restrict __attribute__((foo)) " - "_Nonnull _Null_unspecified _Nonnull"; + StringRef AllQualifiers = + "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " + "_Nonnull [[clang::attr]]"; verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits