[LLD][COFF] Emit tail merge pdata for delay load thunks on ARM64EC (#116810)
[llvm-project.git] / clang / unittests / Sema / SemaNoloadLookupTest.cpp
blobcf89c7331e4e0fd06c7ecaccae56fdf74294f86a
1 //== unittests/Sema/SemaNoloadLookupTest.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 //===----------------------------------------------------------------------===//
9 #include "clang/AST/DeclLookups.h"
10 #include "clang/AST/DeclarationName.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13 #include "clang/Frontend/CompilerInstance.h"
14 #include "clang/Frontend/FrontendAction.h"
15 #include "clang/Frontend/FrontendActions.h"
16 #include "clang/Parse/ParseAST.h"
17 #include "clang/Sema/Lookup.h"
18 #include "clang/Sema/Sema.h"
19 #include "clang/Sema/SemaConsumer.h"
20 #include "clang/Tooling/Tooling.h"
21 #include "gtest/gtest.h"
23 using namespace llvm;
24 using namespace clang;
25 using namespace clang::tooling;
27 namespace {
29 class NoloadLookupTest : public ::testing::Test {
30 void SetUp() override {
31 ASSERT_FALSE(
32 sys::fs::createUniqueDirectory("modules-no-comments-test", TestDir));
35 void TearDown() override { sys::fs::remove_directories(TestDir); }
37 public:
38 SmallString<256> TestDir;
40 void addFile(StringRef Path, StringRef Contents) {
41 ASSERT_FALSE(sys::path::is_absolute(Path));
43 SmallString<256> AbsPath(TestDir);
44 sys::path::append(AbsPath, Path);
46 ASSERT_FALSE(
47 sys::fs::create_directories(llvm::sys::path::parent_path(AbsPath)));
49 std::error_code EC;
50 llvm::raw_fd_ostream OS(AbsPath, EC);
51 ASSERT_FALSE(EC);
52 OS << Contents;
55 std::string GenerateModuleInterface(StringRef ModuleName,
56 StringRef Contents) {
57 std::string FileName = llvm::Twine(ModuleName + ".cppm").str();
58 addFile(FileName, Contents);
60 IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
61 CompilerInstance::createDiagnostics(new DiagnosticOptions());
62 CreateInvocationOptions CIOpts;
63 CIOpts.Diags = Diags;
64 CIOpts.VFS = llvm::vfs::createPhysicalFileSystem();
66 std::string CacheBMIPath =
67 llvm::Twine(TestDir + "/" + ModuleName + ".pcm").str();
68 std::string PrebuiltModulePath =
69 "-fprebuilt-module-path=" + TestDir.str().str();
70 const char *Args[] = {"clang++",
71 "-std=c++20",
72 "--precompile",
73 PrebuiltModulePath.c_str(),
74 "-working-directory",
75 TestDir.c_str(),
76 "-I",
77 TestDir.c_str(),
78 FileName.c_str()};
79 std::shared_ptr<CompilerInvocation> Invocation =
80 createInvocation(Args, CIOpts);
81 EXPECT_TRUE(Invocation);
83 CompilerInstance Instance;
84 Instance.setDiagnostics(Diags.get());
85 Instance.setInvocation(Invocation);
86 Instance.getFrontendOpts().OutputFile = CacheBMIPath;
87 GenerateReducedModuleInterfaceAction Action;
88 EXPECT_TRUE(Instance.ExecuteAction(Action));
89 EXPECT_FALSE(Diags->hasErrorOccurred());
91 return CacheBMIPath;
95 struct TrivialVisibleDeclConsumer : public VisibleDeclConsumer {
96 TrivialVisibleDeclConsumer() {}
97 void EnteredContext(DeclContext *Ctx) override {}
98 void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
99 bool InBaseClass) override {
100 FoundNum++;
103 int FoundNum = 0;
106 class NoloadLookupConsumer : public SemaConsumer {
107 public:
108 void InitializeSema(Sema &S) override { SemaPtr = &S; }
110 bool HandleTopLevelDecl(DeclGroupRef D) override {
111 if (!D.isSingleDecl())
112 return true;
114 Decl *TD = D.getSingleDecl();
116 auto *ID = dyn_cast<ImportDecl>(TD);
117 if (!ID)
118 return true;
120 clang::Module *M = ID->getImportedModule();
121 assert(M);
122 if (M->Name != "R")
123 return true;
125 auto *Std = SemaPtr->getStdNamespace();
126 EXPECT_TRUE(Std);
127 TrivialVisibleDeclConsumer Consumer;
128 SemaPtr->LookupVisibleDecls(Std, Sema::LookupNameKind::LookupOrdinaryName,
129 Consumer,
130 /*IncludeGlobalScope=*/true,
131 /*IncludeDependentBases=*/false,
132 /*LoadExternal=*/false);
133 EXPECT_EQ(Consumer.FoundNum, 1);
134 return true;
137 private:
138 Sema *SemaPtr = nullptr;
141 class NoloadLookupAction : public ASTFrontendAction {
142 std::unique_ptr<ASTConsumer>
143 CreateASTConsumer(CompilerInstance &CI, StringRef /*Unused*/) override {
144 return std::make_unique<NoloadLookupConsumer>();
148 TEST_F(NoloadLookupTest, NonModulesTest) {
149 GenerateModuleInterface("M", R"cpp(
150 module;
151 namespace std {
152 int What();
154 void bar(int x = What()) {
157 export module M;
158 export using std::bar;
159 )cpp");
161 GenerateModuleInterface("R", R"cpp(
162 module;
163 namespace std {
164 class Another;
165 int What(Another);
166 int What();
168 export module R;
169 )cpp");
171 const char *test_file_contents = R"cpp(
172 import M;
173 namespace std {
174 void use() {
175 bar();
178 import R;
179 )cpp";
180 std::string DepArg = "-fprebuilt-module-path=" + TestDir.str().str();
181 EXPECT_TRUE(runToolOnCodeWithArgs(std::make_unique<NoloadLookupAction>(),
182 test_file_contents,
184 "-std=c++20",
185 DepArg.c_str(),
186 "-I",
187 TestDir.c_str(),
189 "test.cpp"));
192 } // namespace