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