clang  3.9.0
LiteralSupport.h
Go to the documentation of this file.
1 //===--- LiteralSupport.h ---------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the NumericLiteralParser, CharLiteralParser, and
11 // StringLiteralParser interfaces.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_LEX_LITERALSUPPORT_H
16 #define LLVM_CLANG_LEX_LITERALSUPPORT_H
17 
18 #include "clang/Basic/CharInfo.h"
19 #include "clang/Basic/LLVM.h"
20 #include "clang/Basic/TokenKinds.h"
21 #include "llvm/ADT/APFloat.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/DataTypes.h"
26 
27 namespace clang {
28 
29 class DiagnosticsEngine;
30 class Preprocessor;
31 class Token;
32 class SourceLocation;
33 class TargetInfo;
34 class SourceManager;
35 class LangOptions;
36 
37 /// Copy characters from Input to Buf, expanding any UCNs.
38 void expandUCNs(SmallVectorImpl<char> &Buf, StringRef Input);
39 
40 /// NumericLiteralParser - This performs strict semantic analysis of the content
41 /// of a ppnumber, classifying it as either integer, floating, or erroneous,
42 /// determines the radix of the value and can convert it to a useful value.
44  Preprocessor &PP; // needed for diagnostics
45 
46  const char *const ThisTokBegin;
47  const char *const ThisTokEnd;
48  const char *DigitsBegin, *SuffixBegin; // markers
49  const char *s; // cursor
50 
51  unsigned radix;
52 
53  bool saw_exponent, saw_period, saw_ud_suffix;
54 
55  SmallString<32> UDSuffixBuf;
56 
57 public:
58  NumericLiteralParser(StringRef TokSpelling,
59  SourceLocation TokLoc,
60  Preprocessor &PP);
61  bool hadError : 1;
62  bool isUnsigned : 1;
63  bool isLong : 1; // This is *not* set for long long.
64  bool isLongLong : 1;
65  bool isHalf : 1; // 1.0h
66  bool isFloat : 1; // 1.0f
67  bool isImaginary : 1; // 1.0i
68  bool isFloat128 : 1; // 1.0q
69  uint8_t MicrosoftInteger; // Microsoft suffix extension i8, i16, i32, or i64.
70 
71  bool isIntegerLiteral() const {
72  return !saw_period && !saw_exponent;
73  }
74  bool isFloatingLiteral() const {
75  return saw_period || saw_exponent;
76  }
77 
78  bool hasUDSuffix() const {
79  return saw_ud_suffix;
80  }
81  StringRef getUDSuffix() const {
82  assert(saw_ud_suffix);
83  return UDSuffixBuf;
84  }
85  unsigned getUDSuffixOffset() const {
86  assert(saw_ud_suffix);
87  return SuffixBegin - ThisTokBegin;
88  }
89 
90  static bool isValidUDSuffix(const LangOptions &LangOpts, StringRef Suffix);
91 
92  unsigned getRadix() const { return radix; }
93 
94  /// GetIntegerValue - Convert this numeric literal value to an APInt that
95  /// matches Val's input width. If there is an overflow (i.e., if the unsigned
96  /// value read is larger than the APInt's bits will hold), set Val to the low
97  /// bits of the result and return true. Otherwise, return false.
98  bool GetIntegerValue(llvm::APInt &Val);
99 
100  /// GetFloatValue - Convert this numeric literal to a floating value, using
101  /// the specified APFloat fltSemantics (specifying float, double, etc).
102  /// The optional bool isExact (passed-by-reference) has its value
103  /// set to true if the returned APFloat can represent the number in the
104  /// literal exactly, and false otherwise.
105  llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result);
106 
107 private:
108 
109  void ParseNumberStartingWithZero(SourceLocation TokLoc);
110  void ParseDecimalOrOctalCommon(SourceLocation TokLoc);
111 
112  static bool isDigitSeparator(char C) { return C == '\''; }
113 
114  /// \brief Determine whether the sequence of characters [Start, End) contains
115  /// any real digits (not digit separators).
116  bool containsDigits(const char *Start, const char *End) {
117  return Start != End && (Start + 1 != End || !isDigitSeparator(Start[0]));
118  }
119 
120  enum CheckSeparatorKind { CSK_BeforeDigits, CSK_AfterDigits };
121 
122  /// \brief Ensure that we don't have a digit separator here.
123  void checkSeparator(SourceLocation TokLoc, const char *Pos,
124  CheckSeparatorKind IsAfterDigits);
125 
126  /// SkipHexDigits - Read and skip over any hex digits, up to End.
127  /// Return a pointer to the first non-hex digit or End.
128  const char *SkipHexDigits(const char *ptr) {
129  while (ptr != ThisTokEnd && (isHexDigit(*ptr) || isDigitSeparator(*ptr)))
130  ptr++;
131  return ptr;
132  }
133 
134  /// SkipOctalDigits - Read and skip over any octal digits, up to End.
135  /// Return a pointer to the first non-hex digit or End.
136  const char *SkipOctalDigits(const char *ptr) {
137  while (ptr != ThisTokEnd &&
138  ((*ptr >= '0' && *ptr <= '7') || isDigitSeparator(*ptr)))
139  ptr++;
140  return ptr;
141  }
142 
143  /// SkipDigits - Read and skip over any digits, up to End.
144  /// Return a pointer to the first non-hex digit or End.
145  const char *SkipDigits(const char *ptr) {
146  while (ptr != ThisTokEnd && (isDigit(*ptr) || isDigitSeparator(*ptr)))
147  ptr++;
148  return ptr;
149  }
150 
151  /// SkipBinaryDigits - Read and skip over any binary digits, up to End.
152  /// Return a pointer to the first non-binary digit or End.
153  const char *SkipBinaryDigits(const char *ptr) {
154  while (ptr != ThisTokEnd &&
155  (*ptr == '0' || *ptr == '1' || isDigitSeparator(*ptr)))
156  ptr++;
157  return ptr;
158  }
159 
160 };
161 
162 /// CharLiteralParser - Perform interpretation and semantic analysis of a
163 /// character literal.
165  uint64_t Value;
167  bool IsMultiChar;
168  bool HadError;
169  SmallString<32> UDSuffixBuf;
170  unsigned UDSuffixOffset;
171 public:
172  CharLiteralParser(const char *begin, const char *end,
173  SourceLocation Loc, Preprocessor &PP,
175 
176  bool hadError() const { return HadError; }
177  bool isAscii() const { return Kind == tok::char_constant; }
178  bool isWide() const { return Kind == tok::wide_char_constant; }
179  bool isUTF8() const { return Kind == tok::utf8_char_constant; }
180  bool isUTF16() const { return Kind == tok::utf16_char_constant; }
181  bool isUTF32() const { return Kind == tok::utf32_char_constant; }
182  bool isMultiChar() const { return IsMultiChar; }
183  uint64_t getValue() const { return Value; }
184  StringRef getUDSuffix() const { return UDSuffixBuf; }
185  unsigned getUDSuffixOffset() const {
186  assert(!UDSuffixBuf.empty() && "no ud-suffix");
187  return UDSuffixOffset;
188  }
189 };
190 
191 /// StringLiteralParser - This decodes string escape characters and performs
192 /// wide string analysis and Translation Phase #6 (concatenation of string
193 /// literals) (C99 5.1.1.2p1).
195  const SourceManager &SM;
196  const LangOptions &Features;
197  const TargetInfo &Target;
198  DiagnosticsEngine *Diags;
199 
200  unsigned MaxTokenLength;
201  unsigned SizeBound;
202  unsigned CharByteWidth;
204  SmallString<512> ResultBuf;
205  char *ResultPtr; // cursor
206  SmallString<32> UDSuffixBuf;
207  unsigned UDSuffixToken;
208  unsigned UDSuffixOffset;
209 public:
211  Preprocessor &PP, bool Complain = true);
213  const SourceManager &sm, const LangOptions &features,
214  const TargetInfo &target,
215  DiagnosticsEngine *diags = nullptr)
216  : SM(sm), Features(features), Target(target), Diags(diags),
217  MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
218  ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
219  init(StringToks);
220  }
221 
222 
223  bool hadError;
224  bool Pascal;
225 
226  StringRef GetString() const {
227  return StringRef(ResultBuf.data(), GetStringLength());
228  }
229  unsigned GetStringLength() const { return ResultPtr-ResultBuf.data(); }
230 
231  unsigned GetNumStringChars() const {
232  return GetStringLength() / CharByteWidth;
233  }
234  /// getOffsetOfStringByte - This function returns the offset of the
235  /// specified byte of the string data represented by Token. This handles
236  /// advancing over escape sequences in the string.
237  ///
238  /// If the Diagnostics pointer is non-null, then this will do semantic
239  /// checking of the string literal and emit errors and warnings.
240  unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const;
241 
242  bool isAscii() const { return Kind == tok::string_literal; }
243  bool isWide() const { return Kind == tok::wide_string_literal; }
244  bool isUTF8() const { return Kind == tok::utf8_string_literal; }
245  bool isUTF16() const { return Kind == tok::utf16_string_literal; }
246  bool isUTF32() const { return Kind == tok::utf32_string_literal; }
247  bool isPascal() const { return Pascal; }
248 
249  StringRef getUDSuffix() const { return UDSuffixBuf; }
250 
251  /// Get the index of a token containing a ud-suffix.
252  unsigned getUDSuffixToken() const {
253  assert(!UDSuffixBuf.empty() && "no ud-suffix");
254  return UDSuffixToken;
255  }
256  /// Get the spelling offset of the first byte of the ud-suffix.
257  unsigned getUDSuffixOffset() const {
258  assert(!UDSuffixBuf.empty() && "no ud-suffix");
259  return UDSuffixOffset;
260  }
261 
262 private:
263  void init(ArrayRef<Token> StringToks);
264  bool CopyStringFragment(const Token &Tok, const char *TokBegin,
265  StringRef Fragment);
266  void DiagnoseLexingError(SourceLocation Loc);
267 };
268 
269 } // end namespace clang
270 
271 #endif
static LLVM_READONLY bool isDigit(unsigned char c)
Return true if this character is an ASCII digit: [0-9].
Definition: CharInfo.h:94
StringLiteralParser(ArrayRef< Token > StringToks, Preprocessor &PP, bool Complain=true)
unsigned getRadix() const
StringRef getUDSuffix() const
iterator begin() const
Definition: Type.h:4235
unsigned getUDSuffixToken() const
Get the index of a token containing a ud-suffix.
unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const
getOffsetOfStringByte - This function returns the offset of the specified byte of the string data rep...
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
StringLiteralParser(ArrayRef< Token > StringToks, const SourceManager &sm, const LangOptions &features, const TargetInfo &target, DiagnosticsEngine *diags=nullptr)
uint64_t getValue() const
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:135
static bool isValidUDSuffix(const LangOptions &LangOpts, StringRef Suffix)
Determine whether a suffix is a valid ud-suffix.
NumericLiteralParser(StringRef TokSpelling, SourceLocation TokLoc, Preprocessor &PP)
integer-constant: [C99 6.4.4.1] decimal-constant integer-suffix octal-constant integer-suffix hexadec...
iterator end() const
const SmallVectorImpl< AnnotatedLine * >::const_iterator End
Exposes information about the current target.
CharLiteralParser(const char *begin, const char *end, SourceLocation Loc, Preprocessor &PP, tok::TokenKind kind)
CharLiteralParser - Perform interpretation and semantic analysis of a character literal.
FormatToken * Token
StringRef getUDSuffix() const
The result type of a method or function.
bool GetIntegerValue(llvm::APInt &Val)
GetIntegerValue - Convert this numeric literal value to an APInt that matches Val's input width...
#define false
Definition: stdbool.h:33
Kind
Encodes a location in the source.
llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result)
GetFloatValue - Convert this numeric literal to a floating value, using the specified APFloat fltSema...
StringRef GetString() const
StringRef getUDSuffix() const
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
void expandUCNs(SmallVectorImpl< char > &Buf, StringRef Input)
Copy characters from Input to Buf, expanding any UCNs.
unsigned getUDSuffixOffset() const
Get the spelling offset of the first byte of the ud-suffix.
unsigned getUDSuffixOffset() const
Defines the clang::TokenKind enum and support functions.
StringLiteralParser - This decodes string escape characters and performs wide string analysis and Tra...
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:43
const StringRef Input
unsigned GetStringLength() const
NumericLiteralParser - This performs strict semantic analysis of the content of a ppnumber...
unsigned getUDSuffixOffset() const
unsigned GetNumStringChars() const
This class handles loading and caching of source files into memory.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:97
static LLVM_READONLY bool isHexDigit(unsigned char c)
Return true if this character is an ASCII hex digit: [0-9a-fA-F].
Definition: CharInfo.h:124