Add support for character constants in PP expressions, like:

#if 'a'

llvm-svn: 39393
This commit is contained in:
Chris Lattner 2007-04-05 06:58:56 +00:00
parent 380048807e
commit f8a0b0fa50
1 changed files with 41 additions and 0 deletions

View File

@ -186,6 +186,47 @@ static bool EvaluateValue(APSInt &Result, LexerToken &PeekTok,
PP.LexNonComment(PeekTok);
return false;
}
case tok::char_constant: { // 'x'
SmallString<32> CharBuffer;
CharBuffer.resize(PeekTok.getLength());
const char *ThisTokBegin = &CharBuffer[0];
unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
PeekTok.getLocation(), PP);
if (Literal.hadError())
return true; // A diagnostic was already emitted.
// Character literals are always int or wchar_t, expand to intmax_t.
TargetInfo &TI = PP.getTargetInfo();
unsigned NumBits;
if (Literal.isWide())
NumBits = TI.getWCharWidth(PeekTok.getLocation());
else
NumBits = TI.getCharWidth(PeekTok.getLocation());
// Set the width.
APSInt Val(NumBits);
// Set the value.
Val = Literal.getValue();
// Set the signedness.
Val.setIsUnsigned(!TI.isCharSigned(PeekTok.getLocation()));
if (Result.getBitWidth() > Val.getBitWidth()) {
if (Val.isSigned())
Result = Val.sext(Result.getBitWidth());
else
Result = Val.zext(Result.getBitWidth());
Result.setIsUnsigned(Val.isUnsigned());
} else {
assert(Result.getBitWidth() == Val.getBitWidth() &&
"intmax_t smaller than char/wchar_t?");
Result = Val;
}
// Consume the token.
PP.LexNonComment(PeekTok);
return false;
}
case tok::l_paren:
PP.LexNonComment(PeekTok); // Eat the (.
// Parse the value and if there are any binary operators involved, parse