Coverage Report

Created: 2019-02-20 07:29

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
741M
LLVM_READNONE inline bool isASCII(char c) {
43
741M
  return static_cast<unsigned char>(c) <= 127;
44
741M
}
45
46
/// Returns true if this is a valid first character of a C identifier,
47
/// which is [a-zA-Z_].
48
LLVM_READONLY inline bool isIdentifierHead(unsigned char c,
49
3.27M
                                           bool AllowDollar = false) {
50
3.27M
  using namespace charinfo;
51
3.27M
  if (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_UNDER))
52
5.04k
    return true;
53
3.27M
  return AllowDollar && 
c == '$'5
;
54
3.27M
}
55
56
/// Returns true if this is a body character of a C identifier,
57
/// which is [a-zA-Z0-9_].
58
LLVM_READONLY inline bool isIdentifierBody(unsigned char c,
59
12.8G
                                           bool AllowDollar = false) {
60
12.8G
  using namespace charinfo;
61
12.8G
  if (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_DIGIT|CHAR_UNDER))
62
12.1G
    return true;
63
692M
  return AllowDollar && 
c == '$'3.71k
;
64
692M
}
65
66
/// Returns true if this character is horizontal ASCII whitespace:
67
/// ' ', '\\t', '\\f', '\\v'.
68
///
69
/// Note that this returns false for '\\0'.
70
1.62G
LLVM_READONLY inline bool isHorizontalWhitespace(unsigned char c) {
71
1.62G
  using namespace charinfo;
72
1.62G
  return (InfoTable[c] & (CHAR_HORZ_WS|CHAR_SPACE)) != 0;
73
1.62G
}
74
75
/// Returns true if this character is vertical ASCII whitespace: '\\n', '\\r'.
76
///
77
/// Note that this returns false for '\\0'.
78
506M
LLVM_READONLY inline bool isVerticalWhitespace(unsigned char c) {
79
506M
  using namespace charinfo;
80
506M
  return (InfoTable[c] & CHAR_VERT_WS) != 0;
81
506M
}
82
83
/// Return true if this character is horizontal or vertical ASCII whitespace:
84
/// ' ', '\\t', '\\f', '\\v', '\\n', '\\r'.
85
///
86
/// Note that this returns false for '\\0'.
87
10.3M
LLVM_READONLY inline bool isWhitespace(unsigned char c) {
88
10.3M
  using namespace charinfo;
89
10.3M
  return (InfoTable[c] & (CHAR_HORZ_WS|CHAR_VERT_WS|CHAR_SPACE)) != 0;
90
10.3M
}
91
92
/// Return true if this character is an ASCII digit: [0-9]
93
18.5M
LLVM_READONLY inline bool isDigit(unsigned char c) {
94
18.5M
  using namespace charinfo;
95
18.5M
  return (InfoTable[c] & CHAR_DIGIT) != 0;
96
18.5M
}
97
98
/// Return true if this character is a lowercase ASCII letter: [a-z]
99
39.1k
LLVM_READONLY inline bool isLowercase(unsigned char c) {
100
39.1k
  using namespace charinfo;
101
39.1k
  return (InfoTable[c] & CHAR_LOWER) != 0;
102
39.1k
}
103
104
/// Return true if this character is an uppercase ASCII letter: [A-Z]
105
25.0k
LLVM_READONLY inline bool isUppercase(unsigned char c) {
106
25.0k
  using namespace charinfo;
107
25.0k
  return (InfoTable[c] & CHAR_UPPER) != 0;
108
25.0k
}
109
110
/// Return true if this character is an ASCII letter: [a-zA-Z]
111
147k
LLVM_READONLY inline bool isLetter(unsigned char c) {
112
147k
  using namespace charinfo;
113
147k
  return (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER)) != 0;
114
147k
}
115
116
/// Return true if this character is an ASCII letter or digit: [a-zA-Z0-9]
117
1.58M
LLVM_READONLY inline bool isAlphanumeric(unsigned char c) {
118
1.58M
  using namespace charinfo;
119
1.58M
  return (InfoTable[c] & (CHAR_DIGIT|CHAR_UPPER|CHAR_LOWER)) != 0;
120
1.58M
}
121
122
/// Return true if this character is an ASCII hex digit: [0-9a-fA-F]
123
2.53M
LLVM_READONLY inline bool isHexDigit(unsigned char c) {
124
2.53M
  using namespace charinfo;
125
2.53M
  return (InfoTable[c] & (CHAR_DIGIT|CHAR_XLETTER)) != 0;
126
2.53M
}
127
128
/// Return true if this character is an ASCII punctuation character.
129
///
130
/// Note that '_' is both a punctuation character and an identifier character!
131
404k
LLVM_READONLY inline bool isPunctuation(unsigned char c) {
132
404k
  using namespace charinfo;
133
404k
  return (InfoTable[c] & (CHAR_UNDER|CHAR_PERIOD|CHAR_RAWDEL|CHAR_PUNCT)) != 0;
134
404k
}
135
136
/// Return true if this character is an ASCII printable character; that is, a
137
/// character that should take exactly one column to print in a fixed-width
138
/// terminal.
139
7.74k
LLVM_READONLY inline bool isPrintable(unsigned char c) {
140
7.74k
  using namespace charinfo;
141
7.74k
  return (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_PERIOD|CHAR_PUNCT|
142
7.74k
                          CHAR_DIGIT|CHAR_UNDER|CHAR_RAWDEL|CHAR_SPACE)) != 0;
143
7.74k
}
144
145
/// Return true if this is the body character of a C preprocessing number,
146
/// which is [a-zA-Z0-9_.].
147
157M
LLVM_READONLY inline bool isPreprocessingNumberBody(unsigned char c) {
148
157M
  using namespace charinfo;
149
157M
  return (InfoTable[c] &
150
157M
          (CHAR_UPPER|CHAR_LOWER|CHAR_DIGIT|CHAR_UNDER|CHAR_PERIOD)) != 0;
151
157M
}
152
153
/// Return true if this is the body character of a C++ raw string delimiter.
154
1.28k
LLVM_READONLY inline bool isRawStringDelimBody(unsigned char c) {
155
1.28k
  using namespace charinfo;
156
1.28k
  return (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_PERIOD|
157
1.28k
                          CHAR_DIGIT|CHAR_UNDER|CHAR_RAWDEL)) != 0;
158
1.28k
}
159
160
161
/// Converts the given ASCII character to its lowercase equivalent.
162
///
163
/// If the character is not an uppercase character, it is returned as is.
164
681
LLVM_READONLY inline char toLowercase(char c) {
165
681
  if (isUppercase(c))
166
216
    return c + 'a' - 'A';
167
465
  return c;
168
465
}
169
170
/// Converts the given ASCII character to its uppercase equivalent.
171
///
172
/// If the character is not a lowercase character, it is returned as is.
173
26.4k
LLVM_READONLY inline char toUppercase(char c) {
174
26.4k
  if (isLowercase(c))
175
24.1k
    return c + 'A' - 'a';
176
2.26k
  return c;
177
2.26k
}
178
179
180
/// Return true if this is a valid ASCII identifier.
181
///
182
/// Note that this is a very simple check; it does not accept UCNs as valid
183
/// identifier characters.
184
LLVM_READONLY inline bool isValidIdentifier(StringRef S,
185
1.41k
                                            bool AllowDollar = false) {
186
1.41k
  if (S.empty() || 
!isIdentifierHead(S[0], AllowDollar)1.41k
)
187
21
    return false;
188
1.39k
189
11.3k
  
for (StringRef::iterator I = S.begin(), E = S.end(); 1.39k
I != E;
++I9.92k
)
190
9.96k
    if (!isIdentifierBody(*I, AllowDollar))
191
43
      return false;
192
1.39k
193
1.39k
  
return true1.35k
;
194
1.39k
}
195
196
} // end namespace clang
197
198
#endif