Author: Jan Svoboda Date: 2022-04-14T10:46:12+02:00 New Revision: d79ad2f1dbc2db63121620f55d6cfa915f2733ac
URL: https://github.com/llvm/llvm-project/commit/d79ad2f1dbc2db63121620f55d6cfa915f2733ac DIFF: https://github.com/llvm/llvm-project/commit/d79ad2f1dbc2db63121620f55d6cfa915f2733ac.diff LOG: [clang][lex] NFCI: Use FileEntryRef in PPCallbacks::InclusionDirective() This patch changes type of the `File` parameter in `PPCallbacks::InclusionDirective()` from `const FileEntry *` to `Optional<FileEntryRef>`. With the API change in place, this patch then removes some uses of the deprecated `FileEntry::getName()` (e.g. in `DependencyGraph.cpp` and `ModuleDependencyCollector.cpp`). Reviewed By: dexonsmith, bnbarham Differential Revision: https://reviews.llvm.org/D123574 Added: Modified: clang-tools-extra/clang-move/Move.cpp clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h clang-tools-extra/clang-tidy/altera/KernelNameRestrictionCheck.cpp clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.cpp clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.cpp clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.h clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp clang-tools-extra/clangd/Headers.cpp clang-tools-extra/clangd/Headers.h clang-tools-extra/clangd/ParsedAST.cpp clang-tools-extra/clangd/index/IndexAction.cpp clang-tools-extra/clangd/unittests/HeadersTests.cpp clang-tools-extra/clangd/unittests/ParsedASTTests.cpp clang-tools-extra/modularize/CoverageChecker.cpp clang-tools-extra/modularize/PreprocessorTracker.cpp clang-tools-extra/pp-trace/PPCallbacksTracker.cpp clang-tools-extra/pp-trace/PPCallbacksTracker.h clang/include/clang/Lex/PPCallbacks.h clang/include/clang/Lex/PreprocessingRecord.h clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h clang/lib/CodeGen/MacroPPCallbacks.cpp clang/lib/CodeGen/MacroPPCallbacks.h clang/lib/Frontend/DependencyFile.cpp clang/lib/Frontend/DependencyGraph.cpp clang/lib/Frontend/ModuleDependencyCollector.cpp clang/lib/Frontend/PrecompiledPreamble.cpp clang/lib/Frontend/PrintPreprocessedOutput.cpp clang/lib/Frontend/Rewrite/InclusionRewriter.cpp clang/lib/Lex/PPDirectives.cpp clang/lib/Lex/PreprocessingRecord.cpp clang/lib/Serialization/ASTReader.cpp clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp clang/tools/libclang/CIndex.cpp clang/tools/libclang/CXIndexDataConsumer.cpp clang/tools/libclang/CXIndexDataConsumer.h clang/tools/libclang/Indexing.cpp clang/unittests/Lex/PPCallbacksTest.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clang-move/Move.cpp b/clang-tools-extra/clang-move/Move.cpp index 8e0ec0f73c4dd..6419bd3372dce 100644 --- a/clang-tools-extra/clang-move/Move.cpp +++ b/clang-tools-extra/clang-move/Move.cpp @@ -131,7 +131,7 @@ class FindAllIncludes : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token & /*IncludeTok*/, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, - const FileEntry * /*File*/, StringRef SearchPath, + Optional<FileEntryRef> /*File*/, StringRef SearchPath, StringRef /*RelativePath*/, const Module * /*Imported*/, SrcMgr::CharacteristicKind /*FileType*/) override { diff --git a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp index a75fca13e2e5a..f88e24c10901c 100644 --- a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp +++ b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp @@ -162,8 +162,9 @@ void ExpandModularHeadersPPCallbacks::FileChanged( void ExpandModularHeadersPPCallbacks::InclusionDirective( SourceLocation DirectiveLoc, const Token &IncludeToken, StringRef IncludedFilename, bool IsAngled, CharSourceRange FilenameRange, - const FileEntry *IncludedFile, StringRef SearchPath, StringRef RelativePath, - const Module *Imported, SrcMgr::CharacteristicKind FileType) { + Optional<FileEntryRef> IncludedFile, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, + SrcMgr::CharacteristicKind FileType) { if (Imported) { serialization::ModuleFile *MF = Compiler.getASTReader()->getModuleManager().lookup( diff --git a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h index 6fdc8bd21c6bd..29f060507e53c 100644 --- a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h +++ b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h @@ -69,8 +69,9 @@ class ExpandModularHeadersPPCallbacks : public PPCallbacks { void InclusionDirective(SourceLocation DirectiveLoc, const Token &IncludeToken, StringRef IncludedFilename, bool IsAngled, CharSourceRange FilenameRange, - const FileEntry *IncludedFile, StringRef SearchPath, - StringRef RelativePath, const Module *Imported, + Optional<FileEntryRef> IncludedFile, + StringRef SearchPath, StringRef RelativePath, + const Module *Imported, SrcMgr::CharacteristicKind FileType) override; void EndOfMainFile() override; diff --git a/clang-tools-extra/clang-tidy/altera/KernelNameRestrictionCheck.cpp b/clang-tools-extra/clang-tidy/altera/KernelNameRestrictionCheck.cpp index 769494207aef2..c51fd53e334cc 100644 --- a/clang-tools-extra/clang-tidy/altera/KernelNameRestrictionCheck.cpp +++ b/clang-tools-extra/clang-tidy/altera/KernelNameRestrictionCheck.cpp @@ -29,9 +29,9 @@ class KernelNameRestrictionPPCallbacks : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FileNameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FileNameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; void EndOfMainFile() override; @@ -62,8 +62,8 @@ void KernelNameRestrictionCheck::registerPPCallbacks(const SourceManager &SM, void KernelNameRestrictionPPCallbacks::InclusionDirective( SourceLocation HashLoc, const Token &, StringRef FileName, bool, - CharSourceRange, const FileEntry *, StringRef, StringRef, const Module *, - SrcMgr::CharacteristicKind) { + CharSourceRange, Optional<FileEntryRef>, StringRef, StringRef, + const Module *, SrcMgr::CharacteristicKind) { IncludeDirective ID = {HashLoc, FileName}; IncludeDirectives.push_back(std::move(ID)); } diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp index 64fef5ff71711..b8fbc6ed1e740 100644 --- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp @@ -24,9 +24,9 @@ class SuspiciousIncludePPCallbacks : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; private: @@ -72,7 +72,7 @@ void SuspiciousIncludeCheck::registerPPCallbacks( void SuspiciousIncludePPCallbacks::InclusionDirective( SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, - bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, + bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) { if (IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import) diff --git a/clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp b/clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp index ab75e3100d85d..948bf16dd5e2c 100644 --- a/clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp +++ b/clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp @@ -26,9 +26,9 @@ class IncludeOrderPPCallbacks : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; void EndOfMainFile() override; @@ -81,7 +81,7 @@ static int getPriority(StringRef Filename, bool IsAngled, bool IsMainModule) { void IncludeOrderPPCallbacks::InclusionDirective( SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, - bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, + bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) { // We recognize the first include as a special main module header and want diff --git a/clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.cpp b/clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.cpp index e64338756a25b..62379f1214d84 100644 --- a/clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.cpp +++ b/clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.cpp @@ -33,9 +33,9 @@ class RestrictedIncludesPPCallbacks void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; private: @@ -46,7 +46,7 @@ class RestrictedIncludesPPCallbacks void RestrictedIncludesPPCallbacks::InclusionDirective( SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, - bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, + bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) { // Compiler provided headers are allowed (e.g stddef.h). diff --git a/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp index af12fec75ddc3..0b44bdaafe23e 100644 --- a/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp @@ -27,9 +27,9 @@ class IncludeModernizePPCallbacks : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; private: @@ -91,7 +91,7 @@ IncludeModernizePPCallbacks::IncludeModernizePPCallbacks(ClangTidyCheck &Check, void IncludeModernizePPCallbacks::InclusionDirective( SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, - bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, + bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) { // FIXME: Take care of library symbols from the global namespace. diff --git a/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp index 318b86be982b9..07e1788857f29 100644 --- a/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp @@ -133,9 +133,9 @@ class MacroToEnumCallbacks : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override { clearCurrentEnum(HashLoc); } diff --git a/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.cpp b/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.cpp index f6163989a461a..7faf7ccce2405 100644 --- a/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.cpp +++ b/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.cpp @@ -22,7 +22,7 @@ namespace portability { void RestrictedIncludesPPCallbacks::InclusionDirective( SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, - bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, + bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) { if (!Check.contains(FileName) && SrcMgr::isSystem(FileType)) { diff --git a/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.h b/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.h index 80eddf7bcee79..48ec614937859 100644 --- a/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.h +++ b/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.h @@ -50,9 +50,9 @@ class RestrictedIncludesPPCallbacks : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; void EndOfMainFile() override; diff --git a/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp b/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp index a6e49439c8434..9815952f81759 100644 --- a/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp @@ -47,9 +47,9 @@ class DuplicateIncludeCallbacks : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; void MacroDefined(const Token &MacroNameTok, @@ -77,7 +77,7 @@ void DuplicateIncludeCallbacks::FileChanged(SourceLocation Loc, void DuplicateIncludeCallbacks::InclusionDirective( SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, - bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, + bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) { if (llvm::find(Files.back(), FileName) != Files.back().end()) { diff --git a/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp b/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp index 6f3eed49831fd..b8bd50d9bc3f1 100644 --- a/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp +++ b/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp @@ -24,7 +24,7 @@ class IncludeInserterCallback : public PPCallbacks { void InclusionDirective(SourceLocation HashLocation, const Token &IncludeToken, StringRef FileNameRef, bool IsAngled, CharSourceRange FileNameRange, - const FileEntry * /*IncludedFile*/, + Optional<FileEntryRef> /*IncludedFile*/, StringRef /*SearchPath*/, StringRef /*RelativePath*/, const Module * /*ImportedModule*/, SrcMgr::CharacteristicKind /*FileType*/) override { diff --git a/clang-tools-extra/clangd/Headers.cpp b/clang-tools-extra/clangd/Headers.cpp index a278ae97f94a5..acc6421798755 100644 --- a/clang-tools-extra/clangd/Headers.cpp +++ b/clang-tools-extra/clangd/Headers.cpp @@ -35,7 +35,8 @@ class IncludeStructure::RecordHeaders : public PPCallbacks, void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, llvm::StringRef FileName, bool IsAngled, CharSourceRange /*FilenameRange*/, - const FileEntry *File, llvm::StringRef /*SearchPath*/, + Optional<FileEntryRef> File, + llvm::StringRef /*SearchPath*/, llvm::StringRef /*RelativePath*/, const clang::Module * /*Imported*/, SrcMgr::CharacteristicKind FileKind) override { @@ -51,7 +52,8 @@ class IncludeStructure::RecordHeaders : public PPCallbacks, auto &Inc = Out->MainFileIncludes.back(); Inc.Written = (IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str(); - Inc.Resolved = std::string(File ? File->tryGetRealPathName() : ""); + Inc.Resolved = + std::string(File ? File->getFileEntry().tryGetRealPathName() : ""); Inc.HashOffset = SM.getFileOffset(HashLoc); Inc.HashLine = SM.getLineNumber(SM.getFileID(HashLoc), Inc.HashOffset) - 1; @@ -60,7 +62,7 @@ class IncludeStructure::RecordHeaders : public PPCallbacks, if (LastPragmaKeepInMainFileLine == Inc.HashLine) Inc.BehindPragmaKeep = true; if (File) { - IncludeStructure::HeaderID HID = Out->getOrCreateID(File); + IncludeStructure::HeaderID HID = Out->getOrCreateID(*File); Inc.HeaderID = static_cast<unsigned>(HID); if (IsAngled) if (auto StdlibHeader = tooling::stdlib::Header::named(Inc.Written)) { @@ -74,15 +76,15 @@ class IncludeStructure::RecordHeaders : public PPCallbacks, // Record include graph (not just for main-file includes) if (File) { - auto *IncludingFileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc)); + auto IncludingFileEntry = SM.getFileEntryRefForID(SM.getFileID(HashLoc)); if (!IncludingFileEntry) { assert(SM.getBufferName(HashLoc).startswith("<") && "Expected #include location to be a file or <built-in>"); // Treat as if included from the main file. - IncludingFileEntry = SM.getFileEntryForID(MainFID); + IncludingFileEntry = SM.getFileEntryRefForID(MainFID); } - auto IncludingID = Out->getOrCreateID(IncludingFileEntry), - IncludedID = Out->getOrCreateID(File); + auto IncludingID = Out->getOrCreateID(*IncludingFileEntry), + IncludedID = Out->getOrCreateID(*File); Out->IncludeChildren[IncludingID].push_back(IncludedID); } } @@ -226,22 +228,22 @@ IncludeStructure::getID(const FileEntry *Entry) const { } IncludeStructure::HeaderID -IncludeStructure::getOrCreateID(const FileEntry *Entry) { +IncludeStructure::getOrCreateID(FileEntryRef Entry) { // Main file's FileEntry was not known at IncludeStructure creation time. - if (Entry == MainFileEntry) { + if (&Entry.getFileEntry() == MainFileEntry) { if (RealPathNames.front().empty()) RealPathNames.front() = MainFileEntry->tryGetRealPathName().str(); return MainFileID; } auto R = UIDToIndex.try_emplace( - Entry->getUniqueID(), + Entry.getUniqueID(), static_cast<IncludeStructure::HeaderID>(RealPathNames.size())); if (R.second) RealPathNames.emplace_back(); IncludeStructure::HeaderID Result = R.first->getSecond(); std::string &RealPathName = RealPathNames[static_cast<unsigned>(Result)]; if (RealPathName.empty()) - RealPathName = Entry->tryGetRealPathName().str(); + RealPathName = Entry.getFileEntry().tryGetRealPathName().str(); return Result; } diff --git a/clang-tools-extra/clangd/Headers.h b/clang-tools-extra/clangd/Headers.h index ed493b8920739..5da0200afd1aa 100644 --- a/clang-tools-extra/clangd/Headers.h +++ b/clang-tools-extra/clangd/Headers.h @@ -134,7 +134,7 @@ class IncludeStructure { enum class HeaderID : unsigned {}; llvm::Optional<HeaderID> getID(const FileEntry *Entry) const; - HeaderID getOrCreateID(const FileEntry *Entry); + HeaderID getOrCreateID(FileEntryRef Entry); StringRef getRealPath(HeaderID ID) const { assert(static_cast<unsigned>(ID) <= RealPathNames.size()); diff --git a/clang-tools-extra/clangd/ParsedAST.cpp b/clang-tools-extra/clangd/ParsedAST.cpp index 77cc4569ae712..e40657d7a0c34 100644 --- a/clang-tools-extra/clangd/ParsedAST.cpp +++ b/clang-tools-extra/clangd/ParsedAST.cpp @@ -211,7 +211,6 @@ class ReplayPreamble : private PPCallbacks { SynthesizedFilenameTok.setKind(tok::header_name); SynthesizedFilenameTok.setLiteralData(Inc.Written.data()); - const FileEntry *FE = File ? &File->getFileEntry() : nullptr; llvm::StringRef WrittenFilename = llvm::StringRef(Inc.Written).drop_front().drop_back(); Delegate->InclusionDirective( @@ -220,7 +219,7 @@ class ReplayPreamble : private PPCallbacks { syntax::FileRange(SM, SynthesizedFilenameTok.getLocation(), SynthesizedFilenameTok.getEndLoc()) .toCharRange(SM), - FE, "SearchPath", "RelPath", + File, "SearchPath", "RelPath", /*Imported=*/nullptr, Inc.FileKind); if (File) Delegate->FileSkipped(*File, SynthesizedFilenameTok, Inc.FileKind); diff --git a/clang-tools-extra/clangd/index/IndexAction.cpp b/clang-tools-extra/clangd/index/IndexAction.cpp index 3899902839a75..431fce85b4d8f 100644 --- a/clang-tools-extra/clangd/index/IndexAction.cpp +++ b/clang-tools-extra/clangd/index/IndexAction.cpp @@ -28,10 +28,10 @@ namespace clang { namespace clangd { namespace { -llvm::Optional<std::string> toURI(const FileEntry *File) { +llvm::Optional<std::string> toURI(Optional<FileEntryRef> File) { if (!File) return llvm::None; - auto AbsolutePath = File->tryGetRealPathName(); + auto AbsolutePath = File->getFileEntry().tryGetRealPathName(); if (AbsolutePath.empty()) return llvm::None; return URI::create(AbsolutePath).toString(); @@ -58,7 +58,7 @@ struct IncludeGraphCollector : public PPCallbacks { return; const auto FileID = SM.getFileID(Loc); - const auto *File = SM.getFileEntryForID(FileID); + auto File = SM.getFileEntryRefForID(FileID); auto URI = toURI(File); if (!URI) return; @@ -84,7 +84,8 @@ struct IncludeGraphCollector : public PPCallbacks { // Add edges from including files to includes. void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, llvm::StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, llvm::StringRef SearchPath, llvm::StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override { @@ -92,7 +93,7 @@ struct IncludeGraphCollector : public PPCallbacks { if (!IncludeURI) return; - auto IncludingURI = toURI(SM.getFileEntryForID(SM.getFileID(HashLoc))); + auto IncludingURI = toURI(SM.getFileEntryRefForID(SM.getFileID(HashLoc))); if (!IncludingURI) return; @@ -106,7 +107,7 @@ struct IncludeGraphCollector : public PPCallbacks { void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok, SrcMgr::CharacteristicKind FileType) override { #ifndef NDEBUG - auto URI = toURI(&SkippedFile.getFileEntry()); + auto URI = toURI(SkippedFile); if (!URI) return; auto I = IG.try_emplace(*URI); diff --git a/clang-tools-extra/clangd/unittests/HeadersTests.cpp b/clang-tools-extra/clangd/unittests/HeadersTests.cpp index 0eb682b9cb878..c0cc3b69f3519 100644 --- a/clang-tools-extra/clangd/unittests/HeadersTests.cpp +++ b/clang-tools-extra/clangd/unittests/HeadersTests.cpp @@ -19,6 +19,7 @@ #include "llvm/Support/Error.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/Path.h" +#include "llvm/Testing/Support/Error.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -68,8 +69,8 @@ class HeadersTest : public ::testing::Test { IncludeStructure::HeaderID getID(StringRef Filename, IncludeStructure &Includes) { auto &SM = Clang->getSourceManager(); - auto Entry = SM.getFileManager().getFile(Filename); - EXPECT_TRUE(Entry); + auto Entry = SM.getFileManager().getFileRef(Filename); + EXPECT_THAT_EXPECTED(Entry, llvm::Succeeded()); return Includes.getOrCreateID(*Entry); } diff --git a/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp b/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp index 1753f20f8521d..8167413468940 100644 --- a/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp +++ b/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp @@ -33,6 +33,7 @@ #include "clang/Lex/Token.h" #include "clang/Tooling/Syntax/Tokens.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Testing/Support/Error.h" #include "gmock/gmock-matchers.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -379,8 +380,9 @@ TEST(ParsedASTTest, ReplayPreambleForTidyCheckers) { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *, - StringRef, StringRef, const clang::Module *, + CharSourceRange FilenameRange, + Optional<FileEntryRef>, StringRef, StringRef, + const clang::Module *, SrcMgr::CharacteristicKind) override { Includes.emplace_back(SM, HashLoc, IncludeTok, FileName, IsAngled, FilenameRange); @@ -560,13 +562,13 @@ TEST(ParsedASTTest, PatchesDeletedIncludes) { auto &FM = SM.getFileManager(); // Copy so that we can getOrCreateID(). IncludeStructure Includes = ExpectedAST.getIncludeStructure(); - auto MainFE = FM.getFile(testPath("foo.cpp")); - ASSERT_TRUE(MainFE); + auto MainFE = FM.getFileRef(testPath("foo.cpp")); + ASSERT_THAT_EXPECTED(MainFE, llvm::Succeeded()); auto MainID = Includes.getOrCreateID(*MainFE); auto &PatchedFM = PatchedAST->getSourceManager().getFileManager(); IncludeStructure PatchedIncludes = PatchedAST->getIncludeStructure(); - auto PatchedMainFE = PatchedFM.getFile(testPath("foo.cpp")); - ASSERT_TRUE(PatchedMainFE); + auto PatchedMainFE = PatchedFM.getFileRef(testPath("foo.cpp")); + ASSERT_THAT_EXPECTED(PatchedMainFE, llvm::Succeeded()); auto PatchedMainID = PatchedIncludes.getOrCreateID(*PatchedMainFE); EXPECT_EQ(Includes.includeDepth(MainID)[MainID], PatchedIncludes.includeDepth(PatchedMainID)[PatchedMainID]); diff --git a/clang-tools-extra/modularize/CoverageChecker.cpp b/clang-tools-extra/modularize/CoverageChecker.cpp index b1c787862c02d..610fb596305c0 100644 --- a/clang-tools-extra/modularize/CoverageChecker.cpp +++ b/clang-tools-extra/modularize/CoverageChecker.cpp @@ -88,9 +88,9 @@ class CoverageCheckerCallbacks : public PPCallbacks { // Include directive callback. void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override { Checker.collectUmbrellaHeaderHeader(File->getName()); } diff --git a/clang-tools-extra/modularize/PreprocessorTracker.cpp b/clang-tools-extra/modularize/PreprocessorTracker.cpp index f472415225a17..171a9380b793e 100644 --- a/clang-tools-extra/modularize/PreprocessorTracker.cpp +++ b/clang-tools-extra/modularize/PreprocessorTracker.cpp @@ -734,7 +734,7 @@ class PreprocessorCallbacks : public clang::PPCallbacks { const clang::Token &IncludeTok, llvm::StringRef FileName, bool IsAngled, clang::CharSourceRange FilenameRange, - const clang::FileEntry *File, + llvm::Optional<clang::FileEntryRef> File, llvm::StringRef SearchPath, llvm::StringRef RelativePath, const clang::Module *Imported, @@ -1277,9 +1277,10 @@ PreprocessorTracker *PreprocessorTracker::create( void PreprocessorCallbacks::InclusionDirective( clang::SourceLocation HashLoc, const clang::Token &IncludeTok, llvm::StringRef FileName, bool IsAngled, - clang::CharSourceRange FilenameRange, const clang::FileEntry *File, - llvm::StringRef SearchPath, llvm::StringRef RelativePath, - const clang::Module *Imported, clang::SrcMgr::CharacteristicKind FileType) { + clang::CharSourceRange FilenameRange, + llvm::Optional<clang::FileEntryRef> File, llvm::StringRef SearchPath, + llvm::StringRef RelativePath, const clang::Module *Imported, + clang::SrcMgr::CharacteristicKind FileType) { int DirectiveLine, DirectiveColumn; std::string HeaderPath = getSourceLocationFile(PP, HashLoc); getSourceLocationLineAndColumn(PP, HashLoc, DirectiveLine, DirectiveColumn); diff --git a/clang-tools-extra/pp-trace/PPCallbacksTracker.cpp b/clang-tools-extra/pp-trace/PPCallbacksTracker.cpp index 785fa9b679bb6..63a07914f2071 100644 --- a/clang-tools-extra/pp-trace/PPCallbacksTracker.cpp +++ b/clang-tools-extra/pp-trace/PPCallbacksTracker.cpp @@ -123,7 +123,7 @@ void PPCallbacksTracker::FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok, SrcMgr::CharacteristicKind FileType) { beginCallback("FileSkipped"); - appendArgument("ParentFile", &SkippedFile.getFileEntry()); + appendArgument("ParentFile", SkippedFile); appendArgument("FilenameTok", FilenameTok); appendArgument("FileType", FileType, CharacteristicKindStrings); } @@ -133,7 +133,7 @@ void PPCallbacksTracker::FileSkipped(const FileEntryRef &SkippedFile, // of whether the inclusion will actually result in an inclusion. void PPCallbacksTracker::InclusionDirective( SourceLocation HashLoc, const Token &IncludeTok, llvm::StringRef FileName, - bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, + bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File, llvm::StringRef SearchPath, llvm::StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) { beginCallback("InclusionDirective"); @@ -485,12 +485,16 @@ void PPCallbacksTracker::appendArgument(const char *Name, FileID Value) { // Append a FileEntry argument to the top trace item. void PPCallbacksTracker::appendArgument(const char *Name, - const FileEntry *Value) { + Optional<FileEntryRef> Value) { if (!Value) { appendArgument(Name, "(null)"); return; } - appendFilePathArgument(Name, Value->getName()); + appendArgument(Name, *Value); +} + +void PPCallbacksTracker::appendArgument(const char *Name, FileEntryRef Value) { + appendFilePathArgument(Name, Value.getName()); } // Append a SourceLocation argument to the top trace item. diff --git a/clang-tools-extra/pp-trace/PPCallbacksTracker.h b/clang-tools-extra/pp-trace/PPCallbacksTracker.h index 7be31031fe718..864014631f04a 100644 --- a/clang-tools-extra/pp-trace/PPCallbacksTracker.h +++ b/clang-tools-extra/pp-trace/PPCallbacksTracker.h @@ -93,7 +93,8 @@ class PPCallbacksTracker : public PPCallbacks { SrcMgr::CharacteristicKind FileType) override; void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, llvm::StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, llvm::StringRef SearchPath, llvm::StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; @@ -177,8 +178,9 @@ class PPCallbacksTracker : public PPCallbacks { /// Append a FileID argument to the top trace item. void appendArgument(const char *Name, FileID Value); - /// Append a FileEntry argument to the top trace item. - void appendArgument(const char *Name, const FileEntry *Value); + /// Append a FileEntryRef argument to the top trace item. + void appendArgument(const char *Name, Optional<FileEntryRef> Value); + void appendArgument(const char *Name, FileEntryRef Value); /// Append a SourceLocation argument to the top trace item. void appendArgument(const char *Name, SourceLocation Value); diff --git a/clang/include/clang/Lex/PPCallbacks.h b/clang/include/clang/Lex/PPCallbacks.h index 298947836a396..5f7cfab00e41a 100644 --- a/clang/include/clang/Lex/PPCallbacks.h +++ b/clang/include/clang/Lex/PPCallbacks.h @@ -107,7 +107,7 @@ class PPCallbacks { StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, - const FileEntry *File, + Optional<FileEntryRef> File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, @@ -428,9 +428,9 @@ class PPChainedCallbacks : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override { First->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled, FilenameRange, File, SearchPath, RelativePath, diff --git a/clang/include/clang/Lex/PreprocessingRecord.h b/clang/include/clang/Lex/PreprocessingRecord.h index 0137d871e916a..063929dd8f963 100644 --- a/clang/include/clang/Lex/PreprocessingRecord.h +++ b/clang/include/clang/Lex/PreprocessingRecord.h @@ -242,13 +242,12 @@ class Token; unsigned ImportedModule : 1; /// The file that was included. - const FileEntry *File; + Optional<FileEntryRef> File; public: - InclusionDirective(PreprocessingRecord &PPRec, - InclusionKind Kind, StringRef FileName, - bool InQuotes, bool ImportedModule, - const FileEntry *File, SourceRange Range); + InclusionDirective(PreprocessingRecord &PPRec, InclusionKind Kind, + StringRef FileName, bool InQuotes, bool ImportedModule, + Optional<FileEntryRef> File, SourceRange Range); /// Determine what kind of inclusion directive this is. InclusionKind getKind() const { return static_cast<InclusionKind>(Kind); } @@ -266,7 +265,7 @@ class Token; /// Retrieve the file entry for the actual file that was included /// by this directive. - const FileEntry *getFile() const { return File; } + Optional<FileEntryRef> getFile() const { return File; } // Implement isa/cast/dyncast/etc. static bool classof(const PreprocessedEntity *PE) { @@ -531,7 +530,7 @@ class Token; void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, - const FileEntry *File, StringRef SearchPath, + Optional<FileEntryRef> File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; void Ifdef(SourceLocation Loc, const Token &MacroNameTok, diff --git a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h index 7936f9c60f6b0..363e159dd990f 100644 --- a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h +++ b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h @@ -136,9 +136,9 @@ class ModuleDepCollectorPP final : public PPCallbacks { FileID PrevFID) override; void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; void moduleImport(SourceLocation ImportLoc, ModuleIdPath Path, const Module *Imported) override; diff --git a/clang/lib/CodeGen/MacroPPCallbacks.cpp b/clang/lib/CodeGen/MacroPPCallbacks.cpp index 92800e738b627..2f09fd2b6c15f 100644 --- a/clang/lib/CodeGen/MacroPPCallbacks.cpp +++ b/clang/lib/CodeGen/MacroPPCallbacks.cpp @@ -167,7 +167,7 @@ void MacroPPCallbacks::FileChanged(SourceLocation Loc, FileChangeReason Reason, void MacroPPCallbacks::InclusionDirective( SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, - bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, + bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) { diff --git a/clang/lib/CodeGen/MacroPPCallbacks.h b/clang/lib/CodeGen/MacroPPCallbacks.h index d249b5b0eb88c..01041b16e4b7e 100644 --- a/clang/lib/CodeGen/MacroPPCallbacks.h +++ b/clang/lib/CodeGen/MacroPPCallbacks.h @@ -100,9 +100,9 @@ class MacroPPCallbacks : public PPCallbacks { /// Callback invoked whenever a directive (#xxx) is processed. void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; /// Hook called whenever a macro definition is seen. diff --git a/clang/lib/Frontend/DependencyFile.cpp b/clang/lib/Frontend/DependencyFile.cpp index 2888273741060..3a810e56571df 100644 --- a/clang/lib/Frontend/DependencyFile.cpp +++ b/clang/lib/Frontend/DependencyFile.cpp @@ -66,9 +66,9 @@ struct DepCollectorPPCallbacks : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override { if (!File) DepCollector.maybeAddDependency(FileName, /*FromModule*/false, diff --git a/clang/lib/Frontend/DependencyGraph.cpp b/clang/lib/Frontend/DependencyGraph.cpp index 4bed4e2d44039..4cbdb3d5eeb89 100644 --- a/clang/lib/Frontend/DependencyGraph.cpp +++ b/clang/lib/Frontend/DependencyGraph.cpp @@ -29,9 +29,9 @@ class DependencyGraphCallback : public PPCallbacks { const Preprocessor *PP; std::string OutputFile; std::string SysRoot; - llvm::SetVector<const FileEntry *> AllFiles; - typedef llvm::DenseMap<const FileEntry *, - SmallVector<const FileEntry *, 2> > DependencyMap; + llvm::SetVector<FileEntryRef> AllFiles; + using DependencyMap = + llvm::DenseMap<FileEntryRef, SmallVector<FileEntryRef, 2>>; DependencyMap Dependencies; @@ -47,9 +47,9 @@ class DependencyGraphCallback : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; void EndOfMainFile() override { @@ -71,7 +71,7 @@ void DependencyGraphCallback::InclusionDirective( StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, - const FileEntry *File, + Optional<FileEntryRef> File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, @@ -80,15 +80,15 @@ void DependencyGraphCallback::InclusionDirective( return; SourceManager &SM = PP->getSourceManager(); - const FileEntry *FromFile - = SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(HashLoc))); + Optional<FileEntryRef> FromFile = + SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(HashLoc))); if (!FromFile) return; - Dependencies[FromFile].push_back(File); + Dependencies[*FromFile].push_back(*File); - AllFiles.insert(File); - AllFiles.insert(FromFile); + AllFiles.insert(*File); + AllFiles.insert(*FromFile); } raw_ostream & @@ -115,7 +115,7 @@ void DependencyGraphCallback::OutputGraphFile() { OS.indent(2); writeNodeReference(OS, AllFiles[I]); OS << " [ shape=\"box\", label=\""; - StringRef FileName = AllFiles[I]->getName(); + StringRef FileName = AllFiles[I].getName(); if (FileName.startswith(SysRoot)) FileName = FileName.substr(SysRoot.size()); diff --git a/clang/lib/Frontend/ModuleDependencyCollector.cpp b/clang/lib/Frontend/ModuleDependencyCollector.cpp index 4301e49f1d80d..7e19ed3d56e50 100644 --- a/clang/lib/Frontend/ModuleDependencyCollector.cpp +++ b/clang/lib/Frontend/ModuleDependencyCollector.cpp @@ -47,9 +47,9 @@ struct ModuleDependencyPPCallbacks : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override { if (!File) return; diff --git a/clang/lib/Frontend/PrecompiledPreamble.cpp b/clang/lib/Frontend/PrecompiledPreamble.cpp index 8aa80a4c96fb4..f29cb038c2080 100644 --- a/clang/lib/Frontend/PrecompiledPreamble.cpp +++ b/clang/lib/Frontend/PrecompiledPreamble.cpp @@ -99,13 +99,13 @@ class MissingFileCollector : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override { - // File is null if it wasn't found. + // File is None if it wasn't found. // (We have some false negatives if PP recovered e.g. <foo> -> "foo") - if (File != nullptr) + if (File) return; // If it's a rare absolute include, we know the full path already. diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp b/clang/lib/Frontend/PrintPreprocessedOutput.cpp index 7737d043043d5..e3bd7178aefee 100644 --- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp @@ -145,9 +145,9 @@ class PrintPPOutputPPCallbacks : public PPCallbacks { FileID PrevFID) override; void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; void Ident(SourceLocation Loc, StringRef str) override; void PragmaMessage(SourceLocation Loc, StringRef Namespace, @@ -394,7 +394,7 @@ void PrintPPOutputPPCallbacks::InclusionDirective( StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, - const FileEntry *File, + Optional<FileEntryRef> File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp index 3e8d582f90c27..9141adec58c48 100644 --- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp +++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp @@ -72,9 +72,9 @@ class InclusionRewriter : public PPCallbacks { SrcMgr::CharacteristicKind FileType) override; void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; void If(SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind ConditionValue) override; @@ -186,7 +186,7 @@ void InclusionRewriter::InclusionDirective(SourceLocation HashLoc, StringRef /*FileName*/, bool /*IsAngled*/, CharSourceRange /*FilenameRange*/, - const FileEntry * /*File*/, + Optional<FileEntryRef> /*File*/, StringRef /*SearchPath*/, StringRef /*RelativePath*/, const Module *Imported, diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 435e493dc37d0..fbc9466e887f1 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -2192,11 +2192,11 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport( if (Callbacks && !IsImportDecl) { // Notify the callback object that we've seen an inclusion directive. // FIXME: Use a diff erent callback for a pp-import? - Callbacks->InclusionDirective( - HashLoc, IncludeTok, LookupFilename, isAngled, FilenameRange, - File ? &File->getFileEntry() : nullptr, SearchPath, RelativePath, - Action == Import ? SuggestedModule.getModule() : nullptr, - FileCharacter); + Callbacks->InclusionDirective(HashLoc, IncludeTok, LookupFilename, isAngled, + FilenameRange, File, SearchPath, RelativePath, + Action == Import ? SuggestedModule.getModule() + : nullptr, + FileCharacter); if (Action == Skip && File) Callbacks->FileSkipped(*File, FilenameTok, FileCharacter); } diff --git a/clang/lib/Lex/PreprocessingRecord.cpp b/clang/lib/Lex/PreprocessingRecord.cpp index ed59dbdf018dc..432068b35f19c 100644 --- a/clang/lib/Lex/PreprocessingRecord.cpp +++ b/clang/lib/Lex/PreprocessingRecord.cpp @@ -42,7 +42,8 @@ ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() = InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec, InclusionKind Kind, StringRef FileName, bool InQuotes, bool ImportedModule, - const FileEntry *File, SourceRange Range) + Optional<FileEntryRef> File, + SourceRange Range) : PreprocessingDirective(InclusionDirectiveKind, Range), InQuotes(InQuotes), Kind(Kind), ImportedModule(ImportedModule), File(File) { char *Memory = (char *)PPRec.Allocate(FileName.size() + 1, alignof(char)); @@ -480,7 +481,7 @@ void PreprocessingRecord::InclusionDirective( StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, - const FileEntry *File, + Optional<FileEntryRef> File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index 3a32027023328..aa9b4f149011b 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -6043,10 +6043,9 @@ PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { case PPD_INCLUSION_DIRECTIVE: { const char *FullFileNameStart = Blob.data() + Record[0]; StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); - const FileEntry *File = nullptr; + Optional<FileEntryRef> File; if (!FullFileName.empty()) - if (auto FE = PP.getFileManager().getFile(FullFileName)) - File = *FE; + File = PP.getFileManager().getOptionalFileRef(FullFileName); // FIXME: Stable encoding InclusionDirective::InclusionKind Kind diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp index d3a84bf8a8e47..21a422f8dbbae 100644 --- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp +++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp @@ -147,7 +147,7 @@ void ModuleDepCollectorPP::FileChanged(SourceLocation Loc, void ModuleDepCollectorPP::InclusionDirective( SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, - bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, + bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) { if (!File && !Imported) { diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index 0c7406a9002b9..9e15ca714ceac 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -8298,7 +8298,8 @@ CXFile clang_getIncludedFile(CXCursor cursor) { return nullptr; const InclusionDirective *ID = getCursorInclusionDirective(cursor); - return const_cast<FileEntry *>(ID->getFile()); + Optional<FileEntryRef> File = ID->getFile(); + return const_cast<FileEntry *>(File ? &File->getFileEntry() : nullptr); } unsigned clang_Cursor_getObjCPropertyAttributes(CXCursor C, unsigned reserved) { diff --git a/clang/tools/libclang/CXIndexDataConsumer.cpp b/clang/tools/libclang/CXIndexDataConsumer.cpp index 2f892fd1a43f5..fa40687495ca1 100644 --- a/clang/tools/libclang/CXIndexDataConsumer.cpp +++ b/clang/tools/libclang/CXIndexDataConsumer.cpp @@ -459,20 +459,22 @@ void CXIndexDataConsumer::enteredMainFile(const FileEntry *File) { void CXIndexDataConsumer::ppIncludedFile(SourceLocation hashLoc, StringRef filename, - const FileEntry *File, + Optional<FileEntryRef> File, bool isImport, bool isAngled, bool isModuleImport) { if (!CB.ppIncludedFile) return; + const FileEntry *FE = File ? &File->getFileEntry() : nullptr; + ScratchAlloc SA(*this); CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc), SA.toCStr(filename), static_cast<CXFile>( - const_cast<FileEntry *>(File)), + const_cast<FileEntry *>(FE)), isImport, isAngled, isModuleImport }; CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info); - FileMap[File] = idxFile; + FileMap[FE] = idxFile; } void CXIndexDataConsumer::importedModule(const ImportDecl *ImportD) { diff --git a/clang/tools/libclang/CXIndexDataConsumer.h b/clang/tools/libclang/CXIndexDataConsumer.h index 8e6045dbf6bc8..f93b06c8d0c22 100644 --- a/clang/tools/libclang/CXIndexDataConsumer.h +++ b/clang/tools/libclang/CXIndexDataConsumer.h @@ -362,9 +362,9 @@ class CXIndexDataConsumer : public index::IndexDataConsumer { void enteredMainFile(const FileEntry *File); - void ppIncludedFile(SourceLocation hashLoc, - StringRef filename, const FileEntry *File, - bool isImport, bool isAngled, bool isModuleImport); + void ppIncludedFile(SourceLocation hashLoc, StringRef filename, + Optional<FileEntryRef> File, bool isImport, bool isAngled, + bool isModuleImport); void importedModule(const ImportDecl *ImportD); void importedPCH(const FileEntry *File); diff --git a/clang/tools/libclang/Indexing.cpp b/clang/tools/libclang/Indexing.cpp index 0e83ec6ca7993..5e0c5e01d9335 100644 --- a/clang/tools/libclang/Indexing.cpp +++ b/clang/tools/libclang/Indexing.cpp @@ -261,9 +261,9 @@ class IndexPPCallbacks : public PPCallbacks { void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override { bool isImport = (IncludeTok.is(tok::identifier) && IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import); diff --git a/clang/unittests/Lex/PPCallbacksTest.cpp b/clang/unittests/Lex/PPCallbacksTest.cpp index bb4098a1f98bd..43f7b4a8dca5b 100644 --- a/clang/unittests/Lex/PPCallbacksTest.cpp +++ b/clang/unittests/Lex/PPCallbacksTest.cpp @@ -35,9 +35,9 @@ class InclusionDirectiveCallbacks : public PPCallbacks { public: void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override { this->HashLoc = HashLoc; this->IncludeTok = IncludeTok; @@ -56,7 +56,7 @@ class InclusionDirectiveCallbacks : public PPCallbacks { SmallString<16> FileName; bool IsAngled; CharSourceRange FilenameRange; - const FileEntry* File; + Optional<FileEntryRef> File; SmallString<16> SearchPath; SmallString<16> RelativePath; const Module* Imported; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits