[cfe-commits] r68933 - in /cfe/trunk: include/clang/Basic/DiagnosticLexKinds.td include/clang/Frontend/PCHBitCodes.h include/clang/Lex/Preprocessor.h lib/Frontend/PCHReader.cpp lib/Frontend/PCHWriter.cpp lib/Lex/PPMacroExpansion.cpp lib/Lex/Preprocessor.cpp test/Lexer/counter.c test/PCH/variables.c test/PCH/variables.h

Chris Lattner sabre at nondot.org
Sun Apr 12 18:29:20 PDT 2009


Author: lattner
Date: Sun Apr 12 20:29:17 2009
New Revision: 68933

URL: http://llvm.org/viewvc/llvm-project?rev=68933&view=rev
Log:
implement the microsoft/gnu "__COUNTER__" macro: rdar://4329310

Added:
    cfe/trunk/test/Lexer/counter.c
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
    cfe/trunk/include/clang/Frontend/PCHBitCodes.h
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/lib/Frontend/PCHWriter.cpp
    cfe/trunk/lib/Lex/PPMacroExpansion.cpp
    cfe/trunk/lib/Lex/Preprocessor.cpp
    cfe/trunk/test/PCH/variables.c
    cfe/trunk/test/PCH/variables.h

Modified: cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td?rev=68933&r1=68932&r2=68933&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td Sun Apr 12 20:29:17 2009
@@ -132,6 +132,8 @@
 def ext_pp_include_level : Extension<
   "__INCLUDE_LEVEL__ is a language extension">;
 def ext_pp_timestamp : Extension<"__TIMESTAMP__ is a language extension">;
+def ext_pp_counter : Extension<
+  "__COUNTER__ is a language extension">;
 
 def err_pp_invalid_directive : Error<"invalid preprocessing directive">;
 def err_pp_hash_error : Error<"#error%0">;

Modified: cfe/trunk/include/clang/Frontend/PCHBitCodes.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHBitCodes.h?rev=68933&r1=68932&r2=68933&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHBitCodes.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHBitCodes.h Sun Apr 12 20:29:17 2009
@@ -165,8 +165,12 @@
       PP_MACRO_FUNCTION_LIKE = 2,
       
       /// \brief Describes one token.
-      /// [PPTOKEN, SLoc, Length, IdentInfoID, Kind, Flags]
-      PP_TOKEN = 3
+      /// [PP_TOKEN, SLoc, Length, IdentInfoID, Kind, Flags]
+      PP_TOKEN = 3,
+      
+      /// \brief The value of the next __COUNTER__ to dispense.
+      /// [PP_COUNTER_VALUE, Val]
+      PP_COUNTER_VALUE = 4
     };
     
 

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=68933&r1=68932&r2=68933&view=diff

==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Sun Apr 12 20:29:17 2009
@@ -67,9 +67,11 @@
   IdentifierInfo *Ident__INCLUDE_LEVEL__;          // __INCLUDE_LEVEL__
   IdentifierInfo *Ident__BASE_FILE__;              // __BASE_FILE__
   IdentifierInfo *Ident__TIMESTAMP__;              // __TIMESTAMP__
+  IdentifierInfo *Ident__COUNTER__;                // __COUNTER__
   IdentifierInfo *Ident_Pragma, *Ident__VA_ARGS__; // _Pragma, __VA_ARGS__
   
   SourceLocation DATELoc, TIMELoc;
+  unsigned CounterValue;  // Next __COUNTER__ value.
 
   enum {
     /// MaxIncludeStackDepth - Maximum depth of #includes.
@@ -590,6 +592,8 @@
   bool SawDateOrTime() const {
     return DATELoc != SourceLocation() || TIMELoc != SourceLocation();
   }
+  unsigned getCounterValue() const { return CounterValue; }
+  void setCounterValue(unsigned V) { CounterValue = V; }
   
   /// AllocateMacroInfo - Allocate a new MacroInfo object with the provide
   ///  SourceLocation.

Modified: cfe/trunk/lib/Frontend/PCHReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReader.cpp?rev=68933&r1=68932&r2=68933&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Sun Apr 12 20:29:17 2009
@@ -318,7 +318,11 @@
     switch (RecType) {
     default:  // Default behavior: ignore unknown records.
       break;
-        
+    case pch::PP_COUNTER_VALUE:
+      if (!Record.empty())
+        PP.setCounterValue(Record[0]);
+      break;
+
     case pch::PP_MACRO_OBJECT_LIKE:
     case pch::PP_MACRO_FUNCTION_LIKE: {
       IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);

Modified: cfe/trunk/lib/Frontend/PCHWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriter.cpp?rev=68933&r1=68932&r2=68933&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Sun Apr 12 20:29:17 2009
@@ -567,6 +567,13 @@
   
   RecordData Record;
 
+  // If the preprocessor __COUNTER__ value has been bumped, remember it.
+  if (PP.getCounterValue() != 0) {
+    Record.push_back(PP.getCounterValue());
+    S.EmitRecord(pch::PP_COUNTER_VALUE, Record);
+    Record.clear();
+  }  
+  
   // Loop over all the macro definitions that are live at the end of the file,
   // emitting each to the PP section.
   // FIXME: Eventually we want to emit an index so that we can lazily load
@@ -627,9 +634,6 @@
     
   }
   
-  // TODO: someday when PP supports __COUNTER__, emit a record for its value if
-  // non-zero.
-  
   S.ExitBlock();
 }
 

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

==============================================================================
--- cfe/trunk/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/trunk/lib/Lex/PPMacroExpansion.cpp Sun Apr 12 20:29:17 2009
@@ -55,6 +55,7 @@
   Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
   Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
   Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
+  Ident__COUNTER__ = RegisterBuiltinMacro("__COUNTER__");
   Ident_Pragma  = RegisterBuiltinMacro("_Pragma");
   
   // GCC Extensions.
@@ -557,6 +558,13 @@
     TmpBuffer[Len] = '"';  // Replace the newline with a quote.
     Tok.setKind(tok::string_literal);
     CreateString(TmpBuffer, Len+1, Tok, Tok.getLocation());
+  } else if (II == Ident__COUNTER__) {
+    Diag(Tok, diag::ext_pp_counter);
+    
+    // __COUNTER__ expands to a simple numeric value.
+    sprintf(TmpBuffer, "%u", CounterValue++);
+    Tok.setKind(tok::numeric_constant);
+    CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
   } else {
     assert(0 && "Unknown identifier!");
   }

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

==============================================================================
--- cfe/trunk/lib/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/lib/Lex/Preprocessor.cpp Sun Apr 12 20:29:17 2009
@@ -53,7 +53,8 @@
     SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts, IILookup),
     CurPPLexer(0), CurDirLookup(0), Callbacks(0) {
   ScratchBuf = new ScratchBuffer(SourceMgr);
-
+  CounterValue = 0; // __COUNTER__ starts at 0.
+      
   // Clear stats.
   NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
   NumIf = NumElse = NumEndif = 0;

Added: cfe/trunk/test/Lexer/counter.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/counter.c?rev=68933&view=auto

==============================================================================
--- cfe/trunk/test/Lexer/counter.c (added)
+++ cfe/trunk/test/Lexer/counter.c Sun Apr 12 20:29:17 2009
@@ -0,0 +1,16 @@
+// __COUNTER__ support: rdar://4329310
+// RUN: clang -E %s > %t &&
+
+#define PASTE2(x,y) x##y
+#define PASTE1(x,y) PASTE2(x,y)
+#define UNIQUE(x) PASTE1(x,__COUNTER__)
+
+// RUN: grep "A: 0" %t &&
+A: __COUNTER__
+
+// RUN: grep "B: foo1" %t &&
+B: UNIQUE(foo);
+// RUN: grep "C: foo2" %t &&
+C: UNIQUE(foo);
+// RUN: grep "D: 3" %t
+D: __COUNTER__

Modified: cfe/trunk/test/PCH/variables.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/variables.c?rev=68933&r1=68932&r2=68933&view=diff

==============================================================================
--- cfe/trunk/test/PCH/variables.c (original)
+++ cfe/trunk/test/PCH/variables.c Sun Apr 12 20:29:17 2009
@@ -15,4 +15,8 @@
 
 int Q = A_MACRO_IN_THE_PCH;
 
-int R = FUNCLIKE_MACRO(A_MACRO_, IN_THE_PCH);
\ No newline at end of file
+int R = FUNCLIKE_MACRO(A_MACRO_, IN_THE_PCH);
+
+
+int UNIQUE(a);  // a2
+int *Arr[] = { &a0, &a1, &a2 };

Modified: cfe/trunk/test/PCH/variables.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/variables.h?rev=68933&r1=68932&r2=68933&view=diff

==============================================================================
--- cfe/trunk/test/PCH/variables.h (original)
+++ cfe/trunk/test/PCH/variables.h Sun Apr 12 20:29:17 2009
@@ -16,3 +16,11 @@
 
 #define A_MACRO_IN_THE_PCH 492
 #define FUNCLIKE_MACRO(X, Y) X ## Y
+
+#define PASTE2(x,y) x##y
+#define PASTE1(x,y) PASTE2(x,y)
+#define UNIQUE(x) PASTE1(x,__COUNTER__)
+
+int UNIQUE(a);  // a0
+int UNIQUE(a);  // a1
+





More information about the cfe-commits mailing list