Skip to content

Commit c77c3d1

Browse files
committed
[clangd] Set correct CWD when using compile_flags.txt
This fixes a bug where clangd would attempt to set CWD to the compile_flags.txt file itself. Differential Revision: https://reviews.llvm.org/D94699
1 parent aeaeb9e commit c77c3d1

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

clang-tools-extra/clangd/GlobalCompilationDatabase.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ parseJSON(PathRef Path, llvm::StringRef Data, std::string &Error) {
271271
}
272272
static std::unique_ptr<tooling::CompilationDatabase>
273273
parseFixed(PathRef Path, llvm::StringRef Data, std::string &Error) {
274-
return tooling::FixedCompilationDatabase::loadFromBuffer(Path, Data, Error);
274+
return tooling::FixedCompilationDatabase::loadFromBuffer(
275+
llvm::sys::path::parent_path(Path), Data, Error);
275276
}
276277

277278
bool DirectoryBasedGlobalCompilationDatabase::DirectoryCache::load(

clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,17 @@ TEST(GlobalCompilationDatabaseTest, BuildDir) {
279279
<< "x/build/compile_flags.json only applicable to x/";
280280
}
281281

282+
TEST(GlobalCompilationDatabaseTest, CompileFlagsDirectory) {
283+
MockFS FS;
284+
FS.Files[testPath("x/compile_flags.txt")] = "-DFOO";
285+
DirectoryBasedGlobalCompilationDatabase CDB(FS);
286+
auto Commands = CDB.getCompileCommand(testPath("x/y.cpp"));
287+
ASSERT_TRUE(Commands.hasValue());
288+
EXPECT_THAT(Commands.getValue().CommandLine, Contains("-DFOO"));
289+
// Make sure we pick the right working directory.
290+
EXPECT_EQ(testPath("x"), Commands.getValue().Directory);
291+
}
292+
282293
TEST(GlobalCompilationDatabaseTest, NonCanonicalFilenames) {
283294
OverlayCDB DB(nullptr);
284295
std::vector<std::string> DiscoveredFiles;

0 commit comments

Comments
 (0)