-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
[include-cleaner] Pass WorkingDir to suggestPathToFileForDiagnostics #95114
Merged
Conversation
This file contains 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
|
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang Author: kadir çetinkaya (kadircet) ChangesAddresses #81215. Full diff: https://github.com/llvm/llvm-project/pull/95114.diff 4 Files Affected:
diff --git a/clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp b/clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
index 2073f0a1d3d87..8332eb685d652 100644
--- a/clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
+++ b/clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
@@ -9,6 +9,7 @@
#include "clang-include-cleaner/IncludeSpeller.h"
#include "clang-include-cleaner/Types.h"
#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringRef.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Registry.h"
#include <memory>
@@ -30,8 +31,14 @@ class DefaultIncludeSpeller : public IncludeSpeller {
return Input.H.verbatim().str();
case Header::Physical:
bool IsAngled = false;
+ std::string WorkingDir;
+ if (auto WD = Input.HS.getFileMgr()
+ .getVirtualFileSystem()
+ .getCurrentWorkingDirectory())
+ WorkingDir = *WD;
std::string FinalSpelling = Input.HS.suggestPathToFileForDiagnostics(
- Input.H.physical(), Input.Main->tryGetRealPathName(), &IsAngled);
+ Input.H.resolvedPath(), WorkingDir, Input.Main->tryGetRealPathName(),
+ &IsAngled);
return IsAngled ? "<" + FinalSpelling + ">" : "\"" + FinalSpelling + "\"";
}
llvm_unreachable("Unknown clang::include_cleaner::Header::Kind enum");
@@ -60,4 +67,4 @@ std::string spellHeader(const IncludeSpeller::Input &Input) {
return Spelling;
}
-} // namespace clang::include_cleaner
\ No newline at end of file
+} // namespace clang::include_cleaner
diff --git a/clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp b/clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp
index a548868071a12..8f6ad09c46cc4 100644
--- a/clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp
@@ -89,6 +89,24 @@ TEST(IncludeSpeller, CanOverrideSystemHeaders) {
HS, MainFile}));
}
+TEST(IncludeSpeller, RelativeIncludeSearchPath) {
+ TestInputs Inputs;
+
+ Inputs.WorkingDir = "/root/inner";
+ Inputs.ExtraArgs.push_back("-I..");
+ Inputs.ExtraFiles["/root/foo.h"] = "";
+ TestAST AST{Inputs};
+
+ auto &FM = AST.fileManager();
+ auto &HS = AST.preprocessor().getHeaderSearchInfo();
+ const auto *MainFile = AST.sourceManager().getFileEntryForID(
+ AST.sourceManager().getMainFileID());
+
+ EXPECT_EQ("\"foo.h\"",
+ spellHeader(
+ {Header{*FM.getOptionalFileRef("/root/foo.h")}, HS, MainFile}));
+}
+
IncludeSpellingStrategy::Add<DummyIncludeSpeller>
Speller("dummy", "Dummy Include Speller");
diff --git a/clang/include/clang/Testing/TestAST.h b/clang/include/clang/Testing/TestAST.h
index 845e31f65438b..8878bfbe16984 100644
--- a/clang/include/clang/Testing/TestAST.h
+++ b/clang/include/clang/Testing/TestAST.h
@@ -49,6 +49,10 @@ struct TestInputs {
/// Keys are plain filenames ("foo.h"), values are file content.
llvm::StringMap<std::string> ExtraFiles = {};
+ /// Root of execution, all relative paths in Args/Files are resolved against
+ /// this.
+ std::string WorkingDir;
+
/// Filename to use for translation unit. A default will be used when empty.
std::string FileName;
diff --git a/clang/lib/Testing/TestAST.cpp b/clang/lib/Testing/TestAST.cpp
index 3a50c2d9b5d05..fe8b93851613d 100644
--- a/clang/lib/Testing/TestAST.cpp
+++ b/clang/lib/Testing/TestAST.cpp
@@ -13,6 +13,7 @@
#include "clang/Frontend/TextDiagnostic.h"
#include "clang/Testing/CommandLineArgs.h"
#include "llvm/ADT/ScopeExit.h"
+#include "llvm/Support/Error.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "gtest/gtest.h"
@@ -106,6 +107,8 @@ TestAST::TestAST(const TestInputs &In) {
// Set up a VFS with only the virtual file visible.
auto VFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
+ if (auto Err = VFS->setCurrentWorkingDirectory(In.WorkingDir))
+ ADD_FAILURE() << "Failed to setWD: " << Err.message();
VFS->addFile(Filename, /*ModificationTime=*/0,
llvm::MemoryBuffer::getMemBufferCopy(In.Code, Filename));
for (const auto &Extra : In.ExtraFiles)
|
hokein
approved these changes
Jun 14, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Addresses #81215.