[clang] 6482383 - [clang-format] FixNamespaceComments does not understand namespace aliases
via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 14 06:53:34 PST 2021
Author: mydeveloperday
Date: 2021-12-14T14:53:04Z
New Revision: 6482383e50a4f02e337e7ce61a471e6295f008e4
URL: https://github.com/llvm/llvm-project/commit/6482383e50a4f02e337e7ce61a471e6295f008e4
DIFF: https://github.com/llvm/llvm-project/commit/6482383e50a4f02e337e7ce61a471e6295f008e4.diff
LOG: [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
Added:
Modified:
clang/lib/Format/NamespaceEndCommentsFixer.cpp
clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/NamespaceEndCommentsFixer.cpp b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
index def551f863cd..38ab5b9df76d 100644
--- a/clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -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();
}
diff --git a/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp b/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
index 9b0301930d21..3afe35e7ea34 100644
--- a/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ b/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -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
More information about the cfe-commits
mailing list