1 //===--- TokenConcatenation.cpp - Token Concatenation Avoidance -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the TokenConcatenation class.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Lex/TokenConcatenation.h"
15 #include "clang/Basic/CharInfo.h"
16 #include "clang/Lex/Preprocessor.h"
17 #include "llvm/Support/ErrorHandling.h"
18 using namespace clang;
21 /// IsStringPrefix - Return true if Str is a string prefix.
22 /// 'L', 'u', 'U', or 'u8'. Including raw versions.
23 static bool IsStringPrefix(StringRef Str, bool CPlusPlus11) {
26 (CPlusPlus11 && (Str[0] == 'u' || Str[0] == 'U' || Str[0] == 'R'))) {
29 return true; // "L", "u", "U", and "R"
31 // Check for raw flavors. Need to make sure the first character wasn't
32 // already R. Need CPlusPlus11 check for "LR".
33 if (Str[1] == 'R' && Str[0] != 'R' && Str.size() == 2 && CPlusPlus11)
34 return true; // "LR", "uR", "UR"
36 // Check for "u8" and "u8R"
37 if (Str[0] == 'u' && Str[1] == '8') {
38 if (Str.size() == 2) return true; // "u8"
39 if (Str.size() == 3 && Str[2] == 'R') return true; // "u8R"
46 /// IsIdentifierStringPrefix - Return true if the spelling of the token
47 /// is literally 'L', 'u', 'U', or 'u8'. Including raw versions.
48 bool TokenConcatenation::IsIdentifierStringPrefix(const Token &Tok) const {
49 const LangOptions &LangOpts = PP.getLangOpts();
51 if (!Tok.needsCleaning()) {
52 if (Tok.getLength() < 1 || Tok.getLength() > 3)
54 SourceManager &SM = PP.getSourceManager();
55 const char *Ptr = SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation()));
56 return IsStringPrefix(StringRef(Ptr, Tok.getLength()),
57 LangOpts.CPlusPlus11);
60 if (Tok.getLength() < 256) {
62 const char *TokPtr = Buffer;
63 unsigned length = PP.getSpelling(Tok, TokPtr);
64 return IsStringPrefix(StringRef(TokPtr, length), LangOpts.CPlusPlus11);
67 return IsStringPrefix(StringRef(PP.getSpelling(Tok)), LangOpts.CPlusPlus11);
70 TokenConcatenation::TokenConcatenation(Preprocessor &pp) : PP(pp) {
71 memset(TokenInfo, 0, sizeof(TokenInfo));
73 // These tokens have custom code in AvoidConcat.
74 TokenInfo[tok::identifier ] |= aci_custom;
75 TokenInfo[tok::numeric_constant] |= aci_custom_firstchar;
76 TokenInfo[tok::period ] |= aci_custom_firstchar;
77 TokenInfo[tok::amp ] |= aci_custom_firstchar;
78 TokenInfo[tok::plus ] |= aci_custom_firstchar;
79 TokenInfo[tok::minus ] |= aci_custom_firstchar;
80 TokenInfo[tok::slash ] |= aci_custom_firstchar;
81 TokenInfo[tok::less ] |= aci_custom_firstchar;
82 TokenInfo[tok::greater ] |= aci_custom_firstchar;
83 TokenInfo[tok::pipe ] |= aci_custom_firstchar;
84 TokenInfo[tok::percent ] |= aci_custom_firstchar;
85 TokenInfo[tok::colon ] |= aci_custom_firstchar;
86 TokenInfo[tok::hash ] |= aci_custom_firstchar;
87 TokenInfo[tok::arrow ] |= aci_custom_firstchar;
89 // These tokens have custom code in C++11 mode.
90 if (PP.getLangOpts().CPlusPlus11) {
91 TokenInfo[tok::string_literal ] |= aci_custom;
92 TokenInfo[tok::wide_string_literal ] |= aci_custom;
93 TokenInfo[tok::utf8_string_literal ] |= aci_custom;
94 TokenInfo[tok::utf16_string_literal] |= aci_custom;
95 TokenInfo[tok::utf32_string_literal] |= aci_custom;
96 TokenInfo[tok::char_constant ] |= aci_custom;
97 TokenInfo[tok::wide_char_constant ] |= aci_custom;
98 TokenInfo[tok::utf16_char_constant ] |= aci_custom;
99 TokenInfo[tok::utf32_char_constant ] |= aci_custom;
102 // These tokens have custom code in C++17 mode.
103 if (PP.getLangOpts().CPlusPlus17)
104 TokenInfo[tok::utf8_char_constant] |= aci_custom;
106 // These tokens have custom code in C++2a mode.
107 if (PP.getLangOpts().CPlusPlus2a)
108 TokenInfo[tok::lessequal ] |= aci_custom_firstchar;
110 // These tokens change behavior if followed by an '='.
111 TokenInfo[tok::amp ] |= aci_avoid_equal; // &=
112 TokenInfo[tok::plus ] |= aci_avoid_equal; // +=
113 TokenInfo[tok::minus ] |= aci_avoid_equal; // -=
114 TokenInfo[tok::slash ] |= aci_avoid_equal; // /=
115 TokenInfo[tok::less ] |= aci_avoid_equal; // <=
116 TokenInfo[tok::greater ] |= aci_avoid_equal; // >=
117 TokenInfo[tok::pipe ] |= aci_avoid_equal; // |=
118 TokenInfo[tok::percent ] |= aci_avoid_equal; // %=
119 TokenInfo[tok::star ] |= aci_avoid_equal; // *=
120 TokenInfo[tok::exclaim ] |= aci_avoid_equal; // !=
121 TokenInfo[tok::lessless ] |= aci_avoid_equal; // <<=
122 TokenInfo[tok::greatergreater] |= aci_avoid_equal; // >>=
123 TokenInfo[tok::caret ] |= aci_avoid_equal; // ^=
124 TokenInfo[tok::equal ] |= aci_avoid_equal; // ==
127 /// GetFirstChar - Get the first character of the token \arg Tok,
128 /// avoiding calls to getSpelling where possible.
129 static char GetFirstChar(Preprocessor &PP, const Token &Tok) {
130 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
131 // Avoid spelling identifiers, the most common form of token.
132 return II->getNameStart()[0];
133 } else if (!Tok.needsCleaning()) {
134 if (Tok.isLiteral() && Tok.getLiteralData()) {
135 return *Tok.getLiteralData();
137 SourceManager &SM = PP.getSourceManager();
138 return *SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation()));
140 } else if (Tok.getLength() < 256) {
142 const char *TokPtr = Buffer;
143 PP.getSpelling(Tok, TokPtr);
146 return PP.getSpelling(Tok)[0];
150 /// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
151 /// the two individual tokens to be lexed as a single token, return true
152 /// (which causes a space to be printed between them). This allows the output
153 /// of -E mode to be lexed to the same token stream as lexing the input
156 /// This code must conservatively return true if it doesn't want to be 100%
157 /// accurate. This will cause the output to include extra space characters,
158 /// but the resulting output won't have incorrect concatenations going on.
159 /// Examples include "..", which we print with a space between, because we
160 /// don't want to track enough to tell "x.." from "...".
161 bool TokenConcatenation::AvoidConcat(const Token &PrevPrevTok,
162 const Token &PrevTok,
163 const Token &Tok) const {
164 // First, check to see if the tokens were directly adjacent in the original
165 // source. If they were, it must be okay to stick them together: if there
166 // were an issue, the tokens would have been lexed differently.
167 SourceManager &SM = PP.getSourceManager();
168 SourceLocation PrevSpellLoc = SM.getSpellingLoc(PrevTok.getLocation());
169 SourceLocation SpellLoc = SM.getSpellingLoc(Tok.getLocation());
170 if (PrevSpellLoc.getLocWithOffset(PrevTok.getLength()) == SpellLoc)
173 tok::TokenKind PrevKind = PrevTok.getKind();
174 if (!PrevTok.isAnnotation() && PrevTok.getIdentifierInfo())
175 PrevKind = tok::identifier; // Language keyword or named operator.
177 // Look up information on when we should avoid concatenation with prevtok.
178 unsigned ConcatInfo = TokenInfo[PrevKind];
180 // If prevtok never causes a problem for anything after it, return quickly.
181 if (ConcatInfo == 0) return false;
183 if (ConcatInfo & aci_avoid_equal) {
184 // If the next token is '=' or '==', avoid concatenation.
185 if (Tok.isOneOf(tok::equal, tok::equalequal))
187 ConcatInfo &= ~aci_avoid_equal;
189 if (Tok.isAnnotation()) {
190 // Modules annotation can show up when generated automatically for includes.
191 assert(Tok.isOneOf(tok::annot_module_include, tok::annot_module_begin,
192 tok::annot_module_end) &&
193 "unexpected annotation in AvoidConcat");
200 // Basic algorithm: we look at the first character of the second token, and
201 // determine whether it, if appended to the first token, would form (or
202 // would contribute) to a larger token if concatenated.
204 if (ConcatInfo & aci_custom) {
205 // If the token does not need to know the first character, don't get it.
207 FirstChar = GetFirstChar(PP, Tok);
212 llvm_unreachable("InitAvoidConcatTokenInfo built wrong");
214 case tok::raw_identifier:
215 llvm_unreachable("tok::raw_identifier in non-raw lexing mode!");
217 case tok::string_literal:
218 case tok::wide_string_literal:
219 case tok::utf8_string_literal:
220 case tok::utf16_string_literal:
221 case tok::utf32_string_literal:
222 case tok::char_constant:
223 case tok::wide_char_constant:
224 case tok::utf8_char_constant:
225 case tok::utf16_char_constant:
226 case tok::utf32_char_constant:
227 if (!PP.getLangOpts().CPlusPlus11)
230 // In C++11, a string or character literal followed by an identifier is a
232 if (Tok.getIdentifierInfo())
235 // A ud-suffix is an identifier. If the previous token ends with one, treat
236 // it as an identifier.
237 if (!PrevTok.hasUDSuffix())
240 case tok::identifier: // id+id or id+number or id+L"foo".
241 // id+'.'... will not append.
242 if (Tok.is(tok::numeric_constant))
243 return GetFirstChar(PP, Tok) != '.';
245 if (Tok.getIdentifierInfo() ||
246 Tok.isOneOf(tok::wide_string_literal, tok::utf8_string_literal,
247 tok::utf16_string_literal, tok::utf32_string_literal,
248 tok::wide_char_constant, tok::utf8_char_constant,
249 tok::utf16_char_constant, tok::utf32_char_constant))
252 // If this isn't identifier + string, we're done.
253 if (Tok.isNot(tok::char_constant) && Tok.isNot(tok::string_literal))
256 // Otherwise, this is a narrow character or string. If the *identifier*
257 // is a literal 'L', 'u8', 'u' or 'U', avoid pasting L "foo" -> L"foo".
258 return IsIdentifierStringPrefix(PrevTok);
260 case tok::numeric_constant:
261 return isPreprocessingNumberBody(FirstChar) ||
262 FirstChar == '+' || FirstChar == '-';
263 case tok::period: // ..., .*, .1234
264 return (FirstChar == '.' && PrevPrevTok.is(tok::period)) ||
265 isDigit(FirstChar) ||
266 (PP.getLangOpts().CPlusPlus && FirstChar == '*');
268 return FirstChar == '&';
269 case tok::plus: // ++
270 return FirstChar == '+';
271 case tok::minus: // --, ->, ->*
272 return FirstChar == '-' || FirstChar == '>';
273 case tok::slash: //, /*, //
274 return FirstChar == '*' || FirstChar == '/';
275 case tok::less: // <<, <<=, <:, <%
276 return FirstChar == '<' || FirstChar == ':' || FirstChar == '%';
277 case tok::greater: // >>, >>=
278 return FirstChar == '>';
279 case tok::pipe: // ||
280 return FirstChar == '|';
281 case tok::percent: // %>, %:
282 return FirstChar == '>' || FirstChar == ':';
283 case tok::colon: // ::, :>
284 return FirstChar == '>' ||
285 (PP.getLangOpts().CPlusPlus && FirstChar == ':');
286 case tok::hash: // ##, #@, %:%:
287 return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
288 case tok::arrow: // ->*
289 return PP.getLangOpts().CPlusPlus && FirstChar == '*';
290 case tok::lessequal: // <=> (C++2a)
291 return PP.getLangOpts().CPlusPlus2a && FirstChar == '>';