Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / unittests / Serialization / NoCommentsTest.cpp
blob2632a6337807acf8f30f6506d0648adcb69d83bd
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 //===----------------------------------------------------------------------===//
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"
24 using namespace llvm;
25 using namespace clang;
27 namespace {
29 class NoComments : 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;
56 TEST_F(NoComments, NonModulesTest) {
57 std::unique_ptr<ASTUnit> AST = tooling::buildASTFromCodeWithArgs(
58 R"cpp(
59 /// Any comments
60 void foo() {}
61 )cpp",
62 /*Args=*/{"-std=c++20"});
63 EXPECT_TRUE(AST);
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));
70 EXPECT_TRUE(foo);
72 const RawComment *RC = getCompletionComment(Ctx, foo);
73 EXPECT_TRUE(RC);
74 EXPECT_TRUE(RC->getRawText(Ctx.getSourceManager()).trim() ==
75 "/// Any comments");
78 TEST_F(NoComments, ModulesTest) {
79 addFile("Comments.cppm", R"cpp(
80 export module Comments;
82 /// Any comments
83 void foo() {}
84 )cpp");
86 IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
87 CompilerInstance::createDiagnostics(new DiagnosticOptions());
88 CreateInvocationOptions CIOpts;
89 CIOpts.Diags = Diags;
90 CIOpts.VFS = llvm::vfs::createPhysicalFileSystem();
92 std::string CacheBMIPath = llvm::Twine(TestDir + "/Comments.pcm").str();
93 const char *Args[] = {
94 "clang++", "-std=c++20", "--precompile", "-working-directory",
95 TestDir.c_str(), "Comments.cppm", "-o", CacheBMIPath.c_str()};
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 GenerateModuleInterfaceAction Action;
104 ASSERT_TRUE(Instance.ExecuteAction(Action));
105 ASSERT_FALSE(Diags->hasErrorOccurred());
107 std::string DepArg =
108 llvm::Twine("-fmodule-file=Comments=" + CacheBMIPath).str();
109 std::unique_ptr<ASTUnit> AST = tooling::buildASTFromCodeWithArgs(
110 R"cpp(
111 import Comments;
112 )cpp",
113 /*Args=*/{"-std=c++20", DepArg.c_str()});
114 EXPECT_TRUE(AST);
116 ASTContext &Ctx = AST->getASTContext();
118 using namespace clang::ast_matchers;
119 auto *foo = selectFirst<FunctionDecl>(
120 "foo", match(functionDecl(hasName("foo")).bind("foo"), Ctx));
121 EXPECT_TRUE(foo);
123 const RawComment *RC = getCompletionComment(Ctx, foo);
124 EXPECT_FALSE(RC);
127 } // anonymous namespace