[clang-tools-extra] r367617 - [clang-tools-extra] Adopt FileManager's error-returning APIs

Harlan Haskins via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 1 14:32:01 PDT 2019


Author: harlanhaskins
Date: Thu Aug  1 14:32:01 2019
New Revision: 367617

URL: http://llvm.org/viewvc/llvm-project?rev=367617&view=rev
Log:
[clang-tools-extra] Adopt FileManager's error-returning APIs

The FileManager has been updated to return llvm::ErrorOr from getFile
and getDirectory, this commit updates all the callers of those APIs from
clang.

Modified:
    clang-tools-extra/trunk/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
    clang-tools-extra/trunk/clang-change-namespace/tool/ClangChangeNamespace.cpp
    clang-tools-extra/trunk/clang-include-fixer/IncludeFixer.cpp
    clang-tools-extra/trunk/clang-move/Move.cpp
    clang-tools-extra/trunk/clang-move/tool/ClangMove.cpp
    clang-tools-extra/trunk/clang-reorder-fields/tool/ClangReorderFields.cpp
    clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp
    clang-tools-extra/trunk/clangd/ClangdUnit.cpp
    clang-tools-extra/trunk/clangd/SourceCode.cpp
    clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp
    clang-tools-extra/trunk/modularize/ModularizeUtilities.cpp

Modified: clang-tools-extra/trunk/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp?rev=367617&r1=367616&r2=367617&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp (original)
+++ clang-tools-extra/trunk/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp Thu Aug  1 14:32:01 2019
@@ -151,13 +151,13 @@ groupReplacements(const TUReplacements &
   auto AddToGroup = [&](const tooling::Replacement &R, bool FromDiag) {
     // Use the file manager to deduplicate paths. FileEntries are
     // automatically canonicalized.
-    if (const FileEntry *Entry = SM.getFileManager().getFile(R.getFilePath())) {
+    if (auto Entry = SM.getFileManager().getFile(R.getFilePath())) {
       if (FromDiag) {
-        auto &Replaces = DiagReplacements[Entry];
+        auto &Replaces = DiagReplacements[*Entry];
         if (!Replaces.insert(R).second)
           return;
       }
-      GroupedReplacements[Entry].push_back(R);
+      GroupedReplacements[*Entry].push_back(R);
     } else if (Warned.insert(R.getFilePath()).second) {
       errs() << "Described file '" << R.getFilePath()
              << "' doesn't exist. Ignoring...\n";

Modified: clang-tools-extra/trunk/clang-change-namespace/tool/ClangChangeNamespace.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-change-namespace/tool/ClangChangeNamespace.cpp?rev=367617&r1=367616&r2=367617&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-change-namespace/tool/ClangChangeNamespace.cpp (original)
+++ clang-tools-extra/trunk/clang-change-namespace/tool/ClangChangeNamespace.cpp Thu Aug  1 14:32:01 2019
@@ -147,8 +147,8 @@ int main(int argc, const char **argv) {
       for (auto I = ChangedFiles.begin(), E = ChangedFiles.end(); I != E; ++I) {
         OS << "  {\n";
         OS << "    \"FilePath\": \"" << *I << "\",\n";
-        const auto *Entry = FileMgr.getFile(*I);
-        auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User);
+        const auto Entry = FileMgr.getFile(*I);
+        auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User);
         std::string Content;
         llvm::raw_string_ostream ContentStream(Content);
         Rewrite.getEditBuffer(ID).write(ContentStream);
@@ -165,9 +165,9 @@ int main(int argc, const char **argv) {
   }
 
   for (const auto &File : ChangedFiles) {
-    const auto *Entry = FileMgr.getFile(File);
+    const auto Entry = FileMgr.getFile(File);
 
-    auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User);
+    auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User);
     outs() << "============== " << File << " ==============\n";
     Rewrite.getEditBuffer(ID).write(llvm::outs());
     outs() << "\n============================================\n";

Modified: clang-tools-extra/trunk/clang-include-fixer/IncludeFixer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-include-fixer/IncludeFixer.cpp?rev=367617&r1=367616&r2=367617&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-include-fixer/IncludeFixer.cpp (original)
+++ clang-tools-extra/trunk/clang-include-fixer/IncludeFixer.cpp Thu Aug  1 14:32:01 2019
@@ -306,8 +306,7 @@ std::string IncludeFixerSemaSource::mini
 
   // Get the FileEntry for the include.
   StringRef StrippedInclude = Include.trim("\"<>");
-  const FileEntry *Entry =
-      SourceManager.getFileManager().getFile(StrippedInclude);
+  auto Entry = SourceManager.getFileManager().getFile(StrippedInclude);
 
   // If the file doesn't exist return the path from the database.
   // FIXME: This should never happen.
@@ -316,7 +315,7 @@ std::string IncludeFixerSemaSource::mini
 
   bool IsSystem = false;
   std::string Suggestion =
-      HeaderSearch.suggestPathToFileForDiagnostics(Entry, "", &IsSystem);
+      HeaderSearch.suggestPathToFileForDiagnostics(*Entry, "", &IsSystem);
 
   return IsSystem ? '<' + Suggestion + '>' : '"' + Suggestion + '"';
 }

Modified: clang-tools-extra/trunk/clang-move/Move.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-move/Move.cpp?rev=367617&r1=367616&r2=367617&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-move/Move.cpp (original)
+++ clang-tools-extra/trunk/clang-move/Move.cpp Thu Aug  1 14:32:01 2019
@@ -92,10 +92,10 @@ std::string MakeAbsolutePath(const Sourc
                  << '\n';
   // Handle symbolic link path cases.
   // We are trying to get the real file path of the symlink.
-  const DirectoryEntry *Dir = SM.getFileManager().getDirectory(
+  auto Dir = SM.getFileManager().getDirectory(
       llvm::sys::path::parent_path(AbsolutePath.str()));
   if (Dir) {
-    StringRef DirName = SM.getFileManager().getCanonicalName(Dir);
+    StringRef DirName = SM.getFileManager().getCanonicalName(*Dir);
     // FIXME: getCanonicalName might fail to get real path on VFS.
     if (llvm::sys::path::is_absolute(DirName)) {
       SmallString<128> AbsoluteFilename;
@@ -115,7 +115,7 @@ AST_POLYMORPHIC_MATCHER_P(isExpansionInF
   auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getBeginLoc());
   if (ExpansionLoc.isInvalid())
     return false;
-  auto FileEntry =
+  auto *FileEntry =
       SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc));
   if (!FileEntry)
     return false;
@@ -842,12 +842,12 @@ void ClangMoveTool::moveDeclsToNewFiles(
 // Move all contents from OldFile to NewFile.
 void ClangMoveTool::moveAll(SourceManager &SM, StringRef OldFile,
                             StringRef NewFile) {
-  const FileEntry *FE = SM.getFileManager().getFile(makeAbsolutePath(OldFile));
+  auto FE = SM.getFileManager().getFile(makeAbsolutePath(OldFile));
   if (!FE) {
     llvm::errs() << "Failed to get file: " << OldFile << "\n";
     return;
   }
-  FileID ID = SM.getOrCreateFileID(FE, SrcMgr::C_User);
+  FileID ID = SM.getOrCreateFileID(*FE, SrcMgr::C_User);
   auto Begin = SM.getLocForStartOfFile(ID);
   auto End = SM.getLocForEndOfFile(ID);
   tooling::Replacement RemoveAll(SM, CharSourceRange::getCharRange(Begin, End),

Modified: clang-tools-extra/trunk/clang-move/tool/ClangMove.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-move/tool/ClangMove.cpp?rev=367617&r1=367616&r2=367617&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-move/tool/ClangMove.cpp (original)
+++ clang-tools-extra/trunk/clang-move/tool/ClangMove.cpp Thu Aug  1 14:32:01 2019
@@ -191,8 +191,8 @@ int main(int argc, const char **argv) {
       for (auto I = Files.begin(), E = Files.end(); I != E; ++I) {
         OS << "  {\n";
         OS << "    \"FilePath\": \"" << *I << "\",\n";
-        const auto *Entry = FileMgr.getFile(*I);
-        auto ID = SM.translateFile(Entry);
+        const auto Entry = FileMgr.getFile(*I);
+        auto ID = SM.translateFile(*Entry);
         std::string Content;
         llvm::raw_string_ostream ContentStream(Content);
         Rewrite.getEditBuffer(ID).write(ContentStream);

Modified: clang-tools-extra/trunk/clang-reorder-fields/tool/ClangReorderFields.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-reorder-fields/tool/ClangReorderFields.cpp?rev=367617&r1=367616&r2=367617&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-reorder-fields/tool/ClangReorderFields.cpp (original)
+++ clang-tools-extra/trunk/clang-reorder-fields/tool/ClangReorderFields.cpp Thu Aug  1 14:32:01 2019
@@ -78,8 +78,8 @@ int main(int argc, const char **argv) {
   Tool.applyAllReplacements(Rewrite);
 
   for (const auto &File : Files) {
-    const auto *Entry = FileMgr.getFile(File);
-    const auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User);
+    auto Entry = FileMgr.getFile(File);
+    const auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User);
     Rewrite.getEditBuffer(ID).write(outs());
   }
 

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp?rev=367617&r1=367616&r2=367617&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp Thu Aug  1 14:32:01 2019
@@ -236,8 +236,11 @@ private:
     if (FilePath.empty())
       return SourceLocation();
 
-    const FileEntry *File = SourceMgr.getFileManager().getFile(FilePath);
-    FileID ID = SourceMgr.getOrCreateFileID(File, SrcMgr::C_User);
+    auto File = SourceMgr.getFileManager().getFile(FilePath);
+    if (!File)
+      return SourceLocation();
+
+    FileID ID = SourceMgr.getOrCreateFileID(*File, SrcMgr::C_User);
     return SourceMgr.getLocForStartOfFile(ID).getLocWithOffset(Offset);
   }
 

Modified: clang-tools-extra/trunk/clangd/ClangdUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdUnit.cpp?rev=367617&r1=367616&r2=367617&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdUnit.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdUnit.cpp Thu Aug  1 14:32:01 2019
@@ -236,7 +236,8 @@ private:
     for (const auto &Inc : Includes.MainFileIncludes) {
       const FileEntry *File = nullptr;
       if (Inc.Resolved != "")
-        File = SM.getFileManager().getFile(Inc.Resolved);
+        if (auto FE = SM.getFileManager().getFile(Inc.Resolved))
+          File = *FE;
 
       llvm::StringRef WrittenFilename =
           llvm::StringRef(Inc.Written).drop_front().drop_back();

Modified: clang-tools-extra/trunk/clangd/SourceCode.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SourceCode.cpp?rev=367617&r1=367616&r2=367617&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/SourceCode.cpp (original)
+++ clang-tools-extra/trunk/clangd/SourceCode.cpp Thu Aug  1 14:32:01 2019
@@ -442,10 +442,10 @@ llvm::Optional<std::string> getCanonical
   //
   //  The file path of Symbol is "/project/src/foo.h" instead of
   //  "/tmp/build/foo.h"
-  if (const DirectoryEntry *Dir = SourceMgr.getFileManager().getDirectory(
+  if (auto Dir = SourceMgr.getFileManager().getDirectory(
           llvm::sys::path::parent_path(FilePath))) {
     llvm::SmallString<128> RealPath;
-    llvm::StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir);
+    llvm::StringRef DirName = SourceMgr.getFileManager().getCanonicalName(*Dir);
     llvm::sys::path::append(RealPath, DirName,
                             llvm::sys::path::filename(FilePath));
     return RealPath.str().str();

Modified: clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp?rev=367617&r1=367616&r2=367617&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp (original)
+++ clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp Thu Aug  1 14:32:01 2019
@@ -56,9 +56,10 @@ const NamedDecl &getTemplateOrThis(const
 std::string toURI(const SourceManager &SM, llvm::StringRef Path,
                   const SymbolCollector::Options &Opts) {
   llvm::SmallString<128> AbsolutePath(Path);
-  if (auto CanonPath =
-          getCanonicalPath(SM.getFileManager().getFile(Path), SM)) {
-    AbsolutePath = *CanonPath;
+  if (auto File = SM.getFileManager().getFile(Path)) {
+    if (auto CanonPath = getCanonicalPath(*File, SM)) {
+      AbsolutePath = *CanonPath;
+    }
   }
   // We don't perform is_absolute check in an else branch because makeAbsolute
   // might return a relative path on some InMemoryFileSystems.

Modified: clang-tools-extra/trunk/modularize/ModularizeUtilities.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/modularize/ModularizeUtilities.cpp?rev=367617&r1=367616&r2=367617&view=diff
==============================================================================
--- clang-tools-extra/trunk/modularize/ModularizeUtilities.cpp (original)
+++ clang-tools-extra/trunk/modularize/ModularizeUtilities.cpp Thu Aug  1 14:32:01 2019
@@ -258,14 +258,15 @@ std::error_code ModularizeUtilities::loa
 std::error_code ModularizeUtilities::loadModuleMap(
     llvm::StringRef InputPath) {
   // Get file entry for module.modulemap file.
-  const FileEntry *ModuleMapEntry =
+  auto ModuleMapEntryOrErr =
     SourceMgr->getFileManager().getFile(InputPath);
 
   // return error if not found.
-  if (!ModuleMapEntry) {
+  if (!ModuleMapEntryOrErr) {
     llvm::errs() << "error: File \"" << InputPath << "\" not found.\n";
-    return std::error_code(1, std::generic_category());
+    return ModuleMapEntryOrErr.getError();
   }
+  const FileEntry *ModuleMapEntry = *ModuleMapEntryOrErr;
 
   // Because the module map parser uses a ForwardingDiagnosticConsumer,
   // which doesn't forward the BeginSourceFile call, we do it explicitly here.
@@ -276,8 +277,12 @@ std::error_code ModularizeUtilities::loa
   StringRef DirName(Dir->getName());
   if (llvm::sys::path::filename(DirName) == "Modules") {
     DirName = llvm::sys::path::parent_path(DirName);
-    if (DirName.endswith(".framework"))
-      Dir = FileMgr->getDirectory(DirName);
+    if (DirName.endswith(".framework")) {
+      if (auto DirEntry = FileMgr->getDirectory(DirName))
+        Dir = *DirEntry;
+      else
+        Dir = nullptr;
+    }
     // FIXME: This assert can fail if there's a race between the above check
     // and the removal of the directory.
     assert(Dir && "parent must exist");




More information about the cfe-commits mailing list