#include "clang/Lex/TokenConcatenation.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/Support/ErrorHandling.h"
using namespace clang;
static bool IsStringPrefix(StringRef Str, bool CPlusPlus11) {
if (Str[0] == 'L' ||
(CPlusPlus11 && (Str[0] == 'u' || Str[0] == 'U' || Str[0] == 'R'))) {
if (Str.size() == 1)
return true;
if (Str[1] == 'R' && Str[0] != 'R' && Str.size() == 2 && CPlusPlus11)
return true;
if (Str[0] == 'u' && Str[1] == '8') {
if (Str.size() == 2) return true; if (Str.size() == 3 && Str[2] == 'R') return true; }
}
return false;
}
bool TokenConcatenation::IsIdentifierStringPrefix(const Token &Tok) const {
const LangOptions &LangOpts = PP.getLangOpts();
if (!Tok.needsCleaning()) {
if (Tok.getLength() < 1 || Tok.getLength() > 3)
return false;
SourceManager &SM = PP.getSourceManager();
const char *Ptr = SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation()));
return IsStringPrefix(StringRef(Ptr, Tok.getLength()),
LangOpts.CPlusPlus11);
}
if (Tok.getLength() < 256) {
char Buffer[256];
const char *TokPtr = Buffer;
unsigned length = PP.getSpelling(Tok, TokPtr);
return IsStringPrefix(StringRef(TokPtr, length), LangOpts.CPlusPlus11);
}
return IsStringPrefix(StringRef(PP.getSpelling(Tok)), LangOpts.CPlusPlus11);
}
TokenConcatenation::TokenConcatenation(const Preprocessor &pp) : PP(pp) {
memset(TokenInfo, 0, sizeof(TokenInfo));
TokenInfo[tok::identifier ] |= aci_custom;
TokenInfo[tok::numeric_constant] |= aci_custom_firstchar;
TokenInfo[tok::period ] |= aci_custom_firstchar;
TokenInfo[tok::amp ] |= aci_custom_firstchar;
TokenInfo[tok::plus ] |= aci_custom_firstchar;
TokenInfo[tok::minus ] |= aci_custom_firstchar;
TokenInfo[tok::slash ] |= aci_custom_firstchar;
TokenInfo[tok::less ] |= aci_custom_firstchar;
TokenInfo[tok::greater ] |= aci_custom_firstchar;
TokenInfo[tok::pipe ] |= aci_custom_firstchar;
TokenInfo[tok::percent ] |= aci_custom_firstchar;
TokenInfo[tok::colon ] |= aci_custom_firstchar;
TokenInfo[tok::hash ] |= aci_custom_firstchar;
TokenInfo[tok::arrow ] |= aci_custom_firstchar;
if (PP.getLangOpts().CPlusPlus11) {
TokenInfo[tok::string_literal ] |= aci_custom;
TokenInfo[tok::wide_string_literal ] |= aci_custom;
TokenInfo[tok::utf8_string_literal ] |= aci_custom;
TokenInfo[tok::utf16_string_literal] |= aci_custom;
TokenInfo[tok::utf32_string_literal] |= aci_custom;
TokenInfo[tok::char_constant ] |= aci_custom;
TokenInfo[tok::wide_char_constant ] |= aci_custom;
TokenInfo[tok::utf16_char_constant ] |= aci_custom;
TokenInfo[tok::utf32_char_constant ] |= aci_custom;
}
if (PP.getLangOpts().CPlusPlus17)
TokenInfo[tok::utf8_char_constant] |= aci_custom;
if (PP.getLangOpts().CPlusPlus20)
TokenInfo[tok::lessequal ] |= aci_custom_firstchar;
TokenInfo[tok::amp ] |= aci_avoid_equal; TokenInfo[tok::plus ] |= aci_avoid_equal; TokenInfo[tok::minus ] |= aci_avoid_equal; TokenInfo[tok::slash ] |= aci_avoid_equal; TokenInfo[tok::less ] |= aci_avoid_equal; TokenInfo[tok::greater ] |= aci_avoid_equal; TokenInfo[tok::pipe ] |= aci_avoid_equal; TokenInfo[tok::percent ] |= aci_avoid_equal; TokenInfo[tok::star ] |= aci_avoid_equal; TokenInfo[tok::exclaim ] |= aci_avoid_equal; TokenInfo[tok::lessless ] |= aci_avoid_equal; TokenInfo[tok::greatergreater] |= aci_avoid_equal; TokenInfo[tok::caret ] |= aci_avoid_equal; TokenInfo[tok::equal ] |= aci_avoid_equal; }
static char GetFirstChar(const Preprocessor &PP, const Token &Tok) {
if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
return II->getNameStart()[0];
} else if (!Tok.needsCleaning()) {
if (Tok.isLiteral() && Tok.getLiteralData()) {
return *Tok.getLiteralData();
} else {
SourceManager &SM = PP.getSourceManager();
return *SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation()));
}
} else if (Tok.getLength() < 256) {
char Buffer[256];
const char *TokPtr = Buffer;
PP.getSpelling(Tok, TokPtr);
return TokPtr[0];
} else {
return PP.getSpelling(Tok)[0];
}
}
bool TokenConcatenation::AvoidConcat(const Token &PrevPrevTok,
const Token &PrevTok,
const Token &Tok) const {
if (PrevTok.isAnnotation())
return true;
SourceManager &SM = PP.getSourceManager();
SourceLocation PrevSpellLoc = SM.getSpellingLoc(PrevTok.getLocation());
SourceLocation SpellLoc = SM.getSpellingLoc(Tok.getLocation());
if (PrevSpellLoc.getLocWithOffset(PrevTok.getLength()) == SpellLoc)
return false;
tok::TokenKind PrevKind = PrevTok.getKind();
if (!PrevTok.isAnnotation() && PrevTok.getIdentifierInfo())
PrevKind = tok::identifier;
unsigned ConcatInfo = TokenInfo[PrevKind];
if (ConcatInfo == 0) return false;
if (ConcatInfo & aci_avoid_equal) {
if (Tok.isOneOf(tok::equal, tok::equalequal))
return true;
ConcatInfo &= ~aci_avoid_equal;
}
if (Tok.isAnnotation()) {
assert(Tok.isOneOf(tok::annot_module_include, tok::annot_module_begin,
tok::annot_module_end) &&
"unexpected annotation in AvoidConcat");
ConcatInfo = 0;
}
if (ConcatInfo == 0)
return false;
char FirstChar = 0;
if (ConcatInfo & aci_custom) {
} else {
FirstChar = GetFirstChar(PP, Tok);
}
switch (PrevKind) {
default:
llvm_unreachable("InitAvoidConcatTokenInfo built wrong");
case tok::raw_identifier:
llvm_unreachable("tok::raw_identifier in non-raw lexing mode!");
case tok::string_literal:
case tok::wide_string_literal:
case tok::utf8_string_literal:
case tok::utf16_string_literal:
case tok::utf32_string_literal:
case tok::char_constant:
case tok::wide_char_constant:
case tok::utf8_char_constant:
case tok::utf16_char_constant:
case tok::utf32_char_constant:
if (!PP.getLangOpts().CPlusPlus11)
return false;
if (Tok.getIdentifierInfo())
return true;
if (!PrevTok.hasUDSuffix())
return false;
LLVM_FALLTHROUGH;
case tok::identifier: if (Tok.is(tok::numeric_constant))
return GetFirstChar(PP, Tok) != '.';
if (Tok.getIdentifierInfo() ||
Tok.isOneOf(tok::wide_string_literal, tok::utf8_string_literal,
tok::utf16_string_literal, tok::utf32_string_literal,
tok::wide_char_constant, tok::utf8_char_constant,
tok::utf16_char_constant, tok::utf32_char_constant))
return true;
if (Tok.isNot(tok::char_constant) && Tok.isNot(tok::string_literal))
return false;
return IsIdentifierStringPrefix(PrevTok);
case tok::numeric_constant:
return isPreprocessingNumberBody(FirstChar) ||
FirstChar == '+' || FirstChar == '-';
case tok::period: return (FirstChar == '.' && PrevPrevTok.is(tok::period)) ||
isDigit(FirstChar) ||
(PP.getLangOpts().CPlusPlus && FirstChar == '*');
case tok::amp: return FirstChar == '&';
case tok::plus: return FirstChar == '+';
case tok::minus: return FirstChar == '-' || FirstChar == '>';
case tok::slash: return FirstChar == '*' || FirstChar == '/';
case tok::less: return FirstChar == '<' || FirstChar == ':' || FirstChar == '%';
case tok::greater: return FirstChar == '>';
case tok::pipe: return FirstChar == '|';
case tok::percent: return FirstChar == '>' || FirstChar == ':';
case tok::colon: return FirstChar == '>' ||
(PP.getLangOpts().CPlusPlus && FirstChar == ':');
case tok::hash: return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
case tok::arrow: return PP.getLangOpts().CPlusPlus && FirstChar == '*';
case tok::lessequal: return PP.getLangOpts().CPlusPlus20 && FirstChar == '>';
}
}