blob: 19baf80aad37f1f7af809b85110a9b53dac97738 [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
Douglas Gregor5cee1192011-07-27 05:40:30 +000020/// IsIdentifierStringPrefix - Return true if the spelling of the token
21/// is literally 'L', 'u', 'U', or 'u8'.
22bool TokenConcatenation::IsIdentifierStringPrefix(const Token &Tok) const {
23 const LangOptions &LangOpts = PP.getLangOptions();
24
Chris Lattnerd7038e12009-02-13 00:46:04 +000025 if (!Tok.needsCleaning()) {
Douglas Gregor5cee1192011-07-27 05:40:30 +000026 if (Tok.getLength() != 1 && Tok.getLength() != 2)
27 return false;
Chris Lattnerd7038e12009-02-13 00:46:04 +000028 SourceManager &SM = PP.getSourceManager();
Douglas Gregor5cee1192011-07-27 05:40:30 +000029 const char *Ptr = SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation()));
30 if (Tok.getLength() == 1)
31 return Ptr[0] == 'L' ||
32 (LangOpts.CPlusPlus0x && (Ptr[0] == 'u' || Ptr[0] == 'U'));
33 if (Tok.getLength() == 2)
34 return LangOpts.CPlusPlus0x && Ptr[0] == 'u' && Ptr[1] == '8';
Chris Lattnerd7038e12009-02-13 00:46:04 +000035 }
Mike Stump1eb44332009-09-09 15:08:12 +000036
Chris Lattnerd7038e12009-02-13 00:46:04 +000037 if (Tok.getLength() < 256) {
38 char Buffer[256];
39 const char *TokPtr = Buffer;
Douglas Gregor5cee1192011-07-27 05:40:30 +000040 unsigned length = PP.getSpelling(Tok, TokPtr);
41 if (length == 1)
42 return TokPtr[0] == 'L' ||
43 (LangOpts.CPlusPlus0x && (TokPtr[0] == 'u' || TokPtr[0] == 'U'));
44 if (length == 2)
45 return LangOpts.CPlusPlus0x && TokPtr[0] == 'u' && TokPtr[1] == '8';
46 return false;
Chris Lattnerd7038e12009-02-13 00:46:04 +000047 }
Mike Stump1eb44332009-09-09 15:08:12 +000048
Douglas Gregor5cee1192011-07-27 05:40:30 +000049 std::string TokStr = PP.getSpelling(Tok);
50 return TokStr == "L" || (LangOpts.CPlusPlus0x && (TokStr == "u8" ||
51 TokStr == "u" ||
52 TokStr == "U"));
Chris Lattnerd7038e12009-02-13 00:46:04 +000053}
54
55TokenConcatenation::TokenConcatenation(Preprocessor &pp) : PP(pp) {
56 memset(TokenInfo, 0, sizeof(TokenInfo));
Mike Stump1eb44332009-09-09 15:08:12 +000057
Chris Lattnerd7038e12009-02-13 00:46:04 +000058 // These tokens have custom code in AvoidConcat.
59 TokenInfo[tok::identifier ] |= aci_custom;
60 TokenInfo[tok::numeric_constant] |= aci_custom_firstchar;
61 TokenInfo[tok::period ] |= aci_custom_firstchar;
62 TokenInfo[tok::amp ] |= aci_custom_firstchar;
63 TokenInfo[tok::plus ] |= aci_custom_firstchar;
64 TokenInfo[tok::minus ] |= aci_custom_firstchar;
65 TokenInfo[tok::slash ] |= aci_custom_firstchar;
66 TokenInfo[tok::less ] |= aci_custom_firstchar;
67 TokenInfo[tok::greater ] |= aci_custom_firstchar;
68 TokenInfo[tok::pipe ] |= aci_custom_firstchar;
69 TokenInfo[tok::percent ] |= aci_custom_firstchar;
70 TokenInfo[tok::colon ] |= aci_custom_firstchar;
71 TokenInfo[tok::hash ] |= aci_custom_firstchar;
72 TokenInfo[tok::arrow ] |= aci_custom_firstchar;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattnerd7038e12009-02-13 00:46:04 +000074 // These tokens change behavior if followed by an '='.
75 TokenInfo[tok::amp ] |= aci_avoid_equal; // &=
76 TokenInfo[tok::plus ] |= aci_avoid_equal; // +=
77 TokenInfo[tok::minus ] |= aci_avoid_equal; // -=
78 TokenInfo[tok::slash ] |= aci_avoid_equal; // /=
79 TokenInfo[tok::less ] |= aci_avoid_equal; // <=
80 TokenInfo[tok::greater ] |= aci_avoid_equal; // >=
81 TokenInfo[tok::pipe ] |= aci_avoid_equal; // |=
82 TokenInfo[tok::percent ] |= aci_avoid_equal; // %=
83 TokenInfo[tok::star ] |= aci_avoid_equal; // *=
84 TokenInfo[tok::exclaim ] |= aci_avoid_equal; // !=
85 TokenInfo[tok::lessless ] |= aci_avoid_equal; // <<=
Chris Lattner86851102010-03-26 17:10:02 +000086 TokenInfo[tok::greatergreater] |= aci_avoid_equal; // >>=
Chris Lattnerd7038e12009-02-13 00:46:04 +000087 TokenInfo[tok::caret ] |= aci_avoid_equal; // ^=
88 TokenInfo[tok::equal ] |= aci_avoid_equal; // ==
89}
90
Daniel Dunbar99c76222009-03-18 03:32:24 +000091/// GetFirstChar - Get the first character of the token \arg Tok,
92/// avoiding calls to getSpelling where possible.
93static char GetFirstChar(Preprocessor &PP, const Token &Tok) {
94 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
95 // Avoid spelling identifiers, the most common form of token.
Daniel Dunbare013d682009-10-18 20:26:12 +000096 return II->getNameStart()[0];
Daniel Dunbar99c76222009-03-18 03:32:24 +000097 } else if (!Tok.needsCleaning()) {
98 if (Tok.isLiteral() && Tok.getLiteralData()) {
99 return *Tok.getLiteralData();
100 } else {
101 SourceManager &SM = PP.getSourceManager();
102 return *SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation()));
103 }
104 } else if (Tok.getLength() < 256) {
105 char Buffer[256];
106 const char *TokPtr = Buffer;
107 PP.getSpelling(Tok, TokPtr);
108 return TokPtr[0];
109 } else {
110 return PP.getSpelling(Tok)[0];
111 }
112}
113
Chris Lattnerd7038e12009-02-13 00:46:04 +0000114/// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
115/// the two individual tokens to be lexed as a single token, return true
116/// (which causes a space to be printed between them). This allows the output
117/// of -E mode to be lexed to the same token stream as lexing the input
118/// directly would.
119///
120/// This code must conservatively return true if it doesn't want to be 100%
121/// accurate. This will cause the output to include extra space characters,
122/// but the resulting output won't have incorrect concatenations going on.
123/// Examples include "..", which we print with a space between, because we
124/// don't want to track enough to tell "x.." from "...".
Chris Lattner88773212010-04-14 03:57:19 +0000125bool TokenConcatenation::AvoidConcat(const Token &PrevPrevTok,
126 const Token &PrevTok,
Chris Lattnerd7038e12009-02-13 00:46:04 +0000127 const Token &Tok) const {
Chris Lattnere1614bb2009-04-21 23:28:41 +0000128 // First, check to see if the tokens were directly adjacent in the original
129 // source. If they were, it must be okay to stick them together: if there
130 // were an issue, the tokens would have been lexed differently.
131 if (PrevTok.getLocation().isFileID() && Tok.getLocation().isFileID() &&
Mike Stump1eb44332009-09-09 15:08:12 +0000132 PrevTok.getLocation().getFileLocWithOffset(PrevTok.getLength()) ==
Chris Lattnere1614bb2009-04-21 23:28:41 +0000133 Tok.getLocation())
134 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Chris Lattnerd7038e12009-02-13 00:46:04 +0000136 tok::TokenKind PrevKind = PrevTok.getKind();
137 if (PrevTok.getIdentifierInfo()) // Language keyword or named operator.
138 PrevKind = tok::identifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Chris Lattnerd7038e12009-02-13 00:46:04 +0000140 // Look up information on when we should avoid concatenation with prevtok.
141 unsigned ConcatInfo = TokenInfo[PrevKind];
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Chris Lattnerd7038e12009-02-13 00:46:04 +0000143 // If prevtok never causes a problem for anything after it, return quickly.
144 if (ConcatInfo == 0) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Chris Lattnerd7038e12009-02-13 00:46:04 +0000146 if (ConcatInfo & aci_avoid_equal) {
147 // If the next token is '=' or '==', avoid concatenation.
148 if (Tok.is(tok::equal) || Tok.is(tok::equalequal))
149 return true;
150 ConcatInfo &= ~aci_avoid_equal;
151 }
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Chris Lattnerd7038e12009-02-13 00:46:04 +0000153 if (ConcatInfo == 0) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Chris Lattnerd7038e12009-02-13 00:46:04 +0000155 // Basic algorithm: we look at the first character of the second token, and
156 // determine whether it, if appended to the first token, would form (or
157 // would contribute) to a larger token if concatenated.
158 char FirstChar = 0;
159 if (ConcatInfo & aci_custom) {
160 // If the token does not need to know the first character, don't get it.
Chris Lattnerd7038e12009-02-13 00:46:04 +0000161 } else {
Daniel Dunbar99c76222009-03-18 03:32:24 +0000162 FirstChar = GetFirstChar(PP, Tok);
Chris Lattnerd7038e12009-02-13 00:46:04 +0000163 }
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Chris Lattnerd7038e12009-02-13 00:46:04 +0000165 switch (PrevKind) {
Abramo Bagnarac4bf2b92010-12-22 08:23:18 +0000166 default:
167 llvm_unreachable("InitAvoidConcatTokenInfo built wrong");
168 return true;
169
170 case tok::raw_identifier:
171 llvm_unreachable("tok::raw_identifier in non-raw lexing mode!");
172 return true;
173
Mike Stump1eb44332009-09-09 15:08:12 +0000174 case tok::identifier: // id+id or id+number or id+L"foo".
Daniel Dunbar99c76222009-03-18 03:32:24 +0000175 // id+'.'... will not append.
176 if (Tok.is(tok::numeric_constant))
177 return GetFirstChar(PP, Tok) != '.';
178
Douglas Gregor5cee1192011-07-27 05:40:30 +0000179 if (Tok.getIdentifierInfo() || Tok.is(tok::wide_string_literal) ||
180 Tok.is(tok::utf8_string_literal) || Tok.is(tok::utf16_string_literal) ||
181 Tok.is(tok::utf32_string_literal) || Tok.is(tok::wide_char_constant) ||
182 Tok.is(tok::utf16_char_constant) || Tok.is(tok::utf32_char_constant))
Chris Lattnerd7038e12009-02-13 00:46:04 +0000183 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Chris Lattnerd7038e12009-02-13 00:46:04 +0000185 // If this isn't identifier + string, we're done.
186 if (Tok.isNot(tok::char_constant) && Tok.isNot(tok::string_literal))
187 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Chris Lattnerd7038e12009-02-13 00:46:04 +0000189 // Otherwise, this is a narrow character or string. If the *identifier*
Douglas Gregor5cee1192011-07-27 05:40:30 +0000190 // is a literal 'L', 'u8', 'u' or 'U', avoid pasting L "foo" -> L"foo".
191 return IsIdentifierStringPrefix(PrevTok);
Chris Lattnerd7038e12009-02-13 00:46:04 +0000192 case tok::numeric_constant:
193 return isalnum(FirstChar) || Tok.is(tok::numeric_constant) ||
194 FirstChar == '+' || FirstChar == '-' || FirstChar == '.';
195 case tok::period: // ..., .*, .1234
Chris Lattner88773212010-04-14 03:57:19 +0000196 return (FirstChar == '.' && PrevPrevTok.is(tok::period)) ||
197 isdigit(FirstChar) ||
Eli Friedman8849f112009-06-15 19:48:50 +0000198 (PP.getLangOptions().CPlusPlus && FirstChar == '*');
Chris Lattnerd7038e12009-02-13 00:46:04 +0000199 case tok::amp: // &&
200 return FirstChar == '&';
201 case tok::plus: // ++
202 return FirstChar == '+';
203 case tok::minus: // --, ->, ->*
204 return FirstChar == '-' || FirstChar == '>';
205 case tok::slash: //, /*, //
206 return FirstChar == '*' || FirstChar == '/';
207 case tok::less: // <<, <<=, <:, <%
208 return FirstChar == '<' || FirstChar == ':' || FirstChar == '%';
209 case tok::greater: // >>, >>=
210 return FirstChar == '>';
211 case tok::pipe: // ||
212 return FirstChar == '|';
213 case tok::percent: // %>, %:
Eli Friedman896ccf82009-05-27 22:33:06 +0000214 return FirstChar == '>' || FirstChar == ':';
Chris Lattnerd7038e12009-02-13 00:46:04 +0000215 case tok::colon: // ::, :>
Eli Friedman8849f112009-06-15 19:48:50 +0000216 return FirstChar == '>' ||
217 (PP.getLangOptions().CPlusPlus && FirstChar == ':');
Chris Lattnerd7038e12009-02-13 00:46:04 +0000218 case tok::hash: // ##, #@, %:%:
219 return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
220 case tok::arrow: // ->*
Eli Friedman8849f112009-06-15 19:48:50 +0000221 return PP.getLangOptions().CPlusPlus && FirstChar == '*';
Chris Lattnerd7038e12009-02-13 00:46:04 +0000222 }
223}