blob: ca7e55d863c01b32b4832dcaec16783050181e33 [file] [log] [blame]
Chris Lattnerd7038e12009-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"
15#include "clang/Lex/Preprocessor.h"
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +000016#include "llvm/Support/ErrorHandling.h"
Mike Stump1eb44332009-09-09 15:08:12 +000017using namespace clang;
Chris Lattnerd7038e12009-02-13 00:46:04 +000018
19
Craig Topper2fa4e862011-08-11 04:06:15 +000020/// IsStringPrefix - Return true if Str is a string prefix.
21/// 'L', 'u', 'U', or 'u8'. Including raw versions.
Craig Topper03720fc2011-08-11 05:10:55 +000022static bool IsStringPrefix(StringRef Str, bool CPlusPlus0x) {
Craig Topper2fa4e862011-08-11 04:06:15 +000023
24 if (Str[0] == 'L' ||
25 (CPlusPlus0x && (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 CPlusPlus0x check for "LR".
32 if (Str[1] == 'R' && Str[0] != 'R' && Str.size() == 2 && CPlusPlus0x)
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
Douglas Gregor5cee1192011-07-27 05:40:30 +000045/// IsIdentifierStringPrefix - Return true if the spelling of the token
Craig Topper2fa4e862011-08-11 04:06:15 +000046/// is literally 'L', 'u', 'U', or 'u8'. Including raw versions.
Douglas Gregor5cee1192011-07-27 05:40:30 +000047bool TokenConcatenation::IsIdentifierStringPrefix(const Token &Tok) const {
48 const LangOptions &LangOpts = PP.getLangOptions();
49
Chris Lattnerd7038e12009-02-13 00:46:04 +000050 if (!Tok.needsCleaning()) {
Craig Topper2fa4e862011-08-11 04:06:15 +000051 if (Tok.getLength() < 1 || Tok.getLength() > 3)
Douglas Gregor5cee1192011-07-27 05:40:30 +000052 return false;
Chris Lattnerd7038e12009-02-13 00:46:04 +000053 SourceManager &SM = PP.getSourceManager();
Douglas Gregor5cee1192011-07-27 05:40:30 +000054 const char *Ptr = SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation()));
Craig Topper2fa4e862011-08-11 04:06:15 +000055 return IsStringPrefix(StringRef(Ptr, Tok.getLength()),
56 LangOpts.CPlusPlus0x);
Chris Lattnerd7038e12009-02-13 00:46:04 +000057 }
Mike Stump1eb44332009-09-09 15:08:12 +000058
Chris Lattnerd7038e12009-02-13 00:46:04 +000059 if (Tok.getLength() < 256) {
60 char Buffer[256];
61 const char *TokPtr = Buffer;
Douglas Gregor5cee1192011-07-27 05:40:30 +000062 unsigned length = PP.getSpelling(Tok, TokPtr);
Craig Topper2fa4e862011-08-11 04:06:15 +000063 return IsStringPrefix(StringRef(TokPtr, length), LangOpts.CPlusPlus0x);
Chris Lattnerd7038e12009-02-13 00:46:04 +000064 }
Mike Stump1eb44332009-09-09 15:08:12 +000065
Craig Topper2fa4e862011-08-11 04:06:15 +000066 return IsStringPrefix(StringRef(PP.getSpelling(Tok)), LangOpts.CPlusPlus0x);
Chris Lattnerd7038e12009-02-13 00:46:04 +000067}
68
69TokenConcatenation::TokenConcatenation(Preprocessor &pp) : PP(pp) {
70 memset(TokenInfo, 0, sizeof(TokenInfo));
Mike Stump1eb44332009-09-09 15:08:12 +000071
Chris Lattnerd7038e12009-02-13 00:46:04 +000072 // 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;
Mike Stump1eb44332009-09-09 15:08:12 +000087
Richard Smith99831e42012-03-06 03:21:47 +000088 // These tokens have custom code in C++11 mode.
89 if (PP.getLangOptions().CPlusPlus0x) {
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
Chris Lattnerd7038e12009-02-13 00:46:04 +0000101 // These tokens change behavior if followed by an '='.
102 TokenInfo[tok::amp ] |= aci_avoid_equal; // &=
103 TokenInfo[tok::plus ] |= aci_avoid_equal; // +=
104 TokenInfo[tok::minus ] |= aci_avoid_equal; // -=
105 TokenInfo[tok::slash ] |= aci_avoid_equal; // /=
106 TokenInfo[tok::less ] |= aci_avoid_equal; // <=
107 TokenInfo[tok::greater ] |= aci_avoid_equal; // >=
108 TokenInfo[tok::pipe ] |= aci_avoid_equal; // |=
109 TokenInfo[tok::percent ] |= aci_avoid_equal; // %=
110 TokenInfo[tok::star ] |= aci_avoid_equal; // *=
111 TokenInfo[tok::exclaim ] |= aci_avoid_equal; // !=
112 TokenInfo[tok::lessless ] |= aci_avoid_equal; // <<=
Chris Lattner86851102010-03-26 17:10:02 +0000113 TokenInfo[tok::greatergreater] |= aci_avoid_equal; // >>=
Chris Lattnerd7038e12009-02-13 00:46:04 +0000114 TokenInfo[tok::caret ] |= aci_avoid_equal; // ^=
115 TokenInfo[tok::equal ] |= aci_avoid_equal; // ==
116}
117
Daniel Dunbar99c76222009-03-18 03:32:24 +0000118/// GetFirstChar - Get the first character of the token \arg Tok,
119/// avoiding calls to getSpelling where possible.
120static char GetFirstChar(Preprocessor &PP, const Token &Tok) {
121 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
122 // Avoid spelling identifiers, the most common form of token.
Daniel Dunbare013d682009-10-18 20:26:12 +0000123 return II->getNameStart()[0];
Daniel Dunbar99c76222009-03-18 03:32:24 +0000124 } else if (!Tok.needsCleaning()) {
125 if (Tok.isLiteral() && Tok.getLiteralData()) {
126 return *Tok.getLiteralData();
127 } else {
128 SourceManager &SM = PP.getSourceManager();
129 return *SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation()));
130 }
131 } else if (Tok.getLength() < 256) {
132 char Buffer[256];
133 const char *TokPtr = Buffer;
134 PP.getSpelling(Tok, TokPtr);
135 return TokPtr[0];
136 } else {
137 return PP.getSpelling(Tok)[0];
138 }
139}
140
Chris Lattnerd7038e12009-02-13 00:46:04 +0000141/// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
142/// the two individual tokens to be lexed as a single token, return true
143/// (which causes a space to be printed between them). This allows the output
144/// of -E mode to be lexed to the same token stream as lexing the input
145/// directly would.
146///
147/// This code must conservatively return true if it doesn't want to be 100%
148/// accurate. This will cause the output to include extra space characters,
149/// but the resulting output won't have incorrect concatenations going on.
150/// Examples include "..", which we print with a space between, because we
151/// don't want to track enough to tell "x.." from "...".
Chris Lattner88773212010-04-14 03:57:19 +0000152bool TokenConcatenation::AvoidConcat(const Token &PrevPrevTok,
153 const Token &PrevTok,
Chris Lattnerd7038e12009-02-13 00:46:04 +0000154 const Token &Tok) const {
Chris Lattnere1614bb2009-04-21 23:28:41 +0000155 // First, check to see if the tokens were directly adjacent in the original
156 // source. If they were, it must be okay to stick them together: if there
157 // were an issue, the tokens would have been lexed differently.
158 if (PrevTok.getLocation().isFileID() && Tok.getLocation().isFileID() &&
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000159 PrevTok.getLocation().getLocWithOffset(PrevTok.getLength()) ==
Chris Lattnere1614bb2009-04-21 23:28:41 +0000160 Tok.getLocation())
161 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Chris Lattnerd7038e12009-02-13 00:46:04 +0000163 tok::TokenKind PrevKind = PrevTok.getKind();
164 if (PrevTok.getIdentifierInfo()) // Language keyword or named operator.
165 PrevKind = tok::identifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Chris Lattnerd7038e12009-02-13 00:46:04 +0000167 // Look up information on when we should avoid concatenation with prevtok.
168 unsigned ConcatInfo = TokenInfo[PrevKind];
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Chris Lattnerd7038e12009-02-13 00:46:04 +0000170 // If prevtok never causes a problem for anything after it, return quickly.
171 if (ConcatInfo == 0) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Chris Lattnerd7038e12009-02-13 00:46:04 +0000173 if (ConcatInfo & aci_avoid_equal) {
174 // If the next token is '=' or '==', avoid concatenation.
175 if (Tok.is(tok::equal) || Tok.is(tok::equalequal))
176 return true;
177 ConcatInfo &= ~aci_avoid_equal;
178 }
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattnerd7038e12009-02-13 00:46:04 +0000180 if (ConcatInfo == 0) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Chris Lattnerd7038e12009-02-13 00:46:04 +0000182 // Basic algorithm: we look at the first character of the second token, and
183 // determine whether it, if appended to the first token, would form (or
184 // would contribute) to a larger token if concatenated.
185 char FirstChar = 0;
186 if (ConcatInfo & aci_custom) {
187 // If the token does not need to know the first character, don't get it.
Chris Lattnerd7038e12009-02-13 00:46:04 +0000188 } else {
Daniel Dunbar99c76222009-03-18 03:32:24 +0000189 FirstChar = GetFirstChar(PP, Tok);
Chris Lattnerd7038e12009-02-13 00:46:04 +0000190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Chris Lattnerd7038e12009-02-13 00:46:04 +0000192 switch (PrevKind) {
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000193 default:
194 llvm_unreachable("InitAvoidConcatTokenInfo built wrong");
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000195
196 case tok::raw_identifier:
197 llvm_unreachable("tok::raw_identifier in non-raw lexing mode!");
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000198
Richard Smith99831e42012-03-06 03:21:47 +0000199 case tok::string_literal:
200 case tok::wide_string_literal:
201 case tok::utf8_string_literal:
202 case tok::utf16_string_literal:
203 case tok::utf32_string_literal:
204 case tok::char_constant:
205 case tok::wide_char_constant:
206 case tok::utf16_char_constant:
207 case tok::utf32_char_constant:
208 if (!PP.getLangOptions().CPlusPlus0x)
209 return false;
210
211 // In C++11, a string or character literal followed by an identifier is a
212 // single token.
213 if (Tok.getIdentifierInfo())
214 return true;
215
216 // A ud-suffix is an identifier. If the previous token ends with one, treat
217 // it as an identifier.
218 if (!PrevTok.hasUDSuffix())
219 return false;
220 // FALL THROUGH.
Mike Stump1eb44332009-09-09 15:08:12 +0000221 case tok::identifier: // id+id or id+number or id+L"foo".
Daniel Dunbar99c76222009-03-18 03:32:24 +0000222 // id+'.'... will not append.
223 if (Tok.is(tok::numeric_constant))
224 return GetFirstChar(PP, Tok) != '.';
225
Douglas Gregor5cee1192011-07-27 05:40:30 +0000226 if (Tok.getIdentifierInfo() || Tok.is(tok::wide_string_literal) ||
227 Tok.is(tok::utf8_string_literal) || Tok.is(tok::utf16_string_literal) ||
228 Tok.is(tok::utf32_string_literal) || Tok.is(tok::wide_char_constant) ||
229 Tok.is(tok::utf16_char_constant) || Tok.is(tok::utf32_char_constant))
Chris Lattnerd7038e12009-02-13 00:46:04 +0000230 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Chris Lattnerd7038e12009-02-13 00:46:04 +0000232 // If this isn't identifier + string, we're done.
233 if (Tok.isNot(tok::char_constant) && Tok.isNot(tok::string_literal))
234 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Chris Lattnerd7038e12009-02-13 00:46:04 +0000236 // Otherwise, this is a narrow character or string. If the *identifier*
Douglas Gregor5cee1192011-07-27 05:40:30 +0000237 // is a literal 'L', 'u8', 'u' or 'U', avoid pasting L "foo" -> L"foo".
238 return IsIdentifierStringPrefix(PrevTok);
Richard Smith99831e42012-03-06 03:21:47 +0000239
Chris Lattnerd7038e12009-02-13 00:46:04 +0000240 case tok::numeric_constant:
241 return isalnum(FirstChar) || Tok.is(tok::numeric_constant) ||
Richard Smith99831e42012-03-06 03:21:47 +0000242 FirstChar == '+' || FirstChar == '-' || FirstChar == '.' ||
243 (PP.getLangOptions().CPlusPlus0x && FirstChar == '_');
Chris Lattnerd7038e12009-02-13 00:46:04 +0000244 case tok::period: // ..., .*, .1234
Chris Lattner88773212010-04-14 03:57:19 +0000245 return (FirstChar == '.' && PrevPrevTok.is(tok::period)) ||
246 isdigit(FirstChar) ||
Eli Friedman8849f112009-06-15 19:48:50 +0000247 (PP.getLangOptions().CPlusPlus && FirstChar == '*');
Chris Lattnerd7038e12009-02-13 00:46:04 +0000248 case tok::amp: // &&
249 return FirstChar == '&';
250 case tok::plus: // ++
251 return FirstChar == '+';
252 case tok::minus: // --, ->, ->*
253 return FirstChar == '-' || FirstChar == '>';
254 case tok::slash: //, /*, //
255 return FirstChar == '*' || FirstChar == '/';
256 case tok::less: // <<, <<=, <:, <%
257 return FirstChar == '<' || FirstChar == ':' || FirstChar == '%';
258 case tok::greater: // >>, >>=
259 return FirstChar == '>';
260 case tok::pipe: // ||
261 return FirstChar == '|';
262 case tok::percent: // %>, %:
Eli Friedman896ccf82009-05-27 22:33:06 +0000263 return FirstChar == '>' || FirstChar == ':';
Chris Lattnerd7038e12009-02-13 00:46:04 +0000264 case tok::colon: // ::, :>
Eli Friedman8849f112009-06-15 19:48:50 +0000265 return FirstChar == '>' ||
266 (PP.getLangOptions().CPlusPlus && FirstChar == ':');
Chris Lattnerd7038e12009-02-13 00:46:04 +0000267 case tok::hash: // ##, #@, %:%:
268 return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
269 case tok::arrow: // ->*
Eli Friedman8849f112009-06-15 19:48:50 +0000270 return PP.getLangOptions().CPlusPlus && FirstChar == '*';
Chris Lattnerd7038e12009-02-13 00:46:04 +0000271 }
272}