test-dissect.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "dissect.h"
  2. static inline const char *show_mode(unsigned mode)
  3. {
  4. static char str[3];
  5. if (mode == -1)
  6. return "def";
  7. #define U(u_r) "-rwm"[(mode / u_r) & 3]
  8. str[0] = U(U_R_AOF);
  9. str[1] = U(U_R_VAL);
  10. str[2] = U(U_R_PTR);
  11. #undef U
  12. return str;
  13. }
  14. static void print_usage(struct position *pos, struct symbol *sym, unsigned mode)
  15. {
  16. static unsigned curr_stream = -1;
  17. static struct ident null;
  18. struct ident *ctx = &null;
  19. if (curr_stream != pos->stream) {
  20. curr_stream = pos->stream;
  21. printf("\nFILE: %s\n\n", stream_name(curr_stream));
  22. }
  23. if (dissect_ctx)
  24. ctx = dissect_ctx->ident;
  25. printf("%4d:%-3d %-16.*s %s ",
  26. pos->line, pos->pos, ctx->len, ctx->name, show_mode(mode));
  27. }
  28. static char symscope(struct symbol *sym)
  29. {
  30. if (sym_is_local(sym)) {
  31. if (!dissect_ctx)
  32. warning(sym->pos, "no context");
  33. return '.';
  34. }
  35. return ' ';
  36. }
  37. static void r_symbol(unsigned mode, struct position *pos, struct symbol *sym)
  38. {
  39. print_usage(pos, sym, mode);
  40. if (!sym->ident)
  41. sym->ident = built_in_ident("__asm__");
  42. printf("%c %c %-32.*s %s\n",
  43. symscope(sym), sym->kind, sym->ident->len, sym->ident->name,
  44. show_typename(sym->ctype.base_type));
  45. switch (sym->kind) {
  46. case 's':
  47. if (sym->type == SYM_STRUCT || sym->type == SYM_UNION)
  48. break;
  49. goto err;
  50. case 'f':
  51. if (sym->type != SYM_BAD && sym->ctype.base_type->type != SYM_FN)
  52. goto err;
  53. case 'v':
  54. if (sym->type == SYM_NODE || sym->type == SYM_BAD)
  55. break;
  56. goto err;
  57. default:
  58. goto err;
  59. }
  60. return;
  61. err:
  62. warning(*pos, "r_symbol bad sym type=%d kind=%d", sym->type, sym->kind);
  63. }
  64. static void r_member(unsigned mode, struct position *pos, struct symbol *sym, struct symbol *mem)
  65. {
  66. struct ident *ni, *si, *mi;
  67. print_usage(pos, sym, mode);
  68. ni = built_in_ident("?");
  69. si = sym->ident ?: ni;
  70. /* mem == NULL means entire struct accessed */
  71. mi = mem ? (mem->ident ?: ni) : built_in_ident("*");
  72. printf("%c m %.*s.%-*.*s %s\n",
  73. symscope(sym), si->len, si->name,
  74. 32-1 - si->len, mi->len, mi->name,
  75. show_typename(mem ? mem->ctype.base_type : sym));
  76. if (sym->ident && sym->kind != 's')
  77. warning(*pos, "r_member bad sym type=%d kind=%d", sym->type, sym->kind);
  78. if (mem && mem->kind != 'm')
  79. warning(*pos, "r_member bad mem->kind = %d", mem->kind);
  80. }
  81. static void r_symdef(struct symbol *sym)
  82. {
  83. r_symbol(-1, &sym->pos, sym);
  84. }
  85. static void r_memdef(struct symbol *sym, struct symbol *mem)
  86. {
  87. r_member(-1, &mem->pos, sym, mem);
  88. }
  89. int main(int argc, char **argv)
  90. {
  91. static struct reporter reporter = {
  92. .r_symdef = r_symdef,
  93. .r_memdef = r_memdef,
  94. .r_symbol = r_symbol,
  95. .r_member = r_member,
  96. };
  97. struct string_list *filelist = NULL;
  98. sparse_initialize(argc, argv, &filelist);
  99. dissect(&reporter, filelist);
  100. return 0;
  101. }