PPCallbacks.h revision 1eb4433ac451dc16f4133a88af2d002ac26c58ef
1//===--- PPCallbacks.h - Callbacks for Preprocessor actions -----*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the PPCallbacks interface. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_LEX_PPCALLBACKS_H 15#define LLVM_CLANG_LEX_PPCALLBACKS_H 16 17#include "clang/Lex/DirectoryLookup.h" 18#include "clang/Basic/SourceLocation.h" 19#include <string> 20 21namespace clang { 22 class SourceLocation; 23 class Token; 24 class IdentifierInfo; 25 class MacroInfo; 26 27/// PPCallbacks - This interface provides a way to observe the actions of the 28/// preprocessor as it does its thing. Clients can define their hooks here to 29/// implement preprocessor level tools. 30class PPCallbacks { 31public: 32 virtual ~PPCallbacks(); 33 34 enum FileChangeReason { 35 EnterFile, ExitFile, SystemHeaderPragma, RenameFile 36 }; 37 38 /// FileChanged - This callback is invoked whenever a source file is 39 /// entered or exited. The SourceLocation indicates the new location, and 40 /// EnteringFile indicates whether this is because we are entering a new 41 /// #include'd file (when true) or whether we're exiting one because we ran 42 /// off the end (when false). 43 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 44 SrcMgr::CharacteristicKind FileType) { 45 } 46 47 /// Ident - This callback is invoked when a #ident or #sccs directive is read. 48 /// 49 virtual void Ident(SourceLocation Loc, const std::string &str) { 50 } 51 52 /// PragmaComment - This callback is invoked when a #pragma comment directive 53 /// is read. 54 /// 55 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, 56 const std::string &Str) { 57 } 58 59 /// MacroExpands - This is called by 60 /// Preprocessor::HandleMacroExpandedIdentifier when a macro invocation is 61 /// found. 62 virtual void MacroExpands(const Token &Id, const MacroInfo* MI) { 63 } 64 65 /// MacroDefined - This hook is called whenever a macro definition is seen. 66 virtual void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI) { 67 } 68 69 /// MacroUndefined - This hook is called whenever a macro #undef is seen. 70 /// MI is released immediately following this callback. 71 virtual void MacroUndefined(const IdentifierInfo *II, const MacroInfo *MI) { 72 } 73}; 74 75/// PPChainedCallbacks - Simple wrapper class for chaining callbacks. 76class PPChainedCallbacks : public PPCallbacks { 77 PPCallbacks *First, *Second; 78 79public: 80 PPChainedCallbacks(PPCallbacks *_First, PPCallbacks *_Second) 81 : First(_First), Second(_Second) {} 82 ~PPChainedCallbacks() { 83 delete Second; 84 delete First; 85 } 86 87 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 88 SrcMgr::CharacteristicKind FileType) { 89 First->FileChanged(Loc, Reason, FileType); 90 Second->FileChanged(Loc, Reason, FileType); 91 } 92 93 virtual void Ident(SourceLocation Loc, const std::string &str) { 94 First->Ident(Loc, str); 95 Second->Ident(Loc, str); 96 } 97 98 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, 99 const std::string &Str) { 100 First->PragmaComment(Loc, Kind, Str); 101 Second->PragmaComment(Loc, Kind, Str); 102 } 103 104 virtual void MacroExpands(const Token &Id, const MacroInfo* MI) { 105 First->MacroExpands(Id, MI); 106 Second->MacroExpands(Id, MI); 107 } 108 109 virtual void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI) { 110 First->MacroDefined(II, MI); 111 Second->MacroDefined(II, MI); 112 } 113 114 virtual void MacroUndefined(const IdentifierInfo *II, const MacroInfo *MI) { 115 First->MacroUndefined(II, MI); 116 Second->MacroUndefined(II, MI); 117 } 118}; 119 120} // end namespace clang 121 122#endif 123