14
14
#include " clang/Lex/Preprocessor.h"
15
15
using namespace clang ;
16
16
17
+ std::pair<Preprocessor::CachedTokensTy::size_type, bool >
18
+ Preprocessor::LastBacktrackPos () {
19
+ assert (isBacktrackEnabled ());
20
+ auto BacktrackPos = BacktrackPositions.back ();
21
+ bool Unannotated =
22
+ static_cast <CachedTokensTy::difference_type>(BacktrackPos) < 0 ;
23
+ return {Unannotated ? ~BacktrackPos : BacktrackPos, Unannotated};
24
+ }
25
+
17
26
// EnableBacktrackAtThisPos - From the point that this method is called, and
18
27
// until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
19
28
// keeps track of the lexed tokens so that a subsequent Backtrack() call will
@@ -24,7 +33,7 @@ using namespace clang;
24
33
// be combined with the EnableBacktrackAtThisPos calls in reverse order.
25
34
void Preprocessor::EnableBacktrackAtThisPos (bool Unannotated) {
26
35
assert (LexLevel == 0 && " cannot use lookahead while lexing" );
27
- BacktrackPositions.push_back ((CachedLexPos << 1 ) | Unannotated );
36
+ BacktrackPositions.push_back (Unannotated ? ~CachedLexPos : CachedLexPos );
28
37
if (Unannotated)
29
38
UnannotatedBacktrackTokens.emplace_back (CachedTokens, CachedTokens.size ());
30
39
EnterCachingLexMode ();
@@ -45,23 +54,21 @@ Preprocessor::CachedTokensTy Preprocessor::PopUnannotatedBacktrackTokens() {
45
54
46
55
// Disable the last EnableBacktrackAtThisPos call.
47
56
void Preprocessor::CommitBacktrackedTokens () {
48
- assert (!BacktrackPositions.empty ()
49
- && " EnableBacktrackAtThisPos was not called!" );
50
- auto BacktrackPos = BacktrackPositions.back ();
57
+ assert (isBacktrackEnabled () && " EnableBacktrackAtThisPos was not called!" );
58
+ auto [BacktrackPos, Unannotated] = LastBacktrackPos ();
51
59
BacktrackPositions.pop_back ();
52
- if (BacktrackPos & 1 )
60
+ if (Unannotated )
53
61
PopUnannotatedBacktrackTokens ();
54
62
}
55
63
56
64
// Make Preprocessor re-lex the tokens that were lexed since
57
65
// EnableBacktrackAtThisPos() was previously called.
58
66
void Preprocessor::Backtrack () {
59
- assert (!BacktrackPositions.empty ()
60
- && " EnableBacktrackAtThisPos was not called!" );
61
- auto BacktrackPos = BacktrackPositions.back ();
67
+ assert (isBacktrackEnabled () && " EnableBacktrackAtThisPos was not called!" );
68
+ auto [BacktrackPos, Unannotated] = LastBacktrackPos ();
62
69
BacktrackPositions.pop_back ();
63
- CachedLexPos = BacktrackPos >> 1 ;
64
- if (BacktrackPos & 1 )
70
+ CachedLexPos = BacktrackPos;
71
+ if (Unannotated )
65
72
CachedTokens = PopUnannotatedBacktrackTokens ();
66
73
recomputeCurLexerKind ();
67
74
}
@@ -149,8 +156,7 @@ void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) {
149
156
for (CachedTokensTy::size_type i = CachedLexPos; i != 0 ; --i) {
150
157
CachedTokensTy::iterator AnnotBegin = CachedTokens.begin () + i-1 ;
151
158
if (AnnotBegin->getLocation () == Tok.getLocation ()) {
152
- assert ((BacktrackPositions.empty () ||
153
- (BacktrackPositions.back () >> 1 ) <= i) &&
159
+ assert ((!isBacktrackEnabled () || LastBacktrackPos ().first <= i) &&
154
160
" The backtrack pos points inside the annotated tokens!" );
155
161
// Replace the cached tokens with the single annotation token.
156
162
if (i < CachedLexPos)
0 commit comments