[cfe-commits] r39203 - in /cfe/cfe/trunk: Driver/PrintPreprocessedOutput.cpp Lex/Pragma.cpp Lex/Preprocessor.cpp clang.xcodeproj/project.pbxproj include/clang/Lex/PPCallbacks.h include/clang/Lex/Preprocessor.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:41:08 PDT 2007


Author: sabre
Date: Wed Jul 11 11:41:08 2007
New Revision: 39203

URL: http://llvm.org/viewvc/llvm-project?rev=39203&view=rev
Log:
Formalize preprocessor callbacks together into a PPCallbacks structure, instead
of having a loose collection of function pointers.  This also allows clients to
maintain state, and reduces the size of the Preprocessor.h interface.

Added:
    cfe/cfe/trunk/include/clang/Lex/PPCallbacks.h   (with props)
Modified:
    cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp
    cfe/cfe/trunk/Lex/Pragma.cpp
    cfe/cfe/trunk/Lex/Preprocessor.cpp
    cfe/cfe/trunk/clang.xcodeproj/project.pbxproj
    cfe/cfe/trunk/include/clang/Lex/Preprocessor.h

Modified: cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp?rev=39203&r1=39202&r2=39203&view=diff

==============================================================================
--- cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp (original)
+++ cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp Wed Jul 11 11:41:08 2007
@@ -13,6 +13,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang.h"
+#include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Lex/Pragma.h"
 #include "clang/Basic/SourceManager.h"
@@ -155,19 +156,30 @@
   } 
 }
 
-/// HandleFileChange - Whenever the preprocessor enters or exits a #include file
-/// it invokes this handler.  Update our conception of the current 
-static void HandleFileChange(SourceLocation Loc,
-                             Preprocessor::FileChangeReason Reason,
-                             DirectoryLookup::DirType FileType) {
+namespace {
+class PrintPPOutputPPCallbacks : public PPCallbacks {
+public:
+  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
+                           DirectoryLookup::DirType FileType);
+  virtual void Ident(SourceLocation Loc, const std::string &str);
+};
+}
+
+/// FileChanged - Whenever the preprocessor enters or exits a #include file
+/// it invokes this handler.  Update our conception of the current source
+/// position.
+
+void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
+                                           FileChangeReason Reason,
+                                           DirectoryLookup::DirType FileType) {
   if (DisableLineMarkers) return;
 
   // Unless we are exiting a #include, make sure to skip ahead to the line the
   // #include directive was at.
   SourceManager &SourceMgr = EModePP->getSourceManager();
-  if (Reason == Preprocessor::EnterFile) {
+  if (Reason == PPCallbacks::EnterFile) {
     MoveToLine(SourceMgr.getIncludeLoc(Loc.getFileID()));
-  } else if (Reason == Preprocessor::SystemHeaderPragma) {
+  } else if (Reason == PPCallbacks::SystemHeaderPragma) {
     MoveToLine(Loc);
     
     // TODO GCC emits the # directive for this directive on the line AFTER the
@@ -194,14 +206,14 @@
   OutputString(&EModeCurFilename[0], EModeCurFilename.size());
   
   switch (Reason) {
-  case Preprocessor::EnterFile:
+  case PPCallbacks::EnterFile:
     OutputString(" 1", 2);
     break;
-  case Preprocessor::ExitFile:
+  case PPCallbacks::ExitFile:
     OutputString(" 2", 2);
     break;
-  case Preprocessor::SystemHeaderPragma: break;
-  case Preprocessor::RenameFile: break;
+  case PPCallbacks::SystemHeaderPragma: break;
+  case PPCallbacks::RenameFile: break;
   }
   
   if (FileType == DirectoryLookup::SystemHeaderDir)
@@ -214,11 +226,11 @@
 
 /// HandleIdent - Handle #ident directives when read by the preprocessor.
 ///
-static void HandleIdent(SourceLocation Loc, const std::string &Val) {
+void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
   MoveToLine(Loc);
   
   OutputString("#ident ", strlen("#ident "));
-  OutputString(&Val[0], Val.size());
+  OutputString(&S[0], S.size());
   EmodeEmittedTokensOnThisLine = true;
 }
 
@@ -370,8 +382,7 @@
   char Buffer[256];
   EModeCurLine = 0;
   EModeCurFilename = "\"<uninit>\"";
-  PP.setFileChangeHandler(HandleFileChange);
-  PP.setIdentHandler(HandleIdent);
+  PP.setPPCallbacks(new PrintPPOutputPPCallbacks());
   EModePP = &PP;
   EmodeEmittedTokensOnThisLine = false;
   

Modified: cfe/cfe/trunk/Lex/Pragma.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/Pragma.cpp?rev=39203&r1=39202&r2=39203&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/Pragma.cpp (original)
+++ cfe/cfe/trunk/Lex/Pragma.cpp Wed Jul 11 11:41:08 2007
@@ -13,6 +13,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Lex/Pragma.h"
+#include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/HeaderSearch.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Basic/Diagnostic.h"
@@ -238,9 +239,10 @@
   HeaderInfo.MarkFileSystemHeader(File);
   
   // Notify the client, if desired, that we are in a new source file.
-  if (FileChangeHandler)
-    FileChangeHandler(TheLexer->getSourceLocation(TheLexer->BufferPtr),
-                      SystemHeaderPragma, DirectoryLookup::SystemHeaderDir);
+  if (Callbacks)
+    Callbacks->FileChanged(TheLexer->getSourceLocation(TheLexer->BufferPtr),
+                           PPCallbacks::SystemHeaderPragma,
+                           DirectoryLookup::SystemHeaderDir);
 }
 
 /// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.

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

==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:41:08 2007
@@ -28,6 +28,7 @@
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Lex/HeaderSearch.h"
 #include "clang/Lex/MacroInfo.h"
+#include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/Pragma.h"
 #include "clang/Lex/ScratchBuffer.h"
 #include "clang/Basic/Diagnostic.h"
@@ -47,7 +48,7 @@
                            HeaderSearch &Headers) 
   : Diags(diags), Features(opts), Target(target), FileMgr(FM), SourceMgr(SM),
     HeaderInfo(Headers), Identifiers(opts),
-    CurLexer(0), CurDirLookup(0), CurMacroExpander(0) {
+    CurLexer(0), CurDirLookup(0), CurMacroExpander(0), Callbacks(0) {
   ScratchBuf = new ScratchBuffer(SourceMgr);
       
   // Clear stats.
@@ -63,10 +64,6 @@
   DisableMacroExpansion = false;
   InMacroArgs = false;
 
-  // There is no file-change handler yet.
-  FileChangeHandler = 0;
-  IdentHandler = 0;
-  
   // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
   // This gets unpoisoned where it is allowed.
   (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
@@ -96,7 +93,8 @@
   delete ScratchBuf;
 }
 
-
+PPCallbacks::~PPCallbacks() {
+}
 
 /// Diag - Forwarding function for diagnostics.  This emits a diagnostic at
 /// the specified LexerToken's location, translating the token's start
@@ -359,7 +357,7 @@
   CurMacroExpander = 0;
   
   // Notify the client, if desired, that we are in a new source file.
-  if (FileChangeHandler && !CurLexer->Is_PragmaLexer) {
+  if (Callbacks && !CurLexer->Is_PragmaLexer) {
     DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
     
     // Get the file entry for the current file.
@@ -367,8 +365,8 @@
           SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
       FileType = HeaderInfo.getFileDirFlavor(FE);
     
-    FileChangeHandler(SourceLocation(CurLexer->getCurFileID(), 0),
-                      EnterFile, FileType);
+    Callbacks->FileChanged(SourceLocation(CurLexer->getCurFileID(), 0),
+                           PPCallbacks::EnterFile, FileType);
   }
 }
 
@@ -999,7 +997,7 @@
     RemoveTopOfLexerStack();
 
     // Notify the client, if desired, that we are in a new source file.
-    if (FileChangeHandler && !isEndOfMacro && CurLexer) {
+    if (Callbacks && !isEndOfMacro && CurLexer) {
       DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
       
       // Get the file entry for the current file.
@@ -1007,8 +1005,8 @@
             SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
         FileType = HeaderInfo.getFileDirFlavor(FE);
 
-      FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
-                        ExitFile, FileType);
+      Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
+                             PPCallbacks::ExitFile, FileType);
     }
 
     // Client should lex another token.
@@ -1467,8 +1465,8 @@
   // Verify that there is nothing after the string, other than EOM.
   CheckEndOfDirective("#ident");
 
-  if (IdentHandler)
-    IdentHandler(Tok.getLocation(), getSpelling(StrTok));
+  if (Callbacks)
+    Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
 }
 
 //===----------------------------------------------------------------------===//

Modified: cfe/cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=39203&r1=39202&r2=39203&view=diff

==============================================================================
--- cfe/cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/cfe/trunk/clang.xcodeproj/project.pbxproj Wed Jul 11 11:41:08 2007
@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		DE01DA490B12ADA300AC22CE /* PPCallbacks.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE01DA480B12ADA300AC22CE /* PPCallbacks.h */; };
 		DE06B73E0A8307640050E87E /* LangOptions.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE06B73D0A8307640050E87E /* LangOptions.h */; };
 		DE06BECB0A854E4B0050E87E /* Scope.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE06BECA0A854E4B0050E87E /* Scope.h */; };
 		DE06D4310A8BB52D0050E87E /* Parser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE06D42F0A8BB52D0050E87E /* Parser.cpp */; };
@@ -132,6 +133,7 @@
 				DE34646E0B043E5B00DBC861 /* Sema.h in CopyFiles */,
 				DE75ED290B044DC90020CF81 /* ASTContext.h in CopyFiles */,
 				DE1733700B068DC60080B521 /* DeclSpec.h in CopyFiles */,
+				DE01DA490B12ADA300AC22CE /* PPCallbacks.h in CopyFiles */,
 			);
 			runOnlyForDeploymentPostprocessing = 1;
 		};
@@ -139,6 +141,7 @@
 
 /* Begin PBXFileReference section */
 		8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
+		DE01DA480B12ADA300AC22CE /* PPCallbacks.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = PPCallbacks.h; sourceTree = "<group>"; };
 		DE06B73D0A8307640050E87E /* LangOptions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = LangOptions.h; sourceTree = "<group>"; };
 		DE06BECA0A854E4B0050E87E /* Scope.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = Scope.h; path = clang/Parse/Scope.h; sourceTree = "<group>"; };
 		DE06D42F0A8BB52D0050E87E /* Parser.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = Parser.cpp; path = Parse/Parser.cpp; sourceTree = "<group>"; };
@@ -385,6 +388,7 @@
 				DED7D73D0A524295003AD0FB /* MacroExpander.h */,
 				DED7D73E0A524295003AD0FB /* MacroInfo.h */,
 				DEAEE98A0A5A2B970045101B /* MultipleIncludeOpt.h */,
+				DE01DA480B12ADA300AC22CE /* PPCallbacks.h */,
 				DED7D73F0A524295003AD0FB /* Pragma.h */,
 				DED7D7400A524295003AD0FB /* Preprocessor.h */,
 				DED7D9170A52518C003AD0FB /* ScratchBuffer.h */,

Added: cfe/cfe/trunk/include/clang/Lex/PPCallbacks.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Lex/PPCallbacks.h?rev=39203&view=auto

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/PPCallbacks.h (added)
+++ cfe/cfe/trunk/include/clang/Lex/PPCallbacks.h Wed Jul 11 11:41:08 2007
@@ -0,0 +1,55 @@
+//===--- PPCallbacks.h - Callbacks for Preprocessor actions -----*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by Chris Lattner and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines the PPCallbacks interface.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_LEX_PPCALLBACKS_H
+#define LLVM_CLANG_LEX_PPCALLBACKS_H
+
+#include "clang/Lex/DirectoryLookup.h"
+#include "clang/Basic/SourceLocation.h"
+#include <string>
+
+namespace llvm {
+namespace clang {
+  class SourceLocation;
+    
+/// PPCallbacks - This interface provides a way to observe the actions of the
+/// preprocessor as it does its thing.  Clients can define their hooks here to
+/// implement preprocessor level tools.
+class PPCallbacks {
+public:
+  virtual ~PPCallbacks();
+  
+  enum FileChangeReason {
+    EnterFile, ExitFile, SystemHeaderPragma, RenameFile
+  };
+  
+  /// FileChanged - This callback is invoked whenever a source file is
+  /// entered or exited.  The SourceLocation indicates the new location, and
+  /// EnteringFile indicates whether this is because we are entering a new
+  /// #include'd file (when true) or whether we're exiting one because we ran
+  /// off the end (when false).
+  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
+                           DirectoryLookup::DirType FileType) {
+  }
+  
+  /// Ident - This callback is invoked when a #ident or #sccs directive is read.
+  ///
+  virtual void Ident(SourceLocation Loc, const std::string &str) {
+  }
+  
+};
+
+}  // end namespace clang
+}  // end namespace llvm
+
+#endif

Propchange: cfe/cfe/trunk/include/clang/Lex/PPCallbacks.h

------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/cfe/trunk/include/clang/Lex/PPCallbacks.h

------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

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=39203&r1=39202&r2=39203&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/Preprocessor.h Wed Jul 11 11:41:08 2007
@@ -14,7 +14,6 @@
 #ifndef LLVM_CLANG_LEX_PREPROCESSOR_H
 #define LLVM_CLANG_LEX_PREPROCESSOR_H
 
-#include "clang/Lex/DirectoryLookup.h"
 #include "clang/Lex/IdentifierTable.h"
 #include "clang/Lex/Lexer.h"
 #include "clang/Lex/MacroExpander.h"
@@ -31,6 +30,8 @@
 class PragmaHandler;
 class ScratchBuffer;
 class TargetInfo;
+class PPCallbacks;
+class DirectoryLookup;
 
 /// Preprocessor - This object forms engages in a tight little dance to
 /// efficiently preprocess tokens.  Lexers know only about tokens within a
@@ -55,35 +56,15 @@
   IdentifierInfo *Ident_Pragma, *Ident__VA_ARGS__; // _Pragma, __VA_ARGS__
   
   SourceLocation DATELoc, TIMELoc;
-public:
-  enum FileChangeReason {
-    EnterFile, ExitFile, SystemHeaderPragma, RenameFile
-  };
-  typedef void (*FileChangeHandler_t)(SourceLocation Loc, 
-                                      FileChangeReason Reason,
-                                      DirectoryLookup::DirType FileType);
-  typedef void (*IdentHandler_t)(SourceLocation Loc, const std::string &str);
-  
-private:
-  /// FileChangeHandler - This callback is invoked whenever a source file is
-  /// entered or exited.  The SourceLocation indicates the new location, and
-  /// EnteringFile indicates whether this is because we are entering a new
-  /// #include'd file (when true) or whether we're exiting one because we ran
-  /// off the end (when false).
-  FileChangeHandler_t FileChangeHandler;
-  
-  /// IdentHandler - This is called whenever a #ident or #sccs directive is
-  /// found.
-  IdentHandler_t IdentHandler;
-  
+
   enum {
     /// MaxIncludeStackDepth - Maximum depth of #includes.
     MaxAllowedIncludeStackDepth = 200
   };
 
   // State that changes while the preprocessor runs:
-  bool DisableMacroExpansion;    // True if macro expansion is disabled.
-  bool InMacroArgs;              // True if parsing fn macro invocation args.
+  bool DisableMacroExpansion : 1;  // True if macro expansion is disabled.
+  bool InMacroArgs : 1;            // True if parsing fn macro invocation args.
 
   /// Identifiers - This is mapping/lookup information for all identifiers in
   /// the program, including program keywords.
@@ -119,6 +100,9 @@
   };
   std::vector<IncludeStackInfo> IncludeMacroStack;
   
+  /// Callbacks - These are actions invoked when some preprocessor activity is
+  /// encountered (e.g. a file is #included, etc).
+  PPCallbacks *Callbacks;
   
   // Various statistics we track for performance analysis.
   unsigned NumDirectives, NumIncluded, NumDefined, NumUndefined, NumPragma;
@@ -156,20 +140,11 @@
   /// expansions going on at the time.
   Lexer *getCurrentFileLexer() const;
   
-  
-  /// setFileChangeHandler - Set the callback invoked whenever a source file is
-  /// entered or exited.  The SourceLocation indicates the new location, and
-  /// EnteringFile indicates whether this is because we are entering a new
-  /// #include'd file (when true) or whether we're exiting one because we ran
-  /// off the end (when false).
-  void setFileChangeHandler(FileChangeHandler_t Handler) {
-    FileChangeHandler = Handler;
-  }
-
-  /// setIdentHandler - Set the callback invoked whenever a #ident/#sccs
-  /// directive is found.
-  void setIdentHandler(IdentHandler_t Handler) {
-    IdentHandler = Handler;
+  /// getPPCallbacks/SetPPCallbacks - Accessors for preprocessor callbacks.
+  ///
+  PPCallbacks *getPPCallbacks() const { return Callbacks; }
+  void setPPCallbacks(PPCallbacks *C) {
+    Callbacks = C;
   }
   
   /// getIdentifierInfo - Return information about the specified preprocessor





More information about the cfe-commits mailing list