Skip to content

Commit b8d2339

Browse files
fix: Check for in-project path if build-translated path check fails (#411)
Tested manually against a tweaked compilation database for Ninja. With this patch, a document is generated when the directory key points to a subdirectory, but the document is not generated without this patch. Will add a proper test case in a follow-up PR once the release is cut.
1 parent 0450ebe commit b8d2339

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

indexer/IdPathMappings.cc

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ bool FileMetadataMap::insert(clang::FileID fileId, AbsolutePathRef absPathRef) {
100100
return this->map.insert({{fileId}, std::move(metadata)}).second;
101101
};
102102

103+
bool checkInProjectPath = true;
104+
103105
if (optPackageMetadata.has_value()) {
106+
checkInProjectPath = false;
104107
if (auto optStrView =
105108
optPackageMetadata->rootPath.makeRelative(absPathRef)) {
106109
return insertRelPath(RootRelativePathRef(*optStrView, RootKind::External),
@@ -135,35 +138,51 @@ bool FileMetadataMap::insert(clang::FileID fileId, AbsolutePathRef absPathRef) {
135138
RootKind::Project),
136139
/*isInProject*/ true);
137140
} else {
138-
spdlog::trace("projectRoot.join(relativePath (= '{}'/'{}')) exists but "
139-
"the real path is '{}",
140-
this->projectRootPath.asRef().asStringView(),
141-
buildRootRelPath->asStringView(),
142-
llvm_ext::toStringView(realPath.str()));
141+
checkInProjectPath = false;
142+
spdlog::warn("projectRoot.join(relativePath (= '{}'/'{}')) exists but "
143+
"the real path is '{}",
144+
this->projectRootPath.asRef().asStringView(),
145+
buildRootRelPath->asStringView(),
146+
llvm_ext::toStringView(realPath.str()));
143147
}
144148
} else if (error == std::errc::no_such_file_or_directory) {
145-
spdlog::trace(
149+
spdlog::warn(
146150
"failed to find file in project at '{}' (root: '{}', rel: '{}')",
147151
originalFileSourcePath.asStringRef(),
148152
this->projectRootPath.asRef().asStringView(),
149153
buildRootRelPath->asStringView());
150154
} else {
151-
spdlog::trace("hit error: {} when getting real path for {}",
152-
error.message(), originalFileSourcePath.asStringRef());
155+
spdlog::warn("hit error: {} when getting real path for {}",
156+
error.message(), originalFileSourcePath.asStringRef());
153157
}
154-
} else if (auto optProjectRootRelPath =
155-
this->projectRootPath.tryMakeRelative(absPathRef)) {
156-
return insertRelPath(optProjectRootRelPath.value(), /*isInProject*/ true);
157-
} else {
158-
if ((spdlog::default_logger_raw()->level() <= spdlog::level::trace)
159-
&& (absPathRef.asStringView().find("usr/include") == std::string::npos)
160-
&& (absPathRef.asStringView().find("usr/lib/clang")
161-
== std::string::npos)) {
162-
spdlog::trace(
163-
"path {} is neither inside project root {} nor inside build root {}",
164-
absPathRef.asStringView(),
165-
this->projectRootPath.asRef().asStringView(),
166-
this->buildRootPath.asRef().asStringView());
158+
}
159+
160+
// For certain CMake projects, the "directory" key changes from TU to TU.
161+
// Consider the layout:
162+
// root/
163+
// \--- subdir/
164+
// \--- file.cpp
165+
// In this case, if the buildRootPath is root/subdir, then the first check
166+
// for the project file at root/file.cpp will fail. So we need to still
167+
// just check directory at root/subdir/file.cpp
168+
// TODO: We should simplify this logic to initially check if buildRootPath
169+
// is a subdir of project path or not, and write down all the possible cases.
170+
if (checkInProjectPath) {
171+
if (auto optProjectRootRelPath =
172+
this->projectRootPath.tryMakeRelative(absPathRef)) {
173+
return insertRelPath(optProjectRootRelPath.value(), /*isInProject*/ true);
174+
} else {
175+
if ((spdlog::default_logger_raw()->level() <= spdlog::level::trace)
176+
&& (absPathRef.asStringView().find("usr/include")
177+
== std::string::npos)
178+
&& (absPathRef.asStringView().find("usr/lib/clang")
179+
== std::string::npos)) {
180+
spdlog::trace("path {} is neither inside project root {} nor inside "
181+
"build root {}",
182+
absPathRef.asStringView(),
183+
this->projectRootPath.asRef().asStringView(),
184+
this->buildRootPath.asRef().asStringView());
185+
}
167186
}
168187
}
169188

0 commit comments

Comments
 (0)