| 1 | //===- unittests/Serialization/ForceCheckFileInputTest.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/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/Lex/PreprocessorOptions.h" |
| 18 | #include "clang/Serialization/ASTReader.h" |
| 19 | #include "clang/Tooling/Tooling.h" |
| 20 | #include "llvm/ADT/SmallString.h" |
| 21 | #include "llvm/Support/FileSystem.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | |
| 24 | #include "gtest/gtest.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | using namespace clang; |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | class ForceCheckFileInputTest : public ::testing::Test { |
| 32 | void SetUp() override { |
| 33 | EXPECT_FALSE(sys::fs::createUniqueDirectory("modules-test" , TestDir)); |
| 34 | } |
| 35 | |
| 36 | void TearDown() override { sys::fs::remove_directories(path: TestDir); } |
| 37 | |
| 38 | public: |
| 39 | SmallString<256> TestDir; |
| 40 | |
| 41 | void addFile(StringRef Path, StringRef Contents) { |
| 42 | EXPECT_FALSE(sys::path::is_absolute(Path)); |
| 43 | |
| 44 | SmallString<256> AbsPath(TestDir); |
| 45 | sys::path::append(path&: AbsPath, a: Path); |
| 46 | |
| 47 | EXPECT_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 | EXPECT_FALSE(EC); |
| 53 | OS << Contents; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | TEST_F(ForceCheckFileInputTest, ForceCheck) { |
| 58 | addFile(Path: "a.cppm" , Contents: R"cpp( |
| 59 | export module a; |
| 60 | export int aa = 43; |
| 61 | )cpp" ); |
| 62 | |
| 63 | std::string BMIPath = llvm::Twine(TestDir + "/a.pcm" ).str(); |
| 64 | |
| 65 | { |
| 66 | CreateInvocationOptions CIOpts; |
| 67 | CIOpts.VFS = llvm::vfs::createPhysicalFileSystem(); |
| 68 | |
| 69 | DiagnosticOptions DiagOpts; |
| 70 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags = |
| 71 | CompilerInstance::createDiagnostics(VFS&: *CIOpts.VFS, Opts&: DiagOpts); |
| 72 | CIOpts.Diags = Diags; |
| 73 | |
| 74 | const char *Args[] = {"clang++" , "-std=c++20" , |
| 75 | "--precompile" , "-working-directory" , |
| 76 | TestDir.c_str(), "a.cppm" }; |
| 77 | std::shared_ptr<CompilerInvocation> Invocation = |
| 78 | createInvocation(Args, Opts: CIOpts); |
| 79 | EXPECT_TRUE(Invocation); |
| 80 | Invocation->getFrontendOpts().DisableFree = false; |
| 81 | |
| 82 | auto Buf = CIOpts.VFS->getBufferForFile(Name: "a.cppm" ); |
| 83 | EXPECT_TRUE(Buf); |
| 84 | |
| 85 | Invocation->getPreprocessorOpts().addRemappedFile(From: "a.cppm" , To: Buf->get()); |
| 86 | |
| 87 | Buf->release(); |
| 88 | |
| 89 | CompilerInstance Instance(std::move(Invocation)); |
| 90 | Instance.setDiagnostics(Diags.get()); |
| 91 | |
| 92 | Instance.getFrontendOpts().OutputFile = BMIPath; |
| 93 | |
| 94 | if (auto VFSWithRemapping = createVFSFromCompilerInvocation( |
| 95 | CI: Instance.getInvocation(), Diags&: Instance.getDiagnostics(), BaseFS: CIOpts.VFS)) |
| 96 | CIOpts.VFS = VFSWithRemapping; |
| 97 | Instance.createFileManager(VFS: CIOpts.VFS); |
| 98 | |
| 99 | Instance.getHeaderSearchOpts().ValidateASTInputFilesContent = true; |
| 100 | |
| 101 | GenerateReducedModuleInterfaceAction Action; |
| 102 | EXPECT_TRUE(Instance.ExecuteAction(Action)); |
| 103 | EXPECT_FALSE(Diags->hasErrorOccurred()); |
| 104 | } |
| 105 | |
| 106 | { |
| 107 | CreateInvocationOptions CIOpts; |
| 108 | CIOpts.VFS = llvm::vfs::createPhysicalFileSystem(); |
| 109 | DiagnosticOptions DiagOpts; |
| 110 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags = |
| 111 | CompilerInstance::createDiagnostics(VFS&: *CIOpts.VFS, Opts&: DiagOpts); |
| 112 | CIOpts.Diags = Diags; |
| 113 | |
| 114 | std::string BMIPath = llvm::Twine(TestDir + "/a.pcm" ).str(); |
| 115 | const char *Args[] = { |
| 116 | "clang++" , "-std=c++20" , "--precompile" , "-working-directory" , |
| 117 | TestDir.c_str(), "a.cppm" , "-o" , BMIPath.c_str()}; |
| 118 | std::shared_ptr<CompilerInvocation> Invocation = |
| 119 | createInvocation(Args, Opts: CIOpts); |
| 120 | EXPECT_TRUE(Invocation); |
| 121 | Invocation->getFrontendOpts().DisableFree = false; |
| 122 | |
| 123 | CompilerInstance Clang(std::move(Invocation)); |
| 124 | |
| 125 | Clang.setDiagnostics(Diags.get()); |
| 126 | FileManager *FM = Clang.createFileManager(VFS: CIOpts.VFS); |
| 127 | Clang.createSourceManager(FileMgr&: *FM); |
| 128 | |
| 129 | EXPECT_TRUE(Clang.createTarget()); |
| 130 | Clang.createPreprocessor(TUKind: TU_Complete); |
| 131 | Clang.getHeaderSearchOpts().ForceCheckCXX20ModulesInputFiles = true; |
| 132 | Clang.getHeaderSearchOpts().ValidateASTInputFilesContent = true; |
| 133 | Clang.createASTReader(); |
| 134 | |
| 135 | addFile(Path: "a.cppm" , Contents: R"cpp( |
| 136 | export module a; |
| 137 | export int aa = 44; |
| 138 | )cpp" ); |
| 139 | |
| 140 | auto ReadResult = |
| 141 | Clang.getASTReader()->ReadAST(FileName: BMIPath, Type: serialization::MK_MainFile, |
| 142 | ImportLoc: SourceLocation(), ClientLoadCapabilities: ASTReader::ARR_None); |
| 143 | |
| 144 | // We shall be able to detect the content change here. |
| 145 | EXPECT_NE(ReadResult, ASTReader::Success); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | } // anonymous namespace |
| 150 | |