1 //===- unittests/Frontend/ASTUnitTest.cpp - ASTUnit 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 //===----------------------------------------------------------------------===//
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 "gtest/gtest.h"
23 using namespace clang
;
27 class ASTUnitTest
: public ::testing::Test
{
30 llvm::SmallString
<256> InputFileName
;
31 std::unique_ptr
<ToolOutputFile
> input_file
;
32 IntrusiveRefCntPtr
<DiagnosticsEngine
> Diags
;
33 std::shared_ptr
<CompilerInvocation
> CInvok
;
34 std::shared_ptr
<PCHContainerOperations
> PCHContainerOps
;
36 std::unique_ptr
<ASTUnit
> createASTUnit(bool isVolatile
) {
37 EXPECT_FALSE(llvm::sys::fs::createTemporaryFile("ast-unit", "cpp", FD
,
39 input_file
= std::make_unique
<ToolOutputFile
>(InputFileName
, FD
);
40 input_file
->os() << "";
42 const char *Args
[] = {"clang", "-xc++", InputFileName
.c_str()};
44 Diags
= CompilerInstance::createDiagnostics(new DiagnosticOptions());
46 CreateInvocationOptions CIOpts
;
48 CInvok
= createInvocation(Args
, std::move(CIOpts
));
53 FileManager
*FileMgr
=
54 new FileManager(FileSystemOptions(), vfs::getRealFileSystem());
55 PCHContainerOps
= std::make_shared
<PCHContainerOperations
>();
57 return ASTUnit::LoadFromCompilerInvocation(
58 CInvok
, PCHContainerOps
, Diags
, FileMgr
, false, CaptureDiagsKind::None
,
59 0, TU_Complete
, false, false, isVolatile
);
63 TEST_F(ASTUnitTest
, SaveLoadPreservesLangOptionsInPrintingPolicy
) {
64 // Check that the printing policy is restored with the correct language
65 // options when loading an ASTUnit from a file. To this end, an ASTUnit
66 // for a C++ translation unit is set up and written to a temporary file.
68 // By default `UseVoidForZeroParams` is true for non-C++ language options,
69 // thus we can check this field after loading the ASTUnit to deduce whether
70 // the correct (C++) language options were used when setting up the printing
74 PrintingPolicy
PolicyWithDefaultLangOpt(LangOptions
{});
75 EXPECT_TRUE(PolicyWithDefaultLangOpt
.UseVoidForZeroParams
);
78 std::unique_ptr
<ASTUnit
> AST
= createASTUnit(false);
81 FAIL() << "failed to create ASTUnit";
83 EXPECT_FALSE(AST
->getASTContext().getPrintingPolicy().UseVoidForZeroParams
);
85 llvm::SmallString
<256> ASTFileName
;
87 llvm::sys::fs::createTemporaryFile("ast-unit", "ast", FD
, ASTFileName
));
88 ToolOutputFile
ast_file(ASTFileName
, FD
);
89 AST
->Save(ASTFileName
.str());
91 EXPECT_TRUE(llvm::sys::fs::exists(ASTFileName
));
92 auto HSOpts
= std::make_shared
<HeaderSearchOptions
>();
94 std::unique_ptr
<ASTUnit
> AU
= ASTUnit::LoadFromASTFile(
95 std::string(ASTFileName
.str()), PCHContainerOps
->getRawReader(),
96 ASTUnit::LoadEverything
, Diags
, FileSystemOptions(), HSOpts
,
97 /*UseDebugInfo=*/false);
100 FAIL() << "failed to load ASTUnit";
102 EXPECT_FALSE(AU
->getASTContext().getPrintingPolicy().UseVoidForZeroParams
);
105 TEST_F(ASTUnitTest
, GetBufferForFileMemoryMapping
) {
106 std::unique_ptr
<ASTUnit
> AST
= createASTUnit(true);
109 FAIL() << "failed to create ASTUnit";
111 std::unique_ptr
<llvm::MemoryBuffer
> memoryBuffer
=
112 AST
->getBufferForFile(InputFileName
);
114 EXPECT_NE(memoryBuffer
->getBufferKind(),
115 llvm::MemoryBuffer::MemoryBuffer_MMap
);
118 TEST_F(ASTUnitTest
, ModuleTextualHeader
) {
119 llvm::IntrusiveRefCntPtr
<llvm::vfs::InMemoryFileSystem
> InMemoryFs
=
120 new llvm::vfs::InMemoryFileSystem();
121 InMemoryFs
->addFile("test.cpp", 0, llvm::MemoryBuffer::getMemBuffer(R
"cpp(
125 InMemoryFs
->addFile("m.modulemap", 0, llvm::MemoryBuffer::getMemBuffer(R
"cpp(
128 textual header "Textual
.h
"
132 InMemoryFs
->addFile("Textual.h", 0, llvm::MemoryBuffer::getMemBuffer(R
"cpp(
136 const char *Args
[] = {"clang", "test.cpp", "-fmodule-map-file=m.modulemap",
138 Diags
= CompilerInstance::createDiagnostics(new DiagnosticOptions());
139 CreateInvocationOptions CIOpts
;
140 CIOpts
.Diags
= Diags
;
141 CInvok
= createInvocation(Args
, std::move(CIOpts
));
144 FileManager
*FileMgr
= new FileManager(FileSystemOptions(), InMemoryFs
);
145 PCHContainerOps
= std::make_shared
<PCHContainerOperations
>();
147 auto AU
= ASTUnit::LoadFromCompilerInvocation(
148 CInvok
, PCHContainerOps
, Diags
, FileMgr
, false, CaptureDiagsKind::None
, 1,
149 TU_Complete
, false, false, false);
151 auto File
= AU
->getFileManager().getFileRef("Textual.h", false, false);
152 ASSERT_TRUE(bool(File
));
153 // Verify that we do not crash here.
155 AU
->getPreprocessor().getHeaderSearchInfo().getExistingFileInfo(*File
));
158 TEST_F(ASTUnitTest
, LoadFromCommandLineEarlyError
) {
160 llvm::sys::fs::createTemporaryFile("ast-unit", "c", FD
, InputFileName
));
161 input_file
= std::make_unique
<ToolOutputFile
>(InputFileName
, FD
);
162 input_file
->os() << "";
164 const char *Args
[] = {"clang", "-target", "foobar", InputFileName
.c_str()};
166 auto Diags
= CompilerInstance::createDiagnostics(new DiagnosticOptions());
167 auto PCHContainerOps
= std::make_shared
<PCHContainerOperations
>();
168 std::unique_ptr
<clang::ASTUnit
> ErrUnit
;
170 std::unique_ptr
<ASTUnit
> AST
= ASTUnit::LoadFromCommandLine(
171 &Args
[0], &Args
[4], PCHContainerOps
, Diags
, "", false, "", false,
172 CaptureDiagsKind::All
, std::nullopt
, true, 0, TU_Complete
, false, false,
173 false, SkipFunctionBodiesScope::None
, false, true, false, false,
174 std::nullopt
, &ErrUnit
, nullptr);
176 ASSERT_EQ(AST
, nullptr);
177 ASSERT_NE(ErrUnit
, nullptr);
178 ASSERT_TRUE(Diags
->hasErrorOccurred());
179 ASSERT_NE(ErrUnit
->stored_diag_size(), 0U);
182 TEST_F(ASTUnitTest
, LoadFromCommandLineWorkingDirectory
) {
184 llvm::sys::fs::createTemporaryFile("bar", "c", FD
, InputFileName
));
185 auto Input
= std::make_unique
<ToolOutputFile
>(InputFileName
, FD
);
188 SmallString
<128> WorkingDir
;
189 ASSERT_FALSE(sys::fs::createUniqueDirectory("foo", WorkingDir
));
190 const char *Args
[] = {"clang", "-working-directory", WorkingDir
.c_str(),
191 InputFileName
.c_str()};
193 auto Diags
= CompilerInstance::createDiagnostics(new DiagnosticOptions());
194 auto PCHContainerOps
= std::make_shared
<PCHContainerOperations
>();
195 std::unique_ptr
<clang::ASTUnit
> ErrUnit
;
197 std::unique_ptr
<ASTUnit
> AST
= ASTUnit::LoadFromCommandLine(
198 &Args
[0], &Args
[4], PCHContainerOps
, Diags
, "", false, "", false,
199 CaptureDiagsKind::All
, std::nullopt
, true, 0, TU_Complete
, false, false,
200 false, SkipFunctionBodiesScope::None
, false, true, false, false,
201 std::nullopt
, &ErrUnit
, nullptr);
203 ASSERT_NE(AST
, nullptr);
204 ASSERT_FALSE(Diags
->hasErrorOccurred());
206 // Make sure '-working-directory' sets both the FileSystemOpts and underlying
207 // VFS working directory.
208 const auto &FM
= AST
->getFileManager();
209 const auto &VFS
= FM
.getVirtualFileSystem();
210 ASSERT_EQ(*VFS
.getCurrentWorkingDirectory(), WorkingDir
.str());
211 ASSERT_EQ(FM
.getFileSystemOpts().WorkingDir
, WorkingDir
.str());
214 } // anonymous namespace