[cfe-commits] r39014 - in /cfe/cfe/trunk: Driver/clang.cpp Lex/IdentifierTable.cpp Lex/Preprocessor.cpp include/clang/Lex/IdentifierTable.h include/clang/Lex/Preprocessor.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:26:50 PDT 2007


Author: sabre
Date: Wed Jul 11 11:26:50 2007
New Revision: 39014

URL: http://llvm.org/viewvc/llvm-project?rev=39014&view=rev
Log:
Move keyword setup from the preprocessor into the IdentifierTable class.

Modified:
    cfe/cfe/trunk/Driver/clang.cpp
    cfe/cfe/trunk/Lex/IdentifierTable.cpp
    cfe/cfe/trunk/Lex/Preprocessor.cpp
    cfe/cfe/trunk/include/clang/Lex/IdentifierTable.h
    cfe/cfe/trunk/include/clang/Lex/Preprocessor.h

Modified: cfe/cfe/trunk/Driver/clang.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Driver/clang.cpp?rev=39014&r1=39013&r2=39014&view=diff

==============================================================================
--- cfe/cfe/trunk/Driver/clang.cpp (original)
+++ cfe/cfe/trunk/Driver/clang.cpp Wed Jul 11 11:26:50 2007
@@ -843,9 +843,6 @@
   // Read any files specified by -imacros or -include.
   ReadPrologFiles(PP, PrologMacros);
   
-  // Set up keywords.
-  PP.AddKeywords();
-  
   // Figure out where to get and map in the main file.
   unsigned MainFileID = 0;
   if (InputFilename != "-") {

Modified: cfe/cfe/trunk/Lex/IdentifierTable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/IdentifierTable.cpp?rev=39014&r1=39013&r2=39014&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/IdentifierTable.cpp (original)
+++ cfe/cfe/trunk/Lex/IdentifierTable.cpp Wed Jul 11 11:26:50 2007
@@ -14,6 +14,7 @@
 
 #include "clang/Lex/IdentifierTable.h"
 #include "clang/Lex/MacroInfo.h"
+#include "clang/Basic/LangOptions.h"
 #include <iostream>
 using namespace llvm;
 using namespace clang;
@@ -126,7 +127,7 @@
 // FIXME: start hashtablesize off at 8K entries, GROW when density gets to 3.
 static unsigned HASH_TABLE_SIZE = 8096;
 
-IdentifierTable::IdentifierTable() {
+IdentifierTable::IdentifierTable(const LangOptions &LangOpts) {
   IdentifierBucket **TableArray = new IdentifierBucket*[HASH_TABLE_SIZE]();
   TheTable = TableArray;
   NumIdentifiers = 0;
@@ -136,6 +137,8 @@
 #endif
   
   memset(TheTable, 0, HASH_TABLE_SIZE*sizeof(IdentifierBucket*));
+  
+  AddKeywords(LangOpts);
 }
 
 IdentifierTable::~IdentifierTable() {
@@ -244,6 +247,88 @@
   }
 }
 
+//===----------------------------------------------------------------------===//
+// Language Keyword Implementation
+//===----------------------------------------------------------------------===//
+
+/// AddKeyword - This method is used to associate a token ID with specific
+/// identifiers because they are language keywords.  This causes the lexer to
+/// automatically map matching identifiers to specialized token codes.
+///
+/// The C90/C99/CPP flags are set to 0 if the token should be enabled in the
+/// specified langauge, set to 1 if it is an extension in the specified
+/// language, and set to 2 if disabled in the specified language.
+static void AddKeyword(const std::string &Keyword, tok::TokenKind TokenCode,
+                       int C90, int C99, int CPP,
+                       const LangOptions &LangOpts, IdentifierTable &Table) {
+  int Flags = LangOpts.CPlusPlus ? CPP : (LangOpts.C99 ? C99 : C90);
+  
+  // Don't add this keyword if disabled in this language or if an extension
+  // and extensions are disabled.
+  if (Flags + LangOpts.NoExtensions >= 2) return;
+  
+  const char *Str = &Keyword[0];
+  IdentifierInfo &Info = Table.get(Str, Str+Keyword.size());
+  Info.setTokenID(TokenCode);
+  Info.setIsExtensionToken(Flags == 1);
+}
+
+/// AddPPKeyword - Register a preprocessor keyword like "define" "undef" or 
+/// "elif".
+static void AddPPKeyword(tok::PPKeywordKind PPID, 
+                         const char *Name, unsigned NameLen,
+                         IdentifierTable &Table) {
+  Table.get(Name, Name+NameLen).setPPKeywordID(PPID);
+}
+
+/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or 
+/// "property".
+static void AddObjCKeyword(tok::ObjCKeywordKind ObjCID, 
+                           const char *Name, unsigned NameLen,
+                           IdentifierTable &Table) {
+  Table.get(Name, Name+NameLen).setObjCKeywordID(ObjCID);
+}
+
+/// AddKeywords - Add all keywords to the symbol table.
+///
+void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
+  enum {
+    C90Shift = 0,
+    EXTC90   = 1 << C90Shift,
+    NOTC90   = 2 << C90Shift,
+    C99Shift = 2,
+    EXTC99   = 1 << C99Shift,
+    NOTC99   = 2 << C99Shift,
+    CPPShift = 4,
+    EXTCPP   = 1 << CPPShift,
+    NOTCPP   = 2 << CPPShift,
+    Mask     = 3
+  };
+  
+  // Add keywords and tokens for the current language.
+#define KEYWORD(NAME, FLAGS) \
+  AddKeyword(#NAME, tok::kw_ ## NAME,  \
+             ((FLAGS) >> C90Shift) & Mask, \
+             ((FLAGS) >> C99Shift) & Mask, \
+             ((FLAGS) >> CPPShift) & Mask, LangOpts, *this);
+#define ALIAS(NAME, TOK) \
+  AddKeyword(NAME, tok::kw_ ## TOK, 0, 0, 0, LangOpts, *this);
+#define PPKEYWORD(NAME) \
+  AddPPKeyword(tok::pp_##NAME, #NAME, strlen(#NAME), *this);
+#define OBJC1_AT_KEYWORD(NAME) \
+  if (LangOpts.ObjC1)          \
+    AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
+#define OBJC2_AT_KEYWORD(NAME) \
+  if (LangOpts.ObjC2)          \
+    AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
+#include "clang/Basic/TokenKinds.def"
+}
+
+
+//===----------------------------------------------------------------------===//
+// Stats Implementation
+//===----------------------------------------------------------------------===//
+
 /// PrintStats - Print statistics about how well the identifier table is doing
 /// at hashing identifiers.
 void IdentifierTable::PrintStats() const {

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

==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:26:50 2007
@@ -45,7 +45,7 @@
                            FileManager &FM, SourceManager &SM, 
                            HeaderSearch &Headers) 
   : Diags(diags), Features(opts), Target(target), FileMgr(FM), SourceMgr(SM),
-    HeaderInfo(Headers), 
+    HeaderInfo(Headers), Identifiers(opts),
     CurLexer(0), CurDirLookup(0), CurMacroExpander(0) {
   ScratchBuf = new ScratchBuffer(SourceMgr);
       
@@ -95,77 +95,6 @@
   delete ScratchBuf;
 }
 
-/// AddPPKeyword - Register a preprocessor keyword like "define" "undef" or 
-/// "elif".
-static void AddPPKeyword(tok::PPKeywordKind PPID, 
-                         const char *Name, unsigned NameLen, Preprocessor &PP) {
-  PP.getIdentifierInfo(Name, Name+NameLen)->setPPKeywordID(PPID);
-}
-
-/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or 
-/// "property".
-static void AddObjCKeyword(tok::ObjCKeywordKind ObjCID, 
-                           const char *Name, unsigned NameLen,
-                           Preprocessor &PP) {
-  PP.getIdentifierInfo(Name, Name+NameLen)->setObjCKeywordID(ObjCID);
-}
-
-/// AddKeywords - Add all keywords to the symbol table.
-///
-void Preprocessor::AddKeywords() {
-  enum {
-    C90Shift = 0,
-    EXTC90   = 1 << C90Shift,
-    NOTC90   = 2 << C90Shift,
-    C99Shift = 2,
-    EXTC99   = 1 << C99Shift,
-    NOTC99   = 2 << C99Shift,
-    CPPShift = 4,
-    EXTCPP   = 1 << CPPShift,
-    NOTCPP   = 2 << CPPShift,
-    Mask     = 3
-  };
-
-  // Add keywords and tokens for the current language.
-#define KEYWORD(NAME, FLAGS) \
-  AddKeyword(#NAME, tok::kw_ ## NAME,  \
-             ((FLAGS) >> C90Shift) & Mask, \
-             ((FLAGS) >> C99Shift) & Mask, \
-             ((FLAGS) >> CPPShift) & Mask);
-#define ALIAS(NAME, TOK) \
-  AddKeyword(NAME, tok::kw_ ## TOK, 0, 0, 0);
-#define PPKEYWORD(NAME) \
-  AddPPKeyword(tok::pp_##NAME, #NAME, strlen(#NAME), *this);
-#define OBJC1_AT_KEYWORD(NAME) \
-  if (Features.ObjC1)          \
-    AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
-#define OBJC2_AT_KEYWORD(NAME) \
-  if (Features.ObjC2)          \
-    AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
-#include "clang/Basic/TokenKinds.def"
-}
-
-/// AddKeyword - This method is used to associate a token ID with specific
-/// identifiers because they are language keywords.  This causes the lexer to
-/// automatically map matching identifiers to specialized token codes.
-///
-/// The C90/C99/CPP flags are set to 0 if the token should be enabled in the
-/// specified langauge, set to 1 if it is an extension in the specified
-/// language, and set to 2 if disabled in the specified language.
-void Preprocessor::AddKeyword(const std::string &Keyword,
-                              tok::TokenKind TokenCode,
-                              int C90, int C99, int CPP) {
-  int Flags = Features.CPlusPlus ? CPP : (Features.C99 ? C99 : C90);
-  
-  // Don't add this keyword if disabled in this language or if an extension
-  // and extensions are disabled.
-  if (Flags+Features.NoExtensions >= 2) return;
-  
-  const char *Str = &Keyword[0];
-  IdentifierInfo &Info = *getIdentifierInfo(Str, Str+Keyword.size());
-  Info.setTokenID(TokenCode);
-  Info.setIsExtensionToken(Flags == 1);
-}
 
 
 /// Diag - Forwarding function for diagnostics.  This emits a diagnostic at

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

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/IdentifierTable.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/IdentifierTable.h Wed Jul 11 11:26:50 2007
@@ -22,6 +22,7 @@
 namespace clang {
   class IdentifierTable;
   class MacroInfo;
+  class LangOptions;
   
 /// IdentifierInfo - One of these records is kept for each identifier that
 /// is lexed.  This contains information about whether the token was #define'd,
@@ -121,7 +122,9 @@
   void *TheMemory;
   unsigned NumIdentifiers;
 public:
-  IdentifierTable();
+  /// IdentifierTable ctor - Create the identifier table, populating it with
+  /// info about the language keywords for the language specified by LangOpts.
+  IdentifierTable(const LangOptions &LangOpts);
   ~IdentifierTable();
   
   /// get - Return the identifier token info for the specified named identifier.
@@ -136,6 +139,8 @@
   /// PrintStats - Print some statistics to stderr that indicate how well the
   /// hashing is doing.
   void PrintStats() const;
+private:
+  void AddKeywords(const LangOptions &LangOpts);
 };
 
 }  // end namespace llvm

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

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/Preprocessor.h Wed Jul 11 11:26:50 2007
@@ -184,21 +184,6 @@
     return getIdentifierInfo(NameStr, NameStr+strlen(NameStr));
   }
   
-  /// AddKeyword - This method is used to associate a token ID with specific
-  /// identifiers because they are language keywords.  This causes the lexer to
-  /// automatically map matching identifiers to specialized token codes.
-  ///
-  /// The C90/C99/CPP flags are set to 0 if the token should be enabled in the
-  /// specified langauge, set to 1 if it is an extension in the specified
-  /// language, and set to 2 if disabled in the specified language.
-  void AddKeyword(const std::string &Keyword, tok::TokenKind TokenCode,
-                  int C90, int C99, int CPP);
-  
-  /// AddKeywords - Add all keywords to the symbol table.
-  ///
-  void AddKeywords();
-  
-  
   /// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
   /// If 'Namespace' is non-null, then it is a token required to exist on the
   /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".





More information about the cfe-commits mailing list