diff options
author | Eric Liu <ioeric@google.com> | 2016-04-19 19:25:33 +0000 |
---|---|---|
committer | Eric Liu <ioeric@google.com> | 2016-04-19 19:25:33 +0000 |
commit | 89be04720148a342c73fd65aac88880f9edde09f (patch) | |
tree | 798d89d6c9a3162ab65ab69ddd33e166c8bb8a0d | |
parent | 50d4e989058d30813754c5dc6cb0988082b127e9 (diff) | |
download | bcm5719-llvm-89be04720148a342c73fd65aac88880f9edde09f.tar.gz bcm5719-llvm-89be04720148a342c73fd65aac88880f9edde09f.zip |
Fixed a bug in AnnotatedLine::startsWith when there are comments in the line.
Summary: When there are comments in the line, one token may be checked multiple times.
Reviewers: mprobst, djasper
Subscribers: ioeric, cfe-commits, klimek
Differential Revision: http://reviews.llvm.org/D19106
llvm-svn: 266803
-rw-r--r-- | clang/lib/Format/TokenAnnotator.h | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h index 5329f1f3f2f..141e8b4106a 100644 --- a/clang/lib/Format/TokenAnnotator.h +++ b/clang/lib/Format/TokenAnnotator.h @@ -83,7 +83,7 @@ public: /// \c true if this line starts with the given tokens in order, ignoring /// comments. template <typename... Ts> bool startsWith(Ts... Tokens) const { - return startsWith(First, Tokens...); + return startsWithInternal(First, Tokens...); } /// \c true if this line looks like a function definition instead of a @@ -124,15 +124,24 @@ private: void operator=(const AnnotatedLine &) = delete; template <typename A, typename... Ts> - bool startsWith(FormatToken *Tok, A K1) const { + bool startsWithInternal(const FormatToken *Tok, A K1) const { + // Even though we skip comments in the outer `startWithInternal` function, + // this loop is still necessary if it is invoked by the public interface + // `startsWith`. while (Tok && Tok->is(tok::comment)) Tok = Tok->Next; return Tok && Tok->is(K1); } template <typename A, typename... Ts> - bool startsWith(FormatToken *Tok, A K1, Ts... Tokens) const { - return startsWith(Tok, K1) && startsWith(Tok->Next, Tokens...); + bool startsWithInternal(const FormatToken *Tok, A K1, Ts... Tokens) const { + // Skip comments before calling `startsWithInternal(Tok, K1)` so that the + // second call to `startsWithInternal` takes the correct `Tok->Next`, which + // should be the next token of the token checked in the first call. + while (Tok && Tok->is(tok::comment)) + Tok = Tok->Next; + return Tok && startsWithInternal(Tok, K1) && + startsWithInternal(Tok->Next, Tokens...); } }; |