[cfe-commits] r63917 - /cfe/trunk/lib/Lex/Preprocessor.cpp

Chris Lattner sabre at nondot.org
Thu Feb 5 20:50:25 PST 2009


Author: lattner
Date: Thu Feb  5 22:50:25 2009
New Revision: 63917

URL: http://llvm.org/viewvc/llvm-project?rev=63917&view=rev
Log:
refactor some code into a DefineTypeSize function.

Modified:
    cfe/trunk/lib/Lex/Preprocessor.cpp

Modified: cfe/trunk/lib/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Preprocessor.cpp?rev=63917&r1=63916&r2=63917&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/lib/Lex/Preprocessor.cpp Thu Feb  5 22:50:25 2009
@@ -411,6 +411,23 @@
 }
 
 
+/// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
+/// named MacroName with the max value for a type with width 'TypeWidth' a
+/// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
+static void DefineTypeSize(const char *MacroName, unsigned TypeWidth,
+                           const char *ValSuffix, bool isSigned,
+                           std::vector<char> &Buf) {
+  char MacroBuf[60];
+  uint64_t MaxVal;
+  if (isSigned)
+    MaxVal = (1LL << (TypeWidth - 1)) - 1;
+  else
+    MaxVal = ~0LL >> (64-TypeWidth);
+  
+  sprintf(MacroBuf, "%s=%llu%s", MacroName, MaxVal, ValSuffix);
+  DefineBuiltinMacro(Buf, MacroBuf);
+}
+
 static void InitializePredefinedMacros(Preprocessor &PP, 
                                        std::vector<char> &Buf) {
   // Compiler version introspection macros.
@@ -530,7 +547,7 @@
     DefineBuiltinMacro(Buf, "__LONG_MAX__=32767L");
   else
     assert(0 && "Unknown long size");
-  char MacroBuf[60];
+  
   unsigned IntMaxWidth;
   const char *IntMaxSuffix;
   if (TI.getIntMaxType() == TargetInfo::SignedLongLong) {
@@ -545,10 +562,8 @@
     IntMaxSuffix = "";
   }
   
-  sprintf(MacroBuf, "__INTMAX_MAX__=%lld%s", (1LL << (IntMaxWidth - 1)) - 1,
-          IntMaxSuffix);
-  DefineBuiltinMacro(Buf, MacroBuf);
-  
+  DefineTypeSize("__INTMAX_MAX__", IntMaxWidth, IntMaxSuffix, true, Buf);
+
   if (TI.getIntMaxType() == TargetInfo::UnsignedLongLong)
     DefineBuiltinMacro(Buf, "__INTMAX_TYPE__=unsigned long long int");
   else if (TI.getIntMaxType() == TargetInfo::SignedLongLong)
@@ -611,7 +626,6 @@
   DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat());
   DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat());
   
-  
   // Add __builtin_va_list typedef.
   {
     const char *VAList = TI.getVAListDeclaration();
@@ -619,6 +633,7 @@
     Buf.push_back('\n');
   }
   
+  char MacroBuf[60];
   if (const char *Prefix = TI.getUserLabelPrefix()) {
     sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix);
     DefineBuiltinMacro(Buf, MacroBuf);





More information about the cfe-commits mailing list