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