Coverage Report

Created: 2018-12-11 17:59

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Lex/TokenLexer.h
Line
Count
Source
1
//===- TokenLexer.h - Lex from a token buffer -------------------*- C++ -*-===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file defines the TokenLexer interface.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_LEX_TOKENLEXER_H
15
#define LLVM_CLANG_LEX_TOKENLEXER_H
16
17
#include "clang/Basic/SourceLocation.h"
18
#include "llvm/ADT/ArrayRef.h"
19
20
namespace clang {
21
22
class MacroArgs;
23
class MacroInfo;
24
class Preprocessor;
25
class Token;
26
class VAOptExpansionContext;
27
28
/// TokenLexer - This implements a lexer that returns tokens from a macro body
29
/// or token stream instead of lexing from a character buffer.  This is used for
30
/// macro expansion and _Pragma handling, for example.
31
class TokenLexer {
32
  friend class Preprocessor;
33
34
  /// The macro we are expanding from. This is null if expanding a token stream.
35
  MacroInfo *Macro = nullptr;
36
37
  /// The actual arguments specified for a function-like macro, or null. The
38
  /// TokenLexer owns the pointed-to object.
39
  MacroArgs *ActualArgs = nullptr;
40
41
  /// The current preprocessor object we are expanding for.
42
  Preprocessor &PP;
43
44
  /// This is the pointer to an array of tokens that the macro is
45
  /// defined to, with arguments expanded for function-like macros.  If this is
46
  /// a token stream, these are the tokens we are returning.  This points into
47
  /// the macro definition we are lexing from, a cache buffer that is owned by
48
  /// the preprocessor, or some other buffer that we may or may not own
49
  /// (depending on OwnsTokens).
50
  /// Note that if it points into Preprocessor's cache buffer, the Preprocessor
51
  /// may update the pointer as needed.
52
  const Token *Tokens;
53
54
  /// This is the length of the Tokens array.
55
  unsigned NumTokens;
56
57
  /// This is the index of the next token that Lex will return.
58
  unsigned CurTokenIdx;
59
60
  /// The source location range where this macro was expanded.
61
  SourceLocation ExpandLocStart, ExpandLocEnd;
62
63
  /// Source location pointing at the source location entry chunk that
64
  /// was reserved for the current macro expansion.
65
  SourceLocation MacroExpansionStart;
66
67
  /// The offset of the macro expansion in the
68
  /// "source location address space".
69
  unsigned MacroStartSLocOffset;
70
71
  /// Location of the macro definition.
72
  SourceLocation MacroDefStart;
73
74
  /// Length of the macro definition.
75
  unsigned MacroDefLength;
76
77
  /// Lexical information about the expansion point of the macro: the identifier
78
  /// that the macro expanded from had these properties.
79
  bool AtStartOfLine : 1;
80
  bool HasLeadingSpace : 1;
81
82
  // When this is true, the next token appended to the
83
  // output list during function argument expansion will get a leading space,
84
  // regardless of whether it had one to begin with or not. This is used for
85
  // placemarker support. If still true after function argument expansion, the
86
  // leading space will be applied to the first token following the macro
87
  // expansion.
88
  bool NextTokGetsSpace : 1;
89
90
  /// This is true if this TokenLexer allocated the Tokens
91
  /// array, and thus needs to free it when destroyed.  For simple object-like
92
  /// macros (for example) we just point into the token buffer of the macro
93
  /// definition, we don't make a copy of it.
94
  bool OwnsTokens : 1;
95
96
  /// This is true when tokens lexed from the TokenLexer
97
  /// should not be subject to further macro expansion.
98
  bool DisableMacroExpansion : 1;
99
100
public:
101
  /// Create a TokenLexer for the specified macro with the specified actual
102
  /// arguments.  Note that this ctor takes ownership of the ActualArgs pointer.
103
  /// ILEnd specifies the location of the ')' for a function-like macro or the
104
  /// identifier for an object-like macro.
105
  TokenLexer(Token &Tok, SourceLocation ILEnd, MacroInfo *MI,
106
             MacroArgs *ActualArgs, Preprocessor &pp)
107
55.0k
      : PP(pp), OwnsTokens(false) {
108
55.0k
    Init(Tok, ILEnd, MI, ActualArgs);
109
55.0k
  }
110
111
  /// Create a TokenLexer for the specified token stream.  If 'OwnsTokens' is
112
  /// specified, this takes ownership of the tokens and delete[]'s them when
113
  /// the token lexer is empty.
114
  TokenLexer(const Token *TokArray, unsigned NumToks, bool DisableExpansion,
115
             bool ownsTokens, Preprocessor &pp)
116
764k
      : PP(pp), OwnsTokens(false) {
117
764k
    Init(TokArray, NumToks, DisableExpansion, ownsTokens);
118
764k
  }
119
120
  TokenLexer(const TokenLexer &) = delete;
121
  TokenLexer &operator=(const TokenLexer &) = delete;
122
741k
  ~TokenLexer() { destroy(); }
123
124
  /// Initialize this TokenLexer to expand from the specified macro
125
  /// with the specified argument information.  Note that this ctor takes
126
  /// ownership of the ActualArgs pointer.  ILEnd specifies the location of the
127
  /// ')' for a function-like macro or the identifier for an object-like macro.
128
  void Init(Token &Tok, SourceLocation ELEnd, MacroInfo *MI,
129
            MacroArgs *Actuals);
130
131
  /// Initialize this TokenLexer with the specified token stream.
132
  /// This does not take ownership of the specified token vector.
133
  ///
134
  /// DisableExpansion is true when macro expansion of tokens lexed from this
135
  /// stream should be disabled.
136
  void Init(const Token *TokArray, unsigned NumToks,
137
            bool DisableMacroExpansion, bool OwnsTokens);
138
139
  /// If the next token lexed will pop this macro off the
140
  /// expansion stack, return 2.  If the next unexpanded token is a '(', return
141
  /// 1, otherwise return 0.
142
  unsigned isNextTokenLParen() const;
143
144
  /// Lex and return a token from this macro stream.
145
  bool Lex(Token &Tok);
146
147
  /// isParsingPreprocessorDirective - Return true if we are in the middle of a
148
  /// preprocessor directive.
149
  bool isParsingPreprocessorDirective() const;
150
151
private:
152
  void destroy();
153
154
  /// Return true if the next lex call will pop this macro off the include
155
  /// stack.
156
392M
  bool isAtEnd() const {
157
392M
    return CurTokenIdx == NumTokens;
158
392M
  }
159
160
  /// Concatenates the next (sub-)sequence of \p Tokens separated by '##'
161
  /// starting with LHSTok - stopping when we encounter a token that is neither
162
  /// '##' nor preceded by '##'.  Places the result back into \p LHSTok and sets
163
  /// \p CurIdx to point to the token following the last one that was pasted.
164
  ///
165
  /// Also performs the MSVC extension wide-literal token pasting involved with:
166
  ///       \code L #macro-arg. \endcode
167
  ///
168
  /// \param[in,out] LHSTok - Contains the token to the left of '##' in \p
169
  /// Tokens upon entry and will contain the resulting concatenated Token upon
170
  /// exit.
171
  ///
172
  /// \param[in] TokenStream - The stream of Tokens we are lexing from.
173
  ///
174
  /// \param[in,out] CurIdx - Upon entry, \pTokens[\pCurIdx] must equal '##'
175
  /// (with the exception of the MSVC extension mentioned above).  Upon exit, it
176
  /// is set to the index of the token following the last token that was
177
  /// concatenated together.
178
  ///
179
  /// \returns If this returns true, the caller should immediately return the
180
  /// token.
181
  bool pasteTokens(Token &LHSTok, ArrayRef<Token> TokenStream,
182
                   unsigned int &CurIdx);
183
184
  /// Calls pasteTokens above, passing in the '*this' object's Tokens and
185
  /// CurTokenIdx data members.
186
  bool pasteTokens(Token &Tok);
187
188
189
  /// Takes the tail sequence of tokens within ReplacementToks that represent
190
  /// the just expanded __VA_OPT__ tokens (possibly zero tokens) and transforms
191
  /// them into a string.  \p VCtx is used to determine which token represents
192
  /// the first __VA_OPT__ replacement token.
193
  ///
194
  /// \param[in,out] ResultToks - Contains the current Replacement Tokens
195
  /// (prior to rescanning and token pasting), the tail end of which represents
196
  /// the tokens just expanded through __VA_OPT__ processing.  These (sub)
197
  /// sequence of tokens are folded into one stringified token.
198
  ///
199
  /// \param[in] VCtx - contains relevant contextual information about the
200
  /// state of the tokens around and including the __VA_OPT__ token, necessary
201
  /// for stringification.
202
  void stringifyVAOPTContents(SmallVectorImpl<Token> &ResultToks,
203
                              const VAOptExpansionContext &VCtx,
204
                              SourceLocation VAOPTClosingParenLoc);
205
206
  /// Expand the arguments of a function-like macro so that we can quickly
207
  /// return preexpanded tokens from Tokens.
208
  void ExpandFunctionArguments();
209
210
  /// In microsoft compatibility mode, /##/ pastes
211
  /// together to form a comment that comments out everything in the current
212
  /// macro, other active macros, and anything left on the current physical
213
  /// source line of the expanded buffer.  Handle this by returning the
214
  /// first token on the next line.
215
  void HandleMicrosoftCommentPaste(Token &Tok, SourceLocation OpLoc);
216
217
  /// If \p loc is a FileID and points inside the current macro
218
  /// definition, returns the appropriate source location pointing at the
219
  /// macro expansion source location entry.
220
  SourceLocation getExpansionLocForMacroDefLoc(SourceLocation loc) const;
221
222
  /// Creates SLocEntries and updates the locations of macro argument
223
  /// tokens to their new expanded locations.
224
  ///
225
  /// \param ArgIdSpellLoc the location of the macro argument id inside the
226
  /// macro definition.
227
  void updateLocForMacroArgTokens(SourceLocation ArgIdSpellLoc,
228
                                  Token *begin_tokens, Token *end_tokens);
229
230
  /// Remove comma ahead of __VA_ARGS__, if present, according to compiler
231
  /// dialect settings.  Returns true if the comma is removed.
232
  bool MaybeRemoveCommaBeforeVaArgs(SmallVectorImpl<Token> &ResultToks,
233
                                    bool HasPasteOperator,
234
                                    MacroInfo *Macro, unsigned MacroArgNo,
235
                                    Preprocessor &PP);
236
237
  void PropagateLineStartLeadingSpaceInfo(Token &Result);
238
};
239
240
} // namespace clang
241
242
#endif // LLVM_CLANG_LEX_TOKENLEXER_H