summaryrefslogtreecommitdiffstats
path: root/clang/Lex/PPExpressions.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2007-04-05 06:58:56 +0000
committerChris Lattner <sabre@nondot.org>2007-04-05 06:58:56 +0000
commitf8a0b0fa50905b7203bb428b5138f5aaa631251b (patch)
tree339b8ad4f1cc774a1258fdfac2a1ff51d5c7c1e6 /clang/Lex/PPExpressions.cpp
parent380048807ebf53d4ee5c9f86488c71f7b055c9ac (diff)
downloadbcm5719-llvm-f8a0b0fa50905b7203bb428b5138f5aaa631251b.tar.gz
bcm5719-llvm-f8a0b0fa50905b7203bb428b5138f5aaa631251b.zip
Add support for character constants in PP expressions, like:
#if 'a' llvm-svn: 39393
Diffstat (limited to 'clang/Lex/PPExpressions.cpp')
-rw-r--r--clang/Lex/PPExpressions.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/clang/Lex/PPExpressions.cpp b/clang/Lex/PPExpressions.cpp
index 4258d8b8a63..df5f4bd26da 100644
--- a/clang/Lex/PPExpressions.cpp
+++ b/clang/Lex/PPExpressions.cpp
@@ -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
OpenPOWER on IntegriCloud