MacroPPCallbacks.cpp 6.6 KB

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