MacroPPCallbacks.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/Parse/Parser.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. static bool isBuiltinFile(SourceManager &SM, SourceLocation Loc) {
  77. StringRef Filename(SM.getPresumedLoc(Loc).getFilename());
  78. return Filename.equals("<built-in>");
  79. }
  80. static bool isCommandLineFile(SourceManager &SM, SourceLocation Loc) {
  81. StringRef Filename(SM.getPresumedLoc(Loc).getFilename());
  82. return Filename.equals("<command line>");
  83. }
  84. void MacroPPCallbacks::updateStatusToNextScope() {
  85. switch (Status) {
  86. case NoScope:
  87. Status = InitializedScope;
  88. break;
  89. case InitializedScope:
  90. Status = BuiltinScope;
  91. break;
  92. case BuiltinScope:
  93. Status = CommandLineIncludeScope;
  94. break;
  95. case CommandLineIncludeScope:
  96. Status = MainFileScope;
  97. break;
  98. case MainFileScope:
  99. llvm_unreachable("There is no next scope, already in the final scope");
  100. }
  101. }
  102. void MacroPPCallbacks::FileEntered(SourceLocation Loc) {
  103. SourceLocation LineLoc = getCorrectLocation(LastHashLoc);
  104. switch (Status) {
  105. case NoScope:
  106. updateStatusToNextScope();
  107. break;
  108. case InitializedScope:
  109. updateStatusToNextScope();
  110. return;
  111. case BuiltinScope:
  112. if (isCommandLineFile(PP.getSourceManager(), Loc))
  113. return;
  114. updateStatusToNextScope();
  115. LLVM_FALLTHROUGH;
  116. case CommandLineIncludeScope:
  117. EnteredCommandLineIncludeFiles++;
  118. break;
  119. case MainFileScope:
  120. break;
  121. }
  122. Scopes.push_back(Gen->getCGDebugInfo()->CreateTempMacroFile(getCurrentScope(),
  123. LineLoc, Loc));
  124. }
  125. void MacroPPCallbacks::FileExited(SourceLocation Loc) {
  126. switch (Status) {
  127. default:
  128. llvm_unreachable("Do not expect to exit a file from current scope");
  129. case BuiltinScope:
  130. if (!isBuiltinFile(PP.getSourceManager(), Loc))
  131. // Skip next scope and change status to MainFileScope.
  132. Status = MainFileScope;
  133. return;
  134. case CommandLineIncludeScope:
  135. if (!EnteredCommandLineIncludeFiles) {
  136. updateStatusToNextScope();
  137. return;
  138. }
  139. EnteredCommandLineIncludeFiles--;
  140. break;
  141. case MainFileScope:
  142. break;
  143. }
  144. Scopes.pop_back();
  145. }
  146. void MacroPPCallbacks::FileChanged(SourceLocation Loc, FileChangeReason Reason,
  147. SrcMgr::CharacteristicKind FileType,
  148. FileID PrevFID) {
  149. // Only care about enter file or exit file changes.
  150. if (Reason == EnterFile)
  151. FileEntered(Loc);
  152. else if (Reason == ExitFile)
  153. FileExited(Loc);
  154. }
  155. void MacroPPCallbacks::InclusionDirective(
  156. SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
  157. bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
  158. StringRef SearchPath, StringRef RelativePath, const Module *Imported,
  159. SrcMgr::CharacteristicKind FileType) {
  160. // Record the line location of the current included file.
  161. LastHashLoc = HashLoc;
  162. }
  163. void MacroPPCallbacks::MacroDefined(const Token &MacroNameTok,
  164. const MacroDirective *MD) {
  165. IdentifierInfo *Id = MacroNameTok.getIdentifierInfo();
  166. SourceLocation location = getCorrectLocation(MacroNameTok.getLocation());
  167. std::string NameBuffer, ValueBuffer;
  168. llvm::raw_string_ostream Name(NameBuffer);
  169. llvm::raw_string_ostream Value(ValueBuffer);
  170. writeMacroDefinition(*Id, *MD->getMacroInfo(), PP, Name, Value);
  171. Gen->getCGDebugInfo()->CreateMacro(getCurrentScope(),
  172. llvm::dwarf::DW_MACINFO_define, location,
  173. Name.str(), Value.str());
  174. }
  175. void MacroPPCallbacks::MacroUndefined(const Token &MacroNameTok,
  176. const MacroDefinition &MD,
  177. const MacroDirective *Undef) {
  178. IdentifierInfo *Id = MacroNameTok.getIdentifierInfo();
  179. SourceLocation location = getCorrectLocation(MacroNameTok.getLocation());
  180. Gen->getCGDebugInfo()->CreateMacro(getCurrentScope(),
  181. llvm::dwarf::DW_MACINFO_undef, location,
  182. Id->getName(), "");
  183. }