PrintParserCallbacks.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===--- PrintParserActions.cpp - Implement -parse-print-callbacks mode ---===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file was developed by Chris Lattner and is distributed under
  6. // the University of Illinois Open Source License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This code simply runs the preprocessor on the input file and prints out the
  11. // result. This is the traditional behavior of the -E option.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang.h"
  15. #include "clang/Lex/IdentifierTable.h"
  16. #include "clang/Parse/Action.h"
  17. #include "clang/Parse/DeclSpec.h"
  18. #include <iostream>
  19. using namespace clang;
  20. namespace {
  21. class ParserPrintActions : public MinimalAction {
  22. /// ParseDeclarator - This callback is invoked when a declarator is parsed
  23. /// and 'Init' specifies the initializer if any. This is for things like:
  24. /// "int X = 4" or "typedef int foo".
  25. virtual DeclTy *ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
  26. DeclTy *LastInGroup) {
  27. std::cout << "ParseDeclarator ";
  28. if (IdentifierInfo *II = D.getIdentifier()) {
  29. std::cout << "'" << II->getName() << "'";
  30. } else {
  31. std::cout << "<anon>";
  32. }
  33. std::cout << "\n";
  34. // Pass up to EmptyActions so that the symbol table is maintained right.
  35. return MinimalAction::ParseDeclarator(S, D, Init, LastInGroup);
  36. }
  37. /// PopScope - This callback is called immediately before the specified scope
  38. /// is popped and deleted.
  39. virtual void PopScope(SourceLocation Loc, Scope *S) {
  40. std::cout << "PopScope\n";
  41. // Pass up to EmptyActions so that the symbol table is maintained right.
  42. MinimalAction::PopScope(Loc, S);
  43. }
  44. };
  45. }
  46. MinimalAction *clang::CreatePrintParserActionsAction() {
  47. return new ParserPrintActions();
  48. }