FrontendActionTest.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/Frontend/FrontendAction.h"
  10. #include "clang/AST/ASTConsumer.h"
  11. #include "clang/AST/ASTContext.h"
  12. #include "clang/AST/RecursiveASTVisitor.h"
  13. #include "clang/Frontend/CompilerInstance.h"
  14. #include "clang/Frontend/CompilerInvocation.h"
  15. #include "clang/Lex/Preprocessor.h"
  16. #include "llvm/ADT/Triple.h"
  17. #include "llvm/Support/MemoryBuffer.h"
  18. #include "gtest/gtest.h"
  19. using namespace llvm;
  20. using namespace clang;
  21. namespace {
  22. class TestASTFrontendAction : public ASTFrontendAction {
  23. public:
  24. TestASTFrontendAction(bool enableIncrementalProcessing = false)
  25. : EnableIncrementalProcessing(enableIncrementalProcessing) { }
  26. bool EnableIncrementalProcessing;
  27. std::vector<std::string> decl_names;
  28. virtual bool BeginSourceFileAction(CompilerInstance &ci, StringRef filename) {
  29. if (EnableIncrementalProcessing)
  30. ci.getPreprocessor().enableIncrementalProcessing();
  31. return ASTFrontendAction::BeginSourceFileAction(ci, filename);
  32. }
  33. virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
  34. StringRef InFile) {
  35. return new Visitor(decl_names);
  36. }
  37. private:
  38. class Visitor : public ASTConsumer, public RecursiveASTVisitor<Visitor> {
  39. public:
  40. Visitor(std::vector<std::string> &decl_names) : decl_names_(decl_names) {}
  41. virtual void HandleTranslationUnit(ASTContext &context) {
  42. TraverseDecl(context.getTranslationUnitDecl());
  43. }
  44. virtual bool VisitNamedDecl(NamedDecl *Decl) {
  45. decl_names_.push_back(Decl->getQualifiedNameAsString());
  46. return true;
  47. }
  48. private:
  49. std::vector<std::string> &decl_names_;
  50. };
  51. };
  52. TEST(ASTFrontendAction, Sanity) {
  53. CompilerInvocation *invocation = new CompilerInvocation;
  54. invocation->getPreprocessorOpts().addRemappedFile(
  55. "test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }"));
  56. invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc",
  57. IK_CXX));
  58. invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
  59. invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  60. CompilerInstance compiler;
  61. compiler.setInvocation(invocation);
  62. compiler.createDiagnostics();
  63. TestASTFrontendAction test_action;
  64. ASSERT_TRUE(compiler.ExecuteAction(test_action));
  65. ASSERT_EQ(2U, test_action.decl_names.size());
  66. EXPECT_EQ("main", test_action.decl_names[0]);
  67. EXPECT_EQ("x", test_action.decl_names[1]);
  68. }
  69. TEST(ASTFrontendAction, IncrementalParsing) {
  70. CompilerInvocation *invocation = new CompilerInvocation;
  71. invocation->getPreprocessorOpts().addRemappedFile(
  72. "test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }"));
  73. invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc",
  74. IK_CXX));
  75. invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
  76. invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  77. CompilerInstance compiler;
  78. compiler.setInvocation(invocation);
  79. compiler.createDiagnostics();
  80. TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true);
  81. ASSERT_TRUE(compiler.ExecuteAction(test_action));
  82. ASSERT_EQ(2U, test_action.decl_names.size());
  83. EXPECT_EQ("main", test_action.decl_names[0]);
  84. EXPECT_EQ("x", test_action.decl_names[1]);
  85. }
  86. } // anonymous namespace