[cfe-dev] A patch for a new callback for the preprocessor

Paolo Bolzoni bolzoni at cs.unipr.it
Thu Mar 12 10:01:06 PDT 2009


dear cfe-dev,

while working with your preprocessor,
I found really useful to attach callbacks to events such as ChangeFile.

Now I also needed a new callback hook for the `macro expansion'
event, which seems to be missing. Hence, I am proposing the attached simple
patch.

This new feature allows me to code checkers for coding rules like
`Your program shall not use the setjmp macro'

pb


diff -u --ignore-all-space -r llvm/tools/clang/include/clang/Lex/PPCallbacks.h src/tools/clang/include/clang/Lex/PPCallbacks.h
--- llvm/tools/clang/include/clang/Lex/PPCallbacks.h. 2009-03-12 11:43:52.000000000 +0100
+++ src/tools/clang/include/clang/Lex/PPCallbacks.h.2009-03-12 16:49:21.000000000 +0100
@@ -15,6 +15,7 @@
 #define LLVM_CLANG_LEX_PPCALLBACKS_H
_
 #include "clang/Lex/DirectoryLookup.h"
+#include "clang/Lex/MacroInfo.h"
 #include "clang/Basic/SourceLocation.h"
 #include <string>
_
@@ -54,6 +55,8 @@
                              const std::string &Str) {
   }   
___
+  virtual void MacroExpands(const Token& Id, const MacroInfo* MI) {
+  }
 };
_
 }  // end namespace clang
diff -u --ignore-all-space -r llvm/tools/clang/lib/Lex/PPMacroExpansion.cpp src/tools/clang/lib/Lex/PPMacroExpansion.cpp
--- llvm/tools/clang/lib/Lex/PPMacroExpansion.cpp.2009-03-12 11:44:24.000000000 +0100
+++ src/tools/clang/lib/Lex/PPMacroExpansion.cpp. 2009-03-12 17:09:42.000000000 +0100
@@ -18,6 +18,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Lex/LexDiagnostic.h"
+#include "clang/Lex/PPCallbacks.h"
 #include <cstdio>
 #include <ctime>
 using namespace clang;
@@ -150,6 +151,8 @@
 /// expanded as a macro, handle it and return the next token as 'Identifier'.
 bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,_
                                                  MacroInfo *MI) {
+  if (Callbacks) Callbacks->MacroExpands(Identifier, MI);
+
   // If this is a macro exapnsion in the "#if !defined(x)" line for the file,
   // then the macro could expand to different things in other contexts, we need
   // to disable the optimization in this case.




More information about the cfe-dev mailing list