Skip to content

Commit

Permalink
[clang] NFCI: Use FileEntryRef in 'clang-tools-extra'
Browse files Browse the repository at this point in the history
  • Loading branch information
jansvoboda11 committed Sep 11, 2023
1 parent b19fe81 commit 797cad9
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ typedef std::vector<std::string> TUReplacementFiles;
typedef std::vector<clang::tooling::TranslationUnitDiagnostics> TUDiagnostics;

/// Map mapping file name to a set of AtomicChange targeting that file.
typedef llvm::DenseMap<const clang::FileEntry *,
std::vector<tooling::AtomicChange>>
FileToChangesMap;
using FileToChangesMap =
llvm::DenseMap<clang::FileEntryRef, std::vector<tooling::AtomicChange>>;

/// Recursively descends through a directory structure rooted at \p
/// Directory and attempts to deserialize *.yaml files as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ std::error_code collectReplacementsFromDirectory(
///
/// \returns A map mapping FileEntry to a set of Replacement targeting that
/// file.
static llvm::DenseMap<const FileEntry *, std::vector<tooling::Replacement>>
static llvm::DenseMap<FileEntryRef, std::vector<tooling::Replacement>>
groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs,
const clang::SourceManager &SM) {
llvm::StringSet<> Warned;
llvm::DenseMap<const FileEntry *, std::vector<tooling::Replacement>>
llvm::DenseMap<FileEntryRef, std::vector<tooling::Replacement>>
GroupedReplacements;

// Deduplicate identical replacements in diagnostics unless they are from the
Expand All @@ -165,7 +165,7 @@ groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs,
else
SM.getFileManager().makeAbsolutePath(Path);

if (auto Entry = SM.getFileManager().getFile(Path)) {
if (auto Entry = SM.getFileManager().getOptionalFileRef(Path)) {
if (SourceTU) {
auto &Replaces = DiagReplacements[*Entry];
auto It = Replaces.find(R);
Expand Down Expand Up @@ -212,10 +212,10 @@ bool mergeAndDeduplicate(const TUReplacements &TUs, const TUDiagnostics &TUDs,
// To report conflicting replacements on corresponding file, all replacements
// are stored into 1 big AtomicChange.
for (const auto &FileAndReplacements : GroupedReplacements) {
const FileEntry *Entry = FileAndReplacements.first;
FileEntryRef Entry = FileAndReplacements.first;
const SourceLocation BeginLoc =
SM.getLocForStartOfFile(SM.getOrCreateFileID(Entry, SrcMgr::C_User));
tooling::AtomicChange FileChange(Entry->getName(), Entry->getName());
tooling::AtomicChange FileChange(Entry.getName(), Entry.getName());
for (const auto &R : FileAndReplacements.second) {
llvm::Error Err =
FileChange.replace(SM, BeginLoc.getLocWithOffset(R.getOffset()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ int main(int argc, char **argv) {
: tooling::ApplyChangesSpec::kNone;

for (const auto &FileChange : Changes) {
const FileEntry *Entry = FileChange.first;
StringRef FileName = Entry->getName();
FileEntryRef Entry = FileChange.first;
StringRef FileName = Entry.getName();
llvm::Expected<std::string> NewFileData =
applyChanges(FileName, FileChange.second, Spec, Diagnostics);
if (!NewFileData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ void FindAllMacros::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
}

void FindAllMacros::EndOfMainFile() {
Reporter->reportSymbols(SM->getFileEntryForID(SM->getMainFileID())->getName(),
FileSymbols);
Reporter->reportSymbols(
SM->getFileEntryRefForID(SM->getMainFileID())->getName(), FileSymbols);
FileSymbols.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ void FindAllSymbols::run(const MatchFinder::MatchResult &Result) {
const SourceManager *SM = Result.SourceManager;
if (auto Symbol = CreateSymbolInfo(ND, *SM, Collector)) {
Filename =
std::string(SM->getFileEntryForID(SM->getMainFileID())->getName());
std::string(SM->getFileEntryRefForID(SM->getMainFileID())->getName());
FileSymbols[*Symbol] += Signals;
}
}
Expand Down
8 changes: 4 additions & 4 deletions clang-tools-extra/clang-move/Move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ AST_POLYMORPHIC_MATCHER_P(isExpansionInFile,
auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getBeginLoc());
if (ExpansionLoc.isInvalid())
return false;
auto *FileEntry =
SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc));
auto FileEntry =
SourceManager.getFileEntryRefForID(SourceManager.getFileID(ExpansionLoc));
if (!FileEntry)
return false;
return MakeAbsolutePath(SourceManager, FileEntry->getName()) ==
Expand All @@ -135,7 +135,7 @@ class FindAllIncludes : public PPCallbacks {
StringRef /*RelativePath*/,
const Module * /*Imported*/,
SrcMgr::CharacteristicKind /*FileType*/) override {
if (const auto *FileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc)))
if (auto FileEntry = SM.getFileEntryRefForID(SM.getFileID(HashLoc)))
MoveTool->addIncludes(FileName, IsAngled, SearchPath,
FileEntry->getName(), FilenameRange, SM);
}
Expand Down Expand Up @@ -341,7 +341,7 @@ bool isInHeaderFile(const Decl *D, llvm::StringRef OriginalRunningDirectory,
if (ExpansionLoc.isInvalid())
return false;

if (const auto *FE = SM.getFileEntryForID(SM.getFileID(ExpansionLoc))) {
if (auto FE = SM.getFileEntryRefForID(SM.getFileID(ExpansionLoc))) {
return MakeAbsolutePath(SM, FE->getName()) ==
MakeAbsolutePath(OriginalRunningDirectory, OldHeader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location,
// location needed depends on the check (in particular, where this check wants
// to apply fixes).
FileID FID = Sources.getDecomposedExpansionLoc(Location).first;
const FileEntry *File = Sources.getFileEntryForID(FID);
OptionalFileEntryRef File = Sources.getFileEntryRefForID(FID);

// -DMACRO definitions on the command line have locations in a virtual buffer
// that doesn't have a FileEntry. Don't skip these as well.
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ AST_POLYMORPHIC_MATCHER(
SourceLocation Loc = SourceManager.getSpellingLoc(Node.getBeginLoc());
if (Loc.isInvalid())
return false;
const FileEntry *FileEntry =
SourceManager.getFileEntryForID(SourceManager.getFileID(Loc));
OptionalFileEntryRef FileEntry =
SourceManager.getFileEntryRefForID(SourceManager.getFileID(Loc));
if (!FileEntry)
return false;
// Determine whether filepath contains "absl/[absl-library]" substring, where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool KernelNameRestrictionPPCallbacks::fileNameIsRestricted(
void KernelNameRestrictionPPCallbacks::EndOfMainFile() {

// Check main file for restricted names.
const FileEntry *Entry = SM.getFileEntryForID(SM.getMainFileID());
OptionalFileEntryRef Entry = SM.getFileEntryRefForID(SM.getMainFileID());
StringRef FileName = llvm::sys::path::filename(Entry->getName());
if (fileNameIsRestricted(FileName))
Check.diag(SM.getLocForStartOfFile(SM.getMainFileID()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
return;
// We don't emit warnings on unused-using-decls from headers, so bail out if
// the main file is a header.
if (const auto *MainFile = Result.SourceManager->getFileEntryForID(
if (auto MainFile = Result.SourceManager->getFileEntryRefForID(
Result.SourceManager->getMainFileID());
utils::isFileExtension(MainFile->getName(), HeaderFileExtensions))
return;
Expand Down
9 changes: 5 additions & 4 deletions clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ class HeaderGuardPPCallbacks : public PPCallbacks {
// guards.
SourceManager &SM = PP->getSourceManager();
if (Reason == EnterFile && FileType == SrcMgr::C_User) {
if (const FileEntry *FE = SM.getFileEntryForID(SM.getFileID(Loc))) {
if (OptionalFileEntryRef FE =
SM.getFileEntryRefForID(SM.getFileID(Loc))) {
std::string FileName = cleanPath(FE->getName());
Files[FileName] = FE;
Files[FileName] = *FE;
}
}
}
Expand Down Expand Up @@ -77,8 +78,8 @@ class HeaderGuardPPCallbacks : public PPCallbacks {
if (!MI->isUsedForHeaderGuard())
continue;

const FileEntry *FE =
SM.getFileEntryForID(SM.getFileID(MI->getDefinitionLoc()));
OptionalFileEntryRef FE =
SM.getFileEntryRefForID(SM.getFileID(MI->getDefinitionLoc()));
std::string FileName = cleanPath(FE->getName());
Files.erase(FileName);

Expand Down
28 changes: 12 additions & 16 deletions clang-tools-extra/modularize/Modularize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ getModularizeArgumentsAdjuster(DependencyMap &Dependencies) {
// want to design to be applicable to a wider range of tools, and stick it
// somewhere into Tooling/ in mainline
struct Location {
const FileEntry *File;
OptionalFileEntryRef File;
unsigned Line, Column;

Location() : File(), Line(), Column() {}
Expand All @@ -391,7 +391,7 @@ struct Location {
return;

std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
File = SM.getFileEntryForID(Decomposed.first);
File = SM.getFileEntryRefForID(Decomposed.first);
if (!File)
return;

Expand Down Expand Up @@ -483,12 +483,12 @@ typedef std::vector<HeaderEntry> HeaderContents;

class EntityMap : public std::map<std::string, SmallVector<Entry, 2>> {
public:
DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
DenseMap<FileEntryRef, HeaderContents> HeaderContentMismatches;

void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) {
// Record this entity in its header.
HeaderEntry HE = { Name, Loc };
CurHeaderContents[Loc.File].push_back(HE);
CurHeaderContents[*Loc.File].push_back(HE);

// Check whether we've seen this entry before.
SmallVector<Entry, 2> &Entries = (*this)[Name];
Expand All @@ -503,16 +503,13 @@ class EntityMap : public std::map<std::string, SmallVector<Entry, 2>> {
}

void mergeCurHeaderContents() {
for (DenseMap<const FileEntry *, HeaderContents>::iterator
H = CurHeaderContents.begin(),
HEnd = CurHeaderContents.end();
for (auto H = CurHeaderContents.begin(), HEnd = CurHeaderContents.end();
H != HEnd; ++H) {
// Sort contents.
llvm::sort(H->second);

// Check whether we've seen this header before.
DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
AllHeaderContents.find(H->first);
auto KnownH = AllHeaderContents.find(H->first);
if (KnownH == AllHeaderContents.end()) {
// We haven't seen this header before; record its contents.
AllHeaderContents.insert(*H);
Expand All @@ -534,8 +531,8 @@ class EntityMap : public std::map<std::string, SmallVector<Entry, 2>> {
}

private:
DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
DenseMap<FileEntryRef, HeaderContents> CurHeaderContents;
DenseMap<FileEntryRef, HeaderContents> AllHeaderContents;
};

class CollectEntitiesVisitor
Expand Down Expand Up @@ -961,18 +958,17 @@ int main(int Argc, const char **Argv) {
// they are included.
// FIXME: Could we provide information about which preprocessor conditionals
// are involved?
for (DenseMap<const FileEntry *, HeaderContents>::iterator
H = Entities.HeaderContentMismatches.begin(),
HEnd = Entities.HeaderContentMismatches.end();
for (auto H = Entities.HeaderContentMismatches.begin(),
HEnd = Entities.HeaderContentMismatches.end();
H != HEnd; ++H) {
if (H->second.empty()) {
errs() << "internal error: phantom header content mismatch\n";
continue;
}

HadErrors = 1;
ModUtil->addUniqueProblemFile(std::string(H->first->getName()));
errs() << "error: header '" << H->first->getName()
ModUtil->addUniqueProblemFile(std::string(H->first.getName()));
errs() << "error: header '" << H->first.getName()
<< "' has different contents depending on how it was included.\n";
for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
errs() << "note: '" << H->second[I].Name << "' in "
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/modularize/PreprocessorTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1292,8 +1292,8 @@ void PreprocessorCallbacks::FileChanged(
PPTracker.handleHeaderEntry(PP, getSourceLocationFile(PP, Loc));
break;
case ExitFile: {
const clang::FileEntry *F =
PP.getSourceManager().getFileEntryForID(PrevFID);
clang::OptionalFileEntryRef F =
PP.getSourceManager().getFileEntryRefForID(PrevFID);
if (F)
PPTracker.handleHeaderExit(F->getName());
} break;
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/pp-trace/PPCallbacksTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,8 @@ void PPCallbacksTracker::appendArgument(const char *Name, FileID Value) {
appendArgument(Name, "(invalid)");
return;
}
const FileEntry *FileEntry = PP.getSourceManager().getFileEntryForID(Value);
OptionalFileEntryRef FileEntry =
PP.getSourceManager().getFileEntryRefForID(Value);
if (!FileEntry) {
appendArgument(Name, "(getFileEntryForID failed)");
return;
Expand Down

0 comments on commit 797cad9

Please sign in to comment.