MacroPPCallbacks.cpp 6.4 KB

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