PPCallbacks.h revision dbd8209b33e6c9f151e4913a9c095d64a95439c4
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 48 /// EndOfMainFile - This callback is invoked when the end of the main file is 49 /// reach, no subsequent callbacks will be made. 50 virtual void EndOfMainFile() { 51 } 52 53 /// Ident - This callback is invoked when a #ident or #sccs directive is read. 54 /// 55 virtual void Ident(SourceLocation Loc, const std::string &str) { 56 } 57 58 /// PragmaComment - This callback is invoked when a #pragma comment directive 59 /// is read. 60 /// 61 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, 62 const std::string &Str) { 63 } 64 65 /// MacroExpands - This is called by 66 /// Preprocessor::HandleMacroExpandedIdentifier when a macro invocation is 67 /// found. 68 virtual void MacroExpands(const Token &Id, const MacroInfo* MI) { 69 } 70 71 /// MacroDefined - This hook is called whenever a macro definition is seen. 72 virtual void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI) { 73 } 74 75 /// MacroUndefined - This hook is called whenever a macro #undef is seen. 76 /// MI is released immediately following this callback. 77 virtual void MacroUndefined(const IdentifierInfo *II, const MacroInfo *MI) { 78 } 79}; 80 81/// PPChainedCallbacks - Simple wrapper class for chaining callbacks. 82class PPChainedCallbacks : public PPCallbacks { 83 PPCallbacks *First, *Second; 84 85public: 86 PPChainedCallbacks(PPCallbacks *_First, PPCallbacks *_Second) 87 : First(_First), Second(_Second) {} 88 ~PPChainedCallbacks() { 89 delete Second; 90 delete First; 91 } 92 93 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 94 SrcMgr::CharacteristicKind FileType) { 95 First->FileChanged(Loc, Reason, FileType); 96 Second->FileChanged(Loc, Reason, FileType); 97 } 98 99 virtual void EndOfMainFile() { 100 First->EndOfMainFile(); 101 Second->EndOfMainFile(); 102 } 103 104 virtual void Ident(SourceLocation Loc, const std::string &str) { 105 First->Ident(Loc, str); 106 Second->Ident(Loc, str); 107 } 108 109 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, 110 const std::string &Str) { 111 First->PragmaComment(Loc, Kind, Str); 112 Second->PragmaComment(Loc, Kind, Str); 113 } 114 115 virtual void MacroExpands(const Token &Id, const MacroInfo* MI) { 116 First->MacroExpands(Id, MI); 117 Second->MacroExpands(Id, MI); 118 } 119 120 virtual void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI) { 121 First->MacroDefined(II, MI); 122 Second->MacroDefined(II, MI); 123 } 124 125 virtual void MacroUndefined(const IdentifierInfo *II, const MacroInfo *MI) { 126 First->MacroUndefined(II, MI); 127 Second->MacroUndefined(II, MI); 128 } 129}; 130 131} // end namespace clang 132 133#endif 134