FrontendActionTest.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //===- unittests/Frontend/FrontendActionTest.cpp - FrontendAction tests ---===//
  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. #include "clang/AST/RecursiveASTVisitor.h"
  10. #include "clang/AST/ASTConsumer.h"
  11. #include "clang/Frontend/CompilerInstance.h"
  12. #include "clang/Frontend/CompilerInvocation.h"
  13. #include "clang/Frontend/FrontendAction.h"
  14. #include "llvm/ADT/Triple.h"
  15. #include "llvm/LLVMContext.h"
  16. #include "llvm/Support/MemoryBuffer.h"
  17. #include "gtest/gtest.h"
  18. using namespace llvm;
  19. using namespace clang;
  20. namespace {
  21. class TestASTFrontendAction : public ASTFrontendAction {
  22. public:
  23. std::vector<std::string> decl_names;
  24. virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
  25. StringRef InFile) {
  26. return new Visitor(decl_names);
  27. }
  28. private:
  29. class Visitor : public ASTConsumer, public RecursiveASTVisitor<Visitor> {
  30. public:
  31. Visitor(std::vector<std::string> &decl_names) : decl_names_(decl_names) {}
  32. virtual void HandleTranslationUnit(ASTContext &context) {
  33. TraverseDecl(context.getTranslationUnitDecl());
  34. }
  35. virtual bool VisitNamedDecl(NamedDecl *Decl) {
  36. decl_names_.push_back(Decl->getQualifiedNameAsString());
  37. return true;
  38. }
  39. private:
  40. std::vector<std::string> &decl_names_;
  41. };
  42. };
  43. TEST(ASTFrontendAction, Sanity) {
  44. CompilerInvocation *invocation = new CompilerInvocation;
  45. invocation->getPreprocessorOpts().addRemappedFile(
  46. "test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }"));
  47. invocation->getFrontendOpts().Inputs.push_back(
  48. std::make_pair(IK_CXX, "test.cc"));
  49. invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
  50. invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  51. CompilerInstance compiler;
  52. compiler.setLLVMContext(new LLVMContext);
  53. compiler.setInvocation(invocation);
  54. compiler.createDiagnostics(0, NULL);
  55. TestASTFrontendAction test_action;
  56. ASSERT_TRUE(compiler.ExecuteAction(test_action));
  57. ASSERT_EQ(3U, test_action.decl_names.size());
  58. EXPECT_EQ("__builtin_va_list", test_action.decl_names[0]);
  59. EXPECT_EQ("main", test_action.decl_names[1]);
  60. EXPECT_EQ("x", test_action.decl_names[2]);
  61. }
  62. } // anonymous namespace