clang 23.0.0git
Lexer.h
Go to the documentation of this file.
1//===- Lexer.h - C Language Family Lexer ------------------------*- 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 Lexer interface.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LEX_LEXER_H
14#define LLVM_CLANG_LEX_LEXER_H
15
21#include "clang/Lex/Token.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringRef.h"
24#include <cassert>
25#include <cstdint>
26#include <optional>
27#include <string>
28
29namespace llvm {
30
31class MemoryBufferRef;
32
33} // namespace llvm
34
35namespace clang {
36
38class Preprocessor;
39class SourceManager;
40class LangOptions;
41
42/// ConflictMarkerKind - Kinds of conflict marker which the lexer might be
43/// recovering from.
45 /// Not within a conflict marker.
47
48 /// A normal or diff3 conflict marker, initiated by at least 7 "<"s,
49 /// separated by at least 7 "="s or "|"s, and terminated by at least 7 ">"s.
51
52 /// A Perforce-style conflict marker, initiated by 4 ">"s,
53 /// separated by 4 "="s, and terminated by 4 "<"s.
55};
56
57/// Describes the bounds (start, size) of the preamble and a flag required by
58/// PreprocessorOptions::PrecompiledPreambleBytes.
59/// The preamble includes the BOM, if any.
61 /// Size of the preamble in bytes.
62 unsigned Size;
63
64 /// Whether the preamble ends at the start of a new line.
65 ///
66 /// Used to inform the lexer as to whether it's starting at the beginning of
67 /// a line after skipping the preamble.
69
72};
73
74/// Lexer - This provides a simple interface that turns a text buffer into a
75/// stream of tokens. This provides no support for file reading or buffering,
76/// or buffering/seeking of tokens, only forward lexing is supported. It relies
77/// on the specified Preprocessor object to handle preprocessor directives, etc.
78class Lexer : public PreprocessorLexer {
79 friend class Preprocessor;
80
81 void anchor() override;
82
83 //===--------------------------------------------------------------------===//
84 // Constant configuration values for this lexer.
85
86 // Start of the buffer.
87 const char *BufferStart;
88
89 // End of the buffer.
90 const char *BufferEnd;
91
92 // Location for start of file.
93 SourceLocation FileLoc;
94
95 // LangOpts enabled by this language.
96 // Storing LangOptions as reference here is important from performance point
97 // of view. Lack of reference means that LangOptions copy constructor would be
98 // called by Lexer(..., const LangOptions &LangOpts,...). Given that local
99 // Lexer objects are created thousands times (in Lexer::getRawToken,
100 // Preprocessor::EnterSourceFile and other places) during single module
101 // processing in frontend it would make std::vector<std::string> copy
102 // constructors surprisingly hot.
103 const LangOptions &LangOpts;
104
105 // True if '//' line comments are enabled.
106 bool LineComment;
107
108 // True if lexer for _Pragma handling.
109 bool Is_PragmaLexer;
110
111 //===--------------------------------------------------------------------===//
112 // Context-specific lexing flags set by the preprocessor.
113 //
114
115 /// ExtendedTokenMode - The lexer can optionally keep comments and whitespace
116 /// and return them as tokens. This is used for -C and -CC modes, and
117 /// whitespace preservation can be useful for some clients that want to lex
118 /// the file in raw mode and get every character from the file.
119 ///
120 /// When this is set to 2 it returns comments and whitespace. When set to 1
121 /// it returns comments, when it is set to 0 it returns normal tokens only.
122 unsigned char ExtendedTokenMode;
123
124 //===--------------------------------------------------------------------===//
125 // Context that changes as the file is lexed.
126 // NOTE: any state that mutates when in raw mode must have save/restore code
127 // in Lexer::peekNextPPToken.
128
129 // BufferPtr - Current pointer into the buffer. This is the next character
130 // to be lexed.
131 const char *BufferPtr;
132
133 // IsAtStartOfLine - True if the next lexed token should get the "start of
134 // line" flag set on it.
135 bool IsAtStartOfLine;
136
137 bool IsAtPhysicalStartOfLine;
138
139 bool HasLeadingSpace;
140
141 bool HasLeadingEmptyMacro;
142
143 /// True if this is the first time we're lexing the input file.
144 bool IsFirstTimeLexingFile;
145
146 // NewLinePtr - A pointer to new line character '\n' being lexed. For '\r\n',
147 // it also points to '\n.'
148 const char *NewLinePtr;
149
150 // CurrentConflictMarkerState - The kind of conflict marker we are handling.
151 ConflictMarkerKind CurrentConflictMarkerState;
152
153 /// Non-empty if this \p Lexer is \p isDependencyDirectivesLexer().
155
156 /// If this \p Lexer is \p isDependencyDirectivesLexer(), it represents the
157 /// next token to use from the current dependency directive.
158 unsigned NextDepDirectiveTokenIndex = 0;
159
160 void InitLexer(const char *BufStart, const char *BufPtr, const char *BufEnd);
161
162public:
163 /// Lexer constructor - Create a new lexer object for the specified buffer
164 /// with the specified preprocessor managing the lexing process. This lexer
165 /// assumes that the associated file buffer and Preprocessor objects will
166 /// outlive it, so it doesn't take ownership of either of them.
167 Lexer(FileID FID, const llvm::MemoryBufferRef &InputFile, Preprocessor &PP,
168 bool IsFirstIncludeOfFile = true);
169
170 /// Lexer constructor - Create a new raw lexer object. This object is only
171 /// suitable for calls to 'LexFromRawLexer'. This lexer assumes that the
172 /// text range will outlive it, so it doesn't take ownership of it.
173 Lexer(SourceLocation FileLoc, const LangOptions &LangOpts,
174 const char *BufStart, const char *BufPtr, const char *BufEnd,
175 bool IsFirstIncludeOfFile = true);
176
177 /// Lexer constructor - Create a new raw lexer object. This object is only
178 /// suitable for calls to 'LexFromRawLexer'. This lexer assumes that the
179 /// text range will outlive it, so it doesn't take ownership of it.
180 Lexer(FileID FID, const llvm::MemoryBufferRef &FromFile,
181 const SourceManager &SM, const LangOptions &LangOpts,
182 bool IsFirstIncludeOfFile = true);
183
184 Lexer(const Lexer &) = delete;
185 Lexer &operator=(const Lexer &) = delete;
186
187 /// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
188 /// _Pragma expansion. This has a variety of magic semantics that this method
189 /// sets up. It returns a new'd Lexer that must be delete'd when done.
190 static Lexer *Create_PragmaLexer(SourceLocation SpellingLoc,
191 SourceLocation ExpansionLocStart,
192 SourceLocation ExpansionLocEnd,
193 unsigned TokLen, Preprocessor &PP);
194
195 /// getFileLoc - Return the File Location for the file we are lexing out of.
196 /// The physical location encodes the location where the characters come from,
197 /// the virtual location encodes where we should *claim* the characters came
198 /// from. Currently this is only used by _Pragma handling.
199 SourceLocation getFileLoc() const { return FileLoc; }
200
201 /// Lex - Return the next token in the file. If this is the end of file, it
202 /// return the tok::eof token. This implicitly involves the preprocessor.
203 bool Lex(Token &Result);
204
205private:
206 /// Called when the preprocessor is in 'dependency scanning lexing mode'.
207 bool LexDependencyDirectiveToken(Token &Result);
208
209 /// Called when the preprocessor is in 'dependency scanning lexing mode' and
210 /// is skipping a conditional block.
211 bool LexDependencyDirectiveTokenWhileSkipping(Token &Result);
212
213 /// True when the preprocessor is in 'dependency scanning lexing mode' and
214 /// created this \p Lexer for lexing a set of dependency directive tokens.
215 bool isDependencyDirectivesLexer() const { return !DepDirectives.empty(); }
216
217 /// Initializes \p Result with data from \p DDTok and advances \p BufferPtr to
218 /// the position just after the token.
219 /// \returns the buffer pointer at the beginning of the token.
220 const char *convertDependencyDirectiveToken(
221 const dependency_directives_scan::Token &DDTok, Token &Result);
222
223public:
224 /// isPragmaLexer - Returns true if this Lexer is being used to lex a pragma.
225 bool isPragmaLexer() const { return Is_PragmaLexer; }
226
227private:
228 /// IndirectLex - An indirect call to 'Lex' that can be invoked via
229 /// the PreprocessorLexer interface.
230 void IndirectLex(Token &Result) override { Lex(Result); }
231
232public:
233 /// LexFromRawLexer - Lex a token from a designated raw lexer (one with no
234 /// associated preprocessor object. Return true if the 'next character to
235 /// read' pointer points at the end of the lexer buffer, false otherwise.
237 assert(LexingRawMode && "Not already in raw mode!");
238 Lex(Result);
239 // Note that lexing to the end of the buffer doesn't implicitly delete the
240 // lexer when in raw mode.
241 return BufferPtr == BufferEnd;
242 }
243
244 /// isKeepWhitespaceMode - Return true if the lexer should return tokens for
245 /// every character in the file, including whitespace and comments. This
246 /// should only be used in raw mode, as the preprocessor is not prepared to
247 /// deal with the excess tokens.
248 bool isKeepWhitespaceMode() const {
249 return ExtendedTokenMode > 1;
250 }
251
252 /// SetKeepWhitespaceMode - This method lets clients enable or disable
253 /// whitespace retention mode.
254 void SetKeepWhitespaceMode(bool Val) {
255 assert((!Val || LexingRawMode || LangOpts.TraditionalCPP) &&
256 "Can only retain whitespace in raw mode or -traditional-cpp");
257 ExtendedTokenMode = Val ? 2 : 0;
258 }
259
260 /// inKeepCommentMode - Return true if the lexer should return comments as
261 /// tokens.
262 bool inKeepCommentMode() const {
263 return ExtendedTokenMode > 0;
264 }
265
266 /// SetCommentRetentionMode - Change the comment retention mode of the lexer
267 /// to the specified mode. This is really only useful when lexing in raw
268 /// mode, because otherwise the lexer needs to manage this.
269 void SetCommentRetentionState(bool Mode) {
270 assert(!isKeepWhitespaceMode() &&
271 "Can't play with comment retention state when retaining whitespace");
272 ExtendedTokenMode = Mode ? 1 : 0;
273 }
274
275 /// Sets the extended token mode back to its initial value, according to the
276 /// language options and preprocessor. This controls whether the lexer
277 /// produces comment and whitespace tokens.
278 ///
279 /// This requires the lexer to have an associated preprocessor. A standalone
280 /// lexer has nothing to reset to.
282
283 /// Gets source code buffer.
284 StringRef getBuffer() const {
285 return StringRef(BufferStart, BufferEnd - BufferStart);
286 }
287
288 /// ReadToEndOfLine - Read the rest of the current preprocessor line as an
289 /// uninterpreted string. This switches the lexer out of directive mode.
291
292
293 /// Diag - Forwarding function for diagnostics. This translate a source
294 /// position in the current buffer into a SourceLocation object for rendering.
295 DiagnosticBuilder Diag(const char *Loc, unsigned DiagID) const;
296
297 /// getSourceLocation - Return a source location identifier for the specified
298 /// offset in the current file.
299 SourceLocation getSourceLocation(const char *Loc, unsigned TokLen = 1) const;
300
301 /// getSourceLocation - Return a source location for the next character in
302 /// the current file.
304 return getSourceLocation(BufferPtr);
305 }
306
307 /// Return the current location in the buffer.
308 const char *getBufferLocation() const { return BufferPtr; }
309
310 /// Returns the current lexing offset.
312 assert(BufferPtr >= BufferStart && "Invalid buffer state");
313 return BufferPtr - BufferStart;
314 }
315
316 /// Set the lexer's buffer pointer to \p Offset.
317 void seek(unsigned Offset, bool IsAtStartOfLine);
318
319 /// Stringify - Convert the specified string into a C string by i) escaping
320 /// '\\' and " characters and ii) replacing newline character(s) with "\\n".
321 /// If Charify is true, this escapes the ' character instead of ".
322 static std::string Stringify(StringRef Str, bool Charify = false);
323
324 /// Stringify - Convert the specified string into a C string by i) escaping
325 /// '\\' and " characters and ii) replacing newline character(s) with "\\n".
326 static void Stringify(SmallVectorImpl<char> &Str);
327
328 /// getSpelling - This method is used to get the spelling of a token into a
329 /// preallocated buffer, instead of as an std::string. The caller is required
330 /// to allocate enough space for the token, which is guaranteed to be at least
331 /// Tok.getLength() bytes long. The length of the actual result is returned.
332 ///
333 /// Note that this method may do two possible things: it may either fill in
334 /// the buffer specified with characters, or it may *change the input pointer*
335 /// to point to a constant buffer with the data already in it (avoiding a
336 /// copy). The caller is not allowed to modify the returned buffer pointer
337 /// if an internal buffer is returned.
338 static unsigned getSpelling(const Token &Tok, const char *&Buffer,
339 const SourceManager &SourceMgr,
340 const LangOptions &LangOpts,
341 bool *Invalid = nullptr);
342
343 /// getSpelling() - Return the 'spelling' of the Tok token. The spelling of a
344 /// token is the characters used to represent the token in the source file
345 /// after trigraph expansion and escaped-newline folding. In particular, this
346 /// wants to get the true, uncanonicalized, spelling of things like digraphs
347 /// UCNs, etc.
348 static std::string getSpelling(const Token &Tok,
349 const SourceManager &SourceMgr,
350 const LangOptions &LangOpts,
351 bool *Invalid = nullptr);
352
353 /// getSpelling - This method is used to get the spelling of the
354 /// token at the given source location. If, as is usually true, it
355 /// is not necessary to copy any data, then the returned string may
356 /// not point into the provided buffer.
357 ///
358 /// This method lexes at the expansion depth of the given
359 /// location and does not jump to the expansion or spelling
360 /// location.
361 static StringRef getSpelling(SourceLocation loc,
362 SmallVectorImpl<char> &buffer,
363 const SourceManager &SM,
364 const LangOptions &options,
365 bool *invalid = nullptr);
366
367 /// MeasureTokenLength - Relex the token at the specified location and return
368 /// its length in bytes in the input file. If the token needs cleaning (e.g.
369 /// includes a trigraph or an escaped newline) then this count includes bytes
370 /// that are part of that.
371 static unsigned MeasureTokenLength(SourceLocation Loc,
372 const SourceManager &SM,
373 const LangOptions &LangOpts);
374
375 /// Finds the end of an identifier-continuation sequence starting at \p Loc.
376 /// This consumes identifier continuation characters (letters, digits,
377 /// underscores, dollar signs if enabled, UCNs, and unicode), and returns
378 /// the source location immediately after the consumed sequence.
379 static SourceLocation
381 const LangOptions &LangOpts);
382
383 /// Relex the token at the specified location.
384 /// \returns true if there was a failure, false on success.
385 static bool getRawToken(SourceLocation Loc, Token &Result,
386 const SourceManager &SM,
387 const LangOptions &LangOpts,
388 bool IgnoreWhiteSpace = false);
389
390 /// Given a location any where in a source buffer, find the location
391 /// that corresponds to the beginning of the token in which the original
392 /// source location lands.
394 const SourceManager &SM,
395 const LangOptions &LangOpts);
396
397 /// Get the physical length (including trigraphs and escaped newlines) of the
398 /// first \p Characters characters of the token starting at TokStart.
399 static unsigned getTokenPrefixLength(SourceLocation TokStart,
400 unsigned CharNo,
401 const SourceManager &SM,
402 const LangOptions &LangOpts);
403
404 /// AdvanceToTokenCharacter - If the current SourceLocation specifies a
405 /// location at the start of a token, return a new location that specifies a
406 /// character within the token. This handles trigraphs and escaped newlines.
408 unsigned Characters,
409 const SourceManager &SM,
410 const LangOptions &LangOpts) {
411 return TokStart.getLocWithOffset(
412 getTokenPrefixLength(TokStart, Characters, SM, LangOpts));
413 }
414
415 /// Computes the source location just past the end of the
416 /// token at this source location.
417 ///
418 /// This routine can be used to produce a source location that
419 /// points just past the end of the token referenced by \p Loc, and
420 /// is generally used when a diagnostic needs to point just after a
421 /// token where it expected something different that it received. If
422 /// the returned source location would not be meaningful (e.g., if
423 /// it points into a macro), this routine returns an invalid
424 /// source location.
425 ///
426 /// \param Offset an offset from the end of the token, where the source
427 /// location should refer to. The default offset (0) produces a source
428 /// location pointing just past the end of the token; an offset of 1 produces
429 /// a source location pointing to the last character in the token, etc.
430 static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset,
431 const SourceManager &SM,
432 const LangOptions &LangOpts);
433
434 /// Given a token range, produce a corresponding CharSourceRange that
435 /// is not a token range. This allows the source range to be used by
436 /// components that don't have access to the lexer and thus can't find the
437 /// end of the range for themselves.
439 const SourceManager &SM,
440 const LangOptions &LangOpts) {
441 SourceLocation End = getLocForEndOfToken(Range.getEnd(), 0, SM, LangOpts);
442 return End.isInvalid() ? CharSourceRange()
444 Range.getBegin(), End);
445 }
447 const SourceManager &SM,
448 const LangOptions &LangOpts) {
449 return Range.isTokenRange()
450 ? getAsCharRange(Range.getAsRange(), SM, LangOpts)
451 : Range;
452 }
453
454 /// Returns true if the given MacroID location points at the first
455 /// token of the macro expansion.
456 ///
457 /// \param MacroBegin If non-null and function returns true, it is set to
458 /// begin location of the macro.
460 const SourceManager &SM,
461 const LangOptions &LangOpts,
462 SourceLocation *MacroBegin = nullptr);
463
464 /// Returns true if the given MacroID location points at the last
465 /// token of the macro expansion.
466 ///
467 /// \param MacroEnd If non-null and function returns true, it is set to
468 /// end location of the macro.
470 const SourceManager &SM,
471 const LangOptions &LangOpts,
472 SourceLocation *MacroEnd = nullptr);
473
474 /// Accepts a range and returns a character range with file locations.
475 ///
476 /// Returns a null range if a part of the range resides inside a macro
477 /// expansion or the range does not reside on the same FileID.
478 ///
479 /// This function is trying to deal with macros and return a range based on
480 /// file locations. The cases where it can successfully handle macros are:
481 ///
482 /// -begin or end range lies at the start or end of a macro expansion, in
483 /// which case the location will be set to the expansion point, e.g:
484 /// \#define M 1 2
485 /// a M
486 /// If you have a range [a, 2] (where 2 came from the macro), the function
487 /// will return a range for "a M"
488 /// if you have range [a, 1], the function will fail because the range
489 /// overlaps with only a part of the macro
490 ///
491 /// -The macro is a function macro and the range can be mapped to the macro
492 /// arguments, e.g:
493 /// \#define M 1 2
494 /// \#define FM(x) x
495 /// FM(a b M)
496 /// if you have range [b, 2], the function will return the file range "b M"
497 /// inside the macro arguments.
498 /// if you have range [a, 2], the function will return the file range
499 /// "FM(a b M)" since the range includes all of the macro expansion.
501 const SourceManager &SM,
502 const LangOptions &LangOpts);
503
504 /// Returns a string for the source that the range encompasses.
505 static StringRef getSourceText(CharSourceRange Range,
506 const SourceManager &SM,
507 const LangOptions &LangOpts,
508 bool *Invalid = nullptr);
509
510 /// Retrieve the name of the immediate macro expansion.
511 ///
512 /// This routine starts from a source location, and finds the name of the macro
513 /// responsible for its immediate expansion. It looks through any intervening
514 /// macro argument expansions to compute this. It returns a StringRef which
515 /// refers to the SourceManager-owned buffer of the source where that macro
516 /// name is spelled. Thus, the result shouldn't out-live that SourceManager.
517 static StringRef getImmediateMacroName(SourceLocation Loc,
518 const SourceManager &SM,
519 const LangOptions &LangOpts);
520
521 /// Retrieve the name of the immediate macro expansion.
522 ///
523 /// This routine starts from a source location, and finds the name of the
524 /// macro responsible for its immediate expansion. It looks through any
525 /// intervening macro argument expansions to compute this. It returns a
526 /// StringRef which refers to the SourceManager-owned buffer of the source
527 /// where that macro name is spelled. Thus, the result shouldn't out-live
528 /// that SourceManager.
529 ///
530 /// This differs from Lexer::getImmediateMacroName in that any macro argument
531 /// location will result in the topmost function macro that accepted it.
532 /// e.g.
533 /// \code
534 /// MAC1( MAC2(foo) )
535 /// \endcode
536 /// for location of 'foo' token, this function will return "MAC1" while
537 /// Lexer::getImmediateMacroName will return "MAC2".
539 SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts);
540
541 /// Compute the preamble of the given file.
542 ///
543 /// The preamble of a file contains the initial comments, include directives,
544 /// and other preprocessor directives that occur before the code in this
545 /// particular file actually begins. The preamble of the main source file is
546 /// a potential prefix header.
547 ///
548 /// \param Buffer The memory buffer containing the file's contents.
549 ///
550 /// \param MaxLines If non-zero, restrict the length of the preamble
551 /// to fewer than this number of lines.
552 ///
553 /// \returns The offset into the file where the preamble ends and the rest
554 /// of the file begins along with a boolean value indicating whether
555 /// the preamble ends at the beginning of a new line.
556 static PreambleBounds ComputePreamble(StringRef Buffer,
557 const LangOptions &LangOpts,
558 unsigned MaxLines = 0);
559
560 /// Finds the token that comes right after the given location.
561 ///
562 /// Returns the next token, or std::nullopt if the location is inside a macro.
563 static std::optional<Token> findNextToken(SourceLocation Loc,
564 const SourceManager &SM,
565 const LangOptions &LangOpts,
566 bool IncludeComments = false);
567
568 /// Finds the token that comes before the given location.
569 static std::optional<Token> findPreviousToken(SourceLocation Loc,
570 const SourceManager &SM,
571 const LangOptions &LangOpts,
572 bool IncludeComments);
573
574 /// Checks that the given token is the first token that occurs after
575 /// the given location (this excludes comments and whitespace). Returns the
576 /// location immediately after the specified token. If the token is not found
577 /// or the location is inside a macro, the returned source location will be
578 /// invalid.
580 tok::TokenKind TKind,
581 const SourceManager &SM,
582 const LangOptions &LangOpts,
583 bool SkipTrailingWhitespaceAndNewLine);
584
585 /// Returns true if the given character could appear in an identifier.
586 static bool isAsciiIdentifierContinueChar(char c,
587 const LangOptions &LangOpts);
588
589 /// Checks whether new line pointed by Str is preceded by escape
590 /// sequence.
591 static bool isNewLineEscaped(const char *BufferStart, const char *Str);
592
593 /// getEscapedNewLineSize - Return the size of the specified escaped newline,
594 /// or 0 if it is not an escaped newline. P[-1] is known to be a "\" on entry
595 /// to this function.
596 static unsigned getEscapedNewLineSize(const char *P);
597
598 /// Diagnose use of a delimited or named escape sequence.
600 bool Named,
601 const LangOptions &Opts,
602 DiagnosticsEngine &Diags);
603
604 /// Represents a char and the number of bytes parsed to produce it.
605 struct SizedChar {
606 char Char;
607 unsigned Size;
608 };
609
610 /// getCharAndSizeNoWarn - Like the getCharAndSize method, but does not ever
611 /// emit a warning.
612 static inline SizedChar getCharAndSizeNoWarn(const char *Ptr,
613 const LangOptions &LangOpts) {
614 // If this is not a trigraph and not a UCN or escaped newline, return
615 // quickly.
616 if (isObviouslySimpleCharacter(Ptr[0])) {
617 return {*Ptr, 1u};
618 }
619
620 return getCharAndSizeSlowNoWarn(Ptr, LangOpts);
621 }
622
623 /// Returns the leading whitespace for line that corresponds to the given
624 /// location \p Loc.
625 static StringRef getIndentationForLine(SourceLocation Loc,
626 const SourceManager &SM);
627
628 /// Check if this is the first time we're lexing the input file.
629 bool isFirstTimeLexingFile() const { return IsFirstTimeLexingFile; }
630
631private:
632 //===--------------------------------------------------------------------===//
633 // Internal implementation interfaces.
634
635 /// LexTokenInternal - Internal interface to lex a preprocessing token. Called
636 /// by Lex.
637 ///
638 bool LexTokenInternal(Token &Result);
639
640 bool CheckUnicodeWhitespace(Token &Result, uint32_t C, const char *CurPtr);
641
642 bool LexUnicodeIdentifierStart(Token &Result, uint32_t C, const char *CurPtr);
643
644 /// FormTokenWithChars - When we lex a token, we have identified a span
645 /// starting at BufferPtr, going to TokEnd that forms the token. This method
646 /// takes that range and assigns it to the token as its location and size. In
647 /// addition, since tokens cannot overlap, this also updates BufferPtr to be
648 /// TokEnd.
649 void FormTokenWithChars(Token &Result, const char *TokEnd,
650 tok::TokenKind Kind) {
651 unsigned TokLen = TokEnd-BufferPtr;
652 Result.setLength(TokLen);
653 Result.setLocation(getSourceLocation(BufferPtr, TokLen));
654 Result.setKind(Kind);
655 BufferPtr = TokEnd;
656 }
657
658 /// peekNextPPToken - Return std::nullopt if there are no more tokens in the
659 /// buffer controlled by this lexer, otherwise return the next unexpanded
660 /// token.
661 std::optional<Token> peekNextPPToken();
662
663 //===--------------------------------------------------------------------===//
664 // Lexer character reading interfaces.
665
666 // This lexer is built on two interfaces for reading characters, both of which
667 // automatically provide phase 1/2 translation. getAndAdvanceChar is used
668 // when we know that we will be reading a character from the input buffer and
669 // that this character will be part of the result token. This occurs in (f.e.)
670 // string processing, because we know we need to read until we find the
671 // closing '"' character.
672 //
673 // The second interface is the combination of getCharAndSize with
674 // ConsumeChar. getCharAndSize reads a phase 1/2 translated character,
675 // returning it and its size. If the lexer decides that this character is
676 // part of the current token, it calls ConsumeChar on it. This two stage
677 // approach allows us to emit diagnostics for characters (e.g. warnings about
678 // trigraphs), knowing that they only are emitted if the character is
679 // consumed.
680
681 /// isObviouslySimpleCharacter - Return true if the specified character is
682 /// obviously the same in translation phase 1 and translation phase 3. This
683 /// can return false for characters that end up being the same, but it will
684 /// never return true for something that needs to be mapped.
685 static bool isObviouslySimpleCharacter(char C) {
686 return C != '?' && C != '\\';
687 }
688
689 /// getAndAdvanceChar - Read a single 'character' from the specified buffer,
690 /// advance over it, and return it. This is tricky in several cases. Here we
691 /// just handle the trivial case and fall-back to the non-inlined
692 /// getCharAndSizeSlow method to handle the hard case.
693 inline char getAndAdvanceChar(const char *&Ptr, Token &Tok) {
694 // If this is not a trigraph and not a UCN or escaped newline, return
695 // quickly.
696 if (isObviouslySimpleCharacter(Ptr[0])) return *Ptr++;
697
698 auto [C, Size] = getCharAndSizeSlow(Ptr, &Tok);
699 Ptr += Size;
700 return C;
701 }
702
703 /// ConsumeChar - When a character (identified by getCharAndSize) is consumed
704 /// and added to a given token, check to see if there are diagnostics that
705 /// need to be emitted or flags that need to be set on the token. If so, do
706 /// it.
707 const char *ConsumeChar(const char *Ptr, unsigned Size, Token &Tok) {
708 // Normal case, we consumed exactly one token. Just return it.
709 if (Size == 1)
710 return Ptr+Size;
711
712 // Otherwise, re-lex the character with a current token, allowing
713 // diagnostics to be emitted and flags to be set.
714 return Ptr + getCharAndSizeSlow(Ptr, &Tok).Size;
715 }
716
717 /// getCharAndSize - Peek a single 'character' from the specified buffer,
718 /// get its size, and return it. This is tricky in several cases. Here we
719 /// just handle the trivial case and fall-back to the non-inlined
720 /// getCharAndSizeSlow method to handle the hard case.
721 inline char getCharAndSize(const char *Ptr, unsigned &Size) {
722 // If this is not a trigraph and not a UCN or escaped newline, return
723 // quickly.
724 if (isObviouslySimpleCharacter(Ptr[0])) {
725 Size = 1;
726 return *Ptr;
727 }
728
729 auto CharAndSize = getCharAndSizeSlow(Ptr);
730 Size = CharAndSize.Size;
731 return CharAndSize.Char;
732 }
733
734 /// getCharAndSizeSlow - Handle the slow/uncommon case of the getCharAndSize
735 /// method.
736 SizedChar getCharAndSizeSlow(const char *Ptr, Token *Tok = nullptr);
737
738 /// SkipEscapedNewLines - If P points to an escaped newline (or a series of
739 /// them), skip over them and return the first non-escaped-newline found,
740 /// otherwise return P.
741 static const char *SkipEscapedNewLines(const char *P);
742
743 /// getCharAndSizeSlowNoWarn - Same as getCharAndSizeSlow, but never emits a
744 /// diagnostic.
745 static SizedChar getCharAndSizeSlowNoWarn(const char *Ptr,
746 const LangOptions &LangOpts);
747
748 //===--------------------------------------------------------------------===//
749 // Other lexer functions.
750
751 void SetByteOffset(unsigned Offset, bool StartOfLine);
752
753 void PropagateLineStartLeadingSpaceInfo(Token &Result);
754
755 const char *LexUDSuffix(Token &Result, const char *CurPtr,
756 bool IsStringLiteral);
757
758 // Helper functions to lex the remainder of a token of the specific type.
759
760 // This function handles both ASCII and Unicode identifiers after
761 // the first codepoint of the identifyier has been parsed.
762 bool LexIdentifierContinue(Token &Result, const char *CurPtr);
763
764 bool LexNumericConstant (Token &Result, const char *CurPtr);
765 bool LexStringLiteral (Token &Result, const char *CurPtr,
766 tok::TokenKind Kind);
767 bool LexRawStringLiteral (Token &Result, const char *CurPtr,
768 tok::TokenKind Kind);
769 bool LexAngledStringLiteral(Token &Result, const char *CurPtr);
770 bool LexCharConstant (Token &Result, const char *CurPtr,
771 tok::TokenKind Kind);
772 bool LexEndOfFile (Token &Result, const char *CurPtr);
773 bool SkipWhitespace(Token &Result, const char *CurPtr);
774 bool SkipLineComment(Token &Result, const char *CurPtr);
775 bool SkipBlockComment(Token &Result, const char *CurPtr);
776 bool SaveLineComment (Token &Result, const char *CurPtr);
777
778 bool IsStartOfConflictMarker(const char *CurPtr);
779 bool HandleEndOfConflictMarker(const char *CurPtr);
780
781 bool lexEditorPlaceholder(Token &Result, const char *CurPtr);
782
783 bool isCodeCompletionPoint(const char *CurPtr) const;
784 void cutOffLexing() { BufferPtr = BufferEnd; }
785
786 bool isHexaLiteral(const char *Start, const LangOptions &LangOpts);
787
788 void codeCompleteIncludedFile(const char *PathStart,
789 const char *CompletionPoint, bool IsAngled);
790
791 std::optional<uint32_t>
792 tryReadNumericUCN(const char *&StartPtr, const char *SlashLoc, Token *Result);
793 std::optional<uint32_t> tryReadNamedUCN(const char *&StartPtr,
794 const char *SlashLoc, Token *Result);
795
796 /// Read a universal character name.
797 ///
798 /// \param StartPtr The position in the source buffer after the initial '\'.
799 /// If the UCN is syntactically well-formed (but not
800 /// necessarily valid), this parameter will be updated to
801 /// point to the character after the UCN.
802 /// \param SlashLoc The position in the source buffer of the '\'.
803 /// \param Result The token being formed. Pass \c nullptr to suppress
804 /// diagnostics and handle token formation in the caller.
805 ///
806 /// \return The Unicode codepoint specified by the UCN, or 0 if the UCN is
807 /// invalid.
808 uint32_t tryReadUCN(const char *&StartPtr, const char *SlashLoc, Token *Result);
809
810 /// Try to consume a UCN as part of an identifier at the current
811 /// location.
812 /// \param CurPtr Initially points to the range of characters in the source
813 /// buffer containing the '\'. Updated to point past the end of
814 /// the UCN on success.
815 /// \param Size The number of characters occupied by the '\' (including
816 /// trigraphs and escaped newlines).
817 /// \param Result The token being produced. Marked as containing a UCN on
818 /// success.
819 /// \return \c true if a UCN was lexed and it produced an acceptable
820 /// identifier character, \c false otherwise.
821 bool tryConsumeIdentifierUCN(const char *&CurPtr, unsigned Size,
822 Token &Result);
823
824 /// Try to consume an identifier character encoded in UTF-8.
825 /// \param CurPtr Points to the start of the (potential) UTF-8 code unit
826 /// sequence. On success, updated to point past the end of it.
827 /// \param Result The token being formed.
828 /// \return \c true if a UTF-8 sequence mapping to an acceptable identifier
829 /// character was lexed, \c false otherwise.
830 bool tryConsumeIdentifierUTF8Char(const char *&CurPtr, Token &Result);
831};
832
833} // namespace clang
834
835#endif // LLVM_CLANG_LEX_LEXER_H
This is the interface for scanning header and source files to get the minimum necessary preprocessor ...
Token Tok
The Token.
Result
Implement __builtin_bit_cast and related operations.
Defines the clang::LangOptions interface.
#define SM(sm)
Defines the PreprocessorLexer interface.
Defines the clang::SourceLocation class and associated facilities.
Defines the clang::TokenKind enum and support functions.
__device__ __2f16 float c
Represents a byte-granular source range.
static CharSourceRange getCharRange(SourceRange R)
A little helper class used to produce diagnostics.
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:233
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
static StringRef getSourceText(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts, bool *Invalid=nullptr)
Returns a string for the source that the range encompasses.
Definition Lexer.cpp:1075
friend class Preprocessor
Definition Lexer.h:79
void SetKeepWhitespaceMode(bool Val)
SetKeepWhitespaceMode - This method lets clients enable or disable whitespace retention mode.
Definition Lexer.h:254
static SourceLocation findLocationAfterToken(SourceLocation loc, tok::TokenKind TKind, const SourceManager &SM, const LangOptions &LangOpts, bool SkipTrailingWhitespaceAndNewLine)
Checks that the given token is the first token that occurs after the given location (this excludes co...
Definition Lexer.cpp:1432
static CharSourceRange getAsCharRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Definition Lexer.h:446
bool LexFromRawLexer(Token &Result)
LexFromRawLexer - Lex a token from a designated raw lexer (one with no associated preprocessor object...
Definition Lexer.h:236
static unsigned getEscapedNewLineSize(const char *P)
getEscapedNewLineSize - Return the size of the specified escaped newline, or 0 if it is not an escape...
Definition Lexer.cpp:1331
bool inKeepCommentMode() const
inKeepCommentMode - Return true if the lexer should return comments as tokens.
Definition Lexer.h:262
void SetCommentRetentionState(bool Mode)
SetCommentRetentionMode - Change the comment retention mode of the lexer to the specified mode.
Definition Lexer.h:269
static std::optional< Token > findPreviousToken(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts, bool IncludeComments)
Finds the token that comes before the given location.
Definition Lexer.cpp:1407
void seek(unsigned Offset, bool IsAtStartOfLine)
Set the lexer's buffer pointer to Offset.
Definition Lexer.cpp:288
static StringRef getImmediateMacroName(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Retrieve the name of the immediate macro expansion.
Definition Lexer.cpp:1111
void ReadToEndOfLine(SmallVectorImpl< char > *Result=nullptr)
ReadToEndOfLine - Read the rest of the current preprocessor line as an uninterpreted string.
Definition Lexer.cpp:3139
static CharSourceRange getAsCharRange(SourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Given a token range, produce a corresponding CharSourceRange that is not a token range.
Definition Lexer.h:438
static bool isAtStartOfMacroExpansion(SourceLocation loc, const SourceManager &SM, const LangOptions &LangOpts, SourceLocation *MacroBegin=nullptr)
Returns true if the given MacroID location points at the first token of the macro expansion.
Definition Lexer.cpp:912
static SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, unsigned Characters, const SourceManager &SM, const LangOptions &LangOpts)
AdvanceToTokenCharacter - If the current SourceLocation specifies a location at the start of a token,...
Definition Lexer.h:407
DiagnosticBuilder Diag(const char *Loc, unsigned DiagID) const
Diag - Forwarding function for diagnostics.
Definition Lexer.cpp:1283
StringRef getBuffer() const
Gets source code buffer.
Definition Lexer.h:284
const char * getBufferLocation() const
Return the current location in the buffer.
Definition Lexer.h:308
bool Lex(Token &Result)
Lex - Return the next token in the file.
Definition Lexer.cpp:3758
bool isPragmaLexer() const
isPragmaLexer - Returns true if this Lexer is being used to lex a pragma.
Definition Lexer.h:225
static void DiagnoseDelimitedOrNamedEscapeSequence(SourceLocation Loc, bool Named, const LangOptions &Opts, DiagnosticsEngine &Diags)
Diagnose use of a delimited or named escape sequence.
Definition Lexer.cpp:3444
static unsigned getTokenPrefixLength(SourceLocation TokStart, unsigned CharNo, const SourceManager &SM, const LangOptions &LangOpts)
Get the physical length (including trigraphs and escaped newlines) of the first Characters characters...
Definition Lexer.cpp:823
Lexer(FileID FID, const llvm::MemoryBufferRef &InputFile, Preprocessor &PP, bool IsFirstIncludeOfFile=true)
Lexer constructor - Create a new lexer object for the specified buffer with the specified preprocesso...
Definition Lexer.cpp:194
static bool isAtEndOfMacroExpansion(SourceLocation loc, const SourceManager &SM, const LangOptions &LangOpts, SourceLocation *MacroEnd=nullptr)
Returns true if the given MacroID location points at the last token of the macro expansion.
Definition Lexer.cpp:934
SourceLocation getSourceLocation() override
getSourceLocation - Return a source location for the next character in the current file.
Definition Lexer.h:303
static CharSourceRange makeFileCharRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Accepts a range and returns a character range with file locations.
Definition Lexer.cpp:1006
unsigned getCurrentBufferOffset()
Returns the current lexing offset.
Definition Lexer.h:311
static bool isNewLineEscaped(const char *BufferStart, const char *Str)
Checks whether new line pointed by Str is preceded by escape sequence.
Definition Lexer.cpp:1189
SourceLocation getFileLoc() const
getFileLoc - Return the File Location for the file we are lexing out of.
Definition Lexer.h:199
static StringRef getIndentationForLine(SourceLocation Loc, const SourceManager &SM)
Returns the leading whitespace for line that corresponds to the given location Loc.
Definition Lexer.cpp:1209
static unsigned getSpelling(const Token &Tok, const char *&Buffer, const SourceManager &SourceMgr, const LangOptions &LangOpts, bool *Invalid=nullptr)
getSpelling - This method is used to get the spelling of a token into a preallocated buffer,...
Definition Lexer.cpp:462
Lexer & operator=(const Lexer &)=delete
bool isKeepWhitespaceMode() const
isKeepWhitespaceMode - Return true if the lexer should return tokens for every character in the file,...
Definition Lexer.h:248
static bool isAsciiIdentifierContinueChar(char c, const LangOptions &LangOpts)
Returns true if the given character could appear in an identifier.
Definition Lexer.cpp:1185
static SourceLocation findEndOfIdentifierContinuation(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Finds the end of an identifier-continuation sequence starting at Loc.
Definition Lexer.cpp:518
static std::optional< Token > findNextToken(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts, bool IncludeComments=false)
Finds the token that comes right after the given location.
Definition Lexer.cpp:1376
static unsigned MeasureTokenLength(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
MeasureTokenLength - Relex the token at the specified location and return its length in bytes in the ...
Definition Lexer.cpp:509
static SourceLocation GetBeginningOfToken(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Given a location any where in a source buffer, find the location that corresponds to the beginning of...
Definition Lexer.cpp:642
void resetExtendedTokenMode()
Sets the extended token mode back to its initial value, according to the language options and preproc...
Definition Lexer.cpp:230
static StringRef getImmediateMacroNameForDiagnostics(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Retrieve the name of the immediate macro expansion.
Definition Lexer.cpp:1158
static Lexer * Create_PragmaLexer(SourceLocation SpellingLoc, SourceLocation ExpansionLocStart, SourceLocation ExpansionLocEnd, unsigned TokLen, Preprocessor &PP)
Create_PragmaLexer: Lexer constructor - Create a new lexer object for _Pragma expansion.
Definition Lexer.cpp:253
static PreambleBounds ComputePreamble(StringRef Buffer, const LangOptions &LangOpts, unsigned MaxLines=0)
Compute the preamble of the given file.
Definition Lexer.cpp:669
Lexer(const Lexer &)=delete
static bool getRawToken(SourceLocation Loc, Token &Result, const SourceManager &SM, const LangOptions &LangOpts, bool IgnoreWhiteSpace=false)
Relex the token at the specified location.
Definition Lexer.cpp:543
static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset, const SourceManager &SM, const LangOptions &LangOpts)
Computes the source location just past the end of the token at this source location.
Definition Lexer.cpp:882
static std::string Stringify(StringRef Str, bool Charify=false)
Stringify - Convert the specified string into a C string by i) escaping '\' and " characters and ii) ...
Definition Lexer.cpp:320
static SizedChar getCharAndSizeNoWarn(const char *Ptr, const LangOptions &LangOpts)
getCharAndSizeNoWarn - Like the getCharAndSize method, but does not ever emit a warning.
Definition Lexer.h:612
bool isFirstTimeLexingFile() const
Check if this is the first time we're lexing the input file.
Definition Lexer.h:629
bool LexingRawMode
True if in raw mode.
const FileID FID
The SourceManager FileID corresponding to the file being lexed.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
Token - This structure provides full information about a lexed token.
Definition Token.h:36
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition TokenKinds.h:25
The JSON file list parser is used to communicate input to InstallAPI.
ConflictMarkerKind
ConflictMarkerKind - Kinds of conflict marker which the lexer might be recovering from.
Definition Lexer.h:44
@ CMK_Perforce
A Perforce-style conflict marker, initiated by 4 ">"s, separated by 4 "="s, and terminated by 4 "<"s.
Definition Lexer.h:54
@ CMK_None
Not within a conflict marker.
Definition Lexer.h:46
@ CMK_Normal
A normal or diff3 conflict marker, initiated by at least 7 "<"s, separated by at least 7 "="s or "|"s...
Definition Lexer.h:50
@ Result
The result type of a method or function.
Definition TypeBase.h:905
unsigned int uint32_t
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
Represents a char and the number of bytes parsed to produce it.
Definition Lexer.h:605
Describes the bounds (start, size) of the preamble and a flag required by PreprocessorOptions::Precom...
Definition Lexer.h:60
unsigned Size
Size of the preamble in bytes.
Definition Lexer.h:62
bool PreambleEndsAtStartOfLine
Whether the preamble ends at the start of a new line.
Definition Lexer.h:68
PreambleBounds(unsigned Size, bool PreambleEndsAtStartOfLine)
Definition Lexer.h:70