Coverage Report

Created: 2021-09-21 08:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Basic/CharInfo.h
Line
Count
Source
1
//===--- clang/Basic/CharInfo.h - Classifying ASCII Characters --*- 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
#ifndef LLVM_CLANG_BASIC_CHARINFO_H
10
#define LLVM_CLANG_BASIC_CHARINFO_H
11
12
#include "clang/Basic/LLVM.h"
13
#include "llvm/ADT/StringRef.h"
14
#include "llvm/Support/Compiler.h"
15
#include "llvm/Support/DataTypes.h"
16
17
namespace clang {
18
namespace charinfo {
19
  extern const uint16_t InfoTable[256];
20
21
  enum {
22
    CHAR_HORZ_WS  = 0x0001,  // '\t', '\f', '\v'.  Note, no '\0'
23
    CHAR_VERT_WS  = 0x0002,  // '\r', '\n'
24
    CHAR_SPACE    = 0x0004,  // ' '
25
    CHAR_DIGIT    = 0x0008,  // 0-9
26
    CHAR_XLETTER  = 0x0010,  // a-f,A-F
27
    CHAR_UPPER    = 0x0020,  // A-Z
28
    CHAR_LOWER    = 0x0040,  // a-z
29
    CHAR_UNDER    = 0x0080,  // _
30
    CHAR_PERIOD   = 0x0100,  // .
31
    CHAR_RAWDEL   = 0x0200,  // {}[]#<>%:;?*+-/^&|~!=,"'
32
    CHAR_PUNCT    = 0x0400   // `$@()
33
  };
34
35
  enum {
36
    CHAR_XUPPER = CHAR_XLETTER | CHAR_UPPER,
37
    CHAR_XLOWER = CHAR_XLETTER | CHAR_LOWER
38
  };
39
} // end namespace charinfo
40
41
/// Returns true if this is an ASCII character.
42
69.8M
LLVM_READNONE inline bool isASCII(char c) {
43
69.8M
  return static_cast<unsigned char>(c) <= 127;
44
69.8M
}
45
46
1.02G
LLVM_READNONE inline bool isASCII(unsigned char c) { return c <= 127; }
47
48
/// Returns true if this is an ASCII character.
49
115
LLVM_READNONE inline bool isASCII(uint32_t c) { return c <= 127; }
50
51
/// Returns true if this is a valid first character of a C identifier,
52
/// which is [a-zA-Z_].
53
LLVM_READONLY inline bool isAsciiIdentifierStart(unsigned char c,
54
9.22M
                                                 bool AllowDollar = false) {
55
9.22M
  using namespace charinfo;
56
9.22M
  if (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_UNDER))
57
309k
    return true;
58
8.91M
  return AllowDollar && 
c == '$'5
;
59
9.22M
}
60
61
/// Returns true if this is a body character of a C identifier,
62
/// which is [a-zA-Z0-9_].
63
LLVM_READONLY inline bool isAsciiIdentifierContinue(unsigned char c,
64
11.5G
                                                    bool AllowDollar = false) {
65
11.5G
  using namespace charinfo;
66
11.5G
  if (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_DIGIT|CHAR_UNDER))
67
9.54G
    return true;
68
2.05G
  return AllowDollar && 
c == '$'4.01k
;
69
11.5G
}
70
71
/// Returns true if this character is horizontal ASCII whitespace:
72
/// ' ', '\\t', '\\f', '\\v'.
73
///
74
/// Note that this returns false for '\\0'.
75
4.19G
LLVM_READONLY inline bool isHorizontalWhitespace(unsigned char c) {
76
4.19G
  using namespace charinfo;
77
4.19G
  return (InfoTable[c] & (CHAR_HORZ_WS|CHAR_SPACE)) != 0;
78
4.19G
}
79
80
/// Returns true if this character is vertical ASCII whitespace: '\\n', '\\r'.
81
///
82
/// Note that this returns false for '\\0'.
83
3.31G
LLVM_READONLY inline bool isVerticalWhitespace(unsigned char c) {
84
3.31G
  using namespace charinfo;
85
3.31G
  return (InfoTable[c] & CHAR_VERT_WS) != 0;
86
3.31G
}
87
88
/// Return true if this character is horizontal or vertical ASCII whitespace:
89
/// ' ', '\\t', '\\f', '\\v', '\\n', '\\r'.
90
///
91
/// Note that this returns false for '\\0'.
92
94.4M
LLVM_READONLY inline bool isWhitespace(unsigned char c) {
93
94.4M
  using namespace charinfo;
94
94.4M
  return (InfoTable[c] & (CHAR_HORZ_WS|CHAR_VERT_WS|CHAR_SPACE)) != 0;
95
94.4M
}
96
97
/// Return true if this character is an ASCII digit: [0-9]
98
39.6M
LLVM_READONLY inline bool isDigit(unsigned char c) {
99
39.6M
  using namespace charinfo;
100
39.6M
  return (InfoTable[c] & CHAR_DIGIT) != 0;
101
39.6M
}
102
103
/// Return true if this character is a lowercase ASCII letter: [a-z]
104
453k
LLVM_READONLY inline bool isLowercase(unsigned char c) {
105
453k
  using namespace charinfo;
106
453k
  return (InfoTable[c] & CHAR_LOWER) != 0;
107
453k
}
108
109
/// Return true if this character is an uppercase ASCII letter: [A-Z]
110
935
LLVM_READONLY inline bool isUppercase(unsigned char c) {
111
935
  using namespace charinfo;
112
935
  return (InfoTable[c] & CHAR_UPPER) != 0;
113
935
}
114
115
/// Return true if this character is an ASCII letter: [a-zA-Z]
116
2.90M
LLVM_READONLY inline bool isLetter(unsigned char c) {
117
2.90M
  using namespace charinfo;
118
2.90M
  return (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER)) != 0;
119
2.90M
}
120
121
/// Return true if this character is an ASCII letter or digit: [a-zA-Z0-9]
122
5.19M
LLVM_READONLY inline bool isAlphanumeric(unsigned char c) {
123
5.19M
  using namespace charinfo;
124
5.19M
  return (InfoTable[c] & (CHAR_DIGIT|CHAR_UPPER|CHAR_LOWER)) != 0;
125
5.19M
}
126
127
/// Return true if this character is an ASCII hex digit: [0-9a-fA-F]
128
5.67M
LLVM_READONLY inline bool isHexDigit(unsigned char c) {
129
5.67M
  using namespace charinfo;
130
5.67M
  return (InfoTable[c] & (CHAR_DIGIT|CHAR_XLETTER)) != 0;
131
5.67M
}
132
133
/// Return true if this character is an ASCII punctuation character.
134
///
135
/// Note that '_' is both a punctuation character and an identifier character!
136
713k
LLVM_READONLY inline bool isPunctuation(unsigned char c) {
137
713k
  using namespace charinfo;
138
713k
  return (InfoTable[c] & (CHAR_UNDER|CHAR_PERIOD|CHAR_RAWDEL|CHAR_PUNCT)) != 0;
139
713k
}
140
141
/// Return true if this character is an ASCII printable character; that is, a
142
/// character that should take exactly one column to print in a fixed-width
143
/// terminal.
144
10.8k
LLVM_READONLY inline bool isPrintable(unsigned char c) {
145
10.8k
  using namespace charinfo;
146
10.8k
  return (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_PERIOD|CHAR_PUNCT|
147
10.8k
                          CHAR_DIGIT|CHAR_UNDER|CHAR_RAWDEL|CHAR_SPACE)) != 0;
148
10.8k
}
149
150
/// Return true if this is the body character of a C preprocessing number,
151
/// which is [a-zA-Z0-9_.].
152
215M
LLVM_READONLY inline bool isPreprocessingNumberBody(unsigned char c) {
153
215M
  using namespace charinfo;
154
215M
  return (InfoTable[c] &
155
215M
          (CHAR_UPPER|CHAR_LOWER|CHAR_DIGIT|CHAR_UNDER|CHAR_PERIOD)) != 0;
156
215M
}
157
158
/// Return true if this is the body character of a C++ raw string delimiter.
159
1.52k
LLVM_READONLY inline bool isRawStringDelimBody(unsigned char c) {
160
1.52k
  using namespace charinfo;
161
1.52k
  return (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_PERIOD|
162
1.52k
                          CHAR_DIGIT|CHAR_UNDER|CHAR_RAWDEL)) != 0;
163
1.52k
}
164
165
166
/// Converts the given ASCII character to its lowercase equivalent.
167
///
168
/// If the character is not an uppercase character, it is returned as is.
169
765
LLVM_READONLY inline char toLowercase(char c) {
170
765
  if (isUppercase(c))
171
227
    return c + 'a' - 'A';
172
538
  return c;
173
765
}
174
175
/// Converts the given ASCII character to its uppercase equivalent.
176
///
177
/// If the character is not a lowercase character, it is returned as is.
178
358k
LLVM_READONLY inline char toUppercase(char c) {
179
358k
  if (isLowercase(c))
180
344k
    return c + 'A' - 'a';
181
14.1k
  return c;
182
358k
}
183
184
185
/// Return true if this is a valid ASCII identifier.
186
///
187
/// Note that this is a very simple check; it does not accept UCNs as valid
188
/// identifier characters.
189
LLVM_READONLY inline bool isValidAsciiIdentifier(StringRef S,
190
23.4k
                                                 bool AllowDollar = false) {
191
23.4k
  if (S.empty() || 
!isAsciiIdentifierStart(S[0], AllowDollar)23.4k
)
192
22
    return false;
193
194
385k
  
for (StringRef::iterator I = S.begin(), E = S.end(); 23.3k
I != E;
++I361k
)
195
361k
    if (!isAsciiIdentifierContinue(*I, AllowDollar))
196
130
      return false;
197
198
23.2k
  return true;
199
23.3k
}
200
201
} // end namespace clang
202
203
#endif