[cfe-commits] r42727 - in /cfe/trunk: Lex/IdentifierTable.cpp Lex/MacroExpander.cpp Lex/PPExpressions.cpp Lex/Preprocessor.cpp include/clang/Lex/IdentifierTable.h include/clang/Lex/MacroExpander.h include/clang/Lex/Preprocessor.h

Chris Lattner sabre at nondot.org
Sun Oct 7 01:44:21 PDT 2007


Author: lattner
Date: Sun Oct  7 03:44:20 2007
New Revision: 42727

URL: http://llvm.org/viewvc/llvm-project?rev=42727&view=rev
Log:
improve layering:
Now instead of IdentifierInfo knowing anything about MacroInfo,
only the preprocessor knows.  This makes MacroInfo truly private
to the Lex library (and its direct clients) instead of being 
accessed in the Basic library.

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

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

==============================================================================
--- cfe/trunk/Lex/IdentifierTable.cpp (original)
+++ cfe/trunk/Lex/IdentifierTable.cpp Sun Oct  7 03:44:20 2007
@@ -13,34 +13,18 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Lex/IdentifierTable.h"
-#include "clang/Lex/MacroInfo.h"
 #include "clang/Basic/LangOptions.h"
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/ADT/DenseMap.h"
 using namespace clang;
 
-static llvm::DenseMap<const IdentifierInfo*, MacroInfo*> Macros;
-
-MacroInfo *IdentifierInfo::getMacroInfoInternal() const {
-  return Macros[this];
-}
-void IdentifierInfo::setMacroInfo(MacroInfo *I) {
-  if (I == 0) {
-    if (HasMacro) {
-      Macros.erase(this);
-      HasMacro = false;
-    }
-  } else {
-    Macros[this] = I;
-    HasMacro = true;
-  }
-}
-
-
 //===----------------------------------------------------------------------===//
 // Token Implementation
 //===----------------------------------------------------------------------===//
 
+// FIXME: Move this elsewhere!
+#include "clang/Lex/Token.h"
+
 /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier. 
 bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
   return getKind() == tok::identifier && 
@@ -70,11 +54,6 @@
   FETokenInfo = 0;
 }
 
-IdentifierInfo::~IdentifierInfo() {
-  if (MacroInfo *Macro = getMacroInfo())
-    delete Macro;
-}
-
 //===----------------------------------------------------------------------===//
 // IdentifierTable Implementation
 //===----------------------------------------------------------------------===//

Modified: cfe/trunk/Lex/MacroExpander.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Lex/MacroExpander.cpp?rev=42727&r1=42726&r2=42727&view=diff

==============================================================================
--- cfe/trunk/Lex/MacroExpander.cpp (original)
+++ cfe/trunk/Lex/MacroExpander.cpp Sun Oct  7 03:44:20 2007
@@ -84,12 +84,13 @@
 
 /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
 /// by pre-expansion, return false.  Otherwise, conservatively return true.
-bool MacroArgs::ArgNeedsPreexpansion(const Token *ArgTok) const {
+bool MacroArgs::ArgNeedsPreexpansion(const Token *ArgTok,
+                                     Preprocessor &PP) const {
   // If there are no identifiers in the argument list, or if the identifiers are
   // known to not be macros, pre-expansion won't modify it.
   for (; ArgTok->getKind() != tok::eof; ++ArgTok)
     if (IdentifierInfo *II = ArgTok->getIdentifierInfo()) {
-      if (II->getMacroInfo() && II->getMacroInfo()->isEnabled())
+      if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled())
         // Return true even though the macro could be a function-like macro
         // without a following '(' token.
         return true;
@@ -238,7 +239,7 @@
   // associated with it.
   destroy();
   
-  Macro = Tok.getIdentifierInfo()->getMacroInfo();
+  Macro = PP.getMacroInfo(Tok.getIdentifierInfo());
   ActualArgs = Actuals;
   CurToken = 0;
   InstantiateLoc = Tok.getLocation();
@@ -377,7 +378,7 @@
       // Only preexpand the argument if it could possibly need it.  This
       // avoids some work in common cases.
       const Token *ArgTok = ActualArgs->getUnexpArgument(ArgNo);
-      if (ActualArgs->ArgNeedsPreexpansion(ArgTok))
+      if (ActualArgs->ArgNeedsPreexpansion(ArgTok, PP))
         ResultArgToks = &ActualArgs->getPreExpArgument(ArgNo, PP)[0];
       else
         ResultArgToks = ArgTok;  // Use non-preexpanded tokens.

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

==============================================================================
--- cfe/trunk/Lex/PPExpressions.cpp (original)
+++ cfe/trunk/Lex/PPExpressions.cpp Sun Oct  7 03:44:20 2007
@@ -105,7 +105,7 @@
 
     // If there is a macro, mark it used.
     if (Result != 0 && ValueLive) {
-      MacroInfo *Macro = II->getMacroInfo();
+      MacroInfo *Macro = PP.getMacroInfo(II);
       Macro->setIsUsed(true);
       
       // If this is the first use of a target-specific macro, warn about it.

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

==============================================================================
--- cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/Lex/Preprocessor.cpp Sun Oct  7 03:44:20 2007
@@ -90,6 +90,15 @@
     delete IncludeMacroStack.back().TheMacroExpander;
     IncludeMacroStack.pop_back();
   }
+
+  // Free any macro definitions.
+  for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
+       Macros.begin(), E = Macros.end(); I != E; ++I) {
+    // Free the macro definition.
+    delete I->second;
+    I->second = 0;
+    I->first->setHasMacroDefinition(false);
+  }
   
   // Free any cached macro expanders.
   for (unsigned i = 0, e = NumCachedMacroExpanders; i != e; ++i)
@@ -493,6 +502,20 @@
 // Macro Expansion Handling.
 //===----------------------------------------------------------------------===//
 
+/// setMacroInfo - Specify a macro for this identifier.
+///
+void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
+  if (MI == 0) {
+    if (II->hasMacroDefinition()) {
+      Macros.erase(II);
+      II->setHasMacroDefinition(false);
+    }
+  } else {
+    Macros[II] = MI;
+    II->setHasMacroDefinition(true);
+  }
+}
+
 /// RegisterBuiltinMacro - Register the specified identifier in the identifier
 /// table and mark it as a builtin macro to be expanded.
 IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
@@ -502,7 +525,7 @@
   // Mark it as being a macro that is builtin.
   MacroInfo *MI = new MacroInfo(SourceLocation());
   MI->setIsBuiltinMacro();
-  Id->setMacroInfo(MI);
+  setMacroInfo(Id, MI);
   return Id;
 }
 
@@ -525,7 +548,8 @@
 /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
 /// in its expansion, currently expands to that token literally.
 static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
-                                          const IdentifierInfo *MacroIdent) {
+                                          const IdentifierInfo *MacroIdent,
+                                          Preprocessor &PP) {
   IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
 
   // If the token isn't an identifier, it's always literally expanded.
@@ -533,7 +557,7 @@
   
   // If the identifier is a macro, and if that macro is enabled, it may be
   // expanded so it's not a trivial expansion.
-  if (II->hasMacroDefinition() && II->getMacroInfo()->isEnabled() &&
+  if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
       // Fast expanding "#define X X" is ok, because X would be disabled.
       II != MacroIdent)
     return false;
@@ -676,7 +700,8 @@
     return false;
     
   } else if (MI->getNumTokens() == 1 &&
-             isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo())){
+             isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
+                                           *this)){
     // Otherwise, if this macro expands into a single trivially-expanded
     // token: expand it now.  This handles common cases like 
     // "#define VAL 42".
@@ -704,7 +729,7 @@
     
     // If this is #define X X, we must mark the result as unexpandible.
     if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
-      if (NewII->getMacroInfo() == MI)
+      if (getMacroInfo(NewII) == MI)
         Identifier.setFlag(Token::DisableExpand);
     
     // Since this is not an identifier token, it can't be macro expanded, so
@@ -1020,7 +1045,7 @@
   }
   
   // If this is a macro to be expanded, do it.
-  if (MacroInfo *MI = II.getMacroInfo()) {
+  if (MacroInfo *MI = getMacroInfo(&II)) {
     if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
       if (MI->isEnabled()) {
         if (!HandleMacroExpandedIdentifier(Identifier, MI))
@@ -1110,17 +1135,14 @@
   CurLexer = 0;
 
   // This is the end of the top-level file.  If the diag::pp_macro_not_used
-  // diagnostic is enabled, walk all of the identifiers, looking for macros that
-  // have not been used.
+  // diagnostic is enabled, look for macros that have not been used.
   if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){
-    for (IdentifierTable::iterator I = Identifiers.begin(),
-         E = Identifiers.end(); I != E; ++I) {
-      const IdentifierInfo &II = I->getValue();
-      if (II.hasMacroDefinition() && !II.getMacroInfo()->isUsed())
-        Diag(II.getMacroInfo()->getDefinitionLoc(), diag::pp_macro_not_used);
+    for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
+         Macros.begin(), E = Macros.end(); I != E; ++I) {
+      if (!I->second->isUsed())
+        Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used);
     }
   }
-  
   return true;
 }
 
@@ -1189,7 +1211,7 @@
     // Error if defining "defined": C99 6.10.8.4.
     Diag(MacroNameTok, diag::err_defined_macro_name);
   } else if (isDefineUndef && II->hasMacroDefinition() &&
-             II->getMacroInfo()->isBuiltinMacro()) {
+             getMacroInfo(II)->isBuiltinMacro()) {
     // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
     if (isDefineUndef == 1)
       Diag(MacroNameTok, diag::pp_redef_builtin_macro);
@@ -2029,7 +2051,7 @@
   
   // Finally, if this identifier already had a macro defined for it, verify that
   // the macro bodies are identical and free the old definition.
-  if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
+  if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
     if (!OtherMI->isUsed())
       Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
 
@@ -2043,7 +2065,7 @@
     delete OtherMI;
   }
   
-  MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
+  setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
 }
 
 /// HandleDefineOtherTargetDirective - Implements #define_other_target.
@@ -2060,7 +2082,7 @@
 
   // If there is already a macro defined by this name, turn it into a
   // target-specific define.
-  if (MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
+  if (MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
     MI->setIsTargetSpecific(true);
     return;
   }
@@ -2086,7 +2108,7 @@
   CheckEndOfDirective("#undef");
   
   // Okay, we finally have a valid identifier to undef.
-  MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
+  MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
   
   // #undef untaints an identifier if it were marked by define_other_target.
   MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
@@ -2099,7 +2121,7 @@
   
   // Free macro definition.
   delete MI;
-  MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
+  setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
 }
 
 
@@ -2140,7 +2162,7 @@
   }
   
   IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
-  MacroInfo *MI = MII->getMacroInfo();
+  MacroInfo *MI = getMacroInfo(MII);
 
   // If there is a macro, process it.
   if (MI) {

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

==============================================================================
--- cfe/trunk/include/clang/Lex/IdentifierTable.h (original)
+++ cfe/trunk/include/clang/Lex/IdentifierTable.h Sun Oct  7 03:44:20 2007
@@ -27,7 +27,6 @@
 }
 
 namespace clang {
-  class MacroInfo;
   struct LangOptions;
   class MultiKeywordSelector; // a private class used by Selector.
   
@@ -51,7 +50,6 @@
   void operator=(const IdentifierInfo&);  // NONASSIGNABLE.
 public:
   IdentifierInfo();
-  ~IdentifierInfo();
 
   /// getName - Return the actual string for this identifier.  The returned 
   /// string is properly null terminated.
@@ -75,13 +73,7 @@
   bool hasMacroDefinition() const {
     return HasMacro;
   }
-  
-  /// getMacroInfo - Return macro information about this identifier, or null if
-  /// it is not a macro.
-  MacroInfo *getMacroInfo() const {
-    return HasMacro ? getMacroInfoInternal() : 0;
-  }
-  void setMacroInfo(MacroInfo *I);
+  void setHasMacroDefinition(bool Val) { HasMacro = Val; }
   
   /// get/setTokenID - If this is a source-language token (e.g. 'for'), this API
   /// can be used to cause the lexer to map identifiers to source-language
@@ -144,8 +136,6 @@
   template<typename T>
   T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }
   void setFETokenInfo(void *T) { FETokenInfo = T; }
-private:
-  MacroInfo *getMacroInfoInternal() const;
 };
 
 /// IdentifierTable - This table implements an efficient mapping from strings to

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

==============================================================================
--- cfe/trunk/include/clang/Lex/MacroExpander.h (original)
+++ cfe/trunk/include/clang/Lex/MacroExpander.h Sun Oct  7 03:44:20 2007
@@ -63,7 +63,7 @@
   
   /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
   /// by pre-expansion, return false.  Otherwise, conservatively return true.
-  bool ArgNeedsPreexpansion(const Token *ArgTok) const;
+  bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const;
   
   /// getUnexpArgument - Return a pointer to the first token of the unexpanded
   /// token list for the specified formal.

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

==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Sun Oct  7 03:44:20 2007
@@ -18,6 +18,7 @@
 #include "clang/Lex/Lexer.h"
 #include "clang/Lex/MacroExpander.h"
 #include "clang/Basic/SourceLocation.h"
+#include "llvm/ADT/DenseMap.h"
 
 namespace clang {
   
@@ -116,6 +117,10 @@
   /// encountered (e.g. a file is #included, etc).
   PPCallbacks *Callbacks;
   
+  /// Macros - For each IdentifierInfo with 'HasMacro' set, we keep a mapping
+  /// to the actual definition of the macro.
+  llvm::DenseMap<IdentifierInfo*, MacroInfo*> Macros;
+  
   // Various statistics we track for performance analysis.
   unsigned NumDirectives, NumIncluded, NumDefined, NumUndefined, NumPragma;
   unsigned NumIf, NumElse, NumEndif;
@@ -174,6 +179,16 @@
     Callbacks = C;
   }
   
+  /// getMacroInfo - Given an identifier, return the MacroInfo it is #defined to
+  /// or null if it isn't #define'd.
+  MacroInfo *getMacroInfo(IdentifierInfo *II) const {
+    return II->hasMacroDefinition() ? Macros.find(II)->second : 0;
+  }
+  
+  /// setMacroInfo - Specify a macro for this identifier.
+  ///
+  void setMacroInfo(IdentifierInfo *II, MacroInfo *MI);
+  
   /// getIdentifierInfo - Return information about the specified preprocessor
   /// identifier token.  The version of this method that takes two character
   /// pointers is preferred unless the identifier is already available as a





More information about the cfe-commits mailing list