1 //===- unittests/Serialization/NoComments.cpp - CI tests -----===//
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
7 //===----------------------------------------------------------------------===//
9 #include "clang/ASTMatchers/ASTMatchFinder.h"
10 #include "clang/ASTMatchers/ASTMatchers.h"
11 #include "clang/Basic/FileManager.h"
12 #include "clang/Frontend/CompilerInstance.h"
13 #include "clang/Frontend/CompilerInvocation.h"
14 #include "clang/Frontend/FrontendActions.h"
15 #include "clang/Frontend/Utils.h"
16 #include "clang/Lex/HeaderSearch.h"
17 #include "clang/Tooling/Tooling.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/raw_ostream.h"
22 #include "gtest/gtest.h"
25 using namespace clang
;
29 class NoComments
: public ::testing::Test
{
30 void SetUp() override
{
32 sys::fs::createUniqueDirectory("modules-no-comments-test", TestDir
));
35 void TearDown() override
{ sys::fs::remove_directories(TestDir
); }
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
);
47 sys::fs::create_directories(llvm::sys::path::parent_path(AbsPath
)));
50 llvm::raw_fd_ostream
OS(AbsPath
, EC
);
56 TEST_F(NoComments
, NonModulesTest
) {
57 std::unique_ptr
<ASTUnit
> AST
= tooling::buildASTFromCodeWithArgs(
62 /*Args=*/{"-std=c++20"});
65 ASTContext
&Ctx
= AST
->getASTContext();
67 using namespace clang::ast_matchers
;
68 auto *foo
= selectFirst
<FunctionDecl
>(
69 "foo", match(functionDecl(hasName("foo")).bind("foo"), Ctx
));
72 const RawComment
*RC
= getCompletionComment(Ctx
, foo
);
74 EXPECT_TRUE(RC
->getRawText(Ctx
.getSourceManager()).trim() ==
78 TEST_F(NoComments
, ModulesTest
) {
79 addFile("Comments.cppm", R
"cpp(
80 export module Comments;
86 IntrusiveRefCntPtr
<DiagnosticsEngine
> Diags
=
87 CompilerInstance::createDiagnostics(new DiagnosticOptions());
88 CreateInvocationOptions CIOpts
;
90 CIOpts
.VFS
= llvm::vfs::createPhysicalFileSystem();
92 std::string CacheBMIPath
= llvm::Twine(TestDir
+ "/Comments.pcm").str();
93 const char *Args
[] = {"clang++", "-std=c++20",
94 "--precompile", "-working-directory",
95 TestDir
.c_str(), "Comments.cppm"};
96 std::shared_ptr
<CompilerInvocation
> Invocation
=
97 createInvocation(Args
, CIOpts
);
98 ASSERT_TRUE(Invocation
);
100 CompilerInstance Instance
;
101 Instance
.setDiagnostics(Diags
.get());
102 Instance
.setInvocation(Invocation
);
103 Instance
.getFrontendOpts().OutputFile
= CacheBMIPath
;
104 GenerateReducedModuleInterfaceAction Action
;
105 ASSERT_TRUE(Instance
.ExecuteAction(Action
));
106 ASSERT_FALSE(Diags
->hasErrorOccurred());
109 llvm::Twine("-fmodule-file=Comments=" + CacheBMIPath
).str();
110 std::unique_ptr
<ASTUnit
> AST
= tooling::buildASTFromCodeWithArgs(
114 /*Args=*/{"-std=c++20", DepArg
.c_str()});
117 ASTContext
&Ctx
= AST
->getASTContext();
119 using namespace clang::ast_matchers
;
120 auto *foo
= selectFirst
<FunctionDecl
>(
121 "foo", match(functionDecl(hasName("foo")).bind("foo"), Ctx
));
124 const RawComment
*RC
= getCompletionComment(Ctx
, foo
);
128 } // anonymous namespace