Richard Smith | 023d2ca | 2014-03-11 01:18:47 +0000 | [diff] [blame] | 1 | //===- unittest/AST/ExternalASTSourceTest.cpp -----------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Richard Smith | 023d2ca | 2014-03-11 01:18:47 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains tests for Clang's ExternalASTSource. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/AST/ASTConsumer.h" |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/ExternalASTSource.h" |
| 16 | #include "clang/Frontend/CompilerInstance.h" |
| 17 | #include "clang/Frontend/CompilerInvocation.h" |
| 18 | #include "clang/Frontend/FrontendActions.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 19 | #include "clang/Lex/PreprocessorOptions.h" |
Richard Smith | 023d2ca | 2014-03-11 01:18:47 +0000 | [diff] [blame] | 20 | #include "gtest/gtest.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | using namespace llvm; |
| 24 | |
| 25 | |
| 26 | class TestFrontendAction : public ASTFrontendAction { |
| 27 | public: |
| 28 | TestFrontendAction(ExternalASTSource *Source) : Source(Source) {} |
| 29 | |
| 30 | private: |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 31 | void ExecuteAction() override { |
Richard Smith | 023d2ca | 2014-03-11 01:18:47 +0000 | [diff] [blame] | 32 | getCompilerInstance().getASTContext().setExternalSource(Source); |
| 33 | getCompilerInstance().getASTContext().getTranslationUnitDecl() |
| 34 | ->setHasExternalVisibleStorage(); |
| 35 | return ASTFrontendAction::ExecuteAction(); |
| 36 | } |
| 37 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 38 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 39 | StringRef InFile) override { |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 40 | return std::make_unique<ASTConsumer>(); |
Richard Smith | 023d2ca | 2014-03-11 01:18:47 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | IntrusiveRefCntPtr<ExternalASTSource> Source; |
| 44 | }; |
| 45 | |
| 46 | bool testExternalASTSource(ExternalASTSource *Source, |
| 47 | StringRef FileContents) { |
| 48 | CompilerInstance Compiler; |
| 49 | Compiler.createDiagnostics(); |
| 50 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 51 | auto Invocation = std::make_shared<CompilerInvocation>(); |
Richard Smith | 023d2ca | 2014-03-11 01:18:47 +0000 | [diff] [blame] | 52 | Invocation->getPreprocessorOpts().addRemappedFile( |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 53 | "test.cc", MemoryBuffer::getMemBuffer(FileContents).release()); |
Richard Smith | 023d2ca | 2014-03-11 01:18:47 +0000 | [diff] [blame] | 54 | const char *Args[] = { "test.cc" }; |
| 55 | CompilerInvocation::CreateFromArgs(*Invocation, Args, |
Richard Smith | 023d2ca | 2014-03-11 01:18:47 +0000 | [diff] [blame] | 56 | Compiler.getDiagnostics()); |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 57 | Compiler.setInvocation(std::move(Invocation)); |
Richard Smith | 023d2ca | 2014-03-11 01:18:47 +0000 | [diff] [blame] | 58 | |
| 59 | TestFrontendAction Action(Source); |
| 60 | return Compiler.ExecuteAction(Action); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | // Ensure that a failed name lookup into an external source only occurs once. |
| 65 | TEST(ExternalASTSourceTest, FailedLookupOccursOnce) { |
| 66 | struct TestSource : ExternalASTSource { |
| 67 | TestSource(unsigned &Calls) : Calls(Calls) {} |
| 68 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 69 | bool FindExternalVisibleDeclsByName(const DeclContext *, |
| 70 | DeclarationName Name) override { |
Richard Smith | 023d2ca | 2014-03-11 01:18:47 +0000 | [diff] [blame] | 71 | if (Name.getAsString() == "j") |
| 72 | ++Calls; |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | unsigned &Calls; |
| 77 | }; |
| 78 | |
David Blaikie | 0e18ed2 | 2014-03-11 06:49:26 +0000 | [diff] [blame] | 79 | unsigned Calls = 0; |
Richard Smith | 023d2ca | 2014-03-11 01:18:47 +0000 | [diff] [blame] | 80 | ASSERT_TRUE(testExternalASTSource(new TestSource(Calls), "int j, k = j;")); |
| 81 | EXPECT_EQ(1u, Calls); |
| 82 | } |