Jeffrey Yasskin | 7a17889 | 2011-02-03 04:51:52 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 1050e8b | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 10 | #include "clang/Frontend/FrontendAction.h" |
Jeffrey Yasskin | 7a17889 | 2011-02-03 04:51:52 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 1050e8b | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
Chandler Carruth | 7cc315c | 2012-12-04 09:53:37 +0000 | [diff] [blame] | 13 | #include "clang/AST/RecursiveASTVisitor.h" |
Jeffrey Yasskin | 7a17889 | 2011-02-03 04:51:52 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CompilerInstance.h" |
| 15 | #include "clang/Frontend/CompilerInvocation.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 16 | #include "clang/Lex/Preprocessor.h" |
Jeffrey Yasskin | 7a17889 | 2011-02-03 04:51:52 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Triple.h" |
Jeffrey Yasskin | 7a17889 | 2011-02-03 04:51:52 +0000 | [diff] [blame] | 18 | #include "llvm/Support/MemoryBuffer.h" |
Jeffrey Yasskin | 7a17889 | 2011-02-03 04:51:52 +0000 | [diff] [blame] | 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | class TestASTFrontendAction : public ASTFrontendAction { |
| 27 | public: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 28 | TestASTFrontendAction(bool enableIncrementalProcessing = false) |
| 29 | : EnableIncrementalProcessing(enableIncrementalProcessing) { } |
| 30 | |
| 31 | bool EnableIncrementalProcessing; |
Jeffrey Yasskin | 7a17889 | 2011-02-03 04:51:52 +0000 | [diff] [blame] | 32 | std::vector<std::string> decl_names; |
| 33 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 34 | virtual bool BeginSourceFileAction(CompilerInstance &ci, StringRef filename) { |
| 35 | if (EnableIncrementalProcessing) |
| 36 | ci.getPreprocessor().enableIncrementalProcessing(); |
| 37 | |
| 38 | return ASTFrontendAction::BeginSourceFileAction(ci, filename); |
| 39 | } |
| 40 | |
Jeffrey Yasskin | 7a17889 | 2011-02-03 04:51:52 +0000 | [diff] [blame] | 41 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 42 | StringRef InFile) { |
| 43 | return new Visitor(decl_names); |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | class Visitor : public ASTConsumer, public RecursiveASTVisitor<Visitor> { |
| 48 | public: |
| 49 | Visitor(std::vector<std::string> &decl_names) : decl_names_(decl_names) {} |
| 50 | |
| 51 | virtual void HandleTranslationUnit(ASTContext &context) { |
| 52 | TraverseDecl(context.getTranslationUnitDecl()); |
| 53 | } |
| 54 | |
| 55 | virtual bool VisitNamedDecl(NamedDecl *Decl) { |
| 56 | decl_names_.push_back(Decl->getQualifiedNameAsString()); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | std::vector<std::string> &decl_names_; |
| 62 | }; |
| 63 | }; |
| 64 | |
| 65 | TEST(ASTFrontendAction, Sanity) { |
| 66 | CompilerInvocation *invocation = new CompilerInvocation; |
| 67 | invocation->getPreprocessorOpts().addRemappedFile( |
| 68 | "test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }")); |
Douglas Gregor | eabcf7e | 2012-01-20 16:33:00 +0000 | [diff] [blame] | 69 | invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc", |
| 70 | IK_CXX)); |
Jeffrey Yasskin | 7a17889 | 2011-02-03 04:51:52 +0000 | [diff] [blame] | 71 | invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly; |
| 72 | invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; |
| 73 | CompilerInstance compiler; |
Jeffrey Yasskin | 7a17889 | 2011-02-03 04:51:52 +0000 | [diff] [blame] | 74 | compiler.setInvocation(invocation); |
Sean Silva | d47afb9 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 75 | compiler.createDiagnostics(); |
Jeffrey Yasskin | 7a17889 | 2011-02-03 04:51:52 +0000 | [diff] [blame] | 76 | |
| 77 | TestASTFrontendAction test_action; |
| 78 | ASSERT_TRUE(compiler.ExecuteAction(test_action)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 79 | ASSERT_EQ(2U, test_action.decl_names.size()); |
| 80 | EXPECT_EQ("main", test_action.decl_names[0]); |
| 81 | EXPECT_EQ("x", test_action.decl_names[1]); |
| 82 | } |
| 83 | |
| 84 | TEST(ASTFrontendAction, IncrementalParsing) { |
| 85 | CompilerInvocation *invocation = new CompilerInvocation; |
| 86 | invocation->getPreprocessorOpts().addRemappedFile( |
| 87 | "test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }")); |
| 88 | invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc", |
| 89 | IK_CXX)); |
| 90 | invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly; |
| 91 | invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; |
| 92 | CompilerInstance compiler; |
| 93 | compiler.setInvocation(invocation); |
| 94 | compiler.createDiagnostics(); |
| 95 | |
| 96 | TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true); |
| 97 | ASSERT_TRUE(compiler.ExecuteAction(test_action)); |
| 98 | ASSERT_EQ(2U, test_action.decl_names.size()); |
| 99 | EXPECT_EQ("main", test_action.decl_names[0]); |
| 100 | EXPECT_EQ("x", test_action.decl_names[1]); |
Jeffrey Yasskin | 7a17889 | 2011-02-03 04:51:52 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } // anonymous namespace |