diff options
author | Krasimir Georgiev <krasimir@google.com> | 2017-03-02 09:54:44 +0000 |
---|---|---|
committer | Krasimir Georgiev <krasimir@google.com> | 2017-03-02 09:54:44 +0000 |
commit | 9163fe2aba8a146b7d5dc5b8e256a7fb10687245 (patch) | |
tree | fdd6f7ad78d4bc967793e64ac9f67b68fe39fcdb /clang/lib/Format/NamespaceEndCommentsFixer.cpp | |
parent | fb0dc6206e4f76c33d8d489351d7d11151a8eed4 (diff) | |
download | bcm5719-llvm-9163fe2aba8a146b7d5dc5b8e256a7fb10687245.tar.gz bcm5719-llvm-9163fe2aba8a146b7d5dc5b8e256a7fb10687245.zip |
[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
Diffstat (limited to 'clang/lib/Format/NamespaceEndCommentsFixer.cpp')
-rw-r--r-- | clang/lib/Format/NamespaceEndCommentsFixer.cpp | 13 |
1 files changed, 3 insertions, 10 deletions
diff --git a/clang/lib/Format/NamespaceEndCommentsFixer.cpp b/clang/lib/Format/NamespaceEndCommentsFixer.cpp index 495c34db754..578dcf7b670 100644 --- a/clang/lib/Format/NamespaceEndCommentsFixer.cpp +++ b/clang/lib/Format/NamespaceEndCommentsFixer.cpp @@ -23,7 +23,7 @@ namespace clang { 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 @@ std::string computeEndCommentText(StringRef NamespaceName, bool AddNewline) { 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 @@ tooling::Replacements NamespaceEndCommentsFixer::analyze( 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; } |