Skip to content

Commit

Permalink
[clang] Convert OptionalFileEntryRefDegradesToFileEntryPtr to std::op…
Browse files Browse the repository at this point in the history
…tional
  • Loading branch information
Krzysztof Parzyszek committed Dec 17, 2022
1 parent 1d43966 commit 8f0df9f
Show file tree
Hide file tree
Showing 70 changed files with 355 additions and 250 deletions.
5 changes: 3 additions & 2 deletions clang-tools-extra/clang-move/Move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "clang/Tooling/Core/Replacement.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Path.h"
#include <optional>

#define DEBUG_TYPE "clang-move"

Expand Down Expand Up @@ -131,8 +132,8 @@ class FindAllIncludes : public PPCallbacks {
void InclusionDirective(SourceLocation HashLoc, const Token & /*IncludeTok*/,
StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange,
Optional<FileEntryRef> /*File*/, StringRef SearchPath,
StringRef /*RelativePath*/,
std::optional<FileEntryRef> /*File*/,
StringRef SearchPath, StringRef /*RelativePath*/,
const Module * /*Imported*/,
SrcMgr::CharacteristicKind /*FileType*/) override {
if (const auto *FileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Serialization/ASTReader.h"
#include <optional>

#define DEBUG_TYPE "clang-tidy"

Expand Down Expand Up @@ -162,7 +163,7 @@ void ExpandModularHeadersPPCallbacks::FileChanged(
void ExpandModularHeadersPPCallbacks::InclusionDirective(
SourceLocation DirectiveLoc, const Token &IncludeToken,
StringRef IncludedFilename, bool IsAngled, CharSourceRange FilenameRange,
Optional<FileEntryRef> IncludedFile, StringRef SearchPath,
std::optional<FileEntryRef> IncludedFile, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {
if (Imported) {
Expand Down Expand Up @@ -224,7 +225,8 @@ void ExpandModularHeadersPPCallbacks::PragmaDiagnostic(SourceLocation Loc,
parseToLocation(Loc);
}
void ExpandModularHeadersPPCallbacks::HasInclude(SourceLocation Loc, StringRef,
bool, Optional<FileEntryRef>,
bool,
std::optional<FileEntryRef>,
SrcMgr::CharacteristicKind) {
parseToLocation(Loc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/DenseSet.h"
#include <optional>

namespace llvm {
namespace vfs {
Expand Down Expand Up @@ -69,7 +70,7 @@ class ExpandModularHeadersPPCallbacks : public PPCallbacks {
void InclusionDirective(SourceLocation DirectiveLoc,
const Token &IncludeToken, StringRef IncludedFilename,
bool IsAngled, CharSourceRange FilenameRange,
Optional<FileEntryRef> IncludedFile,
std::optional<FileEntryRef> IncludedFile,
StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
Expand All @@ -91,7 +92,8 @@ class ExpandModularHeadersPPCallbacks : public PPCallbacks {
void PragmaDiagnosticPop(SourceLocation Loc, StringRef) override;
void PragmaDiagnostic(SourceLocation Loc, StringRef, diag::Severity,
StringRef) override;
void HasInclude(SourceLocation Loc, StringRef, bool, Optional<FileEntryRef> ,
void HasInclude(SourceLocation Loc, StringRef, bool,
std::optional<FileEntryRef>,
SrcMgr::CharacteristicKind) override;
void PragmaOpenCLExtension(SourceLocation NameLoc, const IdentifierInfo *,
SourceLocation StateLoc, unsigned) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
#include <optional>
#include <string>
#include <vector>

Expand All @@ -30,8 +31,9 @@ class KernelNameRestrictionPPCallbacks : public PPCallbacks {
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
CharSourceRange FileNameRange,
Optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
std::optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;

void EndOfMainFile() override;
Expand Down Expand Up @@ -62,7 +64,7 @@ void KernelNameRestrictionCheck::registerPPCallbacks(const SourceManager &SM,

void KernelNameRestrictionPPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &, StringRef FileName, bool,
CharSourceRange, Optional<FileEntryRef>, StringRef, StringRef,
CharSourceRange, std::optional<FileEntryRef>, StringRef, StringRef,
const Module *, SrcMgr::CharacteristicKind) {
IncludeDirective ID = {HashLoc, FileName};
IncludeDirectives.push_back(std::move(ID));
Expand Down
13 changes: 8 additions & 5 deletions clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "SuspiciousIncludeCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/Lex/Preprocessor.h"
#include <optional>

namespace clang {
namespace tidy {
Expand All @@ -25,8 +26,9 @@ class SuspiciousIncludePPCallbacks : public PPCallbacks {
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange,
Optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
std::optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;

private:
Expand Down Expand Up @@ -72,8 +74,9 @@ void SuspiciousIncludeCheck::registerPPCallbacks(

void SuspiciousIncludePPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
bool IsAngled, CharSourceRange FilenameRange,
std::optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {
if (IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import)
return;
Expand All @@ -93,7 +96,7 @@ void SuspiciousIncludePPCallbacks::InclusionDirective(
llvm::sys::path::replace_extension(GuessedFileName,
(HFE.size() ? "." : "") + HFE);

Optional<FileEntryRef> File =
std::optional<FileEntryRef> File =
PP->LookupFile(DiagLoc, GuessedFileName, IsAngled, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
if (File) {
Expand Down
11 changes: 7 additions & 4 deletions clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "llvm/ADT/STLExtras.h"

#include <map>
#include <optional>

namespace clang {
namespace tidy {
Expand All @@ -28,8 +29,9 @@ class IncludeOrderPPCallbacks : public PPCallbacks {
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange,
Optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
std::optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
void EndOfMainFile() override;

Expand Down Expand Up @@ -82,8 +84,9 @@ static int getPriority(StringRef Filename, bool IsAngled, bool IsMainModule) {

void IncludeOrderPPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
bool IsAngled, CharSourceRange FilenameRange,
std::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
// to leave it in the top position.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/HeaderSearchOptions.h"
#include "clang/Lex/Preprocessor.h"
#include <optional>

// FixItHint - Hint to check documentation script to mark this check as
// providing a FixIt.
Expand All @@ -34,8 +35,9 @@ class RestrictedIncludesPPCallbacks
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange,
Optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
std::optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;

private:
Expand All @@ -46,8 +48,9 @@ class RestrictedIncludesPPCallbacks

void RestrictedIncludesPPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
bool IsAngled, CharSourceRange FilenameRange,
std::optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {
// Compiler provided headers are allowed (e.g stddef.h).
if (SrcMgr::isSystem(FileType) && SearchPath == CompilerIncudeDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "llvm/ADT/StringSet.h"

#include <algorithm>
#include <optional>
#include <vector>

using IncludeMarker =
Expand All @@ -33,8 +34,9 @@ class IncludeModernizePPCallbacks : public PPCallbacks {
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange,
Optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
std::optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;

private:
Expand Down Expand Up @@ -179,8 +181,9 @@ IncludeModernizePPCallbacks::IncludeModernizePPCallbacks(

void IncludeModernizePPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
bool IsAngled, CharSourceRange FilenameRange,
std::optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {

// If we don't want to warn for non-main file reports and this is one, skip
Expand Down
6 changes: 4 additions & 2 deletions clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <algorithm>
#include <cassert>
#include <cctype>
#include <optional>
#include <string>

namespace clang {
Expand Down Expand Up @@ -119,8 +120,9 @@ class MacroToEnumCallbacks : public PPCallbacks {
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange,
Optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
std::optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
SrcMgr::CharacteristicKind FileType) override {
clearCurrentEnum(HashLoc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Path.h"
#include <cstring>
#include <optional>

namespace clang {
namespace tidy {
namespace portability {

void RestrictedIncludesPPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
bool IsAngled, CharSourceRange FilenameRange,
std::optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {
if (!Check.contains(FileName) && SrcMgr::isSystem(FileType)) {
SmallString<256> FullPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "../ClangTidyCheck.h"
#include "../GlobList.h"
#include "clang/Lex/PPCallbacks.h"
#include <optional>

namespace clang {
namespace tidy {
Expand Down Expand Up @@ -51,8 +52,9 @@ class RestrictedIncludesPPCallbacks : public PPCallbacks {
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange,
Optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
std::optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
void EndOfMainFile() override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include <memory>
#include <optional>

namespace clang {
namespace tidy {
Expand Down Expand Up @@ -48,8 +49,9 @@ class DuplicateIncludeCallbacks : public PPCallbacks {
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange,
Optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
std::optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath,
const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;

void MacroDefined(const Token &MacroNameTok,
Expand Down Expand Up @@ -77,8 +79,9 @@ void DuplicateIncludeCallbacks::FileChanged(SourceLocation Loc,

void DuplicateIncludeCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
bool IsAngled, CharSourceRange FilenameRange,
std::optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {
if (llvm::is_contained(Files.back(), FileName)) {
// We want to delete the entire line, so make sure that [Start,End] covers
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/Token.h"
#include <optional>

namespace clang {
namespace tidy {
Expand All @@ -24,7 +25,7 @@ class IncludeInserterCallback : public PPCallbacks {
void InclusionDirective(SourceLocation HashLocation,
const Token &IncludeToken, StringRef FileNameRef,
bool IsAngled, CharSourceRange FileNameRange,
Optional<FileEntryRef> /*IncludedFile*/,
std::optional<FileEntryRef> /*IncludedFile*/,
StringRef /*SearchPath*/, StringRef /*RelativePath*/,
const Module * /*ImportedModule*/,
SrcMgr::CharacteristicKind /*FileType*/) override {
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/clangd/Headers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Path.h"
#include <cstring>
#include <optional>

namespace clang {
namespace clangd {
Expand All @@ -35,7 +36,7 @@ class IncludeStructure::RecordHeaders : public PPCallbacks,
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
llvm::StringRef FileName, bool IsAngled,
CharSourceRange /*FilenameRange*/,
Optional<FileEntryRef> File,
std::optional<FileEntryRef> File,
llvm::StringRef /*SearchPath*/,
llvm::StringRef /*RelativePath*/,
const clang::Module * /*Imported*/,
Expand Down
6 changes: 4 additions & 2 deletions clang-tools-extra/clangd/ParsedAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "llvm/ADT/StringRef.h"
#include <algorithm>
#include <memory>
#include <optional>
#include <vector>

// Force the linker to link in Clang-tidy modules.
Expand Down Expand Up @@ -171,9 +172,10 @@ class ReplayPreamble : private PPCallbacks {

void replay() {
for (const auto &Inc : Includes) {
llvm::Optional<FileEntryRef> File;
std::optional<FileEntryRef> File;
if (Inc.Resolved != "")
File = expectedToOptional(SM.getFileManager().getFileRef(Inc.Resolved));
File =
expectedToStdOptional(SM.getFileManager().getFileRef(Inc.Resolved));

// Re-lex the #include directive to find its interesting parts.
auto HashLoc = SM.getComposedLoc(SM.getMainFileID(), Inc.HashOffset);
Expand Down
Loading

0 comments on commit 8f0df9f

Please sign in to comment.