15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Lexer.h - C Language Family Lexer ----------------------*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file defines the Lexer interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#ifndef LLVM_CLANG_LEX_LEXER_H
15176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#define LLVM_CLANG_LEX_LEXER_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/LangOptions.h"
1830a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Lex/PreprocessorLexer.h"
19d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner#include "llvm/ADT/SmallVector.h"
209dc62f044a6ba21f503bd56607d94b32704e7945Chris Lattner#include <cassert>
2130a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include <string>
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
24d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikieclass DiagnosticsEngine;
259a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattnerclass SourceManager;
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass Preprocessor;
273cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattnerclass DiagnosticBuilder;
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
29d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// ConflictMarkerKind - Kinds of conflict marker which the lexer might be
30d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith/// recovering from.
31d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smithenum ConflictMarkerKind {
32d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  /// Not within a conflict marker.
33d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  CMK_None,
3464f9b500987394c69cd9a2f5483d06b5cafb0824Dmitri Gribenko  /// A normal or diff3 conflict marker, initiated by at least 7 "<"s,
3564f9b500987394c69cd9a2f5483d06b5cafb0824Dmitri Gribenko  /// separated by at least 7 "="s or "|"s, and terminated by at least 7 ">"s.
36d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  CMK_Normal,
3764f9b500987394c69cd9a2f5483d06b5cafb0824Dmitri Gribenko  /// A Perforce-style conflict marker, initiated by 4 ">"s,
3864f9b500987394c69cd9a2f5483d06b5cafb0824Dmitri Gribenko  /// separated by 4 "="s, and terminated by 4 "<"s.
39d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  CMK_Perforce
40d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith};
41d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// Lexer - This provides a simple interface that turns a text buffer into a
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// stream of tokens.  This provides no support for file reading or buffering,
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// or buffering/seeking of tokens, only forward lexing is supported.  It relies
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// on the specified Preprocessor object to handle preprocessor directives, etc.
460e977de1eacfbc143bdedad87c14b53814159023Ted Kremenekclass Lexer : public PreprocessorLexer {
47651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void anchor() override;
4899ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Constant configuration values for this lexer.
51448cec4c1c3705f6f49ffdefb58a7329942a2dd8Chris Lattner  const char *BufferStart;       // Start of the buffer.
5225bdb51276d3bfc180a68688082071505a00ed27Chris Lattner  const char *BufferEnd;         // End of the buffer.
539dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  SourceLocation FileLoc;        // Location for start of file.
544e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  LangOptions LangOpts;          // LangOpts enabled by this language (cache).
55d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  bool Is_PragmaLexer;           // True if lexer for _Pragma handling.
5681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Context-specific lexing flags set by the preprocessor.
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //
601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// ExtendedTokenMode - The lexer can optionally keep comments and whitespace
62d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// and return them as tokens.  This is used for -C and -CC modes, and
63d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// whitespace preservation can be useful for some clients that want to lex
64d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// the file in raw mode and get every character from the file.
65d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  ///
66d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// When this is set to 2 it returns comments and whitespace.  When set to 1
67d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// it returns comments, when it is set to 0 it returns normal tokens only.
68d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  unsigned char ExtendedTokenMode;
691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Context that changes as the file is lexed.
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // NOTE: any state that mutates when in raw mode must have save/restore code
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // in Lexer::isNextPPTokenLParen.
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // BufferPtr - Current pointer into the buffer.  This is the next character
765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // to be lexed.
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const char *BufferPtr;
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // IsAtStartOfLine - True if the next lexed token should get the "start of
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // line" flag set on it.
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool IsAtStartOfLine;
821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
83d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool IsAtPhysicalStartOfLine;
84d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman
85d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool HasLeadingSpace;
86d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman
87d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool HasLeadingEmptyMacro;
88d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman
89d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  // CurrentConflictMarkerState - The kind of conflict marker we are handling.
90d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith  ConflictMarkerKind CurrentConflictMarkerState;
91d5e1d606f8c22ebda17c6fbf952f8c1696428758Richard Smith
920e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  Lexer(const Lexer &) = delete;
930e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void operator=(const Lexer &) = delete;
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  friend class Preprocessor;
951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9622d91ca8d7c134eac5cc6a4869e6a84c461ad624Chris Lattner  void InitLexer(const char *BufStart, const char *BufPtr, const char *BufEnd);
975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Lexer constructor - Create a new lexer object for the specified buffer
1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// with the specified preprocessor managing the lexing process.  This lexer
101168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  /// assumes that the associated file buffer and Preprocessor objects will
10225bdb51276d3bfc180a68688082071505a00ed27Chris Lattner  /// outlive it, so it doesn't take ownership of either of them.
1036e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner  Lexer(FileID FID, const llvm::MemoryBuffer *InputBuffer, Preprocessor &PP);
10442e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner
105168ae2d44a443da75ea85db5f3b5081eb0bce113Chris Lattner  /// Lexer constructor - Create a new raw lexer object.  This object is only
106092bf67e5ca560d2fc6aa70be1f172b8b3a5ff96Dmitri Gribenko  /// suitable for calls to 'LexFromRawLexer'.  This lexer assumes that the
107092bf67e5ca560d2fc6aa70be1f172b8b3a5ff96Dmitri Gribenko  /// text range will outlive it, so it doesn't take ownership of it.
1084e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  Lexer(SourceLocation FileLoc, const LangOptions &LangOpts,
109de96c0f29c4cacabe6ea577c61db87c2a85aea6cChris Lattner        const char *BufStart, const char *BufPtr, const char *BufEnd);
1101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
111025c3a66402fb713c2d9bf5dc174ff264765379aChris Lattner  /// Lexer constructor - Create a new raw lexer object.  This object is only
112092bf67e5ca560d2fc6aa70be1f172b8b3a5ff96Dmitri Gribenko  /// suitable for calls to 'LexFromRawLexer'.  This lexer assumes that the
113092bf67e5ca560d2fc6aa70be1f172b8b3a5ff96Dmitri Gribenko  /// text range will outlive it, so it doesn't take ownership of it.
1146e2901407bff59aeb4cc301cc58b034723d0eb49Chris Lattner  Lexer(FileID FID, const llvm::MemoryBuffer *InputBuffer,
1154e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        const SourceManager &SM, const LangOptions &LangOpts);
1161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11742e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  /// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
11842e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  /// _Pragma expansion.  This has a variety of magic semantics that this method
11942e00d19b0dac64732eb5449d52a076282fcbf77Chris Lattner  /// sets up.  It returns a new'd Lexer that must be delete'd when done.
1201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  static Lexer *Create_PragmaLexer(SourceLocation SpellingLoc,
121433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth                                   SourceLocation ExpansionLocStart,
122433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth                                   SourceLocation ExpansionLocEnd,
123bcc2a67e5180612417727cbdd8afd0f79fdf726dChris Lattner                                   unsigned TokLen, Preprocessor &PP);
1241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1264e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  /// getLangOpts - Return the language features currently enabled.
1274e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  /// NOTE: this lexer modifies features as a file is parsed!
1284e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &getLangOpts() const { return LangOpts; }
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1309dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// getFileLoc - Return the File Location for the file we are lexing out of.
1319dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// The physical location encodes the location where the characters come from,
1329dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// the virtual location encodes where we should *claim* the characters came
1339dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  /// from.  Currently this is only used by _Pragma handling.
1349dc1f530c086d2c16f8cba758b0f59a5bf41323aChris Lattner  SourceLocation getFileLoc() const { return FileLoc; }
1351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
136d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedmanprivate:
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Lex - Return the next token in the file.  If this is the end of file, it
138164fcd022185fddc0e689f61ac8ad4b3ac744c2aNico Weber  /// return the tok::eof token.  This implicitly involves the preprocessor.
139d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool Lex(Token &Result);
1404b391087f9c5582d448ab66ccbc7028f7673f380Ted Kremenek
141d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedmanpublic:
1424b391087f9c5582d448ab66ccbc7028f7673f380Ted Kremenek  /// isPragmaLexer - Returns true if this Lexer is being used to lex a pragma.
1434b391087f9c5582d448ab66ccbc7028f7673f380Ted Kremenek  bool isPragmaLexer() const { return Is_PragmaLexer; }
1441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
145d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedmanprivate:
146ab91b7086cceadce2cfd6a69cc90cebcdfcea07fTed Kremenek  /// IndirectLex - An indirect call to 'Lex' that can be invoked via
147ab91b7086cceadce2cfd6a69cc90cebcdfcea07fTed Kremenek  ///  the PreprocessorLexer interface.
148651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void IndirectLex(Token &Result) override { Lex(Result); }
1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
150d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedmanpublic:
151590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner  /// LexFromRawLexer - Lex a token from a designated raw lexer (one with no
152590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner  /// associated preprocessor object.  Return true if the 'next character to
1531fa495304c81e03f07f278a47b5efe9317104aabChris Lattner  /// read' pointer points at the end of the lexer buffer, false otherwise.
154590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner  bool LexFromRawLexer(Token &Result) {
155590f0cc643274267d4d41125b62557e1d87886c3Chris Lattner    assert(LexingRawMode && "Not already in raw mode!");
1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Lex(Result);
157e5956bd2730c051835f9acd9e957c5d79f99e7c3Chris Lattner    // Note that lexing to the end of the buffer doesn't implicitly delete the
158e5956bd2730c051835f9acd9e957c5d79f99e7c3Chris Lattner    // lexer when in raw mode.
1591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return BufferPtr == BufferEnd;
1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
161d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner
162d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// isKeepWhitespaceMode - Return true if the lexer should return tokens for
163d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// every character in the file, including whitespace and comments.  This
164d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// should only be used in raw mode, as the preprocessor is not prepared to
165d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// deal with the excess tokens.
166d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  bool isKeepWhitespaceMode() const {
167d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    return ExtendedTokenMode > 1;
168d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  }
169d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner
170d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// SetKeepWhitespaceMode - This method lets clients enable or disable
171d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// whitespace retention mode.
172d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  void SetKeepWhitespaceMode(bool Val) {
1736aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose    assert((!Val || LexingRawMode || LangOpts.TraditionalCPP) &&
1746aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose           "Can only retain whitespace in raw mode or -traditional-cpp");
175d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    ExtendedTokenMode = Val ? 2 : 0;
176d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  }
177d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner
178d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// inKeepCommentMode - Return true if the lexer should return comments as
179d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  /// tokens.
180d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  bool inKeepCommentMode() const {
181d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    return ExtendedTokenMode > 0;
182d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner  }
1831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
184678c6358c8d4e368c78629099142397c63c1ee35Chris Lattner  /// SetCommentRetentionMode - Change the comment retention mode of the lexer
185678c6358c8d4e368c78629099142397c63c1ee35Chris Lattner  /// to the specified mode.  This is really only useful when lexing in raw
186678c6358c8d4e368c78629099142397c63c1ee35Chris Lattner  /// mode, because otherwise the lexer needs to manage this.
1871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void SetCommentRetentionState(bool Mode) {
188d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    assert(!isKeepWhitespaceMode() &&
189d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner           "Can't play with comment retention state when retaining whitespace");
190d88dc48e33d71732708960170e57a3d1bdc8f847Chris Lattner    ExtendedTokenMode = Mode ? 1 : 0;
191fa95a019da00b926d64ff83358ba73bbc6ae1e37Chris Lattner  }
1921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1936aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose  /// Sets the extended token mode back to its initial value, according to the
1946aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose  /// language options and preprocessor. This controls whether the lexer
1956aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose  /// produces comment and whitespace tokens.
1966aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose  ///
1976aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose  /// This requires the lexer to have an associated preprocessor. A standalone
1986aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose  /// lexer has nothing to reset to.
1996aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose  void resetExtendedTokenMode();
2006aad4a31b35df07fe818f193fcfd3c0197aea467Jordan Rose
20149e1d9814a34e4024a347c2fce8e9567c40e6bf5Alexander Kornienko  /// Gets source code buffer.
20249e1d9814a34e4024a347c2fce8e9567c40e6bf5Alexander Kornienko  StringRef getBuffer() const {
20349e1d9814a34e4024a347c2fce8e9567c40e6bf5Alexander Kornienko    return StringRef(BufferStart, BufferEnd - BufferStart);
20449e1d9814a34e4024a347c2fce8e9567c40e6bf5Alexander Kornienko  }
2051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ReadToEndOfLine - Read the rest of the current preprocessor line as an
2075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// uninterpreted string.  This switches the lexer out of directive mode.
2086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  void ReadToEndOfLine(SmallVectorImpl<char> *Result = nullptr);
2091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Diag - Forwarding function for diagnostics.  This translate a source
2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// position in the current buffer into a SourceLocation object for rendering.
2133cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner  DiagnosticBuilder Diag(const char *Loc, unsigned DiagID) const;
2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getSourceLocation - Return a source location identifier for the specified
2165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// offset in the current file.
217de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner  SourceLocation getSourceLocation(const char *Loc, unsigned TokLen = 1) const;
2181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
219bc0f6bc0391ecdff331885cdc769c20b2cb628a6Ted Kremenek  /// getSourceLocation - Return a source location for the next character in
220bc0f6bc0391ecdff331885cdc769c20b2cb628a6Ted Kremenek  /// the current file.
221651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SourceLocation getSourceLocation() override {
222651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return getSourceLocation(BufferPtr);
223651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
2241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
225fc8ea23eb6cbaaa5046f2abb4c033e24c8659efdDouglas Gregor  /// \brief Return the current location in the buffer.
226fc8ea23eb6cbaaa5046f2abb4c033e24c8659efdDouglas Gregor  const char *getBufferLocation() const { return BufferPtr; }
227fc8ea23eb6cbaaa5046f2abb4c033e24c8659efdDouglas Gregor
2285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Stringify - Convert the specified string into a C string by escaping '\'
2295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// and " characters.  This does not add surrounding ""'s to the string.
2305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// If Charify is true, this escapes the ' character instead of ".
231b6d6993e6e6d3daf4d9876794254d20a134e37c2Pirama Arumuga Nainar  static std::string Stringify(StringRef Str, bool Charify = false);
2321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
233d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner  /// Stringify - Convert the specified string into a C string by escaping '\'
234d8e3083840fef752d11ca183f42786470ed061e3Chris Lattner  /// and " characters.  This does not add surrounding ""'s to the string.
235686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  static void Stringify(SmallVectorImpl<char> &Str);
2361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
237b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
238b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  /// getSpelling - This method is used to get the spelling of a token into a
239b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  /// preallocated buffer, instead of as an std::string.  The caller is required
240b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  /// to allocate enough space for the token, which is guaranteed to be at least
241b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  /// Tok.getLength() bytes long.  The length of the actual result is returned.
242b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  ///
243b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  /// Note that this method may do two possible things: it may either fill in
244b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  /// the buffer specified with characters, or it may *change the input pointer*
245b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  /// to point to a constant buffer with the data already in it (avoiding a
246b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  /// copy).  The caller is not allowed to modify the returned buffer pointer
247b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  /// if an internal buffer is returned.
248b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  static unsigned getSpelling(const Token &Tok, const char *&Buffer,
249b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner                              const SourceManager &SourceMgr,
2504e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                              const LangOptions &LangOpts,
2516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                              bool *Invalid = nullptr);
252b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
253b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  /// getSpelling() - Return the 'spelling' of the Tok token.  The spelling of a
254b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  /// token is the characters used to represent the token in the source file
255b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  /// after trigraph expansion and escaped-newline folding.  In particular, this
256b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  /// wants to get the true, uncanonicalized, spelling of things like digraphs
257b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  /// UCNs, etc.
258b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  static std::string getSpelling(const Token &Tok,
259b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner                                 const SourceManager &SourceMgr,
2604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                                 const LangOptions &LangOpts,
2616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                 bool *Invalid = nullptr);
262834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall
263834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  /// getSpelling - This method is used to get the spelling of the
264834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  /// token at the given source location.  If, as is usually true, it
265834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  /// is not necessary to copy any data, then the returned string may
266834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  /// not point into the provided buffer.
267834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  ///
268433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  /// This method lexes at the expansion depth of the given
269433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  /// location and does not jump to the expansion or spelling
270834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  /// location.
271686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  static StringRef getSpelling(SourceLocation loc,
272fd61d6fe0aa9853f0577ca88a63901c3773e8101Nick Lewycky                               SmallVectorImpl<char> &buffer,
273fd61d6fe0aa9853f0577ca88a63901c3773e8101Nick Lewycky                               const SourceManager &SourceMgr,
274fd61d6fe0aa9853f0577ca88a63901c3773e8101Nick Lewycky                               const LangOptions &LangOpts,
2756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                               bool *invalid = nullptr);
276b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner
2779a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  /// MeasureTokenLength - Relex the token at the specified location and return
2789a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  /// its length in bytes in the input file.  If the token needs cleaning (e.g.
2799a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  /// includes a trigraph or an escaped newline) then this count includes bytes
2809a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  /// that are part of that.
2819a6119437672f42be5f50c3fe89fe843b1bfa5b5Chris Lattner  static unsigned MeasureTokenLength(SourceLocation Loc,
2822c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner                                     const SourceManager &SM,
2832c78b873f4f3823ae859c15674cb3d76c8554113Chris Lattner                                     const LangOptions &LangOpts);
2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
285d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis  /// \brief Relex the token at the specified location.
286d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis  /// \returns true if there was a failure, false on success.
287d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis  static bool getRawToken(SourceLocation Loc, Token &Result,
288d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis                          const SourceManager &SM,
289f0dd853bb1e8f3e59b169e6d34a8556c6003c47cFariborz Jahanian                          const LangOptions &LangOpts,
290f0dd853bb1e8f3e59b169e6d34a8556c6003c47cFariborz Jahanian                          bool IgnoreWhiteSpace = false);
291d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis
292a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  /// \brief Given a location any where in a source buffer, find the location
293a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  /// that corresponds to the beginning of the token in which the original
294a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  /// source location lands.
295a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor  static SourceLocation GetBeginningOfToken(SourceLocation Loc,
296a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor                                            const SourceManager &SM,
297a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor                                            const LangOptions &LangOpts);
298a8e5c5bdbe387b2552c1c23b828f54abcf085a40Douglas Gregor
2997ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// AdvanceToTokenCharacter - If the current SourceLocation specifies a
3007ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// location at the start of a token, return a new location that specifies a
3017ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// character within the token.  This handles trigraphs and escaped newlines.
3027ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  static SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,
3037ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner                                                unsigned Character,
3047ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner                                                const SourceManager &SM,
3054e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                                                const LangOptions &LangOpts);
3067ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner
3077ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// \brief Computes the source location just past the end of the
3087ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// token at this source location.
3097ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  ///
3107ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// This routine can be used to produce a source location that
3117ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// points just past the end of the token referenced by \p Loc, and
3127ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// is generally used when a diagnostic needs to point just after a
3137ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// token where it expected something different that it received. If
3147ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// the returned source location would not be meaningful (e.g., if
3157ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// it points into a macro), this routine returns an invalid
3167ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// source location.
3177ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  ///
3187ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// \param Offset an offset from the end of the token, where the source
3197ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// location should refer to. The default offset (0) produces a source
3207ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// location pointing just past the end of the token; an offset of 1 produces
3217ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  /// a source location pointing to the last character in the token, etc.
3227ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset,
3237ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner                                            const SourceManager &SM,
3244e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                                            const LangOptions &LangOpts);
3257a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
3260e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// \brief Given a token range, produce a corresponding CharSourceRange that
3270e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// is not a token range. This allows the source range to be used by
3280e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// components that don't have access to the lexer and thus can't find the
3290e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  /// end of the range for themselves.
3300e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  static CharSourceRange getAsCharRange(SourceRange Range,
3310e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                        const SourceManager &SM,
3320e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                        const LangOptions &LangOpts) {
3330e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    SourceLocation End = getLocForEndOfToken(Range.getEnd(), 0, SM, LangOpts);
3340e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return End.isInvalid() ? CharSourceRange()
3350e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                           : CharSourceRange::getCharRange(
3360e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                 Range.getBegin(), End.getLocWithOffset(-1));
3370e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
3380e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  static CharSourceRange getAsCharRange(CharSourceRange Range,
3390e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                        const SourceManager &SM,
3400e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                        const LangOptions &LangOpts) {
3410e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    return Range.isTokenRange()
3420e2c34f92f00628d48968dfea096d36381f494cbStephen Hines               ? getAsCharRange(Range.getAsRange(), SM, LangOpts)
3430e2c34f92f00628d48968dfea096d36381f494cbStephen Hines               : Range;
3440e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  }
3450e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
3467a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  /// \brief Returns true if the given MacroID location points at the first
347433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  /// token of the macro expansion.
34869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  ///
34969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  /// \param MacroBegin If non-null and function returns true, it is set to
35069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  /// begin location of the macro.
351433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  static bool isAtStartOfMacroExpansion(SourceLocation loc,
35269bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                        const SourceManager &SM,
35369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                        const LangOptions &LangOpts,
3546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                        SourceLocation *MacroBegin = nullptr);
3557a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
3567a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  /// \brief Returns true if the given MacroID location points at the last
357433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  /// token of the macro expansion.
35869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  ///
35970517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko  /// \param MacroEnd If non-null and function returns true, it is set to
36069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  /// end location of the macro.
361433db06b614f26dc6829e86d6ff469e2cca7d4f9Chandler Carruth  static bool isAtEndOfMacroExpansion(SourceLocation loc,
36269bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                      const SourceManager &SM,
36369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                      const LangOptions &LangOpts,
3646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                      SourceLocation *MacroEnd = nullptr);
3657a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
366a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  /// \brief Accepts a range and returns a character range with file locations.
367a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  ///
36811b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis  /// Returns a null range if a part of the range resides inside a macro
36911b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis  /// expansion or the range does not reside on the same FileID.
37095da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  ///
37195da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  /// This function is trying to deal with macros and return a range based on
37295da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  /// file locations. The cases where it can successfully handle macros are:
37395da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  ///
37495da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  /// -begin or end range lies at the start or end of a macro expansion, in
37595da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  ///  which case the location will be set to the expansion point, e.g:
3767e6f1a23fe26748a4eafc5c64ff6c319f6d932f3James Dennett  ///    \#define M 1 2
37795da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  ///    a M
37895da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  /// If you have a range [a, 2] (where 2 came from the macro), the function
37995da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  /// will return a range for "a M"
38095da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  /// if you have range [a, 1], the function will fail because the range
38195da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  /// overlaps with only a part of the macro
38295da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  ///
38395da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  /// -The macro is a function macro and the range can be mapped to the macro
38495da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  ///  arguments, e.g:
3857e6f1a23fe26748a4eafc5c64ff6c319f6d932f3James Dennett  ///    \#define M 1 2
3867e6f1a23fe26748a4eafc5c64ff6c319f6d932f3James Dennett  ///    \#define FM(x) x
38795da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  ///    FM(a b M)
38895da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  /// if you have range [b, 2], the function will return the file range "b M"
38995da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  /// inside the macro arguments.
39095da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  /// if you have range [a, 2], the function will return the file range
39195da18142abeec5099dedc50d6f418917bd6167dArgyrios Kyrtzidis  /// "FM(a b M)" since the range includes all of the macro expansion.
392a83f4d2315dbeb3914868f1ccb8e74fb2ccdbb0cArgyrios Kyrtzidis  static CharSourceRange makeFileCharRange(CharSourceRange Range,
39311b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis                                           const SourceManager &SM,
39411b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis                                           const LangOptions &LangOpts);
39511b652d41d0d97380ab321a1dba48ecb044f9de8Argyrios Kyrtzidis
396e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  /// \brief Returns a string for the source that the range encompasses.
397e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis  static StringRef getSourceText(CharSourceRange Range,
398e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis                                 const SourceManager &SM,
399e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis                                 const LangOptions &LangOpts,
4006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                 bool *Invalid = nullptr);
401e64d9037658a1b95c79ea275af6167a110b3c563Argyrios Kyrtzidis
402c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// \brief Retrieve the name of the immediate macro expansion.
403c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  ///
404c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// This routine starts from a source location, and finds the name of the macro
405c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// responsible for its immediate expansion. It looks through any intervening
406c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// macro argument expansions to compute this. It returns a StringRef which
407c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// refers to the SourceManager-owned buffer of the source where that macro
408c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// name is spelled. Thus, the result shouldn't out-live that SourceManager.
409c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  static StringRef getImmediateMacroName(SourceLocation Loc,
410c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks                                         const SourceManager &SM,
411c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks                                         const LangOptions &LangOpts);
412c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks
4134967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  /// \brief Retrieve the name of the immediate macro expansion.
4144967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  ///
4154967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  /// This routine starts from a source location, and finds the name of the
4164967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  /// macro responsible for its immediate expansion. It looks through any
4174967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  /// intervening macro argument expansions to compute this. It returns a
4184967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  /// StringRef which refers to the SourceManager-owned buffer of the source
4194967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  /// where that macro name is spelled. Thus, the result shouldn't out-live
4204967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  /// that SourceManager.
4214967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  ///
4224967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  /// This differs from Lexer::getImmediateMacroName in that any macro argument
4234967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  /// location will result in the topmost function macro that accepted it.
4244967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  /// e.g.
4254967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  /// \code
4264967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  ///   MAC1( MAC2(foo) )
4274967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  /// \endcode
4284967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  /// for location of 'foo' token, this function will return "MAC1" while
4294967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  /// Lexer::getImmediateMacroName will return "MAC2".
4304967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar  static StringRef getImmediateMacroNameForDiagnostics(
4314967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar      SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts);
4324967a710c84587c654b56c828382219c3937dacbPirama Arumuga Nainar
433f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  /// \brief Compute the preamble of the given file.
434f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  ///
435f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  /// The preamble of a file contains the initial comments, include directives,
436f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  /// and other preprocessor directives that occur before the code in this
437f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  /// particular file actually begins. The preamble of the main source file is
438f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  /// a potential prefix header.
439f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  ///
440f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  /// \param Buffer The memory buffer containing the file's contents.
441f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  ///
442df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// \param MaxLines If non-zero, restrict the length of the preamble
443df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  /// to fewer than this number of lines.
444df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor  ///
445f033f1da4a34f8df6e95e9929dc04ff54bb8fb01Douglas Gregor  /// \returns The offset into the file where the preamble ends and the rest
446f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// of the file begins along with a boolean value indicating whether
447f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// the preamble ends at the beginning of a new line.
448176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  static std::pair<unsigned, bool> ComputePreamble(StringRef Buffer,
449176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                                   const LangOptions &LangOpts,
450176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                                                   unsigned MaxLines = 0);
451e506f8a41063410c75945ebb804758bd0202947fEli Friedman
452e506f8a41063410c75945ebb804758bd0202947fEli Friedman  /// \brief Checks that the given token is the first token that occurs after
453e506f8a41063410c75945ebb804758bd0202947fEli Friedman  /// the given location (this excludes comments and whitespace). Returns the
454e506f8a41063410c75945ebb804758bd0202947fEli Friedman  /// location immediately after the specified token. If the token is not found
455e506f8a41063410c75945ebb804758bd0202947fEli Friedman  /// or the location is inside a macro, the returned source location will be
456e506f8a41063410c75945ebb804758bd0202947fEli Friedman  /// invalid.
457e506f8a41063410c75945ebb804758bd0202947fEli Friedman  static SourceLocation findLocationAfterToken(SourceLocation loc,
458e506f8a41063410c75945ebb804758bd0202947fEli Friedman                                         tok::TokenKind TKind,
459e506f8a41063410c75945ebb804758bd0202947fEli Friedman                                         const SourceManager &SM,
460e506f8a41063410c75945ebb804758bd0202947fEli Friedman                                         const LangOptions &LangOpts,
461e506f8a41063410c75945ebb804758bd0202947fEli Friedman                                         bool SkipTrailingWhitespaceAndNewLine);
462e506f8a41063410c75945ebb804758bd0202947fEli Friedman
463e506f8a41063410c75945ebb804758bd0202947fEli Friedman  /// \brief Returns true if the given character could appear in an identifier.
464e506f8a41063410c75945ebb804758bd0202947fEli Friedman  static bool isIdentifierBodyChar(char c, const LangOptions &LangOpts);
465e506f8a41063410c75945ebb804758bd0202947fEli Friedman
4669366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara  /// getCharAndSizeNoWarn - Like the getCharAndSize method, but does not ever
4679366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara  /// emit a warning.
4689366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara  static inline char getCharAndSizeNoWarn(const char *Ptr, unsigned &Size,
4699366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara                                          const LangOptions &LangOpts) {
4709366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara    // If this is not a trigraph and not a UCN or escaped newline, return
4719366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara    // quickly.
4729366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara    if (isObviouslySimpleCharacter(Ptr[0])) {
4739366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara      Size = 1;
4749366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara      return *Ptr;
4759366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara    }
4769366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara
4779366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara    Size = 0;
4789366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara    return getCharAndSizeSlowNoWarn(Ptr, Size, LangOpts);
4799366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara  }
4809366a5a8d0c5834cffda2c31c924605fb9dffc9bAbramo Bagnara
4815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
4825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Internal implementation interfaces.
4835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
4845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// LexTokenInternal - Internal interface to lex a preprocessing token. Called
4865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// by Lex.
4875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
488d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine);
489d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman
490d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool CheckUnicodeWhitespace(Token &Result, uint32_t C, const char *CurPtr);
4915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
492c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  /// Given that a token begins with the Unicode character \p C, figure out
493c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  /// what kind of token it is and dispatch to the appropriate lexing helper
494c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  /// function.
495d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool LexUnicode(Token &Result, uint32_t C, const char *CurPtr);
496c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose
4975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FormTokenWithChars - When we lex a token, we have identified a span
4985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// starting at BufferPtr, going to TokEnd that forms the token.  This method
4995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// takes that range and assigns it to the token as its location and size.  In
5005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// addition, since tokens cannot overlap, this also updates BufferPtr to be
5015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// TokEnd.
5021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void FormTokenWithChars(Token &Result, const char *TokEnd,
5039e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner                          tok::TokenKind Kind) {
504de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    unsigned TokLen = TokEnd-BufferPtr;
505de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    Result.setLength(TokLen);
506de7aeefc5573d669ed476d7bda7a8940d3bcadb7Chris Lattner    Result.setLocation(getSourceLocation(BufferPtr, TokLen));
5079e6293d4dfd688429f77ee3b6edba9dfd7ada3a2Chris Lattner    Result.setKind(Kind);
5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    BufferPtr = TokEnd;
5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// isNextPPTokenLParen - Return 1 if the next unexpanded token will return a
5125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// tok::l_paren token, 0 if it is something else and 2 if there are no more
5135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// tokens in the buffer controlled by this lexer.
5145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned isNextPPTokenLParen();
5155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
5175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Lexer character reading interfaces.
5181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // This lexer is built on two interfaces for reading characters, both of which
5205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // automatically provide phase 1/2 translation.  getAndAdvanceChar is used
5215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // when we know that we will be reading a character from the input buffer and
5225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // that this character will be part of the result token. This occurs in (f.e.)
5235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // string processing, because we know we need to read until we find the
5245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // closing '"' character.
5255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //
5264718e67ca9018342483dccb40d43e339101ac386Chris Lattner  // The second interface is the combination of getCharAndSize with
5274718e67ca9018342483dccb40d43e339101ac386Chris Lattner  // ConsumeChar.  getCharAndSize reads a phase 1/2 translated character,
5285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // returning it and its size.  If the lexer decides that this character is
5295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // part of the current token, it calls ConsumeChar on it.  This two stage
5305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // approach allows us to emit diagnostics for characters (e.g. warnings about
5315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // trigraphs), knowing that they only are emitted if the character is
5325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // consumed.
5331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
534f66d3e390fac1dce6b0e3ff1a3d0180638e560f6Chris Lattner  /// isObviouslySimpleCharacter - Return true if the specified character is
535f66d3e390fac1dce6b0e3ff1a3d0180638e560f6Chris Lattner  /// obviously the same in translation phase 1 and translation phase 3.  This
536f66d3e390fac1dce6b0e3ff1a3d0180638e560f6Chris Lattner  /// can return false for characters that end up being the same, but it will
537f66d3e390fac1dce6b0e3ff1a3d0180638e560f6Chris Lattner  /// never return true for something that needs to be mapped.
538f66d3e390fac1dce6b0e3ff1a3d0180638e560f6Chris Lattner  static bool isObviouslySimpleCharacter(char C) {
539f66d3e390fac1dce6b0e3ff1a3d0180638e560f6Chris Lattner    return C != '?' && C != '\\';
540f66d3e390fac1dce6b0e3ff1a3d0180638e560f6Chris Lattner  }
5411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getAndAdvanceChar - Read a single 'character' from the specified buffer,
5435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// advance over it, and return it.  This is tricky in several cases.  Here we
5445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// just handle the trivial case and fall-back to the non-inlined
5455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getCharAndSizeSlow method to handle the hard case.
546d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  inline char getAndAdvanceChar(const char *&Ptr, Token &Tok) {
5475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is not a trigraph and not a UCN or escaped newline, return
5485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // quickly.
549f66d3e390fac1dce6b0e3ff1a3d0180638e560f6Chris Lattner    if (isObviouslySimpleCharacter(Ptr[0])) return *Ptr++;
5501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    unsigned Size = 0;
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    char C = getCharAndSizeSlow(Ptr, Size, &Tok);
5535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Ptr += Size;
5545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return C;
5555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5574718e67ca9018342483dccb40d43e339101ac386Chris Lattner  /// ConsumeChar - When a character (identified by getCharAndSize) is consumed
5585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// and added to a given token, check to see if there are diagnostics that
5595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// need to be emitted or flags that need to be set on the token.  If so, do
5605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// it.
561d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  const char *ConsumeChar(const char *Ptr, unsigned Size, Token &Tok) {
5625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Normal case, we consumed exactly one token.  Just return it.
5635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (Size == 1)
5645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return Ptr+Size;
5655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Otherwise, re-lex the character with a current token, allowing
5675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // diagnostics to be emitted and flags to be set.
5685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Size = 0;
5695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    getCharAndSizeSlow(Ptr, Size, &Tok);
5705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return Ptr+Size;
5715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getCharAndSize - Peek a single 'character' from the specified buffer,
5745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// get its size, and return it.  This is tricky in several cases.  Here we
5755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// just handle the trivial case and fall-back to the non-inlined
5765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getCharAndSizeSlow method to handle the hard case.
5775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  inline char getCharAndSize(const char *Ptr, unsigned &Size) {
5785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // If this is not a trigraph and not a UCN or escaped newline, return
5795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // quickly.
580f66d3e390fac1dce6b0e3ff1a3d0180638e560f6Chris Lattner    if (isObviouslySimpleCharacter(Ptr[0])) {
5815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Size = 1;
5825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      return *Ptr;
5835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
5841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Size = 0;
5865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getCharAndSizeSlow(Ptr, Size);
5875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getCharAndSizeSlow - Handle the slow/uncommon case of the getCharAndSize
5905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// method.
5916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  char getCharAndSizeSlow(const char *Ptr, unsigned &Size,
5926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                          Token *Tok = nullptr);
5931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59424f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  /// getEscapedNewLineSize - Return the size of the specified escaped newline,
59524f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  /// or 0 if it is not an escaped newline. P[-1] is known to be a "\" on entry
59624f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  /// to this function.
59724f0e48c0aa62f2268e061aad70f9b19a59e7b52Chris Lattner  static unsigned getEscapedNewLineSize(const char *P);
5981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
599033749571f8d4c804eeb357c70b06424aa24503bChris Lattner  /// SkipEscapedNewLines - If P points to an escaped newline (or a series of
600033749571f8d4c804eeb357c70b06424aa24503bChris Lattner  /// them), skip over them and return the first non-escaped-newline found,
601033749571f8d4c804eeb357c70b06424aa24503bChris Lattner  /// otherwise return P.
602033749571f8d4c804eeb357c70b06424aa24503bChris Lattner  static const char *SkipEscapedNewLines(const char *P);
603aca25bccefe56121b686706afc84c8cb5d46e65bAnna Zaks
6045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getCharAndSizeSlowNoWarn - Same as getCharAndSizeSlow, but never emits a
6055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// diagnostic.
6065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static char getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
6074e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                                       const LangOptions &LangOpts);
6081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
6105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Other lexer functions.
6111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
612f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  void SkipBytes(unsigned Bytes, bool StartOfLine);
6135cc2c6eb67b6e5361bbe96f79b519fd62ec666d6Richard Smith
614d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  void PropagateLineStartLeadingSpaceInfo(Token &Result);
615d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman
6164ac537b0f07f2efb9fcf081f60d54e6cfb1cf2d5Richard Smith  const char *LexUDSuffix(Token &Result, const char *CurPtr,
6174ac537b0f07f2efb9fcf081f60d54e6cfb1cf2d5Richard Smith                          bool IsStringLiteral);
6184ac537b0f07f2efb9fcf081f60d54e6cfb1cf2d5Richard Smith
6195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Helper functions to lex the remainder of a token of the specific type.
620d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool LexIdentifier         (Token &Result, const char *CurPtr);
621d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool LexNumericConstant    (Token &Result, const char *CurPtr);
622d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool LexStringLiteral      (Token &Result, const char *CurPtr,
6235cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                              tok::TokenKind Kind);
624d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool LexRawStringLiteral   (Token &Result, const char *CurPtr,
6252fa4e86b4fdada3b9ecbbbd99965b83ed879f69bCraig Topper                              tok::TokenKind Kind);
626d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool LexAngledStringLiteral(Token &Result, const char *CurPtr);
627d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool LexCharConstant       (Token &Result, const char *CurPtr,
6285cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                              tok::TokenKind Kind);
629d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  bool LexEndOfFile          (Token &Result, const char *CurPtr);
630d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool SkipWhitespace        (Token &Result, const char *CurPtr,
631d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman                              bool &TokAtPhysicalStartOfLine);
632d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool SkipLineComment       (Token &Result, const char *CurPtr,
633d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman                              bool &TokAtPhysicalStartOfLine);
634d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman  bool SkipBlockComment      (Token &Result, const char *CurPtr,
635d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman                              bool &TokAtPhysicalStartOfLine);
636bb23628148f555a4cf71f98c27096a7a804c085cNico Weber  bool SaveLineComment       (Token &Result, const char *CurPtr);
63734f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner
63834f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  bool IsStartOfConflictMarker(const char *CurPtr);
63934f349da38a7bd99103e12d8ea6c73bc8d025193Chris Lattner  bool HandleEndOfConflictMarker(const char *CurPtr);
6407d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
6417d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  bool isCodeCompletionPoint(const char *CurPtr) const;
6427d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  void cutOffLexing() { BufferPtr = BufferEnd; }
643e506f8a41063410c75945ebb804758bd0202947fEli Friedman
644e506f8a41063410c75945ebb804758bd0202947fEli Friedman  bool isHexaLiteral(const char *Start, const LangOptions &LangOpts);
645c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose
646c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose
647c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  /// Read a universal character name.
648c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  ///
649c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  /// \param CurPtr The position in the source buffer after the initial '\'.
650c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  ///               If the UCN is syntactically well-formed (but not necessarily
651c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  ///               valid), this parameter will be updated to point to the
652c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  ///               character after the UCN.
653c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  /// \param SlashLoc The position in the source buffer of the '\'.
654c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  /// \param Tok The token being formed. Pass \c NULL to suppress diagnostics
655c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  ///            and handle token formation in the caller.
656c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  ///
657c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  /// \return The Unicode codepoint specified by the UCN, or 0 if the UCN is
658c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  ///         invalid.
659c7629d941557f7179eb8fa8a2e2a74d749cbaf7cJordan Rose  uint32_t tryReadUCN(const char *&CurPtr, const char *SlashLoc, Token *Tok);
6605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
661651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \brief Try to consume a UCN as part of an identifier at the current
662651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// location.
663651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \param CurPtr Initially points to the range of characters in the source
664651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ///               buffer containing the '\'. Updated to point past the end of
665651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ///               the UCN on success.
666651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \param Size The number of characters occupied by the '\' (including
667651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ///             trigraphs and escaped newlines).
668651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \param Result The token being produced. Marked as containing a UCN on
669651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ///               success.
670651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \return \c true if a UCN was lexed and it produced an acceptable
671651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ///         identifier character, \c false otherwise.
672651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool tryConsumeIdentifierUCN(const char *&CurPtr, unsigned Size,
673651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                               Token &Result);
674651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
675651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \brief Try to consume an identifier character encoded in UTF-8.
676651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \param CurPtr Points to the start of the (potential) UTF-8 code unit
677651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ///        sequence. On success, updated to point past the end of it.
678651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \return \c true if a UTF-8 sequence mapping to an acceptable identifier
679651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ///         character was lexed, \c false otherwise.
680651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool tryConsumeIdentifierUTF8Char(const char *&CurPtr);
681651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines};
6825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
6845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
686