Skip to content

Commit

Permalink
[clang] NFCI: Use FileEntryRef in suggestPathToFileForDiagnostics()
Browse files Browse the repository at this point in the history
  • Loading branch information
jansvoboda11 committed Sep 10, 2023
1 parent 86f21e9 commit 6966c06
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 16 deletions.
3 changes: 2 additions & 1 deletion clang-tools-extra/clang-include-fixer/IncludeFixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ std::string IncludeFixerSemaSource::minimizeInclude(

// Get the FileEntry for the include.
StringRef StrippedInclude = Include.trim("\"<>");
auto Entry = SourceManager.getFileManager().getFile(StrippedInclude);
auto Entry =
SourceManager.getFileManager().getOptionalFileRef(StrippedInclude);

// If the file doesn't exist return the path from the database.
// FIXME: This should never happen.
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Lex/HeaderSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ class HeaderSearch {
///
/// \param IsAngled If non-null, filled in to indicate whether the suggested
/// path should be referenced as <Header.h> instead of "Header.h".
std::string suggestPathToFileForDiagnostics(const FileEntry *File,
std::string suggestPathToFileForDiagnostics(FileEntryRef File,
llvm::StringRef MainFile,
bool *IsAngled = nullptr) const;

Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/Lex/Preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2718,8 +2718,8 @@ class Preprocessor {
/// \return A file that can be #included to provide the desired effect. Null
/// if no such file could be determined or if a #include is not
/// appropriate (eg, if a module should be imported instead).
const FileEntry *getHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
SourceLocation MLoc);
OptionalFileEntryRef getHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
SourceLocation MLoc);

bool isRecordingPreamble() const {
return PreambleConditionalStack.isRecording();
Expand Down
7 changes: 2 additions & 5 deletions clang/lib/Lex/HeaderSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1923,11 +1923,8 @@ void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
}

std::string HeaderSearch::suggestPathToFileForDiagnostics(
const FileEntry *File, llvm::StringRef MainFile, bool *IsAngled) const {
// FIXME: We assume that the path name currently cached in the FileEntry is
// the most appropriate one for this analysis (and that it's spelled the
// same way as the corresponding header search path).
return suggestPathToFileForDiagnostics(File->getName(), /*WorkingDir=*/"",
FileEntryRef File, llvm::StringRef MainFile, bool *IsAngled) const {
return suggestPathToFileForDiagnostics(File.getName(), /*WorkingDir=*/"",
MainFile, IsAngled);
}

Expand Down
8 changes: 4 additions & 4 deletions clang/lib/Lex/PPDirectives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ Module *Preprocessor::getModuleForLocation(SourceLocation Loc,
: HeaderInfo.lookupModule(getLangOpts().CurrentModule, Loc);
}

const FileEntry *
OptionalFileEntryRef
Preprocessor::getHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
SourceLocation Loc) {
Module *IncM = getModuleForLocation(
Expand Down Expand Up @@ -920,7 +920,7 @@ Preprocessor::getHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
// make a particular module visible. Let the caller know they should
// suggest an import instead.
if (getLangOpts().ObjC || getLangOpts().CPlusPlusModules)
return nullptr;
return std::nullopt;

// If this is an accessible, non-textual header of M's top-level module
// that transitively includes the given location and makes the
Expand All @@ -931,7 +931,7 @@ Preprocessor::getHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
// FIXME: If we're bailing out due to a private header, we shouldn't suggest
// an import either.
if (InPrivateHeader)
return nullptr;
return std::nullopt;

// If the header is includable and has an include guard, assume the
// intended way to expose its contents is by #include, not by importing a
Expand All @@ -942,7 +942,7 @@ Preprocessor::getHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
Loc = SM.getIncludeLoc(ID);
}

return nullptr;
return std::nullopt;
}

OptionalFileEntryRef Preprocessor::LookupFile(
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/Sema/SemaLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5699,7 +5699,7 @@ void Sema::diagnoseMissingImport(SourceLocation Loc, const NamedDecl *Decl,

/// Get a "quoted.h" or <angled.h> include path to use in a diagnostic
/// suggesting the addition of a #include of the specified file.
static std::string getHeaderNameForHeader(Preprocessor &PP, const FileEntry *E,
static std::string getHeaderNameForHeader(Preprocessor &PP, FileEntryRef E,
llvm::StringRef IncludingFile) {
bool IsAngled = false;
auto Path = PP.getHeaderSearchInfo().suggestPathToFileForDiagnostics(
Expand Down Expand Up @@ -5732,11 +5732,12 @@ void Sema::diagnoseMissingImport(SourceLocation UseLoc, const NamedDecl *Decl,

// Try to find a suitable header-name to #include.
std::string HeaderName;
if (const FileEntry *Header =
if (OptionalFileEntryRef Header =
PP.getHeaderToIncludeForDiagnostics(UseLoc, DeclLoc)) {
if (const FileEntry *FE =
SourceMgr.getFileEntryForID(SourceMgr.getFileID(UseLoc)))
HeaderName = getHeaderNameForHeader(PP, Header, FE->tryGetRealPathName());
HeaderName =
getHeaderNameForHeader(PP, *Header, FE->tryGetRealPathName());
}

// If we have a #include we should suggest, or if all definition locations
Expand Down

0 comments on commit 6966c06

Please sign in to comment.