[cfe-commits] r98966 - in /cfe/trunk: include/clang/Lex/PreprocessingRecord.h include/clang/Lex/Preprocessor.h lib/Lex/PreprocessingRecord.cpp lib/Lex/Preprocessor.cpp

Douglas Gregor dgregor at apple.com
Fri Mar 19 10:12:43 PDT 2010


Author: dgregor
Date: Fri Mar 19 12:12:43 2010
New Revision: 98966

URL: http://llvm.org/viewvc/llvm-project?rev=98966&view=rev
Log:
Make the preprocessing record a PPCallbacks subclass itself,
eliminating the extra PopulatePreprocessingRecord object. This will
become useful once we start writing the preprocessing record to
precompiled headers.

Modified:
    cfe/trunk/include/clang/Lex/PreprocessingRecord.h
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/lib/Lex/PreprocessingRecord.cpp
    cfe/trunk/lib/Lex/Preprocessor.cpp

Modified: cfe/trunk/include/clang/Lex/PreprocessingRecord.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/PreprocessingRecord.h?rev=98966&r1=98965&r2=98966&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/PreprocessingRecord.h (original)
+++ cfe/trunk/include/clang/Lex/PreprocessingRecord.h Fri Mar 19 12:12:43 2010
@@ -14,7 +14,9 @@
 #ifndef LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
 #define LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
 
+#include "clang/Lex/PPCallbacks.h"
 #include "clang/Basic/SourceLocation.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/Support/Allocator.h"
 #include <vector>
 
@@ -175,7 +177,7 @@
   /// \brief A record of the steps taken while preprocessing a source file,
   /// including the various preprocessing directives processed, macros 
   /// instantiated, etc.
-  class PreprocessingRecord {
+  class PreprocessingRecord : public PPCallbacks {
     /// \brief Allocator used to store preprocessing objects.
     llvm::BumpPtrAllocator BumpAlloc;
 
@@ -183,6 +185,9 @@
     /// were seen.
     std::vector<PreprocessedEntity *> PreprocessedEntities;
     
+    /// \brief Mapping from MacroInfo structures to their definitions.
+    llvm::DenseMap<const MacroInfo *, MacroDefinition *> MacroDefinitions;
+
   public:
     /// \brief Allocate memory in the preprocessing record.
     void *Allocate(unsigned Size, unsigned Align = 8) {
@@ -202,6 +207,13 @@
     
     /// \brief Add a new preprocessed entity to this record.
     void addPreprocessedEntity(PreprocessedEntity *Entity);
+    
+    /// \brief Retrieve the macro definition that corresponds to the given
+    /// \c MacroInfo.
+    MacroDefinition *findMacroDefinition(MacroInfo *MI);
+    
+    virtual void MacroExpands(const Token &Id, const MacroInfo* MI);
+    virtual void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI);
   };
 } // end namespace clang
 

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=98966&r1=98965&r2=98966&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Fri Mar 19 12:12:43 2010
@@ -215,7 +215,7 @@
   ///
   /// This is an optional side structure that can be enabled with
   /// \c createPreprocessingRecord() prior to preprocessing.
-  llvm::OwningPtr<PreprocessingRecord> Record;
+  PreprocessingRecord *Record;
   
 private:  // Cached tokens state.
   typedef llvm::SmallVector<Token, 1> CachedTokensTy;
@@ -358,7 +358,7 @@
 
   /// \brief Retrieve the preprocessing record, or NULL if there is no
   /// preprocessing record.
-  PreprocessingRecord *getPreprocessingRecord() const { return Record.get(); }
+  PreprocessingRecord *getPreprocessingRecord() const { return Record; }
   
   /// \brief Create a new preprocessing record, which will keep track of 
   /// all macro expansions, macro definitions, etc.

Modified: cfe/trunk/lib/Lex/PreprocessingRecord.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PreprocessingRecord.cpp?rev=98966&r1=98965&r2=98966&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PreprocessingRecord.cpp (original)
+++ cfe/trunk/lib/Lex/PreprocessingRecord.cpp Fri Mar 19 12:12:43 2010
@@ -21,3 +21,27 @@
   PreprocessedEntities.push_back(Entity);
 }
 
+MacroDefinition *PreprocessingRecord::findMacroDefinition(MacroInfo *MI) {
+  llvm::DenseMap<const MacroInfo *, MacroDefinition *>::iterator Pos
+    = MacroDefinitions.find(MI);
+  if (Pos == MacroDefinitions.end())
+    return 0;
+  
+  return Pos->second;
+}
+
+void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI) {
+  PreprocessedEntities.push_back(
+                       new (*this) MacroInstantiation(Id.getIdentifierInfo(),
+                                                      Id.getLocation(),
+                                                      MacroDefinitions[MI]));
+}
+
+void PreprocessingRecord::MacroDefined(const IdentifierInfo *II, 
+                                       const MacroInfo *MI) {
+  SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
+  MacroDefinition *Def
+    = new (*this) MacroDefinition(II, MI->getDefinitionLoc(), R);
+  MacroDefinitions[MI] = Def;
+  PreprocessedEntities.push_back(Def);
+}

Modified: cfe/trunk/lib/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Preprocessor.cpp?rev=98966&r1=98965&r2=98966&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/lib/Lex/Preprocessor.cpp Fri Mar 19 12:12:43 2010
@@ -54,7 +54,7 @@
   : Diags(&diags), Features(opts), Target(target),FileMgr(Headers.getFileMgr()),
     SourceMgr(SM), HeaderInfo(Headers), ExternalSource(0),
     Identifiers(opts, IILookup), BuiltinInfo(Target), CodeCompletionFile(0),
-    CurPPLexer(0), CurDirLookup(0), Callbacks(0), MacroArgCache(0) {
+    CurPPLexer(0), CurDirLookup(0), Callbacks(0), MacroArgCache(0), Record(0) {
   ScratchBuf = new ScratchBuffer(SourceMgr);
   CounterValue = 0; // __COUNTER__ starts at 0.
   OwnsHeaderSearch = OwnsHeaders;
@@ -629,46 +629,10 @@
 
 CommentHandler::~CommentHandler() { }
 
-namespace {
-  /// \brief Preprocessor callback action used to populate a preprocessing
-  /// record.
-  class PopulatePreprocessingRecord : public PPCallbacks {
-    /// \brief The preprocessing record this action will populate.
-    PreprocessingRecord &Record;
-    
-    /// \brief Mapping from MacroInfo structures to their definitions.
-    llvm::DenseMap<const MacroInfo *, MacroDefinition *> MacroDefinitions;
-    
-  public:
-    explicit PopulatePreprocessingRecord(PreprocessingRecord &Record)
-    : Record(Record) { }
-    
-    virtual void MacroExpands(const Token &Id, const MacroInfo* MI);
-    virtual void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI);
-  };  
-}
-
-void PopulatePreprocessingRecord::MacroExpands(const Token &Id, 
-                                               const MacroInfo* MI) {
-  Record.addPreprocessedEntity(
-                       new (Record) MacroInstantiation(Id.getIdentifierInfo(),
-                                                       Id.getLocation(),
-                                                       MacroDefinitions[MI]));
-}
-
-void PopulatePreprocessingRecord::MacroDefined(const IdentifierInfo *II, 
-                                               const MacroInfo *MI) {
-  SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
-  MacroDefinition *Def
-  = new (Record) MacroDefinition(II, MI->getDefinitionLoc(), R);
-  MacroDefinitions[MI] = Def;
-  Record.addPreprocessedEntity(Def);
-}
-
 void Preprocessor::createPreprocessingRecord() {
   if (Record)
     return;
   
-  Record.reset(new PreprocessingRecord);
-  addPPCallbacks(new PopulatePreprocessingRecord(*Record));
+  Record = new PreprocessingRecord;
+  addPPCallbacks(Record);
 }





More information about the cfe-commits mailing list