forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 354
[clang] Fix ASTUnit working directory handling #7028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hamishknight
merged 2 commits into
swiftlang:stable/20221013
from
hamishknight:time-and-place
Jun 28, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| //====-- unittests/Frontend/ReparseWorkingDirTest.cpp - FrontendAction tests =// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "clang/Basic/Diagnostic.h" | ||
| #include "clang/Basic/FileManager.h" | ||
| #include "clang/Frontend/ASTUnit.h" | ||
| #include "clang/Frontend/CompilerInstance.h" | ||
| #include "clang/Frontend/CompilerInvocation.h" | ||
| #include "clang/Frontend/FrontendActions.h" | ||
| #include "clang/Frontend/FrontendOptions.h" | ||
| #include "clang/Lex/PreprocessorOptions.h" | ||
| #include "llvm/Support/FileSystem.h" | ||
| #include "llvm/Support/MemoryBuffer.h" | ||
| #include "llvm/Support/Path.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| using namespace llvm; | ||
| using namespace clang; | ||
|
|
||
| namespace { | ||
| class ReparseWorkingDirTest : public ::testing::Test { | ||
| IntrusiveRefCntPtr<vfs::InMemoryFileSystem> VFS; | ||
| std::shared_ptr<PCHContainerOperations> PCHContainerOpts; | ||
|
|
||
| public: | ||
| void SetUp() override { VFS = new vfs::InMemoryFileSystem(); } | ||
| void TearDown() override {} | ||
|
|
||
| void setWorkingDirectory(StringRef Path) { | ||
| VFS->setCurrentWorkingDirectory(Path); | ||
| } | ||
|
|
||
| void AddFile(const std::string &Filename, const std::string &Contents) { | ||
| ::time_t now; | ||
| ::time(&now); | ||
| VFS->addFile(Filename, now, | ||
| MemoryBuffer::getMemBufferCopy(Contents, Filename)); | ||
| } | ||
|
|
||
| std::unique_ptr<ASTUnit> ParseAST(StringRef EntryFile) { | ||
| PCHContainerOpts = std::make_shared<PCHContainerOperations>(); | ||
| auto CI = std::make_shared<CompilerInvocation>(); | ||
| CI->getFrontendOpts().Inputs.push_back(FrontendInputFile( | ||
| EntryFile, FrontendOptions::getInputKindForExtension( | ||
| llvm::sys::path::extension(EntryFile).substr(1)))); | ||
|
|
||
| CI->getHeaderSearchOpts().AddPath("headers", | ||
| frontend::IncludeDirGroup::Quoted, | ||
| /*isFramework*/ false, | ||
| /*IgnoreSysRoot*/ false); | ||
|
|
||
| CI->getFileSystemOpts().WorkingDir = *VFS->getCurrentWorkingDirectory(); | ||
| CI->getTargetOpts().Triple = "i386-unknown-linux-gnu"; | ||
|
|
||
| IntrusiveRefCntPtr<DiagnosticsEngine> Diags( | ||
| CompilerInstance::createDiagnostics(new DiagnosticOptions, | ||
| new DiagnosticConsumer)); | ||
|
|
||
| FileManager *FileMgr = new FileManager(CI->getFileSystemOpts(), VFS); | ||
|
|
||
| std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation( | ||
| CI, PCHContainerOpts, Diags, FileMgr, false, CaptureDiagsKind::None, | ||
| /*PrecompilePreambleAfterNParses=*/1); | ||
| return AST; | ||
| } | ||
|
|
||
| bool ReparseAST(const std::unique_ptr<ASTUnit> &AST) { | ||
| bool reparseFailed = | ||
| AST->Reparse(PCHContainerOpts, /*RemappedFiles*/ {}, VFS); | ||
| return !reparseFailed; | ||
| } | ||
| }; | ||
|
|
||
| TEST_F(ReparseWorkingDirTest, ReparseWorkingDir) { | ||
| // Setup the working directory path. We use '//root/' to allow the path to be | ||
| // valid on both Windows and Unix. We need the trailing slash for the path | ||
| // to be treated as absolute. | ||
| SmallString<16> WorkingDir; | ||
| llvm::sys::path::append(WorkingDir, "//root", | ||
| llvm::sys::path::get_separator()); | ||
| setWorkingDirectory(WorkingDir); | ||
|
|
||
| SmallString<32> Header; | ||
| llvm::sys::path::append(Header, WorkingDir, "headers", "header.h"); | ||
|
|
||
| SmallString<32> MainName; | ||
| llvm::sys::path::append(MainName, WorkingDir, "main.cpp"); | ||
|
|
||
| AddFile(MainName.str().str(), R"cpp( | ||
| #include "header.h" | ||
| int main() { return foo(); } | ||
| )cpp"); | ||
| AddFile(Header.str().str(), R"h( | ||
| static int foo() { return 0; } | ||
| )h"); | ||
|
|
||
| // Parse the main file, ensuring we can include the header. | ||
| std::unique_ptr<ASTUnit> AST(ParseAST(MainName.str())); | ||
| ASSERT_TRUE(AST.get()); | ||
| ASSERT_FALSE(AST->getDiagnostics().hasErrorOccurred()); | ||
|
|
||
| // Reparse and check that the working directory was preserved. | ||
| ASSERT_TRUE(ReparseAST(AST)); | ||
|
|
||
| const auto &FM = AST->getFileManager(); | ||
| const auto &FS = FM.getVirtualFileSystem(); | ||
| ASSERT_EQ(FM.getFileSystemOpts().WorkingDir, WorkingDir); | ||
| ASSERT_EQ(*FS.getCurrentWorkingDirectory(), WorkingDir); | ||
| } | ||
|
|
||
| } // end anonymous namespace |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.