From c5227a1f53103d6c0cbcb5a59da84356208df0dd Mon Sep 17 00:00:00 2001 From: Francois Ferrand Date: Wed, 29 May 2019 16:30:47 +0000 Subject: [clang-format] Allow configuring list of function-like macros that resolve to a type Summary: Adds a `TypenameMacros` configuration option that causes certain identifiers to be handled in a way similar to `typeof()`. This is enough to: - Prevent misinterpreting declarations of pointers to such types as expressions (`STACK_OF(int) * foo` -> `STACK_OF(int) *foo`), - Avoid surprising line breaks in variable/struct field declarations (`STACK_OF(int)\nfoo;` -> `STACK_OF(int) foo;`, see https://bugs.llvm.org/show_bug.cgi?id=30353). Reviewers: Typz, krasimir, djasper Reviewed By: Typz Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D57184 llvm-svn: 361986 --- clang/unittests/Format/FormatTest.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'clang/unittests/Format/FormatTest.cpp') diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 31f40b16708..87405bccef1 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -13545,6 +13545,35 @@ TEST_F(FormatTest, GuessLanguageWithChildLines) { guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); } +TEST_F(FormatTest, TypenameMacros) { + std::vector TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; + + // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 + FormatStyle Google = getGoogleStyleWithColumns(0); + Google.TypenameMacros = TypenameMacros; + verifyFormat("struct foo {\n" + " int bar;\n" + " TAILQ_ENTRY(a) bleh;\n" + "};", Google); + + FormatStyle Macros = getLLVMStyle(); + Macros.TypenameMacros = TypenameMacros; + + verifyFormat("STACK_OF(int) a;", Macros); + verifyFormat("STACK_OF(int) *a;", Macros); + verifyFormat("STACK_OF(int const *) *a;", Macros); + verifyFormat("STACK_OF(int *const) *a;", Macros); + verifyFormat("STACK_OF(int, string) a;", Macros); + verifyFormat("STACK_OF(LIST(int)) a;", Macros); + verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); + verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); + verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); + + Macros.PointerAlignment = FormatStyle::PAS_Left; + verifyFormat("STACK_OF(int)* a;", Macros); + verifyFormat("STACK_OF(int*)* a;", Macros); +} + } // end namespace } // end namespace format } // end namespace clang -- cgit v1.2.3