diff options
author | Eric Liu <ioeric@google.com> | 2016-04-25 15:09:22 +0000 |
---|---|---|
committer | Eric Liu <ioeric@google.com> | 2016-04-25 15:09:22 +0000 |
commit | a4337997778b3fc144ac76b876a9eb06e6079dea (patch) | |
tree | ad8c03e65765a2f10307e86ca58f847b225308da /lib/Format/TokenAnnotator.h | |
parent | 20ce81cb411f5d369513a778ca3384f174a6fb2d (diff) |
Added Fixer implementation and fix() interface in clang-format for removing redundant code.
Summary:
After applying replacements, redundant code like extra commas or empty namespaces
might be introduced. Fixer can detect and remove any redundant code introduced by replacements.
The current implementation only handles redundant commas.
Reviewers: djasper, klimek
Subscribers: ioeric, mprobst, klimek, cfe-commits
Differential Revision: http://reviews.llvm.org/D18551
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@267416 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/TokenAnnotator.h')
-rw-r--r-- | lib/Format/TokenAnnotator.h | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/Format/TokenAnnotator.h b/lib/Format/TokenAnnotator.h index 141e8b4106..dfe1d1ddeb 100644 --- a/lib/Format/TokenAnnotator.h +++ b/lib/Format/TokenAnnotator.h @@ -86,6 +86,14 @@ public: return startsWithInternal(First, Tokens...); } + /// \c true if this line ends with the given tokens in reversed order, + /// ignoring comments. + /// For example, given tokens [T1, T2, T3, ...], the function returns true if + /// this line is like "... T3 T2 T1". + template <typename... Ts> bool endsWith(Ts... Tokens) const { + return endsWithInternal(Last, Tokens...); + } + /// \c true if this line looks like a function definition instead of a /// function declaration. Asserts MightBeFunctionDecl. bool mightBeFunctionDefinition() const { @@ -143,6 +151,23 @@ private: return Tok && startsWithInternal(Tok, K1) && startsWithInternal(Tok->Next, Tokens...); } + + template <typename A, typename... Ts> + bool endsWithInternal(const FormatToken *Tok, A K1) const { + // See the comments above in `startsWithInternal(Tok, K1)`. + while (Tok && Tok->is(tok::comment)) + Tok = Tok->Previous; + return Tok && Tok->is(K1); + } + + template <typename A, typename... Ts> + bool endsWithInternal(const FormatToken *Tok, A K1, Ts... Tokens) const { + // See the comments above in `startsWithInternal(Tok, K1, Tokens)`. + while (Tok && Tok->is(tok::comment)) + Tok = Tok->Previous; + return Tok && endsWithInternal(Tok, K1) && + endsWithInternal(Tok->Previous, Tokens...); + } }; /// \brief Determines extra information about the tokens comprising an |