PPCallbacks.h revision 210299
1193326Sed//===--- PPCallbacks.h - Callbacks for Preprocessor actions -----*- C++ -*-===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed//  This file defines the PPCallbacks interface.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14193326Sed#ifndef LLVM_CLANG_LEX_PPCALLBACKS_H
15193326Sed#define LLVM_CLANG_LEX_PPCALLBACKS_H
16193326Sed
17193326Sed#include "clang/Lex/DirectoryLookup.h"
18193326Sed#include "clang/Basic/SourceLocation.h"
19210299Sed#include "llvm/ADT/StringRef.h"
20193326Sed#include <string>
21193326Sed
22193326Sednamespace clang {
23193326Sed  class SourceLocation;
24193326Sed  class Token;
25193326Sed  class IdentifierInfo;
26193326Sed  class MacroInfo;
27198092Srdivacky
28193326Sed/// PPCallbacks - This interface provides a way to observe the actions of the
29193326Sed/// preprocessor as it does its thing.  Clients can define their hooks here to
30193326Sed/// implement preprocessor level tools.
31193326Sedclass PPCallbacks {
32193326Sedpublic:
33193326Sed  virtual ~PPCallbacks();
34198092Srdivacky
35193326Sed  enum FileChangeReason {
36193326Sed    EnterFile, ExitFile, SystemHeaderPragma, RenameFile
37193326Sed  };
38198092Srdivacky
39193326Sed  /// FileChanged - This callback is invoked whenever a source file is
40193326Sed  /// entered or exited.  The SourceLocation indicates the new location, and
41193326Sed  /// EnteringFile indicates whether this is because we are entering a new
42193326Sed  /// #include'd file (when true) or whether we're exiting one because we ran
43193326Sed  /// off the end (when false).
44193326Sed  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
45193326Sed                           SrcMgr::CharacteristicKind FileType) {
46193326Sed  }
47198092Srdivacky
48207619Srdivacky  /// FileSkipped - This callback is invoked whenever a source file is
49207619Srdivacky  /// skipped as the result of header guard optimization.  ParentFile
50207619Srdivacky  /// is the file that #includes the skipped file.  FilenameTok is the
51207619Srdivacky  /// token in ParentFile that indicates the skipped file.
52207619Srdivacky  virtual void FileSkipped(const FileEntry &ParentFile,
53207619Srdivacky                           const Token &FilenameTok,
54207619Srdivacky                           SrcMgr::CharacteristicKind FileType) {
55207619Srdivacky  }
56206084Srdivacky
57206084Srdivacky  /// EndOfMainFile - This callback is invoked when the end of the main file is
58206084Srdivacky  /// reach, no subsequent callbacks will be made.
59206084Srdivacky  virtual void EndOfMainFile() {
60206084Srdivacky  }
61206084Srdivacky
62193326Sed  /// Ident - This callback is invoked when a #ident or #sccs directive is read.
63193326Sed  ///
64193326Sed  virtual void Ident(SourceLocation Loc, const std::string &str) {
65193326Sed  }
66198092Srdivacky
67193326Sed  /// PragmaComment - This callback is invoked when a #pragma comment directive
68193326Sed  /// is read.
69193326Sed  ///
70198092Srdivacky  virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
71193326Sed                             const std::string &Str) {
72193326Sed  }
73198092Srdivacky
74210299Sed  /// PragmaMessage - This callback is invoked when a #pragma message directive
75210299Sed  /// is read.
76210299Sed  ///
77210299Sed  virtual void PragmaMessage(SourceLocation Loc, llvm::StringRef Str) {
78210299Sed  }
79210299Sed
80193326Sed  /// MacroExpands - This is called by
81193326Sed  /// Preprocessor::HandleMacroExpandedIdentifier when a macro invocation is
82193326Sed  /// found.
83193326Sed  virtual void MacroExpands(const Token &Id, const MacroInfo* MI) {
84193326Sed  }
85198092Srdivacky
86193326Sed  /// MacroDefined - This hook is called whenever a macro definition is seen.
87193326Sed  virtual void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI) {
88193326Sed  }
89193326Sed
90193326Sed  /// MacroUndefined - This hook is called whenever a macro #undef is seen.
91193326Sed  /// MI is released immediately following this callback.
92193326Sed  virtual void MacroUndefined(const IdentifierInfo *II, const MacroInfo *MI) {
93193326Sed  }
94193326Sed};
95193326Sed
96193326Sed/// PPChainedCallbacks - Simple wrapper class for chaining callbacks.
97193326Sedclass PPChainedCallbacks : public PPCallbacks {
98193326Sed  PPCallbacks *First, *Second;
99193326Sed
100198092Srdivackypublic:
101193326Sed  PPChainedCallbacks(PPCallbacks *_First, PPCallbacks *_Second)
102193326Sed    : First(_First), Second(_Second) {}
103193326Sed  ~PPChainedCallbacks() {
104193326Sed    delete Second;
105193326Sed    delete First;
106193326Sed  }
107193326Sed
108193326Sed  virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
109193326Sed                           SrcMgr::CharacteristicKind FileType) {
110193326Sed    First->FileChanged(Loc, Reason, FileType);
111193326Sed    Second->FileChanged(Loc, Reason, FileType);
112193326Sed  }
113198092Srdivacky
114207619Srdivacky  virtual void FileSkipped(const FileEntry &ParentFile,
115207619Srdivacky                           const Token &FilenameTok,
116207619Srdivacky                           SrcMgr::CharacteristicKind FileType) {
117207619Srdivacky    First->FileSkipped(ParentFile, FilenameTok, FileType);
118207619Srdivacky    Second->FileSkipped(ParentFile, FilenameTok, FileType);
119207619Srdivacky  }
120207619Srdivacky
121206084Srdivacky  virtual void EndOfMainFile() {
122206084Srdivacky    First->EndOfMainFile();
123206084Srdivacky    Second->EndOfMainFile();
124206084Srdivacky  }
125206084Srdivacky
126193326Sed  virtual void Ident(SourceLocation Loc, const std::string &str) {
127193326Sed    First->Ident(Loc, str);
128193326Sed    Second->Ident(Loc, str);
129193326Sed  }
130198092Srdivacky
131198092Srdivacky  virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
132193326Sed                             const std::string &Str) {
133193326Sed    First->PragmaComment(Loc, Kind, Str);
134193326Sed    Second->PragmaComment(Loc, Kind, Str);
135193326Sed  }
136198092Srdivacky
137210299Sed  virtual void PragmaMessage(SourceLocation Loc, llvm::StringRef Str) {
138210299Sed    First->PragmaMessage(Loc, Str);
139210299Sed    Second->PragmaMessage(Loc, Str);
140210299Sed  }
141210299Sed
142193326Sed  virtual void MacroExpands(const Token &Id, const MacroInfo* MI) {
143193326Sed    First->MacroExpands(Id, MI);
144193326Sed    Second->MacroExpands(Id, MI);
145193326Sed  }
146198092Srdivacky
147193326Sed  virtual void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI) {
148193326Sed    First->MacroDefined(II, MI);
149193326Sed    Second->MacroDefined(II, MI);
150193326Sed  }
151193326Sed
152193326Sed  virtual void MacroUndefined(const IdentifierInfo *II, const MacroInfo *MI) {
153193326Sed    First->MacroUndefined(II, MI);
154193326Sed    Second->MacroUndefined(II, MI);
155193326Sed  }
156193326Sed};
157193326Sed
158193326Sed}  // end namespace clang
159193326Sed
160193326Sed#endif
161