[PATCH] D115647: [clang-format] FixNamespaceComments does not understand namespace aliases
MyDeveloperDay via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 13 09:40:56 PST 2021
MyDeveloperDay created this revision.
MyDeveloperDay added reviewers: HazardyKnusperkeks, curdeius, owenpan.
MyDeveloperDay added projects: clang, clang-format.
Herald added a subscriber: jeroen.dobbelaere.
MyDeveloperDay requested review of this revision.
https://github.com/llvm/llvm-project/issues/35876
Ensure a namespace alias doesn't get incorrectly identifier as a namespace
namespace nn {}
void f() {
namespace n = nn;
{
int i;
int j;
} // namespace n=nn;
}
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D115647
Files:
clang/lib/Format/NamespaceEndCommentsFixer.cpp
clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
Index: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
===================================================================
--- clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -1185,6 +1185,22 @@
"}\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));
+}
} // end namespace
} // end namespace format
} // end namespace clang
Index: clang/lib/Format/NamespaceEndCommentsFixer.cpp
===================================================================
--- clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -24,11 +24,11 @@
namespace {
// Computes the name of a namespace given the namespace token.
// Returns "" for anonymous namespace.
-std::string computeName(const FormatToken *NamespaceTok) {
+bool computeName(const FormatToken *NamespaceTok, std::string &name) {
assert(NamespaceTok &&
NamespaceTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
"expecting a namespace token");
- std::string name = "";
+ name = "";
const FormatToken *Tok = NamespaceTok->getNextNonComment();
if (NamespaceTok->is(TT_NamespaceMacro)) {
// Collects all the non-comment tokens between opening parenthesis
@@ -55,10 +55,13 @@
name += Tok->TokenText;
if (Tok->is(tok::kw_inline))
name += " ";
+ // Namespace alias
+ if (Tok->isOneOf(tok::equal, tok::semi))
+ return false;
Tok = Tok->getNextNonComment();
}
}
- return name;
+ return true;
}
std::string computeEndCommentText(StringRef NamespaceName, bool AddNewline,
@@ -242,7 +245,13 @@
}
if (StartLineIndex == SIZE_MAX)
StartLineIndex = EndLine->MatchingOpeningBlockLineIndex;
- std::string NamespaceName = computeName(NamespaceTok);
+
+ std::string NamespaceName;
+ if (!computeName(NamespaceTok, NamespaceName)) {
+ // Its likely a namespace alias.
+ continue;
+ }
+
if (Style.CompactNamespaces) {
if (CompactedNamespacesCount == 0)
NamespaceTokenText = NamespaceTok->TokenText;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115647.393937.patch
Type: text/x-patch
Size: 2653 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211213/29ee982c/attachment.bin>
More information about the cfe-commits
mailing list