blob: 8b70be664e043c05a9dfd8440a9b2b5083ea2cc8 [file] [log] [blame]
Richard Smith023d2ca2014-03-11 01:18:47 +00001//===- unittest/AST/ExternalASTSourceTest.cpp -----------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Smith023d2ca2014-03-11 01:18:47 +00006//
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 Amini9670f842016-07-18 19:02:11 +000019#include "clang/Lex/PreprocessorOptions.h"
Richard Smith023d2ca2014-03-11 01:18:47 +000020#include "gtest/gtest.h"
21
22using namespace clang;
23using namespace llvm;
24
25
26class TestFrontendAction : public ASTFrontendAction {
27public:
28 TestFrontendAction(ExternalASTSource *Source) : Source(Source) {}
29
30private:
Alexander Kornienko34eb2072015-04-11 02:00:23 +000031 void ExecuteAction() override {
Richard Smith023d2ca2014-03-11 01:18:47 +000032 getCompilerInstance().getASTContext().setExternalSource(Source);
33 getCompilerInstance().getASTContext().getTranslationUnitDecl()
34 ->setHasExternalVisibleStorage();
35 return ASTFrontendAction::ExecuteAction();
36 }
37
Alexander Kornienko34eb2072015-04-11 02:00:23 +000038 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
39 StringRef InFile) override {
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +000040 return std::make_unique<ASTConsumer>();
Richard Smith023d2ca2014-03-11 01:18:47 +000041 }
42
43 IntrusiveRefCntPtr<ExternalASTSource> Source;
44};
45
46bool testExternalASTSource(ExternalASTSource *Source,
47 StringRef FileContents) {
48 CompilerInstance Compiler;
49 Compiler.createDiagnostics();
50
David Blaikieea4395e2017-01-06 19:49:01 +000051 auto Invocation = std::make_shared<CompilerInvocation>();
Richard Smith023d2ca2014-03-11 01:18:47 +000052 Invocation->getPreprocessorOpts().addRemappedFile(
Rafael Espindolad87f8d72014-08-27 20:03:29 +000053 "test.cc", MemoryBuffer::getMemBuffer(FileContents).release());
Richard Smith023d2ca2014-03-11 01:18:47 +000054 const char *Args[] = { "test.cc" };
55 CompilerInvocation::CreateFromArgs(*Invocation, Args,
Richard Smith023d2ca2014-03-11 01:18:47 +000056 Compiler.getDiagnostics());
David Blaikieea4395e2017-01-06 19:49:01 +000057 Compiler.setInvocation(std::move(Invocation));
Richard Smith023d2ca2014-03-11 01:18:47 +000058
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.
65TEST(ExternalASTSourceTest, FailedLookupOccursOnce) {
66 struct TestSource : ExternalASTSource {
67 TestSource(unsigned &Calls) : Calls(Calls) {}
68
Alexander Kornienko34eb2072015-04-11 02:00:23 +000069 bool FindExternalVisibleDeclsByName(const DeclContext *,
70 DeclarationName Name) override {
Richard Smith023d2ca2014-03-11 01:18:47 +000071 if (Name.getAsString() == "j")
72 ++Calls;
73 return false;
74 }
75
76 unsigned &Calls;
77 };
78
David Blaikie0e18ed22014-03-11 06:49:26 +000079 unsigned Calls = 0;
Richard Smith023d2ca2014-03-11 01:18:47 +000080 ASSERT_TRUE(testExternalASTSource(new TestSource(Calls), "int j, k = j;"));
81 EXPECT_EQ(1u, Calls);
82}