[clang-format] Use number of unwrapped lines for short namespace
Summary:
This patch makes the namespace comment fixer use the number of unwrapped lines
that a namespace spans to detect it that namespace is short, thus not needing
end comments to be added.
This is needed to ensure clang-format is idempotent. Previously, a short namespace
was detected by the original source code lines. This has the effect of requiring two
runs for this example:
```
namespace { class A; }
```
after first run:
```
namespace {
class A;
}
```
after second run:
```
namespace {
class A;
} // namespace
```
Reviewers: djasper
Reviewed By: djasper
Subscribers: cfe-commits, klimek
Differential Revision: https://reviews.llvm.org/D30528
llvm-svn: 296736
diff --git a/clang/lib/Format/NamespaceEndCommentsFixer.cpp b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
index 495c34d..578dcf7 100644
--- a/clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -23,7 +23,7 @@
namespace format {
namespace {
-// The maximal number of lines that a short namespace spans.
+// The maximal number of unwrapped lines that a short namespace spans.
// Short namespaces don't need an end comment.
static const int kShortNamespaceMaxLines = 1;
@@ -60,14 +60,6 @@
return text;
}
-bool isShort(const FormatToken *NamespaceTok, const FormatToken *RBraceTok,
- const SourceManager &SourceMgr) {
- int StartLine =
- SourceMgr.getSpellingLineNumber(NamespaceTok->Tok.getLocation());
- int EndLine = SourceMgr.getSpellingLineNumber(RBraceTok->Tok.getLocation());
- return EndLine - StartLine + 1 <= kShortNamespaceMaxLines;
-}
-
bool hasEndComment(const FormatToken *RBraceTok) {
return RBraceTok->Next && RBraceTok->Next->is(tok::comment);
}
@@ -151,7 +143,8 @@
const std::string EndCommentText =
computeEndCommentText(NamespaceName, AddNewline);
if (!hasEndComment(RBraceTok)) {
- if (!isShort(NamespaceTok, RBraceTok, SourceMgr))
+ bool isShort = I - StartLineIndex <= kShortNamespaceMaxLines + 1;
+ if (!isShort)
addEndComment(RBraceTok, EndCommentText, SourceMgr, &Fixes);
continue;
}