1 | //===- unittests/Frontend/ASTUnitTest.cpp - ASTUnit 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 <fstream> |
10 | |
11 | #include "clang/Basic/FileManager.h" |
12 | #include "clang/Frontend/ASTUnit.h" |
13 | #include "clang/Frontend/CompilerInstance.h" |
14 | #include "clang/Frontend/CompilerInvocation.h" |
15 | #include "clang/Frontend/PCHContainerOperations.h" |
16 | #include "clang/Lex/HeaderSearch.h" |
17 | #include "llvm/Support/FileSystem.h" |
18 | #include "llvm/Support/Path.h" |
19 | #include "llvm/Support/ToolOutputFile.h" |
20 | #include "llvm/Support/VirtualFileSystem.h" |
21 | #include "gtest/gtest.h" |
22 | |
23 | using namespace llvm; |
24 | using namespace clang; |
25 | |
26 | namespace { |
27 | |
28 | class ASTUnitTest : public ::testing::Test { |
29 | protected: |
30 | int FD; |
31 | llvm::SmallString<256> InputFileName; |
32 | std::unique_ptr<ToolOutputFile> input_file; |
33 | std::shared_ptr<DiagnosticOptions> DiagOpts = |
34 | std::make_shared<DiagnosticOptions>(); |
35 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags; |
36 | std::shared_ptr<CompilerInvocation> CInvok; |
37 | std::shared_ptr<PCHContainerOperations> PCHContainerOps; |
38 | |
39 | std::unique_ptr<ASTUnit> createASTUnit(bool isVolatile) { |
40 | EXPECT_FALSE(llvm::sys::fs::createTemporaryFile("ast-unit" , "cpp" , FD, |
41 | InputFileName)); |
42 | input_file = std::make_unique<ToolOutputFile>(args&: InputFileName, args&: FD); |
43 | input_file->os() << "" ; |
44 | |
45 | const char *Args[] = {"clang" , "-xc++" , InputFileName.c_str()}; |
46 | |
47 | auto VFS = llvm::vfs::getRealFileSystem(); |
48 | Diags = CompilerInstance::createDiagnostics(VFS&: *VFS, Opts&: *DiagOpts); |
49 | |
50 | CreateInvocationOptions CIOpts; |
51 | CIOpts.Diags = Diags; |
52 | CIOpts.VFS = VFS; |
53 | CInvok = createInvocation(Args, Opts: std::move(CIOpts)); |
54 | |
55 | if (!CInvok) |
56 | return nullptr; |
57 | |
58 | FileManager *FileMgr = new FileManager(FileSystemOptions(), VFS); |
59 | PCHContainerOps = std::make_shared<PCHContainerOperations>(); |
60 | |
61 | return ASTUnit::LoadFromCompilerInvocation( |
62 | CI: CInvok, PCHContainerOps, DiagOpts, Diags, FileMgr, OnlyLocalDecls: false, |
63 | CaptureDiagnostics: CaptureDiagsKind::None, PrecompilePreambleAfterNParses: 0, TUKind: TU_Complete, CacheCodeCompletionResults: false, IncludeBriefCommentsInCodeCompletion: false, UserFilesAreVolatile: isVolatile); |
64 | } |
65 | }; |
66 | |
67 | TEST_F(ASTUnitTest, SaveLoadPreservesLangOptionsInPrintingPolicy) { |
68 | // Check that the printing policy is restored with the correct language |
69 | // options when loading an ASTUnit from a file. To this end, an ASTUnit |
70 | // for a C++ translation unit is set up and written to a temporary file. |
71 | |
72 | // By default `UseVoidForZeroParams` is true for non-C++ language options, |
73 | // thus we can check this field after loading the ASTUnit to deduce whether |
74 | // the correct (C++) language options were used when setting up the printing |
75 | // policy. |
76 | |
77 | { |
78 | PrintingPolicy PolicyWithDefaultLangOpt(LangOptions{}); |
79 | EXPECT_TRUE(PolicyWithDefaultLangOpt.UseVoidForZeroParams); |
80 | } |
81 | |
82 | std::unique_ptr<ASTUnit> AST = createASTUnit(isVolatile: false); |
83 | |
84 | if (!AST) |
85 | FAIL() << "failed to create ASTUnit" ; |
86 | |
87 | EXPECT_FALSE(AST->getASTContext().getPrintingPolicy().UseVoidForZeroParams); |
88 | |
89 | llvm::SmallString<256> ASTFileName; |
90 | ASSERT_FALSE( |
91 | llvm::sys::fs::createTemporaryFile("ast-unit" , "ast" , FD, ASTFileName)); |
92 | ToolOutputFile ast_file(ASTFileName, FD); |
93 | AST->Save(File: ASTFileName.str()); |
94 | |
95 | EXPECT_TRUE(llvm::sys::fs::exists(ASTFileName)); |
96 | HeaderSearchOptions HSOpts; |
97 | |
98 | std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile( |
99 | Filename: ASTFileName, PCHContainerRdr: PCHContainerOps->getRawReader(), ToLoad: ASTUnit::LoadEverything, |
100 | DiagOpts, Diags, FileSystemOpts: FileSystemOptions(), HSOpts); |
101 | |
102 | if (!AU) |
103 | FAIL() << "failed to load ASTUnit" ; |
104 | |
105 | EXPECT_FALSE(AU->getASTContext().getPrintingPolicy().UseVoidForZeroParams); |
106 | } |
107 | |
108 | TEST_F(ASTUnitTest, GetBufferForFileMemoryMapping) { |
109 | std::unique_ptr<ASTUnit> AST = createASTUnit(isVolatile: true); |
110 | |
111 | if (!AST) |
112 | FAIL() << "failed to create ASTUnit" ; |
113 | |
114 | std::unique_ptr<llvm::MemoryBuffer> memoryBuffer = |
115 | AST->getBufferForFile(Filename: InputFileName); |
116 | |
117 | EXPECT_NE(memoryBuffer->getBufferKind(), |
118 | llvm::MemoryBuffer::MemoryBuffer_MMap); |
119 | } |
120 | |
121 | TEST_F(ASTUnitTest, ModuleTextualHeader) { |
122 | llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFs = |
123 | new llvm::vfs::InMemoryFileSystem(); |
124 | InMemoryFs->addFile(Path: "test.cpp" , ModificationTime: 0, Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: R"cpp( |
125 | #include "Textual.h" |
126 | void foo() {} |
127 | )cpp" )); |
128 | InMemoryFs->addFile(Path: "m.modulemap" , ModificationTime: 0, Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: R"cpp( |
129 | module M { |
130 | module Textual { |
131 | textual header "Textual.h" |
132 | } |
133 | } |
134 | )cpp" )); |
135 | InMemoryFs->addFile(Path: "Textual.h" , ModificationTime: 0, Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: R"cpp( |
136 | void foo(); |
137 | )cpp" )); |
138 | |
139 | const char *Args[] = {"clang" , "test.cpp" , "-fmodule-map-file=m.modulemap" , |
140 | "-fmodule-name=M" }; |
141 | Diags = CompilerInstance::createDiagnostics(VFS&: *InMemoryFs, Opts&: *DiagOpts); |
142 | CreateInvocationOptions CIOpts; |
143 | CIOpts.Diags = Diags; |
144 | CInvok = createInvocation(Args, Opts: std::move(CIOpts)); |
145 | ASSERT_TRUE(CInvok); |
146 | |
147 | FileManager *FileMgr = new FileManager(FileSystemOptions(), InMemoryFs); |
148 | PCHContainerOps = std::make_shared<PCHContainerOperations>(); |
149 | |
150 | auto AU = ASTUnit::LoadFromCompilerInvocation( |
151 | CI: CInvok, PCHContainerOps, DiagOpts, Diags, FileMgr, OnlyLocalDecls: false, |
152 | CaptureDiagnostics: CaptureDiagsKind::None, PrecompilePreambleAfterNParses: 1, TUKind: TU_Complete, CacheCodeCompletionResults: false, IncludeBriefCommentsInCodeCompletion: false, UserFilesAreVolatile: false); |
153 | ASSERT_TRUE(AU); |
154 | auto File = AU->getFileManager().getFileRef(Filename: "Textual.h" , OpenFile: false, CacheFailure: false); |
155 | ASSERT_TRUE(bool(File)); |
156 | // Verify that we do not crash here. |
157 | EXPECT_TRUE( |
158 | AU->getPreprocessor().getHeaderSearchInfo().getExistingFileInfo(*File)); |
159 | } |
160 | |
161 | TEST_F(ASTUnitTest, LoadFromCommandLineEarlyError) { |
162 | EXPECT_FALSE( |
163 | llvm::sys::fs::createTemporaryFile("ast-unit" , "c" , FD, InputFileName)); |
164 | input_file = std::make_unique<ToolOutputFile>(args&: InputFileName, args&: FD); |
165 | input_file->os() << "" ; |
166 | |
167 | const char *Args[] = {"clang" , "-target" , "foobar" , InputFileName.c_str()}; |
168 | |
169 | auto Diags = CompilerInstance::createDiagnostics( |
170 | VFS&: *llvm::vfs::getRealFileSystem(), Opts&: *DiagOpts); |
171 | auto PCHContainerOps = std::make_shared<PCHContainerOperations>(); |
172 | std::unique_ptr<clang::ASTUnit> ErrUnit; |
173 | |
174 | std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCommandLine( |
175 | ArgBegin: &Args[0], ArgEnd: &Args[4], PCHContainerOps, DiagOpts, Diags, ResourceFilesPath: "" , StorePreamblesInMemory: false, PreambleStoragePath: "" , |
176 | OnlyLocalDecls: false, CaptureDiagnostics: CaptureDiagsKind::All, RemappedFiles: {}, RemappedFilesKeepOriginalName: true, PrecompilePreambleAfterNParses: 0, TUKind: TU_Complete, CacheCodeCompletionResults: false, IncludeBriefCommentsInCodeCompletion: false, |
177 | AllowPCHWithCompilerErrors: false, SkipFunctionBodies: SkipFunctionBodiesScope::None, SingleFileParse: false, UserFilesAreVolatile: true, ForSerialization: false, RetainExcludedConditionalBlocks: false, |
178 | ModuleFormat: std::nullopt, ErrAST: &ErrUnit, VFS: nullptr); |
179 | |
180 | ASSERT_EQ(AST, nullptr); |
181 | ASSERT_NE(ErrUnit, nullptr); |
182 | ASSERT_TRUE(Diags->hasErrorOccurred()); |
183 | ASSERT_NE(ErrUnit->stored_diag_size(), 0U); |
184 | } |
185 | |
186 | TEST_F(ASTUnitTest, LoadFromCommandLineWorkingDirectory) { |
187 | EXPECT_FALSE( |
188 | llvm::sys::fs::createTemporaryFile("bar" , "c" , FD, InputFileName)); |
189 | auto Input = std::make_unique<ToolOutputFile>(args&: InputFileName, args&: FD); |
190 | Input->os() << "" ; |
191 | |
192 | SmallString<128> WorkingDir; |
193 | ASSERT_FALSE(sys::fs::createUniqueDirectory("foo" , WorkingDir)); |
194 | const char *Args[] = {"clang" , "-working-directory" , WorkingDir.c_str(), |
195 | InputFileName.c_str()}; |
196 | |
197 | auto Diags = CompilerInstance::createDiagnostics( |
198 | VFS&: *llvm::vfs::getRealFileSystem(), Opts&: *DiagOpts); |
199 | auto PCHContainerOps = std::make_shared<PCHContainerOperations>(); |
200 | std::unique_ptr<clang::ASTUnit> ErrUnit; |
201 | |
202 | std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCommandLine( |
203 | ArgBegin: &Args[0], ArgEnd: &Args[4], PCHContainerOps, DiagOpts, Diags, ResourceFilesPath: "" , StorePreamblesInMemory: false, PreambleStoragePath: "" , |
204 | OnlyLocalDecls: false, CaptureDiagnostics: CaptureDiagsKind::All, RemappedFiles: {}, RemappedFilesKeepOriginalName: true, PrecompilePreambleAfterNParses: 0, TUKind: TU_Complete, CacheCodeCompletionResults: false, IncludeBriefCommentsInCodeCompletion: false, |
205 | AllowPCHWithCompilerErrors: false, SkipFunctionBodies: SkipFunctionBodiesScope::None, SingleFileParse: false, UserFilesAreVolatile: true, ForSerialization: false, RetainExcludedConditionalBlocks: false, |
206 | ModuleFormat: std::nullopt, ErrAST: &ErrUnit, VFS: nullptr); |
207 | |
208 | ASSERT_NE(AST, nullptr); |
209 | ASSERT_FALSE(Diags->hasErrorOccurred()); |
210 | |
211 | // Make sure '-working-directory' sets both the FileSystemOpts and underlying |
212 | // VFS working directory. |
213 | const auto &FM = AST->getFileManager(); |
214 | const auto &VFS = FM.getVirtualFileSystem(); |
215 | ASSERT_EQ(*VFS.getCurrentWorkingDirectory(), WorkingDir.str()); |
216 | ASSERT_EQ(FM.getFileSystemOpts().WorkingDir, WorkingDir.str()); |
217 | } |
218 | |
219 | } // anonymous namespace |
220 | |