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