| 1 |
//===- unittests/Serialization/NoComments.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 NoComments : public ::testing::Test { |
| 31 |
void SetUp() override { |
| 32 |
ASSERT_FALSE( |
| 33 |
sys::fs::createUniqueDirectory("modules-no-comments-test", TestDir)); |
| 34 |
} |
| 35 |
|
| 36 |
void TearDown() override { sys::fs::remove_directories(TestDir); } |
| 37 |
|
| 38 |
public: |
| 39 |
SmallString<256> TestDir; |
| 40 |
|
| 41 |
void addFile(StringRef Path, StringRef Contents) { |
| 42 |
ASSERT_FALSE(sys::path::is_absolute(Path)); |
| 43 |
|
| 44 |
SmallString<256> AbsPath(TestDir); |
| 45 |
sys::path::append(AbsPath, Path); |
| 46 |
|
| 47 |
ASSERT_FALSE( |
| 48 |
sys::fs::create_directories(llvm::sys::path::parent_path(AbsPath))); |
| 49 |
|
| 50 |
std::error_code EC; |
| 51 |
llvm::raw_fd_ostream OS(AbsPath, EC); |
| 52 |
ASSERT_FALSE(EC); |
| 53 |
OS << Contents; |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
TEST_F(NoComments, NonModulesTest) { |
| 58 |
std::unique_ptr<ASTUnit> AST = tooling::buildASTFromCodeWithArgs( |
| 59 |
R"cpp( |
| 60 |
/// Any comments |
| 61 |
void foo() {} |
| 62 |
)cpp", |
| 63 |
/*Args=*/{"-std=c++20"}); |
| 64 |
EXPECT_TRUE(AST); |
| 65 |
|
| 66 |
ASTContext &Ctx = AST->getASTContext(); |
| 67 |
|
| 68 |
using namespace clang::ast_matchers; |
| 69 |
auto *foo = selectFirst<FunctionDecl>( |
| 70 |
"foo", match(functionDecl(hasName("foo")).bind("foo"), Ctx)); |
| 71 |
EXPECT_TRUE(foo); |
| 72 |
|
| 73 |
const RawComment *RC = getCompletionComment(Ctx, foo); |
| 74 |
EXPECT_TRUE(RC); |
| 75 |
EXPECT_TRUE(RC->getRawText(Ctx.getSourceManager()).trim() == |
| 76 |
"/// Any comments"); |
| 77 |
} |
| 78 |
|
| 79 |
TEST_F(NoComments, ModulesTest) { |
| 80 |
addFile("Comments.cppm", R"cpp( |
| 81 |
export module Comments; |
| 82 |
|
| 83 |
/// Any comments |
| 84 |
void foo() {} |
| 85 |
)cpp"); |
| 86 |
|
| 87 |
CreateInvocationOptions CIOpts; |
| 88 |
CIOpts.VFS = llvm::vfs::createPhysicalFileSystem(); |
| 89 |
DiagnosticOptions DiagOpts; |
| 90 |
IntrusiveRefCntPtr<DiagnosticsEngine> Diags = |
| 91 |
CompilerInstance::createDiagnostics(*CIOpts.VFS, DiagOpts); |
| 92 |
CIOpts.Diags = Diags; |
| 93 |
|
| 94 |
std::string CacheBMIPath = llvm::Twine(TestDir + "/Comments.pcm").str(); |
| 95 |
const char *Args[] = {"clang++", "-std=c++20", |
| 96 |
"--precompile", "-working-directory", |
| 97 |
TestDir.c_str(), "Comments.cppm"}; |
| 98 |
std::shared_ptr<CompilerInvocation> Invocation = |
| 99 |
createInvocation(Args, CIOpts); |
| 100 |
ASSERT_TRUE(Invocation); |
| 101 |
|
| 102 |
CompilerInstance Instance(std::move(Invocation)); |
| 103 |
Instance.createVirtualFileSystem(CIOpts.VFS); |
| 104 |
Instance.setDiagnostics(Diags); |
| 105 |
Instance.getFrontendOpts().OutputFile = CacheBMIPath; |
| 106 |
GenerateReducedModuleInterfaceAction Action; |
| 107 |
ASSERT_TRUE(Instance.ExecuteAction(Action)); |
| 108 |
ASSERT_FALSE(Diags->hasErrorOccurred()); |
| 109 |
|
| 110 |
std::string DepArg = |
| 111 |
llvm::Twine("-fmodule-file=Comments=" + CacheBMIPath).str(); |
| 112 |
std::unique_ptr<ASTUnit> AST = tooling::buildASTFromCodeWithArgs( |
| 113 |
R"cpp( |
| 114 |
import Comments; |
| 115 |
)cpp", |
| 116 |
/*Args=*/{"-std=c++20", DepArg.c_str()}); |
| 117 |
EXPECT_TRUE(AST); |
| 118 |
|
| 119 |
ASTContext &Ctx = AST->getASTContext(); |
| 120 |
|
| 121 |
using namespace clang::ast_matchers; |
| 122 |
auto *foo = selectFirst<FunctionDecl>( |
| 123 |
"foo", match(functionDecl(hasName("foo")).bind("foo"), Ctx)); |
| 124 |
EXPECT_TRUE(foo); |
| 125 |
|
| 126 |
const RawComment *RC = getCompletionComment(Ctx, foo); |
| 127 |
EXPECT_FALSE(RC); |
| 128 |
} |
| 129 |
|
| 130 |
} // anonymous namespace |