[llvm-branch-commits] [clang-tools-extra] 3505d8d - [clangd][NFC] Use PathRef for getCorrespondingHeaderOrSource

Nathan James via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jan 6 18:46:22 PST 2021


Author: Nathan James
Date: 2021-01-07T02:41:19Z
New Revision: 3505d8dc07427b3d9165538d8f1cee574ea66804

URL: https://github.com/llvm/llvm-project/commit/3505d8dc07427b3d9165538d8f1cee574ea66804
DIFF: https://github.com/llvm/llvm-project/commit/3505d8dc07427b3d9165538d8f1cee574ea66804.diff

LOG: [clangd][NFC] Use PathRef for getCorrespondingHeaderOrSource

Added: 
    

Modified: 
    clang-tools-extra/clangd/ClangdServer.cpp
    clang-tools-extra/clangd/HeaderSourceSwitch.cpp
    clang-tools-extra/clangd/HeaderSourceSwitch.h
    clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp
index b760b31c0b87..d5e21cfb063e 100644
--- a/clang-tools-extra/clangd/ClangdServer.cpp
+++ b/clang-tools-extra/clangd/ClangdServer.cpp
@@ -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
   //     
diff erent 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 {

diff  --git a/clang-tools-extra/clangd/HeaderSourceSwitch.cpp b/clang-tools-extra/clangd/HeaderSourceSwitch.cpp
index d6b0b98f8d42..cd493a72b242 100644
--- a/clang-tools-extra/clangd/HeaderSourceSwitch.cpp
+++ b/clang-tools-extra/clangd/HeaderSourceSwitch.cpp
@@ -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) {

diff  --git a/clang-tools-extra/clangd/HeaderSourceSwitch.h b/clang-tools-extra/clangd/HeaderSourceSwitch.h
index a971b385d74c..8cdd3f028280 100644
--- a/clang-tools-extra/clangd/HeaderSourceSwitch.h
+++ b/clang-tools-extra/clangd/HeaderSourceSwitch.h
@@ -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);
 

diff  --git a/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp b/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
index 6f7ad2e2a541..7a40ffae3d1c 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
@@ -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


        


More information about the llvm-branch-commits mailing list