FrontendActionTest.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  34. StringRef InFile) {
  35. return llvm::make_unique<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",
  56. MemoryBuffer::getMemBuffer("int main() { float x; }").release());
  57. invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc",
  58. IK_CXX));
  59. invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
  60. invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  61. CompilerInstance compiler;
  62. compiler.setInvocation(invocation);
  63. compiler.createDiagnostics();
  64. TestASTFrontendAction test_action;
  65. ASSERT_TRUE(compiler.ExecuteAction(test_action));
  66. ASSERT_EQ(2U, test_action.decl_names.size());
  67. EXPECT_EQ("main", test_action.decl_names[0]);
  68. EXPECT_EQ("x", test_action.decl_names[1]);
  69. }
  70. TEST(ASTFrontendAction, IncrementalParsing) {
  71. CompilerInvocation *invocation = new CompilerInvocation;
  72. invocation->getPreprocessorOpts().addRemappedFile(
  73. "test.cc",
  74. MemoryBuffer::getMemBuffer("int main() { float x; }").release());
  75. invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc",
  76. IK_CXX));
  77. invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
  78. invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  79. CompilerInstance compiler;
  80. compiler.setInvocation(invocation);
  81. compiler.createDiagnostics();
  82. TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true);
  83. ASSERT_TRUE(compiler.ExecuteAction(test_action));
  84. ASSERT_EQ(2U, test_action.decl_names.size());
  85. EXPECT_EQ("main", test_action.decl_names[0]);
  86. EXPECT_EQ("x", test_action.decl_names[1]);
  87. }
  88. struct TestPPCallbacks : public PPCallbacks {
  89. TestPPCallbacks() : SeenEnd(false) {}
  90. void EndOfMainFile() override { SeenEnd = true; }
  91. bool SeenEnd;
  92. };
  93. class TestPPCallbacksFrontendAction : public PreprocessorFrontendAction {
  94. TestPPCallbacks *Callbacks;
  95. public:
  96. TestPPCallbacksFrontendAction(TestPPCallbacks *C)
  97. : Callbacks(C), SeenEnd(false) {}
  98. void ExecuteAction() override {
  99. Preprocessor &PP = getCompilerInstance().getPreprocessor();
  100. PP.addPPCallbacks(Callbacks);
  101. PP.EnterMainSourceFile();
  102. }
  103. void EndSourceFileAction() override { SeenEnd = Callbacks->SeenEnd; }
  104. bool SeenEnd;
  105. };
  106. TEST(PreprocessorFrontendAction, EndSourceFile) {
  107. CompilerInvocation *Invocation = new CompilerInvocation;
  108. Invocation->getPreprocessorOpts().addRemappedFile(
  109. "test.cc",
  110. MemoryBuffer::getMemBuffer("int main() { float x; }").release());
  111. Invocation->getFrontendOpts().Inputs.push_back(
  112. FrontendInputFile("test.cc", IK_CXX));
  113. Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
  114. Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  115. CompilerInstance Compiler;
  116. Compiler.setInvocation(Invocation);
  117. Compiler.createDiagnostics();
  118. TestPPCallbacks *Callbacks = new TestPPCallbacks;
  119. TestPPCallbacksFrontendAction TestAction(Callbacks);
  120. ASSERT_FALSE(Callbacks->SeenEnd);
  121. ASSERT_FALSE(TestAction.SeenEnd);
  122. ASSERT_TRUE(Compiler.ExecuteAction(TestAction));
  123. // Check that EndOfMainFile was called before EndSourceFileAction.
  124. ASSERT_TRUE(TestAction.SeenEnd);
  125. }
  126. } // anonymous namespace