MacroPPCallbacks.h revision 321369
151786Sdcs//===--- MacroPPCallbacks.h -------------------------------------*- C++ -*-===//
240843Smsmith//
340843Smsmith//                     The LLVM Compiler Infrastructure
440843Smsmith//
540843Smsmith// This file is distributed under the University of Illinois Open Source
694290Sdcs// License. See LICENSE.TXT for details.
740843Smsmith//
840843Smsmith//===----------------------------------------------------------------------===//
940843Smsmith//
1040843Smsmith//  This file defines implementation for the macro preprocessors callbacks.
1140843Smsmith//
1240843Smsmith//===----------------------------------------------------------------------===//
1340843Smsmith
1440843Smsmith#include "clang/Lex/PPCallbacks.h"
1576116Sdcs
1676116Sdcsnamespace llvm {
1776116Sdcsclass DIMacroFile;
1876116Sdcsclass DIMacroNode;
1976116Sdcs}
2076116Sdcsnamespace clang {
2194290Sdcsclass Preprocessor;
2294290Sdcsclass MacroInfo;
2394290Sdcsclass CodeGenerator;
2494290Sdcs
2594290Sdcsclass MacroPPCallbacks : public PPCallbacks {
2676116Sdcs  /// A pointer to code generator, where debug info generator can be found.
2776116Sdcs  CodeGenerator *Gen;
2876116Sdcs
2976116Sdcs  /// Preprocessor.
3076116Sdcs  Preprocessor &PP;
3176116Sdcs
3276116Sdcs  /// Location of recent included file, used for line number.
3376116Sdcs  SourceLocation LastHashLoc;
3476116Sdcs
3576116Sdcs  /// Counts current number of command line included files, which were entered
3676116Sdcs  /// and were not exited yet.
3776116Sdcs  int EnteredCommandLineIncludeFiles = 0;
3876116Sdcs
3976116Sdcs  enum FileScopeStatus {
4076116Sdcs    NoScope = 0,              // Scope is not initialized yet.
4176116Sdcs    InitializedScope,         // Main file scope is initialized but not set yet.
4276116Sdcs    BuiltinScope,             // <built-in> and <command line> file scopes.
4376116Sdcs    CommandLineIncludeScope,  // Included file, from <command line> file, scope.
4476116Sdcs    MainFileScope             // Main file scope.
4576116Sdcs  };
4676116Sdcs  FileScopeStatus Status;
4776116Sdcs
4876116Sdcs  /// Parent contains all entered files that were not exited yet according to
4940843Smsmith  /// the inclusion order.
5051786Sdcs  llvm::SmallVector<llvm::DIMacroFile *, 4> Scopes;
5151786Sdcs
5240883Smsmith  /// Get current DIMacroFile scope.
5340883Smsmith  /// \return current DIMacroFile scope or nullptr if there is no such scope.
5440883Smsmith  llvm::DIMacroFile *getCurrentScope();
5540883Smsmith
5640883Smsmith  /// Get current line location or invalid location.
5740876Smsmith  /// \param Loc current line location.
5840883Smsmith  /// \return current line location \p `Loc`, or invalid location if it's in a
5940843Smsmith  ///         skipped file scope.
6040843Smsmith  SourceLocation getCorrectLocation(SourceLocation Loc);
6140843Smsmith
6240843Smsmith  /// Use the passed preprocessor to write the macro name and value from the
6340843Smsmith  /// given macro info and identifier info into the given \p `Name` and \p
6440843Smsmith  /// `Value` output streams.
6540843Smsmith  ///
6640843Smsmith  /// \param II Identifier info, used to get the Macro name.
6740843Smsmith  /// \param MI Macro info, used to get the Macro argumets and values.
6840843Smsmith  /// \param PP Preprocessor.
6940843Smsmith  /// \param [out] Name Place holder for returned macro name and arguments.
7040843Smsmith  /// \param [out] Value Place holder for returned macro value.
7140843Smsmith  static void writeMacroDefinition(const IdentifierInfo &II,
7240843Smsmith                                   const MacroInfo &MI, Preprocessor &PP,
7340843Smsmith                                   raw_ostream &Name, raw_ostream &Value);
7440843Smsmith
7540843Smsmith  /// Update current file scope status to next file scope.
7640843Smsmith  void updateStatusToNextScope();
7740843Smsmith
7840843Smsmith  /// Handle the case when entering a file.
7976116Sdcs  ///
8076116Sdcs  /// \param Loc Indicates the new location.
8176116Sdcs  void FileEntered(SourceLocation Loc);
8240843Smsmith
8340843Smsmith  /// Handle the case when exiting a file.
8440843Smsmith  ///
8540843Smsmith  /// \param Loc Indicates the new location.
8640843Smsmith  void FileExited(SourceLocation Loc);
8740843Smsmith
8851786Sdcspublic:
8951786Sdcs  MacroPPCallbacks(CodeGenerator *Gen, Preprocessor &PP);
9040843Smsmith
9140843Smsmith  /// Callback invoked whenever a source file is entered or exited.
9240843Smsmith  ///
9340843Smsmith  /// \param Loc Indicates the new location.
9440843Smsmith  /// \param PrevFID the file that was exited if \p Reason is ExitFile.
9540843Smsmith  void FileChanged(SourceLocation Loc, FileChangeReason Reason,
9640843Smsmith                   SrcMgr::CharacteristicKind FileType,
9740843Smsmith                   FileID PrevFID = FileID()) override;
9840843Smsmith
9940843Smsmith  /// Callback invoked whenever a directive (#xxx) is processed.
10076116Sdcs  void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
10176116Sdcs                          StringRef FileName, bool IsAngled,
10276116Sdcs                          CharSourceRange FilenameRange, const FileEntry *File,
10376116Sdcs                          StringRef SearchPath, StringRef RelativePath,
10476116Sdcs                          const Module *Imported) override;
10576116Sdcs
10640843Smsmith  /// Hook called whenever a macro definition is seen.
10740843Smsmith  void MacroDefined(const Token &MacroNameTok,
10840843Smsmith                    const MacroDirective *MD) override;
10940843Smsmith
11040843Smsmith  /// Hook called whenever a macro \#undef is seen.
11140843Smsmith  ///
11240843Smsmith  /// MD is released immediately following this callback.
11340843Smsmith  void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD,
11440843Smsmith                      const MacroDirective *Undef) override;
11576116Sdcs};
11676116Sdcs
11740843Smsmith} // end namespace clang
11840843Smsmith