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; 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