arichardson created this revision.
arichardson added reviewers: MyDeveloperDay, JakeMerdichAMD, sammccall,
curdeius.
Herald added subscribers: cfe-commits, jfb.
Herald added a project: clang.
arichardson requested review of this revision.
This change also comma-separated identifiers inside parentheses as a
possible function declaration.
When using the always break after return type setting:
Before:
SomeType funcdecl(SomeType);
SomeType funcdecl(SomeType, OtherType);
After:
SomeType
funcdecl(SomeType);
SomeType
funcdecl(SomeType, OtherType);
Depends on D87007 <https://reviews.llvm.org/D87007> (to apply cleanly)
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D87028
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -6683,10 +6683,69 @@
// All declarations and definitions should have the return type moved to its
// own line.
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
+ Style.NamespaceIndentation = FormatStyle::NI_All;
Style.TypenameMacros = {"LIST"};
verifyFormat("SomeType\n"
"funcdecl(LIST(uint64_t));",
Style);
+ verifyFormat("SomeType\n"
+ "funcdecl();\n"
+ "SomeType\n"
+ "funcdecl(SomeType paramname);\n"
+ "SomeType\n"
+ "funcdecl(_Atomic(uint64_t));\n"
+ "SomeType\n"
+ "funcdecl(SomeType param1, OtherType param2);\n"
+ // Also handle parameter lists declaration without names (but
+ // only at the top level, not inside functions
+ "SomeType\n"
+ "funcdecl(SomeType);\n"
+ "SomeType\n"
+ "funcdecl(SomeType, OtherType);\n"
+ // Check that any kind of expression/operator results in parsing
+ // it as a variable declaration and not a function
+ "SomeType vardecl(var + otherVar);\n"
+ "SomeType vardecl(func());\n"
+ "SomeType vardecl(someFunc(arg));\n"
+ "SomeType vardecl(var, var - otherVar);\n"
+ "SomeType x = var * funcdecl();\n"
+ "SomeType x = var * funcdecl(otherVar);\n"
+ "SomeType x = var * funcdecl(var, otherVar);\n"
+ "void\n"
+ "function_scope() {\n"
+ " SomeType x = var * funcdecl();\n"
+ " SomeType x = var * funcdecl(otherVar);\n"
+ " SomeType x = var * funcdecl(var, otherVar);\n"
+ // While clang will parse these as function declarations, at
+ // least for now clang-format assumes they are variables.
+ " SomeType *funcdecl();\n"
+ " SomeType *funcdecl(SomeType);\n"
+ " SomeType *funcdecl(SomeType, OtherType);\n"
+ "}\n"
+ "namespace namspace_scope {\n"
+ " SomeType\n"
+ " funcdecl();\n"
+ " SomeType\n"
+ " funcdecl(SomeType paramname);\n"
+ " SomeType\n"
+ " funcdecl(_Atomic(uint64_t));\n"
+ " SomeType\n"
+ " funcdecl(SomeType param1, OtherType param2);\n"
+ // Inside a namespace we should also parse these as a function
+ // declaration and not as a variable.
+ " SomeType\n"
+ " funcdecl(SomeType);\n"
+ " SomeType\n"
+ " funcdecl(SomeType, OtherType);\n"
+ " SomeType vardecl(var + otherVar);\n"
+ " SomeType vardecl(func());\n"
+ " SomeType vardecl(someFunc(arg));\n"
+ " SomeType vardecl(var, var - otherVar);\n"
+ " SomeType x = var * funcdecl();\n"
+ " SomeType x = var * funcdecl(otherVar);\n"
+ " SomeType x = var * funcdecl(var, otherVar);\n"
+ "} // namespace namspace_scope\n",
+ Style);
verifyFormat("class E {\n"
" int\n"
" f() {\n"
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2398,12 +2398,17 @@
if (Next->MatchingParen->Next &&
Next->MatchingParen->Next->is(TT_PointerOrReference))
return true;
+ // Treat cases where the parameter list only contains comma-separated
+ // identifiers as function declarations. For example:
+ // `SomeType funcdecl(OtherType)` or `SomeType funcdecl(Type1, Type2)`
+ bool CouldBeTypeList = true;
for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
Tok = Tok->Next) {
if (Tok->is(TT_TypeDeclarationParen))
return true;
if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
Tok = Tok->MatchingParen;
+ CouldBeTypeList = false;
continue;
}
if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
@@ -2412,8 +2417,10 @@
if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
Tok->Tok.isLiteral())
return false;
+ if (!Tok->isOneOf(tok::identifier, tok::comma))
+ CouldBeTypeList = false;
}
- return false;
+ return CouldBeTypeList;
}
bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits