blob: 4f42dcf10336032f9c3246a45564441523bed29b [file] [log] [blame]
Richard Smith023d2ca2014-03-11 01:18:47 +00001//===- unittest/AST/ExternalASTSourceTest.cpp -----------------------------===//
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//
10// This file contains tests for Clang's ExternalASTSource.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTConsumer.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/ExternalASTSource.h"
17#include "clang/Frontend/CompilerInstance.h"
18#include "clang/Frontend/CompilerInvocation.h"
19#include "clang/Frontend/FrontendActions.h"
20#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 {
David Blaikie6beb6aa2014-08-10 19:56:51 +000040 return llvm::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
51 CompilerInvocation *Invocation = new CompilerInvocation;
52 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,
56 Args + array_lengthof(Args),
57 Compiler.getDiagnostics());
58 Compiler.setInvocation(Invocation);
59
60 TestFrontendAction Action(Source);
61 return Compiler.ExecuteAction(Action);
62}
63
64
65// Ensure that a failed name lookup into an external source only occurs once.
66TEST(ExternalASTSourceTest, FailedLookupOccursOnce) {
67 struct TestSource : ExternalASTSource {
68 TestSource(unsigned &Calls) : Calls(Calls) {}
69
Alexander Kornienko34eb2072015-04-11 02:00:23 +000070 bool FindExternalVisibleDeclsByName(const DeclContext *,
71 DeclarationName Name) override {
Richard Smith023d2ca2014-03-11 01:18:47 +000072 if (Name.getAsString() == "j")
73 ++Calls;
74 return false;
75 }
76
77 unsigned &Calls;
78 };
79
David Blaikie0e18ed22014-03-11 06:49:26 +000080 unsigned Calls = 0;
Richard Smith023d2ca2014-03-11 01:18:47 +000081 ASSERT_TRUE(testExternalASTSource(new TestSource(Calls), "int j, k = j;"));
82 EXPECT_EQ(1u, Calls);
83}