blob: 4b3bb3e2b69b3869903e6d0ab4a66791e4e5acf1 [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"
Mehdi Amini9670f842016-07-18 19:02:11 +000020#include "clang/Lex/PreprocessorOptions.h"
Richard Smith023d2ca2014-03-11 01:18:47 +000021#include "gtest/gtest.h"
22
23using namespace clang;
24using namespace llvm;
25
26
27class TestFrontendAction : public ASTFrontendAction {
28public:
29 TestFrontendAction(ExternalASTSource *Source) : Source(Source) {}
30
31private:
Alexander Kornienko34eb2072015-04-11 02:00:23 +000032 void ExecuteAction() override {
Richard Smith023d2ca2014-03-11 01:18:47 +000033 getCompilerInstance().getASTContext().setExternalSource(Source);
34 getCompilerInstance().getASTContext().getTranslationUnitDecl()
35 ->setHasExternalVisibleStorage();
36 return ASTFrontendAction::ExecuteAction();
37 }
38
Alexander Kornienko34eb2072015-04-11 02:00:23 +000039 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
40 StringRef InFile) override {
David Blaikie6beb6aa2014-08-10 19:56:51 +000041 return llvm::make_unique<ASTConsumer>();
Richard Smith023d2ca2014-03-11 01:18:47 +000042 }
43
44 IntrusiveRefCntPtr<ExternalASTSource> Source;
45};
46
47bool testExternalASTSource(ExternalASTSource *Source,
48 StringRef FileContents) {
49 CompilerInstance Compiler;
50 Compiler.createDiagnostics();
51
52 CompilerInvocation *Invocation = new CompilerInvocation;
53 Invocation->getPreprocessorOpts().addRemappedFile(
Rafael Espindolad87f8d72014-08-27 20:03:29 +000054 "test.cc", MemoryBuffer::getMemBuffer(FileContents).release());
Richard Smith023d2ca2014-03-11 01:18:47 +000055 const char *Args[] = { "test.cc" };
56 CompilerInvocation::CreateFromArgs(*Invocation, Args,
57 Args + array_lengthof(Args),
58 Compiler.getDiagnostics());
59 Compiler.setInvocation(Invocation);
60
61 TestFrontendAction Action(Source);
62 return Compiler.ExecuteAction(Action);
63}
64
65
66// Ensure that a failed name lookup into an external source only occurs once.
67TEST(ExternalASTSourceTest, FailedLookupOccursOnce) {
68 struct TestSource : ExternalASTSource {
69 TestSource(unsigned &Calls) : Calls(Calls) {}
70
Alexander Kornienko34eb2072015-04-11 02:00:23 +000071 bool FindExternalVisibleDeclsByName(const DeclContext *,
72 DeclarationName Name) override {
Richard Smith023d2ca2014-03-11 01:18:47 +000073 if (Name.getAsString() == "j")
74 ++Calls;
75 return false;
76 }
77
78 unsigned &Calls;
79 };
80
David Blaikie0e18ed22014-03-11 06:49:26 +000081 unsigned Calls = 0;
Richard Smith023d2ca2014-03-11 01:18:47 +000082 ASSERT_TRUE(testExternalASTSource(new TestSource(Calls), "int j, k = j;"));
83 EXPECT_EQ(1u, Calls);
84}