| 1 |
//===- unittests/Serialization/VarDeclConstantInitTest.cpp - CI tests -----===// |
| 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 |
#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 10 |
#include "clang/ASTMatchers/ASTMatchers.h" |
| 11 |
#include "clang/Basic/FileManager.h" |
| 12 |
#include "clang/Driver/CreateInvocationFromArgs.h" |
| 13 |
#include "clang/Frontend/CompilerInstance.h" |
| 14 |
#include "clang/Frontend/CompilerInvocation.h" |
| 15 |
#include "clang/Frontend/FrontendActions.h" |
| 16 |
#include "clang/Frontend/Utils.h" |
| 17 |
#include "clang/Lex/HeaderSearch.h" |
| 18 |
#include "clang/Tooling/Tooling.h" |
| 19 |
#include "llvm/ADT/SmallString.h" |
| 20 |
#include "llvm/Support/FileSystem.h" |
| 21 |
#include "llvm/Support/raw_ostream.h" |
| 22 |
|
| 23 |
#include "gtest/gtest.h" |
| 24 |
|
| 25 |
using namespace llvm; |
| 26 |
using namespace clang; |
| 27 |
|
| 28 |
namespace { |
| 29 |
|
| 30 |
class VarDeclConstantInitTest : public ::testing::Test { |
| 31 |
void SetUp() override { |
| 32 |
ASSERT_FALSE(sys::fs::createUniqueDirectory("modules-test", TestDir)); |
| 33 |
} |
| 34 |
|
| 35 |
void TearDown() override { sys::fs::remove_directories(TestDir); } |
| 36 |
|
| 37 |
public: |
| 38 |
SmallString<256> TestDir; |
| 39 |
|
| 40 |
void addFile(StringRef Path, StringRef Contents) { |
| 41 |
ASSERT_FALSE(sys::path::is_absolute(Path)); |
| 42 |
|
| 43 |
SmallString<256> AbsPath(TestDir); |
| 44 |
sys::path::append(AbsPath, Path); |
| 45 |
|
| 46 |
ASSERT_FALSE( |
| 47 |
sys::fs::create_directories(llvm::sys::path::parent_path(AbsPath))); |
| 48 |
|
| 49 |
std::error_code EC; |
| 50 |
llvm::raw_fd_ostream OS(AbsPath, EC); |
| 51 |
ASSERT_FALSE(EC); |
| 52 |
OS << Contents; |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
TEST_F(VarDeclConstantInitTest, CachedConstantInit) { |
| 57 |
addFile("Cached.cppm", R"cpp( |
| 58 |
export module Fibonacci.Cache; |
| 59 |
|
| 60 |
export namespace Fibonacci |
| 61 |
{ |
| 62 |
constexpr unsigned long Recursive(unsigned long n) |
| 63 |
{ |
| 64 |
if (n == 0) |
| 65 |
return 0; |
| 66 |
if (n == 1) |
| 67 |
return 1; |
| 68 |
return Recursive(n - 2) + Recursive(n - 1); |
| 69 |
} |
| 70 |
|
| 71 |
template<unsigned long N> |
| 72 |
struct Number{}; |
| 73 |
|
| 74 |
struct DefaultStrategy |
| 75 |
{ |
| 76 |
constexpr unsigned long operator()(unsigned long n, auto... other) const |
| 77 |
{ |
| 78 |
return (n + ... + other); |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
constexpr unsigned long Compute(Number<10ul>, auto strategy) |
| 83 |
{ |
| 84 |
return strategy(Recursive(10ul)); |
| 85 |
} |
| 86 |
|
| 87 |
template<unsigned long N, typename Strategy = DefaultStrategy> |
| 88 |
constexpr unsigned long Cache = Compute(Number<N>{}, Strategy{}); |
| 89 |
|
| 90 |
template constexpr unsigned long Cache<10ul>; |
| 91 |
} |
| 92 |
)cpp"); |
| 93 |
|
| 94 |
CreateInvocationOptions CIOpts; |
| 95 |
CIOpts.VFS = llvm::vfs::createPhysicalFileSystem(); |
| 96 |
DiagnosticOptions DiagOpts; |
| 97 |
IntrusiveRefCntPtr<DiagnosticsEngine> Diags = |
| 98 |
CompilerInstance::createDiagnostics(*CIOpts.VFS, DiagOpts); |
| 99 |
CIOpts.Diags = Diags; |
| 100 |
|
| 101 |
const char *Args[] = {"clang++", "-std=c++20", |
| 102 |
"--precompile", "-working-directory", |
| 103 |
TestDir.c_str(), "Cached.cppm"}; |
| 104 |
std::shared_ptr<CompilerInvocation> Invocation = |
| 105 |
createInvocation(Args, CIOpts); |
| 106 |
ASSERT_TRUE(Invocation); |
| 107 |
Invocation->getFrontendOpts().DisableFree = false; |
| 108 |
|
| 109 |
CompilerInstance Instance(std::move(Invocation)); |
| 110 |
Instance.setDiagnostics(Diags); |
| 111 |
|
| 112 |
std::string CacheBMIPath = llvm::Twine(TestDir + "/Cached.pcm").str(); |
| 113 |
Instance.getFrontendOpts().OutputFile = CacheBMIPath; |
| 114 |
|
| 115 |
GenerateReducedModuleInterfaceAction Action; |
| 116 |
ASSERT_TRUE(Instance.ExecuteAction(Action)); |
| 117 |
ASSERT_FALSE(Diags->hasErrorOccurred()); |
| 118 |
|
| 119 |
std::string DepArg = |
| 120 |
llvm::Twine("-fmodule-file=Fibonacci.Cache=" + CacheBMIPath).str(); |
| 121 |
std::unique_ptr<ASTUnit> AST = tooling::buildASTFromCodeWithArgs( |
| 122 |
R"cpp( |
| 123 |
import Fibonacci.Cache; |
| 124 |
)cpp", |
| 125 |
/*Args=*/{"-std=c++20", DepArg.c_str()}); |
| 126 |
|
| 127 |
using namespace clang::ast_matchers; |
| 128 |
ASTContext &Ctx = AST->getASTContext(); |
| 129 |
const auto *cached = selectFirst<VarDecl>( |
| 130 |
"Cache", |
| 131 |
match(varDecl(isTemplateInstantiation(), hasName("Cache")).bind("Cache"), |
| 132 |
Ctx)); |
| 133 |
EXPECT_TRUE(cached); |
| 134 |
EXPECT_TRUE(cached->getEvaluatedStmt()); |
| 135 |
EXPECT_TRUE(cached->getEvaluatedStmt()->WasEvaluated); |
| 136 |
EXPECT_TRUE(cached->getEvaluatedValue()); |
| 137 |
EXPECT_TRUE(cached->getEvaluatedValue()->isInt()); |
| 138 |
EXPECT_EQ(cached->getEvaluatedValue()->getInt(), 55); |
| 139 |
} |
| 140 |
|
| 141 |
} // anonymous namespace |