clang API Documentation
00001 //===--- TokenConcatenation.cpp - Token Concatenation Avoidance -----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the TokenConcatenation class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/Lex/TokenConcatenation.h" 00015 #include "clang/Lex/Preprocessor.h" 00016 #include "llvm/Support/ErrorHandling.h" 00017 using namespace clang; 00018 00019 00020 /// IsStringPrefix - Return true if Str is a string prefix. 00021 /// 'L', 'u', 'U', or 'u8'. Including raw versions. 00022 static bool IsStringPrefix(StringRef Str, bool CPlusPlus0x) { 00023 00024 if (Str[0] == 'L' || 00025 (CPlusPlus0x && (Str[0] == 'u' || Str[0] == 'U' || Str[0] == 'R'))) { 00026 00027 if (Str.size() == 1) 00028 return true; // "L", "u", "U", and "R" 00029 00030 // Check for raw flavors. Need to make sure the first character wasn't 00031 // already R. Need CPlusPlus0x check for "LR". 00032 if (Str[1] == 'R' && Str[0] != 'R' && Str.size() == 2 && CPlusPlus0x) 00033 return true; // "LR", "uR", "UR" 00034 00035 // Check for "u8" and "u8R" 00036 if (Str[0] == 'u' && Str[1] == '8') { 00037 if (Str.size() == 2) return true; // "u8" 00038 if (Str.size() == 3 && Str[2] == 'R') return true; // "u8R" 00039 } 00040 } 00041 00042 return false; 00043 } 00044 00045 /// IsIdentifierStringPrefix - Return true if the spelling of the token 00046 /// is literally 'L', 'u', 'U', or 'u8'. Including raw versions. 00047 bool TokenConcatenation::IsIdentifierStringPrefix(const Token &Tok) const { 00048 const LangOptions &LangOpts = PP.getLangOpts(); 00049 00050 if (!Tok.needsCleaning()) { 00051 if (Tok.getLength() < 1 || Tok.getLength() > 3) 00052 return false; 00053 SourceManager &SM = PP.getSourceManager(); 00054 const char *Ptr = SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation())); 00055 return IsStringPrefix(StringRef(Ptr, Tok.getLength()), 00056 LangOpts.CPlusPlus0x); 00057 } 00058 00059 if (Tok.getLength() < 256) { 00060 char Buffer[256]; 00061 const char *TokPtr = Buffer; 00062 unsigned length = PP.getSpelling(Tok, TokPtr); 00063 return IsStringPrefix(StringRef(TokPtr, length), LangOpts.CPlusPlus0x); 00064 } 00065 00066 return IsStringPrefix(StringRef(PP.getSpelling(Tok)), LangOpts.CPlusPlus0x); 00067 } 00068 00069 TokenConcatenation::TokenConcatenation(Preprocessor &pp) : PP(pp) { 00070 memset(TokenInfo, 0, sizeof(TokenInfo)); 00071 00072 // These tokens have custom code in AvoidConcat. 00073 TokenInfo[tok::identifier ] |= aci_custom; 00074 TokenInfo[tok::numeric_constant] |= aci_custom_firstchar; 00075 TokenInfo[tok::period ] |= aci_custom_firstchar; 00076 TokenInfo[tok::amp ] |= aci_custom_firstchar; 00077 TokenInfo[tok::plus ] |= aci_custom_firstchar; 00078 TokenInfo[tok::minus ] |= aci_custom_firstchar; 00079 TokenInfo[tok::slash ] |= aci_custom_firstchar; 00080 TokenInfo[tok::less ] |= aci_custom_firstchar; 00081 TokenInfo[tok::greater ] |= aci_custom_firstchar; 00082 TokenInfo[tok::pipe ] |= aci_custom_firstchar; 00083 TokenInfo[tok::percent ] |= aci_custom_firstchar; 00084 TokenInfo[tok::colon ] |= aci_custom_firstchar; 00085 TokenInfo[tok::hash ] |= aci_custom_firstchar; 00086 TokenInfo[tok::arrow ] |= aci_custom_firstchar; 00087 00088 // These tokens have custom code in C++11 mode. 00089 if (PP.getLangOpts().CPlusPlus0x) { 00090 TokenInfo[tok::string_literal ] |= aci_custom; 00091 TokenInfo[tok::wide_string_literal ] |= aci_custom; 00092 TokenInfo[tok::utf8_string_literal ] |= aci_custom; 00093 TokenInfo[tok::utf16_string_literal] |= aci_custom; 00094 TokenInfo[tok::utf32_string_literal] |= aci_custom; 00095 TokenInfo[tok::char_constant ] |= aci_custom; 00096 TokenInfo[tok::wide_char_constant ] |= aci_custom; 00097 TokenInfo[tok::utf16_char_constant ] |= aci_custom; 00098 TokenInfo[tok::utf32_char_constant ] |= aci_custom; 00099 } 00100 00101 // These tokens change behavior if followed by an '='. 00102 TokenInfo[tok::amp ] |= aci_avoid_equal; // &= 00103 TokenInfo[tok::plus ] |= aci_avoid_equal; // += 00104 TokenInfo[tok::minus ] |= aci_avoid_equal; // -= 00105 TokenInfo[tok::slash ] |= aci_avoid_equal; // /= 00106 TokenInfo[tok::less ] |= aci_avoid_equal; // <= 00107 TokenInfo[tok::greater ] |= aci_avoid_equal; // >= 00108 TokenInfo[tok::pipe ] |= aci_avoid_equal; // |= 00109 TokenInfo[tok::percent ] |= aci_avoid_equal; // %= 00110 TokenInfo[tok::star ] |= aci_avoid_equal; // *= 00111 TokenInfo[tok::exclaim ] |= aci_avoid_equal; // != 00112 TokenInfo[tok::lessless ] |= aci_avoid_equal; // <<= 00113 TokenInfo[tok::greatergreater] |= aci_avoid_equal; // >>= 00114 TokenInfo[tok::caret ] |= aci_avoid_equal; // ^= 00115 TokenInfo[tok::equal ] |= aci_avoid_equal; // == 00116 } 00117 00118 /// GetFirstChar - Get the first character of the token \arg Tok, 00119 /// avoiding calls to getSpelling where possible. 00120 static char GetFirstChar(Preprocessor &PP, const Token &Tok) { 00121 if (IdentifierInfo *II = Tok.getIdentifierInfo()) { 00122 // Avoid spelling identifiers, the most common form of token. 00123 return II->getNameStart()[0]; 00124 } else if (!Tok.needsCleaning()) { 00125 if (Tok.isLiteral() && Tok.getLiteralData()) { 00126 return *Tok.getLiteralData(); 00127 } else { 00128 SourceManager &SM = PP.getSourceManager(); 00129 return *SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation())); 00130 } 00131 } else if (Tok.getLength() < 256) { 00132 char Buffer[256]; 00133 const char *TokPtr = Buffer; 00134 PP.getSpelling(Tok, TokPtr); 00135 return TokPtr[0]; 00136 } else { 00137 return PP.getSpelling(Tok)[0]; 00138 } 00139 } 00140 00141 /// AvoidConcat - If printing PrevTok immediately followed by Tok would cause 00142 /// the two individual tokens to be lexed as a single token, return true 00143 /// (which causes a space to be printed between them). This allows the output 00144 /// of -E mode to be lexed to the same token stream as lexing the input 00145 /// directly would. 00146 /// 00147 /// This code must conservatively return true if it doesn't want to be 100% 00148 /// accurate. This will cause the output to include extra space characters, 00149 /// but the resulting output won't have incorrect concatenations going on. 00150 /// Examples include "..", which we print with a space between, because we 00151 /// don't want to track enough to tell "x.." from "...". 00152 bool TokenConcatenation::AvoidConcat(const Token &PrevPrevTok, 00153 const Token &PrevTok, 00154 const Token &Tok) const { 00155 // First, check to see if the tokens were directly adjacent in the original 00156 // source. If they were, it must be okay to stick them together: if there 00157 // were an issue, the tokens would have been lexed differently. 00158 if (PrevTok.getLocation().isFileID() && Tok.getLocation().isFileID() && 00159 PrevTok.getLocation().getLocWithOffset(PrevTok.getLength()) == 00160 Tok.getLocation()) 00161 return false; 00162 00163 tok::TokenKind PrevKind = PrevTok.getKind(); 00164 if (PrevTok.getIdentifierInfo()) // Language keyword or named operator. 00165 PrevKind = tok::identifier; 00166 00167 // Look up information on when we should avoid concatenation with prevtok. 00168 unsigned ConcatInfo = TokenInfo[PrevKind]; 00169 00170 // If prevtok never causes a problem for anything after it, return quickly. 00171 if (ConcatInfo == 0) return false; 00172 00173 if (ConcatInfo & aci_avoid_equal) { 00174 // If the next token is '=' or '==', avoid concatenation. 00175 if (Tok.is(tok::equal) || Tok.is(tok::equalequal)) 00176 return true; 00177 ConcatInfo &= ~aci_avoid_equal; 00178 } 00179 00180 if (ConcatInfo == 0) return false; 00181 00182 // Basic algorithm: we look at the first character of the second token, and 00183 // determine whether it, if appended to the first token, would form (or 00184 // would contribute) to a larger token if concatenated. 00185 char FirstChar = 0; 00186 if (ConcatInfo & aci_custom) { 00187 // If the token does not need to know the first character, don't get it. 00188 } else { 00189 FirstChar = GetFirstChar(PP, Tok); 00190 } 00191 00192 switch (PrevKind) { 00193 default: 00194 llvm_unreachable("InitAvoidConcatTokenInfo built wrong"); 00195 00196 case tok::raw_identifier: 00197 llvm_unreachable("tok::raw_identifier in non-raw lexing mode!"); 00198 00199 case tok::string_literal: 00200 case tok::wide_string_literal: 00201 case tok::utf8_string_literal: 00202 case tok::utf16_string_literal: 00203 case tok::utf32_string_literal: 00204 case tok::char_constant: 00205 case tok::wide_char_constant: 00206 case tok::utf16_char_constant: 00207 case tok::utf32_char_constant: 00208 if (!PP.getLangOpts().CPlusPlus0x) 00209 return false; 00210 00211 // In C++11, a string or character literal followed by an identifier is a 00212 // single token. 00213 if (Tok.getIdentifierInfo()) 00214 return true; 00215 00216 // A ud-suffix is an identifier. If the previous token ends with one, treat 00217 // it as an identifier. 00218 if (!PrevTok.hasUDSuffix()) 00219 return false; 00220 // FALL THROUGH. 00221 case tok::identifier: // id+id or id+number or id+L"foo". 00222 // id+'.'... will not append. 00223 if (Tok.is(tok::numeric_constant)) 00224 return GetFirstChar(PP, Tok) != '.'; 00225 00226 if (Tok.getIdentifierInfo() || Tok.is(tok::wide_string_literal) || 00227 Tok.is(tok::utf8_string_literal) || Tok.is(tok::utf16_string_literal) || 00228 Tok.is(tok::utf32_string_literal) || Tok.is(tok::wide_char_constant) || 00229 Tok.is(tok::utf16_char_constant) || Tok.is(tok::utf32_char_constant)) 00230 return true; 00231 00232 // If this isn't identifier + string, we're done. 00233 if (Tok.isNot(tok::char_constant) && Tok.isNot(tok::string_literal)) 00234 return false; 00235 00236 // Otherwise, this is a narrow character or string. If the *identifier* 00237 // is a literal 'L', 'u8', 'u' or 'U', avoid pasting L "foo" -> L"foo". 00238 return IsIdentifierStringPrefix(PrevTok); 00239 00240 case tok::numeric_constant: 00241 return isalnum(FirstChar) || Tok.is(tok::numeric_constant) || 00242 FirstChar == '+' || FirstChar == '-' || FirstChar == '.' || 00243 (PP.getLangOpts().CPlusPlus0x && FirstChar == '_'); 00244 case tok::period: // ..., .*, .1234 00245 return (FirstChar == '.' && PrevPrevTok.is(tok::period)) || 00246 isdigit(FirstChar) || 00247 (PP.getLangOpts().CPlusPlus && FirstChar == '*'); 00248 case tok::amp: // && 00249 return FirstChar == '&'; 00250 case tok::plus: // ++ 00251 return FirstChar == '+'; 00252 case tok::minus: // --, ->, ->* 00253 return FirstChar == '-' || FirstChar == '>'; 00254 case tok::slash: //, /*, // 00255 return FirstChar == '*' || FirstChar == '/'; 00256 case tok::less: // <<, <<=, <:, <% 00257 return FirstChar == '<' || FirstChar == ':' || FirstChar == '%'; 00258 case tok::greater: // >>, >>= 00259 return FirstChar == '>'; 00260 case tok::pipe: // || 00261 return FirstChar == '|'; 00262 case tok::percent: // %>, %: 00263 return FirstChar == '>' || FirstChar == ':'; 00264 case tok::colon: // ::, :> 00265 return FirstChar == '>' || 00266 (PP.getLangOpts().CPlusPlus && FirstChar == ':'); 00267 case tok::hash: // ##, #@, %:%: 00268 return FirstChar == '#' || FirstChar == '@' || FirstChar == '%'; 00269 case tok::arrow: // ->* 00270 return PP.getLangOpts().CPlusPlus && FirstChar == '*'; 00271 } 00272 }