[clangd][NFC] Use PathRef for getCorrespondingHeaderOrSource

This commit is contained in:
Nathan James 2021-01-07 02:40:20 +00:00
parent cfeecdf7b6
commit 3505d8dc07
4 changed files with 12 additions and 17 deletions

View File

@ -549,8 +549,8 @@ void ClangdServer::switchSourceHeader(
// the same directory.
// 2) if 1) fails, we use the AST&Index approach, it is slower but supports
// different code layout.
if (auto CorrespondingFile = getCorrespondingHeaderOrSource(
std::string(Path), TFS.view(llvm::None)))
if (auto CorrespondingFile =
getCorrespondingHeaderOrSource(Path, TFS.view(llvm::None)))
return CB(std::move(CorrespondingFile));
auto Action = [Path = Path.str(), CB = std::move(CB),
this](llvm::Expected<InputsAndAST> InpAST) mutable {

View File

@ -17,8 +17,7 @@ namespace clang {
namespace clangd {
llvm::Optional<Path> getCorrespondingHeaderOrSource(
const Path &OriginalFile,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
PathRef OriginalFile, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
llvm::StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx",
".c++", ".m", ".mm"};
llvm::StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"};
@ -51,25 +50,23 @@ llvm::Optional<Path> getCorrespondingHeaderOrSource(
NewExts = SourceExtensions;
// Storage for the new path.
llvm::SmallString<128> NewPath = llvm::StringRef(OriginalFile);
llvm::SmallString<128> NewPath = OriginalFile;
// Loop through switched extension candidates.
for (llvm::StringRef NewExt : NewExts) {
llvm::sys::path::replace_extension(NewPath, NewExt);
if (VFS->exists(NewPath))
return NewPath.str().str(); // First str() to convert from SmallString to
// StringRef, second to convert from StringRef
// to std::string
return Path(NewPath);
// Also check NewExt in upper-case, just in case.
llvm::sys::path::replace_extension(NewPath, NewExt.upper());
if (VFS->exists(NewPath))
return NewPath.str().str();
return Path(NewPath);
}
return None;
}
llvm::Optional<Path> getCorrespondingHeaderOrSource(const Path &OriginalFile,
llvm::Optional<Path> getCorrespondingHeaderOrSource(PathRef OriginalFile,
ParsedAST &AST,
const SymbolIndex *Index) {
if (!Index) {
@ -121,7 +118,7 @@ llvm::Optional<Path> getCorrespondingHeaderOrSource(const Path &OriginalFile,
// candidates.
Best = It;
}
return Path(std::string(Best->first()));
return Path(Best->first());
}
std::vector<const Decl *> getIndexableLocalDecls(ParsedAST &AST) {

View File

@ -18,12 +18,11 @@ namespace clangd {
/// Given a header file, returns the best matching source file, and vice visa.
/// It only uses the filename heuristics to do the inference.
llvm::Optional<Path> getCorrespondingHeaderOrSource(
const Path &OriginalFile,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS);
PathRef OriginalFile, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS);
/// Given a header file, returns the best matching source file, and vice visa.
/// The heuristics incorporate with the AST and the index (if provided).
llvm::Optional<Path> getCorrespondingHeaderOrSource(const Path &OriginalFile,
llvm::Optional<Path> getCorrespondingHeaderOrSource(PathRef OriginalFile,
ParsedAST &AST,
const SymbolIndex *Index);

View File

@ -65,11 +65,10 @@ const FunctionDecl *getSelectedFunction(const SelectionTree::Node *SelNode) {
llvm::Optional<Path> getSourceFile(llvm::StringRef FileName,
const Tweak::Selection &Sel) {
if (auto Source = getCorrespondingHeaderOrSource(
std::string(FileName),
FileName,
&Sel.AST->getSourceManager().getFileManager().getVirtualFileSystem()))
return *Source;
return getCorrespondingHeaderOrSource(std::string(FileName), *Sel.AST,
Sel.Index);
return getCorrespondingHeaderOrSource(FileName, *Sel.AST, Sel.Index);
}
// Synthesize a DeclContext for TargetNS from CurContext. TargetNS must be empty