[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. llvm-svn: 367617
This commit is contained in:
parent
8d323d1506
commit
a02f85768d
@ -151,13 +151,13 @@ groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs,
|
||||
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";
|
||||
|
@ -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";
|
||||
|
@ -306,8 +306,7 @@ std::string IncludeFixerSemaSource::minimizeInclude(
|
||||
|
||||
// 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::minimizeInclude(
|
||||
|
||||
bool IsSystem = false;
|
||||
std::string Suggestion =
|
||||
HeaderSearch.suggestPathToFileForDiagnostics(Entry, "", &IsSystem);
|
||||
HeaderSearch.suggestPathToFileForDiagnostics(*Entry, "", &IsSystem);
|
||||
|
||||
return IsSystem ? '<' + Suggestion + '>' : '"' + Suggestion + '"';
|
||||
}
|
||||
|
@ -92,10 +92,10 @@ std::string MakeAbsolutePath(const SourceManager &SM, StringRef Path) {
|
||||
<< '\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(isExpansionInFile,
|
||||
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),
|
||||
|
@ -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);
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
@ -442,10 +442,10 @@ llvm::Optional<std::string> getCanonicalPath(const FileEntry *F,
|
||||
//
|
||||
// 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();
|
||||
|
@ -56,9 +56,10 @@ const NamedDecl &getTemplateOrThis(const NamedDecl &ND) {
|
||||
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.
|
||||
|
@ -258,14 +258,15 @@ std::error_code ModularizeUtilities::loadProblemHeaderList(
|
||||
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::loadModuleMap(
|
||||
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");
|
||||
|
Loading…
x
Reference in New Issue
Block a user