MacroPPCallbacks.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //===--- MacroPPCallbacks.h -------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines implementation for the macro preprocessors callbacks.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_LIB_CODEGEN_MACROPPCALLBACKS_H
  13. #define LLVM_CLANG_LIB_CODEGEN_MACROPPCALLBACKS_H
  14. #include "clang/Lex/PPCallbacks.h"
  15. namespace llvm {
  16. class DIMacroFile;
  17. }
  18. namespace clang {
  19. class Preprocessor;
  20. class MacroInfo;
  21. class CodeGenerator;
  22. class MacroPPCallbacks : public PPCallbacks {
  23. /// A pointer to code generator, where debug info generator can be found.
  24. CodeGenerator *Gen;
  25. /// Preprocessor.
  26. Preprocessor &PP;
  27. /// Location of recent included file, used for line number.
  28. SourceLocation LastHashLoc;
  29. /// Counts current number of command line included files, which were entered
  30. /// and were not exited yet.
  31. int EnteredCommandLineIncludeFiles = 0;
  32. enum FileScopeStatus {
  33. NoScope = 0, // Scope is not initialized yet.
  34. InitializedScope, // Main file scope is initialized but not set yet.
  35. BuiltinScope, // <built-in> and <command line> file scopes.
  36. CommandLineIncludeScope, // Included file, from <command line> file, scope.
  37. MainFileScope // Main file scope.
  38. };
  39. FileScopeStatus Status;
  40. /// Parent contains all entered files that were not exited yet according to
  41. /// the inclusion order.
  42. llvm::SmallVector<llvm::DIMacroFile *, 4> Scopes;
  43. /// Get current DIMacroFile scope.
  44. /// \return current DIMacroFile scope or nullptr if there is no such scope.
  45. llvm::DIMacroFile *getCurrentScope();
  46. /// Get current line location or invalid location.
  47. /// \param Loc current line location.
  48. /// \return current line location \p `Loc`, or invalid location if it's in a
  49. /// skipped file scope.
  50. SourceLocation getCorrectLocation(SourceLocation Loc);
  51. /// Use the passed preprocessor to write the macro name and value from the
  52. /// given macro info and identifier info into the given \p `Name` and \p
  53. /// `Value` output streams.
  54. ///
  55. /// \param II Identifier info, used to get the Macro name.
  56. /// \param MI Macro info, used to get the Macro argumets and values.
  57. /// \param PP Preprocessor.
  58. /// \param [out] Name Place holder for returned macro name and arguments.
  59. /// \param [out] Value Place holder for returned macro value.
  60. static void writeMacroDefinition(const IdentifierInfo &II,
  61. const MacroInfo &MI, Preprocessor &PP,
  62. raw_ostream &Name, raw_ostream &Value);
  63. /// Update current file scope status to next file scope.
  64. void updateStatusToNextScope();
  65. /// Handle the case when entering a file.
  66. ///
  67. /// \param Loc Indicates the new location.
  68. void FileEntered(SourceLocation Loc);
  69. /// Handle the case when exiting a file.
  70. ///
  71. /// \param Loc Indicates the new location.
  72. void FileExited(SourceLocation Loc);
  73. public:
  74. MacroPPCallbacks(CodeGenerator *Gen, Preprocessor &PP);
  75. /// Callback invoked whenever a source file is entered or exited.
  76. ///
  77. /// \param Loc Indicates the new location.
  78. /// \param PrevFID the file that was exited if \p Reason is ExitFile.
  79. void FileChanged(SourceLocation Loc, FileChangeReason Reason,
  80. SrcMgr::CharacteristicKind FileType,
  81. FileID PrevFID = FileID()) override;
  82. /// Callback invoked whenever a directive (#xxx) is processed.
  83. void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
  84. StringRef FileName, bool IsAngled,
  85. CharSourceRange FilenameRange,
  86. OptionalFileEntryRef File, StringRef SearchPath,
  87. StringRef RelativePath, const Module *Imported,
  88. SrcMgr::CharacteristicKind FileType) override;
  89. /// Hook called whenever a macro definition is seen.
  90. void MacroDefined(const Token &MacroNameTok,
  91. const MacroDirective *MD) override;
  92. /// Hook called whenever a macro \#undef is seen.
  93. ///
  94. /// MD is released immediately following this callback.
  95. void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD,
  96. const MacroDirective *Undef) override;
  97. };
  98. } // end namespace clang
  99. #endif