[clangd] Use FileManager for getCanonicalPath, NFC
authorHaojian Wu <hokein.wu@gmail.com>
Fri, 2 Jun 2023 10:12:54 +0000 (12:12 +0200)
committerHaojian Wu <hokein.wu@gmail.com>
Fri, 2 Jun 2023 10:55:21 +0000 (12:55 +0200)
get rid of the SourceManager dependency -- getCanonicalPath doesn't use
other SourceManager fields.

clang-tools-extra/clangd/Diagnostics.cpp
clang-tools-extra/clangd/IncludeCleaner.cpp
clang-tools-extra/clangd/SourceCode.cpp
clang-tools-extra/clangd/SourceCode.h
clang-tools-extra/clangd/XRefs.cpp
clang-tools-extra/clangd/index/Background.cpp
clang-tools-extra/clangd/index/SymbolCollector.cpp
clang-tools-extra/clangd/indexer/IndexerMain.cpp
clang-tools-extra/clangd/refactor/Tweak.cpp

index 4c5def3..bae528a 100644 (file)
@@ -710,7 +710,7 @@ void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
     auto FID = SM.getFileID(Info.getLocation());
     if (const auto FE = SM.getFileEntryRefForID(FID)) {
       D.File = FE->getName().str();
-      D.AbsFile = getCanonicalPath(*FE, SM);
+      D.AbsFile = getCanonicalPath(*FE, SM.getFileManager());
     }
     D.ID = Info.getID();
     return D;
index 18be132..a3e08bc 100644 (file)
@@ -336,7 +336,7 @@ std::string spellHeader(ParsedAST &AST, const FileEntry *MainFile,
                         include_cleaner::Header Provider) {
   if (Provider.kind() == include_cleaner::Header::Physical) {
     if (auto CanonicalPath = getCanonicalPath(Provider.physical()->getLastRef(),
-                                              AST.getSourceManager())) {
+                                              AST.getSourceManager().getFileManager())) {
       std::string SpelledHeader =
           llvm::cantFail(URI::includeSpelling(URI::create(*CanonicalPath)));
       if (!SpelledHeader.empty())
index 831adc3..c460ae3 100644 (file)
@@ -514,11 +514,11 @@ std::vector<TextEdit> replacementsToEdits(llvm::StringRef Code,
 }
 
 std::optional<std::string> getCanonicalPath(const FileEntryRef F,
-                                            const SourceManager &SourceMgr) {
+                                            FileManager &FileMgr) {
   llvm::SmallString<128> FilePath = F.getName();
   if (!llvm::sys::path::is_absolute(FilePath)) {
     if (auto EC =
-            SourceMgr.getFileManager().getVirtualFileSystem().makeAbsolute(
+            FileMgr.getVirtualFileSystem().makeAbsolute(
                 FilePath)) {
       elog("Could not turn relative path '{0}' to absolute: {1}", FilePath,
            EC.message());
@@ -537,10 +537,10 @@ std::optional<std::string> getCanonicalPath(const FileEntryRef F,
   //
   //  The file path of Symbol is "/project/src/foo.h" instead of
   //  "/tmp/build/foo.h"
-  if (auto Dir = SourceMgr.getFileManager().getDirectory(
+  if (auto Dir = FileMgr.getDirectory(
           llvm::sys::path::parent_path(FilePath))) {
     llvm::SmallString<128> RealPath;
-    llvm::StringRef DirName = SourceMgr.getFileManager().getCanonicalName(*Dir);
+    llvm::StringRef DirName = FileMgr.getCanonicalName(*Dir);
     llvm::sys::path::append(RealPath, DirName,
                             llvm::sys::path::filename(FilePath));
     return RealPath.str().str();
index 8b7c028..3ba6f8b 100644 (file)
@@ -164,7 +164,7 @@ TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M,
 /// component that generate it, so that paths are normalized as much as
 /// possible.
 std::optional<std::string> getCanonicalPath(const FileEntryRef F,
-                                            const SourceManager &SourceMgr);
+                                            FileManager &FileMgr);
 
 /// Choose the clang-format style we should apply to a certain file.
 /// This will usually use FS to look for .clang-format directories.
index 51a3ef8..ad4819f 100644 (file)
@@ -217,7 +217,7 @@ std::optional<Location> makeLocation(const ASTContext &AST, SourceLocation Loc,
   const auto F = SM.getFileEntryRefForID(SM.getFileID(Loc));
   if (!F)
     return std::nullopt;
-  auto FilePath = getCanonicalPath(*F, SM);
+  auto FilePath = getCanonicalPath(*F, SM.getFileManager());
   if (!FilePath) {
     log("failed to get path!");
     return std::nullopt;
@@ -1688,7 +1688,7 @@ declToHierarchyItem(const NamedDecl &ND, llvm::StringRef TUPath) {
   const auto FE = SM.getFileEntryRefForID(SM.getFileID(NameLoc));
   if (!FE)
     return std::nullopt;
-  auto FilePath = getCanonicalPath(*FE, SM);
+  auto FilePath = getCanonicalPath(*FE, SM.getFileManager());
   if (!FilePath)
     return std::nullopt; // Not useful without a uri.
 
index 73330b3..c35de75 100644 (file)
@@ -291,7 +291,7 @@ llvm::Error BackgroundIndex::index(tooling::CompileCommand Cmd) {
     const auto F = SM.getFileEntryRefForID(FID);
     if (!F)
       return false; // Skip invalid files.
-    auto AbsPath = getCanonicalPath(*F, SM);
+    auto AbsPath = getCanonicalPath(*F, SM.getFileManager());
     if (!AbsPath)
       return false; // Skip files without absolute path.
     auto Digest = digestFile(SM, FID);
index d1840ff..131d0a3 100644 (file)
@@ -208,7 +208,7 @@ public:
   const std::string &toURI(const FileEntryRef FE) {
     auto R = CacheFEToURI.try_emplace(FE);
     if (R.second) {
-      auto CanonPath = getCanonicalPath(FE, SM);
+      auto CanonPath = getCanonicalPath(FE, SM.getFileManager());
       R.first->second = &toURIInternal(CanonPath ? *CanonPath : FE.getName());
     }
     return *R.first->second;
index a66ab91..2717030 100644 (file)
@@ -49,7 +49,7 @@ public:
       const auto F = SM.getFileEntryRefForID(FID);
       if (!F)
         return false; // Skip invalid files.
-      auto AbsPath = getCanonicalPath(*F, SM);
+      auto AbsPath = getCanonicalPath(*F, SM.getFileManager());
       if (!AbsPath)
         return false; // Skip files without absolute path.
       std::lock_guard<std::mutex> Lock(FilesMu);
index bfa8bf6..840843d 100644 (file)
@@ -105,7 +105,7 @@ Tweak::Effect::fileEdit(const SourceManager &SM, FileID FID,
                         tooling::Replacements Replacements) {
   Edit Ed(SM.getBufferData(FID), std::move(Replacements));
   if (const auto FE = SM.getFileEntryRefForID(FID))
-    if (auto FilePath = getCanonicalPath(*FE, SM))
+    if (auto FilePath = getCanonicalPath(*FE, SM.getFileManager()))
       return std::make_pair(*FilePath, std::move(Ed));
   return error("Failed to get absolute path for edited file: {0}",
                SM.getFileEntryRefForID(FID)->getName());