[cfe-commits] r39393 - /cfe/cfe/trunk/Lex/PPExpressions.cpp

clattner at cs.uiuc.edu clattner at cs.uiuc.edu
Wed Jul 11 09:43:54 PDT 2007


Author: clattner
Date: Wed Jul 11 11:43:54 2007
New Revision: 39393

URL: http://llvm.org/viewvc/llvm-project?rev=39393&view=rev
Log:
Add support for character constants in PP expressions, like:

#if 'a'

Modified:
    cfe/cfe/trunk/Lex/PPExpressions.cpp

Modified: cfe/cfe/trunk/Lex/PPExpressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/PPExpressions.cpp?rev=39393&r1=39392&r2=39393&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/PPExpressions.cpp (original)
+++ cfe/cfe/trunk/Lex/PPExpressions.cpp Wed Jul 11 11:43:54 2007
@@ -186,6 +186,47 @@
     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





More information about the cfe-commits mailing list