Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 1 | //===--- 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" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 16 | using namespace clang; |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | /// StartsWithL - Return true if the spelling of this token starts with 'L'. |
| 20 | bool TokenConcatenation::StartsWithL(const Token &Tok) const { |
| 21 | if (!Tok.needsCleaning()) { |
| 22 | SourceManager &SM = PP.getSourceManager(); |
| 23 | return *SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation())) == 'L'; |
| 24 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 25 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 26 | if (Tok.getLength() < 256) { |
| 27 | char Buffer[256]; |
| 28 | const char *TokPtr = Buffer; |
| 29 | PP.getSpelling(Tok, TokPtr); |
| 30 | return TokPtr[0] == 'L'; |
| 31 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 33 | return PP.getSpelling(Tok)[0] == 'L'; |
| 34 | } |
| 35 | |
| 36 | /// IsIdentifierL - Return true if the spelling of this token is literally |
| 37 | /// 'L'. |
| 38 | bool TokenConcatenation::IsIdentifierL(const Token &Tok) const { |
| 39 | if (!Tok.needsCleaning()) { |
| 40 | if (Tok.getLength() != 1) |
| 41 | return false; |
| 42 | SourceManager &SM = PP.getSourceManager(); |
| 43 | return *SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation())) == 'L'; |
| 44 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 46 | if (Tok.getLength() < 256) { |
| 47 | char Buffer[256]; |
| 48 | const char *TokPtr = Buffer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | if (PP.getSpelling(Tok, TokPtr) != 1) |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 50 | return false; |
| 51 | return TokPtr[0] == 'L'; |
| 52 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 54 | return PP.getSpelling(Tok) == "L"; |
| 55 | } |
| 56 | |
| 57 | TokenConcatenation::TokenConcatenation(Preprocessor &pp) : PP(pp) { |
| 58 | memset(TokenInfo, 0, sizeof(TokenInfo)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 60 | // These tokens have custom code in AvoidConcat. |
| 61 | TokenInfo[tok::identifier ] |= aci_custom; |
| 62 | TokenInfo[tok::numeric_constant] |= aci_custom_firstchar; |
| 63 | TokenInfo[tok::period ] |= aci_custom_firstchar; |
| 64 | TokenInfo[tok::amp ] |= aci_custom_firstchar; |
| 65 | TokenInfo[tok::plus ] |= aci_custom_firstchar; |
| 66 | TokenInfo[tok::minus ] |= aci_custom_firstchar; |
| 67 | TokenInfo[tok::slash ] |= aci_custom_firstchar; |
| 68 | TokenInfo[tok::less ] |= aci_custom_firstchar; |
| 69 | TokenInfo[tok::greater ] |= aci_custom_firstchar; |
| 70 | TokenInfo[tok::pipe ] |= aci_custom_firstchar; |
| 71 | TokenInfo[tok::percent ] |= aci_custom_firstchar; |
| 72 | TokenInfo[tok::colon ] |= aci_custom_firstchar; |
| 73 | TokenInfo[tok::hash ] |= aci_custom_firstchar; |
| 74 | TokenInfo[tok::arrow ] |= aci_custom_firstchar; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 76 | // These tokens change behavior if followed by an '='. |
| 77 | TokenInfo[tok::amp ] |= aci_avoid_equal; // &= |
| 78 | TokenInfo[tok::plus ] |= aci_avoid_equal; // += |
| 79 | TokenInfo[tok::minus ] |= aci_avoid_equal; // -= |
| 80 | TokenInfo[tok::slash ] |= aci_avoid_equal; // /= |
| 81 | TokenInfo[tok::less ] |= aci_avoid_equal; // <= |
| 82 | TokenInfo[tok::greater ] |= aci_avoid_equal; // >= |
| 83 | TokenInfo[tok::pipe ] |= aci_avoid_equal; // |= |
| 84 | TokenInfo[tok::percent ] |= aci_avoid_equal; // %= |
| 85 | TokenInfo[tok::star ] |= aci_avoid_equal; // *= |
| 86 | TokenInfo[tok::exclaim ] |= aci_avoid_equal; // != |
| 87 | TokenInfo[tok::lessless ] |= aci_avoid_equal; // <<= |
Chris Lattner | 8685110 | 2010-03-26 17:10:02 +0000 | [diff] [blame] | 88 | TokenInfo[tok::greatergreater] |= aci_avoid_equal; // >>= |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 89 | TokenInfo[tok::caret ] |= aci_avoid_equal; // ^= |
| 90 | TokenInfo[tok::equal ] |= aci_avoid_equal; // == |
| 91 | } |
| 92 | |
Daniel Dunbar | 99c7622 | 2009-03-18 03:32:24 +0000 | [diff] [blame] | 93 | /// GetFirstChar - Get the first character of the token \arg Tok, |
| 94 | /// avoiding calls to getSpelling where possible. |
| 95 | static char GetFirstChar(Preprocessor &PP, const Token &Tok) { |
| 96 | if (IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 97 | // Avoid spelling identifiers, the most common form of token. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 98 | return II->getNameStart()[0]; |
Daniel Dunbar | 99c7622 | 2009-03-18 03:32:24 +0000 | [diff] [blame] | 99 | } else if (!Tok.needsCleaning()) { |
| 100 | if (Tok.isLiteral() && Tok.getLiteralData()) { |
| 101 | return *Tok.getLiteralData(); |
| 102 | } else { |
| 103 | SourceManager &SM = PP.getSourceManager(); |
| 104 | return *SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation())); |
| 105 | } |
| 106 | } else if (Tok.getLength() < 256) { |
| 107 | char Buffer[256]; |
| 108 | const char *TokPtr = Buffer; |
| 109 | PP.getSpelling(Tok, TokPtr); |
| 110 | return TokPtr[0]; |
| 111 | } else { |
| 112 | return PP.getSpelling(Tok)[0]; |
| 113 | } |
| 114 | } |
| 115 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 116 | /// AvoidConcat - If printing PrevTok immediately followed by Tok would cause |
| 117 | /// the two individual tokens to be lexed as a single token, return true |
| 118 | /// (which causes a space to be printed between them). This allows the output |
| 119 | /// of -E mode to be lexed to the same token stream as lexing the input |
| 120 | /// directly would. |
| 121 | /// |
| 122 | /// This code must conservatively return true if it doesn't want to be 100% |
| 123 | /// accurate. This will cause the output to include extra space characters, |
| 124 | /// but the resulting output won't have incorrect concatenations going on. |
| 125 | /// Examples include "..", which we print with a space between, because we |
| 126 | /// don't want to track enough to tell "x.." from "...". |
| 127 | bool TokenConcatenation::AvoidConcat(const Token &PrevTok, |
| 128 | const Token &Tok) const { |
Chris Lattner | e1614bb | 2009-04-21 23:28:41 +0000 | [diff] [blame] | 129 | // First, check to see if the tokens were directly adjacent in the original |
| 130 | // source. If they were, it must be okay to stick them together: if there |
| 131 | // were an issue, the tokens would have been lexed differently. |
| 132 | if (PrevTok.getLocation().isFileID() && Tok.getLocation().isFileID() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | PrevTok.getLocation().getFileLocWithOffset(PrevTok.getLength()) == |
Chris Lattner | e1614bb | 2009-04-21 23:28:41 +0000 | [diff] [blame] | 134 | Tok.getLocation()) |
| 135 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 137 | tok::TokenKind PrevKind = PrevTok.getKind(); |
| 138 | if (PrevTok.getIdentifierInfo()) // Language keyword or named operator. |
| 139 | PrevKind = tok::identifier; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 141 | // Look up information on when we should avoid concatenation with prevtok. |
| 142 | unsigned ConcatInfo = TokenInfo[PrevKind]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 144 | // If prevtok never causes a problem for anything after it, return quickly. |
| 145 | if (ConcatInfo == 0) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 147 | if (ConcatInfo & aci_avoid_equal) { |
| 148 | // If the next token is '=' or '==', avoid concatenation. |
| 149 | if (Tok.is(tok::equal) || Tok.is(tok::equalequal)) |
| 150 | return true; |
| 151 | ConcatInfo &= ~aci_avoid_equal; |
| 152 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 154 | if (ConcatInfo == 0) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 156 | // Basic algorithm: we look at the first character of the second token, and |
| 157 | // determine whether it, if appended to the first token, would form (or |
| 158 | // would contribute) to a larger token if concatenated. |
| 159 | char FirstChar = 0; |
| 160 | if (ConcatInfo & aci_custom) { |
| 161 | // If the token does not need to know the first character, don't get it. |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 162 | } else { |
Daniel Dunbar | 99c7622 | 2009-03-18 03:32:24 +0000 | [diff] [blame] | 163 | FirstChar = GetFirstChar(PP, Tok); |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 164 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 166 | switch (PrevKind) { |
| 167 | default: assert(0 && "InitAvoidConcatTokenInfo built wrong"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | case tok::identifier: // id+id or id+number or id+L"foo". |
Daniel Dunbar | 99c7622 | 2009-03-18 03:32:24 +0000 | [diff] [blame] | 169 | // id+'.'... will not append. |
| 170 | if (Tok.is(tok::numeric_constant)) |
| 171 | return GetFirstChar(PP, Tok) != '.'; |
| 172 | |
| 173 | if (Tok.getIdentifierInfo() || Tok.is(tok::wide_string_literal) /* || |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 174 | Tok.is(tok::wide_char_literal)*/) |
| 175 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 177 | // If this isn't identifier + string, we're done. |
| 178 | if (Tok.isNot(tok::char_constant) && Tok.isNot(tok::string_literal)) |
| 179 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 181 | // FIXME: need a wide_char_constant! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 183 | // If the string was a wide string L"foo" or wide char L'f', it would |
| 184 | // concat with the previous identifier into fooL"bar". Avoid this. |
| 185 | if (StartsWithL(Tok)) |
| 186 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 188 | // Otherwise, this is a narrow character or string. If the *identifier* |
| 189 | // is a literal 'L', avoid pasting L "foo" -> L"foo". |
| 190 | return IsIdentifierL(PrevTok); |
| 191 | case tok::numeric_constant: |
| 192 | return isalnum(FirstChar) || Tok.is(tok::numeric_constant) || |
| 193 | FirstChar == '+' || FirstChar == '-' || FirstChar == '.'; |
| 194 | case tok::period: // ..., .*, .1234 |
Eli Friedman | 8849f11 | 2009-06-15 19:48:50 +0000 | [diff] [blame] | 195 | return FirstChar == '.' || isdigit(FirstChar) || |
| 196 | (PP.getLangOptions().CPlusPlus && FirstChar == '*'); |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 197 | case tok::amp: // && |
| 198 | return FirstChar == '&'; |
| 199 | case tok::plus: // ++ |
| 200 | return FirstChar == '+'; |
| 201 | case tok::minus: // --, ->, ->* |
| 202 | return FirstChar == '-' || FirstChar == '>'; |
| 203 | case tok::slash: //, /*, // |
| 204 | return FirstChar == '*' || FirstChar == '/'; |
| 205 | case tok::less: // <<, <<=, <:, <% |
| 206 | return FirstChar == '<' || FirstChar == ':' || FirstChar == '%'; |
| 207 | case tok::greater: // >>, >>= |
| 208 | return FirstChar == '>'; |
| 209 | case tok::pipe: // || |
| 210 | return FirstChar == '|'; |
| 211 | case tok::percent: // %>, %: |
Eli Friedman | 896ccf8 | 2009-05-27 22:33:06 +0000 | [diff] [blame] | 212 | return FirstChar == '>' || FirstChar == ':'; |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 213 | case tok::colon: // ::, :> |
Eli Friedman | 8849f11 | 2009-06-15 19:48:50 +0000 | [diff] [blame] | 214 | return FirstChar == '>' || |
| 215 | (PP.getLangOptions().CPlusPlus && FirstChar == ':'); |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 216 | case tok::hash: // ##, #@, %:%: |
| 217 | return FirstChar == '#' || FirstChar == '@' || FirstChar == '%'; |
| 218 | case tok::arrow: // ->* |
Eli Friedman | 8849f11 | 2009-06-15 19:48:50 +0000 | [diff] [blame] | 219 | return PP.getLangOptions().CPlusPlus && FirstChar == '*'; |
Chris Lattner | d7038e1 | 2009-02-13 00:46:04 +0000 | [diff] [blame] | 220 | } |
| 221 | } |