[clang-format] FixNamespaceComments does not understand namespace aliases

https://github.com/llvm/llvm-project/issues/35876

Ensure a namespace alias doesn't get incorrectly identifier as a namespace

Reviewed By: HazardyKnusperkeks, curdeius, owenpan

Differential Revision: https://reviews.llvm.org/D115647

Fixes: #35876
This commit is contained in:
mydeveloperday 2021-12-14 14:45:26 +00:00
parent ddfac0759c
commit 6482383e50
2 changed files with 81 additions and 1 deletions

View File

@ -180,9 +180,13 @@ getNamespaceToken(const AnnotatedLine *Line,
if (NamespaceTok->is(tok::l_brace)) {
// "namespace" keyword can be on the line preceding '{', e.g. in styles
// where BraceWrapping.AfterNamespace is true.
if (StartLineIndex > 0)
if (StartLineIndex > 0) {
NamespaceTok = AnnotatedLines[StartLineIndex - 1]->First;
if (AnnotatedLines[StartLineIndex - 1]->endsWith(tok::semi))
return nullptr;
}
}
return NamespaceTok->getNamespaceToken();
}

View File

@ -1185,6 +1185,82 @@ TEST_F(ShortNamespaceLinesTest, MultipleUnwrappedLine) {
"}\n",
Style));
}
TEST_F(ShortNamespaceLinesTest, NamespaceAlias) {
auto Style = getLLVMStyle();
EXPECT_EQ("namespace n = nn;\n"
"{\n"
" int i;\n"
" int j;\n"
"}\n",
fixNamespaceEndComments("namespace n = nn;\n"
"{\n"
" int i;\n"
" int j;\n"
"}\n",
Style));
EXPECT_EQ("namespace n = nn; // comment\n"
"{\n"
" int i;\n"
" int j;\n"
"}\n",
fixNamespaceEndComments("namespace n = nn; // comment\n"
"{\n"
" int i;\n"
" int j;\n"
"}\n",
Style));
EXPECT_EQ("namespace n = nn; /* comment */\n"
"{\n"
" int i;\n"
" int j;\n"
"}\n",
fixNamespaceEndComments("namespace n = nn; /* comment */\n"
"{\n"
" int i;\n"
" int j;\n"
"}\n",
Style));
EXPECT_EQ(
"namespace n = nn; /* comment */ /* comment2 */\n"
"{\n"
" int i;\n"
" int j;\n"
"}\n",
fixNamespaceEndComments("namespace n = nn; /* comment */ /* comment2 */\n"
"{\n"
" int i;\n"
" int j;\n"
"}\n",
Style));
EXPECT_EQ("namespace n = nn; {\n"
" int i;\n"
" int j;\n"
"}\n",
fixNamespaceEndComments("namespace n = nn; {\n"
" int i;\n"
" int j;\n"
"}\n",
Style));
EXPECT_EQ("int foo;\n"
"namespace n\n"
"{\n"
" int i;\n"
" int j;\n"
"}// namespace n\n",
fixNamespaceEndComments("int foo;\n"
"namespace n\n"
"{\n"
" int i;\n"
" int j;\n"
"}\n",
Style));
}
} // end namespace
} // end namespace format
} // end namespace clang