1 | //===- unittest/AST/ExternalASTSourceTest.cpp -----------------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file contains tests for Clang's ExternalASTSource. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "clang/AST/ExternalASTSource.h" |
14 | #include "clang/AST/ASTConsumer.h" |
15 | #include "clang/AST/ASTContext.h" |
16 | #include "clang/Frontend/CompilerInstance.h" |
17 | #include "clang/Frontend/CompilerInvocation.h" |
18 | #include "clang/Frontend/FrontendActions.h" |
19 | #include "clang/Lex/PreprocessorOptions.h" |
20 | #include "llvm/Support/VirtualFileSystem.h" |
21 | #include "gtest/gtest.h" |
22 | |
23 | using namespace clang; |
24 | using namespace llvm; |
25 | |
26 | |
27 | class TestFrontendAction : public ASTFrontendAction { |
28 | public: |
29 | TestFrontendAction(ExternalASTSource *Source) : Source(Source) {} |
30 | |
31 | private: |
32 | void ExecuteAction() override { |
33 | getCompilerInstance().getASTContext().setExternalSource(Source); |
34 | getCompilerInstance().getASTContext().getTranslationUnitDecl() |
35 | ->setHasExternalVisibleStorage(); |
36 | return ASTFrontendAction::ExecuteAction(); |
37 | } |
38 | |
39 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
40 | StringRef InFile) override { |
41 | return std::make_unique<ASTConsumer>(); |
42 | } |
43 | |
44 | IntrusiveRefCntPtr<ExternalASTSource> Source; |
45 | }; |
46 | |
47 | bool testExternalASTSource(ExternalASTSource *Source, StringRef FileContents) { |
48 | |
49 | auto Invocation = std::make_shared<CompilerInvocation>(); |
50 | Invocation->getPreprocessorOpts().addRemappedFile( |
51 | From: "test.cc" , To: MemoryBuffer::getMemBuffer(InputData: FileContents).release()); |
52 | const char *Args[] = { "test.cc" }; |
53 | |
54 | DiagnosticOptions InvocationDiagOpts; |
55 | auto InvocationDiags = CompilerInstance::createDiagnostics( |
56 | VFS&: *llvm::vfs::getRealFileSystem(), Opts&: InvocationDiagOpts); |
57 | CompilerInvocation::CreateFromArgs(Res&: *Invocation, CommandLineArgs: Args, Diags&: *InvocationDiags); |
58 | |
59 | CompilerInstance Compiler(std::move(Invocation)); |
60 | Compiler.createDiagnostics(VFS&: *llvm::vfs::getRealFileSystem()); |
61 | |
62 | TestFrontendAction Action(Source); |
63 | return Compiler.ExecuteAction(Act&: Action); |
64 | } |
65 | |
66 | // Ensure that a failed name lookup into an external source only occurs once. |
67 | TEST(ExternalASTSourceTest, FailedLookupOccursOnce) { |
68 | struct TestSource : ExternalASTSource { |
69 | TestSource(unsigned &Calls) : Calls(Calls) {} |
70 | |
71 | bool |
72 | FindExternalVisibleDeclsByName(const DeclContext *, DeclarationName Name, |
73 | const DeclContext *OriginalDC) override { |
74 | if (Name.getAsString() == "j" ) |
75 | ++Calls; |
76 | return false; |
77 | } |
78 | |
79 | unsigned &Calls; |
80 | }; |
81 | |
82 | unsigned Calls = 0; |
83 | ASSERT_TRUE(testExternalASTSource(new TestSource(Calls), "int j, k = j;" )); |
84 | EXPECT_EQ(1u, Calls); |
85 | } |
86 | |