Skip to content

Commit

Permalink
[clang-format] Make NamespaceEndCommentFixer add at most one comment
Browse files Browse the repository at this point in the history
Summary:
Until now, NamespaceEndCommentFixer was adding missing comments for every run,
which results in multiple end comments for:
```
namespace {
  int i;
  int j;
}
#if A
  int a = 1;
#else
  int a = 2;
#endif
```
result before:

```
namespace {
  int i;
  int j;
}// namespace // namespace
#if A
  int a = 1;
#else
  int a = 2;
#endif
```
result after:
```
namespace {
  int i;
  int j;
}// namespace
#if A
  int a = 1;
#else
  int a = 2;
#endif
```

Reviewers: djasper

Reviewed By: djasper

Subscribers: klimek, cfe-commits

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

llvm-svn: 297028
  • Loading branch information
krasimirgg committed Mar 6, 2017
1 parent adacd8f commit bda7739
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clang/lib/Format/NamespaceEndCommentsFixer.cpp
Expand Up @@ -135,7 +135,10 @@ tooling::Replacements NamespaceEndCommentsFixer::analyze(
NamespaceTok = NamespaceTok->getNextNonComment();
if (NamespaceTok->isNot(tok::kw_namespace))
continue;
const FormatToken *RBraceTok = EndLine->First;
FormatToken *RBraceTok = EndLine->First;
if (RBraceTok->Finalized)
continue;
RBraceTok->Finalized = true;
const std::string NamespaceName = computeName(NamespaceTok);
bool AddNewline = (I + 1 < E) &&
AnnotatedLines[I + 1]->First->NewlinesBefore == 0 &&
Expand Down
18 changes: 18 additions & 0 deletions clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
Expand Up @@ -388,6 +388,24 @@ TEST_F(NamespaceEndCommentsFixerTest,
" int i;\n"
"}\n"
"}\n"));
EXPECT_EQ("namespace {\n"
" int i;\n"
" int j;\n"
"}// namespace\n"
"#if A\n"
" int i;\n"
"#else\n"
" int j;\n"
"#endif",
fixNamespaceEndComments("namespace {\n"
" int i;\n"
" int j;\n"
"}\n"
"#if A\n"
" int i;\n"
"#else\n"
" int j;\n"
"#endif"));
}

TEST_F(NamespaceEndCommentsFixerTest,
Expand Down

0 comments on commit bda7739

Please sign in to comment.