-
Notifications
You must be signed in to change notification settings - Fork 14.2k
[clang-format] Fix a bug in annotating function declaration names #76206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Annotates function declaration names having unnamed parameters.
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesAnnotates function declaration names having unnamed parameters. Full diff: https://github.com/llvm/llvm-project/pull/76206.diff 2 Files Affected:
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index f3551af3424396..3ac3aa3c5e3a22 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3403,7 +3403,8 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
continue;
}
if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
- Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
+ Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis,
+ TT_TypeName)) {
return true;
}
if (Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) || Tok->Tok.isLiteral())
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 8e6935319b2f3d..2cafc0438ffb46 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1718,6 +1718,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
ASSERT_EQ(Tokens.size(), 14u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionTypeLParen);
+
+ auto Style = getLLVMStyle();
+ Style.TypeNames.push_back("time_t");
+ Tokens = annotate("int iso_time(time_t);", Style);
+ ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[3], tok::identifier, TT_TypeName);
}
TEST_F(TokenAnnotatorTest, UnderstandsCtorAndDtorDeclNames) {
|
See here for background info. |
Thanks for doing this. Does it mean we would have to add every type we use to our config? It would be much more convenient if clang-format treated the argument list the same way that it treats in definitions. For example printf "int\niso_time(time_t) { return 1; }\n" | clang-format -style='{ AlwaysBreakAfterReturnType: All }' gives int
iso_time(time_t) {
return 1;
} whereas printf "int\niso_time(time_t);\n" | clang-format -style='{ AlwaysBreakAfterReturnType: All }' will need a config edit to work. Other examples that are currently failing to line break: printf "int\niso_time(struct example);\n" | clang-format -style='{ AlwaysBreakAfterReturnType: All }'
printf "typedef long long int rowid_t;\nint\niso_time(rowid_t);\n" | clang-format -style='{ AlwaysBreakAfterReturnType: All }' |
|
This is C so I guess it can only be a function declaration. Anyway, it's minor to work around, thanks again. |
This boils down to, that for |
@mattmundell you don't need to add |
Annotates function declaration names having unnamed parameters.