[PATCH] D80830: [clang-format] [PR46130] When editing a file with unbalance {} the namespace comment fixer can incorrectly comment the wrong closing brace
MyDeveloperDay via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sat May 30 05:16:59 PDT 2020
This revision was automatically updated to reflect the committed changes.
Closed by commit rG50bdd6073113: [clang-format] [PR46130] When editing a file with unbalance {} the namespace… (authored by MyDeveloperDay).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D80830/new/
https://reviews.llvm.org/D80830
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
@@ -1089,6 +1089,34 @@
"void d() {\n"
"}\n"));
}
+
+TEST_F(NamespaceEndCommentsFixerTest, IgnoreUnbalanced) {
+ EXPECT_EQ("namespace A {\n"
+ "class Foo {\n"
+ "}\n"
+ "}// namespace A\n",
+ fixNamespaceEndComments("namespace A {\n"
+ "class Foo {\n"
+ "}\n"
+ "}\n"));
+ EXPECT_EQ("namespace A {\n"
+ "class Foo {\n"
+ "}\n",
+ fixNamespaceEndComments("namespace A {\n"
+ "class Foo {\n"
+ "}\n"));
+
+ EXPECT_EQ("namespace A {\n"
+ "class Foo {\n"
+ "}\n"
+ "}\n"
+ "}\n",
+ fixNamespaceEndComments("namespace A {\n"
+ "class Foo {\n"
+ "}\n"
+ "}\n"
+ "}\n"));
+}
} // end namespace
} // end namespace format
} // end namespace clang
Index: clang/lib/Format/NamespaceEndCommentsFixer.cpp
===================================================================
--- clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -205,6 +205,23 @@
const SourceManager &SourceMgr = Env.getSourceManager();
AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
tooling::Replacements Fixes;
+
+ // Spin through the lines and ensure we have balanced braces.
+ int Braces = 0;
+ for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
+ FormatToken *Tok = AnnotatedLines[I]->First;
+ while (Tok) {
+ Braces += Tok->is(tok::l_brace) ? 1 : Tok->is(tok::r_brace) ? -1 : 0;
+ Tok = Tok->Next;
+ }
+ }
+ // Don't attempt to comment unbalanced braces or this can
+ // lead to comments being placed on the closing brace which isn't
+ // the matching brace of the namespace. (occurs during incomplete editing).
+ if (Braces != 0) {
+ return {Fixes, 0};
+ }
+
std::string AllNamespaceNames = "";
size_t StartLineIndex = SIZE_MAX;
StringRef NamespaceTokenText;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80830.267451.patch
Type: text/x-patch
Size: 2517 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200530/ea205673/attachment-0001.bin>
More information about the cfe-commits
mailing list