blob: 08327496ab8a8280c109c878300bbcf2c6eb5e10 [file] [log] [blame]
Chris Lattner644d4522009-02-13 00:46:04 +00001//===--- TokenConcatenation.cpp - Token Concatenation Avoidance -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the TokenConcatenation class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/TokenConcatenation.h"
Jordan Rosea08ed592013-02-08 22:30:31 +000015#include "clang/Basic/CharInfo.h"
Chris Lattner644d4522009-02-13 00:46:04 +000016#include "clang/Lex/Preprocessor.h"
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +000017#include "llvm/Support/ErrorHandling.h"
Mike Stump11289f42009-09-09 15:08:12 +000018using namespace clang;
Chris Lattner644d4522009-02-13 00:46:04 +000019
20
Craig Topper54edcca2011-08-11 04:06:15 +000021/// IsStringPrefix - Return true if Str is a string prefix.
22/// 'L', 'u', 'U', or 'u8'. Including raw versions.
Richard Smith2bf7fdb2013-01-02 11:42:31 +000023static bool IsStringPrefix(StringRef Str, bool CPlusPlus11) {
Craig Topper54edcca2011-08-11 04:06:15 +000024
25 if (Str[0] == 'L' ||
Richard Smith2bf7fdb2013-01-02 11:42:31 +000026 (CPlusPlus11 && (Str[0] == 'u' || Str[0] == 'U' || Str[0] == 'R'))) {
Craig Topper54edcca2011-08-11 04:06:15 +000027
28 if (Str.size() == 1)
29 return true; // "L", "u", "U", and "R"
30
31 // Check for raw flavors. Need to make sure the first character wasn't
Richard Smith2bf7fdb2013-01-02 11:42:31 +000032 // already R. Need CPlusPlus11 check for "LR".
33 if (Str[1] == 'R' && Str[0] != 'R' && Str.size() == 2 && CPlusPlus11)
Craig Topper54edcca2011-08-11 04:06:15 +000034 return true; // "LR", "uR", "UR"
35
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"
40 }
41 }
42
43 return false;
44}
45
Douglas Gregorfb65e592011-07-27 05:40:30 +000046/// IsIdentifierStringPrefix - Return true if the spelling of the token
Craig Topper54edcca2011-08-11 04:06:15 +000047/// is literally 'L', 'u', 'U', or 'u8'. Including raw versions.
Douglas Gregorfb65e592011-07-27 05:40:30 +000048bool TokenConcatenation::IsIdentifierStringPrefix(const Token &Tok) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +000049 const LangOptions &LangOpts = PP.getLangOpts();
Douglas Gregorfb65e592011-07-27 05:40:30 +000050
Chris Lattner644d4522009-02-13 00:46:04 +000051 if (!Tok.needsCleaning()) {
Craig Topper54edcca2011-08-11 04:06:15 +000052 if (Tok.getLength() < 1 || Tok.getLength() > 3)
Douglas Gregorfb65e592011-07-27 05:40:30 +000053 return false;
Chris Lattner644d4522009-02-13 00:46:04 +000054 SourceManager &SM = PP.getSourceManager();
Douglas Gregorfb65e592011-07-27 05:40:30 +000055 const char *Ptr = SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation()));
Craig Topper54edcca2011-08-11 04:06:15 +000056 return IsStringPrefix(StringRef(Ptr, Tok.getLength()),
Richard Smith2bf7fdb2013-01-02 11:42:31 +000057 LangOpts.CPlusPlus11);
Chris Lattner644d4522009-02-13 00:46:04 +000058 }
Mike Stump11289f42009-09-09 15:08:12 +000059
Chris Lattner644d4522009-02-13 00:46:04 +000060 if (Tok.getLength() < 256) {
61 char Buffer[256];
62 const char *TokPtr = Buffer;
Douglas Gregorfb65e592011-07-27 05:40:30 +000063 unsigned length = PP.getSpelling(Tok, TokPtr);
Richard Smith2bf7fdb2013-01-02 11:42:31 +000064 return IsStringPrefix(StringRef(TokPtr, length), LangOpts.CPlusPlus11);
Chris Lattner644d4522009-02-13 00:46:04 +000065 }
Mike Stump11289f42009-09-09 15:08:12 +000066
Richard Smith2bf7fdb2013-01-02 11:42:31 +000067 return IsStringPrefix(StringRef(PP.getSpelling(Tok)), LangOpts.CPlusPlus11);
Chris Lattner644d4522009-02-13 00:46:04 +000068}
69
70TokenConcatenation::TokenConcatenation(Preprocessor &pp) : PP(pp) {
71 memset(TokenInfo, 0, sizeof(TokenInfo));
Mike Stump11289f42009-09-09 15:08:12 +000072
Chris Lattner644d4522009-02-13 00:46:04 +000073 // 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;
Mike Stump11289f42009-09-09 15:08:12 +000088
Richard Smithd67aea22012-03-06 03:21:47 +000089 // These tokens have custom code in C++11 mode.
Richard Smith2bf7fdb2013-01-02 11:42:31 +000090 if (PP.getLangOpts().CPlusPlus11) {
Richard Smithd67aea22012-03-06 03:21:47 +000091 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;
100 }
101
Richard Smith3e3a7052014-11-08 06:08:42 +0000102 // These tokens have custom code in C++1z mode.
103 if (PP.getLangOpts().CPlusPlus1z)
104 TokenInfo[tok::utf8_char_constant] |= aci_custom;
105
Chris Lattner644d4522009-02-13 00:46:04 +0000106 // These tokens change behavior if followed by an '='.
107 TokenInfo[tok::amp ] |= aci_avoid_equal; // &=
108 TokenInfo[tok::plus ] |= aci_avoid_equal; // +=
109 TokenInfo[tok::minus ] |= aci_avoid_equal; // -=
110 TokenInfo[tok::slash ] |= aci_avoid_equal; // /=
111 TokenInfo[tok::less ] |= aci_avoid_equal; // <=
112 TokenInfo[tok::greater ] |= aci_avoid_equal; // >=
113 TokenInfo[tok::pipe ] |= aci_avoid_equal; // |=
114 TokenInfo[tok::percent ] |= aci_avoid_equal; // %=
115 TokenInfo[tok::star ] |= aci_avoid_equal; // *=
116 TokenInfo[tok::exclaim ] |= aci_avoid_equal; // !=
117 TokenInfo[tok::lessless ] |= aci_avoid_equal; // <<=
Chris Lattner80dbccd2010-03-26 17:10:02 +0000118 TokenInfo[tok::greatergreater] |= aci_avoid_equal; // >>=
Chris Lattner644d4522009-02-13 00:46:04 +0000119 TokenInfo[tok::caret ] |= aci_avoid_equal; // ^=
120 TokenInfo[tok::equal ] |= aci_avoid_equal; // ==
121}
122
Daniel Dunbardc78bd92009-03-18 03:32:24 +0000123/// GetFirstChar - Get the first character of the token \arg Tok,
124/// avoiding calls to getSpelling where possible.
125static char GetFirstChar(Preprocessor &PP, const Token &Tok) {
126 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
127 // Avoid spelling identifiers, the most common form of token.
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000128 return II->getNameStart()[0];
Daniel Dunbardc78bd92009-03-18 03:32:24 +0000129 } else if (!Tok.needsCleaning()) {
130 if (Tok.isLiteral() && Tok.getLiteralData()) {
131 return *Tok.getLiteralData();
132 } else {
133 SourceManager &SM = PP.getSourceManager();
134 return *SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation()));
135 }
136 } else if (Tok.getLength() < 256) {
137 char Buffer[256];
138 const char *TokPtr = Buffer;
139 PP.getSpelling(Tok, TokPtr);
140 return TokPtr[0];
141 } else {
142 return PP.getSpelling(Tok)[0];
143 }
144}
145
Chris Lattner644d4522009-02-13 00:46:04 +0000146/// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
147/// the two individual tokens to be lexed as a single token, return true
148/// (which causes a space to be printed between them). This allows the output
149/// of -E mode to be lexed to the same token stream as lexing the input
150/// directly would.
151///
152/// This code must conservatively return true if it doesn't want to be 100%
153/// accurate. This will cause the output to include extra space characters,
154/// but the resulting output won't have incorrect concatenations going on.
155/// Examples include "..", which we print with a space between, because we
156/// don't want to track enough to tell "x.." from "...".
Chris Lattner0384e6352010-04-14 03:57:19 +0000157bool TokenConcatenation::AvoidConcat(const Token &PrevPrevTok,
158 const Token &PrevTok,
Chris Lattner644d4522009-02-13 00:46:04 +0000159 const Token &Tok) const {
Chris Lattnerca515cc2009-04-21 23:28:41 +0000160 // First, check to see if the tokens were directly adjacent in the original
161 // source. If they were, it must be okay to stick them together: if there
162 // were an issue, the tokens would have been lexed differently.
Argyrios Kyrtzidis005206a82013-01-29 20:28:24 +0000163 SourceManager &SM = PP.getSourceManager();
164 SourceLocation PrevSpellLoc = SM.getSpellingLoc(PrevTok.getLocation());
165 SourceLocation SpellLoc = SM.getSpellingLoc(Tok.getLocation());
166 if (PrevSpellLoc.getLocWithOffset(PrevTok.getLength()) == SpellLoc)
Chris Lattnerca515cc2009-04-21 23:28:41 +0000167 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000168
Chris Lattner644d4522009-02-13 00:46:04 +0000169 tok::TokenKind PrevKind = PrevTok.getKind();
Ben Langmuir5418f402014-09-10 21:29:41 +0000170 if (!PrevTok.isAnnotation() && PrevTok.getIdentifierInfo())
171 PrevKind = tok::identifier; // Language keyword or named operator.
Mike Stump11289f42009-09-09 15:08:12 +0000172
Chris Lattner644d4522009-02-13 00:46:04 +0000173 // Look up information on when we should avoid concatenation with prevtok.
174 unsigned ConcatInfo = TokenInfo[PrevKind];
Mike Stump11289f42009-09-09 15:08:12 +0000175
Chris Lattner644d4522009-02-13 00:46:04 +0000176 // If prevtok never causes a problem for anything after it, return quickly.
177 if (ConcatInfo == 0) return false;
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattner644d4522009-02-13 00:46:04 +0000179 if (ConcatInfo & aci_avoid_equal) {
180 // If the next token is '=' or '==', avoid concatenation.
181 if (Tok.is(tok::equal) || Tok.is(tok::equalequal))
182 return true;
183 ConcatInfo &= ~aci_avoid_equal;
184 }
Ben Langmuir5418f402014-09-10 21:29:41 +0000185 if (Tok.isAnnotation()) {
186 // Modules annotation can show up when generated automatically for includes.
187 assert((Tok.is(tok::annot_module_include) ||
188 Tok.is(tok::annot_module_begin) ||
189 Tok.is(tok::annot_module_end)) &&
190 "unexpected annotation in AvoidConcat");
191 ConcatInfo = 0;
192 }
Mike Stump11289f42009-09-09 15:08:12 +0000193
Chris Lattner644d4522009-02-13 00:46:04 +0000194 if (ConcatInfo == 0) return false;
Mike Stump11289f42009-09-09 15:08:12 +0000195
Chris Lattner644d4522009-02-13 00:46:04 +0000196 // Basic algorithm: we look at the first character of the second token, and
197 // determine whether it, if appended to the first token, would form (or
198 // would contribute) to a larger token if concatenated.
199 char FirstChar = 0;
200 if (ConcatInfo & aci_custom) {
201 // If the token does not need to know the first character, don't get it.
Chris Lattner644d4522009-02-13 00:46:04 +0000202 } else {
Daniel Dunbardc78bd92009-03-18 03:32:24 +0000203 FirstChar = GetFirstChar(PP, Tok);
Chris Lattner644d4522009-02-13 00:46:04 +0000204 }
Mike Stump11289f42009-09-09 15:08:12 +0000205
Chris Lattner644d4522009-02-13 00:46:04 +0000206 switch (PrevKind) {
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000207 default:
208 llvm_unreachable("InitAvoidConcatTokenInfo built wrong");
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000209
210 case tok::raw_identifier:
211 llvm_unreachable("tok::raw_identifier in non-raw lexing mode!");
Abramo Bagnaraea4f7c72010-12-22 08:23:18 +0000212
Richard Smithd67aea22012-03-06 03:21:47 +0000213 case tok::string_literal:
214 case tok::wide_string_literal:
215 case tok::utf8_string_literal:
216 case tok::utf16_string_literal:
217 case tok::utf32_string_literal:
218 case tok::char_constant:
219 case tok::wide_char_constant:
Richard Smith3e3a7052014-11-08 06:08:42 +0000220 case tok::utf8_char_constant:
Richard Smithd67aea22012-03-06 03:21:47 +0000221 case tok::utf16_char_constant:
222 case tok::utf32_char_constant:
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000223 if (!PP.getLangOpts().CPlusPlus11)
Richard Smithd67aea22012-03-06 03:21:47 +0000224 return false;
225
226 // In C++11, a string or character literal followed by an identifier is a
227 // single token.
Ben Langmuir5eb7cb72014-01-30 21:50:18 +0000228 if (Tok.getIdentifierInfo())
Richard Smithd67aea22012-03-06 03:21:47 +0000229 return true;
230
231 // A ud-suffix is an identifier. If the previous token ends with one, treat
232 // it as an identifier.
233 if (!PrevTok.hasUDSuffix())
234 return false;
235 // FALL THROUGH.
Mike Stump11289f42009-09-09 15:08:12 +0000236 case tok::identifier: // id+id or id+number or id+L"foo".
Daniel Dunbardc78bd92009-03-18 03:32:24 +0000237 // id+'.'... will not append.
238 if (Tok.is(tok::numeric_constant))
239 return GetFirstChar(PP, Tok) != '.';
240
Douglas Gregorfb65e592011-07-27 05:40:30 +0000241 if (Tok.getIdentifierInfo() || Tok.is(tok::wide_string_literal) ||
242 Tok.is(tok::utf8_string_literal) || Tok.is(tok::utf16_string_literal) ||
243 Tok.is(tok::utf32_string_literal) || Tok.is(tok::wide_char_constant) ||
Richard Smith3e3a7052014-11-08 06:08:42 +0000244 Tok.is(tok::utf8_char_constant) || Tok.is(tok::utf16_char_constant) ||
245 Tok.is(tok::utf32_char_constant))
Chris Lattner644d4522009-02-13 00:46:04 +0000246 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000247
Chris Lattner644d4522009-02-13 00:46:04 +0000248 // If this isn't identifier + string, we're done.
249 if (Tok.isNot(tok::char_constant) && Tok.isNot(tok::string_literal))
250 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000251
Chris Lattner644d4522009-02-13 00:46:04 +0000252 // Otherwise, this is a narrow character or string. If the *identifier*
Douglas Gregorfb65e592011-07-27 05:40:30 +0000253 // is a literal 'L', 'u8', 'u' or 'U', avoid pasting L "foo" -> L"foo".
254 return IsIdentifierStringPrefix(PrevTok);
Richard Smithd67aea22012-03-06 03:21:47 +0000255
Chris Lattner644d4522009-02-13 00:46:04 +0000256 case tok::numeric_constant:
Jordan Rosea08ed592013-02-08 22:30:31 +0000257 return isPreprocessingNumberBody(FirstChar) ||
258 FirstChar == '+' || FirstChar == '-';
Chris Lattner644d4522009-02-13 00:46:04 +0000259 case tok::period: // ..., .*, .1234
Chris Lattner0384e6352010-04-14 03:57:19 +0000260 return (FirstChar == '.' && PrevPrevTok.is(tok::period)) ||
Jordan Rosea08ed592013-02-08 22:30:31 +0000261 isDigit(FirstChar) ||
262 (PP.getLangOpts().CPlusPlus && FirstChar == '*');
Chris Lattner644d4522009-02-13 00:46:04 +0000263 case tok::amp: // &&
264 return FirstChar == '&';
265 case tok::plus: // ++
266 return FirstChar == '+';
267 case tok::minus: // --, ->, ->*
268 return FirstChar == '-' || FirstChar == '>';
269 case tok::slash: //, /*, //
270 return FirstChar == '*' || FirstChar == '/';
271 case tok::less: // <<, <<=, <:, <%
272 return FirstChar == '<' || FirstChar == ':' || FirstChar == '%';
273 case tok::greater: // >>, >>=
274 return FirstChar == '>';
275 case tok::pipe: // ||
276 return FirstChar == '|';
277 case tok::percent: // %>, %:
Eli Friedman98dbc1c2009-05-27 22:33:06 +0000278 return FirstChar == '>' || FirstChar == ':';
Chris Lattner644d4522009-02-13 00:46:04 +0000279 case tok::colon: // ::, :>
Eli Friedmandd2ab962009-06-15 19:48:50 +0000280 return FirstChar == '>' ||
David Blaikiebbafb8a2012-03-11 07:00:24 +0000281 (PP.getLangOpts().CPlusPlus && FirstChar == ':');
Chris Lattner644d4522009-02-13 00:46:04 +0000282 case tok::hash: // ##, #@, %:%:
283 return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
284 case tok::arrow: // ->*
David Blaikiebbafb8a2012-03-11 07:00:24 +0000285 return PP.getLangOpts().CPlusPlus && FirstChar == '*';
Chris Lattner644d4522009-02-13 00:46:04 +0000286 }
287}