MacroPPCallbacks.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //===--- MacroPPCallbacks.cpp ---------------------------------------------===//
  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 contains implementation for the macro preprocessors callbacks.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "MacroPPCallbacks.h"
  13. #include "CGDebugInfo.h"
  14. #include "clang/CodeGen/ModuleBuilder.h"
  15. #include "clang/Lex/MacroInfo.h"
  16. #include "clang/Lex/Preprocessor.h"
  17. using namespace clang;
  18. void MacroPPCallbacks::writeMacroDefinition(const IdentifierInfo &II,
  19. const MacroInfo &MI,
  20. Preprocessor &PP, raw_ostream &Name,
  21. raw_ostream &Value) {
  22. Name << II.getName();
  23. if (MI.isFunctionLike()) {
  24. Name << '(';
  25. if (!MI.param_empty()) {
  26. MacroInfo::param_iterator AI = MI.param_begin(), E = MI.param_end();
  27. for (; AI + 1 != E; ++AI) {
  28. Name << (*AI)->getName();
  29. Name << ',';
  30. }
  31. // Last argument.
  32. if ((*AI)->getName() == "__VA_ARGS__")
  33. Name << "...";
  34. else
  35. Name << (*AI)->getName();
  36. }
  37. if (MI.isGNUVarargs())
  38. // #define foo(x...)
  39. Name << "...";
  40. Name << ')';
  41. }
  42. SmallString<128> SpellingBuffer;
  43. bool First = true;
  44. for (const auto &T : MI.tokens()) {
  45. if (!First && T.hasLeadingSpace())
  46. Value << ' ';
  47. Value << PP.getSpelling(T, SpellingBuffer);
  48. First = false;
  49. }
  50. }
  51. MacroPPCallbacks::MacroPPCallbacks(CodeGenerator *Gen, Preprocessor &PP)
  52. : Gen(Gen), PP(PP), Status(NoScope) {}
  53. // This is the expected flow of enter/exit compiler and user files:
  54. // - Main File Enter
  55. // - <built-in> file enter
  56. // {Compiler macro definitions} - (Line=0, no scope)
  57. // - (Optional) <command line> file enter
  58. // {Command line macro definitions} - (Line=0, no scope)
  59. // - (Optional) <command line> file exit
  60. // {Command line file includes} - (Line=0, Main file scope)
  61. // {macro definitions and file includes} - (Line!=0, Parent scope)
  62. // - <built-in> file exit
  63. // {User code macro definitions and file includes} - (Line!=0, Parent scope)
  64. llvm::DIMacroFile *MacroPPCallbacks::getCurrentScope() {
  65. if (Status == MainFileScope || Status == CommandLineIncludeScope)
  66. return Scopes.back();
  67. return nullptr;
  68. }
  69. SourceLocation MacroPPCallbacks::getCorrectLocation(SourceLocation Loc) {
  70. if (Status == MainFileScope || EnteredCommandLineIncludeFiles)
  71. return Loc;
  72. // While parsing skipped files, location of macros is invalid.
  73. // Invalid location represents line zero.
  74. return SourceLocation();
  75. }
  76. void MacroPPCallbacks::updateStatusToNextScope() {
  77. switch (Status) {
  78. case NoScope:
  79. Status = InitializedScope;
  80. break;
  81. case InitializedScope:
  82. Status = BuiltinScope;
  83. break;
  84. case BuiltinScope:
  85. Status = CommandLineIncludeScope;
  86. break;
  87. case CommandLineIncludeScope:
  88. Status = MainFileScope;
  89. break;
  90. case MainFileScope:
  91. llvm_unreachable("There is no next scope, already in the final scope");
  92. }
  93. }
  94. void MacroPPCallbacks::FileEntered(SourceLocation Loc) {
  95. SourceLocation LineLoc = getCorrectLocation(LastHashLoc);
  96. switch (Status) {
  97. case NoScope:
  98. updateStatusToNextScope();
  99. break;
  100. case InitializedScope:
  101. updateStatusToNextScope();
  102. return;
  103. case BuiltinScope:
  104. if (PP.getSourceManager().isWrittenInCommandLineFile(Loc))
  105. return;
  106. updateStatusToNextScope();
  107. LLVM_FALLTHROUGH;
  108. case CommandLineIncludeScope:
  109. EnteredCommandLineIncludeFiles++;
  110. break;
  111. case MainFileScope:
  112. break;
  113. }
  114. Scopes.push_back(Gen->getCGDebugInfo()->CreateTempMacroFile(getCurrentScope(),
  115. LineLoc, Loc));
  116. }
  117. void MacroPPCallbacks::FileExited(SourceLocation Loc) {
  118. switch (Status) {
  119. default:
  120. llvm_unreachable("Do not expect to exit a file from current scope");
  121. case BuiltinScope:
  122. if (!PP.getSourceManager().isWrittenInBuiltinFile(Loc))
  123. // Skip next scope and change status to MainFileScope.
  124. Status = MainFileScope;
  125. return;
  126. case CommandLineIncludeScope:
  127. if (!EnteredCommandLineIncludeFiles) {
  128. updateStatusToNextScope();
  129. return;
  130. }
  131. EnteredCommandLineIncludeFiles--;
  132. break;
  133. case MainFileScope:
  134. break;
  135. }
  136. Scopes.pop_back();
  137. }
  138. void MacroPPCallbacks::FileChanged(SourceLocation Loc, FileChangeReason Reason,
  139. SrcMgr::CharacteristicKind FileType,
  140. FileID PrevFID) {
  141. // Only care about enter file or exit file changes.
  142. if (Reason == EnterFile)
  143. FileEntered(Loc);
  144. else if (Reason == ExitFile)
  145. FileExited(Loc);
  146. }
  147. void MacroPPCallbacks::InclusionDirective(
  148. SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
  149. bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
  150. StringRef SearchPath, StringRef RelativePath, const Module *Imported,
  151. SrcMgr::CharacteristicKind FileType) {
  152. // Record the line location of the current included file.
  153. LastHashLoc = HashLoc;
  154. }
  155. void MacroPPCallbacks::MacroDefined(const Token &MacroNameTok,
  156. const MacroDirective *MD) {
  157. IdentifierInfo *Id = MacroNameTok.getIdentifierInfo();
  158. SourceLocation location = getCorrectLocation(MacroNameTok.getLocation());
  159. std::string NameBuffer, ValueBuffer;
  160. llvm::raw_string_ostream Name(NameBuffer);
  161. llvm::raw_string_ostream Value(ValueBuffer);
  162. writeMacroDefinition(*Id, *MD->getMacroInfo(), PP, Name, Value);
  163. Gen->getCGDebugInfo()->CreateMacro(getCurrentScope(),
  164. llvm::dwarf::DW_MACINFO_define, location,
  165. Name.str(), Value.str());
  166. }
  167. void MacroPPCallbacks::MacroUndefined(const Token &MacroNameTok,
  168. const MacroDefinition &MD,
  169. const MacroDirective *Undef) {
  170. IdentifierInfo *Id = MacroNameTok.getIdentifierInfo();
  171. SourceLocation location = getCorrectLocation(MacroNameTok.getLocation());
  172. Gen->getCGDebugInfo()->CreateMacro(getCurrentScope(),
  173. llvm::dwarf::DW_MACINFO_undef, location,
  174. Id->getName(), "");
  175. }