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